2 * SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "informationpanelcontent.h"
9 #include <KIO/JobUiDelegate>
10 #include <KIO/PreviewJob>
11 #include <KIconEffect>
12 #include <KIconLoader>
13 #include <KJobWidgets>
14 #include <KLocalizedString>
16 #include <KStringHandler>
17 #include <QPainterPath>
20 #include <QTextDocument>
22 #include <Baloo/FileMetaDataWidget>
24 #include <panels/places/placesitem.h>
25 #include <panels/places/placesitemmodel.h>
27 #include <Phonon/BackendCapabilities>
28 #include <Phonon/MediaObject>
31 #include <QDialogButtonBox>
32 #include <QScrollArea>
33 #include <QTextLayout>
35 #include <QVBoxLayout>
39 #include <QLinearGradient>
42 #include "dolphin_informationpanelsettings.h"
43 #include "phononwidget.h"
44 #include "pixmapviewer.h"
46 const int PLAY_ARROW_SIZE
= 24;
47 const int PLAY_ARROW_BORDER_SIZE
= 2;
49 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
52 m_previewJob(nullptr),
53 m_outdatedPreviewTimer(nullptr),
55 m_phononWidget(nullptr),
57 m_metaDataWidget(nullptr),
58 m_metaDataArea(nullptr),
59 m_placesItemModel(nullptr),
62 parent
->installEventFilter(this);
64 // Initialize timer for disabling an outdated preview with a small
65 // delay. This prevents flickering if the new preview can be generated
66 // within a very small timeframe.
67 m_outdatedPreviewTimer
= new QTimer(this);
68 m_outdatedPreviewTimer
->setInterval(100);
69 m_outdatedPreviewTimer
->setSingleShot(true);
70 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
71 this, &InformationPanelContent::markOutdatedPreview
);
73 QVBoxLayout
* layout
= new QVBoxLayout(this);
76 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
78 m_preview
= new PixmapViewer(parent
);
79 m_preview
->setMinimumWidth(minPreviewWidth
);
80 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
82 m_phononWidget
= new PhononWidget(parent
);
83 m_phononWidget
->hide();
84 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
85 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
86 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
87 this, &InformationPanelContent::slotHasVideoChanged
);
90 m_nameLabel
= new QLabel(parent
);
91 QFont font
= m_nameLabel
->font();
93 m_nameLabel
->setFont(font
);
94 m_nameLabel
->setTextFormat(Qt::PlainText
);
95 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
96 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
98 const bool previewsShown
= InformationPanelSettings::previewsShown();
99 m_preview
->setVisible(previewsShown
);
101 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
102 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
103 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
104 this, &InformationPanelContent::urlActivated
);
105 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
106 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
109 m_configureLabel
= new QLabel(i18nc("@label::textbox",
110 "Select which data should be shown:"), this);
111 m_configureLabel
->setWordWrap(true);
112 m_configureLabel
->setVisible(false);
114 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
115 m_configureButtons
->setVisible(false);
116 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
117 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
118 m_configureButtons
->setVisible(false);
119 m_configureLabel
->setVisible(false);
120 emit
configurationFinished();
123 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
124 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
125 m_configureButtons
->setVisible(false);
126 m_configureLabel
->setVisible(false);
127 emit
configurationFinished();
131 m_metaDataArea
= new QScrollArea(parent
);
132 m_metaDataArea
->setWidget(m_metaDataWidget
);
133 m_metaDataArea
->setWidgetResizable(true);
134 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
136 QWidget
* viewport
= m_metaDataArea
->viewport();
137 viewport
->installEventFilter(this);
139 layout
->addWidget(m_preview
);
140 layout
->addWidget(m_phononWidget
);
141 layout
->addWidget(m_nameLabel
);
142 layout
->addWidget(new KSeparator());
143 layout
->addWidget(m_configureLabel
);
144 layout
->addWidget(m_metaDataArea
);
145 layout
->addWidget(m_configureButtons
);
147 m_placesItemModel
= new PlacesItemModel(this);
150 InformationPanelContent::~InformationPanelContent()
152 InformationPanelSettings::self()->save();
155 void InformationPanelContent::showItem(const KFileItem
& item
)
157 // compares item entries, comparing items only compares urls
158 if (m_item
.entry() != item
.entry()) {
160 m_preview
->stopAnimatedImage();
167 void InformationPanelContent::refreshPixmapView()
169 // If there is a preview job, kill it to prevent that we have jobs for
170 // multiple items running, and thus a race condition (bug 250787).
172 m_previewJob
->kill();
175 // try to get a preview pixmap from the item...
177 // Mark the currently shown preview as outdated. This is done
178 // with a small delay to prevent a flickering when the next preview
179 // can be shown within a short timeframe.
180 m_outdatedPreviewTimer
->start();
182 QStringList plugins
= KIO::PreviewJob::availablePlugins();
183 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
184 QSize(m_preview
->width(), m_preview
->height()),
186 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
187 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
188 if (m_previewJob
->uiDelegate()) {
189 KJobWidgets::setWindow(m_previewJob
, this);
192 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
193 this, &InformationPanelContent::showPreview
);
194 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
195 this, &InformationPanelContent::showIcon
);
198 void InformationPanelContent::refreshPreview()
200 // If there is a preview job, kill it to prevent that we have jobs for
201 // multiple items running, and thus a race condition (bug 250787).
203 m_previewJob
->kill();
206 m_preview
->setCursor(Qt::ArrowCursor
);
207 setNameLabelText(m_item
.text());
208 if (InformationPanelSettings::previewsShown()) {
210 const QUrl itemUrl
= m_item
.url();
211 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
214 m_phononWidget
->hide();
216 // in the case of a search-URL the URL is not readable for humans
217 // (at least not useful to show in the Information Panel)
218 m_preview
->setPixmap(
219 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width())
225 const QString mimeType
= m_item
.mimetype();
226 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
227 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
228 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
231 // change the cursor of the preview
232 m_preview
->setCursor(Qt::PointingHandCursor
);
233 m_preview
->installEventFilter(m_phononWidget
);
234 m_phononWidget
->show();
236 // if the video is playing, has been paused or stopped
237 // we don't need to update the preview/phonon widget states
238 // unless the previewed file has changed,
239 // or the setting previewshown has changed
240 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
241 m_phononWidget
->state() != Phonon::State::PausedState
&&
242 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
243 m_item
.targetUrl() != m_phononWidget
->url() ||
244 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
246 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
247 // hides the preview now to avoid flickering when the autoplay video starts
250 // the video won't play before the preview is displayed
254 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
255 adjustWidgetSizes(parentWidget()->width());
258 if (isAnimatedImage
) {
259 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
261 // When we don't need it, hide the phonon widget first to avoid flickering
262 m_phononWidget
->hide();
264 m_preview
->removeEventFilter(m_phononWidget
);
265 m_phononWidget
->clearUrl();
269 m_preview
->stopAnimatedImage();
271 m_phononWidget
->hide();
275 void InformationPanelContent::configureShownProperties()
277 m_configureLabel
->setVisible(true);
278 m_configureButtons
->setVisible(true);
279 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
282 void InformationPanelContent::refreshMetaData()
284 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
285 m_metaDataWidget
->show();
286 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
289 void InformationPanelContent::showItems(const KFileItemList
& items
)
291 // If there is a preview job, kill it to prevent that we have jobs for
292 // multiple items running, and thus a race condition (bug 250787).
294 m_previewJob
->kill();
297 m_preview
->stopAnimatedImage();
299 m_preview
->setPixmap(
300 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
302 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
304 m_metaDataWidget
->setItems(items
);
306 m_phononWidget
->hide();
308 m_item
= KFileItem();
311 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
313 switch (event
->type()) {
314 case QEvent::Resize
: {
315 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
316 if (obj
== m_metaDataArea
->viewport()) {
317 // The size of the meta text area has changed. Adjust the fixed
318 // width in a way that no horizontal scrollbar needs to be shown.
319 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
320 } else if (obj
== parent()) {
321 adjustWidgetSizes(resizeEvent
->size().width());
327 adjustWidgetSizes(parentWidget()->width());
330 case QEvent::FontChange
:
331 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
338 return QWidget::eventFilter(obj
, event
);
341 void InformationPanelContent::showIcon(const KFileItem
& item
)
343 m_outdatedPreviewTimer
->stop();
344 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->height(), m_preview
->width());
345 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
346 m_preview
->setPixmap(pixmap
);
349 void InformationPanelContent::showPreview(const KFileItem
& item
,
350 const QPixmap
& pixmap
)
352 m_outdatedPreviewTimer
->stop();
355 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
360 // compute relative pixel positions
361 const int zeroX
= static_cast<int>(p
.width() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
362 const int zeroY
= static_cast<int>(p
.height() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
365 arrow
<< QPoint(zeroX
, zeroY
);
366 arrow
<< QPoint(zeroX
, zeroY
+ PLAY_ARROW_SIZE
);
367 arrow
<< QPoint(zeroX
+ PLAY_ARROW_SIZE
, zeroY
+ PLAY_ARROW_SIZE
/ 2);
370 path
.addPolygon(arrow
);
372 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
373 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
375 QColor whiteColor
= Qt::white
;
376 QColor blackColor
= Qt::black
;
377 gradient
.setColorAt(0, whiteColor
);
378 gradient
.setColorAt(1, blackColor
);
380 QBrush
brush(gradient
);
382 QPainter
painter(&p
);
384 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
387 painter
.setRenderHint(QPainter::Antialiasing
);
388 painter
.drawPolygon(arrow
);
389 painter
.fillPath(path
, brush
);
392 m_preview
->setPixmap(p
);
395 void InformationPanelContent::markOutdatedPreview()
397 if (m_item
.isDir()) {
398 // directory preview can be long
399 // but since we always have icons to display
400 // use it until the preview is done
403 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
404 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
405 KIconLoader::Desktop
,
406 KIconLoader::DisabledState
);
407 m_preview
->setPixmap(disabledPixmap
);
411 KFileItemList
InformationPanelContent::items()
413 return m_metaDataWidget
->items();
416 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
418 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
419 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
420 // in case the information panel has been resized when the preview was not displayed
421 // we need to refresh its content
426 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
427 m_phononWidget
->setAutoPlay(autoPlay
);
430 void InformationPanelContent::setNameLabelText(const QString
& text
)
432 QTextOption textOption
;
433 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
435 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
437 QTextLayout
textLayout(processedText
);
438 textLayout
.setFont(m_nameLabel
->font());
439 textLayout
.setTextOption(textOption
);
442 wrappedText
.reserve(processedText
.length());
444 // wrap the text to fit into the width of m_nameLabel
445 textLayout
.beginLayout();
446 QTextLine line
= textLayout
.createLine();
447 while (line
.isValid()) {
448 line
.setLineWidth(m_nameLabel
->width());
449 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
451 line
= textLayout
.createLine();
452 if (line
.isValid()) {
453 wrappedText
+= QChar::LineSeparator
;
456 textLayout
.endLayout();
458 m_nameLabel
->setText(wrappedText
);
461 void InformationPanelContent::adjustWidgetSizes(int width
)
463 // If the text inside the name label or the info label cannot
464 // get wrapped, then the maximum width of the label is increased
465 // so that the width of the information panel gets increased.
466 // To prevent this, the maximum width is adjusted to
467 // the current width of the panel.
468 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
469 m_nameLabel
->setMaximumWidth(maxWidth
);
471 // The metadata widget also contains a text widget which may return
472 // a large preferred width.
473 m_metaDataWidget
->setMaximumWidth(maxWidth
);
475 // try to increase the preview as large as possible
476 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
478 if (m_phononWidget
->isVisible()) {
479 // assure that the size of the video player is the same as the preview size
480 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));