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>
34 #include <kseparator.h>
35 #include <kiconloader.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"
66 InformationPanel::InformationPanel(QWidget
* parent
) :
69 m_pendingPreview(false),
71 m_outdatedPreviewTimer(0),
85 InformationPanel::~InformationPanel()
89 QSize
InformationPanel::sizeHint() const
91 QSize size
= Panel::sizeHint();
92 size
.setWidth(minimumSizeHint().width());
96 void InformationPanel::setUrl(const KUrl
& url
)
99 if (url
.isValid() && !isEqualToShownUrl(url
)) {
110 void InformationPanel::setSelection(const KFileItemList
& selection
)
116 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
117 // The selection has not really changed, only the current index.
118 // QItemSelectionModel emits a signal in this case and it is less
119 // expensive doing the check this way instead of patching
120 // DolphinView::emitSelectionChanged().
124 m_selection
= selection
;
126 const int count
= selection
.count();
128 if (!isEqualToShownUrl(url())) {
133 if ((count
== 1) && !selection
.first().url().isEmpty()) {
134 m_urlCandidate
= selection
.first().url();
136 m_infoTimer
->start();
140 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
148 m_fileItem
= KFileItem();
150 // The cursor is above the viewport. If files are selected,
151 // show information regarding the selection.
152 if (m_selection
.size() > 0) {
153 m_pendingPreview
= false;
154 m_infoTimer
->start();
157 const KUrl url
= item
.url();
158 if (url
.isValid() && !isEqualToShownUrl(url
)) {
159 m_urlCandidate
= item
.url();
161 m_infoTimer
->start();
166 void InformationPanel::showEvent(QShowEvent
* event
)
168 Panel::showEvent(event
);
169 if (!event
->spontaneous()) {
170 if (!m_initialized
) {
171 // do a delayed initialization so that no performance
172 // penalty is given when Dolphin is started with a closed
180 void InformationPanel::resizeEvent(QResizeEvent
* event
)
183 // If the text inside the name label or the info label cannot
184 // get wrapped, then the maximum width of the label is increased
185 // so that the width of the information panel gets increased.
186 // To prevent this, the maximum width is adjusted to
187 // the current width of the panel.
188 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
189 m_nameLabel
->setMaximumWidth(maxWidth
);
191 // try to increase the preview as large as possible
192 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
193 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
194 m_infoTimer
->start();
196 Panel::resizeEvent(event
);
200 bool InformationPanel::eventFilter(QObject
* obj
, QEvent
* event
)
202 // Check whether the size of the meta text area has changed and adjust
203 // the fixed width in a way that no horizontal scrollbar needs to be shown.
204 if ((obj
== m_metaTextArea
->viewport()) && (event
->type() == QEvent::Resize
)) {
205 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
206 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
208 return Panel::eventFilter(obj
, event
);
211 void InformationPanel::showItemInfo()
219 if (showMultipleSelectionInfo()) {
220 KIconLoader iconLoader
;
221 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
222 KIconLoader::NoGroup
,
223 KIconLoader::SizeEnormous
);
224 m_preview
->setPixmap(icon
);
225 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
228 const KFileItem item
= fileItem();
229 const KUrl itemUrl
= item
.url();
230 if (!applyPlace(itemUrl
)) {
231 // try to get a preview pixmap from the item...
232 m_pendingPreview
= true;
234 // Mark the currently shown preview as outdated. This is done
235 // with a small delay to prevent a flickering when the next preview
236 // can be shown within a short timeframe.
237 m_outdatedPreviewTimer
->start();
239 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
247 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
248 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
249 connect(job
, SIGNAL(failed(const KFileItem
&)),
250 this, SLOT(showIcon(const KFileItem
&)));
252 setNameLabelText(itemUrl
.fileName());
259 void InformationPanel::slotInfoTimeout()
261 m_shownUrl
= m_urlCandidate
;
265 void InformationPanel::markOutdatedPreview()
267 KIconEffect iconEffect
;
268 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
269 KIconLoader::Desktop
,
270 KIconLoader::DisabledState
);
271 m_preview
->setPixmap(disabledPixmap
);
274 void InformationPanel::showIcon(const KFileItem
& item
)
276 m_outdatedPreviewTimer
->stop();
277 m_pendingPreview
= false;
278 if (!applyPlace(item
.url())) {
279 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
283 void InformationPanel::showPreview(const KFileItem
& item
,
284 const QPixmap
& pixmap
)
286 m_outdatedPreviewTimer
->stop();
289 if (m_pendingPreview
) {
290 m_preview
->setPixmap(pixmap
);
291 m_pendingPreview
= false;
295 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
297 if (m_shownUrl
== KUrl(source
)) {
298 // the currently shown file has been renamed, hence update the item information
299 // for the renamed file
300 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
301 requestDelayedItemInfo(item
);
305 void InformationPanel::slotFilesAdded(const QString
& directory
)
307 if (m_shownUrl
== KUrl(directory
)) {
308 // If the 'trash' icon changes because the trash has been emptied or got filled,
309 // the signal filesAdded("trash:/") will be emitted.
310 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
311 requestDelayedItemInfo(item
);
315 void InformationPanel::slotFilesChanged(const QStringList
& files
)
317 foreach (const QString
& fileName
, files
) {
318 if (m_shownUrl
== KUrl(fileName
)) {
325 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
327 foreach (const QString
& fileName
, files
) {
328 if (m_shownUrl
== KUrl(fileName
)) {
329 // the currently shown item has been removed, show
330 // the parent directory as fallback
338 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
340 if (m_shownUrl
== KUrl(directory
)) {
341 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
342 requestDelayedItemInfo(item
);
346 void InformationPanel::slotLeftDirectory(const QString
& directory
)
348 if (m_shownUrl
== KUrl(directory
)) {
349 // The signal 'leftDirectory' is also emitted when a media
350 // has been unmounted. In this case no directory change will be
351 // done in Dolphin, but the Information Panel must be updated to
352 // indicate an invalid directory.
358 bool InformationPanel::applyPlace(const KUrl
& url
)
360 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
361 int count
= placesModel
->rowCount();
363 for (int i
= 0; i
< count
; ++i
) {
364 QModelIndex index
= placesModel
->index(i
, 0);
366 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
367 setNameLabelText(placesModel
->text(index
));
368 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
376 void InformationPanel::cancelRequest()
381 void InformationPanel::showMetaInfo()
383 m_metaTextLabel
->clear();
385 if (showMultipleSelectionInfo()) {
386 if (m_metaDataWidget
!= 0) {
388 foreach (const KFileItem
& item
, m_selection
) {
389 urls
.append(item
.targetUrl());
391 m_metaDataWidget
->setFiles(urls
);
394 quint64 totalSize
= 0;
395 foreach (const KFileItem
& item
, m_selection
) {
396 // Only count the size of files, not dirs to match what
397 // DolphinViewContainer::selectionStatusBarText() does.
398 if (!item
.isDir() && !item
.isLink()) {
399 totalSize
+= item
.size();
402 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
404 const KFileItem item
= fileItem();
406 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
407 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
409 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
411 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
412 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
415 Nepomuk::Resource
res(item
.url());
417 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
418 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
419 while (it
!= properties
.constEnd()) {
420 Nepomuk::Types::Property
prop(it
.key());
421 // TODO: use Nepomuk::formatValue(res, prop) if available
422 // instead of it.value().toString()
423 m_metaTextLabel
->add(prop
.label(), it
.value().toString());
429 if (m_metaDataWidget
!= 0) {
430 m_metaDataWidget
->setFile(item
.targetUrl());
433 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item
.mimetype())) {
434 if (m_phononWidget
== 0) {
435 m_phononWidget
= new PhononWidget(this);
437 QVBoxLayout
* vBoxLayout
= qobject_cast
<QVBoxLayout
*>(layout());
438 Q_ASSERT(vBoxLayout
!= 0);
439 vBoxLayout
->insertWidget(3, m_phononWidget
);
441 m_phononWidget
->setUrl(item
.url());
443 delete m_phononWidget
;
449 KFileItem
InformationPanel::fileItem() const
451 if (!m_fileItem
.isNull()) {
455 if (!m_selection
.isEmpty()) {
456 Q_ASSERT(m_selection
.count() == 1);
457 return m_selection
.first();
460 // no item is hovered and no selection has been done: provide
461 // an item for the directory represented by m_shownUrl
462 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
467 bool InformationPanel::showMultipleSelectionInfo() const
469 return m_fileItem
.isNull() && (m_selection
.count() > 1);
472 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
474 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
477 void InformationPanel::setNameLabelText(const QString
& text
)
479 QTextOption textOption
;
480 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
482 QTextLayout
textLayout(text
);
483 textLayout
.setFont(m_nameLabel
->font());
484 textLayout
.setTextOption(textOption
);
487 wrappedText
.reserve(text
.length());
489 // wrap the text to fit into the width of m_nameLabel
490 textLayout
.beginLayout();
491 QTextLine line
= textLayout
.createLine();
492 while (line
.isValid()) {
493 line
.setLineWidth(m_nameLabel
->width());
494 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
496 line
= textLayout
.createLine();
497 if (line
.isValid()) {
498 wrappedText
+= QChar::LineSeparator
;
501 textLayout
.endLayout();
503 m_nameLabel
->setText(wrappedText
);
506 void InformationPanel::init()
508 const int spacing
= KDialog::spacingHint();
510 m_infoTimer
= new QTimer(this);
511 m_infoTimer
->setInterval(300);
512 m_infoTimer
->setSingleShot(true);
513 connect(m_infoTimer
, SIGNAL(timeout()),
514 this, SLOT(slotInfoTimeout()));
516 // Initialize timer for disabling an outdated preview with a small
517 // delay. This prevents flickering if the new preview can be generated
518 // within a very small timeframe.
519 m_outdatedPreviewTimer
= new QTimer(this);
520 m_outdatedPreviewTimer
->setInterval(300);
521 m_outdatedPreviewTimer
->setSingleShot(true);
522 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
523 this, SLOT(markOutdatedPreview()));
525 QVBoxLayout
* layout
= new QVBoxLayout
;
526 layout
->setSpacing(spacing
);
529 m_nameLabel
= new QLabel(this);
530 QFont font
= m_nameLabel
->font();
532 m_nameLabel
->setFont(font
);
533 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
534 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
537 m_preview
= new PixmapViewer(this);
538 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
539 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
541 if (MetaDataWidget::metaDataAvailable()) {
542 // rating, comment and tags
543 m_metaDataWidget
= new MetaDataWidget(this);
544 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
547 // general meta text information
548 m_metaTextLabel
= new MetaTextLabel(this);
549 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
551 m_metaTextArea
= new QScrollArea(this);
552 m_metaTextArea
->setWidget(m_metaTextLabel
);
553 m_metaTextArea
->setWidgetResizable(true);
554 m_metaTextArea
->setFrameShape(QFrame::NoFrame
);
556 QWidget
* viewport
= m_metaTextArea
->viewport();
557 viewport
->installEventFilter(this);
559 QPalette palette
= viewport
->palette();
560 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
561 viewport
->setPalette(palette
);
563 layout
->addWidget(m_nameLabel
);
564 layout
->addWidget(new KSeparator(this));
565 layout
->addWidget(m_preview
);
566 layout
->addWidget(new KSeparator(this));
567 if (m_metaDataWidget
!= 0) {
568 layout
->addWidget(m_metaDataWidget
);
569 layout
->addWidget(new KSeparator(this));
571 layout
->addWidget(m_metaTextArea
);
574 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
575 QDBusConnection::sessionBus(), this);
576 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
577 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
578 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
579 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
580 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
581 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
583 m_initialized
= true;
586 #include "informationpanel.moc"