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>
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>
48 #include <QScrollArea>
49 #include <QTextLayout>
51 #include <QVBoxLayout>
54 #include "dolphin_informationpanelsettings.h"
55 #include "filemetadataconfigurationdialog.h"
56 #include "phononwidget.h"
57 #include "pixmapviewer.h"
59 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
62 m_previewJob(nullptr),
63 m_outdatedPreviewTimer(nullptr),
65 m_phononWidget(nullptr),
67 m_metaDataWidget(nullptr),
68 m_metaDataArea(nullptr),
69 m_placesItemModel(nullptr)
71 parent
->installEventFilter(this);
73 // Initialize timer for disabling an outdated preview with a small
74 // delay. This prevents flickering if the new preview can be generated
75 // within a very small timeframe.
76 m_outdatedPreviewTimer
= new QTimer(this);
77 m_outdatedPreviewTimer
->setInterval(300);
78 m_outdatedPreviewTimer
->setSingleShot(true);
79 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
80 this, &InformationPanelContent::markOutdatedPreview
);
82 QVBoxLayout
* layout
= new QVBoxLayout(this);
85 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
87 m_preview
= new PixmapViewer(parent
);
88 m_preview
->setMinimumWidth(minPreviewWidth
);
89 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
91 m_phononWidget
= new PhononWidget(parent
);
92 m_phononWidget
->hide();
93 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
94 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
95 this, &InformationPanelContent::slotHasVideoChanged
);
98 m_nameLabel
= new QLabel(parent
);
99 QFont font
= m_nameLabel
->font();
101 m_nameLabel
->setFont(font
);
102 m_nameLabel
->setTextFormat(Qt::PlainText
);
103 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
104 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
106 const bool previewsShown
= InformationPanelSettings::previewsShown();
107 m_preview
->setVisible(previewsShown
);
110 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
111 connect(m_metaDataWidget
, &KFileMetaDataWidget::urlActivated
,
112 this, &InformationPanelContent::urlActivated
);
114 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
115 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
116 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
117 this, &InformationPanelContent::urlActivated
);
119 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
120 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
122 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
123 // at the bottom. This prevents that the meta data widget gets vertically stretched
124 // in the case where the height of m_metaDataArea > m_metaDataWidget.
125 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
126 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
127 containerLayout
->setContentsMargins(0, 0, 0, 0);
128 containerLayout
->setSpacing(0);
129 containerLayout
->addWidget(m_metaDataWidget
);
130 containerLayout
->addStretch();
132 m_metaDataArea
= new QScrollArea(parent
);
133 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
134 m_metaDataArea
->setWidgetResizable(true);
135 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
137 QWidget
* viewport
= m_metaDataArea
->viewport();
138 viewport
->installEventFilter(this);
140 layout
->addWidget(m_preview
);
141 layout
->addWidget(m_phononWidget
);
142 layout
->addWidget(m_nameLabel
);
143 layout
->addWidget(new KSeparator());
144 layout
->addWidget(m_metaDataArea
);
146 m_placesItemModel
= new PlacesItemModel(this);
149 InformationPanelContent::~InformationPanelContent()
151 InformationPanelSettings::self()->save();
154 void InformationPanelContent::showItem(const KFileItem
& item
)
156 // If there is a preview job, kill it to prevent that we have jobs for
157 // multiple items running, and thus a race condition (bug 250787).
159 m_previewJob
->kill();
162 const QUrl itemUrl
= item
.url();
163 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && item
.localPath().isEmpty();
164 setNameLabelText(item
.text());
166 // in the case of a search-URL the URL is not readable for humans
167 // (at least not useful to show in the Information Panel)
168 m_preview
->setPixmap(
169 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
172 // try to get a preview pixmap from the item...
174 // Mark the currently shown preview as outdated. This is done
175 // with a small delay to prevent a flickering when the next preview
176 // can be shown within a short timeframe. This timer is not started
177 // for directories, as directory previews might fail and return the
180 m_outdatedPreviewTimer
->start();
183 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
184 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
185 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
186 if (m_previewJob
->uiDelegate()) {
187 KJobWidgets::setWindow(m_previewJob
, this);
190 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
191 this, &InformationPanelContent::showPreview
);
192 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
193 this, &InformationPanelContent::showIcon
);
196 if (m_metaDataWidget
) {
198 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
200 m_metaDataWidget
->show();
201 m_metaDataWidget
->setItems(KFileItemList() << item
);
204 if (InformationPanelSettings::previewsShown()) {
205 const QString mimeType
= item
.mimetype();
206 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
208 m_phononWidget
->show();
209 m_phononWidget
->setUrl(item
.targetUrl());
210 if (m_preview
->isVisible()) {
211 m_phononWidget
->setVideoSize(m_preview
->size());
214 m_phononWidget
->hide();
215 m_preview
->setVisible(true);
218 m_phononWidget
->hide();
224 void InformationPanelContent::showItems(const KFileItemList
& items
)
226 // If there is a preview job, kill it to prevent that we have jobs for
227 // multiple items running, and thus a race condition (bug 250787).
229 m_previewJob
->kill();
232 m_preview
->setPixmap(
233 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
235 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
237 if (m_metaDataWidget
) {
238 m_metaDataWidget
->setItems(items
);
241 m_phononWidget
->hide();
243 m_item
= KFileItem();
246 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
248 switch (event
->type()) {
249 case QEvent::Resize
: {
250 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
251 if (obj
== m_metaDataArea
->viewport()) {
252 // The size of the meta text area has changed. Adjust the fixed
253 // width in a way that no horizontal scrollbar needs to be shown.
254 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
255 } else if (obj
== parent()) {
256 adjustWidgetSizes(resizeEvent
->size().width());
262 adjustWidgetSizes(parentWidget()->width());
265 case QEvent::FontChange
:
266 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
273 return QWidget::eventFilter(obj
, event
);
276 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
280 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
281 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
282 previewAction
->setCheckable(true);
283 previewAction
->setChecked(InformationPanelSettings::previewsShown());
285 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
286 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
289 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
290 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
291 dateformatAction
->setCheckable(true);
292 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
294 popup
.addSeparator();
295 foreach (QAction
* action
, customContextMenuActions
) {
296 popup
.addAction(action
);
299 // Open the popup and adjust the settings for the
301 QAction
* action
= popup
.exec(QCursor::pos());
306 const bool isChecked
= action
->isChecked();
307 if (action
== previewAction
) {
308 m_preview
->setVisible(isChecked
);
309 InformationPanelSettings::setPreviewsShown(isChecked
);
310 } else if (action
== configureAction
) {
311 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
312 dialog
->setDescription(i18nc("@label::textbox",
313 "Select which data should be shown in the information panel:"));
314 dialog
->setItems(m_metaDataWidget
->items());
315 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
317 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
320 if (action
== dateformatAction
) {
321 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
323 InformationPanelSettings::setDateFormat(dateFormat
);
329 void InformationPanelContent::showIcon(const KFileItem
& item
)
331 m_outdatedPreviewTimer
->stop();
332 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
333 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
334 m_preview
->setPixmap(pixmap
);
337 void InformationPanelContent::showPreview(const KFileItem
& item
,
338 const QPixmap
& pixmap
)
340 m_outdatedPreviewTimer
->stop();
344 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
345 m_preview
->setPixmap(p
);
348 void InformationPanelContent::markOutdatedPreview()
350 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
351 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
352 KIconLoader::Desktop
,
353 KIconLoader::DisabledState
);
354 m_preview
->setPixmap(disabledPixmap
);
357 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
359 m_preview
->setVisible(!hasVideo
);
362 void InformationPanelContent::refreshMetaData()
364 if (!m_item
.isNull()) {
369 void InformationPanelContent::setNameLabelText(const QString
& text
)
371 QTextOption textOption
;
372 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
374 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
376 QTextLayout
textLayout(processedText
);
377 textLayout
.setFont(m_nameLabel
->font());
378 textLayout
.setTextOption(textOption
);
381 wrappedText
.reserve(processedText
.length());
383 // wrap the text to fit into the width of m_nameLabel
384 textLayout
.beginLayout();
385 QTextLine line
= textLayout
.createLine();
386 while (line
.isValid()) {
387 line
.setLineWidth(m_nameLabel
->width());
388 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
390 line
= textLayout
.createLine();
391 if (line
.isValid()) {
392 wrappedText
+= QChar::LineSeparator
;
395 textLayout
.endLayout();
397 m_nameLabel
->setText(wrappedText
);
400 void InformationPanelContent::adjustWidgetSizes(int width
)
402 // If the text inside the name label or the info label cannot
403 // get wrapped, then the maximum width of the label is increased
404 // so that the width of the information panel gets increased.
405 // To prevent this, the maximum width is adjusted to
406 // the current width of the panel.
407 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
408 m_nameLabel
->setMaximumWidth(maxWidth
);
410 // The metadata widget also contains a text widget which may return
411 // a large preferred width.
412 if (m_metaDataWidget
) {
413 m_metaDataWidget
->setMaximumWidth(maxWidth
);
416 // try to increase the preview as large as possible
417 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
419 if (m_phononWidget
->isVisible()) {
420 // assure that the size of the video player is the same as the preview size
421 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));