]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
Remove not necessary "class classname;"
[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-kmetadata.h>
23
24 #include <klocale.h>
25
26 #include <QtGui/QLabel>
27 #include <QtGui/QGridLayout>
28 #include <QtGui/QTextEdit>
29
30 #ifdef HAVE_KMETADATA
31 #include <kmetadata/kmetadatatagwidget.h>
32 #include <kmetadata/resourcemanager.h>
33 #include <kmetadata/resource.h>
34 #include <kmetadata/variant.h>
35 #include <kmetadata/kratingwidget.h>
36 #endif
37
38 // FIXME: these should be replaced by using KMetaData::File once it is available again
39 static const char* s_nfoFile = "http://ont.semanticdesktop.org/2007/03/22/nfo#File";
40 static const char* s_nfoFileUrl = "http://ont.semanticdesktop.org/2007/03/22/nfo#fileUrl";
41
42
43 bool MetaDataWidget::metaDataAvailable()
44 {
45 #ifdef HAVE_KMETADATA
46 return !Nepomuk::KMetaData::ResourceManager::instance()->init();
47 #else
48 return false;
49 #endif
50 }
51
52
53 class MetaDataWidget::Private
54 {
55 public:
56 #ifdef HAVE_KMETADATA
57 void loadComment(const QString& comment);
58
59 QMap<KUrl, Nepomuk::KMetaData::Resource> files;
60
61 QTextEdit* editComment;
62 KRatingWidget* ratingWidget;
63 Nepomuk::KMetaData::TagWidget* tagWidget;
64 #endif
65 };
66
67 #ifdef HAVE_KMETADATA
68 void MetaDataWidget::Private::loadComment(const QString& comment)
69 {
70 editComment->blockSignals(true);
71 if (comment.isEmpty()) {
72 editComment->setFontItalic(true);
73 editComment->setText(i18n("Click to add comment..."));
74 } else {
75 editComment->setFontItalic(false);
76 editComment->setText(comment);
77 }
78 editComment->blockSignals(false);
79 }
80 #endif
81
82
83 MetaDataWidget::MetaDataWidget(QWidget* parent) :
84 QWidget(parent)
85 {
86 #ifdef HAVE_KMETADATA
87 d = new Private;
88 d->editComment = new QTextEdit(this);
89 d->ratingWidget = new KRatingWidget(this);
90 d->tagWidget = new Nepomuk::KMetaData::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(i18n("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(i18n("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_KMETADATA
132 // FIXME: replace with KMetaData::File once we have it again
133 d->files.clear();
134 bool first = true;
135 QList<Nepomuk::KMetaData::Resource> fileRes;
136 Q_FOREACH( KUrl url, urls ) {
137 Nepomuk::KMetaData::Resource file( url.url(), s_nfoFile );
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_KMETADATA
167 for ( QMap<KUrl, Nepomuk::KMetaData::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_KMETADATA
178 for ( QMap<KUrl, Nepomuk::KMetaData::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_KMETADATA
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"