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