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 <QScrollArea>
44 #include <QTextLayout>
46 #include <QVBoxLayout>
49 #include "dolphin_informationpanelsettings.h"
50 #include "phononwidget.h"
51 #include "pixmapviewer.h"
53 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
56 m_previewJob(nullptr),
57 m_outdatedPreviewTimer(nullptr),
59 m_phononWidget(nullptr),
61 m_metaDataWidget(nullptr),
62 m_metaDataArea(nullptr),
63 m_placesItemModel(nullptr)
65 parent
->installEventFilter(this);
67 // Initialize timer for disabling an outdated preview with a small
68 // delay. This prevents flickering if the new preview can be generated
69 // within a very small timeframe.
70 m_outdatedPreviewTimer
= new QTimer(this);
71 m_outdatedPreviewTimer
->setInterval(300);
72 m_outdatedPreviewTimer
->setSingleShot(true);
73 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
74 this, &InformationPanelContent::markOutdatedPreview
);
76 QVBoxLayout
* layout
= new QVBoxLayout(this);
79 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
81 m_preview
= new PixmapViewer(parent
);
82 m_preview
->setMinimumWidth(minPreviewWidth
);
83 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
85 m_phononWidget
= new PhononWidget(parent
);
86 m_phononWidget
->hide();
87 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
88 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
89 this, &InformationPanelContent::slotHasVideoChanged
);
92 m_nameLabel
= new QLabel(parent
);
93 QFont font
= m_nameLabel
->font();
95 m_nameLabel
->setFont(font
);
96 m_nameLabel
->setTextFormat(Qt::PlainText
);
97 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
98 m_nameLabel
->setSizePolicy(QSizePolicy::Ignored
, QSizePolicy::Fixed
);
100 const bool previewsShown
= InformationPanelSettings::previewsShown();
101 m_preview
->setVisible(previewsShown
);
103 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
104 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
105 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
106 this, &InformationPanelContent::urlActivated
);
107 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
108 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
110 m_metaDataArea
= new QScrollArea(parent
);
111 m_metaDataArea
->setWidget(m_metaDataWidget
);
112 m_metaDataArea
->setWidgetResizable(true);
113 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
115 QWidget
* viewport
= m_metaDataArea
->viewport();
116 viewport
->installEventFilter(this);
118 layout
->addWidget(m_preview
);
119 layout
->addWidget(m_phononWidget
);
120 layout
->addWidget(m_nameLabel
);
121 layout
->addWidget(new KSeparator());
122 layout
->addWidget(m_metaDataArea
);
124 m_placesItemModel
= new PlacesItemModel(this);
127 InformationPanelContent::~InformationPanelContent()
129 InformationPanelSettings::self()->save();
132 void InformationPanelContent::showItem(const KFileItem
& item
)
140 void InformationPanelContent::refreshPreview()
142 // If there is a preview job, kill it to prevent that we have jobs for
143 // multiple items running, and thus a race condition (bug 250787).
145 m_previewJob
->kill();
148 setNameLabelText(m_item
.text());
149 if (InformationPanelSettings::previewsShown()) {
152 const QUrl itemUrl
= m_item
.url();
153 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && m_item
.localPath().isEmpty();
155 // in the case of a search-URL the URL is not readable for humans
156 // (at least not useful to show in the Information Panel)
157 m_preview
->setPixmap(
158 QIcon::fromTheme(QStringLiteral("nepomuk")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
161 // try to get a preview pixmap from the item...
163 // Mark the currently shown preview as outdated. This is done
164 // with a small delay to prevent a flickering when the next preview
165 // can be shown within a short timeframe. This timer is not started
166 // for directories, as directory previews might fail and return the
168 if (!m_item
.isDir()) {
169 m_outdatedPreviewTimer
->start();
172 QStringList plugins
= KIO::PreviewJob::availablePlugins();
173 m_previewJob
= new KIO::PreviewJob(KFileItemList() << m_item
,
174 QSize(m_preview
->width(), m_preview
->height()),
176 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
177 m_previewJob
->setIgnoreMaximumSize(m_item
.isLocalFile());
178 if (m_previewJob
->uiDelegate()) {
179 KJobWidgets::setWindow(m_previewJob
, this);
182 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
183 this, &InformationPanelContent::showPreview
);
184 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
185 this, &InformationPanelContent::showIcon
);
187 const QString mimeType
= m_item
.mimetype();
188 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
190 m_phononWidget
->show();
191 m_phononWidget
->setUrl(m_item
.targetUrl());
192 m_phononWidget
->setVideoSize(m_preview
->size());
194 m_phononWidget
->hide();
199 m_phononWidget
->hide();
203 void InformationPanelContent::refreshMetaData()
205 m_metaDataWidget
->setDateFormat(static_cast<Baloo::DateFormats
>(InformationPanelSettings::dateFormat()));
206 m_metaDataWidget
->show();
207 m_metaDataWidget
->setItems(KFileItemList() << m_item
);
210 void InformationPanelContent::showItems(const KFileItemList
& items
)
212 // If there is a preview job, kill it to prevent that we have jobs for
213 // multiple items running, and thus a race condition (bug 250787).
215 m_previewJob
->kill();
218 m_preview
->setPixmap(
219 QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
)
221 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
223 m_metaDataWidget
->setItems(items
);
225 m_phononWidget
->hide();
227 m_item
= KFileItem();
230 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
232 switch (event
->type()) {
233 case QEvent::Resize
: {
234 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
235 if (obj
== m_metaDataArea
->viewport()) {
236 // The size of the meta text area has changed. Adjust the fixed
237 // width in a way that no horizontal scrollbar needs to be shown.
238 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
239 } else if (obj
== parent()) {
240 adjustWidgetSizes(resizeEvent
->size().width());
246 adjustWidgetSizes(parentWidget()->width());
249 case QEvent::FontChange
:
250 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
257 return QWidget::eventFilter(obj
, event
);
260 void InformationPanelContent::showIcon(const KFileItem
& item
)
262 m_outdatedPreviewTimer
->stop();
263 QPixmap pixmap
= QIcon::fromTheme(item
.iconName()).pixmap(KIconLoader::SizeEnormous
, KIconLoader::SizeEnormous
);
264 KIconLoader::global()->drawOverlays(item
.overlays(), pixmap
, KIconLoader::Desktop
);
265 m_preview
->setPixmap(pixmap
);
268 void InformationPanelContent::showPreview(const KFileItem
& item
,
269 const QPixmap
& pixmap
)
271 m_outdatedPreviewTimer
->stop();
275 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
276 m_preview
->setPixmap(p
);
279 void InformationPanelContent::markOutdatedPreview()
281 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
282 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
283 KIconLoader::Desktop
,
284 KIconLoader::DisabledState
);
285 m_preview
->setPixmap(disabledPixmap
);
288 KFileItemList
InformationPanelContent::items()
290 return m_metaDataWidget
->items();
293 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
295 m_preview
->setVisible(InformationPanelSettings::previewsShown() && !hasVideo
);
298 void InformationPanelContent::setNameLabelText(const QString
& text
)
300 QTextOption textOption
;
301 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
303 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
305 QTextLayout
textLayout(processedText
);
306 textLayout
.setFont(m_nameLabel
->font());
307 textLayout
.setTextOption(textOption
);
310 wrappedText
.reserve(processedText
.length());
312 // wrap the text to fit into the width of m_nameLabel
313 textLayout
.beginLayout();
314 QTextLine line
= textLayout
.createLine();
315 while (line
.isValid()) {
316 line
.setLineWidth(m_nameLabel
->width());
317 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
319 line
= textLayout
.createLine();
320 if (line
.isValid()) {
321 wrappedText
+= QChar::LineSeparator
;
324 textLayout
.endLayout();
326 m_nameLabel
->setText(wrappedText
);
329 void InformationPanelContent::adjustWidgetSizes(int width
)
331 // If the text inside the name label or the info label cannot
332 // get wrapped, then the maximum width of the label is increased
333 // so that the width of the information panel gets increased.
334 // To prevent this, the maximum width is adjusted to
335 // the current width of the panel.
336 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
337 m_nameLabel
->setMaximumWidth(maxWidth
);
339 // The metadata widget also contains a text widget which may return
340 // a large preferred width.
341 m_metaDataWidget
->setMaximumWidth(maxWidth
);
343 // try to increase the preview as large as possible
344 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
346 if (m_phononWidget
->isVisible()) {
347 // assure that the size of the video player is the same as the preview size
348 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));