1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
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"
22 #include <config-nepomuk.h>
25 #include <kfileitem.h>
26 #include <kfileplacesmodel.h>
27 #include <kio/previewjob.h>
28 #include <kiconeffect.h>
29 #include <kiconloader.h>
32 #include <kseparator.h>
34 #include <Phonon/BackendCapabilities>
35 #include <Phonon/MediaObject>
36 #include <Phonon/SeekSlider>
41 #include <QResizeEvent>
42 #include <QScrollArea>
43 #include <QTextLayout>
46 #include <QVBoxLayout>
48 #include "dolphin_informationpanelsettings.h"
49 #include "settings/dolphinsettings.h"
50 #include "kmetadatawidget.h"
51 #include "kmetadataconfigurationdialog.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
55 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
58 m_pendingPreview(false),
59 m_outdatedPreviewTimer(0),
62 m_previewSeparator(0),
67 parent
->installEventFilter(this);
69 // Initialize timer for disabling an outdated preview with a small
70 // delay. This prevents flickering if the new preview can be generated
71 // within a very small timeframe.
72 m_outdatedPreviewTimer
= new QTimer(this);
73 m_outdatedPreviewTimer
->setInterval(300);
74 m_outdatedPreviewTimer
->setSingleShot(true);
75 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
76 this, SLOT(markOutdatedPreview()));
78 QVBoxLayout
* layout
= new QVBoxLayout
;
79 layout
->setSpacing(KDialog::spacingHint());
82 m_nameLabel
= new QLabel(parent
);
83 QFont font
= m_nameLabel
->font();
85 m_nameLabel
->setFont(font
);
86 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
87 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
88 m_nameLabel
->setMaximumWidth(KIconLoader::SizeEnormous
);
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
->setMinimumWidth(minPreviewWidth
);
99 connect(m_phononWidget
, SIGNAL(playingStarted()),
100 this, SLOT(slotPlayingStarted()));
101 connect(m_phononWidget
, SIGNAL(playingStopped()),
102 this, SLOT(slotPlayingStopped()));
104 m_previewSeparator
= new KSeparator(parent
);
106 const bool showPreview
= InformationPanelSettings::showPreview();
107 m_preview
->setVisible(showPreview
);
108 m_previewSeparator
->setVisible(showPreview
);
110 m_metaDataWidget
= new KMetaDataWidget(parent
);
111 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
113 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
114 // at the bottom. This prevents that the meta data widget gets vertically stretched
115 // in the case where the height of m_metaDataArea > m_metaDataWidget.
116 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
117 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
118 containerLayout
->setContentsMargins(0, 0, 0, 0);
119 containerLayout
->setSpacing(0);
120 containerLayout
->addWidget(m_metaDataWidget
);
121 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
122 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
123 containerLayout
->addWidget(stretchWidget
);
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_nameLabel
);
138 layout
->addWidget(new KSeparator(this));
139 layout
->addWidget(m_preview
);
140 layout
->addWidget(m_phononWidget
);
141 layout
->addWidget(m_previewSeparator
);
142 layout
->addWidget(m_metaDataArea
);
143 parent
->setLayout(layout
);
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 if (!applyPlace(itemUrl
)) {
157 // try to get a preview pixmap from the item...
158 m_pendingPreview
= true;
160 // Mark the currently shown preview as outdated. This is done
161 // with a small delay to prevent a flickering when the next preview
162 // can be shown within a short timeframe. This timer is not started
163 // for directories, as directory previews might fail and return the
166 m_outdatedPreviewTimer
->start();
169 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
177 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
178 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
179 connect(job
, SIGNAL(failed(const KFileItem
&)),
180 this, SLOT(showIcon(const KFileItem
&)));
182 setNameLabelText(itemUrl
.fileName());
185 if (m_metaDataWidget
!= 0) {
186 m_metaDataWidget
->setItem(item
);
189 if (InformationPanelSettings::showPreview()) {
190 const QString mimeType
= item
.mimetype();
191 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
192 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
193 // thinks it supports PNG images
195 m_phononWidget
->show();
196 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
197 ? PhononWidget::Video
198 : PhononWidget::Audio
;
199 m_phononWidget
->setMode(mode
);
200 m_phononWidget
->setUrl(item
.url());
201 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
202 m_phononWidget
->setVideoSize(m_preview
->size());
205 m_phononWidget
->hide();
206 m_preview
->setVisible(true);
209 m_phononWidget
->hide();
215 void InformationPanelContent::showItems(const KFileItemList
& items
)
217 m_pendingPreview
= false;
219 KIconLoader iconLoader
;
220 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
221 KIconLoader::NoGroup
,
222 KIconLoader::SizeEnormous
);
223 m_preview
->setPixmap(icon
);
224 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
226 if (m_metaDataWidget
!= 0) {
227 m_metaDataWidget
->setItems(items
);
230 m_phononWidget
->hide();
232 m_item
= KFileItem();
235 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
237 if (event
->type() == QEvent::Resize
) {
238 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
239 if (obj
== m_metaDataArea
->viewport()) {
240 // The size of the meta text area has changed. Adjust the fixed
241 // width in a way that no horizontal scrollbar needs to be shown.
242 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
243 } else if (obj
== parent()) {
244 // If the text inside the name label or the info label cannot
245 // get wrapped, then the maximum width of the label is increased
246 // so that the width of the information panel gets increased.
247 // To prevent this, the maximum width is adjusted to
248 // the current width of the panel.
249 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
250 m_nameLabel
->setMaximumWidth(maxWidth
);
252 // The metadata widget also contains a text widget which may return
253 // a large preferred width.
254 if (m_metaDataWidget
!= 0) {
255 m_metaDataWidget
->setMaximumWidth(maxWidth
);
258 // try to increase the preview as large as possible
259 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
261 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
262 // assure that the size of the video player is the same as the preview size
263 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
267 return Panel::eventFilter(obj
, event
);
270 void InformationPanelContent::configureSettings()
274 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
275 previewAction
->setIcon(KIcon("view-preview"));
276 previewAction
->setCheckable(true);
277 previewAction
->setChecked(InformationPanelSettings::showPreview());
279 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
280 configureAction
->setIcon(KIcon("configure"));
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 m_previewSeparator
->setVisible(isChecked
);
293 InformationPanelSettings::setShowPreview(isChecked
);
294 } else if (action
== configureAction
) {
295 KMetaDataConfigurationDialog
dialog(m_metaDataWidget
, this, Qt::Dialog
);
299 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
304 void InformationPanelContent::showIcon(const KFileItem
& item
)
306 m_outdatedPreviewTimer
->stop();
307 m_pendingPreview
= false;
308 if (!applyPlace(item
.url())) {
309 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
313 void InformationPanelContent::showPreview(const KFileItem
& item
,
314 const QPixmap
& pixmap
)
316 m_outdatedPreviewTimer
->stop();
319 if (m_pendingPreview
) {
320 m_preview
->setPixmap(pixmap
);
321 m_pendingPreview
= false;
325 void InformationPanelContent::markOutdatedPreview()
327 KIconEffect iconEffect
;
328 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
329 KIconLoader::Desktop
,
330 KIconLoader::DisabledState
);
331 m_preview
->setPixmap(disabledPixmap
);
334 void InformationPanelContent::slotPlayingStarted()
336 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
339 void InformationPanelContent::slotPlayingStopped()
341 m_preview
->setVisible(true);
344 bool InformationPanelContent::applyPlace(const KUrl
& url
)
346 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
347 int count
= placesModel
->rowCount();
349 for (int i
= 0; i
< count
; ++i
) {
350 QModelIndex index
= placesModel
->index(i
, 0);
352 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
353 setNameLabelText(placesModel
->text(index
));
354 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
362 void InformationPanelContent::setNameLabelText(const QString
& text
)
364 QTextOption textOption
;
365 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
367 QTextLayout
textLayout(text
);
368 textLayout
.setFont(m_nameLabel
->font());
369 textLayout
.setTextOption(textOption
);
372 wrappedText
.reserve(text
.length());
374 // wrap the text to fit into the width of m_nameLabel
375 textLayout
.beginLayout();
376 QTextLine line
= textLayout
.createLine();
377 while (line
.isValid()) {
378 line
.setLineWidth(m_nameLabel
->width());
379 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
381 line
= textLayout
.createLine();
382 if (line
.isValid()) {
383 wrappedText
+= QChar::LineSeparator
;
386 textLayout
.endLayout();
388 m_nameLabel
->setText(wrappedText
);
391 #include "informationpanelcontent.moc"