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 <kio/previewjob.h>
27 #include <kiconeffect.h>
28 #include <kiconloader.h>
31 #include <kseparator.h>
33 #include <Phonon/BackendCapabilities>
34 #include <Phonon/MediaObject>
35 #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 "phononwidget.h"
51 #include "pixmapviewer.h"
53 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
56 m_pendingPreview(false),
57 m_outdatedPreviewTimer(0),
64 parent
->installEventFilter(this);
66 // Initialize timer for disabling an outdated preview with a small
67 // delay. This prevents flickering if the new preview can be generated
68 // within a very small timeframe.
69 m_outdatedPreviewTimer
= new QTimer(this);
70 m_outdatedPreviewTimer
->setInterval(300);
71 m_outdatedPreviewTimer
->setSingleShot(true);
72 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
73 this, SLOT(markOutdatedPreview()));
75 QVBoxLayout
* layout
= new QVBoxLayout
;
76 layout
->setSpacing(KDialog::spacingHint());
79 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
81 m_preview
= new PixmapViewer(parent
);
82 m_preview
->setMinimumWidth(minPreviewWidth
);
83 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
85 m_phononWidget
= new PhononWidget(parent
);
86 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
87 connect(m_phononWidget
, SIGNAL(playingStarted()),
88 this, SLOT(slotPlayingStarted()));
89 connect(m_phononWidget
, SIGNAL(playingStopped()),
90 this, SLOT(slotPlayingStopped()));
93 m_nameLabel
= new QLabel(parent
);
94 QFont font
= m_nameLabel
->font();
96 m_nameLabel
->setFont(font
);
97 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
98 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
100 const bool showPreview
= InformationPanelSettings::showPreview();
101 m_preview
->setVisible(showPreview
);
103 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
104 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
105 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
107 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
108 // at the bottom. This prevents that the meta data widget gets vertically stretched
109 // in the case where the height of m_metaDataArea > m_metaDataWidget.
110 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
111 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
112 containerLayout
->setContentsMargins(0, 0, 0, 0);
113 containerLayout
->setSpacing(0);
114 containerLayout
->addWidget(m_metaDataWidget
);
115 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
116 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
117 containerLayout
->addWidget(stretchWidget
);
119 m_metaDataArea
= new QScrollArea(parent
);
120 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
121 m_metaDataArea
->setWidgetResizable(true);
122 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
124 QWidget
* viewport
= m_metaDataArea
->viewport();
125 viewport
->installEventFilter(this);
127 QPalette palette
= viewport
->palette();
128 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
129 viewport
->setPalette(palette
);
131 layout
->addWidget(m_preview
);
132 layout
->addWidget(m_phononWidget
);
133 layout
->addWidget(m_nameLabel
);
134 layout
->addWidget(new KSeparator());
135 layout
->addWidget(m_metaDataArea
);
136 parent
->setLayout(layout
);
139 InformationPanelContent::~InformationPanelContent()
141 InformationPanelSettings::self()->writeConfig();
144 void InformationPanelContent::showItem(const KFileItem
& item
)
146 m_pendingPreview
= false;
148 const KUrl itemUrl
= item
.url();
149 const bool isNepomukSearchUrl
= itemUrl
.protocol().startsWith("nepomuk") && item
.nepomukUri().isEmpty();
150 if (!applyPlace(itemUrl
)) {
151 if (isNepomukSearchUrl
) {
152 // in the case of a Nepomuk query-URL the URL is not readable for humans
153 // (at least not useful to show in the Information Panel)
154 KIconLoader iconLoader
;
155 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
156 KIconLoader::NoGroup
,
157 KIconLoader::SizeEnormous
);
158 m_preview
->setPixmap(icon
);
159 setNameLabelText(QString());
161 // try to get a preview pixmap from the item...
162 m_pendingPreview
= true;
164 // Mark the currently shown preview as outdated. This is done
165 // with a small delay to prevent a flickering when the next preview
166 // can be shown within a short timeframe. This timer is not started
167 // for directories, as directory previews might fail and return the
170 m_outdatedPreviewTimer
->start();
173 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
181 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
182 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
183 connect(job
, SIGNAL(failed(const KFileItem
&)),
184 this, SLOT(showIcon(const KFileItem
&)));
186 setNameLabelText(item
.text());
190 if (m_metaDataWidget
!= 0) {
191 if (isNepomukSearchUrl
) {
192 m_metaDataWidget
->hide();
194 m_metaDataWidget
->show();
195 m_metaDataWidget
->setItems(KFileItemList() << item
);
199 if (InformationPanelSettings::showPreview()) {
200 const QString mimeType
= item
.mimetype();
201 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
202 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
203 // thinks it supports PNG images
205 m_phononWidget
->show();
206 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
207 ? PhononWidget::Video
208 : PhononWidget::Audio
;
209 m_phononWidget
->setMode(mode
);
210 m_phononWidget
->setUrl(item
.url());
211 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
212 m_phononWidget
->setVideoSize(m_preview
->size());
215 m_phononWidget
->hide();
216 m_preview
->setVisible(true);
219 m_phononWidget
->hide();
225 void InformationPanelContent::showItems(const KFileItemList
& items
)
227 m_pendingPreview
= false;
229 KIconLoader iconLoader
;
230 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
231 KIconLoader::NoGroup
,
232 KIconLoader::SizeEnormous
);
233 m_preview
->setPixmap(icon
);
234 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
236 if (m_metaDataWidget
!= 0) {
237 m_metaDataWidget
->setItems(items
);
240 m_phononWidget
->hide();
242 m_item
= KFileItem();
245 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
247 switch (event
->type()) {
248 case QEvent::Resize
: {
249 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
250 if (obj
== m_metaDataArea
->viewport()) {
251 // The size of the meta text area has changed. Adjust the fixed
252 // width in a way that no horizontal scrollbar needs to be shown.
253 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
254 } else if (obj
== parent()) {
255 adjustWidgetSizes(resizeEvent
->size().width());
261 adjustWidgetSizes(parentWidget()->width());
268 return Panel::eventFilter(obj
, event
);
271 void InformationPanelContent::configureSettings()
275 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
276 previewAction
->setIcon(KIcon("view-preview"));
277 previewAction
->setCheckable(true);
278 previewAction
->setChecked(InformationPanelSettings::showPreview());
280 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
281 configureAction
->setIcon(KIcon("configure"));
283 // Open the popup and adjust the settings for the
285 QAction
* action
= popup
.exec(QCursor::pos());
290 const bool isChecked
= action
->isChecked();
291 if (action
== previewAction
) {
292 m_preview
->setVisible(isChecked
);
293 InformationPanelSettings::setShowPreview(isChecked
);
294 } else if (action
== configureAction
) {
296 //QPointer<KFileMetaDataConfigurationDialog> dialog =
297 // new KFileMetaDataConfigurationDialog(this);
298 //dialog->setDescription(i18nc("@label::textbox",
299 // "Configure which data should be shown in the Information Panel."));
300 //dialog->setItems(m_metaDataWidget->items());
305 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
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 bool InformationPanelContent::applyPlace(const KUrl
& url
)
352 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
353 int count
= placesModel
->rowCount();
355 for (int i
= 0; i
< count
; ++i
) {
356 QModelIndex index
= placesModel
->index(i
, 0);
358 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
359 setNameLabelText(placesModel
->text(index
));
360 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
368 void InformationPanelContent::setNameLabelText(const QString
& text
)
370 QTextOption textOption
;
371 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
373 QTextLayout
textLayout(text
);
374 textLayout
.setFont(m_nameLabel
->font());
375 textLayout
.setTextOption(textOption
);
378 wrappedText
.reserve(text
.length());
380 // wrap the text to fit into the width of m_nameLabel
381 textLayout
.beginLayout();
382 QTextLine line
= textLayout
.createLine();
383 while (line
.isValid()) {
384 line
.setLineWidth(m_nameLabel
->width());
385 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
387 line
= textLayout
.createLine();
388 if (line
.isValid()) {
389 wrappedText
+= QChar::LineSeparator
;
392 textLayout
.endLayout();
394 m_nameLabel
->setText(wrappedText
);
397 void InformationPanelContent::adjustWidgetSizes(int width
)
399 // If the text inside the name label or the info label cannot
400 // get wrapped, then the maximum width of the label is increased
401 // so that the width of the information panel gets increased.
402 // To prevent this, the maximum width is adjusted to
403 // the current width of the panel.
404 const int maxWidth
= width
- KDialog::spacingHint() * 4;
405 m_nameLabel
->setMaximumWidth(maxWidth
);
407 // The metadata widget also contains a text widget which may return
408 // a large preferred width.
409 if (m_metaDataWidget
!= 0) {
410 m_metaDataWidget
->setMaximumWidth(maxWidth
);
413 // try to increase the preview as large as possible
414 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
416 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
417 // assure that the size of the video player is the same as the preview size
418 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
422 #include "informationpanelcontent.moc"