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"
23 #include <kfileitem.h>
24 #include <kfilemetadatawidget.h>
25 #include <kfileplacesmodel.h>
26 #include <kglobalsettings.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>
42 #include <QResizeEvent>
43 #include <QScrollArea>
44 #include <QTextLayout>
47 #include <QVBoxLayout>
49 #include "dolphin_informationpanelsettings.h"
50 #include "settings/dolphinsettings.h"
51 #include "settings/filemetadataconfigurationdialog.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
55 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
58 m_pendingPreview(false),
59 m_outdatedPreviewTimer(0),
66 parent
->installEventFilter(this);
68 // Initialize timer for disabling an outdated preview with a small
69 // delay. This prevents flickering if the new preview can be generated
70 // within a very small timeframe.
71 m_outdatedPreviewTimer
= new QTimer(this);
72 m_outdatedPreviewTimer
->setInterval(300);
73 m_outdatedPreviewTimer
->setSingleShot(true);
74 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
75 this, SLOT(markOutdatedPreview()));
77 QVBoxLayout
* layout
= new QVBoxLayout
;
78 layout
->setSpacing(KDialog::spacingHint());
81 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
83 m_preview
= new PixmapViewer(parent
);
84 m_preview
->setMinimumWidth(minPreviewWidth
);
85 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
87 m_phononWidget
= new PhononWidget(parent
);
88 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
89 connect(m_phononWidget
, SIGNAL(playingStarted()),
90 this, SLOT(slotPlayingStarted()));
91 connect(m_phononWidget
, SIGNAL(playingStopped()),
92 this, SLOT(slotPlayingStopped()));
95 m_nameLabel
= new QLabel(parent
);
96 QFont font
= m_nameLabel
->font();
98 m_nameLabel
->setFont(font
);
99 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
100 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
102 const bool showPreview
= InformationPanelSettings::showPreview();
103 m_preview
->setVisible(showPreview
);
105 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
106 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
107 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
108 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
110 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
111 // at the bottom. This prevents that the meta data widget gets vertically stretched
112 // in the case where the height of m_metaDataArea > m_metaDataWidget.
113 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
114 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
115 containerLayout
->setContentsMargins(0, 0, 0, 0);
116 containerLayout
->setSpacing(0);
117 containerLayout
->addWidget(m_metaDataWidget
);
118 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
119 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
120 containerLayout
->addWidget(stretchWidget
);
122 m_metaDataArea
= new QScrollArea(parent
);
123 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
124 m_metaDataArea
->setWidgetResizable(true);
125 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
127 QWidget
* viewport
= m_metaDataArea
->viewport();
128 viewport
->installEventFilter(this);
130 QPalette palette
= viewport
->palette();
131 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
132 viewport
->setPalette(palette
);
134 layout
->addWidget(m_preview
);
135 layout
->addWidget(m_phononWidget
);
136 layout
->addWidget(m_nameLabel
);
137 layout
->addWidget(new KSeparator());
138 layout
->addWidget(m_metaDataArea
);
139 parent
->setLayout(layout
);
142 InformationPanelContent::~InformationPanelContent()
144 InformationPanelSettings::self()->writeConfig();
147 void InformationPanelContent::showItem(const KFileItem
& item
)
149 m_pendingPreview
= false;
151 const KUrl itemUrl
= item
.url();
152 const bool isNepomukSearchUrl
= itemUrl
.protocol().startsWith("nepomuk") && item
.nepomukUri().isEmpty();
153 if (!applyPlace(itemUrl
)) {
154 if (isNepomukSearchUrl
) {
155 // in the case of a Nepomuk query-URL the URL is not readable for humans
156 // (at least not useful to show in the Information Panel)
157 KIconLoader iconLoader
;
158 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
159 KIconLoader::NoGroup
,
160 KIconLoader::SizeEnormous
);
161 m_preview
->setPixmap(icon
);
162 setNameLabelText(QString());
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(item
.text());
193 if (m_metaDataWidget
!= 0) {
194 if (isNepomukSearchUrl
) {
195 m_metaDataWidget
->hide();
197 m_metaDataWidget
->show();
198 m_metaDataWidget
->setItems(KFileItemList() << item
);
202 if (InformationPanelSettings::showPreview()) {
203 const QString mimeType
= item
.mimetype();
204 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
205 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
206 // thinks it supports PNG images
208 m_phononWidget
->show();
209 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
210 ? PhononWidget::Video
211 : PhononWidget::Audio
;
212 m_phononWidget
->setMode(mode
);
213 m_phononWidget
->setUrl(item
.url());
214 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
215 m_phononWidget
->setVideoSize(m_preview
->size());
218 m_phononWidget
->hide();
219 m_preview
->setVisible(true);
222 m_phononWidget
->hide();
228 void InformationPanelContent::showItems(const KFileItemList
& items
)
230 m_pendingPreview
= false;
232 KIconLoader iconLoader
;
233 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
234 KIconLoader::NoGroup
,
235 KIconLoader::SizeEnormous
);
236 m_preview
->setPixmap(icon
);
237 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
239 if (m_metaDataWidget
!= 0) {
240 m_metaDataWidget
->setItems(items
);
243 m_phononWidget
->hide();
245 m_item
= KFileItem();
248 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
250 switch (event
->type()) {
251 case QEvent::Resize
: {
252 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
253 if (obj
== m_metaDataArea
->viewport()) {
254 // The size of the meta text area has changed. Adjust the fixed
255 // width in a way that no horizontal scrollbar needs to be shown.
256 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
257 } else if (obj
== parent()) {
258 adjustWidgetSizes(resizeEvent
->size().width());
264 adjustWidgetSizes(parentWidget()->width());
271 return Panel::eventFilter(obj
, event
);
274 void InformationPanelContent::configureSettings()
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 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
284 configureAction
->setIcon(KIcon("configure"));
286 // Open the popup and adjust the settings for the
288 QAction
* action
= popup
.exec(QCursor::pos());
293 const bool isChecked
= action
->isChecked();
294 if (action
== previewAction
) {
295 m_preview
->setVisible(isChecked
);
296 InformationPanelSettings::setShowPreview(isChecked
);
297 } else if (action
== configureAction
) {
298 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
299 dialog
->setDescription(i18nc("@label::textbox",
300 "Configure which data should be shown in the tooltip."));
301 dialog
->setItems(m_metaDataWidget
->items());
302 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
305 dialog
->activateWindow();
306 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
310 void InformationPanelContent::showIcon(const KFileItem
& item
)
312 m_outdatedPreviewTimer
->stop();
313 m_pendingPreview
= false;
314 if (!applyPlace(item
.url())) {
315 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
319 void InformationPanelContent::showPreview(const KFileItem
& item
,
320 const QPixmap
& pixmap
)
322 m_outdatedPreviewTimer
->stop();
325 if (m_pendingPreview
) {
326 m_preview
->setPixmap(pixmap
);
327 m_pendingPreview
= false;
331 void InformationPanelContent::markOutdatedPreview()
333 KIconEffect iconEffect
;
334 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
335 KIconLoader::Desktop
,
336 KIconLoader::DisabledState
);
337 m_preview
->setPixmap(disabledPixmap
);
340 void InformationPanelContent::slotPlayingStarted()
342 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
345 void InformationPanelContent::slotPlayingStopped()
347 m_preview
->setVisible(true);
350 void InformationPanelContent::refreshMetaData()
352 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
357 bool InformationPanelContent::applyPlace(const KUrl
& url
)
359 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
360 int count
= placesModel
->rowCount();
362 for (int i
= 0; i
< count
; ++i
) {
363 QModelIndex index
= placesModel
->index(i
, 0);
365 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
366 setNameLabelText(placesModel
->text(index
));
367 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
375 void InformationPanelContent::setNameLabelText(const QString
& text
)
377 QTextOption textOption
;
378 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
380 QTextLayout
textLayout(text
);
381 textLayout
.setFont(m_nameLabel
->font());
382 textLayout
.setTextOption(textOption
);
385 wrappedText
.reserve(text
.length());
387 // wrap the text to fit into the width of m_nameLabel
388 textLayout
.beginLayout();
389 QTextLine line
= textLayout
.createLine();
390 while (line
.isValid()) {
391 line
.setLineWidth(m_nameLabel
->width());
392 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
394 line
= textLayout
.createLine();
395 if (line
.isValid()) {
396 wrappedText
+= QChar::LineSeparator
;
399 textLayout
.endLayout();
401 m_nameLabel
->setText(wrappedText
);
404 void InformationPanelContent::adjustWidgetSizes(int width
)
406 // If the text inside the name label or the info label cannot
407 // get wrapped, then the maximum width of the label is increased
408 // so that the width of the information panel gets increased.
409 // To prevent this, the maximum width is adjusted to
410 // the current width of the panel.
411 const int maxWidth
= width
- KDialog::spacingHint() * 4;
412 m_nameLabel
->setMaximumWidth(maxWidth
);
414 // The metadata widget also contains a text widget which may return
415 // a large preferred width.
416 if (m_metaDataWidget
!= 0) {
417 m_metaDataWidget
->setMaximumWidth(maxWidth
);
420 // try to increase the preview as large as possible
421 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
423 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
424 // assure that the size of the video player is the same as the preview size
425 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
429 #include "informationpanelcontent.moc"