]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
KDE 4.1 requires Qt4.4 -> remove the #ifdefs...
[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: replace with KMetaData::File once we have it again
133 d->files.clear();
134 bool first = true;
135 QList<Nepomuk::Resource> fileRes;
136 Q_FOREACH( KUrl url, urls ) {
137 Nepomuk::Resource file( url.url(), Soprano::Vocabulary::Xesam::File() );
138 // file.setLocation(url.url());
139 d->files.insert( url, file );
140 fileRes.append( file );
141
142 if ( !first &&
143 d->ratingWidget->rating() != file.rating() ) {
144 d->ratingWidget->setRating( 0 ); // reset rating
145 }
146 else if ( first ) {
147 d->ratingWidget->setRating( file.rating() );
148 }
149
150 if ( !first &&
151 d->editComment->toPlainText() != file.description() ) {
152 d->loadComment( QString() );
153 }
154 else if ( first ) {
155 d->loadComment( file.description() );
156 }
157 first = false;
158 }
159 d->tagWidget->setTaggedResources(fileRes);
160 #endif
161 }
162
163
164 void MetaDataWidget::slotCommentChanged()
165 {
166 #ifdef HAVE_NEPOMUK
167 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
168 it != d->files.end(); ++it ) {
169 it.value().setDescription(d->editComment->toPlainText());
170 }
171 #endif
172 }
173
174
175 void MetaDataWidget::slotRatingChanged(unsigned int rating)
176 {
177 #ifdef HAVE_NEPOMUK
178 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
179 it != d->files.end(); ++it ) {
180 it.value().setRating(rating);
181 }
182 #endif
183 }
184
185
186 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
187 {
188 #ifdef HAVE_NEPOMUK
189 if (obj == d->editComment->viewport() || obj == d->editComment) {
190 if (event->type() == QEvent::FocusOut) {
191 // make sure the info text is displayed again
192 d->loadComment(d->editComment->toPlainText());
193 } else if (event->type() == QEvent::FocusIn) {
194 d->editComment->setFontItalic(false);
195 if (!d->files.isEmpty() && d->files.begin().value().description().isEmpty()) {
196 d->editComment->setText(QString());
197 }
198 }
199 }
200 #endif
201
202 return QWidget::eventFilter(obj, event);
203 }
204
205 #include "metadatawidget.moc"