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 <QDialogButtonBox>
30 #include <QLinearGradient>
33 #include <QScrollArea>
35 #include <QTextLayout>
37 #include <QVBoxLayout>
39 #include "dolphin_informationpanelsettings.h"
40 #include "mediawidget.h"
41 #include "pixmapviewer.h"
43 const int PLAY_ARROW_SIZE
= 24;
44 const int PLAY_ARROW_BORDER_SIZE
= 2;
46 InformationPanelContent::InformationPanelContent(QWidget
*parent
)
49 , m_previewJob(nullptr)
50 , m_outdatedPreviewTimer(nullptr)
52 , m_mediaWidget(nullptr)
53 , m_nameLabel(nullptr)
54 , m_metaDataWidget(nullptr)
55 , m_metaDataArea(nullptr)
58 parent
->installEventFilter(this);
60 // Initialize timer for disabling an outdated preview with a small
61 // delay. This prevents flickering if the new preview can be generated
62 // within a very small timeframe.
63 m_outdatedPreviewTimer
= new QTimer(this);
64 m_outdatedPreviewTimer
->setInterval(100);
65 m_outdatedPreviewTimer
->setSingleShot(true);
66 connect(m_outdatedPreviewTimer
, &QTimer::timeout
, this, &InformationPanelContent::markOutdatedPreview
);
68 QVBoxLayout
*layout
= new QVBoxLayout(this);
71 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
73 m_preview
= new PixmapViewer(parent
);
74 m_preview
->setMinimumWidth(minPreviewWidth
);
75 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
77 m_mediaWidget
= new MediaWidget(parent
);
78 m_mediaWidget
->hide();
79 m_mediaWidget
->setMinimumWidth(minPreviewWidth
);
80 m_mediaWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
81 connect(m_mediaWidget
, &MediaWidget::hasVideoChanged
, this, &InformationPanelContent::slotHasVideoChanged
);
84 m_nameLabel
= new QLabel(parent
);
85 QFont font
= m_nameLabel
->font();
87 m_nameLabel
->setFont(font
);
88 m_nameLabel
->setTextFormat(Qt::PlainText
);
89 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
90 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
91 m_nameLabel
->setTextInteractionFlags(Qt::TextSelectableByMouse
);
93 const bool previewsShown
= InformationPanelSettings::previewsShown();
94 m_preview
->setVisible(previewsShown
);
96 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
97 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
98 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
, this, &InformationPanelContent::urlActivated
);
99 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
100 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
103 m_configureLabel
= new QLabel(i18nc("@label::textbox", "Select which data should be shown:"), this);
104 m_configureLabel
->setWordWrap(true);
105 m_configureLabel
->setVisible(false);
107 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
108 m_configureButtons
->setVisible(false);
109 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
110 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
111 m_configureButtons
->setVisible(false);
112 m_configureLabel
->setVisible(false);
113 Q_EMIT
configurationFinished();
115 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
116 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
117 m_configureButtons
->setVisible(false);
118 m_configureLabel
->setVisible(false);
119 Q_EMIT
configurationFinished();
122 m_metaDataArea
= new QScrollArea(parent
);
123 m_metaDataArea
->setWidget(m_metaDataWidget
);
124 m_metaDataArea
->setWidgetResizable(true);
125 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
127 QWidget
*viewport
= m_metaDataArea
->viewport();
128 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
129 viewport
->installEventFilter(this);
131 layout
->addWidget(m_preview
);
132 layout
->addWidget(m_mediaWidget
);
133 layout
->addWidget(m_nameLabel
);
134 layout
->addWidget(new KSeparator());
135 layout
->addWidget(m_configureLabel
);
136 layout
->addWidget(m_metaDataArea
);
137 layout
->addWidget(m_configureButtons
);
139 grabGesture(Qt::TapAndHoldGesture
);
142 InformationPanelContent::~InformationPanelContent()
144 InformationPanelSettings::self()->save();
147 void InformationPanelContent::showItem(const KFileItem
&item
)
149 // compares item entries, comparing items only compares urls
150 if (m_item
.entry() != item
.entry()) {
152 m_preview
->stopAnimatedImage();
159 void InformationPanelContent::refreshPixmapView()
161 // If there is a preview job, kill it to prevent that we have jobs for
162 // multiple items running, and thus a race condition (bug 250787).
164 m_previewJob
->kill();
167 // try to get a preview pixmap from the item...
169 // Mark the currently shown preview as outdated. This is done
170 // with a small delay to prevent a flickering when the next preview
171 // can be shown within a short timeframe.
172 m_outdatedPreviewTimer
->start();
174 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
175 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
176 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
, QSize(m_preview
->width(), m_preview
->height()), &plugins
);
177 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
178 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile() && !m_item
.isSlow());
179 m_previewJob
->setDevicePixelRatio(devicePixelRatioF());
180 if (m_previewJob
->uiDelegate()) {
181 KJobWidgets::setWindow(m_previewJob
, this);
184 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
, this, &InformationPanelContent::showPreview
);
185 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
, this, &InformationPanelContent::showIcon
);
188 void InformationPanelContent::refreshPreview()
190 // If there is a preview job, kill it to prevent that we have jobs for
191 // multiple items running, and thus a race condition (bug 250787).
193 m_previewJob
->kill();
196 m_preview
->setCursor(Qt::ArrowCursor
);
197 setNameLabelText(m_item
.text());
198 if (InformationPanelSettings::previewsShown()) {
199 const QUrl itemUrl
= m_item
.url();
200 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
203 m_mediaWidget
->hide();
205 // in the case of a search-URL the URL is not readable for humans
206 // (at least not useful to show in the Information Panel)
207 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width()));
211 const QString mimeType
= m_item
.mimetype();
212 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
213 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
214 bool useMedia
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
217 // change the cursor of the preview
218 m_preview
->setCursor(Qt::PointingHandCursor
);
219 m_preview
->installEventFilter(m_mediaWidget
);
221 m_mediaWidget
->show();
223 // if the video is playing, has been paused or stopped
224 // we don't need to update the preview/media widget states
225 // unless the previewed file has changed,
226 // or the setting previewshown has changed
227 if ((m_mediaWidget
->state() != QMediaPlayer::PlayingState
&& m_mediaWidget
->state() != QMediaPlayer::PausedState
228 && m_mediaWidget
->state() != QMediaPlayer::StoppedState
)
229 || m_item
.targetUrl() != m_mediaWidget
->url() || (!m_preview
->isVisible() && !m_mediaWidget
->isVisible())) {
230 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
231 // hides the preview now to avoid flickering when the autoplay video starts
234 // the video won't play before the preview is displayed
238 m_mediaWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? MediaWidget::MediaKind::Video
: MediaWidget::MediaKind::Audio
);
239 adjustWidgetSizes(parentWidget()->width());
242 if (isAnimatedImage
) {
243 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
245 // When we don't need it, hide the media widget first to avoid flickering
246 m_mediaWidget
->hide();
248 m_preview
->removeEventFilter(m_mediaWidget
);
249 m_mediaWidget
->clearUrl();
253 m_preview
->stopAnimatedImage();
255 m_mediaWidget
->hide();
259 void InformationPanelContent::configureShownProperties()
261 m_configureLabel
->setVisible(true);
262 m_configureButtons
->setVisible(true);
263 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
266 void InformationPanelContent::refreshMetaData()
268 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
269 m_metaDataWidget
->show();
270 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
273 void InformationPanelContent::showItems(const KFileItemList
&items
)
275 // If there is a preview job, kill it to prevent that we have jobs for
276 // multiple items running, and thus a race condition (bug 250787).
278 m_previewJob
->kill();
281 m_preview
->stopAnimatedImage();
283 m_preview
->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width()));
284 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
286 m_metaDataWidget
->setItems(items
);
288 m_mediaWidget
->hide();
290 m_item
= KFileItem();
293 bool InformationPanelContent::eventFilter(QObject
*obj
, QEvent
*event
)
295 switch (event
->type()) {
296 case QEvent::Resize
: {
297 QResizeEvent
*resizeEvent
= static_cast<QResizeEvent
*>(event
);
298 if (obj
== m_metaDataArea
->viewport()) {
299 // The size of the meta text area has changed. Adjust the fixed
300 // width in a way that no horizontal scrollbar needs to be shown.
301 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
302 } else if (obj
== parent()) {
303 adjustWidgetSizes(resizeEvent
->size().width());
309 adjustWidgetSizes(parentWidget()->width());
312 case QEvent::FontChange
:
313 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
320 return QWidget::eventFilter(obj
, event
);
323 bool InformationPanelContent::event(QEvent
*event
)
325 if (event
->type() == QEvent::Gesture
) {
326 gestureEvent(static_cast<QGestureEvent
*>(event
));
329 return QWidget::event(event
);
332 bool InformationPanelContent::gestureEvent(QGestureEvent
*event
)
338 QTapAndHoldGesture
*tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
341 if (tap
->state() == Qt::GestureFinished
) {
342 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
350 void InformationPanelContent::showIcon(const KFileItem
&item
)
352 m_outdatedPreviewTimer
->stop();
353 QIcon icon
= QIcon::fromTheme(item
.iconName());
354 QPixmap pixmap
= KIconUtils::addOverlays(icon
, item
.overlays()).pixmap(m_preview
->size(), devicePixelRatioF());
355 pixmap
.setDevicePixelRatio(devicePixelRatioF());
356 m_preview
->setPixmap(pixmap
);
359 void InformationPanelContent::showPreview(const KFileItem
&item
, const QPixmap
&pixmap
)
361 m_outdatedPreviewTimer
->stop();
364 if (!item
.overlays().isEmpty()) {
365 // Avoid scaling the images that are smaller than the preview size, to be consistent when there is no overlays
366 if (pixmap
.height() < m_preview
->height() && pixmap
.width() < m_preview
->width()) {
367 p
= QPixmap(m_preview
->size() * devicePixelRatioF());
368 p
.fill(Qt::transparent
);
369 p
.setDevicePixelRatio(devicePixelRatioF());
371 QPainter
painter(&p
);
372 painter
.drawPixmap(QPointF
{m_preview
->width() / 2.0 - pixmap
.width() / pixmap
.devicePixelRatioF() / 2,
373 m_preview
->height() / 2.0 - pixmap
.height() / pixmap
.devicePixelRatioF() / 2}
377 p
= KIconUtils::addOverlays(p
, item
.overlays()).pixmap(m_preview
->size(), devicePixelRatioF());
378 p
.setDevicePixelRatio(devicePixelRatioF());
382 // adds a play arrow overlay
384 auto maxDim
= qMax(p
.width(), p
.height());
385 auto arrowSize
= qMax(PLAY_ARROW_SIZE
, maxDim
/ 8);
387 // compute relative pixel positions
388 const int zeroX
= static_cast<int>((p
.width() / 2 - arrowSize
/ 2) / p
.devicePixelRatio());
389 const int zeroY
= static_cast<int>((p
.height() / 2 - arrowSize
/ 2) / p
.devicePixelRatio());
392 arrow
<< QPoint(zeroX
, zeroY
);
393 arrow
<< QPoint(zeroX
, zeroY
+ arrowSize
);
394 arrow
<< QPoint(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2);
397 path
.addPolygon(arrow
);
399 QLinearGradient
gradient(QPointF(zeroX
, zeroY
+ arrowSize
/ 2), QPointF(zeroX
+ arrowSize
, zeroY
+ arrowSize
/ 2));
401 QColor whiteColor
= Qt::white
;
402 QColor blackColor
= Qt::black
;
403 gradient
.setColorAt(0, whiteColor
);
404 gradient
.setColorAt(1, blackColor
);
406 QBrush
brush(gradient
);
408 QPainter
painter(&p
);
410 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
413 painter
.setRenderHint(QPainter::Antialiasing
);
414 painter
.drawPolygon(arrow
);
415 painter
.fillPath(path
, brush
);
418 m_preview
->setPixmap(p
);
421 void InformationPanelContent::markOutdatedPreview()
423 if (m_item
.isDir()) {
424 // directory preview can be long
425 // but since we always have icons to display
426 // use it until the preview is done
429 QPixmap disabledPixmap
= m_preview
->pixmap();
430 KIconEffect::toDisabled(disabledPixmap
);
431 m_preview
->setPixmap(disabledPixmap
);
435 KFileItemList
InformationPanelContent::items()
437 return m_metaDataWidget
->items();
440 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
442 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
443 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
444 // in case the information panel has been resized when the preview was not displayed
445 // we need to refresh its content
450 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
)
452 m_mediaWidget
->setAutoPlay(autoPlay
);
455 void InformationPanelContent::setNameLabelText(const QString
&text
)
457 QTextOption textOption
;
458 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
460 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
462 QTextLayout
textLayout(processedText
);
463 textLayout
.setFont(m_nameLabel
->font());
464 textLayout
.setTextOption(textOption
);
467 wrappedText
.reserve(processedText
.length());
469 // wrap the text to fit into the width of m_nameLabel
470 textLayout
.beginLayout();
471 QTextLine line
= textLayout
.createLine();
472 while (line
.isValid()) {
473 line
.setLineWidth(m_nameLabel
->width());
474 wrappedText
+= QStringView(processedText
).mid(line
.textStart(), line
.textLength());
476 line
= textLayout
.createLine();
477 if (line
.isValid()) {
478 wrappedText
+= QChar::LineSeparator
;
481 textLayout
.endLayout();
483 m_nameLabel
->setText(wrappedText
);
486 void InformationPanelContent::adjustWidgetSizes(int width
)
488 // If the text inside the name label or the info label cannot
489 // get wrapped, then the maximum width of the label is increased
490 // so that the width of the information panel gets increased.
491 // To prevent this, the maximum width is adjusted to
492 // the current width of the panel.
493 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
494 m_nameLabel
->setMaximumWidth(maxWidth
);
496 // The metadata widget also contains a text widget which may return
497 // a large preferred width.
498 m_metaDataWidget
->setMaximumWidth(maxWidth
);
500 // try to increase the preview as large as possible
501 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
503 if (m_mediaWidget
->isVisible()) {
504 // assure that the size of the video player is the same as the preview size
505 m_mediaWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
509 #include "moc_informationpanelcontent.cpp"