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 if ( item
.nepomukUri().isValid() ) {
227 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
228 KConfigGroup settings
= config
.group("Show");
229 initMetaInfoSettings(settings
);
231 Nepomuk::Resource
res(item
.url());
233 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
234 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
235 while (it
!= properties
.constEnd()) {
236 Nepomuk::Types::Property
prop(it
.key());
237 if (settings
.readEntry(prop
.name(), true)) {
238 // TODO #1: use Nepomuk::formatValue(res, prop) if available
239 // instead of it.value().toString()
240 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 until
241 // we get translated labels
242 m_metaTextLabel
->add(tunedLabel(prop
.label()) + ':', it
.value().toString());
250 if (m_metaDataWidget
!= 0) {
251 if ( item
.nepomukUri().isValid() ) {
252 m_metaDataWidget
->setFile(item
.nepomukUri());
253 m_metaDataWidget
->show();
256 m_metaDataWidget
->hide();
260 if (InformationPanelSettings::showPreview()) {
261 const QString mimeType
= item
.mimetype();
262 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
263 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
264 // thinks it supports PNG images
266 m_phononWidget
->show();
267 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
268 ? PhononWidget::Video
269 : PhononWidget::Audio
;
270 m_phononWidget
->setMode(mode
);
271 m_phononWidget
->setUrl(item
.url());
272 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
273 m_phononWidget
->setVideoSize(m_preview
->size());
276 m_phononWidget
->hide();
277 m_preview
->setVisible(true);
280 m_phononWidget
->hide();
286 void InformationPanelContent::showItems(const KFileItemList
& items
)
288 m_pendingPreview
= false;
290 KIconLoader iconLoader
;
291 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
292 KIconLoader::NoGroup
,
293 KIconLoader::SizeEnormous
);
294 m_preview
->setPixmap(icon
);
295 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
297 if (m_metaDataWidget
!= 0) {
299 foreach (const KFileItem
& item
, items
) {
300 if ( item
.nepomukUri().isValid() )
301 urls
.append(item
.nepomukUri());
303 m_metaDataWidget
->setFiles(urls
);
304 m_metaDataWidget
->setVisible(!urls
.isEmpty());
307 quint64 totalSize
= 0;
308 foreach (const KFileItem
& item
, items
) {
309 // Only count the size of files, not dirs to match what
310 // DolphinViewContainer::selectionStatusBarText() does.
311 if (!item
.isDir() && !item
.isLink()) {
312 totalSize
+= item
.size();
315 m_metaTextLabel
->clear();
316 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
318 m_phononWidget
->hide();
320 m_item
= KFileItem();
323 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
325 if (event
->type() == QEvent::Resize
) {
326 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
327 if (obj
== m_metaTextArea
->viewport()) {
328 // The size of the meta text area has changed. Adjust the fixed
329 // width in a way that no horizontal scrollbar needs to be shown.
330 m_metaTextLabel
->setFixedWidth(resizeEvent
->size().width());
331 } else if (obj
== parent()) {
332 // If the text inside the name label or the info label cannot
333 // get wrapped, then the maximum width of the label is increased
334 // so that the width of the information panel gets increased.
335 // To prevent this, the maximum width is adjusted to
336 // the current width of the panel.
337 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
338 m_nameLabel
->setMaximumWidth(maxWidth
);
340 // The metadata widget also contains a text widget which may return
341 // a large preferred width.
342 if (m_metaDataWidget
!= 0) {
343 m_metaDataWidget
->setMaximumWidth(maxWidth
);
346 // try to increase the preview as large as possible
347 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
349 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
350 // assure that the size of the video player is the same as the preview size
351 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
355 return Panel::eventFilter(obj
, event
);
358 void InformationPanelContent::configureSettings()
361 if (m_item
.isNull() ||
362 !m_item
.nepomukUri().isValid()) {
368 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
369 previewAction
->setIcon(KIcon("view-preview"));
370 previewAction
->setCheckable(true);
371 previewAction
->setChecked(InformationPanelSettings::showPreview());
373 const bool metaDataAvailable
= MetaDataWidget::metaDataAvailable();
375 QAction
* ratingAction
= popup
.addAction(i18nc("@action:inmenu", "Rating"));
376 ratingAction
->setIcon(KIcon("rating"));
377 ratingAction
->setCheckable(true);
378 ratingAction
->setChecked(InformationPanelSettings::showRating());
379 ratingAction
->setEnabled(metaDataAvailable
);
381 QAction
* commentAction
= popup
.addAction(i18nc("@action:inmenu", "Comment"));
382 commentAction
->setIcon(KIcon("text-plain"));
383 commentAction
->setCheckable(true);
384 commentAction
->setChecked(InformationPanelSettings::showComment());
385 commentAction
->setEnabled(metaDataAvailable
);
387 QAction
* tagsAction
= popup
.addAction(i18nc("@action:inmenu", "Tags"));
388 tagsAction
->setCheckable(true);
389 tagsAction
->setChecked(InformationPanelSettings::showTags());
390 tagsAction
->setEnabled(metaDataAvailable
);
392 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
393 KConfigGroup settings
= config
.group("Show");
394 initMetaInfoSettings(settings
);
396 QList
<QAction
*> actions
;
398 // Get all meta information labels that are available for
399 // the currently shown file item and add them to the popup.
400 Nepomuk::Resource
res(m_item
.url());
401 QHash
<QUrl
, Nepomuk::Variant
> properties
= res
.properties();
402 QHash
<QUrl
, Nepomuk::Variant
>::const_iterator it
= properties
.constBegin();
403 while (it
!= properties
.constEnd()) {
404 Nepomuk::Types::Property
prop(it
.key());
405 const QString key
= prop
.name();
407 // Meta information provided by Nepomuk that is already
408 // available from KFileItem should not be configurable.
409 bool skip
= (key
== "fileExtension") ||
411 (key
== "sourceModified") ||
413 (key
== "mime type");
415 // Check whether there is already a meta information
416 // having the same label. In this case don't show it
417 // twice in the menu.
418 foreach (const QAction
* action
, actions
) {
419 if (action
->data().toString() == key
) {
427 const QString label
= tunedLabel(prop
.label());
428 QAction
* action
= new QAction(label
, &popup
);
429 action
->setCheckable(true);
430 action
->setChecked(settings
.readEntry(key
, true));
431 action
->setData(key
);
432 actions
.append(action
);
438 if (!actions
.isEmpty()) {
439 popup
.addSeparator();
441 // add all items alphabetically sorted to the popup
442 qSort(actions
.begin(), actions
.end(), lessThan
);
443 foreach (QAction
* action
, actions
) {
444 popup
.addAction(action
);
448 // Open the popup and adjust the settings for the
450 QAction
* action
= popup
.exec(QCursor::pos());
455 const bool isChecked
= action
->isChecked();
456 if (action
== previewAction
) {
457 m_preview
->setVisible(isChecked
);
458 m_previewSeparator
->setVisible(isChecked
);
459 InformationPanelSettings::setShowPreview(isChecked
);
460 } else if (action
== ratingAction
) {
461 m_metaDataWidget
->setRatingVisible(isChecked
);
462 InformationPanelSettings::setShowRating(isChecked
);
463 } else if (action
== commentAction
) {
464 m_metaDataWidget
->setCommentVisible(isChecked
);
465 InformationPanelSettings::setShowComment(isChecked
);
466 } else if (action
== tagsAction
) {
467 m_metaDataWidget
->setTagsVisible(isChecked
);
468 InformationPanelSettings::setShowTags(isChecked
);
470 settings
.writeEntry(action
->data().toString(), action
->isChecked());
474 if (m_metaDataWidget
!= 0) {
475 const bool visible
= m_metaDataWidget
->isRatingVisible() ||
476 m_metaDataWidget
->isCommentVisible() ||
477 m_metaDataWidget
->areTagsVisible();
478 m_metaDataSeparator
->setVisible(visible
);
485 void InformationPanelContent::showIcon(const KFileItem
& item
)
487 m_outdatedPreviewTimer
->stop();
488 m_pendingPreview
= false;
489 if (!applyPlace(item
.url())) {
490 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
494 void InformationPanelContent::showPreview(const KFileItem
& item
,
495 const QPixmap
& pixmap
)
497 m_outdatedPreviewTimer
->stop();
500 if (m_pendingPreview
) {
501 m_preview
->setPixmap(pixmap
);
502 m_pendingPreview
= false;
506 void InformationPanelContent::markOutdatedPreview()
508 KIconEffect iconEffect
;
509 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
510 KIconLoader::Desktop
,
511 KIconLoader::DisabledState
);
512 m_preview
->setPixmap(disabledPixmap
);
515 void InformationPanelContent::slotPlayingStarted()
517 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
520 void InformationPanelContent::slotPlayingStopped()
522 m_preview
->setVisible(true);
525 bool InformationPanelContent::applyPlace(const KUrl
& url
)
527 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
528 int count
= placesModel
->rowCount();
530 for (int i
= 0; i
< count
; ++i
) {
531 QModelIndex index
= placesModel
->index(i
, 0);
533 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
534 setNameLabelText(placesModel
->text(index
));
535 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
543 void InformationPanelContent::setNameLabelText(const QString
& text
)
545 QTextOption textOption
;
546 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
548 QTextLayout
textLayout(text
);
549 textLayout
.setFont(m_nameLabel
->font());
550 textLayout
.setTextOption(textOption
);
553 wrappedText
.reserve(text
.length());
555 // wrap the text to fit into the width of m_nameLabel
556 textLayout
.beginLayout();
557 QTextLine line
= textLayout
.createLine();
558 while (line
.isValid()) {
559 line
.setLineWidth(m_nameLabel
->width());
560 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
562 line
= textLayout
.createLine();
563 if (line
.isValid()) {
564 wrappedText
+= QChar::LineSeparator
;
567 textLayout
.endLayout();
569 m_nameLabel
->setText(wrappedText
);
572 void InformationPanelContent::initMetaInfoSettings(KConfigGroup
& group
)
574 if (!group
.readEntry("initialized", false)) {
575 // The resource file is read the first time. Assure
576 // that some meta information is disabled per default.
578 static const char* disabledProperties
[] = {
579 "asText", "contentSize", "depth", "fileExtension",
580 "fileName", "fileSize", "isPartOf", "mimetype", "name",
581 "parentUrl", "plainTextContent", "sourceModified",
583 0 // mandatory last entry
587 while (disabledProperties
[i
] != 0) {
588 group
.writeEntry(disabledProperties
[i
], false);
592 // mark the group as initialized
593 group
.writeEntry("initialized", true);
597 QString
InformationPanelContent::tunedLabel(const QString
& label
) const
600 const int labelLength
= label
.length();
601 if (labelLength
> 0) {
602 tunedLabel
.reserve(labelLength
);
603 tunedLabel
= label
[0].toUpper();
604 for (int i
= 1; i
< labelLength
; ++i
) {
605 if (label
[i
].isUpper() && !label
[i
- 1].isSpace() && !label
[i
- 1].isUpper()) {
607 tunedLabel
+= label
[i
].toLower();
609 tunedLabel
+= label
[i
];
616 void InformationPanelContent::init()
620 #include "informationpanelcontent.moc"