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 "phononwidget.h"
52 #include "pixmapviewer.h"
55 * Helper function for sorting items with qSort() in
56 * InformationPanelContent::contextMenu().
58 bool lessThan(const QAction
* action1
, const QAction
* action2
)
60 return action1
->text() < action2
->text();
64 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(0),
71 m_previewSeparator(0),
76 parent
->installEventFilter(this);
78 // Initialize timer for disabling an outdated preview with a small
79 // delay. This prevents flickering if the new preview can be generated
80 // within a very small timeframe.
81 m_outdatedPreviewTimer
= new QTimer(this);
82 m_outdatedPreviewTimer
->setInterval(300);
83 m_outdatedPreviewTimer
->setSingleShot(true);
84 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
85 this, SLOT(markOutdatedPreview()));
87 QVBoxLayout
* layout
= new QVBoxLayout
;
88 layout
->setSpacing(KDialog::spacingHint());
91 m_nameLabel
= new QLabel(parent
);
92 QFont font
= m_nameLabel
->font();
94 m_nameLabel
->setFont(font
);
95 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
96 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
97 m_nameLabel
->setMaximumWidth(KIconLoader::SizeEnormous
);
100 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
102 m_preview
= new PixmapViewer(parent
);
103 m_preview
->setMinimumWidth(minPreviewWidth
);
104 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
106 m_phononWidget
= new PhononWidget(parent
);
107 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
108 connect(m_phononWidget
, SIGNAL(playingStarted()),
109 this, SLOT(slotPlayingStarted()));
110 connect(m_phononWidget
, SIGNAL(playingStopped()),
111 this, SLOT(slotPlayingStopped()));
113 m_previewSeparator
= new KSeparator(parent
);
115 const bool showPreview
= InformationPanelSettings::showPreview();
116 m_preview
->setVisible(showPreview
);
117 m_previewSeparator
->setVisible(showPreview
);
119 m_metaDataWidget
= new MetaDataWidget(parent
);
120 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
122 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
123 // at the bottom. This prevents that the meta data widget gets vertically stretched
124 // in the case where the height of m_metaDataArea > m_metaDataWidget.
125 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
126 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
127 containerLayout
->setContentsMargins(0, 0, 0, 0);
128 containerLayout
->setSpacing(0);
129 containerLayout
->addWidget(m_metaDataWidget
);
130 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
131 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
132 containerLayout
->addWidget(stretchWidget
);
134 m_metaDataArea
= new QScrollArea(parent
);
135 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
136 m_metaDataArea
->setWidgetResizable(true);
137 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
139 QWidget
* viewport
= m_metaDataArea
->viewport();
140 viewport
->installEventFilter(this);
142 QPalette palette
= viewport
->palette();
143 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
144 viewport
->setPalette(palette
);
146 layout
->addWidget(m_nameLabel
);
147 layout
->addWidget(new KSeparator(this));
148 layout
->addWidget(m_preview
);
149 layout
->addWidget(m_phononWidget
);
150 layout
->addWidget(m_previewSeparator
);
151 layout
->addWidget(m_metaDataArea
);
152 parent
->setLayout(layout
);
155 InformationPanelContent::~InformationPanelContent()
157 InformationPanelSettings::self()->writeConfig();
160 void InformationPanelContent::showItem(const KFileItem
& item
)
162 m_pendingPreview
= false;
164 const KUrl itemUrl
= item
.url();
165 if (!applyPlace(itemUrl
)) {
166 // try to get a preview pixmap from the item...
167 m_pendingPreview
= true;
169 // Mark the currently shown preview as outdated. This is done
170 // with a small delay to prevent a flickering when the next preview
171 // can be shown within a short timeframe. This timer is not started
172 // for directories, as directory previews might fail and return the
175 m_outdatedPreviewTimer
->start();
178 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
186 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
187 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
188 connect(job
, SIGNAL(failed(const KFileItem
&)),
189 this, SLOT(showIcon(const KFileItem
&)));
191 setNameLabelText(itemUrl
.fileName());
194 if (m_metaDataWidget
!= 0) {
195 m_metaDataWidget
->setItem(item
);
198 if (InformationPanelSettings::showPreview()) {
199 const QString mimeType
= item
.mimetype();
200 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
201 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
202 // thinks it supports PNG images
204 m_phononWidget
->show();
205 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
206 ? PhononWidget::Video
207 : PhononWidget::Audio
;
208 m_phononWidget
->setMode(mode
);
209 m_phononWidget
->setUrl(item
.url());
210 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
211 m_phononWidget
->setVideoSize(m_preview
->size());
214 m_phononWidget
->hide();
215 m_preview
->setVisible(true);
218 m_phononWidget
->hide();
224 void InformationPanelContent::showItems(const KFileItemList
& items
)
226 m_pendingPreview
= false;
228 KIconLoader iconLoader
;
229 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
230 KIconLoader::NoGroup
,
231 KIconLoader::SizeEnormous
);
232 m_preview
->setPixmap(icon
);
233 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
235 if (m_metaDataWidget
!= 0) {
236 m_metaDataWidget
->setItems(items
);
239 m_phononWidget
->hide();
241 m_item
= KFileItem();
244 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
246 if (event
->type() == QEvent::Resize
) {
247 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
248 if (obj
== m_metaDataArea
->viewport()) {
249 // The size of the meta text area has changed. Adjust the fixed
250 // width in a way that no horizontal scrollbar needs to be shown.
251 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
252 } else if (obj
== parent()) {
253 // If the text inside the name label or the info label cannot
254 // get wrapped, then the maximum width of the label is increased
255 // so that the width of the information panel gets increased.
256 // To prevent this, the maximum width is adjusted to
257 // the current width of the panel.
258 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
259 m_nameLabel
->setMaximumWidth(maxWidth
);
261 // The metadata widget also contains a text widget which may return
262 // a large preferred width.
263 if (m_metaDataWidget
!= 0) {
264 m_metaDataWidget
->setMaximumWidth(maxWidth
);
267 // try to increase the preview as large as possible
268 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
270 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
271 // assure that the size of the video player is the same as the preview size
272 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
276 return Panel::eventFilter(obj
, event
);
279 void InformationPanelContent::configureSettings()
281 if (m_item
.isNull() ||
282 !m_item
.nepomukUri().isValid()) {
288 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
289 previewAction
->setIcon(KIcon("view-preview"));
290 previewAction
->setCheckable(true);
291 previewAction
->setChecked(InformationPanelSettings::showPreview());
293 const bool metaDataAvailable
= true; // MetaDataWidget::metaDataAvailable(); TODO
295 QAction
* ratingAction
= popup
.addAction(i18nc("@action:inmenu", "Rating"));
296 ratingAction
->setIcon(KIcon("rating"));
297 ratingAction
->setCheckable(true);
298 ratingAction
->setChecked(InformationPanelSettings::showRating());
299 ratingAction
->setEnabled(metaDataAvailable
);
301 QAction
* commentAction
= popup
.addAction(i18nc("@action:inmenu", "Comment"));
302 commentAction
->setIcon(KIcon("text-plain"));
303 commentAction
->setCheckable(true);
304 commentAction
->setChecked(InformationPanelSettings::showComment());
305 commentAction
->setEnabled(metaDataAvailable
);
307 QAction
* tagsAction
= popup
.addAction(i18nc("@action:inmenu", "Tags"));
308 tagsAction
->setCheckable(true);
309 tagsAction
->setChecked(InformationPanelSettings::showTags());
310 tagsAction
->setEnabled(metaDataAvailable
);
312 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
313 KConfigGroup settings
= config
.group("Show");
315 QList
<QAction
*> actions
;
317 // Get all meta information labels that are available for
318 // the currently shown file item and add them to the popup.
319 /*Nepomuk::Resource res(m_item.url());
320 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
321 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
322 while (it != properties.constEnd()) {
323 Nepomuk::Types::Property prop(it.key());
324 const QString key = prop.name();
326 // Meta information provided by Nepomuk that is already
327 // available from KFileItem should not be configurable.
328 bool skip = (key == "fileExtension") ||
330 (key == "sourceModified") ||
332 (key == "mime type");
334 // Check whether there is already a meta information
335 // having the same label. In this case don't show it
336 // twice in the menu.
337 foreach (const QAction* action, actions) {
338 if (action->data().toString() == key) {
346 const QString label = tunedLabel(prop.label());
347 QAction* action = new QAction(label, &popup);
348 action->setCheckable(true);
349 action->setChecked(settings.readEntry(key, true));
350 action->setData(key);
351 actions.append(action);
357 if (!actions
.isEmpty()) {
358 popup
.addSeparator();
360 // add all items alphabetically sorted to the popup
361 qSort(actions
.begin(), actions
.end(), lessThan
);
362 foreach (QAction
* action
, actions
) {
363 popup
.addAction(action
);
367 // Open the popup and adjust the settings for the
369 QAction
* action
= popup
.exec(QCursor::pos());
374 const bool isChecked
= action
->isChecked();
375 if (action
== previewAction
) {
376 m_preview
->setVisible(isChecked
);
377 m_previewSeparator
->setVisible(isChecked
);
378 InformationPanelSettings::setShowPreview(isChecked
);
379 } else if (action
== ratingAction
) {
380 //m_metaDataWidget->setRatingVisible(isChecked);
381 InformationPanelSettings::setShowRating(isChecked
);
382 } else if (action
== commentAction
) {
383 //m_metaDataWidget->setCommentVisible(isChecked);
384 InformationPanelSettings::setShowComment(isChecked
);
385 } else if (action
== tagsAction
) {
386 //m_metaDataWidget->setTagsVisible(isChecked);
387 InformationPanelSettings::setShowTags(isChecked
);
389 settings
.writeEntry(action
->data().toString(), action
->isChecked());
393 /*if (m_metaDataWidget != 0) {
394 const bool visible = m_metaDataWidget->isRatingVisible() ||
395 m_metaDataWidget->isCommentVisible() ||
396 m_metaDataWidget->areTagsVisible();
397 m_metaDataSeparator->setVisible(visible);
403 void InformationPanelContent::showIcon(const KFileItem
& item
)
405 m_outdatedPreviewTimer
->stop();
406 m_pendingPreview
= false;
407 if (!applyPlace(item
.url())) {
408 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
412 void InformationPanelContent::showPreview(const KFileItem
& item
,
413 const QPixmap
& pixmap
)
415 m_outdatedPreviewTimer
->stop();
418 if (m_pendingPreview
) {
419 m_preview
->setPixmap(pixmap
);
420 m_pendingPreview
= false;
424 void InformationPanelContent::markOutdatedPreview()
426 KIconEffect iconEffect
;
427 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
428 KIconLoader::Desktop
,
429 KIconLoader::DisabledState
);
430 m_preview
->setPixmap(disabledPixmap
);
433 void InformationPanelContent::slotPlayingStarted()
435 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
438 void InformationPanelContent::slotPlayingStopped()
440 m_preview
->setVisible(true);
443 bool InformationPanelContent::applyPlace(const KUrl
& url
)
445 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
446 int count
= placesModel
->rowCount();
448 for (int i
= 0; i
< count
; ++i
) {
449 QModelIndex index
= placesModel
->index(i
, 0);
451 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
452 setNameLabelText(placesModel
->text(index
));
453 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
461 void InformationPanelContent::setNameLabelText(const QString
& text
)
463 QTextOption textOption
;
464 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
466 QTextLayout
textLayout(text
);
467 textLayout
.setFont(m_nameLabel
->font());
468 textLayout
.setTextOption(textOption
);
471 wrappedText
.reserve(text
.length());
473 // wrap the text to fit into the width of m_nameLabel
474 textLayout
.beginLayout();
475 QTextLine line
= textLayout
.createLine();
476 while (line
.isValid()) {
477 line
.setLineWidth(m_nameLabel
->width());
478 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
480 line
= textLayout
.createLine();
481 if (line
.isValid()) {
482 wrappedText
+= QChar::LineSeparator
;
485 textLayout
.endLayout();
487 m_nameLabel
->setText(wrappedText
);
490 #include "informationpanelcontent.moc"