add more spacing
[personal-kdebase.git] / apps / dolphin / src / panels / information / informationpanel.cpp
bloba77a39908b390bae6891701524e20da497b18fc6
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 ***************************************************************************/
20 #include "informationpanel.h"
22 #include <config-nepomuk.h>
24 #include <kdialog.h>
25 #include <kdirnotify.h>
26 #include <kfileplacesmodel.h>
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 #include <kio/previewjob.h>
30 #include <kfileitem.h>
31 #include <kglobalsettings.h>
32 #include <kfilemetainfo.h>
33 #include <kiconeffect.h>
34 #include <kseparator.h>
35 #include <kiconloader.h>
37 #include <QEvent>
38 #include <QInputDialog>
39 #include <QLabel>
40 #include <QPainter>
41 #include <QPixmap>
42 #include <QResizeEvent>
43 #include <QTextLayout>
44 #include <QTextLine>
45 #include <QTimer>
46 #include <QVBoxLayout>
48 #include "settings/dolphinsettings.h"
49 #include "metadatawidget.h"
50 #include "metatextlabel.h"
51 #include "pixmapviewer.h"
53 InformationPanel::InformationPanel(QWidget* parent) :
54 Panel(parent),
55 m_initialized(false),
56 m_pendingPreview(false),
57 m_infoTimer(0),
58 m_outdatedPreviewTimer(0),
59 m_shownUrl(),
60 m_urlCandidate(),
61 m_fileItem(),
62 m_selection(),
63 m_nameLabel(0),
64 m_preview(0),
65 m_metaDataWidget(0),
66 m_metaTextLabel(0)
70 InformationPanel::~InformationPanel()
74 QSize InformationPanel::sizeHint() const
76 QSize size = Panel::sizeHint();
77 size.setWidth(minimumSizeHint().width());
78 return size;
81 void InformationPanel::setUrl(const KUrl& url)
83 Panel::setUrl(url);
84 if (url.isValid() && !isEqualToShownUrl(url)) {
85 if (isVisible()) {
86 cancelRequest();
87 m_shownUrl = url;
88 showItemInfo();
89 } else {
90 m_shownUrl = url;
95 void InformationPanel::setSelection(const KFileItemList& selection)
97 if (!isVisible()) {
98 return;
101 if ((selection.count() == 0) && (m_selection.count() == 0)) {
102 // The selection has not really changed, only the current index.
103 // QItemSelectionModel emits a signal in this case and it is less
104 // expensive doing the check this way instead of patching
105 // DolphinView::emitSelectionChanged().
106 return;
109 m_selection = selection;
111 const int count = selection.count();
112 if (count == 0) {
113 if (!isEqualToShownUrl(url())) {
114 m_shownUrl = url();
115 showItemInfo();
117 } else {
118 if ((count == 1) && !selection.first().url().isEmpty()) {
119 m_urlCandidate = selection.first().url();
121 m_infoTimer->start();
125 void InformationPanel::requestDelayedItemInfo(const KFileItem& item)
127 if (!isVisible()) {
128 return;
131 cancelRequest();
133 m_fileItem = KFileItem();
134 if (item.isNull()) {
135 // The cursor is above the viewport. If files are selected,
136 // show information regarding the selection.
137 if (m_selection.size() > 0) {
138 m_pendingPreview = false;
139 m_infoTimer->start();
141 } else {
142 const KUrl url = item.url();
143 if (url.isValid() && !isEqualToShownUrl(url)) {
144 m_urlCandidate = item.url();
145 m_fileItem = item;
146 m_infoTimer->start();
151 void InformationPanel::showEvent(QShowEvent* event)
153 Panel::showEvent(event);
154 if (!event->spontaneous()) {
155 if (!m_initialized) {
156 // do a delayed initialization so that no performance
157 // penalty is given when Dolphin is started with a closed
158 // Information Panel
159 init();
161 showItemInfo();
165 void InformationPanel::resizeEvent(QResizeEvent* event)
167 if (isVisible()) {
168 // If the text inside the name label or the info label cannot
169 // get wrapped, then the maximum width of the label is increased
170 // so that the width of the information panel gets increased.
171 // To prevent this, the maximum width is adjusted to
172 // the current width of the panel.
173 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
174 m_nameLabel->setMaximumWidth(maxWidth);
176 // try to increase the preview as large as possible
177 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
178 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
179 m_infoTimer->start();
182 Panel::resizeEvent(event);
185 void InformationPanel::showItemInfo()
187 if (!isVisible()) {
188 return;
191 cancelRequest();
193 if (showMultipleSelectionInfo()) {
194 KIconLoader iconLoader;
195 QPixmap icon = iconLoader.loadIcon("dialog-information",
196 KIconLoader::NoGroup,
197 KIconLoader::SizeEnormous);
198 m_preview->setPixmap(icon);
199 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
200 m_shownUrl = KUrl();
201 } else {
202 const KFileItem item = fileItem();
203 const KUrl itemUrl = item.url();
204 if (!applyPlace(itemUrl)) {
205 // try to get a preview pixmap from the item...
206 m_pendingPreview = true;
208 // Mark the currently shown preview as outdated. This is done
209 // with a small delay to prevent a flickering when the next preview
210 // can be shown within a short timeframe.
211 m_outdatedPreviewTimer->start();
213 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
214 m_preview->width(),
215 m_preview->height(),
218 false,
219 true);
221 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
222 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
223 connect(job, SIGNAL(failed(const KFileItem&)),
224 this, SLOT(showIcon(const KFileItem&)));
226 setNameLabelText(itemUrl.fileName());
230 showMetaInfo();
233 void InformationPanel::slotInfoTimeout()
235 m_shownUrl = m_urlCandidate;
236 showItemInfo();
239 void InformationPanel::markOutdatedPreview()
241 KIconEffect iconEffect;
242 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
243 KIconLoader::Desktop,
244 KIconLoader::DisabledState);
245 m_preview->setPixmap(disabledPixmap);
248 void InformationPanel::showIcon(const KFileItem& item)
250 m_outdatedPreviewTimer->stop();
251 m_pendingPreview = false;
252 if (!applyPlace(item.url())) {
253 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
257 void InformationPanel::showPreview(const KFileItem& item,
258 const QPixmap& pixmap)
260 m_outdatedPreviewTimer->stop();
262 Q_UNUSED(item);
263 if (m_pendingPreview) {
264 m_preview->setPixmap(pixmap);
265 m_pendingPreview = false;
269 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
271 if (m_shownUrl == KUrl(source)) {
272 // the currently shown file has been renamed, hence update the item information
273 // for the renamed file
274 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
275 requestDelayedItemInfo(item);
279 void InformationPanel::slotFilesAdded(const QString& directory)
281 if (m_shownUrl == KUrl(directory)) {
282 // If the 'trash' icon changes because the trash has been emptied or got filled,
283 // the signal filesAdded("trash:/") will be emitted.
284 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
285 requestDelayedItemInfo(item);
289 void InformationPanel::slotFilesChanged(const QStringList& files)
291 foreach (const QString& fileName, files) {
292 if (m_shownUrl == KUrl(fileName)) {
293 showItemInfo();
294 break;
299 void InformationPanel::slotFilesRemoved(const QStringList& files)
301 foreach (const QString& fileName, files) {
302 if (m_shownUrl == KUrl(fileName)) {
303 // the currently shown item has been removed, show
304 // the parent directory as fallback
305 m_shownUrl = url();
306 showItemInfo();
307 break;
312 void InformationPanel::slotEnteredDirectory(const QString& directory)
314 if (m_shownUrl == KUrl(directory)) {
315 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
316 requestDelayedItemInfo(item);
320 void InformationPanel::slotLeftDirectory(const QString& directory)
322 if (m_shownUrl == KUrl(directory)) {
323 // The signal 'leftDirectory' is also emitted when a media
324 // has been unmounted. In this case no directory change will be
325 // done in Dolphin, but the Information Panel must be updated to
326 // indicate an invalid directory.
327 m_shownUrl = url();
328 showItemInfo();
332 bool InformationPanel::applyPlace(const KUrl& url)
334 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
335 int count = placesModel->rowCount();
337 for (int i = 0; i < count; ++i) {
338 QModelIndex index = placesModel->index(i, 0);
340 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
341 setNameLabelText(placesModel->text(index));
342 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
343 return true;
347 return false;
350 void InformationPanel::cancelRequest()
352 m_infoTimer->stop();
355 void InformationPanel::showMetaInfo()
357 m_metaTextLabel->clear();
359 if (showMultipleSelectionInfo()) {
360 if (m_metaDataWidget != 0) {
361 KUrl::List urls;
362 foreach (const KFileItem& item, m_selection) {
363 urls.append(item.targetUrl());
365 m_metaDataWidget->setFiles(urls);
368 quint64 totalSize = 0;
369 foreach (const KFileItem& item, m_selection) {
370 // Only count the size of files, not dirs to match what
371 // DolphinViewContainer::selectionStatusBarText() does.
372 if (!item.isDir() && !item.isLink()) {
373 totalSize += item.size();
376 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
377 } else {
378 const KFileItem item = fileItem();
379 if (item.isDir()) {
380 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
381 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
382 } else {
383 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
385 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
386 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
388 if (item.isLocalFile()) {
389 // TODO: See convertMetaInfo below, find a way to display only interesting information
390 // in a readable way
391 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
392 KFileMetaInfo::TechnicalInfo |
393 KFileMetaInfo::ContentInfo;
394 const QString path = item.url().path();
395 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
396 if (fileMetaInfo.isValid()) {
397 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
398 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
399 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
400 QString labelText;
401 while (it != end) {
402 const KFileMetaInfoItem& metaInfoItem = it.value();
403 const QVariant& value = metaInfoItem.value();
404 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
405 m_metaTextLabel->add(labelText, value.toString());
407 ++it;
413 if (m_metaDataWidget != 0) {
414 m_metaDataWidget->setFile(item.targetUrl());
419 bool InformationPanel::convertMetaInfo(const QString& key, QString& text) const
421 struct MetaKey {
422 const char* key;
423 QString text;
426 // sorted list of keys, where its data should be shown
427 static const MetaKey keys[] = {
428 { "http://freedesktop.org/standards/xesam/1.0/core#album", i18nc("@label", "Album:") },
429 { "http://freedesktop.org/standards/xesam/1.0/core#artist", i18nc("@label", "Artist:") },
430 { "http://freedesktop.org/standards/xesam/1.0/core#genre", i18nc("@label", "Genre:") },
431 { "http://freedesktop.org/standards/xesam/1.0/core#height", i18nc("@label", "Height:") },
432 { "http://freedesktop.org/standards/xesam/1.0/core#lineCount", i18nc("@label", "Lines:") },
433 { "http://freedesktop.org/standards/xesam/1.0/core#title", i18nc("@label", "Title:") },
434 { "http://freedesktop.org/standards/xesam/1.0/core#type", i18nc("@label", "Type:") },
435 { "http://freedesktop.org/standards/xesam/1.0/core#trackNumber", i18nc("@label", "Track:") },
436 { "http://freedesktop.org/standards/xesam/1.0/core#width", i18nc("@label", "Width:") }
439 // do a binary search for the key...
440 int top = 0;
441 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
442 while (top <= bottom) {
443 const int middle = (top + bottom) / 2;
444 const int result = key.compare(keys[middle].key);
445 if (result < 0) {
446 bottom = middle - 1;
447 } else if (result > 0) {
448 top = middle + 1;
449 } else {
450 text = keys[middle].text;
451 return true;
455 return false;
458 KFileItem InformationPanel::fileItem() const
460 if (!m_fileItem.isNull()) {
461 return m_fileItem;
464 if (!m_selection.isEmpty()) {
465 Q_ASSERT(m_selection.count() == 1);
466 return m_selection.first();
469 // no item is hovered and no selection has been done: provide
470 // an item for the directory represented by m_shownUrl
471 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
472 item.refresh();
473 return item;
476 bool InformationPanel::showMultipleSelectionInfo() const
478 return m_fileItem.isNull() && (m_selection.count() > 1);
481 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
483 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
486 void InformationPanel::setNameLabelText(const QString& text)
488 QTextOption textOption;
489 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
491 QTextLayout textLayout(text);
492 textLayout.setFont(m_nameLabel->font());
493 textLayout.setTextOption(textOption);
495 QString wrappedText;
496 wrappedText.reserve(text.length());
498 // wrap the text to fit into the width of m_nameLabel
499 textLayout.beginLayout();
500 QTextLine line = textLayout.createLine();
501 while (line.isValid()) {
502 line.setLineWidth(m_nameLabel->width());
503 wrappedText += text.mid(line.textStart(), line.textLength());
505 line = textLayout.createLine();
506 if (line.isValid()) {
507 wrappedText += QChar::LineSeparator;
510 textLayout.endLayout();
512 m_nameLabel->setText(wrappedText);
515 void InformationPanel::init()
517 const int spacing = KDialog::spacingHint();
519 m_infoTimer = new QTimer(this);
520 m_infoTimer->setInterval(300);
521 m_infoTimer->setSingleShot(true);
522 connect(m_infoTimer, SIGNAL(timeout()),
523 this, SLOT(slotInfoTimeout()));
525 // Initialize timer for disabling an outdated preview with a small
526 // delay. This prevents flickering if the new preview can be generated
527 // within a very small timeframe.
528 m_outdatedPreviewTimer = new QTimer(this);
529 m_outdatedPreviewTimer->setInterval(300);
530 m_outdatedPreviewTimer->setSingleShot(true);
531 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
532 this, SLOT(markOutdatedPreview()));
534 QVBoxLayout* layout = new QVBoxLayout;
535 layout->setSpacing(spacing);
537 // name
538 m_nameLabel = new QLabel(this);
539 QFont font = m_nameLabel->font();
540 font.setBold(true);
541 m_nameLabel->setFont(font);
542 m_nameLabel->setAlignment(Qt::AlignHCenter);
543 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
545 // preview
546 m_preview = new PixmapViewer(this);
547 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
548 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
550 if (MetaDataWidget::metaDataAvailable()) {
551 // rating, comment and tags
552 m_metaDataWidget = new MetaDataWidget(this);
553 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
556 // general meta text information
557 m_metaTextLabel = new MetaTextLabel(this);
558 m_metaTextLabel->setMinimumWidth(spacing);
559 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
561 layout->addWidget(m_nameLabel);
562 layout->addWidget(new KSeparator(this));
563 layout->addWidget(m_preview);
564 layout->addWidget(new KSeparator(this));
565 if (m_metaDataWidget != 0) {
566 layout->addWidget(m_metaDataWidget);
567 layout->addWidget(new KSeparator(this));
569 layout->addWidget(m_metaTextLabel);
571 // ensure that widgets in the information side bar are aligned towards the top
572 layout->addStretch(1);
573 setLayout(layout);
575 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
576 QDBusConnection::sessionBus(), this);
577 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
578 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
579 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
580 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
581 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
582 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
584 m_initialized = true;
587 #include "informationpanel.moc"