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
) {
172 m_preview
->stopAnimatedImage();
178 void InformationPanelContent::refreshPixmapView()
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).
183 m_previewJob
->kill();
186 // try to get a preview pixmap from the item...
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
193 if (!m_item
.isDir()) {
194 m_outdatedPreviewTimer
->start();
197 QStringList plugins
= KIO::PreviewJob::availablePlugins();
198 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
199 QSize(m_preview
->width(), m_preview
->height()),
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);
207 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
208 this, &InformationPanelContent::showPreview
);
209 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
210 this, &InformationPanelContent::showIcon
);
213 void InformationPanelContent::refreshPreview()
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).
218 m_previewJob
->kill();
221 m_preview
->setCursor(Qt::ArrowCursor
);
222 bool usePhonon
= false;
223 setNameLabelText(m_item
.text());
224 if (InformationPanelSettings::previewsShown()) {
226 const QUrl itemUrl
= m_item
.url();
227 const bool isSearchUrl
= itemUrl
.scheme().contains(QLatin1String("search")) && m_item
.localPath().isEmpty();
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
)
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/"));
246 // change the cursor of the preview
247 m_preview
->setCursor(Qt::PointingHandCursor
);
248 m_preview
->installEventFilter(m_phononWidget
);
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())) {
260 if (InformationPanelSettings::previewsAutoPlay() && m_isVideo
) {
261 // hides the preview now to avoid flickering when the autoplay video starts
264 // the video won't play before the preview is displayed
268 m_phononWidget
->show();
269 m_phononWidget
->setUrl(m_item
.targetUrl(), m_isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
270 adjustWidgetSizes(parentWidget()->width());
273 if (isAnimatedImage
) {
274 m_preview
->setAnimatedImageFileName(itemUrl
.toLocalFile());
276 // When we don't need it, hide the phonon widget first to avoid flickering
277 m_phononWidget
->hide();
279 m_preview
->removeEventFilter(m_phononWidget
);
280 m_phononWidget
->clearUrl();
284 m_preview
->stopAnimatedImage();
286 m_phononWidget
->hide();
290 void InformationPanelContent::configureShownProperties()
292 m_configureLabel
->setVisible(true);
293 m_configureButtons
->setVisible(true);
294 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
297 void InformationPanelContent::refreshMetaData()
299 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
300 m_metaDataWidget
->show();
301 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
304 void InformationPanelContent::showItems(const KFileItemList
& items
)
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).
309 m_previewJob
->kill();
312 m_preview
->stopAnimatedImage();
314 m_preview
->setPixmap(
315 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
317 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
319 m_metaDataWidget
->setItems(items
);
321 m_phononWidget
->hide();
323 m_item
= KFileItem();
326 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
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());
342 adjustWidgetSizes(parentWidget()->width());
345 case QEvent::FontChange
:
346 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
353 return QWidget::eventFilter(obj
, event
);
356 void InformationPanelContent::showIcon(const KFileItem
& item
)
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
);
364 void InformationPanelContent::showPreview(const KFileItem
& item
,
365 const QPixmap
& pixmap
)
367 m_outdatedPreviewTimer
->stop();
370 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
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());
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);
385 path
.addPolygon(arrow
);
387 QLinearGradient
gradient(QPointF(zeroX
, zeroY
),
388 QPointF(zeroX
+ PLAY_ARROW_SIZE
,zeroY
+ PLAY_ARROW_SIZE
));
390 QColor whiteColor
= Qt::white
;
391 QColor blackColor
= Qt::black
;
392 gradient
.setColorAt(0, whiteColor
);
393 gradient
.setColorAt(1, blackColor
);
395 QBrush
brush(gradient
);
397 QPainter
painter(&p
);
399 QPen
pen(blackColor
, PLAY_ARROW_BORDER_SIZE
, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
);
402 painter
.setRenderHint(QPainter::Antialiasing
);
403 painter
.drawPolygon(arrow
);
404 painter
.fillPath(path
, brush
);
407 m_preview
->setPixmap(p
);
410 void InformationPanelContent::markOutdatedPreview()
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
);
419 KFileItemList
InformationPanelContent::items()
421 return m_metaDataWidget
->items();
424 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
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
434 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
435 m_phononWidget
->setAutoPlay(autoPlay
);
438 void InformationPanelContent::setNameLabelText(const QString
& text
)
440 QTextOption textOption
;
441 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
443 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
445 QTextLayout
textLayout(processedText
);
446 textLayout
.setFont(m_nameLabel
->font());
447 textLayout
.setTextOption(textOption
);
450 wrappedText
.reserve(processedText
.length());
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());
459 line
= textLayout
.createLine();
460 if (line
.isValid()) {
461 wrappedText
+= QChar::LineSeparator
;
464 textLayout
.endLayout();
466 m_nameLabel
->setText(wrappedText
);
469 void InformationPanelContent::adjustWidgetSizes(int width
)
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
);
479 // The metadata widget also contains a text widget which may return
480 // a large preferred width.
481 m_metaDataWidget
->setMaximumWidth(maxWidth
);
483 // try to increase the preview as large as possible
484 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
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
));