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
);
102 const bool previewsShown
= InformationPanelSettings::previewsShown();
103 m_preview
->setVisible(previewsShown
);
105 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
106 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
107 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
108 this, &InformationPanelContent::urlActivated
);
109 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
110 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
113 m_configureLabel
= new QLabel(i18nc("@label::textbox",
114 "Select which data should be shown:"), this);
115 m_configureLabel
->setWordWrap(true);
116 m_configureLabel
->setVisible(false);
118 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
119 m_configureButtons
->setVisible(false);
120 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
121 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
122 m_configureButtons
->setVisible(false);
123 m_configureLabel
->setVisible(false);
124 Q_EMIT
configurationFinished();
127 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
128 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
129 m_configureButtons
->setVisible(false);
130 m_configureLabel
->setVisible(false);
131 Q_EMIT
configurationFinished();
135 m_metaDataArea
= new QScrollArea(parent
);
136 m_metaDataArea
->setWidget(m_metaDataWidget
);
137 m_metaDataArea
->setWidgetResizable(true);
138 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
140 QWidget
* viewport
= m_metaDataArea
->viewport();
141 QScroller::grabGesture(viewport
, QScroller::TouchGesture
);
142 viewport
->installEventFilter(this);
144 layout
->addWidget(m_preview
);
145 layout
->addWidget(m_phononWidget
);
146 layout
->addWidget(m_nameLabel
);
147 layout
->addWidget(new KSeparator());
148 layout
->addWidget(m_configureLabel
);
149 layout
->addWidget(m_metaDataArea
);
150 layout
->addWidget(m_configureButtons
);
152 grabGesture(Qt::TapAndHoldGesture
);
154 m_placesItemModel
= new PlacesItemModel(this);
157 InformationPanelContent::~InformationPanelContent()
159 InformationPanelSettings::self()->save();
162 void InformationPanelContent::showItem(const KFileItem
& item
)
164 // compares item entries, comparing items only compares urls
165 if (m_item
.entry() != item
.entry()) {
167 m_preview
->stopAnimatedImage();
174 void InformationPanelContent::refreshPixmapView()
176 // If there is a preview job, kill it to prevent that we have jobs for
177 // multiple items running, and thus a race condition (bug 250787).
179 m_previewJob
->kill();
182 // try to get a preview pixmap from the item...
184 // Mark the currently shown preview as outdated. This is done
185 // with a small delay to prevent a flickering when the next preview
186 // can be shown within a short timeframe.
187 m_outdatedPreviewTimer
->start();
189 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
190 const QStringList plugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
191 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
192 QSize(m_preview
->width(), m_preview
->height()),
194 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
195 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
196 if (m_previewJob
->uiDelegate()) {
197 KJobWidgets::setWindow(m_previewJob
, this);
200 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
201 this, &InformationPanelContent::showPreview
);
202 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
203 this, &InformationPanelContent::showIcon
);
206 void InformationPanelContent::refreshPreview()
208 // If there is a preview job, kill it to prevent that we have jobs for
209 // multiple items running, and thus a race condition (bug 250787).
211 m_previewJob
->kill();
214 m_preview
->setCursor(Qt::ArrowCursor
);
215 setNameLabelText(m_item
.text());
216 if (InformationPanelSettings::previewsShown()) {
218 const QUrl itemUrl
= m_item
.url();
219 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
222 m_phononWidget
->hide();
224 // in the case of a search-URL the URL is not readable for humans
225 // (at least not useful to show in the Information Panel)
226 m_preview
->setPixmap(
227 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview
->height(), m_preview
->width())
233 const QString mimeType
= m_item
.mimetype();
234 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
235 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
236 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
239 // change the cursor of the preview
240 m_preview
->setCursor(Qt::PointingHandCursor
);
241 m_preview
->installEventFilter(m_phononWidget
);
242 m_phononWidget
->show();
244 // if the video is playing, has been paused or stopped
245 // we don't need to update the preview/phonon widget states
246 // unless the previewed file has changed,
247 // or the setting previewshown has changed
248 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
249 m_phononWidget
->state() != Phonon::State::PausedState
&&
250 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
251 m_item
.targetUrl() != m_phononWidget
->url() ||
252 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
254 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
255 // hides the preview now to avoid flickering when the autoplay video starts
258 // the video won't play before the preview is displayed
262 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
263 adjustWidgetSizes(parentWidget()->width());
266 if (isAnimatedImage
) {
267 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
269 // When we don't need it, hide the phonon widget first to avoid flickering
270 m_phononWidget
->hide();
272 m_preview
->removeEventFilter(m_phononWidget
);
273 m_phononWidget
->clearUrl();
277 m_preview
->stopAnimatedImage();
279 m_phononWidget
->hide();
283 void InformationPanelContent::configureShownProperties()
285 m_configureLabel
->setVisible(true);
286 m_configureButtons
->setVisible(true);
287 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
290 void InformationPanelContent::refreshMetaData()
292 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
293 m_metaDataWidget
->show();
294 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
297 void InformationPanelContent::showItems(const KFileItemList
& items
)
299 // If there is a preview job, kill it to prevent that we have jobs for
300 // multiple items running, and thus a race condition (bug 250787).
302 m_previewJob
->kill();
305 m_preview
->stopAnimatedImage();
307 m_preview
->setPixmap(
308 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
310 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
312 m_metaDataWidget
->setItems(items
);
314 m_phononWidget
->hide();
316 m_item
= KFileItem();
319 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
321 switch (event
->type()) {
322 case QEvent::Resize
: {
323 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
324 if (obj
== m_metaDataArea
->viewport()) {
325 // The size of the meta text area has changed. Adjust the fixed
326 // width in a way that no horizontal scrollbar needs to be shown.
327 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
328 } else if (obj
== parent()) {
329 adjustWidgetSizes(resizeEvent
->size().width());
335 adjustWidgetSizes(parentWidget()->width());
338 case QEvent::FontChange
:
339 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
346 return QWidget::eventFilter(obj
, event
);
349 bool InformationPanelContent::event(QEvent
* event
)
351 if (event
->type() == QEvent::Gesture
) {
352 gestureEvent(static_cast<QGestureEvent
*>(event
));
355 return QWidget::event(event
);
358 bool InformationPanelContent::gestureEvent(QGestureEvent
* event
)
364 QTapAndHoldGesture
* tap
= static_cast<QTapAndHoldGesture
*>(event
->gesture(Qt::TapAndHoldGesture
));
367 if (tap
->state() == Qt::GestureFinished
) {
368 Q_EMIT
contextMenuRequested(tap
->position().toPoint());
376 void InformationPanelContent::showIcon(const KFileItem
& item
)
378 m_outdatedPreviewTimer
->stop();
379 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(m_preview
->height(), m_preview
->width());
380 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
381 m_preview
->setPixmap(pixmap
);
384 void InformationPanelContent::showPreview(const KFileItem
& item
,
385 const QPixmap
& pixmap
)
387 m_outdatedPreviewTimer
->stop();
390 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
395 // compute relative pixel positions
396 const int zeroX
= static_cast<int>(p
.width() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
397 const int zeroY
= static_cast<int>(p
.height() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
400 arrow
<< QPoint(zeroX
, zeroY
);
401 arrow
<< QPoint(zeroX
, zeroY
+ PLAY_ARROW_SIZE
);
402 arrow
<< QPoint(zeroX
+ PLAY_ARROW_SIZE
, zeroY
+ PLAY_ARROW_SIZE
/ 2);
405 path
.addPolygon(arrow
);
407 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
408 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
410 QColor whiteColor
= Qt::white
;
411 QColor blackColor
= Qt::black
;
412 gradient
.setColorAt(0, whiteColor
);
413 gradient
.setColorAt(1, blackColor
);
415 QBrush
brush(gradient
);
417 QPainter
painter(&p
);
419 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
422 painter
.setRenderHint(QPainter::Antialiasing
);
423 painter
.drawPolygon(arrow
);
424 painter
.fillPath(path
, brush
);
427 m_preview
->setPixmap(p
);
430 void InformationPanelContent::markOutdatedPreview()
432 if (m_item
.isDir()) {
433 // directory preview can be long
434 // but since we always have icons to display
435 // use it until the preview is done
438 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
439 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
440 KIconLoader::Desktop
,
441 KIconLoader::DisabledState
);
442 m_preview
->setPixmap(disabledPixmap
);
446 KFileItemList
InformationPanelContent::items()
448 return m_metaDataWidget
->items();
451 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
453 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
454 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
455 // in case the information panel has been resized when the preview was not displayed
456 // we need to refresh its content
461 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
462 m_phononWidget
->setAutoPlay(autoPlay
);
465 void InformationPanelContent::setNameLabelText(const QString
& text
)
467 QTextOption textOption
;
468 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
470 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
472 QTextLayout
textLayout(processedText
);
473 textLayout
.setFont(m_nameLabel
->font());
474 textLayout
.setTextOption(textOption
);
477 wrappedText
.reserve(processedText
.length());
479 // wrap the text to fit into the width of m_nameLabel
480 textLayout
.beginLayout();
481 QTextLine line
= textLayout
.createLine();
482 while (line
.isValid()) {
483 line
.setLineWidth(m_nameLabel
->width());
484 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
486 line
= textLayout
.createLine();
487 if (line
.isValid()) {
488 wrappedText
+= QChar::LineSeparator
;
491 textLayout
.endLayout();
493 m_nameLabel
->setText(wrappedText
);
496 void InformationPanelContent::adjustWidgetSizes(int width
)
498 // If the text inside the name label or the info label cannot
499 // get wrapped, then the maximum width of the label is increased
500 // so that the width of the information panel gets increased.
501 // To prevent this, the maximum width is adjusted to
502 // the current width of the panel.
503 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
504 m_nameLabel
->setMaximumWidth(maxWidth
);
506 // The metadata widget also contains a text widget which may return
507 // a large preferred width.
508 m_metaDataWidget
->setMaximumWidth(maxWidth
);
510 // try to increase the preview as large as possible
511 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
513 if (m_phononWidget
->isVisible()) {
514 // assure that the size of the video player is the same as the preview size
515 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));