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 <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>
35 #include <kseparator.h>
36 #include <kiconloader.h>
39 #include <Nepomuk/Resource>
40 #include <Nepomuk/Types/Property>
41 #include <Nepomuk/Variant>
44 #include <Phonon/BackendCapabilities>
45 #include <Phonon/MediaObject>
46 #include <Phonon/SeekSlider>
49 #include <QInputDialog>
53 #include <QResizeEvent>
54 #include <QScrollArea>
55 #include <QTextLayout>
59 #include <QVBoxLayout>
61 #include "settings/dolphinsettings.h"
62 #include "informationpaneldialog.h"
63 #include "metadatawidget.h"
64 #include "metatextlabel.h"
65 #include "phononwidget.h"
66 #include "pixmapviewer.h"
68 InformationPanel::InformationPanel(QWidget
* parent
) :
71 m_pendingPreview(false),
73 m_outdatedPreviewTimer(0),
88 InformationPanel::~InformationPanel()
92 QSize
InformationPanel::sizeHint() const
94 QSize size
= Panel::sizeHint();
95 size
.setWidth(minimumSizeHint().width());
99 void InformationPanel::setUrl(const KUrl
& url
)
102 if (url
.isValid() && !isEqualToShownUrl(url
)) {
113 void InformationPanel::setSelection(const KFileItemList
& selection
)
119 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
120 // The selection has not really changed, only the current index.
121 // QItemSelectionModel emits a signal in this case and it is less
122 // expensive doing the check this way instead of patching
123 // DolphinView::emitSelectionChanged().
127 m_selection
= selection
;
129 const int count
= selection
.count();
131 if (!isEqualToShownUrl(url())) {
136 if ((count
== 1) && !selection
.first().url().isEmpty()) {
137 m_urlCandidate
= selection
.first().url();
139 m_infoTimer
->start();
143 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
151 m_fileItem
= KFileItem();
153 // The cursor is above the viewport. If files are selected,
154 // show information regarding the selection.
155 if (m_selection
.size() > 0) {
156 m_pendingPreview
= false;
157 m_infoTimer
->start();
160 const KUrl url
= item
.url();
161 if (url
.isValid() && !isEqualToShownUrl(url
)) {
162 m_urlCandidate
= item
.url();
164 m_infoTimer
->start();
169 void InformationPanel::showEvent(QShowEvent
* event
)
171 Panel::showEvent(event
);
172 if (!event
->spontaneous()) {
173 if (!m_initialized
) {
174 // do a delayed initialization so that no performance
175 // penalty is given when Dolphin is started with a closed
183 void InformationPanel::resizeEvent(QResizeEvent
* event
)
186 // If the text inside the name label or the info label cannot
187 // get wrapped, then the maximum width of the label is increased
188 // so that the width of the information panel gets increased.
189 // To prevent this, the maximum width is adjusted to
190 // the current width of the panel.
191 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
192 m_nameLabel
->setMaximumWidth(maxWidth
);
194 // try to increase the preview as large as possible
195 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
196 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
197 m_infoTimer
->start();
199 Panel::resizeEvent(event
);
202 bool InformationPanel::eventFilter(QObject
* obj
, QEvent
* event
)
204 // Check whether the size of the meta text area has changed and adjust
205 // the fixed width in a way that no horizontal scrollbar needs to be shown.
206 if ((obj
== m_metaTextArea
->viewport()) && (event
->type() == QEvent::Resize
)) {
207 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
208 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
210 return Panel::eventFilter(obj
, event
);
213 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
215 Panel::contextMenuEvent(event
);
218 popup
.addAction(i18nc("@action:inmenu", "Configure..."));
219 if (popup
.exec(QCursor::pos()) != 0) {
221 m_dialog
= new InformationPanelDialog(this);
222 m_dialog
->setAttribute(Qt::WA_DeleteOnClose
);
230 void InformationPanel::showItemInfo()
238 if (showMultipleSelectionInfo()) {
239 KIconLoader iconLoader
;
240 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
241 KIconLoader::NoGroup
,
242 KIconLoader::SizeEnormous
);
243 m_preview
->setPixmap(icon
);
244 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
247 const KFileItem item
= fileItem();
248 const KUrl itemUrl
= item
.url();
249 if (!applyPlace(itemUrl
)) {
250 // try to get a preview pixmap from the item...
251 m_pendingPreview
= true;
253 // Mark the currently shown preview as outdated. This is done
254 // with a small delay to prevent a flickering when the next preview
255 // can be shown within a short timeframe.
256 m_outdatedPreviewTimer
->start();
258 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
266 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
267 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
268 connect(job
, SIGNAL(failed(const KFileItem
&)),
269 this, SLOT(showIcon(const KFileItem
&)));
271 setNameLabelText(itemUrl
.fileName());
278 void InformationPanel::slotInfoTimeout()
280 m_shownUrl
= m_urlCandidate
;
284 void InformationPanel::markOutdatedPreview()
286 KIconEffect iconEffect
;
287 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
288 KIconLoader::Desktop
,
289 KIconLoader::DisabledState
);
290 m_preview
->setPixmap(disabledPixmap
);
293 void InformationPanel::showIcon(const KFileItem
& item
)
295 m_outdatedPreviewTimer
->stop();
296 m_pendingPreview
= false;
297 if (!applyPlace(item
.url())) {
298 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
302 void InformationPanel::showPreview(const KFileItem
& item
,
303 const QPixmap
& pixmap
)
305 m_outdatedPreviewTimer
->stop();
308 if (m_pendingPreview
) {
309 m_preview
->setPixmap(pixmap
);
310 m_pendingPreview
= false;
314 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
316 const KUrl sourceUrl
= KUrl(source
);
318 // Verify whether the renamed item is selected. If this is the case, the
319 // selection must be updated with the renamed item.
320 bool isSelected
= false;
321 for (int i
= m_selection
.size() - 1; i
>= 0; --i
) {
322 if (m_selection
[i
].url() == sourceUrl
) {
323 m_selection
.removeAt(i
);
329 if ((m_shownUrl
== sourceUrl
) || isSelected
) {
330 m_shownUrl
= KUrl(dest
);
331 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
333 m_selection
.append(m_fileItem
);
339 void InformationPanel::slotFilesAdded(const QString
& directory
)
341 if (m_shownUrl
== KUrl(directory
)) {
342 // If the 'trash' icon changes because the trash has been emptied or got filled,
343 // the signal filesAdded("trash:/") will be emitted.
344 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
345 requestDelayedItemInfo(item
);
349 void InformationPanel::slotFilesChanged(const QStringList
& files
)
351 foreach (const QString
& fileName
, files
) {
352 if (m_shownUrl
== KUrl(fileName
)) {
359 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
361 foreach (const QString
& fileName
, files
) {
362 if (m_shownUrl
== KUrl(fileName
)) {
363 // the currently shown item has been removed, show
364 // the parent directory as fallback
371 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
373 if (m_shownUrl
== KUrl(directory
)) {
374 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
375 requestDelayedItemInfo(item
);
379 void InformationPanel::slotLeftDirectory(const QString
& directory
)
381 if (m_shownUrl
== KUrl(directory
)) {
382 // The signal 'leftDirectory' is also emitted when a media
383 // has been unmounted. In this case no directory change will be
384 // done in Dolphin, but the Information Panel must be updated to
385 // indicate an invalid directory.
390 bool InformationPanel::applyPlace(const KUrl
& url
)
392 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
393 int count
= placesModel
->rowCount();
395 for (int i
= 0; i
< count
; ++i
) {
396 QModelIndex index
= placesModel
->index(i
, 0);
398 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
399 setNameLabelText(placesModel
->text(index
));
400 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
408 void InformationPanel::cancelRequest()
413 void InformationPanel::showMetaInfo()
415 m_metaTextLabel
->clear();
417 if (showMultipleSelectionInfo()) {
418 if (m_metaDataWidget
!= 0) {
420 foreach (const KFileItem
& item
, m_selection
) {
421 urls
.append(item
.targetUrl());
423 m_metaDataWidget
->setFiles(urls
);
426 quint64 totalSize
= 0;
427 foreach (const KFileItem
& item
, m_selection
) {
428 // Only count the size of files, not dirs to match what
429 // DolphinViewContainer::selectionStatusBarText() does.
430 if (!item
.isDir() && !item
.isLink()) {
431 totalSize
+= item
.size();
434 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
436 delete m_phononWidget
;
439 const KFileItem item
= fileItem();
441 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
442 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
444 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
446 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
447 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
450 Nepomuk::Resource
res(item
.url());
452 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
453 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
454 while (it
!= properties
.constEnd()) {
455 Nepomuk::Types::Property
prop(it
.key());
456 // TODO: use Nepomuk::formatValue(res, prop) if available
457 // instead of it.value().toString()
458 m_metaTextLabel
->add(prop
.label(), it
.value().toString());
464 if (m_metaDataWidget
!= 0) {
465 m_metaDataWidget
->setFile(item
.targetUrl());
468 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item
.mimetype())) {
469 if (m_phononWidget
== 0) {
470 m_phononWidget
= new PhononWidget(this);
472 QVBoxLayout
* vBoxLayout
= qobject_cast
<QVBoxLayout
*>(layout());
473 Q_ASSERT(vBoxLayout
!= 0);
474 vBoxLayout
->insertWidget(3, m_phononWidget
);
476 m_phononWidget
->setUrl(item
.url());
478 delete m_phononWidget
;
484 KFileItem
InformationPanel::fileItem() const
486 if (!m_fileItem
.isNull()) {
490 if (!m_selection
.isEmpty()) {
491 Q_ASSERT(m_selection
.count() == 1);
492 return m_selection
.first();
495 // no item is hovered and no selection has been done: provide
496 // an item for the directory represented by m_shownUrl
497 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
502 bool InformationPanel::showMultipleSelectionInfo() const
504 return m_fileItem
.isNull() && (m_selection
.count() > 1);
507 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
509 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
512 void InformationPanel::setNameLabelText(const QString
& text
)
514 QTextOption textOption
;
515 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
517 QTextLayout
textLayout(text
);
518 textLayout
.setFont(m_nameLabel
->font());
519 textLayout
.setTextOption(textOption
);
522 wrappedText
.reserve(text
.length());
524 // wrap the text to fit into the width of m_nameLabel
525 textLayout
.beginLayout();
526 QTextLine line
= textLayout
.createLine();
527 while (line
.isValid()) {
528 line
.setLineWidth(m_nameLabel
->width());
529 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
531 line
= textLayout
.createLine();
532 if (line
.isValid()) {
533 wrappedText
+= QChar::LineSeparator
;
536 textLayout
.endLayout();
538 m_nameLabel
->setText(wrappedText
);
541 void InformationPanel::reset()
545 m_fileItem
= KFileItem();
549 void InformationPanel::init()
551 const int spacing
= KDialog::spacingHint();
553 m_infoTimer
= new QTimer(this);
554 m_infoTimer
->setInterval(300);
555 m_infoTimer
->setSingleShot(true);
556 connect(m_infoTimer
, SIGNAL(timeout()),
557 this, SLOT(slotInfoTimeout()));
559 // Initialize timer for disabling an outdated preview with a small
560 // delay. This prevents flickering if the new preview can be generated
561 // within a very small timeframe.
562 m_outdatedPreviewTimer
= new QTimer(this);
563 m_outdatedPreviewTimer
->setInterval(300);
564 m_outdatedPreviewTimer
->setSingleShot(true);
565 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
566 this, SLOT(markOutdatedPreview()));
568 QVBoxLayout
* layout
= new QVBoxLayout
;
569 layout
->setSpacing(spacing
);
572 m_nameLabel
= new QLabel(this);
573 QFont font
= m_nameLabel
->font();
575 m_nameLabel
->setFont(font
);
576 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
577 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
580 m_preview
= new PixmapViewer(this);
581 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
582 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
584 if (MetaDataWidget::metaDataAvailable()) {
585 // rating, comment and tags
586 m_metaDataWidget
= new MetaDataWidget(this);
587 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
590 // general meta text information
591 m_metaTextLabel
= new MetaTextLabel(this);
592 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
594 m_metaTextArea
= new QScrollArea(this);
595 m_metaTextArea
->setWidget(m_metaTextLabel
);
596 m_metaTextArea
->setWidgetResizable(true);
597 m_metaTextArea
->setFrameShape(QFrame::NoFrame
);
599 QWidget
* viewport
= m_metaTextArea
->viewport();
600 viewport
->installEventFilter(this);
602 QPalette palette
= viewport
->palette();
603 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
604 viewport
->setPalette(palette
);
606 layout
->addWidget(m_nameLabel
);
607 layout
->addWidget(new KSeparator(this));
608 layout
->addWidget(m_preview
);
609 layout
->addWidget(new KSeparator(this));
610 if (m_metaDataWidget
!= 0) {
611 layout
->addWidget(m_metaDataWidget
);
612 layout
->addWidget(new KSeparator(this));
614 layout
->addWidget(m_metaTextArea
);
617 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
618 QDBusConnection::sessionBus(), this);
619 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
620 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
621 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
622 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
623 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
624 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
626 m_initialized
= true;
629 #include "informationpanel.moc"