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>
33 #include <kstringhandler.h>
35 #include <Phonon/BackendCapabilities>
36 #include <Phonon/MediaObject>
37 #include <Phonon/SeekSlider>
43 #include <QResizeEvent>
44 #include <QScrollArea>
45 #include <QTextDocument>
46 #include <QTextLayout>
49 #include <QVBoxLayout>
51 #include "dolphin_informationpanelsettings.h"
52 #include "settings/dolphinsettings.h"
53 #include "settings/filemetadataconfigurationdialog.h"
54 #include "phononwidget.h"
55 #include "pixmapviewer.h"
57 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
60 m_pendingPreview(false),
61 m_outdatedPreviewTimer(0),
68 parent
->installEventFilter(this);
70 // Initialize timer for disabling an outdated preview with a small
71 // delay. This prevents flickering if the new preview can be generated
72 // within a very small timeframe.
73 m_outdatedPreviewTimer
= new QTimer(this);
74 m_outdatedPreviewTimer
->setInterval(300);
75 m_outdatedPreviewTimer
->setSingleShot(true);
76 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
77 this, SLOT(markOutdatedPreview()));
79 QVBoxLayout
* layout
= new QVBoxLayout(this);
80 layout
->setSpacing(KDialog::spacingHint());
83 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
85 m_preview
= new PixmapViewer(parent
);
86 m_preview
->setMinimumWidth(minPreviewWidth
);
87 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
89 m_phononWidget
= new PhononWidget(parent
);
90 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
91 connect(m_phononWidget
, SIGNAL(playingStarted()),
92 this, SLOT(slotPlayingStarted()));
93 connect(m_phononWidget
, SIGNAL(playingStopped()),
94 this, SLOT(slotPlayingStopped()));
97 m_nameLabel
= new QLabel(parent
);
98 QFont font
= m_nameLabel
->font();
100 m_nameLabel
->setFont(font
);
101 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
102 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
104 const bool showPreview
= InformationPanelSettings::showPreview();
105 m_preview
->setVisible(showPreview
);
107 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
108 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
109 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
110 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
112 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
113 // at the bottom. This prevents that the meta data widget gets vertically stretched
114 // in the case where the height of m_metaDataArea > m_metaDataWidget.
115 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
116 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
117 containerLayout
->setContentsMargins(0, 0, 0, 0);
118 containerLayout
->setSpacing(0);
119 containerLayout
->addWidget(m_metaDataWidget
);
120 containerLayout
->addStretch();
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
);
141 InformationPanelContent::~InformationPanelContent()
143 InformationPanelSettings::self()->writeConfig();
146 void InformationPanelContent::showItem(const KFileItem
& item
)
148 m_pendingPreview
= false;
150 const KUrl itemUrl
= item
.url();
151 const bool isNepomukSearchUrl
= itemUrl
.protocol().startsWith("nepomuk") && item
.nepomukUri().isEmpty();
152 if (!applyPlace(itemUrl
)) {
153 if (isNepomukSearchUrl
) {
154 // in the case of a Nepomuk query-URL the URL is not readable for humans
155 // (at least not useful to show in the Information Panel)
156 KIconLoader iconLoader
;
157 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
158 KIconLoader::NoGroup
,
159 KIconLoader::SizeEnormous
);
160 m_preview
->setPixmap(icon
);
161 setNameLabelText(QString());
163 // try to get a preview pixmap from the item...
164 m_pendingPreview
= true;
166 // Mark the currently shown preview as outdated. This is done
167 // with a small delay to prevent a flickering when the next preview
168 // can be shown within a short timeframe. This timer is not started
169 // for directories, as directory previews might fail and return the
172 m_outdatedPreviewTimer
->start();
175 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
183 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
184 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
185 connect(job
, SIGNAL(failed(const KFileItem
&)),
186 this, SLOT(showIcon(const KFileItem
&)));
188 setNameLabelText(item
.text());
192 if (m_metaDataWidget
!= 0) {
193 if (isNepomukSearchUrl
) {
194 m_metaDataWidget
->hide();
196 m_metaDataWidget
->show();
197 m_metaDataWidget
->setItems(KFileItemList() << item
);
201 if (InformationPanelSettings::showPreview()) {
202 const QString mimeType
= item
.mimetype();
203 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
204 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
205 // thinks it supports PNG images
207 m_phononWidget
->show();
208 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
209 ? PhononWidget::Video
210 : PhononWidget::Audio
;
211 m_phononWidget
->setMode(mode
);
212 m_phononWidget
->setUrl(item
.url());
213 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
214 m_phononWidget
->setVideoSize(m_preview
->size());
217 m_phononWidget
->hide();
218 m_preview
->setVisible(true);
221 m_phononWidget
->hide();
227 void InformationPanelContent::showItems(const KFileItemList
& items
)
229 m_pendingPreview
= false;
231 KIconLoader iconLoader
;
232 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
233 KIconLoader::NoGroup
,
234 KIconLoader::SizeEnormous
);
235 m_preview
->setPixmap(icon
);
236 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
238 if (m_metaDataWidget
!= 0) {
239 m_metaDataWidget
->setItems(items
);
242 m_phononWidget
->hide();
244 m_item
= KFileItem();
247 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
249 switch (event
->type()) {
250 case QEvent::Resize
: {
251 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
252 if (obj
== m_metaDataArea
->viewport()) {
253 // The size of the meta text area has changed. Adjust the fixed
254 // width in a way that no horizontal scrollbar needs to be shown.
255 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
256 } else if (obj
== parent()) {
257 adjustWidgetSizes(resizeEvent
->size().width());
263 adjustWidgetSizes(parentWidget()->width());
270 return Panel::eventFilter(obj
, event
);
273 void InformationPanelContent::configureSettings()
277 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
278 previewAction
->setIcon(KIcon("view-preview"));
279 previewAction
->setCheckable(true);
280 previewAction
->setChecked(InformationPanelSettings::showPreview());
282 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
283 configureAction
->setIcon(KIcon("configure"));
285 // Open the popup and adjust the settings for the
287 QAction
* action
= popup
.exec(QCursor::pos());
292 const bool isChecked
= action
->isChecked();
293 if (action
== previewAction
) {
294 m_preview
->setVisible(isChecked
);
295 InformationPanelSettings::setShowPreview(isChecked
);
296 } else if (action
== configureAction
) {
297 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
298 dialog
->setDescription(i18nc("@label::textbox",
299 "Select which data should be shown in the information panel."));
300 dialog
->setItems(m_metaDataWidget
->items());
301 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
304 dialog
->activateWindow();
305 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
309 void InformationPanelContent::showIcon(const KFileItem
& item
)
311 m_outdatedPreviewTimer
->stop();
312 m_pendingPreview
= false;
313 if (!applyPlace(item
.url())) {
314 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
318 void InformationPanelContent::showPreview(const KFileItem
& item
,
319 const QPixmap
& pixmap
)
321 m_outdatedPreviewTimer
->stop();
324 if (m_pendingPreview
) {
325 m_preview
->setPixmap(pixmap
);
326 m_pendingPreview
= false;
330 void InformationPanelContent::markOutdatedPreview()
332 KIconEffect iconEffect
;
333 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
334 KIconLoader::Desktop
,
335 KIconLoader::DisabledState
);
336 m_preview
->setPixmap(disabledPixmap
);
339 void InformationPanelContent::slotPlayingStarted()
341 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
344 void InformationPanelContent::slotPlayingStopped()
346 m_preview
->setVisible(true);
349 void InformationPanelContent::refreshMetaData()
351 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
356 bool InformationPanelContent::applyPlace(const KUrl
& url
)
358 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
359 int count
= placesModel
->rowCount();
361 for (int i
= 0; i
< count
; ++i
) {
362 QModelIndex index
= placesModel
->index(i
, 0);
364 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
365 setNameLabelText(placesModel
->text(index
));
366 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
374 void InformationPanelContent::setNameLabelText(const QString
& text
)
376 QTextOption textOption
;
377 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
379 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
381 QTextLayout
textLayout(processedText
);
382 textLayout
.setFont(m_nameLabel
->font());
383 textLayout
.setTextOption(textOption
);
386 wrappedText
.reserve(processedText
.length());
388 // wrap the text to fit into the width of m_nameLabel
389 textLayout
.beginLayout();
390 QTextLine line
= textLayout
.createLine();
391 while (line
.isValid()) {
392 line
.setLineWidth(m_nameLabel
->width());
393 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
395 line
= textLayout
.createLine();
396 if (line
.isValid()) {
397 wrappedText
+= QChar::LineSeparator
;
400 textLayout
.endLayout();
402 m_nameLabel
->setText(wrappedText
);
405 void InformationPanelContent::adjustWidgetSizes(int width
)
407 // If the text inside the name label or the info label cannot
408 // get wrapped, then the maximum width of the label is increased
409 // so that the width of the information panel gets increased.
410 // To prevent this, the maximum width is adjusted to
411 // the current width of the panel.
412 const int maxWidth
= width
- KDialog::spacingHint() * 4;
413 m_nameLabel
->setMaximumWidth(maxWidth
);
415 // The metadata widget also contains a text widget which may return
416 // a large preferred width.
417 if (m_metaDataWidget
!= 0) {
418 m_metaDataWidget
->setMaximumWidth(maxWidth
);
421 // try to increase the preview as large as possible
422 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
424 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
425 // assure that the size of the video player is the same as the preview size
426 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
430 #include "informationpanelcontent.moc"