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>
42 #include <QResizeEvent>
43 #include <QScrollArea>
44 #include <QTextLayout>
47 #include <QVBoxLayout>
49 #include "dolphin_informationpanelsettings.h"
50 #include "settings/dolphinsettings.h"
51 #include "kmetadatawidget.h"
52 #include "kmetadataconfigurationdialog.h"
53 #include "phononwidget.h"
54 #include "pixmapviewer.h"
56 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
59 m_pendingPreview(false),
60 m_outdatedPreviewTimer(0),
63 m_previewSeparator(0),
68 parent
->installEventFilter(this);
70 // Initialize timer for disabling an outdated preview with a small
71 // delay. This prevents flickering if the new preview can be generated
72 // within a very small timeframe.
73 m_outdatedPreviewTimer
= new QTimer(this);
74 m_outdatedPreviewTimer
->setInterval(300);
75 m_outdatedPreviewTimer
->setSingleShot(true);
76 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
77 this, SLOT(markOutdatedPreview()));
79 QVBoxLayout
* layout
= new QVBoxLayout
;
80 layout
->setSpacing(KDialog::spacingHint());
83 m_nameLabel
= new QLabel(parent
);
84 QFont font
= m_nameLabel
->font();
86 m_nameLabel
->setFont(font
);
87 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
88 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
89 m_nameLabel
->setMaximumWidth(KIconLoader::SizeEnormous
);
92 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
94 m_preview
= new PixmapViewer(parent
);
95 m_preview
->setMinimumWidth(minPreviewWidth
);
96 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
98 m_phononWidget
= new PhononWidget(parent
);
99 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
100 connect(m_phononWidget
, SIGNAL(playingStarted()),
101 this, SLOT(slotPlayingStarted()));
102 connect(m_phononWidget
, SIGNAL(playingStopped()),
103 this, SLOT(slotPlayingStopped()));
105 m_previewSeparator
= new KSeparator(parent
);
107 const bool showPreview
= InformationPanelSettings::showPreview();
108 m_preview
->setVisible(showPreview
);
109 m_previewSeparator
->setVisible(showPreview
);
111 m_metaDataWidget
= new KMetaDataWidget(parent
);
112 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
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 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
123 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
124 containerLayout
->addWidget(stretchWidget
);
126 m_metaDataArea
= new QScrollArea(parent
);
127 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
128 m_metaDataArea
->setWidgetResizable(true);
129 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
131 QWidget
* viewport
= m_metaDataArea
->viewport();
132 viewport
->installEventFilter(this);
134 QPalette palette
= viewport
->palette();
135 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
136 viewport
->setPalette(palette
);
138 layout
->addWidget(m_nameLabel
);
139 layout
->addWidget(new KSeparator(this));
140 layout
->addWidget(m_preview
);
141 layout
->addWidget(m_phononWidget
);
142 layout
->addWidget(m_previewSeparator
);
143 layout
->addWidget(m_metaDataArea
);
144 parent
->setLayout(layout
);
147 InformationPanelContent::~InformationPanelContent()
149 InformationPanelSettings::self()->writeConfig();
152 void InformationPanelContent::showItem(const KFileItem
& item
)
154 m_pendingPreview
= false;
156 const KUrl itemUrl
= item
.url();
157 if (!applyPlace(itemUrl
)) {
158 // try to get a preview pixmap from the item...
159 m_pendingPreview
= true;
161 // Mark the currently shown preview as outdated. This is done
162 // with a small delay to prevent a flickering when the next preview
163 // can be shown within a short timeframe. This timer is not started
164 // for directories, as directory previews might fail and return the
167 m_outdatedPreviewTimer
->start();
170 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
178 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
179 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
180 connect(job
, SIGNAL(failed(const KFileItem
&)),
181 this, SLOT(showIcon(const KFileItem
&)));
183 setNameLabelText(itemUrl
.fileName());
186 if (m_metaDataWidget
!= 0) {
187 m_metaDataWidget
->setItem(item
);
190 if (InformationPanelSettings::showPreview()) {
191 const QString mimeType
= item
.mimetype();
192 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
193 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
194 // thinks it supports PNG images
196 m_phononWidget
->show();
197 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
198 ? PhononWidget::Video
199 : PhononWidget::Audio
;
200 m_phononWidget
->setMode(mode
);
201 m_phononWidget
->setUrl(item
.url());
202 if ((mode
== PhononWidget::Video
) && 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
!= 0) {
228 m_metaDataWidget
->setItems(items
);
231 m_phononWidget
->hide();
233 m_item
= KFileItem();
236 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
238 if (event
->type() == QEvent::Resize
) {
239 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
240 if (obj
== m_metaDataArea
->viewport()) {
241 // The size of the meta text area has changed. Adjust the fixed
242 // width in a way that no horizontal scrollbar needs to be shown.
243 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
244 } else if (obj
== parent()) {
245 // If the text inside the name label or the info label cannot
246 // get wrapped, then the maximum width of the label is increased
247 // so that the width of the information panel gets increased.
248 // To prevent this, the maximum width is adjusted to
249 // the current width of the panel.
250 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
251 m_nameLabel
->setMaximumWidth(maxWidth
);
253 // The metadata widget also contains a text widget which may return
254 // a large preferred width.
255 if (m_metaDataWidget
!= 0) {
256 m_metaDataWidget
->setMaximumWidth(maxWidth
);
259 // try to increase the preview as large as possible
260 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
262 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
263 // assure that the size of the video player is the same as the preview size
264 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
268 return Panel::eventFilter(obj
, event
);
271 void InformationPanelContent::configureSettings()
275 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
276 previewAction
->setIcon(KIcon("view-preview"));
277 previewAction
->setCheckable(true);
278 previewAction
->setChecked(InformationPanelSettings::showPreview());
280 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
281 configureAction
->setIcon(KIcon("configure"));
283 // Open the popup and adjust the settings for the
285 QAction
* action
= popup
.exec(QCursor::pos());
290 const bool isChecked
= action
->isChecked();
291 if (action
== previewAction
) {
292 m_preview
->setVisible(isChecked
);
293 m_previewSeparator
->setVisible(isChecked
);
294 InformationPanelSettings::setShowPreview(isChecked
);
295 } else if (action
== configureAction
) {
296 QPointer
<KMetaDataConfigurationDialog
> dialog
= new KMetaDataConfigurationDialog(m_metaDataWidget
, this, Qt::Dialog
);
297 dialog
->setDescription(i18nc("@label::textbox",
298 "Configure which data should be shown in the Information Panel."));
303 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
308 void InformationPanelContent::showIcon(const KFileItem
& item
)
310 m_outdatedPreviewTimer
->stop();
311 m_pendingPreview
= false;
312 if (!applyPlace(item
.url())) {
313 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
317 void InformationPanelContent::showPreview(const KFileItem
& item
,
318 const QPixmap
& pixmap
)
320 m_outdatedPreviewTimer
->stop();
323 if (m_pendingPreview
) {
324 m_preview
->setPixmap(pixmap
);
325 m_pendingPreview
= false;
329 void InformationPanelContent::markOutdatedPreview()
331 KIconEffect iconEffect
;
332 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
333 KIconLoader::Desktop
,
334 KIconLoader::DisabledState
);
335 m_preview
->setPixmap(disabledPixmap
);
338 void InformationPanelContent::slotPlayingStarted()
340 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
343 void InformationPanelContent::slotPlayingStopped()
345 m_preview
->setVisible(true);
348 bool InformationPanelContent::applyPlace(const KUrl
& url
)
350 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
351 int count
= placesModel
->rowCount();
353 for (int i
= 0; i
< count
; ++i
) {
354 QModelIndex index
= placesModel
->index(i
, 0);
356 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
357 setNameLabelText(placesModel
->text(index
));
358 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
366 void InformationPanelContent::setNameLabelText(const QString
& text
)
368 QTextOption textOption
;
369 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
371 QTextLayout
textLayout(text
);
372 textLayout
.setFont(m_nameLabel
->font());
373 textLayout
.setTextOption(textOption
);
376 wrappedText
.reserve(text
.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
+= text
.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 #include "informationpanelcontent.moc"