1 /***************************************************************************
2 * Copyright (C) 2009 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 "informationpanelcontent.h"
22 #include <config-nepomuk.h>
25 #include <kfileitem.h>
26 #include <kfileplacesmodel.h>
27 #include <kio/previewjob.h>
28 #include <kiconeffect.h>
29 #include <kiconloader.h>
32 #include <kseparator.h>
34 #include <Phonon/BackendCapabilities>
35 #include <Phonon/MediaObject>
36 #include <Phonon/SeekSlider>
41 #include <QResizeEvent>
42 #include <QScrollArea>
43 #include <QTextLayout>
46 #include <QVBoxLayout>
48 #include "dolphin_informationpanelsettings.h"
49 #include "settings/dolphinsettings.h"
50 #include "metadatawidget.h"
51 #include "metatextlabel.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
56 #define DISABLE_NEPOMUK_LEGACY
57 #include <Nepomuk/Resource>
58 #include <Nepomuk/Types/Property>
59 #include <Nepomuk/Variant>
63 * Helper function for sorting items with qSort() in
64 * InformationPanelContent::contextMenu().
66 bool lessThan(const QAction
* action1
, const QAction
* action2
)
68 return action1
->text() < action2
->text();
72 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
75 m_pendingPreview(false),
76 m_outdatedPreviewTimer(0),
79 m_previewSeparator(0),
82 m_metaDataSeparator(0),
86 parent
->installEventFilter(this);
88 // Initialize timer for disabling an outdated preview with a small
89 // delay. This prevents flickering if the new preview can be generated
90 // within a very small timeframe.
91 m_outdatedPreviewTimer
= new QTimer(this);
92 m_outdatedPreviewTimer
->setInterval(300);
93 m_outdatedPreviewTimer
->setSingleShot(true);
94 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
95 this, SLOT(markOutdatedPreview()));
97 QVBoxLayout
* layout
= new QVBoxLayout
;
98 layout
->setSpacing(KDialog::spacingHint());
101 m_nameLabel
= new QLabel(parent
);
102 QFont font
= m_nameLabel
->font();
104 m_nameLabel
->setFont(font
);
105 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
106 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
107 m_nameLabel
->setMaximumWidth(KIconLoader::SizeEnormous
);
110 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
112 m_preview
= new PixmapViewer(parent
);
113 m_preview
->setMinimumWidth(minPreviewWidth
);
114 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
116 m_phononWidget
= new PhononWidget(parent
);
117 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
118 connect(m_phononWidget
, SIGNAL(playingStarted()),
119 this, SLOT(slotPlayingStarted()));
120 connect(m_phononWidget
, SIGNAL(playingStopped()),
121 this, SLOT(slotPlayingStopped()));
123 m_previewSeparator
= new KSeparator(parent
);
125 const bool showPreview
= InformationPanelSettings::showPreview();
126 m_preview
->setVisible(showPreview
);
127 m_previewSeparator
->setVisible(showPreview
);
129 if (MetaDataWidget::metaDataAvailable()) {
130 // rating, comment and tags
131 m_metaDataWidget
= new MetaDataWidget(parent
);
132 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
133 m_metaDataWidget
->setMaximumWidth(KIconLoader::SizeEnormous
);
135 const bool showRating
= InformationPanelSettings::showRating();
136 const bool showComment
= InformationPanelSettings::showComment();
137 const bool showTags
= InformationPanelSettings::showTags();
139 m_metaDataWidget
->setRatingVisible(showRating
);
140 m_metaDataWidget
->setCommentVisible(showComment
);
141 m_metaDataWidget
->setTagsVisible(showTags
);
143 m_metaDataSeparator
= new KSeparator(this);
144 m_metaDataSeparator
->setVisible(showRating
|| showComment
|| showTags
);
147 // general meta text information
148 m_metaTextLabel
= new MetaTextLabel(parent
);
149 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
151 m_metaTextArea
= new QScrollArea(parent
);
152 m_metaTextArea
->setWidget(m_metaTextLabel
);
153 m_metaTextArea
->setWidgetResizable(true);
154 m_metaTextArea
->setFrameShape(QFrame::NoFrame
);
156 QWidget
* viewport
= m_metaTextArea
->viewport();
157 viewport
->installEventFilter(this);
159 QPalette palette
= viewport
->palette();
160 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
161 viewport
->setPalette(palette
);
163 layout
->addWidget(m_nameLabel
);
164 layout
->addWidget(new KSeparator(this));
165 layout
->addWidget(m_preview
);
166 layout
->addWidget(m_phononWidget
);
167 layout
->addWidget(m_previewSeparator
);
168 if (m_metaDataWidget
!= 0) {
169 layout
->addWidget(m_metaDataWidget
);
170 layout
->addWidget(m_metaDataSeparator
);
172 layout
->addWidget(m_metaTextArea
);
173 parent
->setLayout(layout
);
176 InformationPanelContent::~InformationPanelContent()
178 InformationPanelSettings::self()->writeConfig();
181 void InformationPanelContent::showItem(const KFileItem
& item
)
183 m_pendingPreview
= false;
185 const KUrl itemUrl
= item
.url();
186 if (!applyPlace(itemUrl
)) {
187 // try to get a preview pixmap from the item...
188 m_pendingPreview
= true;
190 // Mark the currently shown preview as outdated. This is done
191 // with a small delay to prevent a flickering when the next preview
192 // can be shown within a short timeframe. This timer is not started
193 // for directories, as directory previews might fail and return the
196 m_outdatedPreviewTimer
->start();
199 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
207 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
208 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
209 connect(job
, SIGNAL(failed(const KFileItem
&)),
210 this, SLOT(showIcon(const KFileItem
&)));
212 setNameLabelText(itemUrl
.fileName());
215 m_metaTextLabel
->clear();
217 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
218 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
220 m_metaTextLabel
->add(i18nc("@label", "Type:"), item
.mimeComment());
222 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(item
.size()));
223 m_metaTextLabel
->add(i18nc("@label", "Modified:"), item
.timeString());
226 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
227 KConfigGroup settings
= config
.group("Show");
228 initMetaInfoSettings(settings
);
230 Nepomuk::Resource
res(item
.url());
232 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
233 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
234 while (it
!= properties
.constEnd()) {
235 Nepomuk::Types::Property
prop(it
.key());
236 if (settings
.readEntry(prop
.name(), true)) {
237 // TODO #1: use Nepomuk::formatValue(res, prop) if available
238 // instead of it.value().toString()
239 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 until
240 // we get translated labels
241 m_metaTextLabel
->add(tunedLabel(prop
.label()) + ':', it
.value().toString());
248 if (m_metaDataWidget
!= 0) {
249 m_metaDataWidget
->setFile(item
.targetUrl());
252 if (InformationPanelSettings::showPreview()) {
253 const QString mimeType
= item
.mimetype();
254 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
255 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
256 // thinks it supports PNG images
258 m_phononWidget
->show();
259 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
260 ? PhononWidget::Video
261 : PhononWidget::Audio
;
262 m_phononWidget
->setMode(mode
);
263 m_phononWidget
->setUrl(item
.url());
264 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
265 m_phononWidget
->setVideoSize(m_preview
->size());
268 m_phononWidget
->hide();
269 m_preview
->setVisible(true);
272 m_phononWidget
->hide();
278 void InformationPanelContent::showItems(const KFileItemList
& items
)
280 m_pendingPreview
= false;
282 KIconLoader iconLoader
;
283 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
284 KIconLoader::NoGroup
,
285 KIconLoader::SizeEnormous
);
286 m_preview
->setPixmap(icon
);
287 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
289 if (m_metaDataWidget
!= 0) {
291 foreach (const KFileItem
& item
, items
) {
292 urls
.append(item
.targetUrl());
294 m_metaDataWidget
->setFiles(urls
);
297 quint64 totalSize
= 0;
298 foreach (const KFileItem
& item
, items
) {
299 // Only count the size of files, not dirs to match what
300 // DolphinViewContainer::selectionStatusBarText() does.
301 if (!item
.isDir() && !item
.isLink()) {
302 totalSize
+= item
.size();
305 m_metaTextLabel
->clear();
306 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
308 m_phononWidget
->hide();
310 m_item
= KFileItem();
313 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
315 if (event
->type() == QEvent::Resize
) {
316 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
317 if (obj
== m_metaTextArea
->viewport()) {
318 // The size of the meta text area has changed. Adjust the fixed
319 // width in a way that no horizontal scrollbar needs to be shown.
320 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
321 } else if (obj
== parent()) {
322 // If the text inside the name label or the info label cannot
323 // get wrapped, then the maximum width of the label is increased
324 // so that the width of the information panel gets increased.
325 // To prevent this, the maximum width is adjusted to
326 // the current width of the panel.
327 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
328 m_nameLabel
->setMaximumWidth(maxWidth
);
330 // The metadata widget also contains a text widget which may return
331 // a large preferred width.
332 if (m_metaDataWidget
!= 0) {
333 m_metaDataWidget
->setMaximumWidth(maxWidth
);
336 // try to increase the preview as large as possible
337 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
339 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
340 // assure that the size of the video player is the same as the preview size
341 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
345 return Panel::eventFilter(obj
, event
);
348 void InformationPanelContent::configureSettings()
351 if (m_item
.isNull()) {
357 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
358 previewAction
->setIcon(KIcon("view-preview"));
359 previewAction
->setCheckable(true);
360 previewAction
->setChecked(InformationPanelSettings::showPreview());
362 const bool metaDataAvailable
= MetaDataWidget::metaDataAvailable();
364 QAction
* ratingAction
= popup
.addAction(i18nc("@action:inmenu", "Rating"));
365 ratingAction
->setIcon(KIcon("rating"));
366 ratingAction
->setCheckable(true);
367 ratingAction
->setChecked(InformationPanelSettings::showRating());
368 ratingAction
->setEnabled(metaDataAvailable
);
370 QAction
* commentAction
= popup
.addAction(i18nc("@action:inmenu", "Comment"));
371 commentAction
->setIcon(KIcon("text-plain"));
372 commentAction
->setCheckable(true);
373 commentAction
->setChecked(InformationPanelSettings::showComment());
374 commentAction
->setEnabled(metaDataAvailable
);
376 QAction
* tagsAction
= popup
.addAction(i18nc("@action:inmenu", "Tags"));
377 tagsAction
->setCheckable(true);
378 tagsAction
->setChecked(InformationPanelSettings::showTags());
379 tagsAction
->setEnabled(metaDataAvailable
);
381 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
382 KConfigGroup settings
= config
.group("Show");
383 initMetaInfoSettings(settings
);
385 QList
<QAction
*> actions
;
387 // Get all meta information labels that are available for
388 // the currently shown file item and add them to the popup.
389 Nepomuk::Resource
res(m_item
.url());
390 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
391 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
392 while (it
!= properties
.constEnd()) {
393 Nepomuk::Types::Property
prop(it
.key());
394 const QString key
= prop
.name();
396 // Meta information provided by Nepomuk that is already
397 // available from KFileItem should not be configurable.
398 bool skip
= (key
== "fileExtension") ||
400 (key
== "sourceModified") ||
402 (key
== "mime type");
404 // Check whether there is already a meta information
405 // having the same label. In this case don't show it
406 // twice in the menu.
407 foreach (const QAction
* action
, actions
) {
408 if (action
->data().toString() == key
) {
416 const QString label
= tunedLabel(prop
.label());
417 QAction
* action
= new QAction(label
, &popup
);
418 action
->setCheckable(true);
419 action
->setChecked(settings
.readEntry(key
, true));
420 action
->setData(key
);
421 actions
.append(action
);
427 if (!actions
.isEmpty()) {
428 popup
.addSeparator();
430 // add all items alphabetically sorted to the popup
431 qSort(actions
.begin(), actions
.end(), lessThan
);
432 foreach (QAction
* action
, actions
) {
433 popup
.addAction(action
);
437 // Open the popup and adjust the settings for the
439 QAction
* action
= popup
.exec(QCursor::pos());
444 const bool isChecked
= action
->isChecked();
445 if (action
== previewAction
) {
446 m_preview
->setVisible(isChecked
);
447 m_previewSeparator
->setVisible(isChecked
);
448 InformationPanelSettings::setShowPreview(isChecked
);
449 } else if (action
== ratingAction
) {
450 m_metaDataWidget
->setRatingVisible(isChecked
);
451 InformationPanelSettings::setShowRating(isChecked
);
452 } else if (action
== commentAction
) {
453 m_metaDataWidget
->setCommentVisible(isChecked
);
454 InformationPanelSettings::setShowComment(isChecked
);
455 } else if (action
== tagsAction
) {
456 m_metaDataWidget
->setTagsVisible(isChecked
);
457 InformationPanelSettings::setShowTags(isChecked
);
459 settings
.writeEntry(action
->data().toString(), action
->isChecked());
463 if (m_metaDataWidget
!= 0) {
464 const bool visible
= m_metaDataWidget
->isRatingVisible() ||
465 m_metaDataWidget
->isCommentVisible() ||
466 m_metaDataWidget
->areTagsVisible();
467 m_metaDataSeparator
->setVisible(visible
);
474 void InformationPanelContent::showIcon(const KFileItem
& item
)
476 m_outdatedPreviewTimer
->stop();
477 m_pendingPreview
= false;
478 if (!applyPlace(item
.url())) {
479 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
483 void InformationPanelContent::showPreview(const KFileItem
& item
,
484 const QPixmap
& pixmap
)
486 m_outdatedPreviewTimer
->stop();
489 if (m_pendingPreview
) {
490 m_preview
->setPixmap(pixmap
);
491 m_pendingPreview
= false;
495 void InformationPanelContent::markOutdatedPreview()
497 KIconEffect iconEffect
;
498 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
499 KIconLoader::Desktop
,
500 KIconLoader::DisabledState
);
501 m_preview
->setPixmap(disabledPixmap
);
504 void InformationPanelContent::slotPlayingStarted()
506 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
509 void InformationPanelContent::slotPlayingStopped()
511 m_preview
->setVisible(true);
514 bool InformationPanelContent::applyPlace(const KUrl
& url
)
516 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
517 int count
= placesModel
->rowCount();
519 for (int i
= 0; i
< count
; ++i
) {
520 QModelIndex index
= placesModel
->index(i
, 0);
522 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
523 setNameLabelText(placesModel
->text(index
));
524 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
532 void InformationPanelContent::setNameLabelText(const QString
& text
)
534 QTextOption textOption
;
535 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
537 QTextLayout
textLayout(text
);
538 textLayout
.setFont(m_nameLabel
->font());
539 textLayout
.setTextOption(textOption
);
542 wrappedText
.reserve(text
.length());
544 // wrap the text to fit into the width of m_nameLabel
545 textLayout
.beginLayout();
546 QTextLine line
= textLayout
.createLine();
547 while (line
.isValid()) {
548 line
.setLineWidth(m_nameLabel
->width());
549 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
551 line
= textLayout
.createLine();
552 if (line
.isValid()) {
553 wrappedText
+= QChar::LineSeparator
;
556 textLayout
.endLayout();
558 m_nameLabel
->setText(wrappedText
);
561 void InformationPanelContent::initMetaInfoSettings(KConfigGroup
& group
)
563 if (!group
.readEntry("initialized", false)) {
564 // The resource file is read the first time. Assure
565 // that some meta information is disabled per default.
567 static const char* disabledProperties
[] = {
568 "asText", "contentSize", "depth", "fileExtension",
569 "fileName", "fileSize", "isPartOf", "mimetype", "name",
570 "parentUrl", "plainTextContent", "sourceModified",
572 0 // mandatory last entry
576 while (disabledProperties
[i
] != 0) {
577 group
.writeEntry(disabledProperties
[i
], false);
581 // mark the group as initialized
582 group
.writeEntry("initialized", true);
586 QString
InformationPanelContent::tunedLabel(const QString
& label
) const
589 const int labelLength
= label
.length();
590 if (labelLength
> 0) {
591 tunedLabel
.reserve(labelLength
);
592 tunedLabel
= label
[0].toUpper();
593 for (int i
= 1; i
< labelLength
; ++i
) {
594 if (label
[i
].isUpper() && !label
[i
- 1].isSpace() && !label
[i
- 1].isUpper()) {
596 tunedLabel
+= label
[i
].toLower();
598 tunedLabel
+= label
[i
];
605 void InformationPanelContent::init()
609 #include "informationpanelcontent.moc"