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