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 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
174 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
175 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
176 if (m_previewJob
->uiDelegate()) {
177 KJobWidgets::setWindow(m_previewJob
, this);
180 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
181 this, &InformationPanelContent::showPreview
);
182 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
183 this, &InformationPanelContent::showIcon
);
186 if (m_metaDataWidget
) {
187 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
188 m_metaDataWidget
->show();
189 m_metaDataWidget
->setItems(KFileItemList() << item
);
192 if (InformationPanelSettings::previewsShown()) {
193 const QString mimeType
= item
.mimetype();
194 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
196 m_phononWidget
->show();
197 m_phononWidget
->setUrl(item
.targetUrl());
198 if (m_preview
->isVisible()) {
199 m_phononWidget
->setVideoSize(m_preview
->size());
202 m_phononWidget
->hide();
203 m_preview
->setVisible(true);
206 m_phononWidget
->hide();
212 void InformationPanelContent::showItems(const KFileItemList
& items
)
214 // If there is a preview job, kill it to prevent that we have jobs for
215 // multiple items running, and thus a race condition (bug 250787).
217 m_previewJob
->kill();
220 m_preview
->setPixmap(
221 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
223 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
225 if (m_metaDataWidget
) {
226 m_metaDataWidget
->setItems(items
);
229 m_phononWidget
->hide();
231 m_item
= KFileItem();
234 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
236 switch (event
->type()) {
237 case QEvent::Resize
: {
238 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
239 if (obj
== m_metaDataArea
->viewport()) {
240 // The size of the meta text area has changed. Adjust the fixed
241 // width in a way that no horizontal scrollbar needs to be shown.
242 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
243 } else if (obj
== parent()) {
244 adjustWidgetSizes(resizeEvent
->size().width());
250 adjustWidgetSizes(parentWidget()->width());
253 case QEvent::FontChange
:
254 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
261 return QWidget::eventFilter(obj
, event
);
264 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
268 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
269 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
270 previewAction
->setCheckable(true);
271 previewAction
->setChecked(InformationPanelSettings::previewsShown());
273 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
274 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
276 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
277 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
278 dateformatAction
->setCheckable(true);
279 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
281 popup
.addSeparator();
282 foreach (QAction
* action
, customContextMenuActions
) {
283 popup
.addAction(action
);
286 // Open the popup and adjust the settings for the
288 QAction
* action
= popup
.exec(QCursor::pos());
293 const bool isChecked
= action
->isChecked();
294 if (action
== previewAction
) {
295 m_preview
->setVisible(isChecked
);
296 InformationPanelSettings::setPreviewsShown(isChecked
);
297 } else if (action
== configureAction
) {
298 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
299 dialog
->setDescription(i18nc("@label::textbox",
300 "Select which data should be shown in the information panel:"));
301 dialog
->setItems(m_metaDataWidget
->items());
302 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
304 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
306 if (action
== dateformatAction
) {
307 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
309 InformationPanelSettings::setDateFormat(dateFormat
);
314 void InformationPanelContent::showIcon(const KFileItem
& item
)
316 m_outdatedPreviewTimer
->stop();
317 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
318 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
319 m_preview
->setPixmap(pixmap
);
322 void InformationPanelContent::showPreview(const KFileItem
& item
,
323 const QPixmap
& pixmap
)
325 m_outdatedPreviewTimer
->stop();
329 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
330 m_preview
->setPixmap(p
);
333 void InformationPanelContent::markOutdatedPreview()
335 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
336 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
337 KIconLoader::Desktop
,
338 KIconLoader::DisabledState
);
339 m_preview
->setPixmap(disabledPixmap
);
342 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
344 m_preview
->setVisible(!hasVideo
);
347 void InformationPanelContent::refreshMetaData()
349 if (!m_item
.isNull()) {
354 void InformationPanelContent::setNameLabelText(const QString
& text
)
356 QTextOption textOption
;
357 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
359 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
361 QTextLayout
textLayout(processedText
);
362 textLayout
.setFont(m_nameLabel
->font());
363 textLayout
.setTextOption(textOption
);
366 wrappedText
.reserve(processedText
.length());
368 // wrap the text to fit into the width of m_nameLabel
369 textLayout
.beginLayout();
370 QTextLine line
= textLayout
.createLine();
371 while (line
.isValid()) {
372 line
.setLineWidth(m_nameLabel
->width());
373 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
375 line
= textLayout
.createLine();
376 if (line
.isValid()) {
377 wrappedText
+= QChar::LineSeparator
;
380 textLayout
.endLayout();
382 m_nameLabel
->setText(wrappedText
);
385 void InformationPanelContent::adjustWidgetSizes(int width
)
387 // If the text inside the name label or the info label cannot
388 // get wrapped, then the maximum width of the label is increased
389 // so that the width of the information panel gets increased.
390 // To prevent this, the maximum width is adjusted to
391 // the current width of the panel.
392 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
393 m_nameLabel
->setMaximumWidth(maxWidth
);
395 // The metadata widget also contains a text widget which may return
396 // a large preferred width.
397 if (m_metaDataWidget
) {
398 m_metaDataWidget
->setMaximumWidth(maxWidth
);
401 // try to increase the preview as large as possible
402 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
404 if (m_phononWidget
->isVisible()) {
405 // assure that the size of the video player is the same as the preview size
406 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));