]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
As requested by Peter: upgrade version to 1.0
[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->ratingWidget = new KRatingWidget(this);
89 d->tagWidget = new Nepomuk::TagWidget(this);
90 connect(d->ratingWidget, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
91 connect(d->editComment, SIGNAL(textChanged()), this, SLOT(slotCommentChanged()));
92
93 QVBoxLayout* lay = new QVBoxLayout(this);
94 lay->setMargin(0);
95 QHBoxLayout* hbox = new QHBoxLayout;
96 hbox->addWidget(new QLabel(i18nc("@label:slider", "Rating:"), this));
97 hbox->addStretch(1);
98 hbox->addWidget(d->ratingWidget);
99 lay->addLayout(hbox);
100 lay->addWidget(d->editComment);
101 hbox = new QHBoxLayout;
102 hbox->addWidget(new QLabel(i18nc("@label:textbox", "Tags:"), this));
103 hbox->addWidget(d->tagWidget, 1);
104 lay->addLayout(hbox);
105
106 d->editComment->installEventFilter(this);
107 d->editComment->viewport()->installEventFilter(this);
108 #else
109 d = 0;
110 #endif
111 }
112
113
114 MetaDataWidget::~MetaDataWidget()
115 {
116 delete d;
117 }
118
119
120 void MetaDataWidget::setFile(const KUrl& url)
121 {
122 KUrl::List urls;
123 urls.append( url );
124 setFiles( urls );
125 }
126
127
128 void MetaDataWidget::setFiles(const KUrl::List& urls)
129 {
130 #ifdef HAVE_NEPOMUK
131 // FIXME: replace with KMetaData::File once we have it again
132 d->files.clear();
133 bool first = true;
134 QList<Nepomuk::Resource> fileRes;
135 Q_FOREACH( KUrl url, urls ) {
136 Nepomuk::Resource file( url.url(), Soprano::Vocabulary::Xesam::File() );
137 // file.setLocation(url.url());
138 d->files.insert( url, file );
139 fileRes.append( file );
140
141 if ( !first &&
142 d->ratingWidget->rating() != file.rating() ) {
143 d->ratingWidget->setRating( 0 ); // reset rating
144 }
145 else if ( first ) {
146 d->ratingWidget->setRating( file.rating() );
147 }
148
149 if ( !first &&
150 d->editComment->toPlainText() != file.description() ) {
151 d->loadComment( QString() );
152 }
153 else if ( first ) {
154 d->loadComment( file.description() );
155 }
156 first = false;
157 }
158 d->tagWidget->setTaggedResources(fileRes);
159 #endif
160 }
161
162
163 void MetaDataWidget::slotCommentChanged()
164 {
165 #ifdef HAVE_NEPOMUK
166 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
167 it != d->files.end(); ++it ) {
168 it.value().setDescription(d->editComment->toPlainText());
169 }
170 #endif
171 }
172
173
174 void MetaDataWidget::slotRatingChanged(unsigned int rating)
175 {
176 #ifdef HAVE_NEPOMUK
177 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
178 it != d->files.end(); ++it ) {
179 it.value().setRating(rating);
180 }
181 #endif
182 }
183
184
185 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
186 {
187 #ifdef HAVE_NEPOMUK
188 if (obj == d->editComment->viewport() || obj == d->editComment) {
189 if (event->type() == QEvent::FocusOut) {
190 // make sure the info text is displayed again
191 d->loadComment(d->editComment->toPlainText());
192 } else if (event->type() == QEvent::FocusIn) {
193 d->editComment->setFontItalic(false);
194 if (!d->files.isEmpty() && d->files.begin().value().description().isEmpty()) {
195 d->editComment->setText(QString());
196 }
197 }
198 }
199 #endif
200
201 return QWidget::eventFilter(obj, event);
202 }
203
204 #include "metadatawidget.moc"