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 containerLayout
->addWidget(new QWidget(metaDataWidgetContainer
));
132 m_metaDataArea
= new QScrollArea(parent
);
133 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
134 m_metaDataArea
->setWidgetResizable(true);
135 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
137 QWidget
* viewport
= m_metaDataArea
->viewport();
138 viewport
->installEventFilter(this);
140 QPalette palette
= viewport
->palette();
141 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
142 viewport
->setPalette(palette
);
144 layout
->addWidget(m_nameLabel
);
145 layout
->addWidget(new KSeparator(this));
146 layout
->addWidget(m_preview
);
147 layout
->addWidget(m_phononWidget
);
148 layout
->addWidget(m_previewSeparator
);
149 layout
->addWidget(m_metaDataArea
);
150 parent
->setLayout(layout
);
153 InformationPanelContent::~InformationPanelContent()
155 InformationPanelSettings::self()->writeConfig();
158 void InformationPanelContent::showItem(const KFileItem
& item
)
160 m_pendingPreview
= false;
162 const KUrl itemUrl
= item
.url();
163 if (!applyPlace(itemUrl
)) {
164 // try to get a preview pixmap from the item...
165 m_pendingPreview
= true;
167 // Mark the currently shown preview as outdated. This is done
168 // with a small delay to prevent a flickering when the next preview
169 // can be shown within a short timeframe. This timer is not started
170 // for directories, as directory previews might fail and return the
173 m_outdatedPreviewTimer
->start();
176 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
184 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
185 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
186 connect(job
, SIGNAL(failed(const KFileItem
&)),
187 this, SLOT(showIcon(const KFileItem
&)));
189 setNameLabelText(itemUrl
.fileName());
192 if (m_metaDataWidget
!= 0) {
193 m_metaDataWidget
->setItem(item
);
196 if (InformationPanelSettings::showPreview()) {
197 const QString mimeType
= item
.mimetype();
198 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
199 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
200 // thinks it supports PNG images
202 m_phononWidget
->show();
203 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
204 ? PhononWidget::Video
205 : PhononWidget::Audio
;
206 m_phononWidget
->setMode(mode
);
207 m_phononWidget
->setUrl(item
.url());
208 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
209 m_phononWidget
->setVideoSize(m_preview
->size());
212 m_phononWidget
->hide();
213 m_preview
->setVisible(true);
216 m_phononWidget
->hide();
222 void InformationPanelContent::showItems(const KFileItemList
& items
)
224 m_pendingPreview
= false;
226 KIconLoader iconLoader
;
227 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
228 KIconLoader::NoGroup
,
229 KIconLoader::SizeEnormous
);
230 m_preview
->setPixmap(icon
);
231 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
233 if (m_metaDataWidget
!= 0) {
234 m_metaDataWidget
->setItems(items
);
237 m_phononWidget
->hide();
239 m_item
= KFileItem();
242 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
244 if (event
->type() == QEvent::Resize
) {
245 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
246 if (obj
== m_metaDataArea
->viewport()) {
247 // The size of the meta text area has changed. Adjust the fixed
248 // width in a way that no horizontal scrollbar needs to be shown.
249 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
250 } else if (obj
== parent()) {
251 // If the text inside the name label or the info label cannot
252 // get wrapped, then the maximum width of the label is increased
253 // so that the width of the information panel gets increased.
254 // To prevent this, the maximum width is adjusted to
255 // the current width of the panel.
256 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
257 m_nameLabel
->setMaximumWidth(maxWidth
);
259 // The metadata widget also contains a text widget which may return
260 // a large preferred width.
261 if (m_metaDataWidget
!= 0) {
262 m_metaDataWidget
->setMaximumWidth(maxWidth
);
265 // try to increase the preview as large as possible
266 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
268 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
269 // assure that the size of the video player is the same as the preview size
270 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
274 return Panel::eventFilter(obj
, event
);
277 void InformationPanelContent::configureSettings()
279 if (m_item
.isNull() ||
280 !m_item
.nepomukUri().isValid()) {
286 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
287 previewAction
->setIcon(KIcon("view-preview"));
288 previewAction
->setCheckable(true);
289 previewAction
->setChecked(InformationPanelSettings::showPreview());
291 const bool metaDataAvailable
= true; // MetaDataWidget::metaDataAvailable(); TODO
293 QAction
* ratingAction
= popup
.addAction(i18nc("@action:inmenu", "Rating"));
294 ratingAction
->setIcon(KIcon("rating"));
295 ratingAction
->setCheckable(true);
296 ratingAction
->setChecked(InformationPanelSettings::showRating());
297 ratingAction
->setEnabled(metaDataAvailable
);
299 QAction
* commentAction
= popup
.addAction(i18nc("@action:inmenu", "Comment"));
300 commentAction
->setIcon(KIcon("text-plain"));
301 commentAction
->setCheckable(true);
302 commentAction
->setChecked(InformationPanelSettings::showComment());
303 commentAction
->setEnabled(metaDataAvailable
);
305 QAction
* tagsAction
= popup
.addAction(i18nc("@action:inmenu", "Tags"));
306 tagsAction
->setCheckable(true);
307 tagsAction
->setChecked(InformationPanelSettings::showTags());
308 tagsAction
->setEnabled(metaDataAvailable
);
310 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
311 KConfigGroup settings
= config
.group("Show");
313 QList
<QAction
*> actions
;
315 // Get all meta information labels that are available for
316 // the currently shown file item and add them to the popup.
317 /*Nepomuk::Resource res(m_item.url());
318 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
319 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
320 while (it != properties.constEnd()) {
321 Nepomuk::Types::Property prop(it.key());
322 const QString key = prop.name();
324 // Meta information provided by Nepomuk that is already
325 // available from KFileItem should not be configurable.
326 bool skip = (key == "fileExtension") ||
328 (key == "sourceModified") ||
330 (key == "mime type");
332 // Check whether there is already a meta information
333 // having the same label. In this case don't show it
334 // twice in the menu.
335 foreach (const QAction* action, actions) {
336 if (action->data().toString() == key) {
344 const QString label = tunedLabel(prop.label());
345 QAction* action = new QAction(label, &popup);
346 action->setCheckable(true);
347 action->setChecked(settings.readEntry(key, true));
348 action->setData(key);
349 actions.append(action);
355 if (!actions
.isEmpty()) {
356 popup
.addSeparator();
358 // add all items alphabetically sorted to the popup
359 qSort(actions
.begin(), actions
.end(), lessThan
);
360 foreach (QAction
* action
, actions
) {
361 popup
.addAction(action
);
365 // Open the popup and adjust the settings for the
367 QAction
* action
= popup
.exec(QCursor::pos());
372 const bool isChecked
= action
->isChecked();
373 if (action
== previewAction
) {
374 m_preview
->setVisible(isChecked
);
375 m_previewSeparator
->setVisible(isChecked
);
376 InformationPanelSettings::setShowPreview(isChecked
);
377 } else if (action
== ratingAction
) {
378 //m_metaDataWidget->setRatingVisible(isChecked);
379 InformationPanelSettings::setShowRating(isChecked
);
380 } else if (action
== commentAction
) {
381 //m_metaDataWidget->setCommentVisible(isChecked);
382 InformationPanelSettings::setShowComment(isChecked
);
383 } else if (action
== tagsAction
) {
384 //m_metaDataWidget->setTagsVisible(isChecked);
385 InformationPanelSettings::setShowTags(isChecked
);
387 settings
.writeEntry(action
->data().toString(), action
->isChecked());
391 /*if (m_metaDataWidget != 0) {
392 const bool visible = m_metaDataWidget->isRatingVisible() ||
393 m_metaDataWidget->isCommentVisible() ||
394 m_metaDataWidget->areTagsVisible();
395 m_metaDataSeparator->setVisible(visible);
401 void InformationPanelContent::showIcon(const KFileItem
& item
)
403 m_outdatedPreviewTimer
->stop();
404 m_pendingPreview
= false;
405 if (!applyPlace(item
.url())) {
406 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
410 void InformationPanelContent::showPreview(const KFileItem
& item
,
411 const QPixmap
& pixmap
)
413 m_outdatedPreviewTimer
->stop();
416 if (m_pendingPreview
) {
417 m_preview
->setPixmap(pixmap
);
418 m_pendingPreview
= false;
422 void InformationPanelContent::markOutdatedPreview()
424 KIconEffect iconEffect
;
425 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
426 KIconLoader::Desktop
,
427 KIconLoader::DisabledState
);
428 m_preview
->setPixmap(disabledPixmap
);
431 void InformationPanelContent::slotPlayingStarted()
433 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
436 void InformationPanelContent::slotPlayingStopped()
438 m_preview
->setVisible(true);
441 bool InformationPanelContent::applyPlace(const KUrl
& url
)
443 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
444 int count
= placesModel
->rowCount();
446 for (int i
= 0; i
< count
; ++i
) {
447 QModelIndex index
= placesModel
->index(i
, 0);
449 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
450 setNameLabelText(placesModel
->text(index
));
451 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
459 void InformationPanelContent::setNameLabelText(const QString
& text
)
461 QTextOption textOption
;
462 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
464 QTextLayout
textLayout(text
);
465 textLayout
.setFont(m_nameLabel
->font());
466 textLayout
.setTextOption(textOption
);
469 wrappedText
.reserve(text
.length());
471 // wrap the text to fit into the width of m_nameLabel
472 textLayout
.beginLayout();
473 QTextLine line
= textLayout
.createLine();
474 while (line
.isValid()) {
475 line
.setLineWidth(m_nameLabel
->width());
476 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
478 line
= textLayout
.createLine();
479 if (line
.isValid()) {
480 wrappedText
+= QChar::LineSeparator
;
483 textLayout
.endLayout();
485 m_nameLabel
->setText(wrappedText
);
488 #include "informationpanelcontent.moc"