]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
mediawidget: bind arrow keys to slide in media
[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 <QDialogButtonBox>
28 #include <QGesture>
29 #include <QLabel>
30 #include <QLinearGradient>
31 #include <QPainter>
32 #include <QPolygon>
33 #include <QScrollArea>
34 #include <QScroller>
35 #include <QTextLayout>
36 #include <QTimer>
37 #include <QVBoxLayout>
38
39 #include "dolphin_informationpanelsettings.h"
40 #include "mediawidget.h"
41 #include "pixmapviewer.h"
42
43 const int PLAY_ARROW_SIZE = 24;
44 const int PLAY_ARROW_BORDER_SIZE = 2;
45
46 InformationPanelContent::InformationPanelContent(QWidget *parent)
47 : QWidget(parent)
48 , m_item()
49 , m_previewJob(nullptr)
50 , m_outdatedPreviewTimer(nullptr)
51 , m_preview(nullptr)
52 , m_mediaWidget(nullptr)
53 , m_nameLabel(nullptr)
54 , m_metaDataWidget(nullptr)
55 , m_metaDataArea(nullptr)
56 , m_isVideo(false)
57 {
58 parent->installEventFilter(this);
59
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);
67
68 QVBoxLayout *layout = new QVBoxLayout(this);
69
70 // preview
71 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
72
73 m_preview = new PixmapViewer(parent);
74 m_preview->setMinimumWidth(minPreviewWidth);
75 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
76
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);
82
83 // name
84 m_nameLabel = new QLabel(parent);
85 QFont font = m_nameLabel->font();
86 font.setBold(true);
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);
92
93 const bool previewsShown = InformationPanelSettings::previewsShown();
94 m_preview->setVisible(previewsShown);
95
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);
101
102 // Configuration
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);
106
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();
114 });
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();
120 });
121
122 m_metaDataArea = new QScrollArea(parent);
123 m_metaDataArea->setWidget(m_metaDataWidget);
124 m_metaDataArea->setWidgetResizable(true);
125 m_metaDataArea->setFrameShape(QFrame::NoFrame);
126
127 QWidget *viewport = m_metaDataArea->viewport();
128 QScroller::grabGesture(viewport, QScroller::TouchGesture);
129 viewport->installEventFilter(this);
130
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);
138
139 grabGesture(Qt::TapAndHoldGesture);
140 }
141
142 InformationPanelContent::~InformationPanelContent()
143 {
144 InformationPanelSettings::self()->save();
145 }
146
147 void InformationPanelContent::showItem(const KFileItem &item)
148 {
149 // compares item entries, comparing items only compares urls
150 if (m_item.entry() != item.entry()) {
151 m_item = item;
152 m_preview->stopAnimatedImage();
153 refreshMetaData();
154 }
155
156 refreshPreview();
157 }
158
159 void InformationPanelContent::refreshPixmapView()
160 {
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).
163 if (m_previewJob) {
164 m_previewJob->kill();
165 }
166
167 // try to get a preview pixmap from the item...
168
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();
173
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);
182 }
183
184 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview, this, &InformationPanelContent::showPreview);
185 connect(m_previewJob.data(), &KIO::PreviewJob::failed, this, &InformationPanelContent::showIcon);
186 }
187
188 void InformationPanelContent::refreshPreview()
189 {
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).
192 if (m_previewJob) {
193 m_previewJob->kill();
194 }
195
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();
201 if (isSearchUrl) {
202 m_preview->show();
203 m_mediaWidget->hide();
204
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()));
208 } else {
209 refreshPixmapView();
210
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/"));
215
216 if (useMedia) {
217 // change the cursor of the preview
218 m_preview->setCursor(Qt::PointingHandCursor);
219 m_preview->installEventFilter(m_mediaWidget);
220
221 m_mediaWidget->show();
222
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
232 m_preview->hide();
233 } else {
234 // the video won't play before the preview is displayed
235 m_preview->show();
236 }
237
238 m_mediaWidget->setUrl(m_item.targetUrl(), m_isVideo ? MediaWidget::MediaKind::Video : MediaWidget::MediaKind::Audio);
239 adjustWidgetSizes(parentWidget()->width());
240 }
241 } else {
242 if (isAnimatedImage) {
243 m_preview->setAnimatedImageFileName(itemUrl.toLocalFile());
244 }
245 // When we don't need it, hide the media widget first to avoid flickering
246 m_mediaWidget->hide();
247 m_preview->show();
248 m_preview->removeEventFilter(m_mediaWidget);
249 m_mediaWidget->clearUrl();
250 }
251 }
252 } else {
253 m_preview->stopAnimatedImage();
254 m_preview->hide();
255 m_mediaWidget->hide();
256 }
257 }
258
259 void InformationPanelContent::configureShownProperties()
260 {
261 m_configureLabel->setVisible(true);
262 m_configureButtons->setVisible(true);
263 m_metaDataWidget->setConfigurationMode(Baloo::ConfigurationMode::ReStart);
264 }
265
266 void InformationPanelContent::refreshMetaData()
267 {
268 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
269 m_metaDataWidget->show();
270 m_metaDataWidget->setItems(KFileItemList() << m_item);
271 }
272
273 void InformationPanelContent::showItems(const KFileItemList &items)
274 {
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).
277 if (m_previewJob) {
278 m_previewJob->kill();
279 }
280
281 m_preview->stopAnimatedImage();
282
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()));
285
286 m_metaDataWidget->setItems(items);
287
288 m_mediaWidget->hide();
289
290 m_item = KFileItem();
291 }
292
293 bool InformationPanelContent::eventFilter(QObject *obj, QEvent *event)
294 {
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());
304 }
305 break;
306 }
307
308 case QEvent::Polish:
309 adjustWidgetSizes(parentWidget()->width());
310 break;
311
312 case QEvent::FontChange:
313 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
314 break;
315
316 default:
317 break;
318 }
319
320 return QWidget::eventFilter(obj, event);
321 }
322
323 bool InformationPanelContent::event(QEvent *event)
324 {
325 if (event->type() == QEvent::Gesture) {
326 gestureEvent(static_cast<QGestureEvent *>(event));
327 return true;
328 }
329 return QWidget::event(event);
330 }
331
332 bool InformationPanelContent::gestureEvent(QGestureEvent *event)
333 {
334 if (!underMouse()) {
335 return false;
336 }
337
338 QTapAndHoldGesture *tap = static_cast<QTapAndHoldGesture *>(event->gesture(Qt::TapAndHoldGesture));
339
340 if (tap) {
341 if (tap->state() == Qt::GestureFinished) {
342 Q_EMIT contextMenuRequested(tap->position().toPoint());
343 }
344 event->accept();
345 return true;
346 }
347 return false;
348 }
349
350 void InformationPanelContent::showIcon(const KFileItem &item)
351 {
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);
357 }
358
359 void InformationPanelContent::showPreview(const KFileItem &item, const QPixmap &pixmap)
360 {
361 m_outdatedPreviewTimer->stop();
362
363 QPixmap p = pixmap;
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());
370
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}
374 .toPoint(),
375 pixmap);
376 }
377 p = KIconUtils::addOverlays(p, item.overlays()).pixmap(m_preview->size(), devicePixelRatioF());
378 p.setDevicePixelRatio(devicePixelRatioF());
379 }
380
381 if (m_isVideo) {
382 // adds a play arrow overlay
383
384 auto maxDim = qMax(p.width(), p.height());
385 auto arrowSize = qMax(PLAY_ARROW_SIZE, maxDim / 8);
386
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());
390
391 QPolygon arrow;
392 arrow << QPoint(zeroX, zeroY);
393 arrow << QPoint(zeroX, zeroY + arrowSize);
394 arrow << QPoint(zeroX + arrowSize, zeroY + arrowSize / 2);
395
396 QPainterPath path;
397 path.addPolygon(arrow);
398
399 QLinearGradient gradient(QPointF(zeroX, zeroY + arrowSize / 2), QPointF(zeroX + arrowSize, zeroY + arrowSize / 2));
400
401 QColor whiteColor = Qt::white;
402 QColor blackColor = Qt::black;
403 gradient.setColorAt(0, whiteColor);
404 gradient.setColorAt(1, blackColor);
405
406 QBrush brush(gradient);
407
408 QPainter painter(&p);
409
410 QPen pen(blackColor, PLAY_ARROW_BORDER_SIZE, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
411 painter.setPen(pen);
412
413 painter.setRenderHint(QPainter::Antialiasing);
414 painter.drawPolygon(arrow);
415 painter.fillPath(path, brush);
416 }
417
418 m_preview->setPixmap(p);
419 }
420
421 void InformationPanelContent::markOutdatedPreview()
422 {
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
427 showIcon(m_item);
428 } else {
429 QPixmap disabledPixmap = m_preview->pixmap();
430 KIconEffect::toDisabled(disabledPixmap);
431 m_preview->setPixmap(disabledPixmap);
432 }
433 }
434
435 KFileItemList InformationPanelContent::items()
436 {
437 return m_metaDataWidget->items();
438 }
439
440 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
441 {
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
446 refreshPixmapView();
447 }
448 }
449
450 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay)
451 {
452 m_mediaWidget->setAutoPlay(autoPlay);
453 }
454
455 void InformationPanelContent::setNameLabelText(const QString &text)
456 {
457 QTextOption textOption;
458 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
459
460 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
461
462 QTextLayout textLayout(processedText);
463 textLayout.setFont(m_nameLabel->font());
464 textLayout.setTextOption(textOption);
465
466 QString wrappedText;
467 wrappedText.reserve(processedText.length());
468
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());
475
476 line = textLayout.createLine();
477 if (line.isValid()) {
478 wrappedText += QChar::LineSeparator;
479 }
480 }
481 textLayout.endLayout();
482
483 m_nameLabel->setText(wrappedText);
484 }
485
486 void InformationPanelContent::adjustWidgetSizes(int width)
487 {
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);
495
496 // The metadata widget also contains a text widget which may return
497 // a large preferred width.
498 m_metaDataWidget->setMaximumWidth(maxWidth);
499
500 // try to increase the preview as large as possible
501 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
502
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));
506 }
507 }
508
509 #include "moc_informationpanelcontent.cpp"