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"
23 #include <KIO/JobUiDelegate>
24 #include <KIO/PreviewJob>
25 #include <KJobWidgets>
26 #include <KIconEffect>
27 #include <KIconLoader>
29 #include <KLocalizedString>
32 #include <KStringHandler>
33 #include <QTextDocument>
36 #include <KFileMetaDataWidget>
38 #include <Baloo/FileMetaDataWidget>
41 #include <panels/places/placesitem.h>
42 #include <panels/places/placesitemmodel.h>
44 #include <Phonon/BackendCapabilities>
45 #include <Phonon/MediaObject>
46 #include <Phonon/SeekSlider>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextLayout>
56 #include <QVBoxLayout>
57 #include <QFontDatabase>
60 #include "dolphin_informationpanelsettings.h"
61 #include "filemetadataconfigurationdialog.h"
62 #include "phononwidget.h"
63 #include "pixmapviewer.h"
65 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
69 m_outdatedPreviewTimer(0),
77 parent
->installEventFilter(this);
79 // Initialize timer for disabling an outdated preview with a small
80 // delay. This prevents flickering if the new preview can be generated
81 // within a very small timeframe.
82 m_outdatedPreviewTimer
= new QTimer(this);
83 m_outdatedPreviewTimer
->setInterval(300);
84 m_outdatedPreviewTimer
->setSingleShot(true);
85 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
86 this, &InformationPanelContent::markOutdatedPreview
);
88 QVBoxLayout
* layout
= new QVBoxLayout(this);
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
, &PhononWidget::hasVideoChanged
,
101 this, &InformationPanelContent::slotHasVideoChanged
);
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
);
117 connect(m_metaDataWidget
, &KFileMetaDataWidget::urlActivated
,
118 this, &InformationPanelContent::urlActivated
);
120 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
121 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
122 this, &InformationPanelContent::urlActivated
);
124 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
125 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
127 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
128 // at the bottom. This prevents that the meta data widget gets vertically stretched
129 // in the case where the height of m_metaDataArea > m_metaDataWidget.
130 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
131 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
132 containerLayout
->setContentsMargins(0, 0, 0, 0);
133 containerLayout
->setSpacing(0);
134 containerLayout
->addWidget(m_metaDataWidget
);
135 containerLayout
->addStretch();
137 m_metaDataArea
= new QScrollArea(parent
);
138 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
139 m_metaDataArea
->setWidgetResizable(true);
140 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
142 QWidget
* viewport
= m_metaDataArea
->viewport();
143 viewport
->installEventFilter(this);
145 QPalette palette
= viewport
->palette();
146 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
147 viewport
->setPalette(palette
);
149 layout
->addWidget(m_preview
);
150 layout
->addWidget(m_phononWidget
);
151 layout
->addWidget(m_nameLabel
);
152 layout
->addWidget(new KSeparator());
153 layout
->addWidget(m_metaDataArea
);
155 m_placesItemModel
= new PlacesItemModel(this);
158 InformationPanelContent::~InformationPanelContent()
160 InformationPanelSettings::self()->save();
163 void InformationPanelContent::showItem(const KFileItem
& item
)
165 // If there is a preview job, kill it to prevent that we have jobs for
166 // multiple items running, and thus a race condition (bug 250787).
168 m_previewJob
->kill();
171 const QUrl itemUrl
= item
.url();
172 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && item
.localPath().isEmpty();
173 if (!applyPlace(itemUrl
)) {
174 setNameLabelText(item
.text());
176 // in the case of a search-URL the URL is not readable for humans
177 // (at least not useful to show in the Information Panel)
178 KIconLoader iconLoader
;
179 QPixmap icon
= iconLoader
.loadIcon(QStringLiteral("nepomuk"),
180 KIconLoader::NoGroup
,
181 KIconLoader::SizeEnormous
);
182 m_preview
->setPixmap(icon
);
184 // try to get a preview pixmap from the item...
186 // Mark the currently shown preview as outdated. This is done
187 // with a small delay to prevent a flickering when the next preview
188 // can be shown within a short timeframe. This timer is not started
189 // for directories, as directory previews might fail and return the
192 m_outdatedPreviewTimer
->start();
195 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
196 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
197 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
198 if (m_previewJob
->ui()) {
199 KJobWidgets::setWindow(m_previewJob
, this);
202 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
203 this, &InformationPanelContent::showPreview
);
204 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
205 this, &InformationPanelContent::showIcon
);
209 if (m_metaDataWidget
) {
210 m_metaDataWidget
->show();
211 m_metaDataWidget
->setItems(KFileItemList() << item
);
214 if (InformationPanelSettings::previewsShown()) {
215 const QString mimeType
= item
.mimetype();
216 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
218 m_phononWidget
->show();
219 m_phononWidget
->setUrl(item
.targetUrl());
220 if (m_preview
->isVisible()) {
221 m_phononWidget
->setVideoSize(m_preview
->size());
224 m_phononWidget
->hide();
225 m_preview
->setVisible(true);
228 m_phononWidget
->hide();
234 void InformationPanelContent::showItems(const KFileItemList
& items
)
236 // If there is a preview job, kill it to prevent that we have jobs for
237 // multiple items running, and thus a race condition (bug 250787).
239 m_previewJob
->kill();
242 KIconLoader iconLoader
;
243 QPixmap icon
= iconLoader
.loadIcon(QStringLiteral("dialog-information"),
244 KIconLoader::NoGroup
,
245 KIconLoader::SizeEnormous
);
246 m_preview
->setPixmap(icon
);
247 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
249 if (m_metaDataWidget
) {
250 m_metaDataWidget
->setItems(items
);
253 m_phononWidget
->hide();
255 m_item
= KFileItem();
258 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
260 switch (event
->type()) {
261 case QEvent::Resize
: {
262 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
263 if (obj
== m_metaDataArea
->viewport()) {
264 // The size of the meta text area has changed. Adjust the fixed
265 // width in a way that no horizontal scrollbar needs to be shown.
266 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
267 } else if (obj
== parent()) {
268 adjustWidgetSizes(resizeEvent
->size().width());
274 adjustWidgetSizes(parentWidget()->width());
277 case QEvent::FontChange
:
278 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
285 return QWidget::eventFilter(obj
, event
);
288 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
292 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
293 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
294 previewAction
->setCheckable(true);
295 previewAction
->setChecked(InformationPanelSettings::previewsShown());
297 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
298 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
300 popup
.addSeparator();
301 foreach (QAction
* action
, customContextMenuActions
) {
302 popup
.addAction(action
);
305 // Open the popup and adjust the settings for the
307 QAction
* action
= popup
.exec(QCursor::pos());
312 const bool isChecked
= action
->isChecked();
313 if (action
== previewAction
) {
314 m_preview
->setVisible(isChecked
);
315 InformationPanelSettings::setPreviewsShown(isChecked
);
316 } else if (action
== configureAction
) {
317 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
318 dialog
->setDescription(i18nc("@label::textbox",
319 "Select which data should be shown in the information panel:"));
320 dialog
->setItems(m_metaDataWidget
->items());
321 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
323 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
327 void InformationPanelContent::showIcon(const KFileItem
& item
)
329 m_outdatedPreviewTimer
->stop();
330 if (!applyPlace(item
.targetUrl())) {
331 const QPixmap icon
= KIconLoader::global()->loadIcon(item
.iconName(), KIconLoader::Desktop
,
332 KIconLoader::SizeEnormous
, KIconLoader::DefaultState
,
334 m_preview
->setPixmap(icon
);
338 void InformationPanelContent::showPreview(const KFileItem
& item
,
339 const QPixmap
& pixmap
)
341 m_outdatedPreviewTimer
->stop();
345 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
346 m_preview
->setPixmap(p
);
349 void InformationPanelContent::markOutdatedPreview()
351 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
352 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
353 KIconLoader::Desktop
,
354 KIconLoader::DisabledState
);
355 m_preview
->setPixmap(disabledPixmap
);
358 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
360 m_preview
->setVisible(!hasVideo
);
363 void InformationPanelContent::refreshMetaData()
365 if (!m_item
.isNull()) {
370 bool InformationPanelContent::applyPlace(const QUrl
& url
)
372 const int count
= m_placesItemModel
->count();
373 for (int i
= 0; i
< count
; ++i
) {
374 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
375 if (item
->url().matches(url
, QUrl::StripTrailingSlash
)) {
376 setNameLabelText(item
->text());
377 m_preview
->setPixmap(QIcon::fromTheme(item
->icon()).pixmap(128, 128));
385 void InformationPanelContent::setNameLabelText(const QString
& text
)
387 QTextOption textOption
;
388 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
390 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
392 QTextLayout
textLayout(processedText
);
393 textLayout
.setFont(m_nameLabel
->font());
394 textLayout
.setTextOption(textOption
);
397 wrappedText
.reserve(processedText
.length());
399 // wrap the text to fit into the width of m_nameLabel
400 textLayout
.beginLayout();
401 QTextLine line
= textLayout
.createLine();
402 while (line
.isValid()) {
403 line
.setLineWidth(m_nameLabel
->width());
404 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
406 line
= textLayout
.createLine();
407 if (line
.isValid()) {
408 wrappedText
+= QChar::LineSeparator
;
411 textLayout
.endLayout();
413 m_nameLabel
->setText(wrappedText
);
416 void InformationPanelContent::adjustWidgetSizes(int width
)
418 // If the text inside the name label or the info label cannot
419 // get wrapped, then the maximum width of the label is increased
420 // so that the width of the information panel gets increased.
421 // To prevent this, the maximum width is adjusted to
422 // the current width of the panel.
423 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
424 m_nameLabel
->setMaximumWidth(maxWidth
);
426 // The metadata widget also contains a text widget which may return
427 // a large preferred width.
428 if (m_metaDataWidget
) {
429 m_metaDataWidget
->setMaximumWidth(maxWidth
);
432 // try to increase the preview as large as possible
433 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
435 if (m_phononWidget
->isVisible()) {
436 // assure that the size of the video player is the same as the preview size
437 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));