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
);
199 bool InformationPanel::eventFilter(QObject
* obj
, QEvent
* event
)
201 // Check whether the size of the meta text area has changed and adjust
202 // the fixed width in a way that no horizontal scrollbar needs to be shown.
203 if ((obj
== m_metaTextArea
->viewport()) && (event
->type() == QEvent::Resize
)) {
204 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
205 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
207 return Panel::eventFilter(obj
, event
);
210 void InformationPanel::showItemInfo()
218 if (showMultipleSelectionInfo()) {
219 KIconLoader iconLoader
;
220 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
221 KIconLoader::NoGroup
,
222 KIconLoader::SizeEnormous
);
223 m_preview
->setPixmap(icon
);
224 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
227 const KFileItem item
= fileItem();
228 const KUrl itemUrl
= item
.url();
229 if (!applyPlace(itemUrl
)) {
230 // try to get a preview pixmap from the item...
231 m_pendingPreview
= true;
233 // Mark the currently shown preview as outdated. This is done
234 // with a small delay to prevent a flickering when the next preview
235 // can be shown within a short timeframe.
236 m_outdatedPreviewTimer
->start();
238 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
246 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
247 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
248 connect(job
, SIGNAL(failed(const KFileItem
&)),
249 this, SLOT(showIcon(const KFileItem
&)));
251 setNameLabelText(itemUrl
.fileName());
258 void InformationPanel::slotInfoTimeout()
260 m_shownUrl
= m_urlCandidate
;
264 void InformationPanel::markOutdatedPreview()
266 KIconEffect iconEffect
;
267 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
268 KIconLoader::Desktop
,
269 KIconLoader::DisabledState
);
270 m_preview
->setPixmap(disabledPixmap
);
273 void InformationPanel::showIcon(const KFileItem
& item
)
275 m_outdatedPreviewTimer
->stop();
276 m_pendingPreview
= false;
277 if (!applyPlace(item
.url())) {
278 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
282 void InformationPanel::showPreview(const KFileItem
& item
,
283 const QPixmap
& pixmap
)
285 m_outdatedPreviewTimer
->stop();
288 if (m_pendingPreview
) {
289 m_preview
->setPixmap(pixmap
);
290 m_pendingPreview
= false;
294 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
296 if (m_shownUrl
== KUrl(source
)) {
297 // the currently shown file has been renamed, hence update the item information
298 // for the renamed file
299 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
300 requestDelayedItemInfo(item
);
304 void InformationPanel::slotFilesAdded(const QString
& directory
)
306 if (m_shownUrl
== KUrl(directory
)) {
307 // If the 'trash' icon changes because the trash has been emptied or got filled,
308 // the signal filesAdded("trash:/") will be emitted.
309 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
310 requestDelayedItemInfo(item
);
314 void InformationPanel::slotFilesChanged(const QStringList
& files
)
316 foreach (const QString
& fileName
, files
) {
317 if (m_shownUrl
== KUrl(fileName
)) {
324 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
326 foreach (const QString
& fileName
, files
) {
327 if (m_shownUrl
== KUrl(fileName
)) {
328 // the currently shown item has been removed, show
329 // the parent directory as fallback
337 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
339 if (m_shownUrl
== KUrl(directory
)) {
340 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
341 requestDelayedItemInfo(item
);
345 void InformationPanel::slotLeftDirectory(const QString
& directory
)
347 if (m_shownUrl
== KUrl(directory
)) {
348 // The signal 'leftDirectory' is also emitted when a media
349 // has been unmounted. In this case no directory change will be
350 // done in Dolphin, but the Information Panel must be updated to
351 // indicate an invalid directory.
357 bool InformationPanel::applyPlace(const KUrl
& url
)
359 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
360 int count
= placesModel
->rowCount();
362 for (int i
= 0; i
< count
; ++i
) {
363 QModelIndex index
= placesModel
->index(i
, 0);
365 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
366 setNameLabelText(placesModel
->text(index
));
367 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
375 void InformationPanel::cancelRequest()
380 void InformationPanel::showMetaInfo()
382 m_metaTextLabel
->clear();
384 if (showMultipleSelectionInfo()) {
385 if (m_metaDataWidget
!= 0) {
387 foreach (const KFileItem
& item
, m_selection
) {
388 urls
.append(item
.targetUrl());
390 m_metaDataWidget
->setFiles(urls
);
393 quint64 totalSize
= 0;
394 foreach (const KFileItem
& item
, m_selection
) {
395 // Only count the size of files, not dirs to match what
396 // DolphinViewContainer::selectionStatusBarText() does.
397 if (!item
.isDir() && !item
.isLink()) {
398 totalSize
+= item
.size();
401 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
403 const KFileItem item
= fileItem();
405 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
406 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
408 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
410 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
411 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
414 Nepomuk::Resource
res(item
.url());
416 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
417 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
418 while (it
!= properties
.constEnd()) {
419 Nepomuk::Types::Property
prop(it
.key());
420 // TODO: use Nepomuk::formatValue(res, prop) if available
421 // instead of it.value().toString()
422 m_metaTextLabel
->add(prop
.label(), it
.value().toString());
428 if (m_metaDataWidget
!= 0) {
429 m_metaDataWidget
->setFile(item
.targetUrl());
432 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item
.mimetype())) {
433 if (m_phononWidget
== 0) {
434 m_phononWidget
= new PhononWidget(this);
436 QVBoxLayout
* vBoxLayout
= qobject_cast
<QVBoxLayout
*>(layout());
437 Q_ASSERT(vBoxLayout
!= 0);
438 vBoxLayout
->insertWidget(3, m_phononWidget
);
440 m_phononWidget
->setUrl(item
.url());
442 delete m_phononWidget
;
448 KFileItem
InformationPanel::fileItem() const
450 if (!m_fileItem
.isNull()) {
454 if (!m_selection
.isEmpty()) {
455 Q_ASSERT(m_selection
.count() == 1);
456 return m_selection
.first();
459 // no item is hovered and no selection has been done: provide
460 // an item for the directory represented by m_shownUrl
461 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
466 bool InformationPanel::showMultipleSelectionInfo() const
468 return m_fileItem
.isNull() && (m_selection
.count() > 1);
471 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
473 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
476 void InformationPanel::setNameLabelText(const QString
& text
)
478 QTextOption textOption
;
479 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
481 QTextLayout
textLayout(text
);
482 textLayout
.setFont(m_nameLabel
->font());
483 textLayout
.setTextOption(textOption
);
486 wrappedText
.reserve(text
.length());
488 // wrap the text to fit into the width of m_nameLabel
489 textLayout
.beginLayout();
490 QTextLine line
= textLayout
.createLine();
491 while (line
.isValid()) {
492 line
.setLineWidth(m_nameLabel
->width());
493 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
495 line
= textLayout
.createLine();
496 if (line
.isValid()) {
497 wrappedText
+= QChar::LineSeparator
;
500 textLayout
.endLayout();
502 m_nameLabel
->setText(wrappedText
);
505 void InformationPanel::init()
507 const int spacing
= KDialog::spacingHint();
509 m_infoTimer
= new QTimer(this);
510 m_infoTimer
->setInterval(300);
511 m_infoTimer
->setSingleShot(true);
512 connect(m_infoTimer
, SIGNAL(timeout()),
513 this, SLOT(slotInfoTimeout()));
515 // Initialize timer for disabling an outdated preview with a small
516 // delay. This prevents flickering if the new preview can be generated
517 // within a very small timeframe.
518 m_outdatedPreviewTimer
= new QTimer(this);
519 m_outdatedPreviewTimer
->setInterval(300);
520 m_outdatedPreviewTimer
->setSingleShot(true);
521 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
522 this, SLOT(markOutdatedPreview()));
524 QVBoxLayout
* layout
= new QVBoxLayout
;
525 layout
->setSpacing(spacing
);
528 m_nameLabel
= new QLabel(this);
529 QFont font
= m_nameLabel
->font();
531 m_nameLabel
->setFont(font
);
532 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
533 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
536 m_preview
= new PixmapViewer(this);
537 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
538 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
540 if (MetaDataWidget::metaDataAvailable()) {
541 // rating, comment and tags
542 m_metaDataWidget
= new MetaDataWidget(this);
543 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
546 // general meta text information
547 m_metaTextLabel
= new MetaTextLabel(this);
548 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
550 m_metaTextArea
= new QScrollArea(this);
551 m_metaTextArea
->setWidget(m_metaTextLabel
);
552 m_metaTextArea
->setWidgetResizable(true);
553 m_metaTextArea
->setFrameShape(QFrame::NoFrame
);
555 QWidget
* viewport
= m_metaTextArea
->viewport();
556 viewport
->installEventFilter(this);
558 QPalette palette
= viewport
->palette();
559 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
560 viewport
->setPalette(palette
);
562 layout
->addWidget(m_nameLabel
);
563 layout
->addWidget(new KSeparator(this));
564 layout
->addWidget(m_preview
);
565 layout
->addWidget(new KSeparator(this));
566 if (m_metaDataWidget
!= 0) {
567 layout
->addWidget(m_metaDataWidget
);
568 layout
->addWidget(new KSeparator(this));
570 layout
->addWidget(m_metaTextArea
);
573 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
574 QDBusConnection::sessionBus(), this);
575 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
576 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
577 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
578 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
579 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
580 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
582 m_initialized
= true;
585 #include "informationpanel.moc"