]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
fixed regression because of disconnecting non-available slots - now files don't get...
[dolphin.git] / src / metadatawidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Sebastian Trueg <trueg@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "metadatawidget.h"
21
22 #include <config-nepomuk.h>
23
24 #include <klocale.h>
25
26 #include <QtCore/QEvent>
27 #include <QtGui/QLabel>
28 #include <QtGui/QGridLayout>
29 #include <QtGui/QTextEdit>
30
31 #ifdef HAVE_NEPOMUK
32 #include <nepomuk/kmetadatatagwidget.h>
33 #include <nepomuk/resourcemanager.h>
34 #include <nepomuk/resource.h>
35 #include <nepomuk/variant.h>
36 #include <nepomuk/kratingwidget.h>
37 #include <nepomuk/global.h>
38 #include <Soprano/Vocabulary/Xesam>
39 #endif
40
41
42 bool MetaDataWidget::metaDataAvailable()
43 {
44 #ifdef HAVE_NEPOMUK
45 return !Nepomuk::ResourceManager::instance()->init();
46 #else
47 return false;
48 #endif
49 }
50
51
52 class MetaDataWidget::Private
53 {
54 public:
55 #ifdef HAVE_NEPOMUK
56 void loadComment(const QString& comment);
57
58 QMap<KUrl, Nepomuk::Resource> files;
59
60 QTextEdit* editComment;
61 KRatingWidget* ratingWidget;
62 Nepomuk::TagWidget* tagWidget;
63 #endif
64 };
65
66 #ifdef HAVE_NEPOMUK
67 void MetaDataWidget::Private::loadComment(const QString& comment)
68 {
69 editComment->blockSignals(true);
70 if (comment.isEmpty()) {
71 editComment->setFontItalic(true);
72 editComment->setText(i18nc("@info:tooltip", "Click to add comment..."));
73 } else {
74 editComment->setFontItalic(false);
75 editComment->setText(comment);
76 }
77 editComment->blockSignals(false);
78 }
79 #endif
80
81
82 MetaDataWidget::MetaDataWidget(QWidget* parent) :
83 QWidget(parent)
84 {
85 #ifdef HAVE_NEPOMUK
86 d = new Private;
87 d->editComment = new QTextEdit(this);
88 d->editComment->setFocusPolicy(Qt::ClickFocus);
89 d->ratingWidget = new KRatingWidget(this);
90 d->tagWidget = new Nepomuk::TagWidget(this);
91 connect(d->ratingWidget, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
92 connect(d->editComment, SIGNAL(textChanged()), this, SLOT(slotCommentChanged()));
93
94 QVBoxLayout* lay = new QVBoxLayout(this);
95 lay->setMargin(0);
96 QHBoxLayout* hbox = new QHBoxLayout;
97 hbox->addWidget(new QLabel(i18nc("@label:slider", "Rating:"), this));
98 hbox->addStretch(1);
99 hbox->addWidget(d->ratingWidget);
100 lay->addLayout(hbox);
101 lay->addWidget(d->editComment);
102 hbox = new QHBoxLayout;
103 hbox->addWidget(new QLabel(i18nc("@label:textbox", "Tags:"), this));
104 hbox->addWidget(d->tagWidget, 1);
105 lay->addLayout(hbox);
106
107 d->editComment->installEventFilter(this);
108 d->editComment->viewport()->installEventFilter(this);
109 #else
110 d = 0;
111 #endif
112 }
113
114
115 MetaDataWidget::~MetaDataWidget()
116 {
117 delete d;
118 }
119
120
121 void MetaDataWidget::setFile(const KUrl& url)
122 {
123 KUrl::List urls;
124 urls.append( url );
125 setFiles( urls );
126 }
127
128
129 void MetaDataWidget::setFiles(const KUrl::List& urls)
130 {
131 #ifdef HAVE_NEPOMUK
132 // FIXME #1: For 100 files MetaDataWidget::setFiles() blocks
133 // for around 15 seconds (maybe we should delegate this to a KJob).
134 // In the meantime we temporary just reset the widgets:
135 d->ratingWidget->setRating( 0 );
136 d->loadComment( QString() );
137 return;
138
139 // FIXME #2: replace with KMetaData::File once we have it again
140 d->files.clear();
141 bool first = true;
142 QList<Nepomuk::Resource> fileRes;
143 Q_FOREACH( KUrl url, urls ) {
144 Nepomuk::Resource file( url.url(), Soprano::Vocabulary::Xesam::File() );
145 // file.setLocation(url.url());
146 d->files.insert( url, file );
147 fileRes.append( file );
148
149 if ( !first &&
150 d->ratingWidget->rating() != file.rating() ) {
151 d->ratingWidget->setRating( 0 ); // reset rating
152 }
153 else if ( first ) {
154 d->ratingWidget->setRating( (qint32)(file.rating()) );
155 }
156
157 if ( !first &&
158 d->editComment->toPlainText() != file.description() ) {
159 d->loadComment( QString() );
160 }
161 else if ( first ) {
162 d->loadComment( file.description() );
163 }
164 first = false;
165 }
166 d->tagWidget->setTaggedResources(fileRes);
167 #endif
168 }
169
170
171 void MetaDataWidget::slotCommentChanged()
172 {
173 #ifdef HAVE_NEPOMUK
174 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
175 it != d->files.end(); ++it ) {
176 it.value().setDescription(d->editComment->toPlainText());
177 }
178 #endif
179 }
180
181
182 void MetaDataWidget::slotRatingChanged(unsigned int rating)
183 {
184 #ifdef HAVE_NEPOMUK
185 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
186 it != d->files.end(); ++it ) {
187 it.value().setRating(rating);
188 }
189 #endif
190 }
191
192
193 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
194 {
195 #ifdef HAVE_NEPOMUK
196 if (obj == d->editComment->viewport() || obj == d->editComment) {
197 if (event->type() == QEvent::FocusOut) {
198 // make sure the info text is displayed again
199 d->loadComment(d->editComment->toPlainText());
200 } else if (event->type() == QEvent::FocusIn) {
201 d->editComment->setFontItalic(false);
202 if (!d->files.isEmpty() && d->files.begin().value().description().isEmpty()) {
203 d->editComment->setText(QString());
204 }
205 }
206 }
207 #endif
208
209 return QWidget::eventFilter(obj, event);
210 }
211
212 #include "metadatawidget.moc"