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
) :
68 m_previewJob(nullptr),
69 m_outdatedPreviewTimer(nullptr),
71 m_phononWidget(nullptr),
73 m_metaDataWidget(nullptr),
74 m_metaDataArea(nullptr),
75 m_placesItemModel(nullptr)
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::Ignored
, 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 layout
->addWidget(m_preview
);
146 layout
->addWidget(m_phononWidget
);
147 layout
->addWidget(m_nameLabel
);
148 layout
->addWidget(new KSeparator());
149 layout
->addWidget(m_metaDataArea
);
151 m_placesItemModel
= new PlacesItemModel(this);
154 InformationPanelContent::~InformationPanelContent()
156 InformationPanelSettings::self()->save();
159 void InformationPanelContent::showItem(const KFileItem
& item
)
161 // If there is a preview job, kill it to prevent that we have jobs for
162 // multiple items running, and thus a race condition (bug 250787).
164 m_previewJob
->kill();
167 const QUrl itemUrl
= item
.url();
168 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && item
.localPath().isEmpty();
169 if (!applyPlace(itemUrl
)) {
170 setNameLabelText(item
.text());
172 // in the case of a search-URL the URL is not readable for humans
173 // (at least not useful to show in the Information Panel)
174 m_preview
->setPixmap(
175 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
178 // try to get a preview pixmap from the item...
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 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
190 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
191 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
192 if (m_previewJob
->uiDelegate()) {
193 KJobWidgets::setWindow(m_previewJob
, this);
196 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
197 this, &InformationPanelContent::showPreview
);
198 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
199 this, &InformationPanelContent::showIcon
);
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(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("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 // If there is a preview job, kill it to prevent that we have jobs for
231 // multiple items running, and thus a race condition (bug 250787).
233 m_previewJob
->kill();
236 m_preview
->setPixmap(
237 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
239 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
241 if (m_metaDataWidget
) {
242 m_metaDataWidget
->setItems(items
);
245 m_phononWidget
->hide();
247 m_item
= KFileItem();
250 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
252 switch (event
->type()) {
253 case QEvent::Resize
: {
254 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
255 if (obj
== m_metaDataArea
->viewport()) {
256 // The size of the meta text area has changed. Adjust the fixed
257 // width in a way that no horizontal scrollbar needs to be shown.
258 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
259 } else if (obj
== parent()) {
260 adjustWidgetSizes(resizeEvent
->size().width());
266 adjustWidgetSizes(parentWidget()->width());
269 case QEvent::FontChange
:
270 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
277 return QWidget::eventFilter(obj
, event
);
280 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
284 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
285 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
286 previewAction
->setCheckable(true);
287 previewAction
->setChecked(InformationPanelSettings::previewsShown());
289 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
290 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
292 popup
.addSeparator();
293 foreach (QAction
* action
, customContextMenuActions
) {
294 popup
.addAction(action
);
297 // Open the popup and adjust the settings for the
299 QAction
* action
= popup
.exec(QCursor::pos());
304 const bool isChecked
= action
->isChecked();
305 if (action
== previewAction
) {
306 m_preview
->setVisible(isChecked
);
307 InformationPanelSettings::setPreviewsShown(isChecked
);
308 } else if (action
== configureAction
) {
309 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
310 dialog
->setDescription(i18nc("@label::textbox",
311 "Select which data should be shown in the information panel:"));
312 dialog
->setItems(m_metaDataWidget
->items());
313 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
315 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
319 void InformationPanelContent::showIcon(const KFileItem
& item
)
321 m_outdatedPreviewTimer
->stop();
322 if (!applyPlace(item
.targetUrl())) {
323 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
324 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
325 m_preview
->setPixmap(pixmap
);
329 void InformationPanelContent::showPreview(const KFileItem
& item
,
330 const QPixmap
& pixmap
)
332 m_outdatedPreviewTimer
->stop();
336 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
337 m_preview
->setPixmap(p
);
340 void InformationPanelContent::markOutdatedPreview()
342 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
343 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
344 KIconLoader::Desktop
,
345 KIconLoader::DisabledState
);
346 m_preview
->setPixmap(disabledPixmap
);
349 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
351 m_preview
->setVisible(!hasVideo
);
354 void InformationPanelContent::refreshMetaData()
356 if (!m_item
.isNull()) {
361 bool InformationPanelContent::applyPlace(const QUrl
& url
)
363 const int count
= m_placesItemModel
->count();
364 for (int i
= 0; i
< count
; ++i
) {
365 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
366 if (item
->url().matches(url
, QUrl::StripTrailingSlash
)) {
367 setNameLabelText(item
->text());
368 m_preview
->setPixmap(QIcon::fromTheme(item
->icon()).pixmap(128, 128));
376 void InformationPanelContent::setNameLabelText(const QString
& text
)
378 QTextOption textOption
;
379 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
381 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
383 QTextLayout
textLayout(processedText
);
384 textLayout
.setFont(m_nameLabel
->font());
385 textLayout
.setTextOption(textOption
);
388 wrappedText
.reserve(processedText
.length());
390 // wrap the text to fit into the width of m_nameLabel
391 textLayout
.beginLayout();
392 QTextLine line
= textLayout
.createLine();
393 while (line
.isValid()) {
394 line
.setLineWidth(m_nameLabel
->width());
395 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
397 line
= textLayout
.createLine();
398 if (line
.isValid()) {
399 wrappedText
+= QChar::LineSeparator
;
402 textLayout
.endLayout();
404 m_nameLabel
->setText(wrappedText
);
407 void InformationPanelContent::adjustWidgetSizes(int width
)
409 // If the text inside the name label or the info label cannot
410 // get wrapped, then the maximum width of the label is increased
411 // so that the width of the information panel gets increased.
412 // To prevent this, the maximum width is adjusted to
413 // the current width of the panel.
414 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
415 m_nameLabel
->setMaximumWidth(maxWidth
);
417 // The metadata widget also contains a text widget which may return
418 // a large preferred width.
419 if (m_metaDataWidget
) {
420 m_metaDataWidget
->setMaximumWidth(maxWidth
);
423 // try to increase the preview as large as possible
424 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
426 if (m_phononWidget
->isVisible()) {
427 // assure that the size of the video player is the same as the preview size
428 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));