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>
34 #include <nepomuk2/filemetadatawidget.h>
36 #include <panels/places/placesitem.h>
37 #include <panels/places/placesitemmodel.h>
39 #include <Phonon/BackendCapabilities>
40 #include <Phonon/MediaObject>
41 #include <Phonon/SeekSlider>
47 #include <QResizeEvent>
48 #include <QScrollArea>
49 #include <QTextDocument>
50 #include <QTextLayout>
53 #include <QVBoxLayout>
55 #include "dolphin_informationpanelsettings.h"
56 #include "filemetadataconfigurationdialog.h"
57 #include "phononwidget.h"
58 #include "pixmapviewer.h"
60 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
63 m_pendingPreview(false),
64 m_outdatedPreviewTimer(0),
72 parent
->installEventFilter(this);
74 // Initialize timer for disabling an outdated preview with a small
75 // delay. This prevents flickering if the new preview can be generated
76 // within a very small timeframe.
77 m_outdatedPreviewTimer
= new QTimer(this);
78 m_outdatedPreviewTimer
->setInterval(300);
79 m_outdatedPreviewTimer
->setSingleShot(true);
80 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
81 this, SLOT(markOutdatedPreview()));
83 QVBoxLayout
* layout
= new QVBoxLayout(this);
84 layout
->setSpacing(KDialog::spacingHint());
87 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
89 m_preview
= new PixmapViewer(parent
);
90 m_preview
->setMinimumWidth(minPreviewWidth
);
91 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
93 m_phononWidget
= new PhononWidget(parent
);
94 m_phononWidget
->hide();
95 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
96 connect(m_phononWidget
, SIGNAL(hasVideoChanged(bool)),
97 this, SLOT(slotHasVideoChanged(bool)));
100 m_nameLabel
= new QLabel(parent
);
101 QFont font
= m_nameLabel
->font();
103 m_nameLabel
->setFont(font
);
104 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
105 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
107 const bool previewsShown
= InformationPanelSettings::previewsShown();
108 m_preview
->setVisible(previewsShown
);
110 m_metaDataWidget
= new Nepomuk2::FileMetaDataWidget(parent
);
111 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
112 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
113 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
115 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
116 // at the bottom. This prevents that the meta data widget gets vertically stretched
117 // in the case where the height of m_metaDataArea > m_metaDataWidget.
118 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
119 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
120 containerLayout
->setContentsMargins(0, 0, 0, 0);
121 containerLayout
->setSpacing(0);
122 containerLayout
->addWidget(m_metaDataWidget
);
123 containerLayout
->addStretch();
125 m_metaDataArea
= new QScrollArea(parent
);
126 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
127 m_metaDataArea
->setWidgetResizable(true);
128 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
130 QWidget
* viewport
= m_metaDataArea
->viewport();
131 viewport
->installEventFilter(this);
133 QPalette palette
= viewport
->palette();
134 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
135 viewport
->setPalette(palette
);
137 layout
->addWidget(m_preview
);
138 layout
->addWidget(m_phononWidget
);
139 layout
->addWidget(m_nameLabel
);
140 layout
->addWidget(new KSeparator());
141 layout
->addWidget(m_metaDataArea
);
143 m_placesItemModel
= new PlacesItemModel(this);
146 InformationPanelContent::~InformationPanelContent()
148 InformationPanelSettings::self()->writeConfig();
151 void InformationPanelContent::showItem(const KFileItem
& item
)
153 m_pendingPreview
= false;
155 const KUrl itemUrl
= item
.url();
156 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
157 if (!applyPlace(itemUrl
)) {
158 setNameLabelText(item
.text());
160 // in the case of a search-URL the URL is not readable for humans
161 // (at least not useful to show in the Information Panel)
162 KIconLoader iconLoader
;
163 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
164 KIconLoader::NoGroup
,
165 KIconLoader::SizeEnormous
);
166 m_preview
->setPixmap(icon
);
168 // try to get a preview pixmap from the item...
169 m_pendingPreview
= true;
171 // Mark the currently shown preview as outdated. This is done
172 // with a small delay to prevent a flickering when the next preview
173 // can be shown within a short timeframe. This timer is not started
174 // for directories, as directory previews might fail and return the
177 m_outdatedPreviewTimer
->start();
180 KIO::PreviewJob
* job
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
181 job
->setScaleType(KIO::PreviewJob::Unscaled
);
182 job
->setIgnoreMaximumSize(item
.isLocalFile());
184 job
->ui()->setWindow(this);
187 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
188 this, SLOT(showPreview(KFileItem
,QPixmap
)));
189 connect(job
, SIGNAL(failed(KFileItem
)),
190 this, SLOT(showIcon(KFileItem
)));
194 if (m_metaDataWidget
) {
195 m_metaDataWidget
->show();
196 m_metaDataWidget
->setItems(KFileItemList() << item
);
199 if (InformationPanelSettings::previewsShown()) {
200 const QString mimeType
= item
.mimetype();
201 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
203 m_phononWidget
->show();
204 m_phononWidget
->setUrl(item
.targetUrl());
205 if (m_preview
->isVisible()) {
206 m_phononWidget
->setVideoSize(m_preview
->size());
209 m_phononWidget
->hide();
210 m_preview
->setVisible(true);
213 m_phononWidget
->hide();
219 void InformationPanelContent::showItems(const KFileItemList
& items
)
221 m_pendingPreview
= false;
223 KIconLoader iconLoader
;
224 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
225 KIconLoader::NoGroup
,
226 KIconLoader::SizeEnormous
);
227 m_preview
->setPixmap(icon
);
228 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
230 if (m_metaDataWidget
) {
231 m_metaDataWidget
->setItems(items
);
234 m_phononWidget
->hide();
236 m_item
= KFileItem();
239 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
241 switch (event
->type()) {
242 case QEvent::Resize
: {
243 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
244 if (obj
== m_metaDataArea
->viewport()) {
245 // The size of the meta text area has changed. Adjust the fixed
246 // width in a way that no horizontal scrollbar needs to be shown.
247 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
248 } else if (obj
== parent()) {
249 adjustWidgetSizes(resizeEvent
->size().width());
255 adjustWidgetSizes(parentWidget()->width());
262 return QWidget::eventFilter(obj
, event
);
265 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
269 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
270 previewAction
->setIcon(KIcon("view-preview"));
271 previewAction
->setCheckable(true);
272 previewAction
->setChecked(InformationPanelSettings::previewsShown());
274 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
275 configureAction
->setIcon(KIcon("configure"));
277 popup
.addSeparator();
278 foreach (QAction
* action
, customContextMenuActions
) {
279 popup
.addAction(action
);
282 // Open the popup and adjust the settings for the
284 QAction
* action
= popup
.exec(QCursor::pos());
289 const bool isChecked
= action
->isChecked();
290 if (action
== previewAction
) {
291 m_preview
->setVisible(isChecked
);
292 InformationPanelSettings::setPreviewsShown(isChecked
);
293 } else if (action
== configureAction
) {
294 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
295 dialog
->setDescription(i18nc("@label::textbox",
296 "Select which data should be shown in the information panel:"));
297 dialog
->setItems(m_metaDataWidget
->items());
298 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
301 dialog
->activateWindow();
302 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
306 void InformationPanelContent::showIcon(const KFileItem
& item
)
308 m_outdatedPreviewTimer
->stop();
309 m_pendingPreview
= false;
310 if (!applyPlace(item
.targetUrl())) {
311 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
312 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
316 void InformationPanelContent::showPreview(const KFileItem
& item
,
317 const QPixmap
& pixmap
)
319 m_outdatedPreviewTimer
->stop();
321 if (m_pendingPreview
) {
323 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
324 m_preview
->setPixmap(p
);
325 m_pendingPreview
= false;
329 void InformationPanelContent::markOutdatedPreview()
331 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
332 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
333 KIconLoader::Desktop
,
334 KIconLoader::DisabledState
);
335 m_preview
->setPixmap(disabledPixmap
);
338 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
340 m_preview
->setVisible(!hasVideo
);
343 void InformationPanelContent::refreshMetaData()
345 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
350 bool InformationPanelContent::applyPlace(const KUrl
& url
)
352 const int count
= m_placesItemModel
->count();
353 for (int i
= 0; i
< count
; ++i
) {
354 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
355 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
356 setNameLabelText(item
->text());
357 m_preview
->setPixmap(KIcon(item
->icon()).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"