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 <KIconEffect>
28 #include <KIconLoader>
31 #include <kseparator.h>
32 #include <KStringHandler>
35 #include <KFileMetaDataWidget>
37 #include <nepomuk2/filemetadatawidget.h>
40 #include <panels/places/placesitem.h>
41 #include <panels/places/placesitemmodel.h>
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextDocument>
54 #include <QTextLayout>
57 #include <QVBoxLayout>
59 #include "dolphin_informationpanelsettings.h"
60 #include "filemetadataconfigurationdialog.h"
61 #include "phononwidget.h"
62 #include "pixmapviewer.h"
64 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(0),
76 parent
->installEventFilter(this);
78 // Initialize timer for disabling an outdated preview with a small
79 // delay. This prevents flickering if the new preview can be generated
80 // within a very small timeframe.
81 m_outdatedPreviewTimer
= new QTimer(this);
82 m_outdatedPreviewTimer
->setInterval(300);
83 m_outdatedPreviewTimer
->setSingleShot(true);
84 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
85 this, SLOT(markOutdatedPreview()));
87 QVBoxLayout
* layout
= new QVBoxLayout(this);
88 layout
->setSpacing(KDialog::spacingHint());
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
, SIGNAL(hasVideoChanged(bool)),
101 this, SLOT(slotHasVideoChanged(bool)));
104 m_nameLabel
= new QLabel(parent
);
105 QFont font
= m_nameLabel
->font();
107 m_nameLabel
->setFont(font
);
108 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
109 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
111 const bool previewsShown
= InformationPanelSettings::previewsShown();
112 m_preview
->setVisible(previewsShown
);
115 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
117 m_metaDataWidget
= new Nepomuk2::FileMetaDataWidget(parent
);
119 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
120 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
121 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
123 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
124 // at the bottom. This prevents that the meta data widget gets vertically stretched
125 // in the case where the height of m_metaDataArea > m_metaDataWidget.
126 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
127 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
128 containerLayout
->setContentsMargins(0, 0, 0, 0);
129 containerLayout
->setSpacing(0);
130 containerLayout
->addWidget(m_metaDataWidget
);
131 containerLayout
->addStretch();
133 m_metaDataArea
= new QScrollArea(parent
);
134 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
135 m_metaDataArea
->setWidgetResizable(true);
136 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
138 QWidget
* viewport
= m_metaDataArea
->viewport();
139 viewport
->installEventFilter(this);
141 QPalette palette
= viewport
->palette();
142 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
143 viewport
->setPalette(palette
);
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()->writeConfig();
159 void InformationPanelContent::showItem(const KFileItem
& item
)
161 m_pendingPreview
= false;
163 const KUrl itemUrl
= item
.url();
164 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
165 if (!applyPlace(itemUrl
)) {
166 setNameLabelText(Qt::escape(item
.text()));
168 // in the case of a search-URL the URL is not readable for humans
169 // (at least not useful to show in the Information Panel)
170 KIconLoader iconLoader
;
171 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
172 KIconLoader::NoGroup
,
173 KIconLoader::SizeEnormous
);
174 m_preview
->setPixmap(icon
);
176 // try to get a preview pixmap from the item...
177 m_pendingPreview
= true;
179 // Mark the currently shown preview as outdated. This is done
180 // with a small delay to prevent a flickering when the next preview
181 // can be shown within a short timeframe. This timer is not started
182 // for directories, as directory previews might fail and return the
185 m_outdatedPreviewTimer
->start();
188 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
189 job
->setScaleType(KIO::PreviewJob::Unscaled
);
190 job
->setIgnoreMaximumSize(item
.isLocalFile());
192 job
->ui()->setWindow(this);
195 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
196 this, SLOT(showPreview(KFileItem
,QPixmap
)));
197 connect(job
, SIGNAL(failed(KFileItem
)),
198 this, SLOT(showIcon(KFileItem
)));
202 if (m_metaDataWidget
) {
203 m_metaDataWidget
->show();
204 m_metaDataWidget
->setItems(KFileItemList() << item
);
207 if (InformationPanelSettings::previewsShown()) {
208 const QString mimeType
= item
.mimetype();
209 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
211 m_phononWidget
->show();
212 m_phononWidget
->setUrl(item
.targetUrl());
213 if (m_preview
->isVisible()) {
214 m_phononWidget
->setVideoSize(m_preview
->size());
217 m_phononWidget
->hide();
218 m_preview
->setVisible(true);
221 m_phononWidget
->hide();
227 void InformationPanelContent::showItems(const KFileItemList
& items
)
229 m_pendingPreview
= false;
231 KIconLoader iconLoader
;
232 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
233 KIconLoader::NoGroup
,
234 KIconLoader::SizeEnormous
);
235 m_preview
->setPixmap(icon
);
236 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
238 if (m_metaDataWidget
) {
239 m_metaDataWidget
->setItems(items
);
242 m_phononWidget
->hide();
244 m_item
= KFileItem();
247 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
249 switch (event
->type()) {
250 case QEvent::Resize
: {
251 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
252 if (obj
== m_metaDataArea
->viewport()) {
253 // The size of the meta text area has changed. Adjust the fixed
254 // width in a way that no horizontal scrollbar needs to be shown.
255 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
256 } else if (obj
== parent()) {
257 adjustWidgetSizes(resizeEvent
->size().width());
263 adjustWidgetSizes(parentWidget()->width());
270 return QWidget::eventFilter(obj
, event
);
273 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
277 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
278 previewAction
->setIcon(KIcon("view-preview"));
279 previewAction
->setCheckable(true);
280 previewAction
->setChecked(InformationPanelSettings::previewsShown());
282 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
283 configureAction
->setIcon(KIcon("configure"));
285 popup
.addSeparator();
286 foreach (QAction
* action
, customContextMenuActions
) {
287 popup
.addAction(action
);
290 // Open the popup and adjust the settings for the
292 QAction
* action
= popup
.exec(QCursor::pos());
297 const bool isChecked
= action
->isChecked();
298 if (action
== previewAction
) {
299 m_preview
->setVisible(isChecked
);
300 InformationPanelSettings::setPreviewsShown(isChecked
);
301 } else if (action
== configureAction
) {
302 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
303 dialog
->setDescription(i18nc("@label::textbox",
304 "Select which data should be shown in the information panel:"));
305 dialog
->setItems(m_metaDataWidget
->items());
306 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
309 dialog
->activateWindow();
310 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
314 void InformationPanelContent::showIcon(const KFileItem
& item
)
316 m_outdatedPreviewTimer
->stop();
317 m_pendingPreview
= false;
318 if (!applyPlace(item
.targetUrl())) {
319 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
320 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
324 void InformationPanelContent::showPreview(const KFileItem
& item
,
325 const QPixmap
& pixmap
)
327 m_outdatedPreviewTimer
->stop();
329 if (m_pendingPreview
) {
331 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
332 m_preview
->setPixmap(p
);
333 m_pendingPreview
= false;
337 void InformationPanelContent::markOutdatedPreview()
339 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
340 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
341 KIconLoader::Desktop
,
342 KIconLoader::DisabledState
);
343 m_preview
->setPixmap(disabledPixmap
);
346 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
348 m_preview
->setVisible(!hasVideo
);
351 void InformationPanelContent::refreshMetaData()
353 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
358 bool InformationPanelContent::applyPlace(const KUrl
& url
)
360 const int count
= m_placesItemModel
->count();
361 for (int i
= 0; i
< count
; ++i
) {
362 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
363 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
364 setNameLabelText(Qt::escape(item
->text()));
365 m_preview
->setPixmap(KIcon(item
->icon()).pixmap(128, 128));
373 void InformationPanelContent::setNameLabelText(const QString
& text
)
375 QTextOption textOption
;
376 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
378 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
380 QTextLayout
textLayout(processedText
);
381 textLayout
.setFont(m_nameLabel
->font());
382 textLayout
.setTextOption(textOption
);
385 wrappedText
.reserve(processedText
.length());
387 // wrap the text to fit into the width of m_nameLabel
388 textLayout
.beginLayout();
389 QTextLine line
= textLayout
.createLine();
390 while (line
.isValid()) {
391 line
.setLineWidth(m_nameLabel
->width());
392 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
394 line
= textLayout
.createLine();
395 if (line
.isValid()) {
396 wrappedText
+= QChar::LineSeparator
;
399 textLayout
.endLayout();
401 m_nameLabel
->setText(wrappedText
);
404 void InformationPanelContent::adjustWidgetSizes(int width
)
406 // If the text inside the name label or the info label cannot
407 // get wrapped, then the maximum width of the label is increased
408 // so that the width of the information panel gets increased.
409 // To prevent this, the maximum width is adjusted to
410 // the current width of the panel.
411 const int maxWidth
= width
- KDialog::spacingHint() * 4;
412 m_nameLabel
->setMaximumWidth(maxWidth
);
414 // The metadata widget also contains a text widget which may return
415 // a large preferred width.
416 if (m_metaDataWidget
) {
417 m_metaDataWidget
->setMaximumWidth(maxWidth
);
420 // try to increase the preview as large as possible
421 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
423 if (m_phononWidget
->isVisible()) {
424 // assure that the size of the video player is the same as the preview size
425 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
429 #include "informationpanelcontent.moc"