]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
14a470b116178d7bee7b98c11d73a10af3ef9596
[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 QPixmap p = pixmap;
366 if (!item.overlays().isEmpty()) {
367 // Avoid scaling the images that are smaller than the preview size, to be consistent when there is no overlays
368 if (pixmap.height() < m_preview->height() && pixmap.width() < m_preview->width()) {
369 p = QPixmap(m_preview->size() * devicePixelRatioF());
370 p.fill(Qt::transparent);
371 p.setDevicePixelRatio(devicePixelRatioF());
372
373 QPainter painter(&p);
374 painter.drawPixmap(QPointF{m_preview->width() / 2.0 - pixmap.width() / pixmap.devicePixelRatioF() / 2,
375 m_preview->height() / 2.0 - pixmap.height() / pixmap.devicePixelRatioF() / 2}
376 .toPoint(),
377 pixmap);
378 }
379 p = KIconUtils::addOverlays(p, item.overlays()).pixmap(m_preview->size(), devicePixelRatioF());
380 p.setDevicePixelRatio(devicePixelRatioF());
381 }
382
383 if (m_isVideo) {
384 // adds a play arrow overlay
385
386 auto maxDim = qMax(p.width(), p.height());
387 auto arrowSize = qMax(PLAY_ARROW_SIZE, maxDim / 8);
388
389 // compute relative pixel positions
390 const int zeroX = static_cast<int>((p.width() / 2 - arrowSize / 2) / p.devicePixelRatio());
391 const int zeroY = static_cast<int>((p.height() / 2 - arrowSize / 2) / p.devicePixelRatio());
392
393 QPolygon arrow;
394 arrow << QPoint(zeroX, zeroY);
395 arrow << QPoint(zeroX, zeroY + arrowSize);
396 arrow << QPoint(zeroX + arrowSize, zeroY + arrowSize / 2);
397
398 QPainterPath path;
399 path.addPolygon(arrow);
400
401 QLinearGradient gradient(QPointF(zeroX, zeroY + arrowSize / 2), QPointF(zeroX + arrowSize, zeroY + arrowSize / 2));
402
403 QColor whiteColor = Qt::white;
404 QColor blackColor = Qt::black;
405 gradient.setColorAt(0, whiteColor);
406 gradient.setColorAt(1, blackColor);
407
408 QBrush brush(gradient);
409
410 QPainter painter(&p);
411
412 QPen pen(blackColor, PLAY_ARROW_BORDER_SIZE, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
413 painter.setPen(pen);
414
415 painter.setRenderHint(QPainter::Antialiasing);
416 painter.drawPolygon(arrow);
417 painter.fillPath(path, brush);
418 }
419
420 m_preview->setPixmap(p);
421 }
422
423 void InformationPanelContent::markOutdatedPreview()
424 {
425 if (m_item.isDir()) {
426 // directory preview can be long
427 // but since we always have icons to display
428 // use it until the preview is done
429 showIcon(m_item);
430 } else {
431 QPixmap disabledPixmap = m_preview->pixmap();
432 KIconEffect::toDisabled(disabledPixmap);
433 m_preview->setPixmap(disabledPixmap);
434 }
435 }
436
437 KFileItemList InformationPanelContent::items()
438 {
439 return m_metaDataWidget->items();
440 }
441
442 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
443 {
444 m_preview->setVisible(InformationPanelSettings::previewsShown() && !hasVideo);
445 if (m_preview->isVisible() && m_preview->size().width() != m_preview->pixmap().size().width()) {
446 // in case the information panel has been resized when the preview was not displayed
447 // we need to refresh its content
448 refreshPixmapView();
449 }
450 }
451
452 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay)
453 {
454 m_phononWidget->setAutoPlay(autoPlay);
455 }
456
457 void InformationPanelContent::setNameLabelText(const QString &text)
458 {
459 QTextOption textOption;
460 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
461
462 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
463
464 QTextLayout textLayout(processedText);
465 textLayout.setFont(m_nameLabel->font());
466 textLayout.setTextOption(textOption);
467
468 QString wrappedText;
469 wrappedText.reserve(processedText.length());
470
471 // wrap the text to fit into the width of m_nameLabel
472 textLayout.beginLayout();
473 QTextLine line = textLayout.createLine();
474 while (line.isValid()) {
475 line.setLineWidth(m_nameLabel->width());
476 wrappedText += QStringView(processedText).mid(line.textStart(), line.textLength());
477
478 line = textLayout.createLine();
479 if (line.isValid()) {
480 wrappedText += QChar::LineSeparator;
481 }
482 }
483 textLayout.endLayout();
484
485 m_nameLabel->setText(wrappedText);
486 }
487
488 void InformationPanelContent::adjustWidgetSizes(int width)
489 {
490 // If the text inside the name label or the info label cannot
491 // get wrapped, then the maximum width of the label is increased
492 // so that the width of the information panel gets increased.
493 // To prevent this, the maximum width is adjusted to
494 // the current width of the panel.
495 const int maxWidth = width - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 4;
496 m_nameLabel->setMaximumWidth(maxWidth);
497
498 // The metadata widget also contains a text widget which may return
499 // a large preferred width.
500 m_metaDataWidget->setMaximumWidth(maxWidth);
501
502 // try to increase the preview as large as possible
503 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
504
505 if (m_phononWidget->isVisible()) {
506 // assure that the size of the video player is the same as the preview size
507 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
508 }
509 }
510
511 #include "moc_informationpanelcontent.cpp"