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