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 <KConfigGroup>
12 #include <KIconEffect>
13 #include <KIconLoader>
14 #include <KJobWidgets>
15 #include <KLocalizedString>
17 #include <KSharedConfig>
18 #include <KStringHandler>
19 #include <QPainterPath>
22 #include <QTextDocument>
24 #include <Baloo/FileMetaDataWidget>
26 #include <Phonon/BackendCapabilities>
27 #include <Phonon/MediaObject>
30 #include <QDialogButtonBox>
31 #include <QScrollArea>
32 #include <QTextLayout>
34 #include <QVBoxLayout>
39 #include <QLinearGradient>
43 #include "dolphin_informationpanelsettings.h"
44 #include "phononwidget.h"
45 #include "pixmapviewer.h"
47 const int PLAY_ARROW_SIZE
= 24;
48 const int PLAY_ARROW_BORDER_SIZE
= 2;
50 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
53 m_previewJob(nullptr),
54 m_outdatedPreviewTimer(nullptr),
56 m_phononWidget(nullptr),
58 m_metaDataWidget(nullptr),
59 m_metaDataArea(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
);
97 m_nameLabel
->setTextInteractionFlags(Qt::TextSelectableByMouse
);
99 const bool previewsShown
= InformationPanelSettings::previewsShown();
100 m_preview
->setVisible(previewsShown
);
102 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
103 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
104 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
105 this, &InformationPanelContent::urlActivated
);
106 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
107 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
110 m_configureLabel
= new QLabel(i18nc("@label::textbox",
111 "Select which data should be shown:"), this);
112 m_configureLabel
->setWordWrap(true);
113 m_configureLabel
->setVisible(false);
115 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
116 m_configureButtons
->setVisible(false);
117 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
118 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
119 m_configureButtons
->setVisible(false);
120 m_configureLabel
->setVisible(false);
121 Q_EMIT
configurationFinished();
124 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
125 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
126 m_configureButtons
->setVisible(false);
127 m_configureLabel
->setVisible(false);
128 Q_EMIT
configurationFinished();
132 m_metaDataArea
= new QScrollArea(parent
);
133 m_metaDataArea
->setWidget(m_metaDataWidget
);
134 m_metaDataArea
->setWidgetResizable(true);
135 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
137 QWidget
* viewport
= m_metaDataArea
->viewport();
138 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
139 viewport
->installEventFilter(this);
141 layout
->addWidget(m_preview
);
142 layout
->addWidget(m_phononWidget
);
143 layout
->addWidget(m_nameLabel
);
144 layout
->addWidget(new KSeparator());
145 layout
->addWidget(m_configureLabel
);
146 layout
->addWidget(m_metaDataArea
);
147 layout
->addWidget(m_configureButtons
);
149 grabGesture(Qt::TapAndHoldGesture
);
152 InformationPanelContent::~InformationPanelContent()
154 InformationPanelSettings::self()->save();
157 void InformationPanelContent::showItem(const KFileItem
& item
)
159 // compares item entries, comparing items only compares urls
160 if (m_item
.entry() != item
.entry()) {
162 m_preview
->stopAnimatedImage();
169 void InformationPanelContent::refreshPixmapView()
171 // If there is a preview job, kill it to prevent that we have jobs for
172 // multiple items running, and thus a race condition (bug 250787).
174 m_previewJob
->kill();
177 // try to get a preview pixmap from the item...
179 // Mark the currently shown preview as outdated. This is done
180 // with a small delay to prevent a flickering when the next preview
181 // can be shown within a short timeframe.
182 m_outdatedPreviewTimer
->start();
184 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
185 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
186 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
187 QSize(m_preview
->width(), m_preview
->height()),
189 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
190 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile() && !m_item
.isSlow());
191 if (m_previewJob
->uiDelegate()) {
192 KJobWidgets::setWindow(m_previewJob
, this);
195 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
196 this, &InformationPanelContent::showPreview
);
197 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
198 this, &InformationPanelContent::showIcon
);
201 void InformationPanelContent::refreshPreview()
203 // If there is a preview job, kill it to prevent that we have jobs for
204 // multiple items running, and thus a race condition (bug 250787).
206 m_previewJob
->kill();
209 m_preview
->setCursor(Qt::ArrowCursor
);
210 setNameLabelText(m_item
.text());
211 if (InformationPanelSettings::previewsShown()) {
213 const QUrl itemUrl
= m_item
.url();
214 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
217 m_phononWidget
->hide();
219 // in the case of a search-URL the URL is not readable for humans
220 // (at least not useful to show in the Information Panel)
221 m_preview
->setPixmap(
222 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width())
228 const QString mimeType
= m_item
.mimetype();
229 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
230 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
231 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
234 // change the cursor of the preview
235 m_preview
->setCursor(Qt::PointingHandCursor
);
236 m_preview
->installEventFilter(m_phononWidget
);
237 m_phononWidget
->show();
239 // if the video is playing, has been paused or stopped
240 // we don't need to update the preview/phonon widget states
241 // unless the previewed file has changed,
242 // or the setting previewshown has changed
243 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
244 m_phononWidget
->state() != Phonon::State::PausedState
&&
245 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
246 m_item
.targetUrl() != m_phononWidget
->url() ||
247 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
249 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
250 // hides the preview now to avoid flickering when the autoplay video starts
253 // the video won't play before the preview is displayed
257 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
258 adjustWidgetSizes(parentWidget()->width());
261 if (isAnimatedImage
) {
262 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
264 // When we don't need it, hide the phonon widget first to avoid flickering
265 m_phononWidget
->hide();
267 m_preview
->removeEventFilter(m_phononWidget
);
268 m_phononWidget
->clearUrl();
272 m_preview
->stopAnimatedImage();
274 m_phononWidget
->hide();
278 void InformationPanelContent::configureShownProperties()
280 m_configureLabel
->setVisible(true);
281 m_configureButtons
->setVisible(true);
282 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
285 void InformationPanelContent::refreshMetaData()
287 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
288 m_metaDataWidget
->show();
289 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
292 void InformationPanelContent::showItems(const KFileItemList
& items
)
294 // If there is a preview job, kill it to prevent that we have jobs for
295 // multiple items running, and thus a race condition (bug 250787).
297 m_previewJob
->kill();
300 m_preview
->stopAnimatedImage();
302 m_preview
->setPixmap(
303 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
305 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
307 m_metaDataWidget
->setItems(items
);
309 m_phononWidget
->hide();
311 m_item
= KFileItem();
314 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
316 switch (event
->type()) {
317 case QEvent::Resize
: {
318 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
319 if (obj
== m_metaDataArea
->viewport()) {
320 // The size of the meta text area has changed. Adjust the fixed
321 // width in a way that no horizontal scrollbar needs to be shown.
322 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
323 } else if (obj
== parent()) {
324 adjustWidgetSizes(resizeEvent
->size().width());
330 adjustWidgetSizes(parentWidget()->width());
333 case QEvent::FontChange
:
334 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
341 return QWidget::eventFilter(obj
, event
);
344 bool InformationPanelContent::event(QEvent
* event
)
346 if (event
->type() == QEvent::Gesture
) {
347 gestureEvent(static_cast<QGestureEvent
*>(event
));
350 return QWidget::event(event
);
353 bool InformationPanelContent::gestureEvent(QGestureEvent
* event
)
359 QTapAndHoldGesture
* tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
362 if (tap
->state() == Qt::GestureFinished
) {
363 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
371 void InformationPanelContent::showIcon(const KFileItem
& item
)
373 m_outdatedPreviewTimer
->stop();
374 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->height(), m_preview
->width());
375 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
376 m_preview
->setPixmap(pixmap
);
379 void InformationPanelContent::showPreview(const KFileItem
& item
,
380 const QPixmap
& pixmap
)
382 m_outdatedPreviewTimer
->stop();
385 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
388 // adds a play arrow overlay
390 auto maxDim
= qMax(p
.width(), p
.height());
391 auto arrowSize
= qMax(PLAY_ARROW_SIZE
, maxDim
/ 8);
393 // compute relative pixel positions
394 const int zeroX
= static_cast<int>((p
.width() / 2 - arrowSize
/ 2) / pixmap
.devicePixelRatio());
395 const int zeroY
= static_cast<int>((p
.height() / 2 - arrowSize
/ 2) / pixmap
.devicePixelRatio());
398 arrow
<< QPoint(zeroX
, zeroY
);
399 arrow
<< QPoint(zeroX
, zeroY
+ arrowSize
);
400 arrow
<< QPoint(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2);
403 path
.addPolygon(arrow
);
405 QLinearGradient
gradient(QPointF(zeroX
, zeroY
+ arrowSize
/ 2),
406 QPointF(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2));
408 QColor whiteColor
= Qt::white
;
409 QColor blackColor
= Qt::black
;
410 gradient
.setColorAt(0, whiteColor
);
411 gradient
.setColorAt(1, blackColor
);
413 QBrush
brush(gradient
);
415 QPainter
painter(&p
);
417 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
420 painter
.setRenderHint(QPainter::Antialiasing
);
421 painter
.drawPolygon(arrow
);
422 painter
.fillPath(path
, brush
);
425 m_preview
->setPixmap(p
);
428 void InformationPanelContent::markOutdatedPreview()
430 if (m_item
.isDir()) {
431 // directory preview can be long
432 // but since we always have icons to display
433 // use it until the preview is done
436 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
437 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
438 KIconLoader::Desktop
,
439 KIconLoader::DisabledState
);
440 m_preview
->setPixmap(disabledPixmap
);
444 KFileItemList
InformationPanelContent::items()
446 return m_metaDataWidget
->items();
449 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
451 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
452 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
453 // in case the information panel has been resized when the preview was not displayed
454 // we need to refresh its content
459 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
460 m_phononWidget
->setAutoPlay(autoPlay
);
463 void InformationPanelContent::setNameLabelText(const QString
& text
)
465 QTextOption textOption
;
466 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
468 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
470 QTextLayout
textLayout(processedText
);
471 textLayout
.setFont(m_nameLabel
->font());
472 textLayout
.setTextOption(textOption
);
475 wrappedText
.reserve(processedText
.length());
477 // wrap the text to fit into the width of m_nameLabel
478 textLayout
.beginLayout();
479 QTextLine line
= textLayout
.createLine();
480 while (line
.isValid()) {
481 line
.setLineWidth(m_nameLabel
->width());
482 wrappedText
+= QStringView(processedText
).mid(line
.textStart(), line
.textLength());
484 line
= textLayout
.createLine();
485 if (line
.isValid()) {
486 wrappedText
+= QChar::LineSeparator
;
489 textLayout
.endLayout();
491 m_nameLabel
->setText(wrappedText
);
494 void InformationPanelContent::adjustWidgetSizes(int width
)
496 // If the text inside the name label or the info label cannot
497 // get wrapped, then the maximum width of the label is increased
498 // so that the width of the information panel gets increased.
499 // To prevent this, the maximum width is adjusted to
500 // the current width of the panel.
501 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
502 m_nameLabel
->setMaximumWidth(maxWidth
);
504 // The metadata widget also contains a text widget which may return
505 // a large preferred width.
506 m_metaDataWidget
->setMaximumWidth(maxWidth
);
508 // try to increase the preview as large as possible
509 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
511 if (m_phononWidget
->isVisible()) {
512 // assure that the size of the video player is the same as the preview size
513 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));