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