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
);
121 m_metaDataArea
= new QScrollArea(parent
);
122 //m_metaDataArea->setWidget(m_metaDataArea);
123 m_metaDataArea
->setWidgetResizable(true);
124 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
126 QWidget
* viewport
= m_metaDataArea
->viewport();
127 viewport
->installEventFilter(this);
129 QPalette palette
= viewport
->palette();
130 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
131 viewport
->setPalette(palette
);
133 layout
->addWidget(m_nameLabel
);
134 layout
->addWidget(new KSeparator(this));
135 layout
->addWidget(m_preview
);
136 layout
->addWidget(m_phononWidget
);
137 layout
->addWidget(m_previewSeparator
);
138 if (m_metaDataWidget
!= 0) {
139 layout
->addWidget(m_metaDataWidget
);
141 layout
->addWidget(m_metaDataArea
);
142 parent
->setLayout(layout
);
145 InformationPanelContent::~InformationPanelContent()
147 InformationPanelSettings::self()->writeConfig();
150 void InformationPanelContent::showItem(const KFileItem
& item
)
152 m_pendingPreview
= false;
154 const KUrl itemUrl
= item
.url();
155 if (!applyPlace(itemUrl
)) {
156 // try to get a preview pixmap from the item...
157 m_pendingPreview
= true;
159 // Mark the currently shown preview as outdated. This is done
160 // with a small delay to prevent a flickering when the next preview
161 // can be shown within a short timeframe. This timer is not started
162 // for directories, as directory previews might fail and return the
165 m_outdatedPreviewTimer
->start();
168 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
176 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
177 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
178 connect(job
, SIGNAL(failed(const KFileItem
&)),
179 this, SLOT(showIcon(const KFileItem
&)));
181 setNameLabelText(itemUrl
.fileName());
184 if (m_metaDataWidget
!= 0) {
185 m_metaDataWidget
->setItem(item
);
188 if (InformationPanelSettings::showPreview()) {
189 const QString mimeType
= item
.mimetype();
190 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
191 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
192 // thinks it supports PNG images
194 m_phononWidget
->show();
195 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
196 ? PhononWidget::Video
197 : PhononWidget::Audio
;
198 m_phononWidget
->setMode(mode
);
199 m_phononWidget
->setUrl(item
.url());
200 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
201 m_phononWidget
->setVideoSize(m_preview
->size());
204 m_phononWidget
->hide();
205 m_preview
->setVisible(true);
208 m_phononWidget
->hide();
214 void InformationPanelContent::showItems(const KFileItemList
& items
)
216 m_pendingPreview
= false;
218 KIconLoader iconLoader
;
219 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
220 KIconLoader::NoGroup
,
221 KIconLoader::SizeEnormous
);
222 m_preview
->setPixmap(icon
);
223 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
225 if (m_metaDataWidget
!= 0) {
226 m_metaDataWidget
->setItems(items
);
229 m_phononWidget
->hide();
231 m_item
= KFileItem();
234 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
236 if (event
->type() == QEvent::Resize
) {
237 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
238 if (obj
== m_metaDataArea
->viewport()) {
239 // The size of the meta text area has changed. Adjust the fixed
240 // width in a way that no horizontal scrollbar needs to be shown.
241 //m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
242 } else if (obj
== parent()) {
243 // If the text inside the name label or the info label cannot
244 // get wrapped, then the maximum width of the label is increased
245 // so that the width of the information panel gets increased.
246 // To prevent this, the maximum width is adjusted to
247 // the current width of the panel.
248 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
249 m_nameLabel
->setMaximumWidth(maxWidth
);
251 // The metadata widget also contains a text widget which may return
252 // a large preferred width.
253 if (m_metaDataWidget
!= 0) {
254 //m_metaDataWidget->setMaximumWidth(maxWidth);
257 // try to increase the preview as large as possible
258 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
260 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
261 // assure that the size of the video player is the same as the preview size
262 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
266 return Panel::eventFilter(obj
, event
);
269 void InformationPanelContent::configureSettings()
271 if (m_item
.isNull() ||
272 !m_item
.nepomukUri().isValid()) {
278 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
279 previewAction
->setIcon(KIcon("view-preview"));
280 previewAction
->setCheckable(true);
281 previewAction
->setChecked(InformationPanelSettings::showPreview());
283 const bool metaDataAvailable
= true; // MetaDataWidget::metaDataAvailable(); TODO
285 QAction
* ratingAction
= popup
.addAction(i18nc("@action:inmenu", "Rating"));
286 ratingAction
->setIcon(KIcon("rating"));
287 ratingAction
->setCheckable(true);
288 ratingAction
->setChecked(InformationPanelSettings::showRating());
289 ratingAction
->setEnabled(metaDataAvailable
);
291 QAction
* commentAction
= popup
.addAction(i18nc("@action:inmenu", "Comment"));
292 commentAction
->setIcon(KIcon("text-plain"));
293 commentAction
->setCheckable(true);
294 commentAction
->setChecked(InformationPanelSettings::showComment());
295 commentAction
->setEnabled(metaDataAvailable
);
297 QAction
* tagsAction
= popup
.addAction(i18nc("@action:inmenu", "Tags"));
298 tagsAction
->setCheckable(true);
299 tagsAction
->setChecked(InformationPanelSettings::showTags());
300 tagsAction
->setEnabled(metaDataAvailable
);
302 KConfig
config("kmetainformationrc", KConfig::NoGlobals
);
303 KConfigGroup settings
= config
.group("Show");
305 QList
<QAction
*> actions
;
307 // Get all meta information labels that are available for
308 // the currently shown file item and add them to the popup.
309 /*Nepomuk::Resource res(m_item.url());
310 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
311 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
312 while (it != properties.constEnd()) {
313 Nepomuk::Types::Property prop(it.key());
314 const QString key = prop.name();
316 // Meta information provided by Nepomuk that is already
317 // available from KFileItem should not be configurable.
318 bool skip = (key == "fileExtension") ||
320 (key == "sourceModified") ||
322 (key == "mime type");
324 // Check whether there is already a meta information
325 // having the same label. In this case don't show it
326 // twice in the menu.
327 foreach (const QAction* action, actions) {
328 if (action->data().toString() == key) {
336 const QString label = tunedLabel(prop.label());
337 QAction* action = new QAction(label, &popup);
338 action->setCheckable(true);
339 action->setChecked(settings.readEntry(key, true));
340 action->setData(key);
341 actions.append(action);
347 if (!actions
.isEmpty()) {
348 popup
.addSeparator();
350 // add all items alphabetically sorted to the popup
351 qSort(actions
.begin(), actions
.end(), lessThan
);
352 foreach (QAction
* action
, actions
) {
353 popup
.addAction(action
);
357 // Open the popup and adjust the settings for the
359 QAction
* action
= popup
.exec(QCursor::pos());
364 const bool isChecked
= action
->isChecked();
365 if (action
== previewAction
) {
366 m_preview
->setVisible(isChecked
);
367 m_previewSeparator
->setVisible(isChecked
);
368 InformationPanelSettings::setShowPreview(isChecked
);
369 } else if (action
== ratingAction
) {
370 //m_metaDataWidget->setRatingVisible(isChecked);
371 InformationPanelSettings::setShowRating(isChecked
);
372 } else if (action
== commentAction
) {
373 //m_metaDataWidget->setCommentVisible(isChecked);
374 InformationPanelSettings::setShowComment(isChecked
);
375 } else if (action
== tagsAction
) {
376 //m_metaDataWidget->setTagsVisible(isChecked);
377 InformationPanelSettings::setShowTags(isChecked
);
379 settings
.writeEntry(action
->data().toString(), action
->isChecked());
383 /*if (m_metaDataWidget != 0) {
384 const bool visible = m_metaDataWidget->isRatingVisible() ||
385 m_metaDataWidget->isCommentVisible() ||
386 m_metaDataWidget->areTagsVisible();
387 m_metaDataSeparator->setVisible(visible);
393 void InformationPanelContent::showIcon(const KFileItem
& item
)
395 m_outdatedPreviewTimer
->stop();
396 m_pendingPreview
= false;
397 if (!applyPlace(item
.url())) {
398 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
402 void InformationPanelContent::showPreview(const KFileItem
& item
,
403 const QPixmap
& pixmap
)
405 m_outdatedPreviewTimer
->stop();
408 if (m_pendingPreview
) {
409 m_preview
->setPixmap(pixmap
);
410 m_pendingPreview
= false;
414 void InformationPanelContent::markOutdatedPreview()
416 KIconEffect iconEffect
;
417 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
418 KIconLoader::Desktop
,
419 KIconLoader::DisabledState
);
420 m_preview
->setPixmap(disabledPixmap
);
423 void InformationPanelContent::slotPlayingStarted()
425 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
428 void InformationPanelContent::slotPlayingStopped()
430 m_preview
->setVisible(true);
433 bool InformationPanelContent::applyPlace(const KUrl
& url
)
435 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
436 int count
= placesModel
->rowCount();
438 for (int i
= 0; i
< count
; ++i
) {
439 QModelIndex index
= placesModel
->index(i
, 0);
441 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
442 setNameLabelText(placesModel
->text(index
));
443 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
451 void InformationPanelContent::setNameLabelText(const QString
& text
)
453 QTextOption textOption
;
454 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
456 QTextLayout
textLayout(text
);
457 textLayout
.setFont(m_nameLabel
->font());
458 textLayout
.setTextOption(textOption
);
461 wrappedText
.reserve(text
.length());
463 // wrap the text to fit into the width of m_nameLabel
464 textLayout
.beginLayout();
465 QTextLine line
= textLayout
.createLine();
466 while (line
.isValid()) {
467 line
.setLineWidth(m_nameLabel
->width());
468 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
470 line
= textLayout
.createLine();
471 if (line
.isValid()) {
472 wrappedText
+= QChar::LineSeparator
;
475 textLayout
.endLayout();
477 m_nameLabel
->setText(wrappedText
);
480 #include "informationpanelcontent.moc"