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"
22 #include <KIO/JobUiDelegate>
23 #include <KIO/PreviewJob>
24 #include <KIconEffect>
25 #include <KIconLoader>
26 #include <KJobWidgets>
27 #include <KLocalizedString>
29 #include <KStringHandler>
33 #include <QTextDocument>
35 #include <Baloo/FileMetaDataWidget>
37 #include <panels/places/placesitem.h>
38 #include <panels/places/placesitemmodel.h>
40 #include <Phonon/BackendCapabilities>
41 #include <Phonon/MediaObject>
44 #include <QScrollArea>
45 #include <QTextLayout>
47 #include <QVBoxLayout>
50 #include "dolphin_informationpanelsettings.h"
51 #include "filemetadataconfigurationdialog.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
55 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
58 m_previewJob(nullptr),
59 m_outdatedPreviewTimer(nullptr),
61 m_phononWidget(nullptr),
63 m_metaDataWidget(nullptr),
64 m_metaDataArea(nullptr),
65 m_placesItemModel(nullptr)
67 parent
->installEventFilter(this);
69 // Initialize timer for disabling an outdated preview with a small
70 // delay. This prevents flickering if the new preview can be generated
71 // within a very small timeframe.
72 m_outdatedPreviewTimer
= new QTimer(this);
73 m_outdatedPreviewTimer
->setInterval(300);
74 m_outdatedPreviewTimer
->setSingleShot(true);
75 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
76 this, &InformationPanelContent::markOutdatedPreview
);
78 QVBoxLayout
* layout
= new QVBoxLayout(this);
81 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
83 m_preview
= new PixmapViewer(parent
);
84 m_preview
->setMinimumWidth(minPreviewWidth
);
85 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
87 m_phononWidget
= new PhononWidget(parent
);
88 m_phononWidget
->hide();
89 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
90 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
91 this, &InformationPanelContent::slotHasVideoChanged
);
94 m_nameLabel
= new QLabel(parent
);
95 QFont font
= m_nameLabel
->font();
97 m_nameLabel
->setFont(font
);
98 m_nameLabel
->setTextFormat(Qt::PlainText
);
99 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
100 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
102 const bool previewsShown
= InformationPanelSettings::previewsShown();
103 m_preview
->setVisible(previewsShown
);
105 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
106 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
107 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
108 this, &InformationPanelContent::urlActivated
);
109 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
110 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
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 layout
->addWidget(m_preview
);
131 layout
->addWidget(m_phononWidget
);
132 layout
->addWidget(m_nameLabel
);
133 layout
->addWidget(new KSeparator());
134 layout
->addWidget(m_metaDataArea
);
136 m_placesItemModel
= new PlacesItemModel(this);
139 InformationPanelContent::~InformationPanelContent()
141 InformationPanelSettings::self()->save();
144 void InformationPanelContent::showItem(const KFileItem
& item
)
146 // If there is a preview job, kill it to prevent that we have jobs for
147 // multiple items running, and thus a race condition (bug 250787).
149 m_previewJob
->kill();
152 const QUrl itemUrl
= item
.url();
153 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && item
.localPath().isEmpty();
154 setNameLabelText(item
.text());
156 // in the case of a search-URL the URL is not readable for humans
157 // (at least not useful to show in the Information Panel)
158 m_preview
->setPixmap(
159 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
162 // try to get a preview pixmap from the item...
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 QStringList plugins
= KIO::PreviewJob::availablePlugins();
174 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
,
175 QSize(m_preview
->width(), m_preview
->height()),
177 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
178 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
179 if (m_previewJob
->uiDelegate()) {
180 KJobWidgets::setWindow(m_previewJob
, this);
183 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
184 this, &InformationPanelContent::showPreview
);
185 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
186 this, &InformationPanelContent::showIcon
);
189 if (m_metaDataWidget
) {
190 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
191 m_metaDataWidget
->show();
192 m_metaDataWidget
->setItems(KFileItemList() << item
);
195 if (InformationPanelSettings::previewsShown()) {
196 const QString mimeType
= item
.mimetype();
197 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
199 m_phononWidget
->show();
200 m_phononWidget
->setUrl(item
.targetUrl());
201 if (m_preview
->isVisible()) {
202 m_phononWidget
->setVideoSize(m_preview
->size());
205 m_phononWidget
->hide();
206 m_preview
->setVisible(true);
209 m_phononWidget
->hide();
215 void InformationPanelContent::showItems(const KFileItemList
& items
)
217 // If there is a preview job, kill it to prevent that we have jobs for
218 // multiple items running, and thus a race condition (bug 250787).
220 m_previewJob
->kill();
223 m_preview
->setPixmap(
224 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
226 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
228 if (m_metaDataWidget
) {
229 m_metaDataWidget
->setItems(items
);
232 m_phononWidget
->hide();
234 m_item
= KFileItem();
237 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
239 switch (event
->type()) {
240 case QEvent::Resize
: {
241 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
242 if (obj
== m_metaDataArea
->viewport()) {
243 // The size of the meta text area has changed. Adjust the fixed
244 // width in a way that no horizontal scrollbar needs to be shown.
245 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
246 } else if (obj
== parent()) {
247 adjustWidgetSizes(resizeEvent
->size().width());
253 adjustWidgetSizes(parentWidget()->width());
256 case QEvent::FontChange
:
257 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
264 return QWidget::eventFilter(obj
, event
);
267 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
, const QPointF
& pos
)
271 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
272 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
273 previewAction
->setCheckable(true);
274 previewAction
->setChecked(InformationPanelSettings::previewsShown());
276 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
277 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
279 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
280 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
281 dateformatAction
->setCheckable(true);
282 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
284 popup
.addSeparator();
285 foreach (QAction
* action
, customContextMenuActions
) {
286 popup
.addAction(action
);
289 // Open the popup and adjust the settings for the
291 QAction
* action
= popup
.exec(pos
.toPoint());
296 const bool isChecked
= action
->isChecked();
297 if (action
== previewAction
) {
298 m_preview
->setVisible(isChecked
);
299 InformationPanelSettings::setPreviewsShown(isChecked
);
300 } else if (action
== configureAction
) {
301 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
302 dialog
->setDescription(i18nc("@label::textbox",
303 "Select which data should be shown in the information panel:"));
304 dialog
->setItems(m_metaDataWidget
->items());
305 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
307 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
309 if (action
== dateformatAction
) {
310 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
312 InformationPanelSettings::setDateFormat(dateFormat
);
317 void InformationPanelContent::showIcon(const KFileItem
& item
)
319 m_outdatedPreviewTimer
->stop();
320 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
321 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
322 m_preview
->setPixmap(pixmap
);
325 void InformationPanelContent::showPreview(const KFileItem
& item
,
326 const QPixmap
& pixmap
)
328 m_outdatedPreviewTimer
->stop();
332 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
333 m_preview
->setPixmap(p
);
336 void InformationPanelContent::markOutdatedPreview()
338 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
339 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
340 KIconLoader::Desktop
,
341 KIconLoader::DisabledState
);
342 m_preview
->setPixmap(disabledPixmap
);
345 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
347 m_preview
->setVisible(!hasVideo
);
350 void InformationPanelContent::refreshMetaData()
352 if (!m_item
.isNull()) {
357 void InformationPanelContent::setNameLabelText(const QString
& text
)
359 QTextOption textOption
;
360 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
362 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
364 QTextLayout
textLayout(processedText
);
365 textLayout
.setFont(m_nameLabel
->font());
366 textLayout
.setTextOption(textOption
);
369 wrappedText
.reserve(processedText
.length());
371 // wrap the text to fit into the width of m_nameLabel
372 textLayout
.beginLayout();
373 QTextLine line
= textLayout
.createLine();
374 while (line
.isValid()) {
375 line
.setLineWidth(m_nameLabel
->width());
376 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
378 line
= textLayout
.createLine();
379 if (line
.isValid()) {
380 wrappedText
+= QChar::LineSeparator
;
383 textLayout
.endLayout();
385 m_nameLabel
->setText(wrappedText
);
388 void InformationPanelContent::adjustWidgetSizes(int width
)
390 // If the text inside the name label or the info label cannot
391 // get wrapped, then the maximum width of the label is increased
392 // so that the width of the information panel gets increased.
393 // To prevent this, the maximum width is adjusted to
394 // the current width of the panel.
395 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
396 m_nameLabel
->setMaximumWidth(maxWidth
);
398 // The metadata widget also contains a text widget which may return
399 // a large preferred width.
400 if (m_metaDataWidget
) {
401 m_metaDataWidget
->setMaximumWidth(maxWidth
);
404 // try to increase the preview as large as possible
405 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
407 if (m_phononWidget
->isVisible()) {
408 // assure that the size of the video player is the same as the preview size
409 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));