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 <KIconEffect>
12 #include <KIconLoader>
13 #include <KJobWidgets>
14 #include <KLocalizedString>
16 #include <KStringHandler>
17 #include <QPainterPath>
20 #include <QTextDocument>
22 #include <Baloo/FileMetaDataWidget>
24 #include <panels/places/placesitem.h>
25 #include <panels/places/placesitemmodel.h>
27 #include <Phonon/BackendCapabilities>
28 #include <Phonon/MediaObject>
31 #include <QDialogButtonBox>
32 #include <QScrollArea>
33 #include <QTextLayout>
35 #include <QVBoxLayout>
40 #include <QLinearGradient>
44 #include "dolphin_informationpanelsettings.h"
45 #include "phononwidget.h"
46 #include "pixmapviewer.h"
48 const int PLAY_ARROW_SIZE
= 24;
49 const int PLAY_ARROW_BORDER_SIZE
= 2;
51 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
54 m_previewJob(nullptr),
55 m_outdatedPreviewTimer(nullptr),
57 m_phononWidget(nullptr),
59 m_metaDataWidget(nullptr),
60 m_metaDataArea(nullptr),
61 m_placesItemModel(nullptr),
64 parent
->installEventFilter(this);
66 // Initialize timer for disabling an outdated preview with a small
67 // delay. This prevents flickering if the new preview can be generated
68 // within a very small timeframe.
69 m_outdatedPreviewTimer
= new QTimer(this);
70 m_outdatedPreviewTimer
->setInterval(100);
71 m_outdatedPreviewTimer
->setSingleShot(true);
72 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
73 this, &InformationPanelContent::markOutdatedPreview
);
75 QVBoxLayout
* layout
= new QVBoxLayout(this);
78 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
80 m_preview
= new PixmapViewer(parent
);
81 m_preview
->setMinimumWidth(minPreviewWidth
);
82 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
84 m_phononWidget
= new PhononWidget(parent
);
85 m_phononWidget
->hide();
86 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
87 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
88 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
89 this, &InformationPanelContent::slotHasVideoChanged
);
92 m_nameLabel
= new QLabel(parent
);
93 QFont font
= m_nameLabel
->font();
95 m_nameLabel
->setFont(font
);
96 m_nameLabel
->setTextFormat(Qt::PlainText
);
97 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
98 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
100 const bool previewsShown
= InformationPanelSettings::previewsShown();
101 m_preview
->setVisible(previewsShown
);
103 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
104 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
105 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
106 this, &InformationPanelContent::urlActivated
);
107 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
108 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
111 m_configureLabel
= new QLabel(i18nc("@label::textbox",
112 "Select which data should be shown:"), this);
113 m_configureLabel
->setWordWrap(true);
114 m_configureLabel
->setVisible(false);
116 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
117 m_configureButtons
->setVisible(false);
118 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
119 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
120 m_configureButtons
->setVisible(false);
121 m_configureLabel
->setVisible(false);
122 emit
configurationFinished();
125 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
126 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
127 m_configureButtons
->setVisible(false);
128 m_configureLabel
->setVisible(false);
129 emit
configurationFinished();
133 m_metaDataArea
= new QScrollArea(parent
);
134 m_metaDataArea
->setWidget(m_metaDataWidget
);
135 m_metaDataArea
->setWidgetResizable(true);
136 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
138 QWidget
* viewport
= m_metaDataArea
->viewport();
139 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
140 viewport
->installEventFilter(this);
142 layout
->addWidget(m_preview
);
143 layout
->addWidget(m_phononWidget
);
144 layout
->addWidget(m_nameLabel
);
145 layout
->addWidget(new KSeparator());
146 layout
->addWidget(m_configureLabel
);
147 layout
->addWidget(m_metaDataArea
);
148 layout
->addWidget(m_configureButtons
);
150 grabGesture(Qt::TapAndHoldGesture
);
152 m_placesItemModel
= new PlacesItemModel(this);
155 InformationPanelContent::~InformationPanelContent()
157 InformationPanelSettings::self()->save();
160 void InformationPanelContent::showItem(const KFileItem
& item
)
162 // compares item entries, comparing items only compares urls
163 if (m_item
.entry() != item
.entry()) {
165 m_preview
->stopAnimatedImage();
172 void InformationPanelContent::refreshPixmapView()
174 // If there is a preview job, kill it to prevent that we have jobs for
175 // multiple items running, and thus a race condition (bug 250787).
177 m_previewJob
->kill();
180 // try to get a preview pixmap from the item...
182 // Mark the currently shown preview as outdated. This is done
183 // with a small delay to prevent a flickering when the next preview
184 // can be shown within a short timeframe.
185 m_outdatedPreviewTimer
->start();
187 QStringList plugins
= KIO::PreviewJob::availablePlugins();
188 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
189 QSize(m_preview
->width(), m_preview
->height()),
191 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
192 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
193 if (m_previewJob
->uiDelegate()) {
194 KJobWidgets::setWindow(m_previewJob
, this);
197 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
198 this, &InformationPanelContent::showPreview
);
199 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
200 this, &InformationPanelContent::showIcon
);
203 void InformationPanelContent::refreshPreview()
205 // If there is a preview job, kill it to prevent that we have jobs for
206 // multiple items running, and thus a race condition (bug 250787).
208 m_previewJob
->kill();
211 m_preview
->setCursor(Qt::ArrowCursor
);
212 setNameLabelText(m_item
.text());
213 if (InformationPanelSettings::previewsShown()) {
215 const QUrl itemUrl
= m_item
.url();
216 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
219 m_phononWidget
->hide();
221 // in the case of a search-URL the URL is not readable for humans
222 // (at least not useful to show in the Information Panel)
223 m_preview
->setPixmap(
224 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width())
230 const QString mimeType
= m_item
.mimetype();
231 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
232 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
233 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
236 // change the cursor of the preview
237 m_preview
->setCursor(Qt::PointingHandCursor
);
238 m_preview
->installEventFilter(m_phononWidget
);
239 m_phononWidget
->show();
241 // if the video is playing, has been paused or stopped
242 // we don't need to update the preview/phonon widget states
243 // unless the previewed file has changed,
244 // or the setting previewshown has changed
245 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
246 m_phononWidget
->state() != Phonon::State::PausedState
&&
247 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
248 m_item
.targetUrl() != m_phononWidget
->url() ||
249 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
251 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
252 // hides the preview now to avoid flickering when the autoplay video starts
255 // the video won't play before the preview is displayed
259 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
260 adjustWidgetSizes(parentWidget()->width());
263 if (isAnimatedImage
) {
264 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
266 // When we don't need it, hide the phonon widget first to avoid flickering
267 m_phononWidget
->hide();
269 m_preview
->removeEventFilter(m_phononWidget
);
270 m_phononWidget
->clearUrl();
274 m_preview
->stopAnimatedImage();
276 m_phononWidget
->hide();
280 void InformationPanelContent::configureShownProperties()
282 m_configureLabel
->setVisible(true);
283 m_configureButtons
->setVisible(true);
284 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
287 void InformationPanelContent::refreshMetaData()
289 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
290 m_metaDataWidget
->show();
291 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
294 void InformationPanelContent::showItems(const KFileItemList
& items
)
296 // If there is a preview job, kill it to prevent that we have jobs for
297 // multiple items running, and thus a race condition (bug 250787).
299 m_previewJob
->kill();
302 m_preview
->stopAnimatedImage();
304 m_preview
->setPixmap(
305 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
307 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
309 m_metaDataWidget
->setItems(items
);
311 m_phononWidget
->hide();
313 m_item
= KFileItem();
316 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
318 switch (event
->type()) {
319 case QEvent::Resize
: {
320 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
321 if (obj
== m_metaDataArea
->viewport()) {
322 // The size of the meta text area has changed. Adjust the fixed
323 // width in a way that no horizontal scrollbar needs to be shown.
324 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
325 } else if (obj
== parent()) {
326 adjustWidgetSizes(resizeEvent
->size().width());
332 adjustWidgetSizes(parentWidget()->width());
335 case QEvent::FontChange
:
336 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
343 return QWidget::eventFilter(obj
, event
);
346 bool InformationPanelContent::event(QEvent
* event
)
348 if (event
->type() == QEvent::Gesture
) {
349 gestureEvent(static_cast<QGestureEvent
*>(event
));
352 return QWidget::event(event
);
355 bool InformationPanelContent::gestureEvent(QGestureEvent
* event
)
361 QTapAndHoldGesture
* tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
364 if (tap
->state() == Qt::GestureFinished
) {
365 emit
contextMenuRequested(tap
->position().toPoint());
373 void InformationPanelContent::showIcon(const KFileItem
& item
)
375 m_outdatedPreviewTimer
->stop();
376 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->height(), m_preview
->width());
377 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
378 m_preview
->setPixmap(pixmap
);
381 void InformationPanelContent::showPreview(const KFileItem
& item
,
382 const QPixmap
& pixmap
)
384 m_outdatedPreviewTimer
->stop();
387 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
392 // compute relative pixel positions
393 const int zeroX
= static_cast<int>(p
.width() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
394 const int zeroY
= static_cast<int>(p
.height() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
397 arrow
<< QPoint(zeroX
, zeroY
);
398 arrow
<< QPoint(zeroX
, zeroY
+ PLAY_ARROW_SIZE
);
399 arrow
<< QPoint(zeroX
+ PLAY_ARROW_SIZE
, zeroY
+ PLAY_ARROW_SIZE
/ 2);
402 path
.addPolygon(arrow
);
404 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
405 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
407 QColor whiteColor
= Qt::white
;
408 QColor blackColor
= Qt::black
;
409 gradient
.setColorAt(0, whiteColor
);
410 gradient
.setColorAt(1, blackColor
);
412 QBrush
brush(gradient
);
414 QPainter
painter(&p
);
416 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
419 painter
.setRenderHint(QPainter::Antialiasing
);
420 painter
.drawPolygon(arrow
);
421 painter
.fillPath(path
, brush
);
424 m_preview
->setPixmap(p
);
427 void InformationPanelContent::markOutdatedPreview()
429 if (m_item
.isDir()) {
430 // directory preview can be long
431 // but since we always have icons to display
432 // use it until the preview is done
435 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
436 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
437 KIconLoader::Desktop
,
438 KIconLoader::DisabledState
);
439 m_preview
->setPixmap(disabledPixmap
);
443 KFileItemList
InformationPanelContent::items()
445 return m_metaDataWidget
->items();
448 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
450 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
451 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
452 // in case the information panel has been resized when the preview was not displayed
453 // we need to refresh its content
458 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
459 m_phononWidget
->setAutoPlay(autoPlay
);
462 void InformationPanelContent::setNameLabelText(const QString
& text
)
464 QTextOption textOption
;
465 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
467 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
469 QTextLayout
textLayout(processedText
);
470 textLayout
.setFont(m_nameLabel
->font());
471 textLayout
.setTextOption(textOption
);
474 wrappedText
.reserve(processedText
.length());
476 // wrap the text to fit into the width of m_nameLabel
477 textLayout
.beginLayout();
478 QTextLine line
= textLayout
.createLine();
479 while (line
.isValid()) {
480 line
.setLineWidth(m_nameLabel
->width());
481 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
483 line
= textLayout
.createLine();
484 if (line
.isValid()) {
485 wrappedText
+= QChar::LineSeparator
;
488 textLayout
.endLayout();
490 m_nameLabel
->setText(wrappedText
);
493 void InformationPanelContent::adjustWidgetSizes(int width
)
495 // If the text inside the name label or the info label cannot
496 // get wrapped, then the maximum width of the label is increased
497 // so that the width of the information panel gets increased.
498 // To prevent this, the maximum width is adjusted to
499 // the current width of the panel.
500 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
501 m_nameLabel
->setMaximumWidth(maxWidth
);
503 // The metadata widget also contains a text widget which may return
504 // a large preferred width.
505 m_metaDataWidget
->setMaximumWidth(maxWidth
);
507 // try to increase the preview as large as possible
508 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
510 if (m_phononWidget
->isVisible()) {
511 // assure that the size of the video player is the same as the preview size
512 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));