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"
23 #include <KIO/JobUiDelegate>
24 #include <KIO/PreviewJob>
25 #include <KJobWidgets>
26 #include <KIconEffect>
27 #include <KIconLoader>
29 #include <KLocalizedString>
32 #include <KStringHandler>
33 #include <QTextDocument>
36 #include <KFileMetaDataWidget>
38 #include <Baloo/FileMetaDataWidget>
41 #include <panels/places/placesitem.h>
42 #include <panels/places/placesitemmodel.h>
44 #include <Phonon/BackendCapabilities>
45 #include <Phonon/MediaObject>
46 #include <Phonon/SeekSlider>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextLayout>
56 #include <QVBoxLayout>
57 #include <QFontDatabase>
60 #include "dolphin_informationpanelsettings.h"
61 #include "filemetadataconfigurationdialog.h"
62 #include "phononwidget.h"
63 #include "pixmapviewer.h"
65 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
69 m_outdatedPreviewTimer(0),
77 parent
->installEventFilter(this);
79 // Initialize timer for disabling an outdated preview with a small
80 // delay. This prevents flickering if the new preview can be generated
81 // within a very small timeframe.
82 m_outdatedPreviewTimer
= new QTimer(this);
83 m_outdatedPreviewTimer
->setInterval(300);
84 m_outdatedPreviewTimer
->setSingleShot(true);
85 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
86 this, &InformationPanelContent::markOutdatedPreview
);
88 QVBoxLayout
* layout
= new QVBoxLayout(this);
91 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
93 m_preview
= new PixmapViewer(parent
);
94 m_preview
->setMinimumWidth(minPreviewWidth
);
95 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
97 m_phononWidget
= new PhononWidget(parent
);
98 m_phononWidget
->hide();
99 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
100 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
101 this, &InformationPanelContent::slotHasVideoChanged
);
104 m_nameLabel
= new QLabel(parent
);
105 QFont font
= m_nameLabel
->font();
107 m_nameLabel
->setFont(font
);
108 m_nameLabel
->setTextFormat(Qt::PlainText
);
109 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
110 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 const bool previewsShown
= InformationPanelSettings::previewsShown();
113 m_preview
->setVisible(previewsShown
);
116 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
117 connect(m_metaDataWidget
, &KFileMetaDataWidget::urlActivated
,
118 this, &InformationPanelContent::urlActivated
);
120 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
121 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
122 this, &InformationPanelContent::urlActivated
);
124 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
125 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
127 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
128 // at the bottom. This prevents that the meta data widget gets vertically stretched
129 // in the case where the height of m_metaDataArea > m_metaDataWidget.
130 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
131 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
132 containerLayout
->setContentsMargins(0, 0, 0, 0);
133 containerLayout
->setSpacing(0);
134 containerLayout
->addWidget(m_metaDataWidget
);
135 containerLayout
->addStretch();
137 m_metaDataArea
= new QScrollArea(parent
);
138 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
139 m_metaDataArea
->setWidgetResizable(true);
140 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
142 QWidget
* viewport
= m_metaDataArea
->viewport();
143 viewport
->installEventFilter(this);
145 layout
->addWidget(m_preview
);
146 layout
->addWidget(m_phononWidget
);
147 layout
->addWidget(m_nameLabel
);
148 layout
->addWidget(new KSeparator());
149 layout
->addWidget(m_metaDataArea
);
151 m_placesItemModel
= new PlacesItemModel(this);
154 InformationPanelContent::~InformationPanelContent()
156 InformationPanelSettings::self()->save();
159 void InformationPanelContent::showItem(const KFileItem
& item
)
161 // If there is a preview job, kill it to prevent that we have jobs for
162 // multiple items running, and thus a race condition (bug 250787).
164 m_previewJob
->kill();
167 const QUrl itemUrl
= item
.url();
168 const bool isSearchUrl
= itemUrl
.scheme().contains(QStringLiteral("search")) && item
.localPath().isEmpty();
169 if (!applyPlace(itemUrl
)) {
170 setNameLabelText(item
.text());
172 // in the case of a search-URL the URL is not readable for humans
173 // (at least not useful to show in the Information Panel)
174 KIconLoader iconLoader
;
175 QPixmap icon
= iconLoader
.loadIcon(QStringLiteral("nepomuk"),
176 KIconLoader::NoGroup
,
177 KIconLoader::SizeEnormous
);
178 m_preview
->setPixmap(icon
);
180 // try to get a preview pixmap from the item...
182 // Mark the currently shown preview as outdated. This is done
183 // with a small delay to prevent a flickering when the next preview
184 // can be shown within a short timeframe. This timer is not started
185 // for directories, as directory previews might fail and return the
188 m_outdatedPreviewTimer
->start();
191 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
192 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
193 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
194 if (m_previewJob
->ui()) {
195 KJobWidgets::setWindow(m_previewJob
, this);
198 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
199 this, &InformationPanelContent::showPreview
);
200 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
201 this, &InformationPanelContent::showIcon
);
205 if (m_metaDataWidget
) {
206 m_metaDataWidget
->show();
207 m_metaDataWidget
->setItems(KFileItemList() << item
);
210 if (InformationPanelSettings::previewsShown()) {
211 const QString mimeType
= item
.mimetype();
212 const bool usePhonon
= mimeType
.startsWith(QLatin1String("audio/")) || mimeType
.startsWith(QLatin1String("video/"));
214 m_phononWidget
->show();
215 m_phononWidget
->setUrl(item
.targetUrl());
216 if (m_preview
->isVisible()) {
217 m_phononWidget
->setVideoSize(m_preview
->size());
220 m_phononWidget
->hide();
221 m_preview
->setVisible(true);
224 m_phononWidget
->hide();
230 void InformationPanelContent::showItems(const KFileItemList
& items
)
232 // If there is a preview job, kill it to prevent that we have jobs for
233 // multiple items running, and thus a race condition (bug 250787).
235 m_previewJob
->kill();
238 KIconLoader iconLoader
;
239 QPixmap icon
= iconLoader
.loadIcon(QStringLiteral("dialog-information"),
240 KIconLoader::NoGroup
,
241 KIconLoader::SizeEnormous
);
242 m_preview
->setPixmap(icon
);
243 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
245 if (m_metaDataWidget
) {
246 m_metaDataWidget
->setItems(items
);
249 m_phononWidget
->hide();
251 m_item
= KFileItem();
254 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
256 switch (event
->type()) {
257 case QEvent::Resize
: {
258 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
259 if (obj
== m_metaDataArea
->viewport()) {
260 // The size of the meta text area has changed. Adjust the fixed
261 // width in a way that no horizontal scrollbar needs to be shown.
262 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
263 } else if (obj
== parent()) {
264 adjustWidgetSizes(resizeEvent
->size().width());
270 adjustWidgetSizes(parentWidget()->width());
273 case QEvent::FontChange
:
274 m_metaDataWidget
->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont
));
281 return QWidget::eventFilter(obj
, event
);
284 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
288 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
289 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
290 previewAction
->setCheckable(true);
291 previewAction
->setChecked(InformationPanelSettings::previewsShown());
293 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
294 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
296 popup
.addSeparator();
297 foreach (QAction
* action
, customContextMenuActions
) {
298 popup
.addAction(action
);
301 // Open the popup and adjust the settings for the
303 QAction
* action
= popup
.exec(QCursor::pos());
308 const bool isChecked
= action
->isChecked();
309 if (action
== previewAction
) {
310 m_preview
->setVisible(isChecked
);
311 InformationPanelSettings::setPreviewsShown(isChecked
);
312 } else if (action
== configureAction
) {
313 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
314 dialog
->setDescription(i18nc("@label::textbox",
315 "Select which data should be shown in the information panel:"));
316 dialog
->setItems(m_metaDataWidget
->items());
317 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
319 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
323 void InformationPanelContent::showIcon(const KFileItem
& item
)
325 m_outdatedPreviewTimer
->stop();
326 if (!applyPlace(item
.targetUrl())) {
327 const QPixmap icon
= KIconLoader::global()->loadIcon(item
.iconName(), KIconLoader::Desktop
,
328 KIconLoader::SizeEnormous
, KIconLoader::DefaultState
,
330 m_preview
->setPixmap(icon
);
334 void InformationPanelContent::showPreview(const KFileItem
& item
,
335 const QPixmap
& pixmap
)
337 m_outdatedPreviewTimer
->stop();
341 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
342 m_preview
->setPixmap(p
);
345 void InformationPanelContent::markOutdatedPreview()
347 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
348 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
349 KIconLoader::Desktop
,
350 KIconLoader::DisabledState
);
351 m_preview
->setPixmap(disabledPixmap
);
354 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
356 m_preview
->setVisible(!hasVideo
);
359 void InformationPanelContent::refreshMetaData()
361 if (!m_item
.isNull()) {
366 bool InformationPanelContent::applyPlace(const QUrl
& url
)
368 const int count
= m_placesItemModel
->count();
369 for (int i
= 0; i
< count
; ++i
) {
370 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
371 if (item
->url().matches(url
, QUrl::StripTrailingSlash
)) {
372 setNameLabelText(item
->text());
373 m_preview
->setPixmap(QIcon::fromTheme(item
->icon()).pixmap(128, 128));
381 void InformationPanelContent::setNameLabelText(const QString
& text
)
383 QTextOption textOption
;
384 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
386 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
388 QTextLayout
textLayout(processedText
);
389 textLayout
.setFont(m_nameLabel
->font());
390 textLayout
.setTextOption(textOption
);
393 wrappedText
.reserve(processedText
.length());
395 // wrap the text to fit into the width of m_nameLabel
396 textLayout
.beginLayout();
397 QTextLine line
= textLayout
.createLine();
398 while (line
.isValid()) {
399 line
.setLineWidth(m_nameLabel
->width());
400 wrappedText
+= processedText
.midRef(line
.textStart(), line
.textLength());
402 line
= textLayout
.createLine();
403 if (line
.isValid()) {
404 wrappedText
+= QChar::LineSeparator
;
407 textLayout
.endLayout();
409 m_nameLabel
->setText(wrappedText
);
412 void InformationPanelContent::adjustWidgetSizes(int width
)
414 // If the text inside the name label or the info label cannot
415 // get wrapped, then the maximum width of the label is increased
416 // so that the width of the information panel gets increased.
417 // To prevent this, the maximum width is adjusted to
418 // the current width of the panel.
419 const int maxWidth
= width
- style()->layoutSpacing(QSizePolicy::DefaultType
, QSizePolicy::DefaultType
, Qt::Horizontal
) * 4;
420 m_nameLabel
->setMaximumWidth(maxWidth
);
422 // The metadata widget also contains a text widget which may return
423 // a large preferred width.
424 if (m_metaDataWidget
) {
425 m_metaDataWidget
->setMaximumWidth(maxWidth
);
428 // try to increase the preview as large as possible
429 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
431 if (m_phononWidget
->isVisible()) {
432 // assure that the size of the video player is the same as the preview size
433 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));