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 <kfilemetadatawidget.h>
25 #include <KGlobalSettings>
26 #include <KIO/JobUiDelegate>
27 #include <KIO/PreviewJob>
28 #include <KIconEffect>
29 #include <KIconLoader>
32 #include <kseparator.h>
33 #include <KStringHandler>
35 #include <panels/places/placesitem.h>
36 #include <panels/places/placesitemmodel.h>
38 #include <Phonon/BackendCapabilities>
39 #include <Phonon/MediaObject>
40 #include <Phonon/SeekSlider>
46 #include <QResizeEvent>
47 #include <QScrollArea>
48 #include <QTextDocument>
49 #include <QTextLayout>
52 #include <QVBoxLayout>
54 #include "dolphin_informationpanelsettings.h"
55 #include "filemetadataconfigurationdialog.h"
56 #include "phononwidget.h"
57 #include "pixmapviewer.h"
59 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
62 m_pendingPreview(false),
63 m_outdatedPreviewTimer(0),
71 parent
->installEventFilter(this);
73 // Initialize timer for disabling an outdated preview with a small
74 // delay. This prevents flickering if the new preview can be generated
75 // within a very small timeframe.
76 m_outdatedPreviewTimer
= new QTimer(this);
77 m_outdatedPreviewTimer
->setInterval(300);
78 m_outdatedPreviewTimer
->setSingleShot(true);
79 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
80 this, SLOT(markOutdatedPreview()));
82 QVBoxLayout
* layout
= new QVBoxLayout(this);
83 layout
->setSpacing(KDialog::spacingHint());
86 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
88 m_preview
= new PixmapViewer(parent
);
89 m_preview
->setMinimumWidth(minPreviewWidth
);
90 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
92 m_phononWidget
= new PhononWidget(parent
);
93 m_phononWidget
->hide();
94 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
95 connect(m_phononWidget
, SIGNAL(hasVideoChanged(bool)),
96 this, SLOT(slotHasVideoChanged(bool)));
99 m_nameLabel
= new QLabel(parent
);
100 QFont font
= m_nameLabel
->font();
102 m_nameLabel
->setFont(font
);
103 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
104 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
106 const bool previewsShown
= InformationPanelSettings::previewsShown();
107 m_preview
->setVisible(previewsShown
);
109 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
110 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
111 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
112 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
114 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
115 // at the bottom. This prevents that the meta data widget gets vertically stretched
116 // in the case where the height of m_metaDataArea > m_metaDataWidget.
117 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
118 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
119 containerLayout
->setContentsMargins(0, 0, 0, 0);
120 containerLayout
->setSpacing(0);
121 containerLayout
->addWidget(m_metaDataWidget
);
122 containerLayout
->addStretch();
124 m_metaDataArea
= new QScrollArea(parent
);
125 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
126 m_metaDataArea
->setWidgetResizable(true);
127 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
129 QWidget
* viewport
= m_metaDataArea
->viewport();
130 viewport
->installEventFilter(this);
132 QPalette palette
= viewport
->palette();
133 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
134 viewport
->setPalette(palette
);
136 layout
->addWidget(m_preview
);
137 layout
->addWidget(m_phononWidget
);
138 layout
->addWidget(m_nameLabel
);
139 layout
->addWidget(new KSeparator());
140 layout
->addWidget(m_metaDataArea
);
142 m_placesItemModel
= new PlacesItemModel(this);
145 InformationPanelContent::~InformationPanelContent()
147 InformationPanelSettings::self()->writeConfig();
150 void InformationPanelContent::showItem(const KFileItem
& item
)
152 m_pendingPreview
= false;
154 const KUrl itemUrl
= item
.url();
155 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
156 if (!applyPlace(itemUrl
)) {
157 setNameLabelText(item
.text());
159 // in the case of a search-URL the URL is not readable for humans
160 // (at least not useful to show in the Information Panel)
161 KIconLoader iconLoader
;
162 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
163 KIconLoader::NoGroup
,
164 KIconLoader::SizeEnormous
);
165 m_preview
->setPixmap(icon
);
167 // try to get a preview pixmap from the item...
168 m_pendingPreview
= true;
170 // Mark the currently shown preview as outdated. This is done
171 // with a small delay to prevent a flickering when the next preview
172 // can be shown within a short timeframe. This timer is not started
173 // for directories, as directory previews might fail and return the
176 m_outdatedPreviewTimer
->start();
179 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
180 job
->setScaleType(KIO::PreviewJob::Unscaled
);
181 job
->setIgnoreMaximumSize(item
.isLocalFile());
183 job
->ui()->setWindow(this);
186 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
187 this, SLOT(showPreview(KFileItem
,QPixmap
)));
188 connect(job
, SIGNAL(failed(KFileItem
)),
189 this, SLOT(showIcon(KFileItem
)));
193 if (m_metaDataWidget
) {
194 m_metaDataWidget
->show();
195 m_metaDataWidget
->setItems(KFileItemList() << item
);
198 if (InformationPanelSettings::previewsShown()) {
199 const QString mimeType
= item
.mimetype();
200 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
202 m_phononWidget
->show();
203 m_phononWidget
->setUrl(item
.targetUrl());
204 if (m_preview
->isVisible()) {
205 m_phononWidget
->setVideoSize(m_preview
->size());
208 m_phononWidget
->hide();
209 m_preview
->setVisible(true);
212 m_phononWidget
->hide();
218 void InformationPanelContent::showItems(const KFileItemList
& items
)
220 m_pendingPreview
= false;
222 KIconLoader iconLoader
;
223 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
224 KIconLoader::NoGroup
,
225 KIconLoader::SizeEnormous
);
226 m_preview
->setPixmap(icon
);
227 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
229 if (m_metaDataWidget
) {
230 m_metaDataWidget
->setItems(items
);
233 m_phononWidget
->hide();
235 m_item
= KFileItem();
238 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
240 switch (event
->type()) {
241 case QEvent::Resize
: {
242 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
243 if (obj
== m_metaDataArea
->viewport()) {
244 // The size of the meta text area has changed. Adjust the fixed
245 // width in a way that no horizontal scrollbar needs to be shown.
246 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
247 } else if (obj
== parent()) {
248 adjustWidgetSizes(resizeEvent
->size().width());
254 adjustWidgetSizes(parentWidget()->width());
261 return QWidget::eventFilter(obj
, event
);
264 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
268 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
269 previewAction
->setIcon(KIcon("view-preview"));
270 previewAction
->setCheckable(true);
271 previewAction
->setChecked(InformationPanelSettings::previewsShown());
273 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
274 configureAction
->setIcon(KIcon("configure"));
276 popup
.addSeparator();
277 foreach (QAction
* action
, customContextMenuActions
) {
278 popup
.addAction(action
);
281 // Open the popup and adjust the settings for the
283 QAction
* action
= popup
.exec(QCursor::pos());
288 const bool isChecked
= action
->isChecked();
289 if (action
== previewAction
) {
290 m_preview
->setVisible(isChecked
);
291 InformationPanelSettings::setPreviewsShown(isChecked
);
292 } else if (action
== configureAction
) {
293 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
294 dialog
->setDescription(i18nc("@label::textbox",
295 "Select which data should be shown in the information panel:"));
296 dialog
->setItems(m_metaDataWidget
->items());
297 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
300 dialog
->activateWindow();
301 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
305 void InformationPanelContent::showIcon(const KFileItem
& item
)
307 m_outdatedPreviewTimer
->stop();
308 m_pendingPreview
= false;
309 if (!applyPlace(item
.targetUrl())) {
310 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
311 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
315 void InformationPanelContent::showPreview(const KFileItem
& item
,
316 const QPixmap
& pixmap
)
318 m_outdatedPreviewTimer
->stop();
320 if (m_pendingPreview
) {
322 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
323 m_preview
->setPixmap(p
);
324 m_pendingPreview
= false;
328 void InformationPanelContent::markOutdatedPreview()
330 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
331 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
332 KIconLoader::Desktop
,
333 KIconLoader::DisabledState
);
334 m_preview
->setPixmap(disabledPixmap
);
337 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
339 m_preview
->setVisible(!hasVideo
);
342 void InformationPanelContent::refreshMetaData()
344 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
349 bool InformationPanelContent::applyPlace(const KUrl
& url
)
351 const int count
= m_placesItemModel
->count();
352 for (int i
= 0; i
< count
; ++i
) {
353 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
354 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
355 setNameLabelText(item
->text());
356 m_preview
->setPixmap(KIcon(item
->icon()).pixmap(128, 128));
364 void InformationPanelContent::setNameLabelText(const QString
& text
)
366 QTextOption textOption
;
367 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
369 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
371 QTextLayout
textLayout(processedText
);
372 textLayout
.setFont(m_nameLabel
->font());
373 textLayout
.setTextOption(textOption
);
376 wrappedText
.reserve(processedText
.length());
378 // wrap the text to fit into the width of m_nameLabel
379 textLayout
.beginLayout();
380 QTextLine line
= textLayout
.createLine();
381 while (line
.isValid()) {
382 line
.setLineWidth(m_nameLabel
->width());
383 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
385 line
= textLayout
.createLine();
386 if (line
.isValid()) {
387 wrappedText
+= QChar::LineSeparator
;
390 textLayout
.endLayout();
392 m_nameLabel
->setText(wrappedText
);
395 void InformationPanelContent::adjustWidgetSizes(int width
)
397 // If the text inside the name label or the info label cannot
398 // get wrapped, then the maximum width of the label is increased
399 // so that the width of the information panel gets increased.
400 // To prevent this, the maximum width is adjusted to
401 // the current width of the panel.
402 const int maxWidth
= width
- KDialog::spacingHint() * 4;
403 m_nameLabel
->setMaximumWidth(maxWidth
);
405 // The metadata widget also contains a text widget which may return
406 // a large preferred width.
407 if (m_metaDataWidget
) {
408 m_metaDataWidget
->setMaximumWidth(maxWidth
);
411 // try to increase the preview as large as possible
412 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
414 if (m_phononWidget
->isVisible()) {
415 // assure that the size of the video player is the same as the preview size
416 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
420 #include "informationpanelcontent.moc"