]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
minor coding guideline fixes
[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 void MetaDataWidget::Private::loadComment(const QString& comment)
70 {
71 editComment->blockSignals(true);
72 if (comment.isEmpty()) {
73 editComment->setFontItalic(true);
74 editComment->setText(i18n("Click to add comment..."));
75 } else {
76 editComment->setFontItalic(false);
77 editComment->setText(comment);
78 }
79 editComment->blockSignals(false);
80 }
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(int)), this, SLOT(slotRatingChanged(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 #ifdef HAVE_KMETADATA
124 // FIXME: replace with KMetaData::File once we have it again
125 d->fileUrl = url;
126 d->file = Nepomuk::KMetaData::Resource(url.url(), s_nfoFile);
127 d->ratingWidget->setRating(d->file.rating());
128 d->tagWidget->setTaggedResource(d->file);
129 d->loadComment(d->file.description());
130 #endif
131 }
132
133
134 void MetaDataWidget::setFiles(const KUrl::List& urls)
135 {
136 #ifdef HAVE_KMETADATA
137 // FIXME: support multiple files
138 setFile(urls.first());
139 #endif
140 }
141
142
143 void MetaDataWidget::slotCommentChanged()
144 {
145 #ifdef HAVE_KMETADATA
146 if (d->editComment->toPlainText() != d->file.description()) {
147 // d->file.setLocation(url.url());
148 d->file.setProperty(s_nfoFileUrl, d->fileUrl.url());
149 d->file.setDescription(d->editComment->toPlainText());
150 }
151 #endif
152 }
153
154
155 void MetaDataWidget::slotRatingChanged(int rating)
156 {
157 #ifdef HAVE_KMETADATA
158 if (rating != d->file.rating()) {
159 // d->file.setLocation(url.url());
160 d->file.setProperty(s_nfoFileUrl, d->fileUrl.url());
161 d->file.setRating(rating);
162 }
163 #endif
164 }
165
166
167 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
168 {
169 #ifdef HAVE_KMETADATA
170 if (obj == d->editComment->viewport() || obj == d->editComment) {
171 if (event->type() == QEvent::FocusOut) {
172 // make sure the info text is displayed again
173 d->loadComment(d->editComment->toPlainText());
174 } else if (event->type() == QEvent::FocusIn) {
175 d->editComment->setFontItalic(false);
176 if (d->file.description().isEmpty()) {
177 d->editComment->setText(QString());
178 }
179 }
180 }
181 #endif
182
183 return QWidget::eventFilter(obj, event);
184 }
185
186 #include "metadatawidget.moc"