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 if (item
!= m_item
) {
177 void InformationPanelContent::refreshPixmapView()
179 // If there is a preview job, kill it to prevent that we have jobs for
180 // multiple items running, and thus a race condition (bug 250787).
182 m_previewJob
->kill();
185 // try to get a preview pixmap from the item...
187 // Mark the currently shown preview as outdated. This is done
188 // with a small delay to prevent a flickering when the next preview
189 // can be shown within a short timeframe. This timer is not started
190 // for directories, as directory previews might fail and return the
192 if (!m_item
.isDir()) {
193 m_outdatedPreviewTimer
->start();
196 QStringList plugins
= KIO::PreviewJob::availablePlugins();
197 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
198 QSize(m_preview
->width(), m_preview
->height()),
200 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
201 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
202 if (m_previewJob
->uiDelegate()) {
203 KJobWidgets::setWindow(m_previewJob
, this);
206 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
207 this, &InformationPanelContent::showPreview
);
208 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
209 this, &InformationPanelContent::showIcon
);
212 void InformationPanelContent::refreshPreview()
214 // If there is a preview job, kill it to prevent that we have jobs for
215 // multiple items running, and thus a race condition (bug 250787).
217 m_previewJob
->kill();
220 m_preview
->setCursor(Qt::ArrowCursor
);
221 bool usePhonon
= false;
222 setNameLabelText(m_item
.text());
223 if (InformationPanelSettings::previewsShown()) {
225 const QUrl itemUrl
= m_item
.url();
226 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
230 // in the case of a search-URL the URL is not readable for humans
231 // (at least not useful to show in the Information Panel)
232 m_preview
->setPixmap(
233 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
239 const QString mimeType
= m_item
.mimetype();
240 m_isVideo
= mimeType
.startsWith(QLatin1String("video/"));
241 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
);
248 // if the video is playing, has been paused or stopped
249 // we don't need to update the preview/phonon widget states
250 // unless the previewed file has changed,
251 // or the setting previewshown has changed
252 if ((m_phononWidget
->state() != Phonon::State::PlayingState
&&
253 m_phononWidget
->state() != Phonon::State::PausedState
&&
254 m_phononWidget
->state() != Phonon::State::StoppedState
) ||
255 m_item
.targetUrl() != m_phononWidget
->url() ||
256 (!m_preview
->isVisible() &&! m_phononWidget
->isVisible())) {
258 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
259 // hides the preview now to avoid flickering when the autoplay video starts
262 // the video won't play before the preview is displayed
266 m_phononWidget
->show();
267 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
268 adjustWidgetSizes(parentWidget()->width());
271 // When we don't need it, hide the phonon widget first to avoid flickering
272 m_phononWidget
->hide();
274 m_preview
->removeEventFilter(m_phononWidget
);
275 m_phononWidget
->clearUrl();
280 m_phononWidget
->hide();
284 void InformationPanelContent::configureShownProperties()
286 m_configureLabel
->setVisible(true);
287 m_configureButtons
->setVisible(true);
288 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
291 void InformationPanelContent::refreshMetaData()
293 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
294 m_metaDataWidget
->show();
295 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
298 void InformationPanelContent::showItems(const KFileItemList
& items
)
300 // If there is a preview job, kill it to prevent that we have jobs for
301 // multiple items running, and thus a race condition (bug 250787).
303 m_previewJob
->kill();
306 m_preview
->setPixmap(
307 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
309 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
311 m_metaDataWidget
->setItems(items
);
313 m_phononWidget
->hide();
315 m_item
= KFileItem();
318 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
320 switch (event
->type()) {
321 case QEvent::Resize
: {
322 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
323 if (obj
== m_metaDataArea
->viewport()) {
324 // The size of the meta text area has changed. Adjust the fixed
325 // width in a way that no horizontal scrollbar needs to be shown.
326 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
327 } else if (obj
== parent()) {
328 adjustWidgetSizes(resizeEvent
->size().width());
334 adjustWidgetSizes(parentWidget()->width());
337 case QEvent::FontChange
:
338 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
345 return QWidget::eventFilter(obj
, event
);
348 void InformationPanelContent::showIcon(const KFileItem
& item
)
350 m_outdatedPreviewTimer
->stop();
351 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
352 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
353 m_preview
->setPixmap(pixmap
);
356 void InformationPanelContent::showPreview(const KFileItem
& item
,
357 const QPixmap
& pixmap
)
359 m_outdatedPreviewTimer
->stop();
362 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
367 // compute relative pixel positions
368 const int zeroX
= static_cast<int>(p
.width() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
369 const int zeroY
= static_cast<int>(p
.height() / 2 - PLAY_ARROW_SIZE
/ 2 / devicePixelRatio());
372 arrow
<< QPoint(zeroX
, zeroY
);
373 arrow
<< QPoint(zeroX
, zeroY
+ PLAY_ARROW_SIZE
);
374 arrow
<< QPoint(zeroX
+ PLAY_ARROW_SIZE
, zeroY
+ PLAY_ARROW_SIZE
/ 2);
377 path
.addPolygon(arrow
);
379 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
380 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
382 QColor whiteColor
= Qt::white
;
383 QColor blackColor
= Qt::black
;
384 gradient
.setColorAt(0, whiteColor
);
385 gradient
.setColorAt(1, blackColor
);
387 QBrush
brush(gradient
);
389 QPainter
painter(&p
);
391 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
394 painter
.setRenderHint(QPainter::Antialiasing
);
395 painter
.drawPolygon(arrow
);
396 painter
.fillPath(path
, brush
);
399 m_preview
->setPixmap(p
);
402 void InformationPanelContent::markOutdatedPreview()
404 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
405 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
406 KIconLoader::Desktop
,
407 KIconLoader::DisabledState
);
408 m_preview
->setPixmap(disabledPixmap
);
411 KFileItemList
InformationPanelContent::items()
413 return m_metaDataWidget
->items();
416 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
418 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
419 if (m_preview
->isVisible() && m_preview
->size().width() != m_preview
->pixmap().size().width()) {
420 // in case the information panel has been resized when the preview was not displayed
421 // we need to refresh its content
426 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
427 m_phononWidget
->setAutoPlay(autoPlay
);
430 void InformationPanelContent::setNameLabelText(const QString
& text
)
432 QTextOption textOption
;
433 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
435 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
437 QTextLayout
textLayout(processedText
);
438 textLayout
.setFont(m_nameLabel
->font());
439 textLayout
.setTextOption(textOption
);
442 wrappedText
.reserve(processedText
.length());
444 // wrap the text to fit into the width of m_nameLabel
445 textLayout
.beginLayout();
446 QTextLine line
= textLayout
.createLine();
447 while (line
.isValid()) {
448 line
.setLineWidth(m_nameLabel
->width());
449 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
451 line
= textLayout
.createLine();
452 if (line
.isValid()) {
453 wrappedText
+= QChar::LineSeparator
;
456 textLayout
.endLayout();
458 m_nameLabel
->setText(wrappedText
);
461 void InformationPanelContent::adjustWidgetSizes(int width
)
463 // If the text inside the name label or the info label cannot
464 // get wrapped, then the maximum width of the label is increased
465 // so that the width of the information panel gets increased.
466 // To prevent this, the maximum width is adjusted to
467 // the current width of the panel.
468 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
469 m_nameLabel
->setMaximumWidth(maxWidth
);
471 // The metadata widget also contains a text widget which may return
472 // a large preferred width.
473 m_metaDataWidget
->setMaximumWidth(maxWidth
);
475 // try to increase the preview as large as possible
476 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
478 if (m_phononWidget
->isVisible()) {
479 // assure that the size of the video player is the same as the preview size
480 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));