]> 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(100);
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.
193 m_outdatedPreviewTimer->start();
194
195 QStringList plugins = KIO::PreviewJob::availablePlugins();
196 m_previewJob = new KIO::PreviewJob(KFileItemList() << m_item,
197 QSize(m_preview->width(), m_preview->height()),
198 &plugins);
199 m_previewJob->setScaleType(KIO::PreviewJob::Unscaled);
200 m_previewJob->setIgnoreMaximumSize(m_item.isLocalFile());
201 if (m_previewJob->uiDelegate()) {
202 KJobWidgets::setWindow(m_previewJob, this);
203 }
204
205 connect(m_previewJob.data(), &KIO::PreviewJob::gotPreview,
206 this, &InformationPanelContent::showPreview);
207 connect(m_previewJob.data(), &KIO::PreviewJob::failed,
208 this, &InformationPanelContent::showIcon);
209 }
210
211 void InformationPanelContent::refreshPreview()
212 {
213 // If there is a preview job, kill it to prevent that we have jobs for
214 // multiple items running, and thus a race condition (bug 250787).
215 if (m_previewJob) {
216 m_previewJob->kill();
217 }
218
219 m_preview->setCursor(Qt::ArrowCursor);
220 setNameLabelText(m_item.text());
221 if (InformationPanelSettings::previewsShown()) {
222
223 const QUrl itemUrl = m_item.url();
224 const bool isSearchUrl = itemUrl.scheme().contains(QLatin1String("search")) && m_item.localPath().isEmpty();
225 if (isSearchUrl) {
226 m_preview->show();
227 m_phononWidget->hide();
228
229 // in the case of a search-URL the URL is not readable for humans
230 // (at least not useful to show in the Information Panel)
231 m_preview->setPixmap(
232 QIcon::fromTheme(QStringLiteral("baloo")).pixmap(m_preview->height(), m_preview->width())
233 );
234 } else {
235
236 refreshPixmapView();
237
238 const QString mimeType = m_item.mimetype();
239 const bool isAnimatedImage = m_preview->isAnimatedImage(itemUrl.toLocalFile());
240 m_isVideo = !isAnimatedImage && mimeType.startsWith(QLatin1String("video/"));
241 bool usePhonon = m_isVideo || mimeType.startsWith(QLatin1String("audio/"));
242
243 if (usePhonon) {
244 // change the cursor of the preview
245 m_preview->setCursor(Qt::PointingHandCursor);
246 m_preview->installEventFilter(m_phononWidget);
247 m_phononWidget->show();
248
249 // if the video is playing, has been paused or stopped
250 // we don't need to update the preview/phonon widget states
251 // unless the previewed file has changed,
252 // or the setting previewshown has changed
253 if ((m_phononWidget->state() != Phonon::State::PlayingState &&
254 m_phononWidget->state() != Phonon::State::PausedState &&
255 m_phononWidget->state() != Phonon::State::StoppedState) ||
256 m_item.targetUrl() != m_phononWidget->url() ||
257 (!m_preview->isVisible() &&! m_phononWidget->isVisible())) {
258
259 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo) {
260 // hides the preview now to avoid flickering when the autoplay video starts
261 m_preview->hide();
262 } else {
263 // the video won't play before the preview is displayed
264 m_preview->show();
265 }
266
267 m_phononWidget->setUrl(m_item.targetUrl(), m_isVideo ? PhononWidget::MediaKind::Video : PhononWidget::MediaKind::Audio);
268 adjustWidgetSizes(parentWidget()->width());
269 }
270 } else {
271 if (isAnimatedImage) {
272 m_preview->setAnimatedImageFileName(itemUrl.toLocalFile());
273 }
274 // When we don't need it, hide the phonon widget first to avoid flickering
275 m_phononWidget->hide();
276 m_preview->show();
277 m_preview->removeEventFilter(m_phononWidget);
278 m_phononWidget->clearUrl();
279 }
280 }
281 } else {
282 m_preview->stopAnimatedImage();
283 m_preview->hide();
284 m_phononWidget->hide();
285 }
286 }
287
288 void InformationPanelContent::configureShownProperties()
289 {
290 m_configureLabel->setVisible(true);
291 m_configureButtons->setVisible(true);
292 m_metaDataWidget->setConfigurationMode(Baloo::ConfigurationMode::ReStart);
293 }
294
295 void InformationPanelContent::refreshMetaData()
296 {
297 m_metaDataWidget->setDateFormat(static_cast<Baloo::DateFormats>(InformationPanelSettings::dateFormat()));
298 m_metaDataWidget->show();
299 m_metaDataWidget->setItems(KFileItemList() << m_item);
300 }
301
302 void InformationPanelContent::showItems(const KFileItemList& items)
303 {
304 // If there is a preview job, kill it to prevent that we have jobs for
305 // multiple items running, and thus a race condition (bug 250787).
306 if (m_previewJob) {
307 m_previewJob->kill();
308 }
309
310 m_preview->stopAnimatedImage();
311
312 m_preview->setPixmap(
313 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview->height(), m_preview->width())
314 );
315 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
316
317 m_metaDataWidget->setItems(items);
318
319 m_phononWidget->hide();
320
321 m_item = KFileItem();
322 }
323
324 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
325 {
326 switch (event->type()) {
327 case QEvent::Resize: {
328 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
329 if (obj == m_metaDataArea->viewport()) {
330 // The size of the meta text area has changed. Adjust the fixed
331 // width in a way that no horizontal scrollbar needs to be shown.
332 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
333 } else if (obj == parent()) {
334 adjustWidgetSizes(resizeEvent->size().width());
335 }
336 break;
337 }
338
339 case QEvent::Polish:
340 adjustWidgetSizes(parentWidget()->width());
341 break;
342
343 case QEvent::FontChange:
344 m_metaDataWidget->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
345 break;
346
347 default:
348 break;
349 }
350
351 return QWidget::eventFilter(obj, event);
352 }
353
354 void InformationPanelContent::showIcon(const KFileItem& item)
355 {
356 m_outdatedPreviewTimer->stop();
357 QPixmap pixmap = QIcon::fromTheme(item.iconName()).pixmap(m_preview->height(), m_preview->width());
358 KIconLoader::global()->drawOverlays(item.overlays(), pixmap, KIconLoader::Desktop);
359 m_preview->setPixmap(pixmap);
360 }
361
362 void InformationPanelContent::showPreview(const KFileItem& item,
363 const QPixmap& pixmap)
364 {
365 m_outdatedPreviewTimer->stop();
366
367 QPixmap p = pixmap;
368 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
369
370 if (m_isVideo) {
371 // adds a play arrow
372
373 // compute relative pixel positions
374 const int zeroX = static_cast<int>(p.width() / 2 - PLAY_ARROW_SIZE / 2 / devicePixelRatio());
375 const int zeroY = static_cast<int>(p.height() / 2 - PLAY_ARROW_SIZE / 2 / devicePixelRatio());
376
377 QPolygon arrow;
378 arrow << QPoint(zeroX, zeroY);
379 arrow << QPoint(zeroX, zeroY + PLAY_ARROW_SIZE);
380 arrow << QPoint(zeroX + PLAY_ARROW_SIZE, zeroY + PLAY_ARROW_SIZE / 2);
381
382 QPainterPath path;
383 path.addPolygon(arrow);
384
385 QLinearGradient gradient(QPointF(zeroX, zeroY),
386 QPointF(zeroX + PLAY_ARROW_SIZE,zeroY + PLAY_ARROW_SIZE));
387
388 QColor whiteColor = Qt::white;
389 QColor blackColor = Qt::black;
390 gradient.setColorAt(0, whiteColor);
391 gradient.setColorAt(1, blackColor);
392
393 QBrush brush(gradient);
394
395 QPainter painter(&p);
396
397 QPen pen(blackColor, PLAY_ARROW_BORDER_SIZE, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
398 painter.setPen(pen);
399
400 painter.setRenderHint(QPainter::Antialiasing);
401 painter.drawPolygon(arrow);
402 painter.fillPath(path, brush);
403 }
404
405 m_preview->setPixmap(p);
406 }
407
408 void InformationPanelContent::markOutdatedPreview()
409 {
410 if (m_item.isDir()) {
411 // directory preview can be long
412 // but since we always have icons to display
413 // use it until the preview is done
414 showIcon(m_item);
415 } else {
416 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
417 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
418 KIconLoader::Desktop,
419 KIconLoader::DisabledState);
420 m_preview->setPixmap(disabledPixmap);
421 }
422 }
423
424 KFileItemList InformationPanelContent::items()
425 {
426 return m_metaDataWidget->items();
427 }
428
429 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
430 {
431 m_preview->setVisible(InformationPanelSettings::previewsShown() && !hasVideo);
432 if (m_preview->isVisible() && m_preview->size().width() != m_preview->pixmap().size().width()) {
433 // in case the information panel has been resized when the preview was not displayed
434 // we need to refresh its content
435 refreshPixmapView();
436 }
437 }
438
439 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay) {
440 m_phononWidget->setAutoPlay(autoPlay);
441 }
442
443 void InformationPanelContent::setNameLabelText(const QString& text)
444 {
445 QTextOption textOption;
446 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
447
448 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
449
450 QTextLayout textLayout(processedText);
451 textLayout.setFont(m_nameLabel->font());
452 textLayout.setTextOption(textOption);
453
454 QString wrappedText;
455 wrappedText.reserve(processedText.length());
456
457 // wrap the text to fit into the width of m_nameLabel
458 textLayout.beginLayout();
459 QTextLine line = textLayout.createLine();
460 while (line.isValid()) {
461 line.setLineWidth(m_nameLabel->width());
462 wrappedText += processedText.midRef(line.textStart(), line.textLength());
463
464 line = textLayout.createLine();
465 if (line.isValid()) {
466 wrappedText += QChar::LineSeparator;
467 }
468 }
469 textLayout.endLayout();
470
471 m_nameLabel->setText(wrappedText);
472 }
473
474 void InformationPanelContent::adjustWidgetSizes(int width)
475 {
476 // If the text inside the name label or the info label cannot
477 // get wrapped, then the maximum width of the label is increased
478 // so that the width of the information panel gets increased.
479 // To prevent this, the maximum width is adjusted to
480 // the current width of the panel.
481 const int maxWidth = width - style()->layoutSpacing(QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Horizontal) * 4;
482 m_nameLabel->setMaximumWidth(maxWidth);
483
484 // The metadata widget also contains a text widget which may return
485 // a large preferred width.
486 m_metaDataWidget->setMaximumWidth(maxWidth);
487
488 // try to increase the preview as large as possible
489 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
490
491 if (m_phononWidget->isVisible()) {
492 // assure that the size of the video player is the same as the preview size
493 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
494 }
495 }
496