1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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>
25 #include <kdirnotify.h>
26 #include <kfileplacesmodel.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>
38 #include <QInputDialog>
42 #include <QResizeEvent>
43 #include <QTextLayout>
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
) :
56 m_pendingPreview(false),
58 m_outdatedPreviewTimer(0),
70 InformationPanel::~InformationPanel()
74 QSize
InformationPanel::sizeHint() const
76 QSize size
= Panel::sizeHint();
77 size
.setWidth(minimumSizeHint().width());
81 void InformationPanel::setUrl(const KUrl
& url
)
84 if (url
.isValid() && !isEqualToShownUrl(url
)) {
95 void InformationPanel::setSelection(const KFileItemList
& selection
)
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().
109 m_selection
= selection
;
111 const int count
= selection
.count();
113 if (!isEqualToShownUrl(url())) {
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
)
133 m_fileItem
= KFileItem();
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();
142 const KUrl url
= item
.url();
143 if (url
.isValid() && !isEqualToShownUrl(url
)) {
144 m_urlCandidate
= item
.url();
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
165 void InformationPanel::resizeEvent(QResizeEvent
* event
)
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()
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()));
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
,
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());
233 void InformationPanel::slotInfoTimeout()
235 m_shownUrl
= m_urlCandidate
;
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();
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
)) {
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
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.
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));
350 void InformationPanel::cancelRequest()
355 void InformationPanel::showMetaInfo()
357 m_metaTextLabel
->clear();
359 if (showMultipleSelectionInfo()) {
360 if (m_metaDataWidget
!= 0) {
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
));
378 const KFileItem item
= fileItem();
380 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
381 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
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
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();
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());
413 if (m_metaDataWidget
!= 0) {
414 m_metaDataWidget
->setFile(item
.targetUrl());
419 bool InformationPanel::convertMetaInfo(const QString
& key
, QString
& text
) const
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...
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
);
447 } else if (result
> 0) {
450 text
= keys
[middle
].text
;
458 KFileItem
InformationPanel::fileItem() const
460 if (!m_fileItem
.isNull()) {
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
);
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
);
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
);
538 m_nameLabel
= new QLabel(this);
539 QFont font
= m_nameLabel
->font();
541 m_nameLabel
->setFont(font
);
542 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
543 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
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);
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"