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(100);
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.
193 m_outdatedPreviewTimer
->start();
195 QStringList plugins
= KIO::PreviewJob::availablePlugins();
196 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
197 QSize(m_preview
->width(), m_preview
->height()),
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);
205 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
206 this, &InformationPanelContent::showPreview
);
207 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
208 this, &InformationPanelContent::showIcon
);
211 void InformationPanelContent::refreshPreview()
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).
216 m_previewJob
->kill();
219 m_preview
->setCursor(Qt::ArrowCursor
);
220 setNameLabelText(m_item
.text());
221 if (InformationPanelSettings::previewsShown()) {
223 const QUrl itemUrl
= m_item
.url();
224 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
227 m_phononWidget
->hide();
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())
238 const QString mimeType
= m_item
.mimetype();
239 const bool isAnimatedImage
= m_preview
->isAnimatedMimeType(mimeType
);
240 m_isVideo
= !isAnimatedImage
&& mimeType
.startsWith(QLatin1String("video/"));
241 bool usePhonon
= m_isVideo
|| mimeType
.startsWith(QLatin1String("audio/"));
244 // change the cursor of the preview
245 m_preview
->setCursor(Qt::PointingHandCursor
);
246 m_preview
->installEventFilter(m_phononWidget
);
247 m_phononWidget
->show();
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())) {
259 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
260 // hides the preview now to avoid flickering when the autoplay video starts
263 // the video won't play before the preview is displayed
267 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
268 adjustWidgetSizes(parentWidget()->width());
271 if (isAnimatedImage
) {
272 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
274 // When we don't need it, hide the phonon widget first to avoid flickering
275 m_phononWidget
->hide();
277 m_preview
->removeEventFilter(m_phononWidget
);
278 m_phononWidget
->clearUrl();
282 m_preview
->stopAnimatedImage();
284 m_phononWidget
->hide();
288 void InformationPanelContent::configureShownProperties()
290 m_configureLabel
->setVisible(true);
291 m_configureButtons
->setVisible(true);
292 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
295 void InformationPanelContent::refreshMetaData()
297 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
298 m_metaDataWidget
->show();
299 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
302 void InformationPanelContent::showItems(const KFileItemList
& items
)
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).
307 m_previewJob
->kill();
310 m_preview
->stopAnimatedImage();
312 m_preview
->setPixmap(
313 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(m_preview
->height(), m_preview
->width())
315 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
317 m_metaDataWidget
->setItems(items
);
319 m_phononWidget
->hide();
321 m_item
= KFileItem();
324 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
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());
340 adjustWidgetSizes(parentWidget()->width());
343 case QEvent::FontChange
:
344 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
351 return QWidget::eventFilter(obj
, event
);
354 void InformationPanelContent::showIcon(const KFileItem
& item
)
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
);
362 void InformationPanelContent::showPreview(const KFileItem
& item
,
363 const QPixmap
& pixmap
)
365 m_outdatedPreviewTimer
->stop();
368 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
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());
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);
383 path
.addPolygon(arrow
);
385 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
386 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
388 QColor whiteColor
= Qt::white
;
389 QColor blackColor
= Qt::black
;
390 gradient
.setColorAt(0, whiteColor
);
391 gradient
.setColorAt(1, blackColor
);
393 QBrush
brush(gradient
);
395 QPainter
painter(&p
);
397 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
400 painter
.setRenderHint(QPainter::Antialiasing
);
401 painter
.drawPolygon(arrow
);
402 painter
.fillPath(path
, brush
);
405 m_preview
->setPixmap(p
);
408 void InformationPanelContent::markOutdatedPreview()
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
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
);
424 KFileItemList
InformationPanelContent::items()
426 return m_metaDataWidget
->items();
429 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
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
439 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
440 m_phononWidget
->setAutoPlay(autoPlay
);
443 void InformationPanelContent::setNameLabelText(const QString
& text
)
445 QTextOption textOption
;
446 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
448 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
450 QTextLayout
textLayout(processedText
);
451 textLayout
.setFont(m_nameLabel
->font());
452 textLayout
.setTextOption(textOption
);
455 wrappedText
.reserve(processedText
.length());
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());
464 line
= textLayout
.createLine();
465 if (line
.isValid()) {
466 wrappedText
+= QChar::LineSeparator
;
469 textLayout
.endLayout();
471 m_nameLabel
->setText(wrappedText
);
474 void InformationPanelContent::adjustWidgetSizes(int width
)
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
);
484 // The metadata widget also contains a text widget which may return
485 // a large preferred width.
486 m_metaDataWidget
->setMaximumWidth(maxWidth
);
488 // try to increase the preview as large as possible
489 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
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
));