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>
30 #include <QPainterPath>
33 #include <QTextDocument>
35 #include <Baloo/FileMetaDataWidget>
37 #include <panels/places/placesitem.h>
38 #include <panels/places/placesitemmodel.h>
40 #include <Phonon/BackendCapabilities>
41 #include <Phonon/MediaObject>
44 #include <QDialogButtonBox>
45 #include <QScrollArea>
46 #include <QTextLayout>
48 #include <QVBoxLayout>
52 #include <QLinearGradient>
55 #include "dolphin_informationpanelsettings.h"
56 #include "phononwidget.h"
57 #include "pixmapviewer.h"
59 const int PLAY_ARROW_SIZE
= 24;
60 const int PLAY_ARROW_BORDER_SIZE
= 2;
62 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
65 m_previewJob(nullptr),
66 m_outdatedPreviewTimer(nullptr),
68 m_phononWidget(nullptr),
70 m_metaDataWidget(nullptr),
71 m_metaDataArea(nullptr),
72 m_placesItemModel(nullptr),
75 parent
->installEventFilter(this);
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
);
86 QVBoxLayout
* layout
= new QVBoxLayout(this);
89 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
91 m_preview
= new PixmapViewer(parent
);
92 m_preview
->setMinimumWidth(minPreviewWidth
);
93 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
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
);
103 m_nameLabel
= new QLabel(parent
);
104 QFont font
= m_nameLabel
->font();
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
);
111 const bool previewsShown
= InformationPanelSettings::previewsShown();
112 m_preview
->setVisible(previewsShown
);
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
);
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);
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();
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();
144 m_metaDataArea
= new QScrollArea(parent
);
145 m_metaDataArea
->setWidget(m_metaDataWidget
);
146 m_metaDataArea
->setWidgetResizable(true);
147 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
149 QWidget
* viewport
= m_metaDataArea
->viewport();
150 viewport
->installEventFilter(this);
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
);
160 m_placesItemModel
= new PlacesItemModel(this);
163 InformationPanelContent::~InformationPanelContent()
165 InformationPanelSettings::self()->save();
168 void InformationPanelContent::showItem(const KFileItem
& item
)
170 // compares item entries, comparing items only compares urls
171 if (m_item
.entry() != item
.entry()) {
173 m_preview
->stopAnimatedImage();
180 void InformationPanelContent::refreshPixmapView()
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).
185 m_previewJob
->kill();
188 // try to get a preview pixmap from the item...
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
195 if (!m_item
.isDir()) {
196 m_outdatedPreviewTimer
->start();
199 QStringList plugins
= KIO::PreviewJob::availablePlugins();
200 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
201 QSize(m_preview
->width(), m_preview
->height()),
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);
209 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
210 this, &InformationPanelContent::showPreview
);
211 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
212 this, &InformationPanelContent::showIcon
);
215 void InformationPanelContent::refreshPreview()
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).
220 m_previewJob
->kill();
223 m_preview
->setCursor(Qt::ArrowCursor
);
224 bool usePhonon
= false;
225 setNameLabelText(m_item
.text());
226 if (InformationPanelSettings::previewsShown()) {
228 const QUrl itemUrl
= m_item
.url();
229 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
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())
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/"));
248 // change the cursor of the preview
249 m_preview
->setCursor(Qt::PointingHandCursor
);
250 m_preview
->installEventFilter(m_phononWidget
);
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())) {
262 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
263 // hides the preview now to avoid flickering when the autoplay video starts
266 // the video won't play before the preview is displayed
270 m_phononWidget
->show();
271 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
272 adjustWidgetSizes(parentWidget()->width());
275 if (isAnimatedImage
) {
276 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
278 // When we don't need it, hide the phonon widget first to avoid flickering
279 m_phononWidget
->hide();
281 m_preview
->removeEventFilter(m_phononWidget
);
282 m_phononWidget
->clearUrl();
286 m_preview
->stopAnimatedImage();
288 m_phononWidget
->hide();
292 void InformationPanelContent::configureShownProperties()
294 m_configureLabel
->setVisible(true);
295 m_configureButtons
->setVisible(true);
296 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
299 void InformationPanelContent::refreshMetaData()
301 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
302 m_metaDataWidget
->show();
303 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
306 void InformationPanelContent::showItems(const KFileItemList
& items
)
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).
311 m_previewJob
->kill();
314 m_preview
->stopAnimatedImage();
316 m_preview
->setPixmap(
317 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
319 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
321 m_metaDataWidget
->setItems(items
);
323 m_phononWidget
->hide();
325 m_item
= KFileItem();
328 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
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());
344 adjustWidgetSizes(parentWidget()->width());
347 case QEvent::FontChange
:
348 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
355 return QWidget::eventFilter(obj
, event
);
358 void InformationPanelContent::showIcon(const KFileItem
& item
)
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
);
366 void InformationPanelContent::showPreview(const KFileItem
& item
,
367 const QPixmap
& pixmap
)
369 m_outdatedPreviewTimer
->stop();
372 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
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());
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);
387 path
.addPolygon(arrow
);
389 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
390 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
392 QColor whiteColor
= Qt::white
;
393 QColor blackColor
= Qt::black
;
394 gradient
.setColorAt(0, whiteColor
);
395 gradient
.setColorAt(1, blackColor
);
397 QBrush
brush(gradient
);
399 QPainter
painter(&p
);
401 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
404 painter
.setRenderHint(QPainter::Antialiasing
);
405 painter
.drawPolygon(arrow
);
406 painter
.fillPath(path
, brush
);
409 m_preview
->setPixmap(p
);
412 void InformationPanelContent::markOutdatedPreview()
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
);
421 KFileItemList
InformationPanelContent::items()
423 return m_metaDataWidget
->items();
426 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
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
436 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
437 m_phononWidget
->setAutoPlay(autoPlay
);
440 void InformationPanelContent::setNameLabelText(const QString
& text
)
442 QTextOption textOption
;
443 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
445 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
447 QTextLayout
textLayout(processedText
);
448 textLayout
.setFont(m_nameLabel
->font());
449 textLayout
.setTextOption(textOption
);
452 wrappedText
.reserve(processedText
.length());
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());
461 line
= textLayout
.createLine();
462 if (line
.isValid()) {
463 wrappedText
+= QChar::LineSeparator
;
466 textLayout
.endLayout();
468 m_nameLabel
->setText(wrappedText
);
471 void InformationPanelContent::adjustWidgetSizes(int width
)
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
);
481 // The metadata widget also contains a text widget which may return
482 // a large preferred width.
483 m_metaDataWidget
->setMaximumWidth(maxWidth
);
485 // try to increase the preview as large as possible
486 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
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
));