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