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