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 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
90 this, &InformationPanelContent::slotHasVideoChanged
);
93 m_nameLabel
= new QLabel(parent
);
94 QFont font
= m_nameLabel
->font();
96 m_nameLabel
->setFont(font
);
97 m_nameLabel
->setTextFormat(Qt::PlainText
);
98 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
99 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
101 const bool previewsShown
= InformationPanelSettings::previewsShown();
102 m_preview
->setVisible(previewsShown
);
104 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
105 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
106 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
107 this, &InformationPanelContent::urlActivated
);
108 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
109 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
112 m_configureLabel
= new QLabel(i18nc("@label::textbox",
113 "Select which data should be shown:"), this);
114 m_configureLabel
->setWordWrap(true);
115 m_configureLabel
->setVisible(false);
117 m_configureButtons
= new QDialogButtonBox(QDialogButtonBox::Save
| QDialogButtonBox::Cancel
);
118 m_configureButtons
->setVisible(false);
119 connect(m_configureButtons
, &QDialogButtonBox::accepted
, this, [this]() {
120 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Accept
);
121 m_configureButtons
->setVisible(false);
122 m_configureLabel
->setVisible(false);
123 emit
configurationFinished();
126 connect(m_configureButtons
, &QDialogButtonBox::rejected
, this, [this]() {
127 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::Cancel
);
128 m_configureButtons
->setVisible(false);
129 m_configureLabel
->setVisible(false);
130 emit
configurationFinished();
134 m_metaDataArea
= new QScrollArea(parent
);
135 m_metaDataArea
->setWidget(m_metaDataWidget
);
136 m_metaDataArea
->setWidgetResizable(true);
137 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
139 QWidget
* viewport
= m_metaDataArea
->viewport();
140 viewport
->installEventFilter(this);
142 layout
->addWidget(m_preview
);
143 layout
->addWidget(m_phononWidget
);
144 layout
->addWidget(m_nameLabel
);
145 layout
->addWidget(new KSeparator());
146 layout
->addWidget(m_configureLabel
);
147 layout
->addWidget(m_metaDataArea
);
148 layout
->addWidget(m_configureButtons
);
150 m_placesItemModel
= new PlacesItemModel(this);
153 InformationPanelContent::~InformationPanelContent()
155 InformationPanelSettings::self()->save();
158 void InformationPanelContent::showItem(const KFileItem
& item
)
166 void InformationPanelContent::refreshPreview()
168 // If there is a preview job, kill it to prevent that we have jobs for
169 // multiple items running, and thus a race condition (bug 250787).
171 m_previewJob
->kill();
174 setNameLabelText(m_item
.text());
175 if (InformationPanelSettings::previewsShown()) {
178 const QUrl itemUrl
= m_item
.url();
179 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && m_item
.localPath().isEmpty();
181 // in the case of a search-URL the URL is not readable for humans
182 // (at least not useful to show in the Information Panel)
183 m_preview
->setPixmap(
184 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
187 // try to get a preview pixmap from the item...
189 // Mark the currently shown preview as outdated. This is done
190 // with a small delay to prevent a flickering when the next preview
191 // can be shown within a short timeframe. This timer is not started
192 // for directories, as directory previews might fail and return the
194 if (!m_item
.isDir()) {
195 m_outdatedPreviewTimer
->start();
198 QStringList plugins
= KIO::PreviewJob::availablePlugins();
199 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
200 QSize(m_preview
->width(), m_preview
->height()),
202 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
203 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
204 if (m_previewJob
->uiDelegate()) {
205 KJobWidgets::setWindow(m_previewJob
, this);
208 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
209 this, &InformationPanelContent::showPreview
);
210 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
211 this, &InformationPanelContent::showIcon
);
213 const QString mimeType
= m_item
.mimetype();
214 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
216 m_phononWidget
->show();
217 m_phononWidget
->setUrl(m_item
.targetUrl());
218 m_phononWidget
->setVideoSize(m_preview
->size());
220 m_phononWidget
->hide();
225 m_phononWidget
->hide();
229 void InformationPanelContent::configureShownProperties()
231 m_configureLabel
->setVisible(true);
232 m_configureButtons
->setVisible(true);
233 m_metaDataWidget
->setConfigurationMode(Baloo::ConfigurationMode::ReStart
);
236 void InformationPanelContent::refreshMetaData()
238 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
239 m_metaDataWidget
->show();
240 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
243 void InformationPanelContent::showItems(const KFileItemList
& items
)
245 // If there is a preview job, kill it to prevent that we have jobs for
246 // multiple items running, and thus a race condition (bug 250787).
248 m_previewJob
->kill();
251 m_preview
->setPixmap(
252 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
254 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
256 m_metaDataWidget
->setItems(items
);
258 m_phononWidget
->hide();
260 m_item
= KFileItem();
263 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
265 switch (event
->type()) {
266 case QEvent::Resize
: {
267 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
268 if (obj
== m_metaDataArea
->viewport()) {
269 // The size of the meta text area has changed. Adjust the fixed
270 // width in a way that no horizontal scrollbar needs to be shown.
271 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
272 } else if (obj
== parent()) {
273 adjustWidgetSizes(resizeEvent
->size().width());
279 adjustWidgetSizes(parentWidget()->width());
282 case QEvent::FontChange
:
283 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
290 return QWidget::eventFilter(obj
, event
);
293 void InformationPanelContent::showIcon(const KFileItem
& item
)
295 m_outdatedPreviewTimer
->stop();
296 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
297 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
298 m_preview
->setPixmap(pixmap
);
301 void InformationPanelContent::showPreview(const KFileItem
& item
,
302 const QPixmap
& pixmap
)
304 m_outdatedPreviewTimer
->stop();
308 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
309 m_preview
->setPixmap(p
);
312 void InformationPanelContent::markOutdatedPreview()
314 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
315 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
316 KIconLoader::Desktop
,
317 KIconLoader::DisabledState
);
318 m_preview
->setPixmap(disabledPixmap
);
321 KFileItemList
InformationPanelContent::items()
323 return m_metaDataWidget
->items();
326 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
328 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
331 void InformationPanelContent::setNameLabelText(const QString
& text
)
333 QTextOption textOption
;
334 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
336 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
338 QTextLayout
textLayout(processedText
);
339 textLayout
.setFont(m_nameLabel
->font());
340 textLayout
.setTextOption(textOption
);
343 wrappedText
.reserve(processedText
.length());
345 // wrap the text to fit into the width of m_nameLabel
346 textLayout
.beginLayout();
347 QTextLine line
= textLayout
.createLine();
348 while (line
.isValid()) {
349 line
.setLineWidth(m_nameLabel
->width());
350 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
352 line
= textLayout
.createLine();
353 if (line
.isValid()) {
354 wrappedText
+= QChar::LineSeparator
;
357 textLayout
.endLayout();
359 m_nameLabel
->setText(wrappedText
);
362 void InformationPanelContent::adjustWidgetSizes(int width
)
364 // If the text inside the name label or the info label cannot
365 // get wrapped, then the maximum width of the label is increased
366 // so that the width of the information panel gets increased.
367 // To prevent this, the maximum width is adjusted to
368 // the current width of the panel.
369 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
370 m_nameLabel
->setMaximumWidth(maxWidth
);
372 // The metadata widget also contains a text widget which may return
373 // a large preferred width.
374 m_metaDataWidget
->setMaximumWidth(maxWidth
);
376 // try to increase the preview as large as possible
377 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
379 if (m_phononWidget
->isVisible()) {
380 // assure that the size of the video player is the same as the preview size
381 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));