1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "informationpanelcontent.h"
22 #include <KIO/JobUiDelegate>
23 #include <KIO/PreviewJob>
24 #include <KIconEffect>
25 #include <KIconLoader>
26 #include <KJobWidgets>
27 #include <KLocalizedString>
29 #include <KStringHandler>
32 #include <QTextDocument>
34 #include <Baloo/FileMetaDataWidget>
36 #include <panels/places/placesitem.h>
37 #include <panels/places/placesitemmodel.h>
39 #include <Phonon/BackendCapabilities>
40 #include <Phonon/MediaObject>
43 #include <QDialogButtonBox>
44 #include <QScrollArea>
45 #include <QTextLayout>
47 #include <QVBoxLayout>
51 #include <QLinearGradient>
54 #include "dolphin_informationpanelsettings.h"
55 #include "phononwidget.h"
56 #include "pixmapviewer.h"
58 const int PLAY_ARROW_SIZE
= 24;
59 const int PLAY_ARROW_BORDER_SIZE
= 2;
61 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
64 m_previewJob(nullptr),
65 m_outdatedPreviewTimer(nullptr),
67 m_phononWidget(nullptr),
69 m_metaDataWidget(nullptr),
70 m_metaDataArea(nullptr),
71 m_placesItemModel(nullptr),
74 parent
->installEventFilter(this);
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
);
85 QVBoxLayout
* layout
= new QVBoxLayout(this);
88 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
90 m_preview
= new PixmapViewer(parent
);
91 m_preview
->setMinimumWidth(minPreviewWidth
);
92 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
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
);
102 m_nameLabel
= new QLabel(parent
);
103 QFont font
= m_nameLabel
->font();
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
);
110 const bool previewsShown
= InformationPanelSettings::previewsShown();
111 m_preview
->setVisible(previewsShown
);
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
);
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);
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();
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();
143 m_metaDataArea
= new QScrollArea(parent
);
144 m_metaDataArea
->setWidget(m_metaDataWidget
);
145 m_metaDataArea
->setWidgetResizable(true);
146 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
148 QWidget
* viewport
= m_metaDataArea
->viewport();
149 viewport
->installEventFilter(this);
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
);
159 m_placesItemModel
= new PlacesItemModel(this);
162 InformationPanelContent::~InformationPanelContent()
164 InformationPanelSettings::self()->save();
167 void InformationPanelContent::showItem(const KFileItem
& item
)
169 // compares item entries, comparing items only compares urls
170 if (m_item
.entry() != item
.entry()) {
172 m_preview
->stopAnimatedImage();
179 void InformationPanelContent::refreshPixmapView()
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).
184 m_previewJob
->kill();
187 // try to get a preview pixmap from the item...
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
194 if (!m_item
.isDir()) {
195 m_outdatedPreviewTimer
->start();
198 QStringList plugins
= KIO::PreviewJob::availablePlugins();
199 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
200 QSize(m_preview
->width(), m_preview
->height()),
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);
208 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
209 this, &InformationPanelContent::showPreview
);
210 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
211 this, &InformationPanelContent::showIcon
);
214 void InformationPanelContent::refreshPreview()
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).
219 m_previewJob
->kill();
222 m_preview
->setCursor(Qt::ArrowCursor
);
223 bool usePhonon
= false;
224 setNameLabelText(m_item
.text());
225 if (InformationPanelSettings::previewsShown()) {
227 const QUrl itemUrl
= m_item
.url();
228 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
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
)
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/"));
247 // change the cursor of the preview
248 m_preview
->setCursor(Qt::PointingHandCursor
);
249 m_preview
->installEventFilter(m_phononWidget
);
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())) {
261 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
262 // hides the preview now to avoid flickering when the autoplay video starts
265 // the video won't play before the preview is displayed
269 m_phononWidget
->show();
270 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
271 adjustWidgetSizes(parentWidget()->width());
274 if (isAnimatedImage
) {
275 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
277 // When we don't need it, hide the phonon widget first to avoid flickering
278 m_phononWidget
->hide();
280 m_preview
->removeEventFilter(m_phononWidget
);
281 m_phononWidget
->clearUrl();
285 m_preview
->stopAnimatedImage();
287 m_phononWidget
->hide();
291 void InformationPanelContent::configureShownProperties()
293 m_configureLabel
->setVisible(true);
294 m_configureButtons
->setVisible(true);
295 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
298 void InformationPanelContent::refreshMetaData()
300 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
301 m_metaDataWidget
->show();
302 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
305 void InformationPanelContent::showItems(const KFileItemList
& items
)
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).
310 m_previewJob
->kill();
313 m_preview
->stopAnimatedImage();
315 m_preview
->setPixmap(
316 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
318 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
320 m_metaDataWidget
->setItems(items
);
322 m_phononWidget
->hide();
324 m_item
= KFileItem();
327 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
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());
343 adjustWidgetSizes(parentWidget()->width());
346 case QEvent::FontChange
:
347 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
354 return QWidget::eventFilter(obj
, event
);
357 void InformationPanelContent::showIcon(const KFileItem
& item
)
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
);
365 void InformationPanelContent::showPreview(const KFileItem
& item
,
366 const QPixmap
& pixmap
)
368 m_outdatedPreviewTimer
->stop();
371 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
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());
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);
386 path
.addPolygon(arrow
);
388 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
389 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
391 QColor whiteColor
= Qt::white
;
392 QColor blackColor
= Qt::black
;
393 gradient
.setColorAt(0, whiteColor
);
394 gradient
.setColorAt(1, blackColor
);
396 QBrush
brush(gradient
);
398 QPainter
painter(&p
);
400 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
403 painter
.setRenderHint(QPainter::Antialiasing
);
404 painter
.drawPolygon(arrow
);
405 painter
.fillPath(path
, brush
);
408 m_preview
->setPixmap(p
);
411 void InformationPanelContent::markOutdatedPreview()
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
);
420 KFileItemList
InformationPanelContent::items()
422 return m_metaDataWidget
->items();
425 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
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
435 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
436 m_phononWidget
->setAutoPlay(autoPlay
);
439 void InformationPanelContent::setNameLabelText(const QString
& text
)
441 QTextOption textOption
;
442 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
444 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
446 QTextLayout
textLayout(processedText
);
447 textLayout
.setFont(m_nameLabel
->font());
448 textLayout
.setTextOption(textOption
);
451 wrappedText
.reserve(processedText
.length());
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());
460 line
= textLayout
.createLine();
461 if (line
.isValid()) {
462 wrappedText
+= QChar::LineSeparator
;
465 textLayout
.endLayout();
467 m_nameLabel
->setText(wrappedText
);
470 void InformationPanelContent::adjustWidgetSizes(int width
)
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
);
480 // The metadata widget also contains a text widget which may return
481 // a large preferred width.
482 m_metaDataWidget
->setMaximumWidth(maxWidth
);
484 // try to increase the preview as large as possible
485 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
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
));