]> cloud.milkyroute.net Git - dolphin.git/blob - src/metadatawidget.cpp
when the column view is used, then 'Split view' should take the root URL of the colum...
[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 #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 editComment->blockSignals(true);
60 if (comment.isEmpty()) {
61 editComment->setFontItalic(true);
62 editComment->setText(i18n("Click to add comment..."));
63 } else {
64 editComment->setFontItalic(false);
65 editComment->setText(comment);
66 }
67 editComment->blockSignals(false);
68 }
69
70 KUrl fileUrl;
71
72 Nepomuk::KMetaData::Resource file;
73
74 QTextEdit* editComment;
75 KRatingWidget* ratingWidget;
76 Nepomuk::KMetaData::TagWidget* tagWidget;
77 #endif
78 };
79
80
81 MetaDataWidget::MetaDataWidget(QWidget* parent)
82 : QWidget(parent)
83 {
84 #ifdef HAVE_KMETADATA
85 d = new Private;
86 d->editComment = new QTextEdit(this);
87 d->tagWidget = new Nepomuk::KMetaData::TagWidget(this);
88 d->ratingWidget = new KRatingWidget(this);
89 connect(d->ratingWidget, SIGNAL(ratingChanged(int)), this, SLOT(slotRatingChanged(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(i18n("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(i18n("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 #ifdef HAVE_KMETADATA
122 // FIXME: replace with KMetaData::File once we have it again
123 d->fileUrl = url;
124 d->file = Nepomuk::KMetaData::Resource(url.url(), s_nfoFile);
125 d->ratingWidget->setRating(d->file.rating());
126 d->tagWidget->setTaggedResource(d->file);
127 d->loadComment(d->file.description());
128 #endif
129 }
130
131
132 void MetaDataWidget::setFiles(const KUrl::List urls)
133 {
134 #ifdef HAVE_KMETADATA
135 // FIXME: support multiple files
136 setFile( urls.first() );
137 #endif
138 }
139
140
141 void MetaDataWidget::slotCommentChanged()
142 {
143 #ifdef HAVE_KMETADATA
144 if ( d->editComment->toPlainText() != d->file.description() ) {
145 // d->file.setLocation(url.url());
146 d->file.setProperty( s_nfoFileUrl, d->fileUrl.url() );
147 d->file.setDescription(d->editComment->toPlainText());
148 }
149 #endif
150 }
151
152
153 void MetaDataWidget::slotRatingChanged(int r)
154 {
155 #ifdef HAVE_KMETADATA
156 if ( r != d->file.rating() ) {
157 // d->file.setLocation(url.url());
158 d->file.setProperty( s_nfoFileUrl, d->fileUrl.url() );
159 d->file.setRating(r);
160 }
161 #endif
162 }
163
164
165 bool MetaDataWidget::eventFilter(QObject* obj, QEvent* event)
166 {
167 #ifdef HAVE_KMETADATA
168 if (obj == d->editComment->viewport()
169 || obj == d->editComment) {
170 if (event->type() == QEvent::FocusOut) {
171 // make sure the info text is displayed again
172 d->loadComment(d->editComment->toPlainText());
173 } else if (event->type() == QEvent::FocusIn) {
174 d->editComment->setFontItalic(false);
175 if (d->file.description().isEmpty())
176 d->editComment->setText(QString());
177 }
178 }
179 #endif
180
181 return QWidget::eventFilter(obj, event);
182 }
183
184 #include "metadatawidget.moc"