]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
* changed "Click to add comment..." to simply "Add comment..." for consistency with...
[dolphin.git] / src / infosidebarpage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 "infosidebarpage.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kfileplacesmodel.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
29 #include <kdialog.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kseparator.h>
33 #include <kiconloader.h>
34
35 #include <QEvent>
36 #include <QInputDialog>
37 #include <QLabel>
38 #include <QPainter>
39 #include <QPixmap>
40 #include <QResizeEvent>
41 #include <QStyleOptionMenuItem>
42 #include <QTimer>
43 #include <QVBoxLayout>
44
45 #include "dolphinsettings.h"
46 #include "metadatawidget.h"
47 #include "metatextlabel.h"
48 #include "pixmapviewer.h"
49
50 class InfoSeparator : public QWidget
51 {
52 public:
53 InfoSeparator(QWidget* parent);
54 virtual ~InfoSeparator();
55
56 protected:
57 virtual void paintEvent(QPaintEvent* event);
58 };
59
60 InfoSeparator::InfoSeparator(QWidget* parent) :
61 QWidget(parent)
62 {
63 setMinimumSize(0, 8);
64 }
65
66 InfoSeparator::~InfoSeparator()
67 {
68 }
69
70 void InfoSeparator::paintEvent(QPaintEvent* event)
71 {
72 Q_UNUSED(event);
73 QPainter painter(this);
74
75 QStyleOptionMenuItem option;
76 option.initFrom(this);
77 option.menuItemType = QStyleOptionMenuItem::Separator;
78 style()->drawControl(QStyle::CE_MenuItem, &option, &painter, this);
79 }
80
81 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
82 SidebarPage(parent),
83 m_pendingPreview(false),
84 m_shownUrl(),
85 m_urlCandidate(),
86 m_fileItem(),
87 m_nameLabel(0),
88 m_preview(0),
89 m_metaDataWidget(0),
90 m_metaTextLabel(0)
91 {
92 const int spacing = KDialog::spacingHint();
93
94 m_timer = new QTimer(this);
95 m_timer->setSingleShot(true);
96 connect(m_timer, SIGNAL(timeout()),
97 this, SLOT(slotTimeout()));
98
99 QVBoxLayout* layout = new QVBoxLayout;
100 layout->setSpacing(spacing);
101
102 // name
103 m_nameLabel = new QLabel(this);
104 QFont font = m_nameLabel->font();
105 font.setBold(true);
106 m_nameLabel->setFont(font);
107 m_nameLabel->setAlignment(Qt::AlignHCenter);
108 m_nameLabel->setWordWrap(true);
109
110 // preview
111 m_preview = new PixmapViewer(this);
112 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
113 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
114
115 if (MetaDataWidget::metaDataAvailable()) {
116 // rating, comment and tags
117 m_metaDataWidget = new MetaDataWidget(this);
118 }
119
120 // general meta text information
121 m_metaTextLabel = new MetaTextLabel(this);
122 m_metaTextLabel->setMinimumWidth(spacing);
123
124 layout->addWidget(m_nameLabel);
125 layout->addWidget(m_preview);
126 layout->addWidget(new InfoSeparator(this));
127 if (m_metaDataWidget != 0) {
128 layout->addWidget(m_metaDataWidget);
129 layout->addWidget(new InfoSeparator(this));
130 }
131 layout->addWidget(m_metaTextLabel);
132
133 // ensure that widgets in the information side bar are aligned towards the top
134 layout->addStretch(1);
135 setLayout(layout);
136 }
137
138 InfoSidebarPage::~InfoSidebarPage()
139 {
140 }
141
142 QSize InfoSidebarPage::sizeHint() const
143 {
144 QSize size = SidebarPage::sizeHint();
145 size.setWidth(minimumSizeHint().width());
146 return size;
147 }
148
149 void InfoSidebarPage::setUrl(const KUrl& url)
150 {
151 SidebarPage::setUrl(url);
152 if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
153 cancelRequest();
154 m_shownUrl = url;
155 showItemInfo();
156 }
157 }
158
159 void InfoSidebarPage::setSelection(const KFileItemList& selection)
160 {
161 SidebarPage::setSelection(selection);
162
163 const int count = selection.count();
164 if (count == 0) {
165 m_shownUrl = url();
166 showItemInfo();
167 } else {
168 if (count == 1) {
169 const KUrl url = selection.first().url();
170 if (!url.isEmpty()) {
171 m_urlCandidate = url;
172 }
173 }
174 m_timer->start(TimerDelay);
175 }
176 }
177
178 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
179 {
180 if (!selection().isEmpty()) {
181 // if items are selected, no item information may get requested
182 return;
183 }
184
185 cancelRequest();
186
187 m_fileItem = KFileItem();
188
189 if (!item.isNull() && (selection().size() <= 1)) {
190 const KUrl url = item.url();
191 if (!url.isEmpty()) {
192 m_urlCandidate = url;
193 m_fileItem = item;
194 m_timer->start(TimerDelay);
195 }
196 }
197 }
198
199 void InfoSidebarPage::showEvent(QShowEvent* event)
200 {
201 SidebarPage::showEvent(event);
202 if (event->spontaneous()) {
203 return;
204 }
205 showItemInfo();
206 }
207
208 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
209 {
210 // If the text inside the name label or the info label cannot
211 // get wrapped, then the maximum width of the label is increased
212 // so that the width of the information sidebar gets increased.
213 // To prevent this, the maximum width is adjusted to
214 // the current width of the sidebar.
215 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
216 m_nameLabel->setMaximumWidth(maxWidth);
217
218 // try to increase the preview as large as possible
219 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
220 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
221 m_timer->start(TimerDelay);
222
223 SidebarPage::resizeEvent(event);
224 }
225
226 void InfoSidebarPage::showItemInfo()
227 {
228 if (!isVisible()) {
229 return;
230 }
231
232 cancelRequest();
233
234 const KFileItemList& selectedItems = selection();
235 const KUrl file = selectedItems.isEmpty() ? m_shownUrl : selectedItems[0].url();
236 if (!file.isValid()) {
237 return;
238 }
239
240 const int itemCount = selectedItems.count();
241 if (itemCount > 1) {
242 KIconLoader iconLoader;
243 QPixmap icon = iconLoader.loadIcon("dialog-information",
244 KIconLoader::NoGroup,
245 KIconLoader::SizeEnormous);
246 m_preview->setPixmap(icon);
247 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems.count()));
248 } else if (!applyPlace(file)) {
249 // try to get a preview pixmap from the item...
250 KUrl::List list;
251 list.append(file);
252
253 m_pendingPreview = true;
254 m_preview->setPixmap(QPixmap());
255
256 KIO::PreviewJob* job = KIO::filePreview(list,
257 m_preview->width(),
258 m_preview->height(),
259 0,
260 0,
261 true,
262 false);
263 job->setIgnoreMaximumSize(true);
264
265 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
266 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
267 connect(job, SIGNAL(failed(const KFileItem&)),
268 this, SLOT(showIcon(const KFileItem&)));
269
270 m_nameLabel->setText(file.fileName());
271 }
272
273 showMetaInfo();
274 }
275
276 void InfoSidebarPage::slotTimeout()
277 {
278 m_shownUrl = m_urlCandidate;
279 showItemInfo();
280 }
281
282 void InfoSidebarPage::showIcon(const KFileItem& item)
283 {
284 m_pendingPreview = false;
285 if (!applyPlace(item.url())) {
286 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
287 }
288 }
289
290 void InfoSidebarPage::showPreview(const KFileItem& item,
291 const QPixmap& pixmap)
292 {
293 Q_UNUSED(item);
294 if (m_pendingPreview) {
295 m_preview->setPixmap(pixmap);
296 m_pendingPreview = false;
297 }
298 }
299
300 bool InfoSidebarPage::applyPlace(const KUrl& url)
301 {
302 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
303 int count = placesModel->rowCount();
304
305 for (int i = 0; i < count; ++i) {
306 QModelIndex index = placesModel->index(i, 0);
307
308 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
309 m_nameLabel->setText(placesModel->text(index));
310 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
311 return true;
312 }
313 }
314
315 return false;
316 }
317
318 void InfoSidebarPage::cancelRequest()
319 {
320 m_timer->stop();
321 }
322
323 void InfoSidebarPage::showMetaInfo()
324 {
325 m_metaTextLabel->clear();
326
327 const KFileItemList& selectedItems = selection();
328 if (selectedItems.size() <= 1) {
329 KFileItem fileItem;
330 if (m_fileItem.isNull()) {
331 // no pending request is ongoing
332 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
333 fileItem.refresh();
334 } else {
335 fileItem = m_fileItem;
336 }
337
338 if (fileItem.isDir()) {
339 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
340 } else {
341 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
342
343 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
344 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
345
346 // TODO: See convertMetaInfo below, find a way to display only interesting information
347 // in a readable way
348 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
349 KFileMetaInfo::TechnicalInfo |
350 KFileMetaInfo::ContentInfo |
351 KFileMetaInfo::Thumbnail;
352 const QString path = fileItem.url().url();
353 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
354 if (fileMetaInfo.isValid()) {
355 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
356 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
357 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
358 QString labelText;
359 while (it != end) {
360 const KFileMetaInfoItem& metaInfoItem = it.value();
361 const QVariant& value = metaInfoItem.value();
362 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
363 m_metaTextLabel->add(labelText, value.toString());
364 }
365 ++it;
366 }
367 }
368 }
369
370 if (m_metaDataWidget != 0) {
371 m_metaDataWidget->setFile(fileItem.url());
372 }
373 } else {
374 if (m_metaDataWidget != 0) {
375 KUrl::List urls;
376 foreach (const KFileItem& item, selectedItems) {
377 urls.append(item.url());
378 }
379 m_metaDataWidget->setFiles(urls);
380 }
381
382 unsigned long int totalSize = 0;
383 foreach (const KFileItem& item, selectedItems) {
384 // Only count the size of files, not dirs to match what
385 // DolphinViewContainer::selectionStatusBarText() does.
386 if (!item.isDir() && !item.isLink()) {
387 totalSize += item.size();
388 }
389 }
390 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
391 }
392 }
393
394 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
395 {
396 // TODO: This code prevents that interesting meta information might be hidden
397 // and only bypasses the current problem that not all the meta information should
398 // be shown to the user. Check whether it's possible with Nepomuk to show
399 // all "user relevant" information in a readable way...
400
401 struct MetaKey {
402 const char* key;
403 const char* text;
404 };
405
406 // sorted list of keys, where its data should be shown
407 static const MetaKey keys[] = {
408 { "audio.album", "Album:" },
409 { "audio.artist", "Artist:" },
410 { "audio.title", "Title:" },
411 };
412
413 // do a binary search for the key...
414 int top = 0;
415 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
416 while (top <= bottom) {
417 const int middle = (top + bottom) / 2;
418 const int result = key.compare(keys[middle].key);
419 if (result < 0) {
420 bottom = middle - 1;
421 } else if (result > 0) {
422 top = middle + 1;
423 } else {
424 text = keys[middle].text;
425 return true;
426 }
427 }
428
429 return false;
430 }
431
432 #include "infosidebarpage.moc"