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