]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
Allow turning off the feature that additional information might be shown in the icons...
[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 #endif
39
40
41 bool MetaDataWidget::metaDataAvailable()
42 {
43 #ifdef HAVE_NEPOMUK
44 return !Nepomuk::ResourceManager::instance()->init();
45 #else
46 return false;
47 #endif
48 }
49
50
51 class MetaDataWidget::Private
52 {
53 public:
54 #ifdef HAVE_NEPOMUK
55 void loadComment(const QString& comment);
56
57 QMap<KUrl, Nepomuk::Resource> files;
58
59 QTextEdit* editComment;
60 KRatingWidget* ratingWidget;
61 Nepomuk::TagWidget* tagWidget;
62 #endif
63 };
64
65 #ifdef HAVE_NEPOMUK
66 void MetaDataWidget::Private::loadComment(const QString& comment)
67 {
68 editComment->blockSignals(true);
69 if (comment.isEmpty()) {
70 editComment->setFontItalic(true);
71 editComment->setText(i18nc("@info:tooltip", "Click to add comment..."));
72 } else {
73 editComment->setFontItalic(false);
74 editComment->setText(comment);
75 }
76 editComment->blockSignals(false);
77 }
78 #endif
79
80
81 MetaDataWidget::MetaDataWidget(QWidget* parent) :
82 QWidget(parent)
83 {
84 #ifdef HAVE_NEPOMUK
85 d = new Private;
86 d->editComment = new QTextEdit(this);
87 d->ratingWidget = new KRatingWidget(this);
88 d->tagWidget = new Nepomuk::TagWidget(this);
89 connect(d->ratingWidget, SIGNAL(ratingChanged(unsigned int)), this, SLOT(slotRatingChanged(unsigned int)));
90 connect(d->editComment, SIGNAL(textChanged()), this, SLOT(slotCommentChanged()));
91
92 QVBoxLayout* lay = new QVBoxLayout(this);
93 lay->setMargin(0);
94 QHBoxLayout* hbox = new QHBoxLayout;
95 hbox->addWidget(new QLabel(i18nc("@label:slider", "Rating:"), this));
96 hbox->addStretch(1);
97 hbox->addWidget(d->ratingWidget);
98 lay->addLayout(hbox);
99 lay->addWidget(d->editComment);
100 hbox = new QHBoxLayout;
101 hbox->addWidget(new QLabel(i18nc("@label:textbox", "Tags:"), this));
102 hbox->addWidget(d->tagWidget, 1);
103 lay->addLayout(hbox);
104
105 d->editComment->installEventFilter(this);
106 d->editComment->viewport()->installEventFilter(this);
107 #else
108 d = 0;
109 #endif
110 }
111
112
113 MetaDataWidget::~MetaDataWidget()
114 {
115 delete d;
116 }
117
118
119 void MetaDataWidget::setFile(const KUrl& url)
120 {
121 KUrl::List urls;
122 urls.append( url );
123 setFiles( urls );
124 }
125
126
127 void MetaDataWidget::setFiles(const KUrl::List& urls)
128 {
129 #ifdef HAVE_NEPOMUK
130 // FIXME: replace with KMetaData::File once we have it again
131 d->files.clear();
132 bool first = true;
133 QList<Nepomuk::Resource> fileRes;
134 Q_FOREACH( KUrl url, urls ) {
135 Nepomuk::Resource file( url.url(), Nepomuk::NFO::File() );
136 // file.setLocation(url.url());
137 d->files.insert( url, file );
138 fileRes.append( file );
139
140 if ( !first &&
141 d->ratingWidget->rating() != file.rating() ) {
142 d->ratingWidget->setRating( 0 ); // reset rating
143 }
144 else if ( first ) {
145 d->ratingWidget->setRating( file.rating() );
146 }
147
148 if ( !first &&
149 d->editComment->toPlainText() != file.description() ) {
150 d->loadComment( QString() );
151 }
152 else if ( first ) {
153 d->loadComment( file.description() );
154 }
155 first = false;
156 }
157 d->tagWidget->setTaggedResources(fileRes);
158 #endif
159 }
160
161
162 void MetaDataWidget::slotCommentChanged()
163 {
164 #ifdef HAVE_NEPOMUK
165 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
166 it != d->files.end(); ++it ) {
167 it.value().setDescription(d->editComment->toPlainText());
168 }
169 #endif
170 }
171
172
173 void MetaDataWidget::slotRatingChanged(unsigned int rating)
174 {
175 #ifdef HAVE_NEPOMUK
176 for ( QMap<KUrl, Nepomuk::Resource>::iterator it = d->files.begin();
177 it != d->files.end(); ++it ) {
178 it.value().setRating(rating);
179 }
180 #endif
181 }
182
183
184 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
185 {
186 #ifdef HAVE_NEPOMUK
187 if (obj == d->editComment->viewport() || obj == d->editComment) {
188 if (event->type() == QEvent::FocusOut) {
189 // make sure the info text is displayed again
190 d->loadComment(d->editComment->toPlainText());
191 } else if (event->type() == QEvent::FocusIn) {
192 d->editComment->setFontItalic(false);
193 if (!d->files.isEmpty() && d->files.begin().value().description().isEmpty()) {
194 d->editComment->setText(QString());
195 }
196 }
197 }
198 #endif
199
200 return QWidget::eventFilter(obj, event);
201 }
202
203 #include "metadatawidget.moc"