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 if (showMultipleSelectionInfo()) {
187 KIconLoader iconLoader
;
188 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
189 KIconLoader::NoGroup
,
190 KIconLoader::SizeEnormous
);
191 m_preview
->setPixmap(icon
);
192 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
194 const KFileItem item
= fileItem();
195 const KUrl itemUrl
= item
.url();
196 if (!applyPlace(itemUrl
)) {
197 // try to get a preview pixmap from the item...
198 m_pendingPreview
= true;
200 // Mark the currently shown preview as outdated. This is done
201 // with a small delay to prevent a flickering when the next preview
202 // can be shown within a short timeframe.
203 m_outdatedPreviewTimer
->start();
205 KIO::PreviewJob
* job
= KIO::filePreview(KUrl::List() << itemUrl
,
212 job
->setIgnoreMaximumSize(true);
214 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
215 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
216 connect(job
, SIGNAL(failed(const KFileItem
&)),
217 this, SLOT(showIcon(const KFileItem
&)));
219 m_nameLabel
->setText(itemUrl
.fileName());
226 void InfoSidebarPage::slotInfoTimeout()
228 m_shownUrl
= m_urlCandidate
;
232 void InfoSidebarPage::markOutdatedPreview()
234 KIconEffect iconEffect
;
235 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
236 KIconLoader::Desktop
,
237 KIconLoader::DisabledState
);
238 m_preview
->setPixmap(disabledPixmap
);
241 void InfoSidebarPage::showIcon(const KFileItem
& item
)
243 m_outdatedPreviewTimer
->stop();
244 m_pendingPreview
= false;
245 if (!applyPlace(item
.url())) {
246 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
250 void InfoSidebarPage::showPreview(const KFileItem
& item
,
251 const QPixmap
& pixmap
)
253 m_outdatedPreviewTimer
->stop();
256 if (m_pendingPreview
) {
257 m_preview
->setPixmap(pixmap
);
258 m_pendingPreview
= false;
262 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
264 if (m_shownUrl
== KUrl(source
)) {
265 // the currently shown file has been renamed, hence update the item information
266 // for the renamed file
267 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
268 requestDelayedItemInfo(item
);
272 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
274 if (m_shownUrl
== KUrl(directory
)) {
275 // If the 'trash' icon changes because the trash has been emptied or got filled,
276 // the signal filesAdded("trash:/") will be emitted.
277 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
278 requestDelayedItemInfo(item
);
282 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
284 foreach (const QString
& fileName
, files
) {
285 if (m_shownUrl
== KUrl(fileName
)) {
292 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
294 foreach (const QString
& fileName
, files
) {
295 if (m_shownUrl
== KUrl(fileName
)) {
296 // the currently shown item has been removed, show
297 // the parent directory as fallback
305 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
307 if (m_shownUrl
== KUrl(directory
)) {
308 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
309 requestDelayedItemInfo(item
);
313 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
315 if (m_shownUrl
== KUrl(directory
)) {
316 // The signal 'leftDirectory' is also emitted when a media
317 // has been unmounted. In this case no directory change will be
318 // done in Dolphin, but the Information Panel must be updated to
319 // indicate an invalid directory.
325 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
327 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
328 int count
= placesModel
->rowCount();
330 for (int i
= 0; i
< count
; ++i
) {
331 QModelIndex index
= placesModel
->index(i
, 0);
333 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
334 m_nameLabel
->setText(placesModel
->text(index
));
335 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
343 void InfoSidebarPage::cancelRequest()
348 void InfoSidebarPage::showMetaInfo()
350 m_metaTextLabel
->clear();
352 if (showMultipleSelectionInfo()) {
353 if (m_metaDataWidget
!= 0) {
355 foreach (const KFileItem
& item
, m_selection
) {
356 urls
.append(item
.targetUrl());
358 m_metaDataWidget
->setFiles(urls
);
361 quint64 totalSize
= 0;
362 foreach (const KFileItem
& item
, m_selection
) {
363 // Only count the size of files, not dirs to match what
364 // DolphinViewContainer::selectionStatusBarText() does.
365 if (!item
.isDir() && !item
.isLink()) {
366 totalSize
+= item
.size();
369 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
371 const KFileItem item
= fileItem();
373 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
374 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
376 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
378 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
379 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
381 if (item
.isLocalFile()) {
382 // TODO: See convertMetaInfo below, find a way to display only interesting information
384 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
385 KFileMetaInfo::TechnicalInfo
|
386 KFileMetaInfo::ContentInfo
;
387 const QString path
= item
.url().path();
388 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
389 if (fileMetaInfo
.isValid()) {
390 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
391 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
392 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
395 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
396 const QVariant
& value
= metaInfoItem
.value();
397 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
398 m_metaTextLabel
->add(labelText
, value
.toString());
406 if (m_metaDataWidget
!= 0) {
407 m_metaDataWidget
->setFile(item
.targetUrl());
412 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
414 // TODO: This code prevents that interesting meta information might be hidden
415 // and only bypasses the current problem that not all the meta information should
416 // be shown to the user. Check whether it's possible with Nepomuk to show
417 // all "user relevant" information in a readable way...
424 // sorted list of keys, where its data should be shown
425 static const MetaKey keys
[] = {
426 { "audio.album", "Album:" },
427 { "audio.artist", "Artist:" },
428 { "audio.title", "Title:" },
431 // do a binary search for the key...
433 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
434 while (top
<= bottom
) {
435 const int middle
= (top
+ bottom
) / 2;
436 const int result
= key
.compare(keys
[middle
].key
);
439 } else if (result
> 0) {
442 text
= keys
[middle
].text
;
450 KFileItem
InfoSidebarPage::fileItem() const
452 if (!m_fileItem
.isNull()) {
456 if (!m_selection
.isEmpty()) {
457 Q_ASSERT(m_selection
.count() == 1);
458 return m_selection
.first();
461 // no item is hovered and no selection has been done: provide
462 // an item for the directory represented by m_shownUrl
463 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
468 bool InfoSidebarPage::showMultipleSelectionInfo() const
470 return m_fileItem
.isNull() && (m_selection
.count() > 1);
473 void InfoSidebarPage::init()
475 const int spacing
= KDialog::spacingHint();
477 m_infoTimer
= new QTimer(this);
478 m_infoTimer
->setInterval(300);
479 m_infoTimer
->setSingleShot(true);
480 connect(m_infoTimer
, SIGNAL(timeout()),
481 this, SLOT(slotInfoTimeout()));
483 // Initialize timer for disabling an outdated preview with a small
484 // delay. This prevents flickering if the new preview can be generated
485 // within a very small timeframe.
486 m_outdatedPreviewTimer
= new QTimer(this);
487 m_outdatedPreviewTimer
->setInterval(300);
488 m_outdatedPreviewTimer
->setSingleShot(true);
489 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
490 this, SLOT(markOutdatedPreview()));
492 QVBoxLayout
* layout
= new QVBoxLayout
;
493 layout
->setSpacing(spacing
);
496 m_nameLabel
= new QLabel(this);
497 QFont font
= m_nameLabel
->font();
499 m_nameLabel
->setFont(font
);
500 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
501 m_nameLabel
->setWordWrap(true);
502 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
505 m_preview
= new PixmapViewer(this);
506 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
507 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
509 if (MetaDataWidget::metaDataAvailable()) {
510 // rating, comment and tags
511 m_metaDataWidget
= new MetaDataWidget(this);
512 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
515 // general meta text information
516 m_metaTextLabel
= new MetaTextLabel(this);
517 m_metaTextLabel
->setMinimumWidth(spacing
);
518 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
520 layout
->addWidget(m_nameLabel
);
521 layout
->addWidget(new KSeparator(this));
522 layout
->addWidget(m_preview
);
523 layout
->addWidget(new KSeparator(this));
524 if (m_metaDataWidget
!= 0) {
525 layout
->addWidget(m_metaDataWidget
);
526 layout
->addWidget(new KSeparator(this));
528 layout
->addWidget(m_metaTextLabel
);
530 // ensure that widgets in the information side bar are aligned towards the top
531 layout
->addStretch(1);
534 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
535 QDBusConnection::sessionBus(), this);
536 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
537 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
538 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
539 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
540 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
541 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
543 m_initialized
= true;
546 #include "infosidebarpage.moc"