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