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() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
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();
114 if ((count
== 1) && !selection
.first().url().isEmpty()) {
115 m_urlCandidate
= selection
.first().url();
117 m_infoTimer
->start();
121 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
123 if (!m_initialized
) {
129 m_fileItem
= KFileItem();
131 // The cursor is above the viewport. If files are selected,
132 // show information regarding the selection.
133 if (m_selection
.size() > 0) {
134 m_pendingPreview
= false;
135 m_infoTimer
->start();
137 } else if (!item
.url().isEmpty()) {
138 m_urlCandidate
= item
.url();
140 m_infoTimer
->start();
144 void InfoSidebarPage::showEvent(QShowEvent
* event
)
146 SidebarPage::showEvent(event
);
147 if (!event
->spontaneous()) {
148 if (!m_initialized
) {
149 // do a delayed initialization so that no performance
150 // penalty is given when Dolphin is started with a closed
158 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
161 // If the text inside the name label or the info label cannot
162 // get wrapped, then the maximum width of the label is increased
163 // so that the width of the information sidebar gets increased.
164 // To prevent this, the maximum width is adjusted to
165 // the current width of the sidebar.
166 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
167 m_nameLabel
->setMaximumWidth(maxWidth
);
169 // try to increase the preview as large as possible
170 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
171 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
172 m_infoTimer
->start();
175 SidebarPage::resizeEvent(event
);
178 void InfoSidebarPage::showItemInfo()
186 const KUrl file
= fileUrl();
187 if (!file
.isValid()) {
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()));
198 } else if (!applyPlace(file
)) {
199 // try to get a preview pixmap from the item...
203 m_pendingPreview
= true;
205 // Mark the currently shown preview as outdated. This is done
206 // with a small delay to prevent a flickering when the next preview
207 // can be shown within a short timeframe.
208 m_outdatedPreviewTimer
->start();
210 KIO::PreviewJob
* job
= KIO::filePreview(list
,
217 job
->setIgnoreMaximumSize(true);
219 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
220 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
221 connect(job
, SIGNAL(failed(const KFileItem
&)),
222 this, SLOT(showIcon(const KFileItem
&)));
224 m_nameLabel
->setText(file
.fileName());
230 void InfoSidebarPage::slotInfoTimeout()
232 m_shownUrl
= m_urlCandidate
;
236 void InfoSidebarPage::markOutdatedPreview()
238 KIconEffect iconEffect
;
239 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
240 KIconLoader::Desktop
,
241 KIconLoader::DisabledState
);
242 m_preview
->setPixmap(disabledPixmap
);
245 void InfoSidebarPage::showIcon(const KFileItem
& item
)
247 m_outdatedPreviewTimer
->stop();
248 m_pendingPreview
= false;
249 if (!applyPlace(item
.url())) {
250 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
254 void InfoSidebarPage::showPreview(const KFileItem
& item
,
255 const QPixmap
& pixmap
)
257 m_outdatedPreviewTimer
->stop();
260 if (m_pendingPreview
) {
261 m_preview
->setPixmap(pixmap
);
262 m_pendingPreview
= false;
266 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
268 if (m_shownUrl
== KUrl(source
)) {
269 // the currently shown file has been renamed, hence update the item information
270 // for the renamed file
271 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
272 requestDelayedItemInfo(item
);
276 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
278 if (m_shownUrl
== KUrl(directory
)) {
279 // If the 'trash' icon changes because the trash has been emptied or got filled,
280 // the signal filesAdded("trash:/") will be emitted.
281 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
282 requestDelayedItemInfo(item
);
286 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
288 foreach (const QString
& fileName
, files
) {
289 if (m_shownUrl
== KUrl(fileName
)) {
296 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
298 foreach (const QString
& fileName
, files
) {
299 if (m_shownUrl
== KUrl(fileName
)) {
300 // the currently shown item has been removed, show
301 // the parent directory as fallback
309 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
311 if (m_shownUrl
== KUrl(directory
)) {
312 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
313 requestDelayedItemInfo(item
);
317 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
319 if (m_shownUrl
== KUrl(directory
)) {
320 // The signal 'leftDirectory' is also emitted when a media
321 // has been unmounted. In this case no directory change will be
322 // done in Dolphin, but the Information Panel must be updated to
323 // indicate an invalid directory.
329 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
331 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
332 int count
= placesModel
->rowCount();
334 for (int i
= 0; i
< count
; ++i
) {
335 QModelIndex index
= placesModel
->index(i
, 0);
337 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
338 m_nameLabel
->setText(placesModel
->text(index
));
339 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
347 void InfoSidebarPage::cancelRequest()
352 void InfoSidebarPage::showMetaInfo()
354 m_metaTextLabel
->clear();
356 if (showMultipleSelectionInfo()) {
357 if (m_metaDataWidget
!= 0) {
359 foreach (const KFileItem
& item
, m_selection
) {
360 urls
.append(item
.targetUrl());
362 m_metaDataWidget
->setFiles(urls
);
365 quint64 totalSize
= 0;
366 foreach (const KFileItem
& item
, m_selection
) {
367 // Only count the size of files, not dirs to match what
368 // DolphinViewContainer::selectionStatusBarText() does.
369 if (!item
.isDir() && !item
.isLink()) {
370 totalSize
+= item
.size();
373 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
375 KFileItem
fileItem(KFileItem::Unknown
, KFileItem::Unknown
, fileUrl());
378 if (fileItem
.isDir()) {
379 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
380 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
382 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
384 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
385 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
387 if (fileItem
.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
= fileItem
.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(fileItem
.targetUrl());
418 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
420 // TODO: This code prevents that interesting meta information might be hidden
421 // and only bypasses the current problem that not all the meta information should
422 // be shown to the user. Check whether it's possible with Nepomuk to show
423 // all "user relevant" information in a readable way...
430 // sorted list of keys, where its data should be shown
431 static const MetaKey keys
[] = {
432 { "audio.album", "Album:" },
433 { "audio.artist", "Artist:" },
434 { "audio.title", "Title:" },
437 // do a binary search for the key...
439 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
440 while (top
<= bottom
) {
441 const int middle
= (top
+ bottom
) / 2;
442 const int result
= key
.compare(keys
[middle
].key
);
445 } else if (result
> 0) {
448 text
= keys
[middle
].text
;
456 KUrl
InfoSidebarPage::fileUrl() const
458 return (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
461 bool InfoSidebarPage::showMultipleSelectionInfo() const
463 return m_fileItem
.isNull() && (m_selection
.count() > 1);
466 void InfoSidebarPage::init()
468 const int spacing
= KDialog::spacingHint();
470 m_infoTimer
= new QTimer(this);
471 m_infoTimer
->setInterval(300);
472 m_infoTimer
->setSingleShot(true);
473 connect(m_infoTimer
, SIGNAL(timeout()),
474 this, SLOT(slotInfoTimeout()));
476 // Initialize timer for disabling an outdated preview with a small
477 // delay. This prevents flickering if the new preview can be generated
478 // within a very small timeframe.
479 m_outdatedPreviewTimer
= new QTimer(this);
480 m_outdatedPreviewTimer
->setInterval(300);
481 m_outdatedPreviewTimer
->setSingleShot(true);
482 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
483 this, SLOT(markOutdatedPreview()));
485 QVBoxLayout
* layout
= new QVBoxLayout
;
486 layout
->setSpacing(spacing
);
489 m_nameLabel
= new QLabel(this);
490 QFont font
= m_nameLabel
->font();
492 m_nameLabel
->setFont(font
);
493 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
494 m_nameLabel
->setWordWrap(true);
495 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
498 m_preview
= new PixmapViewer(this);
499 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
500 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
502 if (MetaDataWidget::metaDataAvailable()) {
503 // rating, comment and tags
504 m_metaDataWidget
= new MetaDataWidget(this);
505 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
508 // general meta text information
509 m_metaTextLabel
= new MetaTextLabel(this);
510 m_metaTextLabel
->setMinimumWidth(spacing
);
511 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
513 layout
->addWidget(m_nameLabel
);
514 layout
->addWidget(new KSeparator(this));
515 layout
->addWidget(m_preview
);
516 layout
->addWidget(new KSeparator(this));
517 if (m_metaDataWidget
!= 0) {
518 layout
->addWidget(m_metaDataWidget
);
519 layout
->addWidget(new KSeparator(this));
521 layout
->addWidget(m_metaTextLabel
);
523 // ensure that widgets in the information side bar are aligned towards the top
524 layout
->addStretch(1);
527 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
528 QDBusConnection::sessionBus(), this);
529 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
530 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
531 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
532 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
533 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
534 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
536 m_initialized
= true;
539 #include "infosidebarpage.moc"