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 "infosidebarpage.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 "dolphinsettings.h"
49 #include "metadatawidget.h"
50 #include "metatextlabel.h"
51 #include "pixmapviewer.h"
53 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
56 m_pendingPreview(false),
58 m_outdatedPreviewTimer(0),
70 InfoSidebarPage::~InfoSidebarPage()
74 QSize
InfoSidebarPage::sizeHint() const
76 QSize size
= SidebarPage::sizeHint();
77 size
.setWidth(minimumSizeHint().width());
81 void InfoSidebarPage::setUrl(const KUrl
& url
)
83 SidebarPage::setUrl(url
);
84 if (url
.isValid() && !isEqualToShownUrl(url
)) {
95 void InfoSidebarPage::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 InfoSidebarPage::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 InfoSidebarPage::showEvent(QShowEvent
* event
)
153 SidebarPage::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 InfoSidebarPage::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 sidebar gets increased.
171 // To prevent this, the maximum width is adjusted to
172 // the current width of the sidebar.
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 SidebarPage::resizeEvent(event
);
185 void InfoSidebarPage::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(KUrl::List() << itemUrl
,
220 job
->setIgnoreMaximumSize(true);
222 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
223 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
224 connect(job
, SIGNAL(failed(const KFileItem
&)),
225 this, SLOT(showIcon(const KFileItem
&)));
227 setNameLabelText(itemUrl
.fileName());
234 void InfoSidebarPage::slotInfoTimeout()
236 m_shownUrl
= m_urlCandidate
;
240 void InfoSidebarPage::markOutdatedPreview()
242 KIconEffect iconEffect
;
243 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
244 KIconLoader::Desktop
,
245 KIconLoader::DisabledState
);
246 m_preview
->setPixmap(disabledPixmap
);
249 void InfoSidebarPage::showIcon(const KFileItem
& item
)
251 m_outdatedPreviewTimer
->stop();
252 m_pendingPreview
= false;
253 if (!applyPlace(item
.url())) {
254 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
258 void InfoSidebarPage::showPreview(const KFileItem
& item
,
259 const QPixmap
& pixmap
)
261 m_outdatedPreviewTimer
->stop();
264 if (m_pendingPreview
) {
265 m_preview
->setPixmap(pixmap
);
266 m_pendingPreview
= false;
270 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
272 if (m_shownUrl
== KUrl(source
)) {
273 // the currently shown file has been renamed, hence update the item information
274 // for the renamed file
275 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
276 requestDelayedItemInfo(item
);
280 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
282 if (m_shownUrl
== KUrl(directory
)) {
283 // If the 'trash' icon changes because the trash has been emptied or got filled,
284 // the signal filesAdded("trash:/") will be emitted.
285 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
286 requestDelayedItemInfo(item
);
290 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
292 foreach (const QString
& fileName
, files
) {
293 if (m_shownUrl
== KUrl(fileName
)) {
300 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
302 foreach (const QString
& fileName
, files
) {
303 if (m_shownUrl
== KUrl(fileName
)) {
304 // the currently shown item has been removed, show
305 // the parent directory as fallback
313 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
315 if (m_shownUrl
== KUrl(directory
)) {
316 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
317 requestDelayedItemInfo(item
);
321 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
323 if (m_shownUrl
== KUrl(directory
)) {
324 // The signal 'leftDirectory' is also emitted when a media
325 // has been unmounted. In this case no directory change will be
326 // done in Dolphin, but the Information Panel must be updated to
327 // indicate an invalid directory.
333 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
335 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
336 int count
= placesModel
->rowCount();
338 for (int i
= 0; i
< count
; ++i
) {
339 QModelIndex index
= placesModel
->index(i
, 0);
341 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
342 setNameLabelText(placesModel
->text(index
));
343 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
351 void InfoSidebarPage::cancelRequest()
356 void InfoSidebarPage::showMetaInfo()
358 m_metaTextLabel
->clear();
360 if (showMultipleSelectionInfo()) {
361 if (m_metaDataWidget
!= 0) {
363 foreach (const KFileItem
& item
, m_selection
) {
364 urls
.append(item
.targetUrl());
366 m_metaDataWidget
->setFiles(urls
);
369 quint64 totalSize
= 0;
370 foreach (const KFileItem
& item
, m_selection
) {
371 // Only count the size of files, not dirs to match what
372 // DolphinViewContainer::selectionStatusBarText() does.
373 if (!item
.isDir() && !item
.isLink()) {
374 totalSize
+= item
.size();
377 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
379 const KFileItem item
= fileItem();
381 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
382 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
384 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
386 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
387 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
389 if (item
.isLocalFile()) {
390 // TODO: See convertMetaInfo below, find a way to display only interesting information
392 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
393 KFileMetaInfo::TechnicalInfo
|
394 KFileMetaInfo::ContentInfo
;
395 const QString path
= item
.url().path();
396 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
397 if (fileMetaInfo
.isValid()) {
398 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
399 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
400 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
403 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
404 const QVariant
& value
= metaInfoItem
.value();
405 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
406 m_metaTextLabel
->add(labelText
, value
.toString());
414 if (m_metaDataWidget
!= 0) {
415 m_metaDataWidget
->setFile(item
.targetUrl());
420 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
427 // sorted list of keys, where its data should be shown
428 static const MetaKey keys
[] = {
429 { "http://freedesktop.org/standards/xesam/1.0/core#album", i18nc("@label", "Album:") },
430 { "http://freedesktop.org/standards/xesam/1.0/core#artist", i18nc("@label", "Artist:") },
431 { "http://freedesktop.org/standards/xesam/1.0/core#genre", i18nc("@label", "Genre:") },
432 { "http://freedesktop.org/standards/xesam/1.0/core#height", i18nc("@label", "Height:") },
433 { "http://freedesktop.org/standards/xesam/1.0/core#lineCount", i18nc("@label", "Lines:") },
434 { "http://freedesktop.org/standards/xesam/1.0/core#title", i18nc("@label", "Title:") },
435 { "http://freedesktop.org/standards/xesam/1.0/core#type", i18nc("@label", "Type:") },
436 { "http://freedesktop.org/standards/xesam/1.0/core#trackNumber", i18nc("@label", "Track:") },
437 { "http://freedesktop.org/standards/xesam/1.0/core#width", i18nc("@label", "Width:") }
440 // do a binary search for the key...
442 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
443 while (top
<= bottom
) {
444 const int middle
= (top
+ bottom
) / 2;
445 const int result
= key
.compare(keys
[middle
].key
);
448 } else if (result
> 0) {
451 text
= keys
[middle
].text
;
459 KFileItem
InfoSidebarPage::fileItem() const
461 if (!m_fileItem
.isNull()) {
465 if (!m_selection
.isEmpty()) {
466 Q_ASSERT(m_selection
.count() == 1);
467 return m_selection
.first();
470 // no item is hovered and no selection has been done: provide
471 // an item for the directory represented by m_shownUrl
472 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
477 bool InfoSidebarPage::showMultipleSelectionInfo() const
479 return m_fileItem
.isNull() && (m_selection
.count() > 1);
482 bool InfoSidebarPage::isEqualToShownUrl(const KUrl
& url
) const
484 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
487 void InfoSidebarPage::setNameLabelText(const QString
& text
)
489 QTextOption textOption
;
490 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
492 QTextLayout
textLayout(text
);
493 textLayout
.setFont(m_nameLabel
->font());
494 textLayout
.setTextOption(textOption
);
497 wrappedText
.reserve(text
.length());
499 // wrap the text to fit into the width of m_nameLabel
500 textLayout
.beginLayout();
501 QTextLine line
= textLayout
.createLine();
502 while (line
.isValid()) {
503 line
.setLineWidth(m_nameLabel
->width());
504 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
506 line
= textLayout
.createLine();
507 if (line
.isValid()) {
508 wrappedText
+= QChar::LineSeparator
;
511 textLayout
.endLayout();
513 m_nameLabel
->setText(wrappedText
);
516 void InfoSidebarPage::init()
518 const int spacing
= KDialog::spacingHint();
520 m_infoTimer
= new QTimer(this);
521 m_infoTimer
->setInterval(300);
522 m_infoTimer
->setSingleShot(true);
523 connect(m_infoTimer
, SIGNAL(timeout()),
524 this, SLOT(slotInfoTimeout()));
526 // Initialize timer for disabling an outdated preview with a small
527 // delay. This prevents flickering if the new preview can be generated
528 // within a very small timeframe.
529 m_outdatedPreviewTimer
= new QTimer(this);
530 m_outdatedPreviewTimer
->setInterval(300);
531 m_outdatedPreviewTimer
->setSingleShot(true);
532 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
533 this, SLOT(markOutdatedPreview()));
535 QVBoxLayout
* layout
= new QVBoxLayout
;
536 layout
->setSpacing(spacing
);
539 m_nameLabel
= new QLabel(this);
540 QFont font
= m_nameLabel
->font();
542 m_nameLabel
->setFont(font
);
543 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
544 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
547 m_preview
= new PixmapViewer(this);
548 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
549 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
551 if (MetaDataWidget::metaDataAvailable()) {
552 // rating, comment and tags
553 m_metaDataWidget
= new MetaDataWidget(this);
554 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
557 // general meta text information
558 m_metaTextLabel
= new MetaTextLabel(this);
559 m_metaTextLabel
->setMinimumWidth(spacing
);
560 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
562 layout
->addWidget(m_nameLabel
);
563 layout
->addWidget(new KSeparator(this));
564 layout
->addWidget(m_preview
);
565 layout
->addWidget(new KSeparator(this));
566 if (m_metaDataWidget
!= 0) {
567 layout
->addWidget(m_metaDataWidget
);
568 layout
->addWidget(new KSeparator(this));
570 layout
->addWidget(m_metaTextLabel
);
572 // ensure that widgets in the information side bar are aligned towards the top
573 layout
->addStretch(1);
576 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
577 QDBusConnection::sessionBus(), this);
578 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
579 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
580 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
581 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
582 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
583 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
585 m_initialized
= true;
588 #include "infosidebarpage.moc"