]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
information panel: scale according dpr
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /*
2 * SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "informationpanelcontent.h"
8
9 #include <KConfigGroup>
10 #include <KIO/PreviewJob>
11 #include <KIconEffect>
12 #include <KIconLoader>
13 #include <KIconUtils>
14 #include <KJobWidgets>
15 #include <KLocalizedString>
16 #include <KSeparator>
17 #include <KSharedConfig>
18 #include <KStringHandler>
19 #include <QPainterPath>
20
21 #include <QIcon>
22 #include <QStyle>
23 #include <QTextDocument>
24
25 #include <Baloo/FileMetaDataWidget>
26
27 #include <phonon/BackendCapabilities>
28 #include <phonon/MediaObject>
29
30 #include <QDialogButtonBox>
31 #include <QGesture>
32 #include <QLabel>
33 #include <QLinearGradient>
34 #include <QPainter>
35 #include <QPolygon>
36 #include <QScrollArea>
37 #include <QScroller>
38 #include <QTextLayout>
39 #include <QTimer>
40 #include <QVBoxLayout>
41
42 #include "dolphin_informationpanelsettings.h"
43 #include "phononwidget.h"
44 #include "pixmapviewer.h"
45
46 const int PLAY_ARROW_SIZE = 24;
47 const int PLAY_ARROW_BORDER_SIZE = 2;
48
49 InformationPanelContent::InformationPanelContent(QWidget *parent)
50 : QWidget(parent)
51 , m_item()
52 , m_previewJob(nullptr)
53 , m_outdatedPreviewTimer(nullptr)
54 , m_preview(nullptr)
55 , m_phononWidget(nullptr)
56 , m_nameLabel(nullptr)
57 , m_metaDataWidget(nullptr)
58 , m_metaDataArea(nullptr)
59 , m_isVideo(false)
60 {
61 parent->installEventFilter(this);
62
63 // Initialize timer for disabling an outdated preview with a small
64 // delay. This prevents flickering if the new preview can be generated
65 // within a very small timeframe.
66 m_outdatedPreviewTimer = new QTimer(this);
67 m_outdatedPreviewTimer->setInterval(100);
68 m_outdatedPreviewTimer->setSingleShot(true);
69 connect(m_outdatedPreviewTimer, &QTimer::timeout, this, &InformationPanelContent::markOutdatedPreview);
70
71 QVBoxLayout *layout = new QVBoxLayout(this);
72
73 // preview
74 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
75
76 m_preview = new PixmapViewer(parent);
77 m_preview->setMinimumWidth(minPreviewWidth);
78 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
79
80 m_phononWidget = new PhononWidget(parent);
81 m_phononWidget->hide();
82 m_phononWidget->setMinimumWidth(minPreviewWidth);
83 m_phononWidget->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
84 connect(m_phononWidget, &PhononWidget::hasVideoChanged, this, &InformationPanelContent::slotHasVideoChanged);
85
86 // name
87 m_nameLabel = new QLabel(parent);
88 QFont font = m_nameLabel->font();
89 font.setBold(true);
90 m_nameLabel->setFont(font);
91 m_nameLabel->setTextFormat(Qt::PlainText);
92 m_nameLabel->setAlignment(Qt::AlignHCenter);
93 m_nameLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
94 m_nameLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
95
96 const bool previewsShown = InformationPanelSettings::previewsShown();
97 m_preview->setVisible(previewsShown);
98
99 m_metaDataWidget = new Baloo::FileMetaDataWidget(parent);
100 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
101 connect(m_metaDataWidget, &Baloo::FileMetaDataWidget::urlActivated, this, &InformationPanelContent::urlActivated);
102 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
103 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
104
105 // Configuration
106 m_configureLabel = new QLabel(i18nc("@label::textbox", "Select which data should be shown:"), this);
107 m_configureLabel->setWordWrap(true);
108 m_configureLabel->setVisible(false);
109
110 m_configureButtons = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
111 m_configureButtons->setVisible(false);
112 connect(m_configureButtons, &QDialogButtonBox::accepted, this, [this]() {
113 m_metaDataWidget->setConfigurationMode(Baloo::ConfigurationMode::Accept);
114 m_configureButtons->setVisible(false);
115 m_configureLabel->setVisible(false);
116 Q_EMIT configurationFinished();
117 });
118 connect(m_configureButtons, &QDialogButtonBox::rejected, this, [this]() {
119 m_metaDataWidget->setConfigurationMode(Baloo::ConfigurationMode::Cancel);
120 m_configureButtons->setVisible(false);
121 m_configureLabel->setVisible(false);
122 Q_EMIT configurationFinished();
123 });
124
125 m_metaDataArea = new QScrollArea(parent);
126 m_metaDataArea->setWidget(m_metaDataWidget);
127 m_metaDataArea->setWidgetResizable(true);
128 m_metaDataArea->setFrameShape(QFrame::NoFrame);
129
130 QWidget *viewport = m_metaDataArea->viewport();
131 QScroller::grabGesture(viewport, QScroller::TouchGesture);
132 viewport->installEventFilter(this);
133
134 layout->addWidget(m_preview);
135 layout->addWidget(m_phononWidget);
136 layout->addWidget(m_nameLabel);
137 layout->addWidget(new KSeparator());
138 layout->addWidget(m_configureLabel);
139 layout->addWidget(m_metaDataArea);
140 layout->addWidget(m_configureButtons);
141
142 grabGesture(Qt::TapAndHoldGesture);
143 }
144
145 InformationPanelContent::~InformationPanelContent()
146 {
147 InformationPanelSettings::self()->save();
148 }
149
150 void InformationPanelContent::showItem(const KFileItem &item)
151 {
152 // compares item entries, comparing items only compares urls
153 if (m_item.entry() != item.entry()) {
154 m_item = item;
155 m_preview->stopAnimatedImage();
156 refreshMetaData();
157 }
158
159 refreshPreview();
160 }
161
162 void InformationPanelContent::refreshPixmapView()
163 {
164 // If there is a preview job, kill it to prevent that we have jobs for
165 // multiple items running, and thus a race condition (bug 250787).
166 if (m_previewJob) {
167 m_previewJob->kill();
168 }
169
170 // try to get a preview pixmap from the item...
171
172 // Mark the currently shown preview as outdated. This is done
173 // with a small delay to prevent a flickering when the next preview
174 // can be shown within a short timeframe.
175 m_outdatedPreviewTimer->start();
176
177 const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
178 const QStringList plugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
179 m_previewJob = new KIO::PreviewJob(KFileItemList() << m_item, QSize(m_preview->width(), m_preview->height()), &plugins);
180 m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
181 m_previewJob->setIgnoreMaximumSize(m_item.isLocalFile() && !m_item.isSlow());
182 m_previewJob->setDevicePixelRatio(devicePixelRatioF());
183 if (m_previewJob->uiDelegate()) {
184 KJobWidgets::setWindow(m_previewJob, this);
185 }
186
187 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview, this, &InformationPanelContent::showPreview);
188 connect(m_previewJob.data(), &KIO::PreviewJob::failed, this, &InformationPanelContent::showIcon);
189 }
190
191 void InformationPanelContent::refreshPreview()
192 {
193 // If there is a preview job, kill it to prevent that we have jobs for
194 // multiple items running, and thus a race condition (bug 250787).
195 if (m_previewJob) {
196 m_previewJob->kill();
197 }
198
199 m_preview->setCursor(Qt::ArrowCursor);
200 setNameLabelText(m_item.text());
201 if (InformationPanelSettings::previewsShown()) {
202 const QUrl itemUrl = m_item.url();
203 const bool isSearchUrl = itemUrl.scheme().contains(QLatin1String("search")) && m_item.localPath().isEmpty();
204 if (isSearchUrl) {
205 m_preview->show();
206 m_phononWidget->hide();
207
208 // in the case of a search-URL the URL is not readable for humans
209 // (at least not useful to show in the Information Panel)
210 m_preview->setPixmap(QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview->height(), m_preview->width()));
211 } else {
212 refreshPixmapView();
213
214 const QString mimeType = m_item.mimetype();
215 const bool isAnimatedImage = m_preview->isAnimatedMimeType(mimeType);
216 m_isVideo = !isAnimatedImage && mimeType.startsWith(QLatin1String("video/"));
217 bool usePhonon = m_isVideo || mimeType.startsWith(QLatin1String("audio/"));
218
219 if (usePhonon) {
220 // change the cursor of the preview
221 m_preview->setCursor(Qt::PointingHandCursor);
222 m_preview->installEventFilter(m_phononWidget);
223 m_phononWidget->show();
224
225 // if the video is playing, has been paused or stopped
226 // we don't need to update the preview/phonon widget states
227 // unless the previewed file has changed,
228 // or the setting previewshown has changed
229 if ((m_phononWidget->state() != Phonon::State::PlayingState && m_phononWidget->state() != Phonon::State::PausedState
230 && m_phononWidget->state() != Phonon::State::StoppedState)
231 || m_item.targetUrl() != m_phononWidget->url() || (!m_preview->isVisible() && !m_phononWidget->isVisible())) {
232 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo) {
233 // hides the preview now to avoid flickering when the autoplay video starts
234 m_preview->hide();
235 } else {
236 // the video won't play before the preview is displayed
237 m_preview->show();
238 }
239
240 m_phononWidget->setUrl(m_item.targetUrl(), m_isVideo ? PhononWidget::MediaKind::Video : PhononWidget::MediaKind::Audio);
241 adjustWidgetSizes(parentWidget()->width());
242 }
243 } else {
244 if (isAnimatedImage) {
245 m_preview->setAnimatedImageFileName(itemUrl.toLocalFile());
246 }
247 // When we don't need it, hide the phonon widget first to avoid flickering
248 m_phononWidget->hide();
249 m_preview->show();
250 m_preview->removeEventFilter(m_phononWidget);
251 m_phononWidget->clearUrl();
252 }
253 }
254 } else {
255 m_preview->stopAnimatedImage();
256 m_preview->hide();
257 m_phononWidget->hide();
258 }
259 }
260
261 void InformationPanelContent::configureShownProperties()
262 {
263 m_configureLabel->setVisible(true);
264 m_configureButtons->setVisible(true);
265 m_metaDataWidget->setConfigurationMode(Baloo::ConfigurationMode::ReStart);
266 }
267
268 void InformationPanelContent::refreshMetaData()
269 {
270 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
271 m_metaDataWidget->show();
272 m_metaDataWidget->setItems(KFileItemList() << m_item);
273 }
274
275 void InformationPanelContent::showItems(const KFileItemList &items)
276 {
277 // If there is a preview job, kill it to prevent that we have jobs for
278 // multiple items running, and thus a race condition (bug 250787).
279 if (m_previewJob) {
280 m_previewJob->kill();
281 }
282
283 m_preview->stopAnimatedImage();
284
285 m_preview->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview->height(), m_preview->width()));
286 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
287
288 m_metaDataWidget->setItems(items);
289
290 m_phononWidget->hide();
291
292 m_item = KFileItem();
293 }
294
295 bool InformationPanelContent::eventFilter(QObject *obj, QEvent *event)
296 {
297 switch (event->type()) {
298 case QEvent::Resize: {
299 QResizeEvent *resizeEvent = static_cast<QResizeEvent *>(event);
300 if (obj == m_metaDataArea->viewport()) {
301 // The size of the meta text area has changed. Adjust the fixed
302 // width in a way that no horizontal scrollbar needs to be shown.
303 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
304 } else if (obj == parent()) {
305 adjustWidgetSizes(resizeEvent->size().width());
306 }
307 break;
308 }
309
310 case QEvent::Polish:
311 adjustWidgetSizes(parentWidget()->width());
312 break;
313
314 case QEvent::FontChange:
315 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
316 break;
317
318 default:
319 break;
320 }
321
322 return QWidget::eventFilter(obj, event);
323 }
324
325 bool InformationPanelContent::event(QEvent *event)
326 {
327 if (event->type() == QEvent::Gesture) {
328 gestureEvent(static_cast<QGestureEvent *>(event));
329 return true;
330 }
331 return QWidget::event(event);
332 }
333
334 bool InformationPanelContent::gestureEvent(QGestureEvent *event)
335 {
336 if (!underMouse()) {
337 return false;
338 }
339
340 QTapAndHoldGesture *tap = static_cast<QTapAndHoldGesture *>(event->gesture(Qt::TapAndHoldGesture));
341
342 if (tap) {
343 if (tap->state() == Qt::GestureFinished) {
344 Q_EMIT contextMenuRequested(tap->position().toPoint());
345 }
346 event->accept();
347 return true;
348 }
349 return false;
350 }
351
352 void InformationPanelContent::showIcon(const KFileItem &item)
353 {
354 m_outdatedPreviewTimer->stop();
355 QIcon icon = QIcon::fromTheme(item.iconName());
356 QPixmap pixmap = KIconUtils::addOverlays(icon, item.overlays()).pixmap(m_preview->size(), devicePixelRatioF());
357 pixmap.setDevicePixelRatio(devicePixelRatioF());
358 m_preview->setPixmap(pixmap);
359 }
360
361 void InformationPanelContent::showPreview(const KFileItem &item, const QPixmap &pixmap)
362 {
363 m_outdatedPreviewTimer->stop();
364
365 const QSize logicalSize = pixmap.size() / pixmap.devicePixelRatioF();
366 QPixmap p = KIconUtils::addOverlays(pixmap, item.overlays()).pixmap(logicalSize, pixmap.devicePixelRatioF());
367 p.setDevicePixelRatio(pixmap.devicePixelRatioF());
368
369 if (m_isVideo) {
370 // adds a play arrow overlay
371
372 auto maxDim = qMax(p.width(), p.height());
373 auto arrowSize = qMax(PLAY_ARROW_SIZE, maxDim / 8);
374
375 // compute relative pixel positions
376 const int zeroX = static_cast<int>((p.width() / 2 - arrowSize / 2) / p.devicePixelRatio());
377 const int zeroY = static_cast<int>((p.height() / 2 - arrowSize / 2) / p.devicePixelRatio());
378
379 QPolygon arrow;
380 arrow << QPoint(zeroX, zeroY);
381 arrow << QPoint(zeroX, zeroY + arrowSize);
382 arrow << QPoint(zeroX + arrowSize, zeroY + arrowSize / 2);
383
384 QPainterPath path;
385 path.addPolygon(arrow);
386
387 QLinearGradient gradient(QPointF(zeroX, zeroY + arrowSize / 2), QPointF(zeroX + arrowSize, zeroY + arrowSize / 2));
388
389 QColor whiteColor = Qt::white;
390 QColor blackColor = Qt::black;
391 gradient.setColorAt(0, whiteColor);
392 gradient.setColorAt(1, blackColor);
393
394 QBrush brush(gradient);
395
396 QPainter painter(&p);
397
398 QPen pen(blackColor, PLAY_ARROW_BORDER_SIZE, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
399 painter.setPen(pen);
400
401 painter.setRenderHint(QPainter::Antialiasing);
402 painter.drawPolygon(arrow);
403 painter.fillPath(path, brush);
404 }
405
406 m_preview->setPixmap(p);
407 }
408
409 void InformationPanelContent::markOutdatedPreview()
410 {
411 if (m_item.isDir()) {
412 // directory preview can be long
413 // but since we always have icons to display
414 // use it until the preview is done
415 showIcon(m_item);
416 } else {
417 #if KICONTHEMES_VERSION >= QT_VERSION_CHECK(6, 5, 0)
418 QPixmap disabledPixmap = m_preview->pixmap();
419 KIconEffect::toDisabled(disabledPixmap);
420 #else
421 QImage img = m_preview->pixmap().toImage();
422 KIconEffect::toGray(img, 1);
423 KIconEffect::semiTransparent(img);
424 QPixmap disabledPixmap = QPixmap::fromImage(img);
425 #endif
426 m_preview->setPixmap(disabledPixmap);
427 }
428 }
429
430 KFileItemList InformationPanelContent::items()
431 {
432 return m_metaDataWidget->items();
433 }
434
435 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
436 {
437 m_preview->setVisible(InformationPanelSettings::previewsShown() && !hasVideo);
438 if (m_preview->isVisible() && m_preview->size().width() != m_preview->pixmap().size().width()) {
439 // in case the information panel has been resized when the preview was not displayed
440 // we need to refresh its content
441 refreshPixmapView();
442 }
443 }
444
445 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay)
446 {
447 m_phononWidget->setAutoPlay(autoPlay);
448 }
449
450 void InformationPanelContent::setNameLabelText(const QString &text)
451 {
452 QTextOption textOption;
453 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
454
455 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
456
457 QTextLayout textLayout(processedText);
458 textLayout.setFont(m_nameLabel->font());
459 textLayout.setTextOption(textOption);
460
461 QString wrappedText;
462 wrappedText.reserve(processedText.length());
463
464 // wrap the text to fit into the width of m_nameLabel
465 textLayout.beginLayout();
466 QTextLine line = textLayout.createLine();
467 while (line.isValid()) {
468 line.setLineWidth(m_nameLabel->width());
469 wrappedText += QStringView(processedText).mid(line.textStart(), line.textLength());
470
471 line = textLayout.createLine();
472 if (line.isValid()) {
473 wrappedText += QChar::LineSeparator;
474 }
475 }
476 textLayout.endLayout();
477
478 m_nameLabel->setText(wrappedText);
479 }
480
481 void InformationPanelContent::adjustWidgetSizes(int width)
482 {
483 // If the text inside the name label or the info label cannot
484 // get wrapped, then the maximum width of the label is increased
485 // so that the width of the information panel gets increased.
486 // To prevent this, the maximum width is adjusted to
487 // the current width of the panel.
488 const int maxWidth = width - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 4;
489 m_nameLabel->setMaximumWidth(maxWidth);
490
491 // The metadata widget also contains a text widget which may return
492 // a large preferred width.
493 m_metaDataWidget->setMaximumWidth(maxWidth);
494
495 // try to increase the preview as large as possible
496 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
497
498 if (m_phononWidget->isVisible()) {
499 // assure that the size of the video player is the same as the preview size
500 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
501 }
502 }
503
504 #include "moc_informationpanelcontent.cpp"