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