1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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"
24 #include <KGlobalSettings>
25 #include <KIO/JobUiDelegate>
26 #include <KIO/PreviewJob>
27 #include <KIconEffect>
28 #include <KIconLoader>
31 #include <kseparator.h>
32 #include <KStringHandler>
35 #include <KFileMetaDataWidget>
37 #include <nepomuk2/filemetadatawidget.h>
40 #include <panels/places/placesitem.h>
41 #include <panels/places/placesitemmodel.h>
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextDocument>
54 #include <QTextLayout>
57 #include <QVBoxLayout>
59 #include "dolphin_informationpanelsettings.h"
60 #include "filemetadataconfigurationdialog.h"
61 #include "phononwidget.h"
62 #include "pixmapviewer.h"
64 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(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(this);
88 layout
->setSpacing(KDialog::spacingHint());
91 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
93 m_preview
= new PixmapViewer(parent
);
94 m_preview
->setMinimumWidth(minPreviewWidth
);
95 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
97 m_phononWidget
= new PhononWidget(parent
);
98 m_phononWidget
->hide();
99 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
100 connect(m_phononWidget
, SIGNAL(hasVideoChanged(bool)),
101 this, SLOT(slotHasVideoChanged(bool)));
104 m_nameLabel
= new QLabel(parent
);
105 QFont font
= m_nameLabel
->font();
107 m_nameLabel
->setFont(font
);
108 m_nameLabel
->setTextFormat(Qt::PlainText
);
109 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
110 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 const bool previewsShown
= InformationPanelSettings::previewsShown();
113 m_preview
->setVisible(previewsShown
);
116 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
118 m_metaDataWidget
= new Nepomuk2::FileMetaDataWidget(parent
);
120 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
121 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
122 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
124 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
125 // at the bottom. This prevents that the meta data widget gets vertically stretched
126 // in the case where the height of m_metaDataArea > m_metaDataWidget.
127 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
128 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
129 containerLayout
->setContentsMargins(0, 0, 0, 0);
130 containerLayout
->setSpacing(0);
131 containerLayout
->addWidget(m_metaDataWidget
);
132 containerLayout
->addStretch();
134 m_metaDataArea
= new QScrollArea(parent
);
135 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
136 m_metaDataArea
->setWidgetResizable(true);
137 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
139 QWidget
* viewport
= m_metaDataArea
->viewport();
140 viewport
->installEventFilter(this);
142 QPalette palette
= viewport
->palette();
143 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
144 viewport
->setPalette(palette
);
146 layout
->addWidget(m_preview
);
147 layout
->addWidget(m_phononWidget
);
148 layout
->addWidget(m_nameLabel
);
149 layout
->addWidget(new KSeparator());
150 layout
->addWidget(m_metaDataArea
);
152 m_placesItemModel
= new PlacesItemModel(this);
155 InformationPanelContent::~InformationPanelContent()
157 InformationPanelSettings::self()->writeConfig();
160 void InformationPanelContent::showItem(const KFileItem
& item
)
162 m_pendingPreview
= false;
164 const KUrl itemUrl
= item
.url();
165 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
166 if (!applyPlace(itemUrl
)) {
167 setNameLabelText(item
.text());
169 // in the case of a search-URL the URL is not readable for humans
170 // (at least not useful to show in the Information Panel)
171 KIconLoader iconLoader
;
172 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
173 KIconLoader::NoGroup
,
174 KIconLoader::SizeEnormous
);
175 m_preview
->setPixmap(icon
);
177 // try to get a preview pixmap from the item...
178 m_pendingPreview
= true;
180 // Mark the currently shown preview as outdated. This is done
181 // with a small delay to prevent a flickering when the next preview
182 // can be shown within a short timeframe. This timer is not started
183 // for directories, as directory previews might fail and return the
186 m_outdatedPreviewTimer
->start();
189 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
190 job
->setScaleType(KIO::PreviewJob::Unscaled
);
191 job
->setIgnoreMaximumSize(item
.isLocalFile());
193 job
->ui()->setWindow(this);
196 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
197 this, SLOT(showPreview(KFileItem
,QPixmap
)));
198 connect(job
, SIGNAL(failed(KFileItem
)),
199 this, SLOT(showIcon(KFileItem
)));
203 if (m_metaDataWidget
) {
204 m_metaDataWidget
->show();
205 m_metaDataWidget
->setItems(KFileItemList() << item
);
208 if (InformationPanelSettings::previewsShown()) {
209 const QString mimeType
= item
.mimetype();
210 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
212 m_phononWidget
->show();
213 m_phononWidget
->setUrl(item
.targetUrl());
214 if (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("@label", "%1 item selected", "%1 items selected", items
.count()));
239 if (m_metaDataWidget
) {
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 QWidget::eventFilter(obj
, event
);
274 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
278 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
279 previewAction
->setIcon(KIcon("view-preview"));
280 previewAction
->setCheckable(true);
281 previewAction
->setChecked(InformationPanelSettings::previewsShown());
283 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
284 configureAction
->setIcon(KIcon("configure"));
286 popup
.addSeparator();
287 foreach (QAction
* action
, customContextMenuActions
) {
288 popup
.addAction(action
);
291 // Open the popup and adjust the settings for the
293 QAction
* action
= popup
.exec(QCursor::pos());
298 const bool isChecked
= action
->isChecked();
299 if (action
== previewAction
) {
300 m_preview
->setVisible(isChecked
);
301 InformationPanelSettings::setPreviewsShown(isChecked
);
302 } else if (action
== configureAction
) {
303 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
304 dialog
->setDescription(i18nc("@label::textbox",
305 "Select which data should be shown in the information panel:"));
306 dialog
->setItems(m_metaDataWidget
->items());
307 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
310 dialog
->activateWindow();
311 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
315 void InformationPanelContent::showIcon(const KFileItem
& item
)
317 m_outdatedPreviewTimer
->stop();
318 m_pendingPreview
= false;
319 if (!applyPlace(item
.targetUrl())) {
320 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
321 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
325 void InformationPanelContent::showPreview(const KFileItem
& item
,
326 const QPixmap
& pixmap
)
328 m_outdatedPreviewTimer
->stop();
330 if (m_pendingPreview
) {
332 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
333 m_preview
->setPixmap(p
);
334 m_pendingPreview
= false;
338 void InformationPanelContent::markOutdatedPreview()
340 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
341 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
342 KIconLoader::Desktop
,
343 KIconLoader::DisabledState
);
344 m_preview
->setPixmap(disabledPixmap
);
347 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
349 m_preview
->setVisible(!hasVideo
);
352 void InformationPanelContent::refreshMetaData()
354 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
359 bool InformationPanelContent::applyPlace(const KUrl
& url
)
361 const int count
= m_placesItemModel
->count();
362 for (int i
= 0; i
< count
; ++i
) {
363 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
364 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
365 setNameLabelText(item
->text());
366 m_preview
->setPixmap(KIcon(item
->icon()).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
) {
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()) {
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"