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 "informationpanel.h"
22 #include <config-nepomuk.h>
25 #include <kdirnotify.h>
26 #include <kfileitem.h>
27 #include <kfilemetainfo.h>
28 #include <kfileplacesmodel.h>
29 #include <kglobalsettings.h>
30 #include <kio/previewjob.h>
31 #include <kiconeffect.h>
32 #include <kiconloader.h>
35 #include <kseparator.h>
38 #include <Nepomuk/Resource>
39 #include <Nepomuk/Types/Property>
40 #include <Nepomuk/Variant>
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
48 #include <QInputDialog>
52 #include <QResizeEvent>
53 #include <QScrollArea>
54 #include <QTextLayout>
58 #include <QVBoxLayout>
60 #include "settings/dolphinsettings.h"
61 #include "metadatawidget.h"
62 #include "metatextlabel.h"
63 #include "phononwidget.h"
64 #include "pixmapviewer.h"
67 * Helper function for sorting items with qSort() in
68 * InformationPanel::contextMenu().
70 bool lessThan(const QAction
* action1
, const QAction
* action2
)
72 return action1
->text() < action2
->text();
76 InformationPanel::InformationPanel(QWidget
* parent
) :
79 m_pendingPreview(false),
81 m_outdatedPreviewTimer(0),
95 InformationPanel::~InformationPanel()
99 QSize
InformationPanel::sizeHint() const
101 QSize size
= Panel::sizeHint();
102 size
.setWidth(minimumSizeHint().width());
106 void InformationPanel::setUrl(const KUrl
& url
)
109 if (url
.isValid() && !isEqualToShownUrl(url
)) {
120 void InformationPanel::setSelection(const KFileItemList
& selection
)
126 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
127 // The selection has not really changed, only the current index.
128 // QItemSelectionModel emits a signal in this case and it is less
129 // expensive doing the check this way instead of patching
130 // DolphinView::emitSelectionChanged().
134 m_selection
= selection
;
136 const int count
= selection
.count();
138 if (!isEqualToShownUrl(url())) {
143 if ((count
== 1) && !selection
.first().url().isEmpty()) {
144 m_urlCandidate
= selection
.first().url();
146 m_infoTimer
->start();
150 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
158 m_fileItem
= KFileItem();
160 // The cursor is above the viewport. If files are selected,
161 // show information regarding the selection.
162 if (m_selection
.size() > 0) {
163 m_pendingPreview
= false;
164 m_infoTimer
->start();
167 const KUrl url
= item
.url();
168 if (url
.isValid() && !isEqualToShownUrl(url
)) {
169 m_urlCandidate
= item
.url();
171 m_infoTimer
->start();
176 void InformationPanel::showEvent(QShowEvent
* event
)
178 Panel::showEvent(event
);
179 if (!event
->spontaneous()) {
180 if (!m_initialized
) {
181 // do a delayed initialization so that no performance
182 // penalty is given when Dolphin is started with a closed
190 void InformationPanel::resizeEvent(QResizeEvent
* event
)
193 // If the text inside the name label or the info label cannot
194 // get wrapped, then the maximum width of the label is increased
195 // so that the width of the information panel gets increased.
196 // To prevent this, the maximum width is adjusted to
197 // the current width of the panel.
198 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
199 m_nameLabel
->setMaximumWidth(maxWidth
);
201 // try to increase the preview as large as possible
202 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
203 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
204 m_infoTimer
->start();
206 Panel::resizeEvent(event
);
209 bool InformationPanel::eventFilter(QObject
* obj
, QEvent
* event
)
211 // Check whether the size of the meta text area has changed and adjust
212 // the fixed width in a way that no horizontal scrollbar needs to be shown.
213 if ((obj
== m_metaTextArea
->viewport()) && (event
->type() == QEvent::Resize
)) {
214 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
215 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
217 return Panel::eventFilter(obj
, event
);
220 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
222 Panel::contextMenuEvent(event
);
225 if (showMultipleSelectionInfo()) {
230 popup
.addTitle(i18nc("@title:menu", "Shown Information"));
232 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
233 KConfigGroup settings
= config
.group("Show");
234 initMetaInfoSettings(settings
);
236 QList
<QAction
*> actions
;
238 // Get all meta information labels that are available for
239 // the currently shown file item and add them to the popup.
240 Nepomuk::Resource
res(fileItem().url());
241 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
242 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
243 while (it
!= properties
.constEnd()) {
244 Nepomuk::Types::Property
prop(it
.key());
245 const QString key
= prop
.label();
247 // Meta information provided by Nepomuk that is already
248 // available from KFileItem should not be configurable.
249 bool skip
= (key
== "fileExtension") ||
251 (key
== "sourceModified") ||
253 (key
== "mime type");
255 // Check whether there is already a meta information
256 // having the same label. In this case don't show it
257 // twice in the menu.
258 foreach (const QAction
* action
, actions
) {
259 if (action
->data().toString() == key
) {
267 const QString label
= key
; // TODO
268 QAction
* action
= new QAction(label
, &popup
);
269 action
->setCheckable(true);
270 action
->setChecked(settings
.readEntry(key
, true));
271 action
->setData(key
);
272 actions
.append(action
);
278 if (actions
.count() == 0) {
282 // add all items alphabetically sorted to the popup
283 qSort(actions
.begin(), actions
.end(), lessThan
);
284 foreach (QAction
* action
, actions
) {
285 popup
.addAction(action
);
288 // Open the popup and adjust the settings for the
290 QAction
* action
= popup
.exec(QCursor::pos());
292 settings
.writeEntry(action
->data().toString(), action
->isChecked());
299 void InformationPanel::showItemInfo()
307 if (showMultipleSelectionInfo()) {
308 KIconLoader iconLoader
;
309 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
310 KIconLoader::NoGroup
,
311 KIconLoader::SizeEnormous
);
312 m_preview
->setPixmap(icon
);
313 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
316 const KFileItem item
= fileItem();
317 const KUrl itemUrl
= item
.url();
318 if (!applyPlace(itemUrl
)) {
319 // try to get a preview pixmap from the item...
320 m_pendingPreview
= true;
322 // Mark the currently shown preview as outdated. This is done
323 // with a small delay to prevent a flickering when the next preview
324 // can be shown within a short timeframe.
325 m_outdatedPreviewTimer
->start();
327 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
335 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
336 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
337 connect(job
, SIGNAL(failed(const KFileItem
&)),
338 this, SLOT(showIcon(const KFileItem
&)));
340 setNameLabelText(itemUrl
.fileName());
347 void InformationPanel::slotInfoTimeout()
349 m_shownUrl
= m_urlCandidate
;
353 void InformationPanel::markOutdatedPreview()
355 KIconEffect iconEffect
;
356 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
357 KIconLoader::Desktop
,
358 KIconLoader::DisabledState
);
359 m_preview
->setPixmap(disabledPixmap
);
362 void InformationPanel::showIcon(const KFileItem
& item
)
364 m_outdatedPreviewTimer
->stop();
365 m_pendingPreview
= false;
366 if (!applyPlace(item
.url())) {
367 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
371 void InformationPanel::showPreview(const KFileItem
& item
,
372 const QPixmap
& pixmap
)
374 m_outdatedPreviewTimer
->stop();
377 if (m_pendingPreview
) {
378 m_preview
->setPixmap(pixmap
);
379 m_pendingPreview
= false;
383 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
385 const KUrl sourceUrl
= KUrl(source
);
387 // Verify whether the renamed item is selected. If this is the case, the
388 // selection must be updated with the renamed item.
389 bool isSelected
= false;
390 for (int i
= m_selection
.size() - 1; i
>= 0; --i
) {
391 if (m_selection
[i
].url() == sourceUrl
) {
392 m_selection
.removeAt(i
);
398 if ((m_shownUrl
== sourceUrl
) || isSelected
) {
399 m_shownUrl
= KUrl(dest
);
400 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
402 m_selection
.append(m_fileItem
);
408 void InformationPanel::slotFilesAdded(const QString
& directory
)
410 if (m_shownUrl
== KUrl(directory
)) {
411 // If the 'trash' icon changes because the trash has been emptied or got filled,
412 // the signal filesAdded("trash:/") will be emitted.
413 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
414 requestDelayedItemInfo(item
);
418 void InformationPanel::slotFilesChanged(const QStringList
& files
)
420 foreach (const QString
& fileName
, files
) {
421 if (m_shownUrl
== KUrl(fileName
)) {
428 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
430 foreach (const QString
& fileName
, files
) {
431 if (m_shownUrl
== KUrl(fileName
)) {
432 // the currently shown item has been removed, show
433 // the parent directory as fallback
440 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
442 if (m_shownUrl
== KUrl(directory
)) {
443 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
444 requestDelayedItemInfo(item
);
448 void InformationPanel::slotLeftDirectory(const QString
& directory
)
450 if (m_shownUrl
== KUrl(directory
)) {
451 // The signal 'leftDirectory' is also emitted when a media
452 // has been unmounted. In this case no directory change will be
453 // done in Dolphin, but the Information Panel must be updated to
454 // indicate an invalid directory.
459 bool InformationPanel::applyPlace(const KUrl
& url
)
461 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
462 int count
= placesModel
->rowCount();
464 for (int i
= 0; i
< count
; ++i
) {
465 QModelIndex index
= placesModel
->index(i
, 0);
467 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
468 setNameLabelText(placesModel
->text(index
));
469 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
477 void InformationPanel::cancelRequest()
482 void InformationPanel::showMetaInfo()
484 m_metaTextLabel
->clear();
486 if (showMultipleSelectionInfo()) {
487 if (m_metaDataWidget
!= 0) {
489 foreach (const KFileItem
& item
, m_selection
) {
490 urls
.append(item
.targetUrl());
492 m_metaDataWidget
->setFiles(urls
);
495 quint64 totalSize
= 0;
496 foreach (const KFileItem
& item
, m_selection
) {
497 // Only count the size of files, not dirs to match what
498 // DolphinViewContainer::selectionStatusBarText() does.
499 if (!item
.isDir() && !item
.isLink()) {
500 totalSize
+= item
.size();
503 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
505 delete m_phononWidget
;
508 const KFileItem item
= fileItem();
510 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
511 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
513 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
515 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
516 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
519 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
520 KConfigGroup settings
= config
.group("Show");
521 initMetaInfoSettings(settings
);
523 Nepomuk::Resource
res(item
.url());
525 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
526 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
527 while (it
!= properties
.constEnd()) {
528 Nepomuk::Types::Property
prop(it
.key());
529 const QString label
= prop
.label();
530 if (settings
.readEntry(label
, true)) {
531 // TODO: use Nepomuk::formatValue(res, prop) if available
532 // instead of it.value().toString()
533 m_metaTextLabel
->add(label
, it
.value().toString());
540 if (m_metaDataWidget
!= 0) {
541 m_metaDataWidget
->setFile(item
.targetUrl());
544 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item
.mimetype())) {
545 if (m_phononWidget
== 0) {
546 m_phononWidget
= new PhononWidget(this);
548 QVBoxLayout
* vBoxLayout
= qobject_cast
<QVBoxLayout
*>(layout());
549 Q_ASSERT(vBoxLayout
!= 0);
550 vBoxLayout
->insertWidget(3, m_phononWidget
);
552 m_phononWidget
->setUrl(item
.url());
554 delete m_phononWidget
;
560 KFileItem
InformationPanel::fileItem() const
562 if (!m_fileItem
.isNull()) {
566 if (!m_selection
.isEmpty()) {
567 Q_ASSERT(m_selection
.count() == 1);
568 return m_selection
.first();
571 // no item is hovered and no selection has been done: provide
572 // an item for the directory represented by m_shownUrl
573 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
578 bool InformationPanel::showMultipleSelectionInfo() const
580 return m_fileItem
.isNull() && (m_selection
.count() > 1);
583 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
585 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
588 void InformationPanel::setNameLabelText(const QString
& text
)
590 QTextOption textOption
;
591 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
593 QTextLayout
textLayout(text
);
594 textLayout
.setFont(m_nameLabel
->font());
595 textLayout
.setTextOption(textOption
);
598 wrappedText
.reserve(text
.length());
600 // wrap the text to fit into the width of m_nameLabel
601 textLayout
.beginLayout();
602 QTextLine line
= textLayout
.createLine();
603 while (line
.isValid()) {
604 line
.setLineWidth(m_nameLabel
->width());
605 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
607 line
= textLayout
.createLine();
608 if (line
.isValid()) {
609 wrappedText
+= QChar::LineSeparator
;
612 textLayout
.endLayout();
614 m_nameLabel
->setText(wrappedText
);
617 void InformationPanel::reset()
621 m_fileItem
= KFileItem();
625 void InformationPanel::initMetaInfoSettings(KConfigGroup
& group
)
627 if (!group
.readEntry("initialized", false)) {
628 // The resource file is read the first time. Assure
629 // that some meta information is disabled per default.
630 group
.writeEntry("fileExtension", false);
631 group
.writeEntry("url", false);
632 group
.writeEntry("sourceModified", false);
633 group
.writeEntry("parentUrl", false);
634 group
.writeEntry("size", false);
635 group
.writeEntry("mime type", false);
636 group
.writeEntry("depth", false);
637 group
.writeEntry("name", false);
639 // mark the group as initialized
640 group
.writeEntry("initialized", true);
644 void InformationPanel::init()
646 const int spacing
= KDialog::spacingHint();
648 m_infoTimer
= new QTimer(this);
649 m_infoTimer
->setInterval(300);
650 m_infoTimer
->setSingleShot(true);
651 connect(m_infoTimer
, SIGNAL(timeout()),
652 this, SLOT(slotInfoTimeout()));
654 // Initialize timer for disabling an outdated preview with a small
655 // delay. This prevents flickering if the new preview can be generated
656 // within a very small timeframe.
657 m_outdatedPreviewTimer
= new QTimer(this);
658 m_outdatedPreviewTimer
->setInterval(300);
659 m_outdatedPreviewTimer
->setSingleShot(true);
660 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
661 this, SLOT(markOutdatedPreview()));
663 QVBoxLayout
* layout
= new QVBoxLayout
;
664 layout
->setSpacing(spacing
);
667 m_nameLabel
= new QLabel(this);
668 QFont font
= m_nameLabel
->font();
670 m_nameLabel
->setFont(font
);
671 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
672 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
675 m_preview
= new PixmapViewer(this);
676 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
677 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
679 if (MetaDataWidget::metaDataAvailable()) {
680 // rating, comment and tags
681 m_metaDataWidget
= new MetaDataWidget(this);
682 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
685 // general meta text information
686 m_metaTextLabel
= new MetaTextLabel(this);
687 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
689 m_metaTextArea
= new QScrollArea(this);
690 m_metaTextArea
->setWidget(m_metaTextLabel
);
691 m_metaTextArea
->setWidgetResizable(true);
692 m_metaTextArea
->setFrameShape(QFrame::NoFrame
);
694 QWidget
* viewport
= m_metaTextArea
->viewport();
695 viewport
->installEventFilter(this);
697 QPalette palette
= viewport
->palette();
698 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
699 viewport
->setPalette(palette
);
701 layout
->addWidget(m_nameLabel
);
702 layout
->addWidget(new KSeparator(this));
703 layout
->addWidget(m_preview
);
704 layout
->addWidget(new KSeparator(this));
705 if (m_metaDataWidget
!= 0) {
706 layout
->addWidget(m_metaDataWidget
);
707 layout
->addWidget(new KSeparator(this));
709 layout
->addWidget(m_metaTextArea
);
712 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
713 QDBusConnection::sessionBus(), this);
714 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
715 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
716 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
717 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
718 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
719 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
721 m_initialized
= true;
724 #include "informationpanel.moc"