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 <panels/places/placesitem.h>
27 #include <panels/places/placesitemmodel.h>
29 #include <Phonon/BackendCapabilities>
30 #include <Phonon/MediaObject>
33 #include <QDialogButtonBox>
34 #include <QScrollArea>
35 #include <QTextLayout>
37 #include <QVBoxLayout>
42 #include <QLinearGradient>
46 #include "dolphin_informationpanelsettings.h"
47 #include "phononwidget.h"
48 #include "pixmapviewer.h"
50 const int PLAY_ARROW_SIZE
= 24;
51 const int PLAY_ARROW_BORDER_SIZE
= 2;
53 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
56 m_previewJob(nullptr),
57 m_outdatedPreviewTimer(nullptr),
59 m_phononWidget(nullptr),
61 m_metaDataWidget(nullptr),
62 m_metaDataArea(nullptr),
63 m_placesItemModel(nullptr),
66 parent
->installEventFilter(this);
68 // Initialize timer for disabling an outdated preview with a small
69 // delay. This prevents flickering if the new preview can be generated
70 // within a very small timeframe.
71 m_outdatedPreviewTimer
= new QTimer(this);
72 m_outdatedPreviewTimer
->setInterval(100);
73 m_outdatedPreviewTimer
->setSingleShot(true);
74 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
75 this, &InformationPanelContent::markOutdatedPreview
);
77 QVBoxLayout
* layout
= new QVBoxLayout(this);
80 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
82 m_preview
= new PixmapViewer(parent
);
83 m_preview
->setMinimumWidth(minPreviewWidth
);
84 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
86 m_phononWidget
= new PhononWidget(parent
);
87 m_phononWidget
->hide();
88 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
89 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
90 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
91 this, &InformationPanelContent::slotHasVideoChanged
);
94 m_nameLabel
= new QLabel(parent
);
95 QFont font
= m_nameLabel
->font();
97 m_nameLabel
->setFont(font
);
98 m_nameLabel
->setTextFormat(Qt::PlainText
);
99 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
100 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
101 m_nameLabel
->setTextInteractionFlags(Qt::TextSelectableByMouse
);
103 const bool previewsShown
= InformationPanelSettings::previewsShown();
104 m_preview
->setVisible(previewsShown
);
106 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
107 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
108 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
109 this, &InformationPanelContent::urlActivated
);
110 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
111 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
114 m_configureLabel
= new QLabel(i18nc("@label::textbox",
115 "Select which data should be shown:"), this);
116 m_configureLabel
->setWordWrap(true);
117 m_configureLabel
->setVisible(false);
119 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
120 m_configureButtons
->setVisible(false);
121 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
122 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
123 m_configureButtons
->setVisible(false);
124 m_configureLabel
->setVisible(false);
125 Q_EMIT
configurationFinished();
128 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
129 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
130 m_configureButtons
->setVisible(false);
131 m_configureLabel
->setVisible(false);
132 Q_EMIT
configurationFinished();
136 m_metaDataArea
= new QScrollArea(parent
);
137 m_metaDataArea
->setWidget(m_metaDataWidget
);
138 m_metaDataArea
->setWidgetResizable(true);
139 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
141 QWidget
* viewport
= m_metaDataArea
->viewport();
142 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
143 viewport
->installEventFilter(this);
145 layout
->addWidget(m_preview
);
146 layout
->addWidget(m_phononWidget
);
147 layout
->addWidget(m_nameLabel
);
148 layout
->addWidget(new KSeparator());
149 layout
->addWidget(m_configureLabel
);
150 layout
->addWidget(m_metaDataArea
);
151 layout
->addWidget(m_configureButtons
);
153 grabGesture(Qt::TapAndHoldGesture
);
155 m_placesItemModel
= new PlacesItemModel(this);
158 InformationPanelContent::~InformationPanelContent()
160 InformationPanelSettings::self()->save();
163 void InformationPanelContent::showItem(const KFileItem
& item
)
165 // compares item entries, comparing items only compares urls
166 if (m_item
.entry() != item
.entry()) {
168 m_preview
->stopAnimatedImage();
175 void InformationPanelContent::refreshPixmapView()
177 // If there is a preview job, kill it to prevent that we have jobs for
178 // multiple items running, and thus a race condition (bug 250787).
180 m_previewJob
->kill();
183 // try to get a preview pixmap from the item...
185 // Mark the currently shown preview as outdated. This is done
186 // with a small delay to prevent a flickering when the next preview
187 // can be shown within a short timeframe.
188 m_outdatedPreviewTimer
->start();
190 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
191 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
192 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
193 QSize(m_preview
->width(), m_preview
->height()),
195 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
196 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
197 if (m_previewJob
->uiDelegate()) {
198 KJobWidgets::setWindow(m_previewJob
, this);
201 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
202 this, &InformationPanelContent::showPreview
);
203 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
204 this, &InformationPanelContent::showIcon
);
207 void InformationPanelContent::refreshPreview()
209 // If there is a preview job, kill it to prevent that we have jobs for
210 // multiple items running, and thus a race condition (bug 250787).
212 m_previewJob
->kill();
215 m_preview
->setCursor(Qt::ArrowCursor
);
216 setNameLabelText(m_item
.text());
217 if (InformationPanelSettings::previewsShown()) {
219 const QUrl itemUrl
= m_item
.url();
220 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
223 m_phononWidget
->hide();
225 // in the case of a search-URL the URL is not readable for humans
226 // (at least not useful to show in the Information Panel)
227 m_preview
->setPixmap(
228 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width())
234 const QString mimeType
= m_item
.mimetype();
235 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
236 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
237 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
240 // change the cursor of the preview
241 m_preview
->setCursor(Qt::PointingHandCursor
);
242 m_preview
->installEventFilter(m_phononWidget
);
243 m_phononWidget
->show();
245 // if the video is playing, has been paused or stopped
246 // we don't need to update the preview/phonon widget states
247 // unless the previewed file has changed,
248 // or the setting previewshown has changed
249 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
250 m_phononWidget
->state() != Phonon::State::PausedState
&&
251 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
252 m_item
.targetUrl() != m_phononWidget
->url() ||
253 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
255 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
256 // hides the preview now to avoid flickering when the autoplay video starts
259 // the video won't play before the preview is displayed
263 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
264 adjustWidgetSizes(parentWidget()->width());
267 if (isAnimatedImage
) {
268 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
270 // When we don't need it, hide the phonon widget first to avoid flickering
271 m_phononWidget
->hide();
273 m_preview
->removeEventFilter(m_phononWidget
);
274 m_phononWidget
->clearUrl();
278 m_preview
->stopAnimatedImage();
280 m_phononWidget
->hide();
284 void InformationPanelContent::configureShownProperties()
286 m_configureLabel
->setVisible(true);
287 m_configureButtons
->setVisible(true);
288 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
291 void InformationPanelContent::refreshMetaData()
293 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
294 m_metaDataWidget
->show();
295 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
298 void InformationPanelContent::showItems(const KFileItemList
& items
)
300 // If there is a preview job, kill it to prevent that we have jobs for
301 // multiple items running, and thus a race condition (bug 250787).
303 m_previewJob
->kill();
306 m_preview
->stopAnimatedImage();
308 m_preview
->setPixmap(
309 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
311 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
313 m_metaDataWidget
->setItems(items
);
315 m_phononWidget
->hide();
317 m_item
= KFileItem();
320 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
322 switch (event
->type()) {
323 case QEvent::Resize
: {
324 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
325 if (obj
== m_metaDataArea
->viewport()) {
326 // The size of the meta text area has changed. Adjust the fixed
327 // width in a way that no horizontal scrollbar needs to be shown.
328 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
329 } else if (obj
== parent()) {
330 adjustWidgetSizes(resizeEvent
->size().width());
336 adjustWidgetSizes(parentWidget()->width());
339 case QEvent::FontChange
:
340 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
347 return QWidget::eventFilter(obj
, event
);
350 bool InformationPanelContent::event(QEvent
* event
)
352 if (event
->type() == QEvent::Gesture
) {
353 gestureEvent(static_cast<QGestureEvent
*>(event
));
356 return QWidget::event(event
);
359 bool InformationPanelContent::gestureEvent(QGestureEvent
* event
)
365 QTapAndHoldGesture
* tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
368 if (tap
->state() == Qt::GestureFinished
) {
369 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
377 void InformationPanelContent::showIcon(const KFileItem
& item
)
379 m_outdatedPreviewTimer
->stop();
380 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->height(), m_preview
->width());
381 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
382 m_preview
->setPixmap(pixmap
);
385 void InformationPanelContent::showPreview(const KFileItem
& item
,
386 const QPixmap
& pixmap
)
388 m_outdatedPreviewTimer
->stop();
391 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
396 // compute relative pixel positions
397 const int zeroX
= static_cast<int>((p
.width() / 2 - PLAY_ARROW_SIZE
/ 2) / pixmap
.devicePixelRatio());
398 const int zeroY
= static_cast<int>((p
.height() / 2 - PLAY_ARROW_SIZE
/ 2) / pixmap
.devicePixelRatio());
401 arrow
<< QPoint(zeroX
, zeroY
);
402 arrow
<< QPoint(zeroX
, zeroY
+ PLAY_ARROW_SIZE
);
403 arrow
<< QPoint(zeroX
+ PLAY_ARROW_SIZE
, zeroY
+ PLAY_ARROW_SIZE
/ 2);
406 path
.addPolygon(arrow
);
408 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
409 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
411 QColor whiteColor
= Qt::white
;
412 QColor blackColor
= Qt::black
;
413 gradient
.setColorAt(0, whiteColor
);
414 gradient
.setColorAt(1, blackColor
);
416 QBrush
brush(gradient
);
418 QPainter
painter(&p
);
420 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
423 painter
.setRenderHint(QPainter::Antialiasing
);
424 painter
.drawPolygon(arrow
);
425 painter
.fillPath(path
, brush
);
428 m_preview
->setPixmap(p
);
431 void InformationPanelContent::markOutdatedPreview()
433 if (m_item
.isDir()) {
434 // directory preview can be long
435 // but since we always have icons to display
436 // use it until the preview is done
439 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
440 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
441 KIconLoader::Desktop
,
442 KIconLoader::DisabledState
);
443 m_preview
->setPixmap(disabledPixmap
);
447 KFileItemList
InformationPanelContent::items()
449 return m_metaDataWidget
->items();
452 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
454 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
455 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
456 // in case the information panel has been resized when the preview was not displayed
457 // we need to refresh its content
462 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
463 m_phononWidget
->setAutoPlay(autoPlay
);
466 void InformationPanelContent::setNameLabelText(const QString
& text
)
468 QTextOption textOption
;
469 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
471 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
473 QTextLayout
textLayout(processedText
);
474 textLayout
.setFont(m_nameLabel
->font());
475 textLayout
.setTextOption(textOption
);
478 wrappedText
.reserve(processedText
.length());
480 // wrap the text to fit into the width of m_nameLabel
481 textLayout
.beginLayout();
482 QTextLine line
= textLayout
.createLine();
483 while (line
.isValid()) {
484 line
.setLineWidth(m_nameLabel
->width());
485 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
487 line
= textLayout
.createLine();
488 if (line
.isValid()) {
489 wrappedText
+= QChar::LineSeparator
;
492 textLayout
.endLayout();
494 m_nameLabel
->setText(wrappedText
);
497 void InformationPanelContent::adjustWidgetSizes(int width
)
499 // If the text inside the name label or the info label cannot
500 // get wrapped, then the maximum width of the label is increased
501 // so that the width of the information panel gets increased.
502 // To prevent this, the maximum width is adjusted to
503 // the current width of the panel.
504 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
505 m_nameLabel
->setMaximumWidth(maxWidth
);
507 // The metadata widget also contains a text widget which may return
508 // a large preferred width.
509 m_metaDataWidget
->setMaximumWidth(maxWidth
);
511 // try to increase the preview as large as possible
512 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
514 if (m_phononWidget
->isVisible()) {
515 // assure that the size of the video player is the same as the preview size
516 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));