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>
44 #include <QVBoxLayout>
46 #include "dolphinsettings.h"
47 #include "metadatawidget.h"
48 #include "metatextlabel.h"
49 #include "pixmapviewer.h"
51 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
54 m_pendingPreview(false),
56 m_outdatedPreviewTimer(0),
68 InfoSidebarPage::~InfoSidebarPage()
72 QSize
InfoSidebarPage::sizeHint() const
74 QSize size
= SidebarPage::sizeHint();
75 size
.setWidth(minimumSizeHint().width());
79 void InfoSidebarPage::setUrl(const KUrl
& url
)
81 SidebarPage::setUrl(url
);
82 if (url
.isValid() && !isEqualToShownUrl(url
)) {
93 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
99 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
100 // The selection has not really changed, only the current index.
101 // QItemSelectionModel emits a signal in this case and it is less
102 // expensive doing the check this way instead of patching
103 // DolphinView::emitSelectionChanged().
107 m_selection
= selection
;
109 const int count
= selection
.count();
111 if (!isEqualToShownUrl(url())) {
116 if ((count
== 1) && !selection
.first().url().isEmpty()) {
117 m_urlCandidate
= selection
.first().url();
119 m_infoTimer
->start();
123 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
131 m_fileItem
= KFileItem();
133 // The cursor is above the viewport. If files are selected,
134 // show information regarding the selection.
135 if (m_selection
.size() > 0) {
136 m_pendingPreview
= false;
137 m_infoTimer
->start();
140 const KUrl url
= item
.url();
141 if (url
.isValid() && !isEqualToShownUrl(url
)) {
142 m_urlCandidate
= item
.url();
144 m_infoTimer
->start();
149 void InfoSidebarPage::showEvent(QShowEvent
* event
)
151 SidebarPage::showEvent(event
);
152 if (!event
->spontaneous()) {
153 if (!m_initialized
) {
154 // do a delayed initialization so that no performance
155 // penalty is given when Dolphin is started with a closed
163 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
166 // If the text inside the name label or the info label cannot
167 // get wrapped, then the maximum width of the label is increased
168 // so that the width of the information sidebar gets increased.
169 // To prevent this, the maximum width is adjusted to
170 // the current width of the sidebar.
171 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
172 m_nameLabel
->setMaximumWidth(maxWidth
);
174 // try to increase the preview as large as possible
175 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
176 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
177 m_infoTimer
->start();
180 SidebarPage::resizeEvent(event
);
183 void InfoSidebarPage::showItemInfo()
191 if (showMultipleSelectionInfo()) {
192 KIconLoader iconLoader
;
193 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
194 KIconLoader::NoGroup
,
195 KIconLoader::SizeEnormous
);
196 m_preview
->setPixmap(icon
);
197 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
200 const KFileItem item
= fileItem();
201 const KUrl itemUrl
= item
.url();
202 if (!applyPlace(itemUrl
)) {
203 // try to get a preview pixmap from the item...
204 m_pendingPreview
= true;
206 // Mark the currently shown preview as outdated. This is done
207 // with a small delay to prevent a flickering when the next preview
208 // can be shown within a short timeframe.
209 m_outdatedPreviewTimer
->start();
211 KIO::PreviewJob
* job
= KIO::filePreview(KUrl::List() << itemUrl
,
218 job
->setIgnoreMaximumSize(true);
220 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
221 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
222 connect(job
, SIGNAL(failed(const KFileItem
&)),
223 this, SLOT(showIcon(const KFileItem
&)));
225 m_nameLabel
->setText(itemUrl
.fileName());
232 void InfoSidebarPage::slotInfoTimeout()
234 m_shownUrl
= m_urlCandidate
;
238 void InfoSidebarPage::markOutdatedPreview()
240 KIconEffect iconEffect
;
241 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
242 KIconLoader::Desktop
,
243 KIconLoader::DisabledState
);
244 m_preview
->setPixmap(disabledPixmap
);
247 void InfoSidebarPage::showIcon(const KFileItem
& item
)
249 m_outdatedPreviewTimer
->stop();
250 m_pendingPreview
= false;
251 if (!applyPlace(item
.url())) {
252 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
256 void InfoSidebarPage::showPreview(const KFileItem
& item
,
257 const QPixmap
& pixmap
)
259 m_outdatedPreviewTimer
->stop();
262 if (m_pendingPreview
) {
263 m_preview
->setPixmap(pixmap
);
264 m_pendingPreview
= false;
268 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
270 if (m_shownUrl
== KUrl(source
)) {
271 // the currently shown file has been renamed, hence update the item information
272 // for the renamed file
273 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
274 requestDelayedItemInfo(item
);
278 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
280 if (m_shownUrl
== KUrl(directory
)) {
281 // If the 'trash' icon changes because the trash has been emptied or got filled,
282 // the signal filesAdded("trash:/") will be emitted.
283 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
284 requestDelayedItemInfo(item
);
288 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
290 foreach (const QString
& fileName
, files
) {
291 if (m_shownUrl
== KUrl(fileName
)) {
298 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
300 foreach (const QString
& fileName
, files
) {
301 if (m_shownUrl
== KUrl(fileName
)) {
302 // the currently shown item has been removed, show
303 // the parent directory as fallback
311 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
313 if (m_shownUrl
== KUrl(directory
)) {
314 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
315 requestDelayedItemInfo(item
);
319 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
321 if (m_shownUrl
== KUrl(directory
)) {
322 // The signal 'leftDirectory' is also emitted when a media
323 // has been unmounted. In this case no directory change will be
324 // done in Dolphin, but the Information Panel must be updated to
325 // indicate an invalid directory.
331 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
333 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
334 int count
= placesModel
->rowCount();
336 for (int i
= 0; i
< count
; ++i
) {
337 QModelIndex index
= placesModel
->index(i
, 0);
339 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
340 m_nameLabel
->setText(placesModel
->text(index
));
341 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
349 void InfoSidebarPage::cancelRequest()
354 void InfoSidebarPage::showMetaInfo()
356 m_metaTextLabel
->clear();
358 if (showMultipleSelectionInfo()) {
359 if (m_metaDataWidget
!= 0) {
361 foreach (const KFileItem
& item
, m_selection
) {
362 urls
.append(item
.targetUrl());
364 m_metaDataWidget
->setFiles(urls
);
367 quint64 totalSize
= 0;
368 foreach (const KFileItem
& item
, m_selection
) {
369 // Only count the size of files, not dirs to match what
370 // DolphinViewContainer::selectionStatusBarText() does.
371 if (!item
.isDir() && !item
.isLink()) {
372 totalSize
+= item
.size();
375 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
377 const KFileItem item
= fileItem();
379 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
380 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
382 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
384 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
385 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
387 if (item
.isLocalFile()) {
388 // TODO: See convertMetaInfo below, find a way to display only interesting information
390 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
391 KFileMetaInfo::TechnicalInfo
|
392 KFileMetaInfo::ContentInfo
;
393 const QString path
= item
.url().path();
394 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
395 if (fileMetaInfo
.isValid()) {
396 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
397 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
398 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
401 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
402 const QVariant
& value
= metaInfoItem
.value();
403 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
404 m_metaTextLabel
->add(labelText
, value
.toString());
412 if (m_metaDataWidget
!= 0) {
413 m_metaDataWidget
->setFile(item
.targetUrl());
418 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
425 // sorted list of keys, where its data should be shown
426 static const MetaKey keys
[] = {
427 { "http://freedesktop.org/standards/xesam/1.0/core#album", i18nc("@label", "Album:") },
428 { "http://freedesktop.org/standards/xesam/1.0/core#artist", i18nc("@label", "Artist:") },
429 { "http://freedesktop.org/standards/xesam/1.0/core#genre", i18nc("@label", "Genre:") },
430 { "http://freedesktop.org/standards/xesam/1.0/core#height", i18nc("@label", "Height:") },
431 { "http://freedesktop.org/standards/xesam/1.0/core#lineCount", i18nc("@label", "Lines:") },
432 { "http://freedesktop.org/standards/xesam/1.0/core#title", i18nc("@label", "Title:") },
433 { "http://freedesktop.org/standards/xesam/1.0/core#type", i18nc("@label", "Type:") },
434 { "http://freedesktop.org/standards/xesam/1.0/core#trackNumber", i18nc("@label", "Track:") },
435 { "http://freedesktop.org/standards/xesam/1.0/core#width", i18nc("@label", "Width:") }
438 // do a binary search for the key...
440 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
441 while (top
<= bottom
) {
442 const int middle
= (top
+ bottom
) / 2;
443 const int result
= key
.compare(keys
[middle
].key
);
446 } else if (result
> 0) {
449 text
= keys
[middle
].text
;
457 KFileItem
InfoSidebarPage::fileItem() const
459 if (!m_fileItem
.isNull()) {
463 if (!m_selection
.isEmpty()) {
464 Q_ASSERT(m_selection
.count() == 1);
465 return m_selection
.first();
468 // no item is hovered and no selection has been done: provide
469 // an item for the directory represented by m_shownUrl
470 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
475 bool InfoSidebarPage::showMultipleSelectionInfo() const
477 return m_fileItem
.isNull() && (m_selection
.count() > 1);
480 bool InfoSidebarPage::isEqualToShownUrl(const KUrl
& url
) const
482 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
485 void InfoSidebarPage::init()
487 const int spacing
= KDialog::spacingHint();
489 m_infoTimer
= new QTimer(this);
490 m_infoTimer
->setInterval(300);
491 m_infoTimer
->setSingleShot(true);
492 connect(m_infoTimer
, SIGNAL(timeout()),
493 this, SLOT(slotInfoTimeout()));
495 // Initialize timer for disabling an outdated preview with a small
496 // delay. This prevents flickering if the new preview can be generated
497 // within a very small timeframe.
498 m_outdatedPreviewTimer
= new QTimer(this);
499 m_outdatedPreviewTimer
->setInterval(300);
500 m_outdatedPreviewTimer
->setSingleShot(true);
501 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
502 this, SLOT(markOutdatedPreview()));
504 QVBoxLayout
* layout
= new QVBoxLayout
;
505 layout
->setSpacing(spacing
);
508 m_nameLabel
= new QLabel(this);
509 QFont font
= m_nameLabel
->font();
511 m_nameLabel
->setFont(font
);
512 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
513 m_nameLabel
->setWordWrap(true);
514 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
517 m_preview
= new PixmapViewer(this);
518 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
519 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
521 if (MetaDataWidget::metaDataAvailable()) {
522 // rating, comment and tags
523 m_metaDataWidget
= new MetaDataWidget(this);
524 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
527 // general meta text information
528 m_metaTextLabel
= new MetaTextLabel(this);
529 m_metaTextLabel
->setMinimumWidth(spacing
);
530 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
532 layout
->addWidget(m_nameLabel
);
533 layout
->addWidget(new KSeparator(this));
534 layout
->addWidget(m_preview
);
535 layout
->addWidget(new KSeparator(this));
536 if (m_metaDataWidget
!= 0) {
537 layout
->addWidget(m_metaDataWidget
);
538 layout
->addWidget(new KSeparator(this));
540 layout
->addWidget(m_metaTextLabel
);
542 // ensure that widgets in the information side bar are aligned towards the top
543 layout
->addStretch(1);
546 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
547 QDBusConnection::sessionBus(), this);
548 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
549 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
550 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
551 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
552 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
553 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
555 m_initialized
= true;
558 #include "infosidebarpage.moc"