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>
50 #include "dolphin_informationpanelsettings.h"
51 #include "phononwidget.h"
52 #include "pixmapviewer.h"
54 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
57 m_previewJob(nullptr),
58 m_outdatedPreviewTimer(nullptr),
60 m_phononWidget(nullptr),
62 m_metaDataWidget(nullptr),
63 m_metaDataArea(nullptr),
64 m_placesItemModel(nullptr)
66 parent
->installEventFilter(this);
68 // Initialize timer for disabling an outdated preview with a small
69 // delay. This prevents flickering if the new preview can be generated
70 // within a very small timeframe.
71 m_outdatedPreviewTimer
= new QTimer(this);
72 m_outdatedPreviewTimer
->setInterval(300);
73 m_outdatedPreviewTimer
->setSingleShot(true);
74 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
75 this, &InformationPanelContent::markOutdatedPreview
);
77 QVBoxLayout
* layout
= new QVBoxLayout(this);
80 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
82 m_preview
= new PixmapViewer(parent
);
83 m_preview
->setMinimumWidth(minPreviewWidth
);
84 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
86 m_phononWidget
= new PhononWidget(parent
);
87 m_phononWidget
->hide();
88 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
89 m_phononWidget
->setAutoPlay(InformationPanelSettings::previewsAutoPlay());
90 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
91 this, &InformationPanelContent::slotHasVideoChanged
);
94 m_nameLabel
= new QLabel(parent
);
95 QFont font
= m_nameLabel
->font();
97 m_nameLabel
->setFont(font
);
98 m_nameLabel
->setTextFormat(Qt::PlainText
);
99 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
100 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
102 const bool previewsShown
= InformationPanelSettings::previewsShown();
103 m_preview
->setVisible(previewsShown
);
105 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
106 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
107 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
108 this, &InformationPanelContent::urlActivated
);
109 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
110 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
113 m_configureLabel
= new QLabel(i18nc("@label::textbox",
114 "Select which data should be shown:"), this);
115 m_configureLabel
->setWordWrap(true);
116 m_configureLabel
->setVisible(false);
118 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
119 m_configureButtons
->setVisible(false);
120 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
121 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
122 m_configureButtons
->setVisible(false);
123 m_configureLabel
->setVisible(false);
124 emit
configurationFinished();
127 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
128 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
129 m_configureButtons
->setVisible(false);
130 m_configureLabel
->setVisible(false);
131 emit
configurationFinished();
135 m_metaDataArea
= new QScrollArea(parent
);
136 m_metaDataArea
->setWidget(m_metaDataWidget
);
137 m_metaDataArea
->setWidgetResizable(true);
138 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
140 QWidget
* viewport
= m_metaDataArea
->viewport();
141 viewport
->installEventFilter(this);
143 layout
->addWidget(m_preview
);
144 layout
->addWidget(m_phononWidget
);
145 layout
->addWidget(m_nameLabel
);
146 layout
->addWidget(new KSeparator());
147 layout
->addWidget(m_configureLabel
);
148 layout
->addWidget(m_metaDataArea
);
149 layout
->addWidget(m_configureButtons
);
151 m_placesItemModel
= new PlacesItemModel(this);
154 InformationPanelContent::~InformationPanelContent()
156 InformationPanelSettings::self()->save();
159 void InformationPanelContent::showItem(const KFileItem
& item
)
161 if (item
!= m_item
) {
169 void InformationPanelContent::refreshPreview()
171 // If there is a preview job, kill it to prevent that we have jobs for
172 // multiple items running, and thus a race condition (bug 250787).
174 m_previewJob
->kill();
177 setNameLabelText(m_item
.text());
178 if (InformationPanelSettings::previewsShown()) {
180 const QUrl itemUrl
= m_item
.url();
181 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && m_item
.localPath().isEmpty();
185 // in the case of a search-URL the URL is not readable for humans
186 // (at least not useful to show in the Information Panel)
187 m_preview
->setPixmap(
188 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
191 // try to get a preview pixmap from the item...
193 // Mark the currently shown preview as outdated. This is done
194 // with a small delay to prevent a flickering when the next preview
195 // can be shown within a short timeframe. This timer is not started
196 // for directories, as directory previews might fail and return the
198 if (!m_item
.isDir()) {
199 m_outdatedPreviewTimer
->start();
202 QStringList plugins
= KIO::PreviewJob::availablePlugins();
203 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
204 QSize(m_preview
->width(), m_preview
->height()),
206 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
207 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
208 if (m_previewJob
->uiDelegate()) {
209 KJobWidgets::setWindow(m_previewJob
, this);
212 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
213 this, &InformationPanelContent::showPreview
);
214 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
215 this, &InformationPanelContent::showIcon
);
217 const QString mimeType
= m_item
.mimetype();
218 const bool isVideo
= mimeType
.startsWith(QLatin1String("video/"));
219 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || isVideo
;
223 if (InformationPanelSettings::previewsAutoPlay() && isVideo
) {
224 // hides the preview now to avoid flickering when the autoplay video starts
227 // the video won't play before the preview is displayed
231 m_phononWidget
->show();
232 m_phononWidget
->setUrl(m_item
.targetUrl(), isVideo
? PhononWidget::MediaKind::Video
: PhononWidget::MediaKind::Audio
);
233 m_phononWidget
->setVideoSize(m_preview
->size());
235 // When we don't need it, hide the phonon widget first to avoid flickering
236 m_phononWidget
->hide();
242 m_phononWidget
->hide();
246 void InformationPanelContent::configureShownProperties()
248 m_configureLabel
->setVisible(true);
249 m_configureButtons
->setVisible(true);
250 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
253 void InformationPanelContent::refreshMetaData()
255 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
256 m_metaDataWidget
->show();
257 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
260 void InformationPanelContent::showItems(const KFileItemList
& items
)
262 // If there is a preview job, kill it to prevent that we have jobs for
263 // multiple items running, and thus a race condition (bug 250787).
265 m_previewJob
->kill();
268 m_preview
->setPixmap(
269 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
271 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
273 m_metaDataWidget
->setItems(items
);
275 m_phononWidget
->hide();
277 m_item
= KFileItem();
280 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
282 switch (event
->type()) {
283 case QEvent::Resize
: {
284 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
285 if (obj
== m_metaDataArea
->viewport()) {
286 // The size of the meta text area has changed. Adjust the fixed
287 // width in a way that no horizontal scrollbar needs to be shown.
288 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
289 } else if (obj
== parent()) {
290 adjustWidgetSizes(resizeEvent
->size().width());
296 adjustWidgetSizes(parentWidget()->width());
299 case QEvent::FontChange
:
300 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
307 return QWidget::eventFilter(obj
, event
);
310 void InformationPanelContent::showIcon(const KFileItem
& item
)
312 m_outdatedPreviewTimer
->stop();
313 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
314 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
315 m_preview
->setPixmap(pixmap
);
318 void InformationPanelContent::showPreview(const KFileItem
& item
,
319 const QPixmap
& pixmap
)
321 m_outdatedPreviewTimer
->stop();
325 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
326 m_preview
->setPixmap(p
);
329 void InformationPanelContent::markOutdatedPreview()
331 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
332 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
333 KIconLoader::Desktop
,
334 KIconLoader::DisabledState
);
335 m_preview
->setPixmap(disabledPixmap
);
338 KFileItemList
InformationPanelContent::items()
340 return m_metaDataWidget
->items();
343 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
345 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
348 void InformationPanelContent::setPreviewAutoPlay(bool autoPlay
) {
349 m_phononWidget
->setAutoPlay(autoPlay
);
352 void InformationPanelContent::setNameLabelText(const QString
& text
)
354 QTextOption textOption
;
355 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
357 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
359 QTextLayout
textLayout(processedText
);
360 textLayout
.setFont(m_nameLabel
->font());
361 textLayout
.setTextOption(textOption
);
364 wrappedText
.reserve(processedText
.length());
366 // wrap the text to fit into the width of m_nameLabel
367 textLayout
.beginLayout();
368 QTextLine line
= textLayout
.createLine();
369 while (line
.isValid()) {
370 line
.setLineWidth(m_nameLabel
->width());
371 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
373 line
= textLayout
.createLine();
374 if (line
.isValid()) {
375 wrappedText
+= QChar::LineSeparator
;
378 textLayout
.endLayout();
380 m_nameLabel
->setText(wrappedText
);
383 void InformationPanelContent::adjustWidgetSizes(int width
)
385 // If the text inside the name label or the info label cannot
386 // get wrapped, then the maximum width of the label is increased
387 // so that the width of the information panel gets increased.
388 // To prevent this, the maximum width is adjusted to
389 // the current width of the panel.
390 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
391 m_nameLabel
->setMaximumWidth(maxWidth
);
393 // The metadata widget also contains a text widget which may return
394 // a large preferred width.
395 m_metaDataWidget
->setMaximumWidth(maxWidth
);
397 // try to increase the preview as large as possible
398 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
400 if (m_phononWidget
->isVisible()) {
401 // assure that the size of the video player is the same as the preview size
402 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));