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
)
125 if (!m_initialized
) {
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()));
199 const KFileItem item
= fileItem();
200 const KUrl itemUrl
= item
.url();
201 if (!applyPlace(itemUrl
)) {
202 // 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(KUrl::List() << itemUrl
,
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(itemUrl
.fileName());
231 void InfoSidebarPage::slotInfoTimeout()
233 m_shownUrl
= m_urlCandidate
;
237 void InfoSidebarPage::markOutdatedPreview()
239 KIconEffect iconEffect
;
240 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
241 KIconLoader::Desktop
,
242 KIconLoader::DisabledState
);
243 m_preview
->setPixmap(disabledPixmap
);
246 void InfoSidebarPage::showIcon(const KFileItem
& item
)
248 m_outdatedPreviewTimer
->stop();
249 m_pendingPreview
= false;
250 if (!applyPlace(item
.url())) {
251 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
255 void InfoSidebarPage::showPreview(const KFileItem
& item
,
256 const QPixmap
& pixmap
)
258 m_outdatedPreviewTimer
->stop();
261 if (m_pendingPreview
) {
262 m_preview
->setPixmap(pixmap
);
263 m_pendingPreview
= false;
267 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
269 if (m_shownUrl
== KUrl(source
)) {
270 // the currently shown file has been renamed, hence update the item information
271 // for the renamed file
272 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
273 requestDelayedItemInfo(item
);
277 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
279 if (m_shownUrl
== KUrl(directory
)) {
280 // If the 'trash' icon changes because the trash has been emptied or got filled,
281 // the signal filesAdded("trash:/") will be emitted.
282 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
283 requestDelayedItemInfo(item
);
287 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
289 foreach (const QString
& fileName
, files
) {
290 if (m_shownUrl
== KUrl(fileName
)) {
297 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
299 foreach (const QString
& fileName
, files
) {
300 if (m_shownUrl
== KUrl(fileName
)) {
301 // the currently shown item has been removed, show
302 // the parent directory as fallback
310 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
312 if (m_shownUrl
== KUrl(directory
)) {
313 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
314 requestDelayedItemInfo(item
);
318 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
320 if (m_shownUrl
== KUrl(directory
)) {
321 // The signal 'leftDirectory' is also emitted when a media
322 // has been unmounted. In this case no directory change will be
323 // done in Dolphin, but the Information Panel must be updated to
324 // indicate an invalid directory.
330 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
332 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
333 int count
= placesModel
->rowCount();
335 for (int i
= 0; i
< count
; ++i
) {
336 QModelIndex index
= placesModel
->index(i
, 0);
338 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
339 m_nameLabel
->setText(placesModel
->text(index
));
340 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
348 void InfoSidebarPage::cancelRequest()
353 void InfoSidebarPage::showMetaInfo()
355 m_metaTextLabel
->clear();
357 if (showMultipleSelectionInfo()) {
358 if (m_metaDataWidget
!= 0) {
360 foreach (const KFileItem
& item
, m_selection
) {
361 urls
.append(item
.targetUrl());
363 m_metaDataWidget
->setFiles(urls
);
366 quint64 totalSize
= 0;
367 foreach (const KFileItem
& item
, m_selection
) {
368 // Only count the size of files, not dirs to match what
369 // DolphinViewContainer::selectionStatusBarText() does.
370 if (!item
.isDir() && !item
.isLink()) {
371 totalSize
+= item
.size();
374 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
376 const KFileItem item
= fileItem();
378 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
379 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
381 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
383 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
384 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
386 if (item
.isLocalFile()) {
387 // TODO: See convertMetaInfo below, find a way to display only interesting information
389 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
390 KFileMetaInfo::TechnicalInfo
|
391 KFileMetaInfo::ContentInfo
;
392 const QString path
= item
.url().path();
393 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
394 if (fileMetaInfo
.isValid()) {
395 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
396 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
397 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
400 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
401 const QVariant
& value
= metaInfoItem
.value();
402 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
403 m_metaTextLabel
->add(labelText
, value
.toString());
411 if (m_metaDataWidget
!= 0) {
412 m_metaDataWidget
->setFile(item
.targetUrl());
417 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
419 // TODO: This code prevents that interesting meta information might be hidden
420 // and only bypasses the current problem that not all the meta information should
421 // be shown to the user. Check whether it's possible with Nepomuk to show
422 // all "user relevant" information in a readable way...
429 // sorted list of keys, where its data should be shown
430 static const MetaKey keys
[] = {
431 { "audio.album", "Album:" },
432 { "audio.artist", "Artist:" },
433 { "audio.title", "Title:" },
436 // do a binary search for the key...
438 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
439 while (top
<= bottom
) {
440 const int middle
= (top
+ bottom
) / 2;
441 const int result
= key
.compare(keys
[middle
].key
);
444 } else if (result
> 0) {
447 text
= keys
[middle
].text
;
455 KFileItem
InfoSidebarPage::fileItem() const
457 if (!m_fileItem
.isNull()) {
461 if (!m_selection
.isEmpty()) {
462 Q_ASSERT(m_selection
.count() == 1);
463 return m_selection
.first();
466 // no item is hovered and no selection has been done: provide
467 // an item for the directory represented by m_shownUrl
468 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
473 bool InfoSidebarPage::showMultipleSelectionInfo() const
475 return m_fileItem
.isNull() && (m_selection
.count() > 1);
478 bool InfoSidebarPage::isEqualToShownUrl(const KUrl
& url
) const
480 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
483 void InfoSidebarPage::init()
485 const int spacing
= KDialog::spacingHint();
487 m_infoTimer
= new QTimer(this);
488 m_infoTimer
->setInterval(300);
489 m_infoTimer
->setSingleShot(true);
490 connect(m_infoTimer
, SIGNAL(timeout()),
491 this, SLOT(slotInfoTimeout()));
493 // Initialize timer for disabling an outdated preview with a small
494 // delay. This prevents flickering if the new preview can be generated
495 // within a very small timeframe.
496 m_outdatedPreviewTimer
= new QTimer(this);
497 m_outdatedPreviewTimer
->setInterval(300);
498 m_outdatedPreviewTimer
->setSingleShot(true);
499 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
500 this, SLOT(markOutdatedPreview()));
502 QVBoxLayout
* layout
= new QVBoxLayout
;
503 layout
->setSpacing(spacing
);
506 m_nameLabel
= new QLabel(this);
507 QFont font
= m_nameLabel
->font();
509 m_nameLabel
->setFont(font
);
510 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
511 m_nameLabel
->setWordWrap(true);
512 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
515 m_preview
= new PixmapViewer(this);
516 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
517 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
519 if (MetaDataWidget::metaDataAvailable()) {
520 // rating, comment and tags
521 m_metaDataWidget
= new MetaDataWidget(this);
522 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
525 // general meta text information
526 m_metaTextLabel
= new MetaTextLabel(this);
527 m_metaTextLabel
->setMinimumWidth(spacing
);
528 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
530 layout
->addWidget(m_nameLabel
);
531 layout
->addWidget(new KSeparator(this));
532 layout
->addWidget(m_preview
);
533 layout
->addWidget(new KSeparator(this));
534 if (m_metaDataWidget
!= 0) {
535 layout
->addWidget(m_metaDataWidget
);
536 layout
->addWidget(new KSeparator(this));
538 layout
->addWidget(m_metaTextLabel
);
540 // ensure that widgets in the information side bar are aligned towards the top
541 layout
->addStretch(1);
544 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
545 QDBusConnection::sessionBus(), this);
546 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
547 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
548 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
549 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
550 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
551 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
553 m_initialized
= true;
556 #include "infosidebarpage.moc"