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"
24 #include <KGlobalSettings>
25 #include <KIO/JobUiDelegate>
26 #include <KIO/PreviewJob>
27 #include <KJobWidgets>
28 #include <KIconEffect>
29 #include <KIconLoader>
33 #include <kseparator.h>
34 #include <KStringHandler>
37 #include <KFileMetaDataWidget>
39 #include <baloo/filemetadatawidget.h>
42 #include <panels/places/placesitem.h>
43 #include <panels/places/placesitemmodel.h>
45 #include <Phonon/BackendCapabilities>
46 #include <Phonon/MediaObject>
47 #include <Phonon/SeekSlider>
53 #include <QResizeEvent>
54 #include <QScrollArea>
55 #include <QTextDocument>
56 #include <QTextLayout>
59 #include <QVBoxLayout>
61 #include "dolphin_informationpanelsettings.h"
62 #include "filemetadataconfigurationdialog.h"
63 #include "phononwidget.h"
64 #include "pixmapviewer.h"
66 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
70 m_outdatedPreviewTimer(0),
78 parent
->installEventFilter(this);
80 // Initialize timer for disabling an outdated preview with a small
81 // delay. This prevents flickering if the new preview can be generated
82 // within a very small timeframe.
83 m_outdatedPreviewTimer
= new QTimer(this);
84 m_outdatedPreviewTimer
->setInterval(300);
85 m_outdatedPreviewTimer
->setSingleShot(true);
86 connect(m_outdatedPreviewTimer
, &QTimer::timeout
,
87 this, &InformationPanelContent::markOutdatedPreview
);
89 QVBoxLayout
* layout
= new QVBoxLayout(this);
90 layout
->setSpacing(KDialog::spacingHint());
93 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
95 m_preview
= new PixmapViewer(parent
);
96 m_preview
->setMinimumWidth(minPreviewWidth
);
97 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
99 m_phononWidget
= new PhononWidget(parent
);
100 m_phononWidget
->hide();
101 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
102 connect(m_phononWidget
, &PhononWidget::hasVideoChanged
,
103 this, &InformationPanelContent::slotHasVideoChanged
);
106 m_nameLabel
= new QLabel(parent
);
107 QFont font
= m_nameLabel
->font();
109 m_nameLabel
->setFont(font
);
110 m_nameLabel
->setTextFormat(Qt::PlainText
);
111 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
112 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
114 const bool previewsShown
= InformationPanelSettings::previewsShown();
115 m_preview
->setVisible(previewsShown
);
118 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
119 connect(m_metaDataWidget
, &KFileMetaDataWidget::urlActivated
,
120 this, &InformationPanelContent::urlActivated
);
122 m_metaDataWidget
= new Baloo::FileMetaDataWidget(parent
);
123 connect(m_metaDataWidget
, &Baloo::FileMetaDataWidget::urlActivated
,
124 this, &InformationPanelContent::urlActivated
);
126 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
127 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
129 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
130 // at the bottom. This prevents that the meta data widget gets vertically stretched
131 // in the case where the height of m_metaDataArea > m_metaDataWidget.
132 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
133 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
134 containerLayout
->setContentsMargins(0, 0, 0, 0);
135 containerLayout
->setSpacing(0);
136 containerLayout
->addWidget(m_metaDataWidget
);
137 containerLayout
->addStretch();
139 m_metaDataArea
= new QScrollArea(parent
);
140 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
141 m_metaDataArea
->setWidgetResizable(true);
142 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
144 QWidget
* viewport
= m_metaDataArea
->viewport();
145 viewport
->installEventFilter(this);
147 QPalette palette
= viewport
->palette();
148 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
149 viewport
->setPalette(palette
);
151 layout
->addWidget(m_preview
);
152 layout
->addWidget(m_phononWidget
);
153 layout
->addWidget(m_nameLabel
);
154 layout
->addWidget(new KSeparator());
155 layout
->addWidget(m_metaDataArea
);
157 m_placesItemModel
= new PlacesItemModel(this);
160 InformationPanelContent::~InformationPanelContent()
162 InformationPanelSettings::self()->writeConfig();
165 void InformationPanelContent::showItem(const KFileItem
& item
)
167 // If there is a preview job, kill it to prevent that we have jobs for
168 // multiple items running, and thus a race condition (bug 250787).
170 m_previewJob
->kill();
173 const KUrl itemUrl
= item
.url();
174 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.localPath().isEmpty();
175 if (!applyPlace(itemUrl
)) {
176 setNameLabelText(item
.text());
178 // in the case of a search-URL the URL is not readable for humans
179 // (at least not useful to show in the Information Panel)
180 KIconLoader iconLoader
;
181 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
182 KIconLoader::NoGroup
,
183 KIconLoader::SizeEnormous
);
184 m_preview
->setPixmap(icon
);
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
194 m_outdatedPreviewTimer
->start();
197 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
198 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
199 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
200 if (m_previewJob
->ui()) {
201 KJobWidgets::setWindow(m_previewJob
, this);
204 connect(m_previewJob
.data(), &KIO::PreviewJob::gotPreview
,
205 this, &InformationPanelContent::showPreview
);
206 connect(m_previewJob
.data(), &KIO::PreviewJob::failed
,
207 this, &InformationPanelContent::showIcon
);
211 if (m_metaDataWidget
) {
212 m_metaDataWidget
->show();
213 m_metaDataWidget
->setItems(KFileItemList() << item
);
216 if (InformationPanelSettings::previewsShown()) {
217 const QString mimeType
= item
.mimetype();
218 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
220 m_phononWidget
->show();
221 m_phononWidget
->setUrl(item
.targetUrl());
222 if (m_preview
->isVisible()) {
223 m_phononWidget
->setVideoSize(m_preview
->size());
226 m_phononWidget
->hide();
227 m_preview
->setVisible(true);
230 m_phononWidget
->hide();
236 void InformationPanelContent::showItems(const KFileItemList
& items
)
238 // If there is a preview job, kill it to prevent that we have jobs for
239 // multiple items running, and thus a race condition (bug 250787).
241 m_previewJob
->kill();
244 KIconLoader iconLoader
;
245 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
246 KIconLoader::NoGroup
,
247 KIconLoader::SizeEnormous
);
248 m_preview
->setPixmap(icon
);
249 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
251 if (m_metaDataWidget
) {
252 m_metaDataWidget
->setItems(items
);
255 m_phononWidget
->hide();
257 m_item
= KFileItem();
260 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
262 switch (event
->type()) {
263 case QEvent::Resize
: {
264 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
265 if (obj
== m_metaDataArea
->viewport()) {
266 // The size of the meta text area has changed. Adjust the fixed
267 // width in a way that no horizontal scrollbar needs to be shown.
268 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
269 } else if (obj
== parent()) {
270 adjustWidgetSizes(resizeEvent
->size().width());
276 adjustWidgetSizes(parentWidget()->width());
279 case QEvent::FontChange
:
280 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
287 return QWidget::eventFilter(obj
, event
);
290 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
294 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
295 previewAction
->setIcon(QIcon::fromTheme("view-preview"));
296 previewAction
->setCheckable(true);
297 previewAction
->setChecked(InformationPanelSettings::previewsShown());
299 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
300 configureAction
->setIcon(KIcon("configure"));
302 popup
.addSeparator();
303 foreach (QAction
* action
, customContextMenuActions
) {
304 popup
.addAction(action
);
307 // Open the popup and adjust the settings for the
309 QAction
* action
= popup
.exec(QCursor::pos());
314 const bool isChecked
= action
->isChecked();
315 if (action
== previewAction
) {
316 m_preview
->setVisible(isChecked
);
317 InformationPanelSettings::setPreviewsShown(isChecked
);
318 } else if (action
== configureAction
) {
319 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
320 dialog
->setDescription(i18nc("@label::textbox",
321 "Select which data should be shown in the information panel:"));
322 dialog
->setItems(m_metaDataWidget
->items());
323 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
326 dialog
->activateWindow();
327 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, this, &InformationPanelContent::refreshMetaData
);
331 void InformationPanelContent::showIcon(const KFileItem
& item
)
333 m_outdatedPreviewTimer
->stop();
334 if (!applyPlace(item
.targetUrl())) {
335 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
336 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
340 void InformationPanelContent::showPreview(const KFileItem
& item
,
341 const QPixmap
& pixmap
)
343 m_outdatedPreviewTimer
->stop();
347 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
348 m_preview
->setPixmap(p
);
351 void InformationPanelContent::markOutdatedPreview()
353 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
354 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
355 KIconLoader::Desktop
,
356 KIconLoader::DisabledState
);
357 m_preview
->setPixmap(disabledPixmap
);
360 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
362 m_preview
->setVisible(!hasVideo
);
365 void InformationPanelContent::refreshMetaData()
367 if (!m_item
.isNull()) {
372 bool InformationPanelContent::applyPlace(const KUrl
& url
)
374 const int count
= m_placesItemModel
->count();
375 for (int i
= 0; i
< count
; ++i
) {
376 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
377 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
378 setNameLabelText(item
->text());
379 m_preview
->setPixmap(KIcon(item
->icon()).pixmap(128, 128));
387 void InformationPanelContent::setNameLabelText(const QString
& text
)
389 QTextOption textOption
;
390 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
392 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
394 QTextLayout
textLayout(processedText
);
395 textLayout
.setFont(m_nameLabel
->font());
396 textLayout
.setTextOption(textOption
);
399 wrappedText
.reserve(processedText
.length());
401 // wrap the text to fit into the width of m_nameLabel
402 textLayout
.beginLayout();
403 QTextLine line
= textLayout
.createLine();
404 while (line
.isValid()) {
405 line
.setLineWidth(m_nameLabel
->width());
406 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
408 line
= textLayout
.createLine();
409 if (line
.isValid()) {
410 wrappedText
+= QChar::LineSeparator
;
413 textLayout
.endLayout();
415 m_nameLabel
->setText(wrappedText
);
418 void InformationPanelContent::adjustWidgetSizes(int width
)
420 // If the text inside the name label or the info label cannot
421 // get wrapped, then the maximum width of the label is increased
422 // so that the width of the information panel gets increased.
423 // To prevent this, the maximum width is adjusted to
424 // the current width of the panel.
425 const int maxWidth
= width
- KDialog::spacingHint() * 4;
426 m_nameLabel
->setMaximumWidth(maxWidth
);
428 // The metadata widget also contains a text widget which may return
429 // a large preferred width.
430 if (m_metaDataWidget
) {
431 m_metaDataWidget
->setMaximumWidth(maxWidth
);
434 // try to increase the preview as large as possible
435 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
437 if (m_phononWidget
->isVisible()) {
438 // assure that the size of the video player is the same as the preview size
439 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
443 #include "informationpanelcontent.moc"