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