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