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