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 <KConfigGroup>
10 #include <KIO/PreviewJob>
11 #include <KIconEffect>
12 #include <KIconLoader>
14 #include <KJobWidgets>
15 #include <KLocalizedString>
17 #include <KSharedConfig>
18 #include <KStringHandler>
19 #include <QPainterPath>
23 #include <QTextDocument>
25 #include <Baloo/FileMetaDataWidget>
27 #include <phonon/BackendCapabilities>
28 #include <phonon/MediaObject>
30 #include <QDialogButtonBox>
33 #include <QLinearGradient>
36 #include <QScrollArea>
38 #include <QTextLayout>
40 #include <QVBoxLayout>
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)
56 , m_nameLabel(nullptr)
57 , m_metaDataWidget(nullptr)
58 , m_metaDataArea(nullptr)
61 parent
->installEventFilter(this);
63 // Initialize timer for disabling an outdated preview with a small
64 // delay. This prevents flickering if the new preview can be generated
65 // within a very small timeframe.
66 m_outdatedPreviewTimer
= new QTimer(this);
67 m_outdatedPreviewTimer
->setInterval(100);
68 m_outdatedPreviewTimer
->setSingleShot(true);
69 connect(m_outdatedPreviewTimer
, &QTimer::timeout
, this, &InformationPanelContent::markOutdatedPreview
);
71 QVBoxLayout
*layout
= new QVBoxLayout(this);
74 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
76 m_preview
= new PixmapViewer(parent
);
77 m_preview
->setMinimumWidth(minPreviewWidth
);
78 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
80 m_phononWidget
= new PhononWidget(parent
);
81 m_phononWidget
->hide();
82 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
83 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
84 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
, this, &InformationPanelContent::slotHasVideoChanged
);
87 m_nameLabel
= new QLabel(parent
);
88 QFont font
= m_nameLabel
->font();
90 m_nameLabel
->setFont(font
);
91 m_nameLabel
->setTextFormat(Qt::PlainText
);
92 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
93 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
94 m_nameLabel
->setTextInteractionFlags(Qt::TextSelectableByMouse
);
96 const bool previewsShown
= InformationPanelSettings::previewsShown();
97 m_preview
->setVisible(previewsShown
);
99 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
100 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
101 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
, this, &InformationPanelContent::urlActivated
);
102 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
103 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
106 m_configureLabel
= new QLabel(i18nc("@label::textbox", "Select which data should be shown:"), this);
107 m_configureLabel
->setWordWrap(true);
108 m_configureLabel
->setVisible(false);
110 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
111 m_configureButtons
->setVisible(false);
112 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
113 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
114 m_configureButtons
->setVisible(false);
115 m_configureLabel
->setVisible(false);
116 Q_EMIT
configurationFinished();
118 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
119 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
120 m_configureButtons
->setVisible(false);
121 m_configureLabel
->setVisible(false);
122 Q_EMIT
configurationFinished();
125 m_metaDataArea
= new QScrollArea(parent
);
126 m_metaDataArea
->setWidget(m_metaDataWidget
);
127 m_metaDataArea
->setWidgetResizable(true);
128 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
130 QWidget
*viewport
= m_metaDataArea
->viewport();
131 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
132 viewport
->installEventFilter(this);
134 layout
->addWidget(m_preview
);
135 layout
->addWidget(m_phononWidget
);
136 layout
->addWidget(m_nameLabel
);
137 layout
->addWidget(new KSeparator());
138 layout
->addWidget(m_configureLabel
);
139 layout
->addWidget(m_metaDataArea
);
140 layout
->addWidget(m_configureButtons
);
142 grabGesture(Qt::TapAndHoldGesture
);
145 InformationPanelContent::~InformationPanelContent()
147 InformationPanelSettings::self()->save();
150 void InformationPanelContent::showItem(const KFileItem
&item
)
152 // compares item entries, comparing items only compares urls
153 if (m_item
.entry() != item
.entry()) {
155 m_preview
->stopAnimatedImage();
162 void InformationPanelContent::refreshPixmapView()
164 // If there is a preview job, kill it to prevent that we have jobs for
165 // multiple items running, and thus a race condition (bug 250787).
167 m_previewJob
->kill();
170 // try to get a preview pixmap from the item...
172 // Mark the currently shown preview as outdated. This is done
173 // with a small delay to prevent a flickering when the next preview
174 // can be shown within a short timeframe.
175 m_outdatedPreviewTimer
->start();
177 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
178 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
179 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(m_preview
->width(), m_preview
->height()), &plugins
);
180 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
181 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile() && !m_item
.isSlow());
182 m_previewJob
->setDevicePixelRatio(devicePixelRatioF());
183 if (m_previewJob
->uiDelegate()) {
184 KJobWidgets::setWindow(m_previewJob
, this);
187 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
, this, &InformationPanelContent::showPreview
);
188 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
, this, &InformationPanelContent::showIcon
);
191 void InformationPanelContent::refreshPreview()
193 // If there is a preview job, kill it to prevent that we have jobs for
194 // multiple items running, and thus a race condition (bug 250787).
196 m_previewJob
->kill();
199 m_preview
->setCursor(Qt::ArrowCursor
);
200 setNameLabelText(m_item
.text());
201 if (InformationPanelSettings::previewsShown()) {
202 const QUrl itemUrl
= m_item
.url();
203 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
206 m_phononWidget
->hide();
208 // in the case of a search-URL the URL is not readable for humans
209 // (at least not useful to show in the Information Panel)
210 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width()));
214 const QString mimeType
= m_item
.mimetype();
215 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
216 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
217 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
220 // change the cursor of the preview
221 m_preview
->setCursor(Qt::PointingHandCursor
);
222 m_preview
->installEventFilter(m_phononWidget
);
223 m_phononWidget
->show();
225 // if the video is playing, has been paused or stopped
226 // we don't need to update the preview/phonon widget states
227 // unless the previewed file has changed,
228 // or the setting previewshown has changed
229 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&& m_phononWidget
->state() != Phonon::State::PausedState
230 && m_phononWidget
->state() != Phonon::State::StoppedState
)
231 || m_item
.targetUrl() != m_phononWidget
->url() || (!m_preview
->isVisible() && !m_phononWidget
->isVisible())) {
232 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
233 // hides the preview now to avoid flickering when the autoplay video starts
236 // the video won't play before the preview is displayed
240 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
241 adjustWidgetSizes(parentWidget()->width());
244 if (isAnimatedImage
) {
245 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
247 // When we don't need it, hide the phonon widget first to avoid flickering
248 m_phononWidget
->hide();
250 m_preview
->removeEventFilter(m_phononWidget
);
251 m_phononWidget
->clearUrl();
255 m_preview
->stopAnimatedImage();
257 m_phononWidget
->hide();
261 void InformationPanelContent::configureShownProperties()
263 m_configureLabel
->setVisible(true);
264 m_configureButtons
->setVisible(true);
265 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
268 void InformationPanelContent::refreshMetaData()
270 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
271 m_metaDataWidget
->show();
272 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
275 void InformationPanelContent::showItems(const KFileItemList
&items
)
277 // If there is a preview job, kill it to prevent that we have jobs for
278 // multiple items running, and thus a race condition (bug 250787).
280 m_previewJob
->kill();
283 m_preview
->stopAnimatedImage();
285 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width()));
286 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
288 m_metaDataWidget
->setItems(items
);
290 m_phononWidget
->hide();
292 m_item
= KFileItem();
295 bool InformationPanelContent::eventFilter(QObject
*obj
, QEvent
*event
)
297 switch (event
->type()) {
298 case QEvent::Resize
: {
299 QResizeEvent
*resizeEvent
= static_cast<QResizeEvent
*>(event
);
300 if (obj
== m_metaDataArea
->viewport()) {
301 // The size of the meta text area has changed. Adjust the fixed
302 // width in a way that no horizontal scrollbar needs to be shown.
303 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
304 } else if (obj
== parent()) {
305 adjustWidgetSizes(resizeEvent
->size().width());
311 adjustWidgetSizes(parentWidget()->width());
314 case QEvent::FontChange
:
315 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
322 return QWidget::eventFilter(obj
, event
);
325 bool InformationPanelContent::event(QEvent
*event
)
327 if (event
->type() == QEvent::Gesture
) {
328 gestureEvent(static_cast<QGestureEvent
*>(event
));
331 return QWidget::event(event
);
334 bool InformationPanelContent::gestureEvent(QGestureEvent
*event
)
340 QTapAndHoldGesture
*tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
343 if (tap
->state() == Qt::GestureFinished
) {
344 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
352 void InformationPanelContent::showIcon(const KFileItem
&item
)
354 m_outdatedPreviewTimer
->stop();
355 QIcon icon
= QIcon::fromTheme(item
.iconName());
356 QPixmap pixmap
= KIconUtils::addOverlays(icon
, item
.overlays()).pixmap(m_preview
->size(), devicePixelRatioF());
357 pixmap
.setDevicePixelRatio(devicePixelRatioF());
358 m_preview
->setPixmap(pixmap
);
361 void InformationPanelContent::showPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
363 m_outdatedPreviewTimer
->stop();
365 const QSize logicalSize
= pixmap
.size() / pixmap
.devicePixelRatioF();
366 QPixmap p
= KIconUtils::addOverlays(pixmap
, item
.overlays()).pixmap(logicalSize
, pixmap
.devicePixelRatioF());
367 p
.setDevicePixelRatio(pixmap
.devicePixelRatioF());
370 // adds a play arrow overlay
372 auto maxDim
= qMax(p
.width(), p
.height());
373 auto arrowSize
= qMax(PLAY_ARROW_SIZE
, maxDim
/ 8);
375 // compute relative pixel positions
376 const int zeroX
= static_cast<int>((p
.width() / 2 - arrowSize
/ 2) / p
.devicePixelRatio());
377 const int zeroY
= static_cast<int>((p
.height() / 2 - arrowSize
/ 2) / p
.devicePixelRatio());
380 arrow
<< QPoint(zeroX
, zeroY
);
381 arrow
<< QPoint(zeroX
, zeroY
+ arrowSize
);
382 arrow
<< QPoint(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2);
385 path
.addPolygon(arrow
);
387 QLinearGradient
gradient(QPointF(zeroX
, zeroY
+ arrowSize
/ 2), QPointF(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2));
389 QColor whiteColor
= Qt::white
;
390 QColor blackColor
= Qt::black
;
391 gradient
.setColorAt(0, whiteColor
);
392 gradient
.setColorAt(1, blackColor
);
394 QBrush
brush(gradient
);
396 QPainter
painter(&p
);
398 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
401 painter
.setRenderHint(QPainter::Antialiasing
);
402 painter
.drawPolygon(arrow
);
403 painter
.fillPath(path
, brush
);
406 m_preview
->setPixmap(p
);
409 void InformationPanelContent::markOutdatedPreview()
411 if (m_item
.isDir()) {
412 // directory preview can be long
413 // but since we always have icons to display
414 // use it until the preview is done
417 #if KICONTHEMES_VERSION >= QT_VERSION_CHECK(6, 5, 0)
418 QPixmap disabledPixmap
= m_preview
->pixmap();
419 KIconEffect::toDisabled(disabledPixmap
);
421 QImage img
= m_preview
->pixmap().toImage();
422 KIconEffect::toGray(img
, 1);
423 KIconEffect::semiTransparent(img
);
424 QPixmap disabledPixmap
= QPixmap::fromImage(img
);
426 m_preview
->setPixmap(disabledPixmap
);
430 KFileItemList
InformationPanelContent::items()
432 return m_metaDataWidget
->items();
435 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
437 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
438 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
439 // in case the information panel has been resized when the preview was not displayed
440 // we need to refresh its content
445 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
)
447 m_phononWidget
->setAutoPlay(autoPlay
);
450 void InformationPanelContent::setNameLabelText(const QString
&text
)
452 QTextOption textOption
;
453 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
455 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
457 QTextLayout
textLayout(processedText
);
458 textLayout
.setFont(m_nameLabel
->font());
459 textLayout
.setTextOption(textOption
);
462 wrappedText
.reserve(processedText
.length());
464 // wrap the text to fit into the width of m_nameLabel
465 textLayout
.beginLayout();
466 QTextLine line
= textLayout
.createLine();
467 while (line
.isValid()) {
468 line
.setLineWidth(m_nameLabel
->width());
469 wrappedText
+= QStringView(processedText
).mid(line
.textStart(), line
.textLength());
471 line
= textLayout
.createLine();
472 if (line
.isValid()) {
473 wrappedText
+= QChar::LineSeparator
;
476 textLayout
.endLayout();
478 m_nameLabel
->setText(wrappedText
);
481 void InformationPanelContent::adjustWidgetSizes(int width
)
483 // If the text inside the name label or the info label cannot
484 // get wrapped, then the maximum width of the label is increased
485 // so that the width of the information panel gets increased.
486 // To prevent this, the maximum width is adjusted to
487 // the current width of the panel.
488 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
489 m_nameLabel
->setMaximumWidth(maxWidth
);
491 // The metadata widget also contains a text widget which may return
492 // a large preferred width.
493 m_metaDataWidget
->setMaximumWidth(maxWidth
);
495 // try to increase the preview as large as possible
496 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
498 if (m_phononWidget
->isVisible()) {
499 // assure that the size of the video player is the same as the preview size
500 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
504 #include "moc_informationpanelcontent.cpp"