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 <KFilePlacesModel>
26 #include <KGlobalSettings>
27 #include <KIO/JobUiDelegate>
28 #include <KIO/PreviewJob>
29 #include <KIconEffect>
30 #include <KIconLoader>
33 #include <kseparator.h>
34 #include <KStringHandler>
36 #include <Phonon/BackendCapabilities>
37 #include <Phonon/MediaObject>
38 #include <Phonon/SeekSlider>
44 #include <QResizeEvent>
45 #include <QScrollArea>
46 #include <QTextDocument>
47 #include <QTextLayout>
50 #include <QVBoxLayout>
52 #include "dolphin_informationpanelsettings.h"
53 #include "filemetadataconfigurationdialog.h"
54 #include "phononwidget.h"
55 #include "pixmapviewer.h"
56 #include "views/dolphinplacesmodel.h"
58 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
61 m_pendingPreview(false),
62 m_outdatedPreviewTimer(0),
69 parent
->installEventFilter(this);
71 // Initialize timer for disabling an outdated preview with a small
72 // delay. This prevents flickering if the new preview can be generated
73 // within a very small timeframe.
74 m_outdatedPreviewTimer
= new QTimer(this);
75 m_outdatedPreviewTimer
->setInterval(300);
76 m_outdatedPreviewTimer
->setSingleShot(true);
77 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
78 this, SLOT(markOutdatedPreview()));
80 QVBoxLayout
* layout
= new QVBoxLayout(this);
81 layout
->setSpacing(KDialog::spacingHint());
84 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
86 m_preview
= new PixmapViewer(parent
);
87 m_preview
->setMinimumWidth(minPreviewWidth
);
88 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
90 m_phononWidget
= new PhononWidget(parent
);
91 m_phononWidget
->hide();
92 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
93 connect(m_phononWidget
, SIGNAL(hasVideoChanged(bool)),
94 this, SLOT(slotHasVideoChanged(bool)));
97 m_nameLabel
= new QLabel(parent
);
98 QFont font
= m_nameLabel
->font();
100 m_nameLabel
->setFont(font
);
101 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
102 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
104 const bool previewsShown
= InformationPanelSettings::previewsShown();
105 m_preview
->setVisible(previewsShown
);
107 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
108 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
109 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
110 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
112 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
113 // at the bottom. This prevents that the meta data widget gets vertically stretched
114 // in the case where the height of m_metaDataArea > m_metaDataWidget.
115 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
116 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
117 containerLayout
->setContentsMargins(0, 0, 0, 0);
118 containerLayout
->setSpacing(0);
119 containerLayout
->addWidget(m_metaDataWidget
);
120 containerLayout
->addStretch();
122 m_metaDataArea
= new QScrollArea(parent
);
123 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
124 m_metaDataArea
->setWidgetResizable(true);
125 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
127 QWidget
* viewport
= m_metaDataArea
->viewport();
128 viewport
->installEventFilter(this);
130 QPalette palette
= viewport
->palette();
131 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
132 viewport
->setPalette(palette
);
134 layout
->addWidget(m_preview
);
135 layout
->addWidget(m_phononWidget
);
136 layout
->addWidget(m_nameLabel
);
137 layout
->addWidget(new KSeparator());
138 layout
->addWidget(m_metaDataArea
);
141 InformationPanelContent::~InformationPanelContent()
143 InformationPanelSettings::self()->writeConfig();
146 void InformationPanelContent::showItem(const KFileItem
& item
)
148 m_pendingPreview
= false;
150 const KUrl itemUrl
= item
.url();
151 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
152 if (!applyPlace(itemUrl
)) {
153 setNameLabelText(item
.text());
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 KIconLoader iconLoader
;
158 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
159 KIconLoader::NoGroup
,
160 KIconLoader::SizeEnormous
);
161 m_preview
->setPixmap(icon
);
163 // try to get a preview pixmap from the item...
164 m_pendingPreview
= true;
166 // Mark the currently shown preview as outdated. This is done
167 // with a small delay to prevent a flickering when the next preview
168 // can be shown within a short timeframe. This timer is not started
169 // for directories, as directory previews might fail and return the
172 m_outdatedPreviewTimer
->start();
175 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
176 job
->setScaleType(KIO::PreviewJob::Unscaled
);
177 job
->setIgnoreMaximumSize(item
.isLocalFile());
179 job
->ui()->setWindow(this);
182 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
183 this, SLOT(showPreview(KFileItem
,QPixmap
)));
184 connect(job
, SIGNAL(failed(KFileItem
)),
185 this, SLOT(showIcon(KFileItem
)));
189 if (m_metaDataWidget
) {
190 m_metaDataWidget
->show();
191 m_metaDataWidget
->setItems(KFileItemList() << item
);
194 if (InformationPanelSettings::previewsShown()) {
195 const QString mimeType
= item
.mimetype();
196 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
197 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
198 // thinks it supports PNG images
200 m_phononWidget
->show();
201 m_phononWidget
->setUrl(item
.targetUrl());
202 if (m_preview
->isVisible()) {
203 m_phononWidget
->setVideoSize(m_preview
->size());
206 m_phononWidget
->hide();
207 m_preview
->setVisible(true);
210 m_phononWidget
->hide();
216 void InformationPanelContent::showItems(const KFileItemList
& items
)
218 m_pendingPreview
= false;
220 KIconLoader iconLoader
;
221 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
222 KIconLoader::NoGroup
,
223 KIconLoader::SizeEnormous
);
224 m_preview
->setPixmap(icon
);
225 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
227 if (m_metaDataWidget
) {
228 m_metaDataWidget
->setItems(items
);
231 m_phononWidget
->hide();
233 m_item
= KFileItem();
236 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
238 switch (event
->type()) {
239 case QEvent::Resize
: {
240 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
241 if (obj
== m_metaDataArea
->viewport()) {
242 // The size of the meta text area has changed. Adjust the fixed
243 // width in a way that no horizontal scrollbar needs to be shown.
244 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
245 } else if (obj
== parent()) {
246 adjustWidgetSizes(resizeEvent
->size().width());
252 adjustWidgetSizes(parentWidget()->width());
259 return QWidget::eventFilter(obj
, event
);
262 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
266 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
267 previewAction
->setIcon(KIcon("view-preview"));
268 previewAction
->setCheckable(true);
269 previewAction
->setChecked(InformationPanelSettings::previewsShown());
271 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
272 configureAction
->setIcon(KIcon("configure"));
274 popup
.addSeparator();
275 foreach (QAction
* action
, customContextMenuActions
) {
276 popup
.addAction(action
);
279 // Open the popup and adjust the settings for the
281 QAction
* action
= popup
.exec(QCursor::pos());
286 const bool isChecked
= action
->isChecked();
287 if (action
== previewAction
) {
288 m_preview
->setVisible(isChecked
);
289 InformationPanelSettings::setPreviewsShown(isChecked
);
290 } else if (action
== configureAction
) {
291 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
292 dialog
->setDescription(i18nc("@label::textbox",
293 "Select which data should be shown in the information panel:"));
294 dialog
->setItems(m_metaDataWidget
->items());
295 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
298 dialog
->activateWindow();
299 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
303 void InformationPanelContent::showIcon(const KFileItem
& item
)
305 m_outdatedPreviewTimer
->stop();
306 m_pendingPreview
= false;
307 if (!applyPlace(item
.targetUrl())) {
308 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
309 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
313 void InformationPanelContent::showPreview(const KFileItem
& item
,
314 const QPixmap
& pixmap
)
316 m_outdatedPreviewTimer
->stop();
318 if (m_pendingPreview
) {
320 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
321 m_preview
->setPixmap(p
);
322 m_pendingPreview
= false;
326 void InformationPanelContent::markOutdatedPreview()
328 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
329 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
330 KIconLoader::Desktop
,
331 KIconLoader::DisabledState
);
332 m_preview
->setPixmap(disabledPixmap
);
335 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
337 m_preview
->setVisible(!hasVideo
);
340 void InformationPanelContent::refreshMetaData()
342 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
347 bool InformationPanelContent::applyPlace(const KUrl
& url
)
349 KFilePlacesModel
* placesModel
= DolphinPlacesModel::instance();
350 const int count
= placesModel
->rowCount();
352 for (int i
= 0; i
< count
; ++i
) {
353 QModelIndex index
= placesModel
->index(i
, 0);
355 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
356 setNameLabelText(placesModel
->text(index
));
357 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
365 void InformationPanelContent::setNameLabelText(const QString
& text
)
367 QTextOption textOption
;
368 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
370 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
372 QTextLayout
textLayout(processedText
);
373 textLayout
.setFont(m_nameLabel
->font());
374 textLayout
.setTextOption(textOption
);
377 wrappedText
.reserve(processedText
.length());
379 // wrap the text to fit into the width of m_nameLabel
380 textLayout
.beginLayout();
381 QTextLine line
= textLayout
.createLine();
382 while (line
.isValid()) {
383 line
.setLineWidth(m_nameLabel
->width());
384 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
386 line
= textLayout
.createLine();
387 if (line
.isValid()) {
388 wrappedText
+= QChar::LineSeparator
;
391 textLayout
.endLayout();
393 m_nameLabel
->setText(wrappedText
);
396 void InformationPanelContent::adjustWidgetSizes(int width
)
398 // If the text inside the name label or the info label cannot
399 // get wrapped, then the maximum width of the label is increased
400 // so that the width of the information panel gets increased.
401 // To prevent this, the maximum width is adjusted to
402 // the current width of the panel.
403 const int maxWidth
= width
- KDialog::spacingHint() * 4;
404 m_nameLabel
->setMaximumWidth(maxWidth
);
406 // The metadata widget also contains a text widget which may return
407 // a large preferred width.
408 if (m_metaDataWidget
) {
409 m_metaDataWidget
->setMaximumWidth(maxWidth
);
412 // try to increase the preview as large as possible
413 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
415 if (m_phononWidget
->isVisible()) {
416 // assure that the size of the video player is the same as the preview size
417 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
421 #include "informationpanelcontent.moc"