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>
13 #include <KJobWidgets>
14 #include <KLocalizedString>
16 #include <KSharedConfig>
17 #include <KStringHandler>
18 #include <QPainterPath>
22 #include <QTextDocument>
24 #include <Baloo/FileMetaDataWidget>
26 #include <phonon/BackendCapabilities>
27 #include <phonon/MediaObject>
29 #include <QDialogButtonBox>
32 #include <QLinearGradient>
35 #include <QScrollArea>
37 #include <QTextLayout>
39 #include <QVBoxLayout>
41 #include "dolphin_informationpanelsettings.h"
42 #include "phononwidget.h"
43 #include "pixmapviewer.h"
45 const int PLAY_ARROW_SIZE
= 24;
46 const int PLAY_ARROW_BORDER_SIZE
= 2;
48 InformationPanelContent::InformationPanelContent(QWidget
*parent
)
51 , m_previewJob(nullptr)
52 , m_outdatedPreviewTimer(nullptr)
54 , m_phononWidget(nullptr)
55 , m_nameLabel(nullptr)
56 , m_metaDataWidget(nullptr)
57 , m_metaDataArea(nullptr)
60 parent
->installEventFilter(this);
62 // Initialize timer for disabling an outdated preview with a small
63 // delay. This prevents flickering if the new preview can be generated
64 // within a very small timeframe.
65 m_outdatedPreviewTimer
= new QTimer(this);
66 m_outdatedPreviewTimer
->setInterval(100);
67 m_outdatedPreviewTimer
->setSingleShot(true);
68 connect(m_outdatedPreviewTimer
, &QTimer::timeout
, this, &InformationPanelContent::markOutdatedPreview
);
70 QVBoxLayout
*layout
= new QVBoxLayout(this);
73 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
75 m_preview
= new PixmapViewer(parent
);
76 m_preview
->setMinimumWidth(minPreviewWidth
);
77 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
79 m_phononWidget
= new PhononWidget(parent
);
80 m_phononWidget
->hide();
81 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
82 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
83 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
, this, &InformationPanelContent::slotHasVideoChanged
);
86 m_nameLabel
= new QLabel(parent
);
87 QFont font
= m_nameLabel
->font();
89 m_nameLabel
->setFont(font
);
90 m_nameLabel
->setTextFormat(Qt::PlainText
);
91 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
92 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
93 m_nameLabel
->setTextInteractionFlags(Qt::TextSelectableByMouse
);
95 const bool previewsShown
= InformationPanelSettings::previewsShown();
96 m_preview
->setVisible(previewsShown
);
98 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
99 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
100 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
, this, &InformationPanelContent::urlActivated
);
101 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
102 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
105 m_configureLabel
= new QLabel(i18nc("@label::textbox", "Select which data should be shown:"), this);
106 m_configureLabel
->setWordWrap(true);
107 m_configureLabel
->setVisible(false);
109 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
110 m_configureButtons
->setVisible(false);
111 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
112 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
113 m_configureButtons
->setVisible(false);
114 m_configureLabel
->setVisible(false);
115 Q_EMIT
configurationFinished();
117 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
118 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
119 m_configureButtons
->setVisible(false);
120 m_configureLabel
->setVisible(false);
121 Q_EMIT
configurationFinished();
124 m_metaDataArea
= new QScrollArea(parent
);
125 m_metaDataArea
->setWidget(m_metaDataWidget
);
126 m_metaDataArea
->setWidgetResizable(true);
127 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
129 QWidget
*viewport
= m_metaDataArea
->viewport();
130 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
131 viewport
->installEventFilter(this);
133 layout
->addWidget(m_preview
);
134 layout
->addWidget(m_phononWidget
);
135 layout
->addWidget(m_nameLabel
);
136 layout
->addWidget(new KSeparator());
137 layout
->addWidget(m_configureLabel
);
138 layout
->addWidget(m_metaDataArea
);
139 layout
->addWidget(m_configureButtons
);
141 grabGesture(Qt::TapAndHoldGesture
);
144 InformationPanelContent::~InformationPanelContent()
146 InformationPanelSettings::self()->save();
149 void InformationPanelContent::showItem(const KFileItem
&item
)
151 // compares item entries, comparing items only compares urls
152 if (m_item
.entry() != item
.entry()) {
154 m_preview
->stopAnimatedImage();
161 void InformationPanelContent::refreshPixmapView()
163 // If there is a preview job, kill it to prevent that we have jobs for
164 // multiple items running, and thus a race condition (bug 250787).
166 m_previewJob
->kill();
169 // try to get a preview pixmap from the item...
171 // Mark the currently shown preview as outdated. This is done
172 // with a small delay to prevent a flickering when the next preview
173 // can be shown within a short timeframe.
174 m_outdatedPreviewTimer
->start();
176 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
177 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
178 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(m_preview
->width(), m_preview
->height()), &plugins
);
179 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
180 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile() && !m_item
.isSlow());
181 m_previewJob
->setDevicePixelRatio(devicePixelRatioF());
182 if (m_previewJob
->uiDelegate()) {
183 KJobWidgets::setWindow(m_previewJob
, this);
186 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
, this, &InformationPanelContent::showPreview
);
187 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
, this, &InformationPanelContent::showIcon
);
190 void InformationPanelContent::refreshPreview()
192 // If there is a preview job, kill it to prevent that we have jobs for
193 // multiple items running, and thus a race condition (bug 250787).
195 m_previewJob
->kill();
198 m_preview
->setCursor(Qt::ArrowCursor
);
199 setNameLabelText(m_item
.text());
200 if (InformationPanelSettings::previewsShown()) {
201 const QUrl itemUrl
= m_item
.url();
202 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
205 m_phononWidget
->hide();
207 // in the case of a search-URL the URL is not readable for humans
208 // (at least not useful to show in the Information Panel)
209 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width()));
213 const QString mimeType
= m_item
.mimetype();
214 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
215 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
216 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
219 // change the cursor of the preview
220 m_preview
->setCursor(Qt::PointingHandCursor
);
221 m_preview
->installEventFilter(m_phononWidget
);
222 m_phononWidget
->show();
224 // if the video is playing, has been paused or stopped
225 // we don't need to update the preview/phonon widget states
226 // unless the previewed file has changed,
227 // or the setting previewshown has changed
228 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&& m_phononWidget
->state() != Phonon::State::PausedState
229 && m_phononWidget
->state() != Phonon::State::StoppedState
)
230 || m_item
.targetUrl() != m_phononWidget
->url() || (!m_preview
->isVisible() && !m_phononWidget
->isVisible())) {
231 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
232 // hides the preview now to avoid flickering when the autoplay video starts
235 // the video won't play before the preview is displayed
239 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
240 adjustWidgetSizes(parentWidget()->width());
243 if (isAnimatedImage
) {
244 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
246 // When we don't need it, hide the phonon widget first to avoid flickering
247 m_phononWidget
->hide();
249 m_preview
->removeEventFilter(m_phononWidget
);
250 m_phononWidget
->clearUrl();
254 m_preview
->stopAnimatedImage();
256 m_phononWidget
->hide();
260 void InformationPanelContent::configureShownProperties()
262 m_configureLabel
->setVisible(true);
263 m_configureButtons
->setVisible(true);
264 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
267 void InformationPanelContent::refreshMetaData()
269 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
270 m_metaDataWidget
->show();
271 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
274 void InformationPanelContent::showItems(const KFileItemList
&items
)
276 // If there is a preview job, kill it to prevent that we have jobs for
277 // multiple items running, and thus a race condition (bug 250787).
279 m_previewJob
->kill();
282 m_preview
->stopAnimatedImage();
284 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width()));
285 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
287 m_metaDataWidget
->setItems(items
);
289 m_phononWidget
->hide();
291 m_item
= KFileItem();
294 bool InformationPanelContent::eventFilter(QObject
*obj
, QEvent
*event
)
296 switch (event
->type()) {
297 case QEvent::Resize
: {
298 QResizeEvent
*resizeEvent
= static_cast<QResizeEvent
*>(event
);
299 if (obj
== m_metaDataArea
->viewport()) {
300 // The size of the meta text area has changed. Adjust the fixed
301 // width in a way that no horizontal scrollbar needs to be shown.
302 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
303 } else if (obj
== parent()) {
304 adjustWidgetSizes(resizeEvent
->size().width());
310 adjustWidgetSizes(parentWidget()->width());
313 case QEvent::FontChange
:
314 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
321 return QWidget::eventFilter(obj
, event
);
324 bool InformationPanelContent::event(QEvent
*event
)
326 if (event
->type() == QEvent::Gesture
) {
327 gestureEvent(static_cast<QGestureEvent
*>(event
));
330 return QWidget::event(event
);
333 bool InformationPanelContent::gestureEvent(QGestureEvent
*event
)
339 QTapAndHoldGesture
*tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
342 if (tap
->state() == Qt::GestureFinished
) {
343 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
351 void InformationPanelContent::showIcon(const KFileItem
&item
)
353 m_outdatedPreviewTimer
->stop();
354 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->size(), devicePixelRatioF());
355 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
356 m_preview
->setPixmap(pixmap
);
359 void InformationPanelContent::showPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
361 m_outdatedPreviewTimer
->stop();
364 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
367 // adds a play arrow overlay
369 auto maxDim
= qMax(p
.width(), p
.height());
370 auto arrowSize
= qMax(PLAY_ARROW_SIZE
, maxDim
/ 8);
372 // compute relative pixel positions
373 const int zeroX
= static_cast<int>((p
.width() / 2 - arrowSize
/ 2) / pixmap
.devicePixelRatio());
374 const int zeroY
= static_cast<int>((p
.height() / 2 - arrowSize
/ 2) / pixmap
.devicePixelRatio());
377 arrow
<< QPoint(zeroX
, zeroY
);
378 arrow
<< QPoint(zeroX
, zeroY
+ arrowSize
);
379 arrow
<< QPoint(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2);
382 path
.addPolygon(arrow
);
384 QLinearGradient
gradient(QPointF(zeroX
, zeroY
+ arrowSize
/ 2), QPointF(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2));
386 QColor whiteColor
= Qt::white
;
387 QColor blackColor
= Qt::black
;
388 gradient
.setColorAt(0, whiteColor
);
389 gradient
.setColorAt(1, blackColor
);
391 QBrush
brush(gradient
);
393 QPainter
painter(&p
);
395 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
398 painter
.setRenderHint(QPainter::Antialiasing
);
399 painter
.drawPolygon(arrow
);
400 painter
.fillPath(path
, brush
);
403 m_preview
->setPixmap(p
);
406 void InformationPanelContent::markOutdatedPreview()
408 if (m_item
.isDir()) {
409 // directory preview can be long
410 // but since we always have icons to display
411 // use it until the preview is done
414 #if KICONTHEMES_VERSION >= QT_VERSION_CHECK(6, 5, 0)
415 QPixmap disabledPixmap
= m_preview
->pixmap();
416 KIconEffect::toDisabled(disabledPixmap
);
418 QImage img
= m_preview
->pixmap().toImage();
419 KIconEffect::toGray(img
, 1);
420 KIconEffect::semiTransparent(img
);
421 QPixmap disabledPixmap
= QPixmap::fromImage(img
);
423 m_preview
->setPixmap(disabledPixmap
);
427 KFileItemList
InformationPanelContent::items()
429 return m_metaDataWidget
->items();
432 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
434 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
435 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
436 // in case the information panel has been resized when the preview was not displayed
437 // we need to refresh its content
442 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
)
444 m_phononWidget
->setAutoPlay(autoPlay
);
447 void InformationPanelContent::setNameLabelText(const QString
&text
)
449 QTextOption textOption
;
450 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
452 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
454 QTextLayout
textLayout(processedText
);
455 textLayout
.setFont(m_nameLabel
->font());
456 textLayout
.setTextOption(textOption
);
459 wrappedText
.reserve(processedText
.length());
461 // wrap the text to fit into the width of m_nameLabel
462 textLayout
.beginLayout();
463 QTextLine line
= textLayout
.createLine();
464 while (line
.isValid()) {
465 line
.setLineWidth(m_nameLabel
->width());
466 wrappedText
+= QStringView(processedText
).mid(line
.textStart(), line
.textLength());
468 line
= textLayout
.createLine();
469 if (line
.isValid()) {
470 wrappedText
+= QChar::LineSeparator
;
473 textLayout
.endLayout();
475 m_nameLabel
->setText(wrappedText
);
478 void InformationPanelContent::adjustWidgetSizes(int width
)
480 // If the text inside the name label or the info label cannot
481 // get wrapped, then the maximum width of the label is increased
482 // so that the width of the information panel gets increased.
483 // To prevent this, the maximum width is adjusted to
484 // the current width of the panel.
485 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
486 m_nameLabel
->setMaximumWidth(maxWidth
);
488 // The metadata widget also contains a text widget which may return
489 // a large preferred width.
490 m_metaDataWidget
->setMaximumWidth(maxWidth
);
492 // try to increase the preview as large as possible
493 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
495 if (m_phononWidget
->isVisible()) {
496 // assure that the size of the video player is the same as the preview size
497 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
501 #include "moc_informationpanelcontent.cpp"