]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
Fix signal/slot
[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 KUrl fileUrl;
60
61 Nepomuk::KMetaData::Resource file;
62
63 QTextEdit* editComment;
64 KRatingWidget* ratingWidget;
65 Nepomuk::KMetaData::TagWidget* tagWidget;
66 #endif
67 };
68
69 #ifdef HAVE_KMETADATA
70 void MetaDataWidget::Private::loadComment(const QString& comment)
71 {
72 editComment->blockSignals(true);
73 if (comment.isEmpty()) {
74 editComment->setFontItalic(true);
75 editComment->setText(i18n("Click to add comment..."));
76 } else {
77 editComment->setFontItalic(false);
78 editComment->setText(comment);
79 }
80 editComment->blockSignals(false);
81 }
82 #endif
83
84
85 MetaDataWidget::MetaDataWidget(QWidget* parent) :
86 QWidget(parent)
87 {
88 #ifdef HAVE_KMETADATA
89 d = new Private;
90 d->editComment = new QTextEdit(this);
91 d->ratingWidget = new KRatingWidget(this);
92 d->tagWidget = new Nepomuk::KMetaData::TagWidget(this);
93 connect(d->ratingWidget, SIGNAL(ratingChanged(int)), this, SLOT(slotRatingChanged(int)));
94 connect(d->editComment, SIGNAL(textChanged()), this, SLOT(slotCommentChanged()));
95
96 QVBoxLayout* lay = new QVBoxLayout(this);
97 lay->setMargin(0);
98 QHBoxLayout* hbox = new QHBoxLayout;
99 hbox->addWidget(new QLabel(i18n("Rating:"), this));
100 hbox->addStretch(1);
101 hbox->addWidget(d->ratingWidget);
102 lay->addLayout(hbox);
103 lay->addWidget(d->editComment);
104 hbox = new QHBoxLayout;
105 hbox->addWidget(new QLabel(i18n("Tags:"), this));
106 hbox->addWidget(d->tagWidget, 1);
107 lay->addLayout(hbox);
108
109 d->editComment->installEventFilter(this);
110 d->editComment->viewport()->installEventFilter(this);
111 #else
112 d = 0;
113 #endif
114 }
115
116
117 MetaDataWidget::~MetaDataWidget()
118 {
119 delete d;
120 }
121
122
123 void MetaDataWidget::setFile(const KUrl& url)
124 {
125 #ifdef HAVE_KMETADATA
126 // FIXME: replace with KMetaData::File once we have it again
127 d->fileUrl = url;
128 d->file = Nepomuk::KMetaData::Resource(url.url(), s_nfoFile);
129 d->ratingWidget->setRating(d->file.rating());
130 d->tagWidget->setTaggedResource(d->file);
131 d->loadComment(d->file.description());
132 #endif
133 }
134
135
136 void MetaDataWidget::setFiles(const KUrl::List& urls)
137 {
138 #ifdef HAVE_KMETADATA
139 // FIXME: support multiple files
140 setFile(urls.first());
141 #endif
142 }
143
144
145 void MetaDataWidget::slotCommentChanged()
146 {
147 #ifdef HAVE_KMETADATA
148 if (d->editComment->toPlainText() != d->file.description()) {
149 // d->file.setLocation(url.url());
150 d->file.setProperty(s_nfoFileUrl, d->fileUrl.url());
151 d->file.setDescription(d->editComment->toPlainText());
152 }
153 #endif
154 }
155
156
157 void MetaDataWidget::slotRatingChanged(int rating)
158 {
159 #ifdef HAVE_KMETADATA
160 if (rating != d->file.rating()) {
161 // d->file.setLocation(url.url());
162 d->file.setProperty(s_nfoFileUrl, d->fileUrl.url());
163 d->file.setRating(rating);
164 }
165 #endif
166 }
167
168
169 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
170 {
171 #ifdef HAVE_KMETADATA
172 if (obj == d->editComment->viewport() || obj == d->editComment) {
173 if (event->type() == QEvent::FocusOut) {
174 // make sure the info text is displayed again
175 d->loadComment(d->editComment->toPlainText());
176 } else if (event->type() == QEvent::FocusIn) {
177 d->editComment->setFontItalic(false);
178 if (d->file.description().isEmpty()) {
179 d->editComment->setText(QString());
180 }
181 }
182 }
183 #endif
184
185 return QWidget::eventFilter(obj, event);
186 }
187
188 #include "metadatawidget.moc"