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"
23 #include <kfileitem.h>
24 #include <kfileplacesmodel.h>
25 #include <kio/previewjob.h>
26 #include <kiconeffect.h>
27 #include <kiconloader.h>
30 #include <kseparator.h>
32 #include <Phonon/BackendCapabilities>
33 #include <Phonon/MediaObject>
34 #include <Phonon/SeekSlider>
40 #include <QResizeEvent>
41 #include <QScrollArea>
42 #include <QTextLayout>
45 #include <QVBoxLayout>
47 #include "dolphin_informationpanelsettings.h"
48 #include "settings/dolphinsettings.h"
49 #include "kmetadatawidget.h"
50 #include "kmetadataconfigurationdialog.h"
51 #include "phononwidget.h"
52 #include "pixmapviewer.h"
54 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
57 m_pendingPreview(false),
58 m_outdatedPreviewTimer(0),
65 parent
->installEventFilter(this);
67 // Initialize timer for disabling an outdated preview with a small
68 // delay. This prevents flickering if the new preview can be generated
69 // within a very small timeframe.
70 m_outdatedPreviewTimer
= new QTimer(this);
71 m_outdatedPreviewTimer
->setInterval(300);
72 m_outdatedPreviewTimer
->setSingleShot(true);
73 connect(m_outdatedPreviewTimer
, SIGNAL(timeout()),
74 this, SLOT(markOutdatedPreview()));
76 QVBoxLayout
* layout
= new QVBoxLayout
;
77 layout
->setSpacing(KDialog::spacingHint());
80 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
82 m_preview
= new PixmapViewer(parent
);
83 m_preview
->setMinimumWidth(minPreviewWidth
);
84 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
86 m_phononWidget
= new PhononWidget(parent
);
87 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
88 connect(m_phononWidget
, SIGNAL(playingStarted()),
89 this, SLOT(slotPlayingStarted()));
90 connect(m_phononWidget
, SIGNAL(playingStopped()),
91 this, SLOT(slotPlayingStopped()));
94 m_nameLabel
= new QLabel(parent
);
95 QFont font
= m_nameLabel
->font();
97 m_nameLabel
->setFont(font
);
98 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
99 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
101 const bool showPreview
= InformationPanelSettings::showPreview();
102 m_preview
->setVisible(showPreview
);
104 m_metaDataWidget
= new KMetaDataWidget(parent
);
105 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
107 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
108 // at the bottom. This prevents that the meta data widget gets vertically stretched
109 // in the case where the height of m_metaDataArea > m_metaDataWidget.
110 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
111 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
112 containerLayout
->setContentsMargins(0, 0, 0, 0);
113 containerLayout
->setSpacing(0);
114 containerLayout
->addWidget(m_metaDataWidget
);
115 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
116 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
117 containerLayout
->addWidget(stretchWidget
);
119 m_metaDataArea
= new QScrollArea(parent
);
120 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
121 m_metaDataArea
->setWidgetResizable(true);
122 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
124 QWidget
* viewport
= m_metaDataArea
->viewport();
125 viewport
->installEventFilter(this);
127 QPalette palette
= viewport
->palette();
128 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
129 viewport
->setPalette(palette
);
131 layout
->addWidget(m_preview
);
132 layout
->addWidget(m_phononWidget
);
133 layout
->addWidget(m_nameLabel
);
134 layout
->addWidget(new KSeparator());
135 layout
->addWidget(m_metaDataArea
);
136 parent
->setLayout(layout
);
139 InformationPanelContent::~InformationPanelContent()
141 InformationPanelSettings::self()->writeConfig();
144 void InformationPanelContent::showItem(const KFileItem
& item
)
146 m_pendingPreview
= false;
148 const KUrl itemUrl
= item
.url();
149 if (!applyPlace(itemUrl
)) {
150 // try to get a preview pixmap from the item...
151 m_pendingPreview
= true;
153 // Mark the currently shown preview as outdated. This is done
154 // with a small delay to prevent a flickering when the next preview
155 // can be shown within a short timeframe. This timer is not started
156 // for directories, as directory previews might fail and return the
159 m_outdatedPreviewTimer
->start();
162 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
170 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
171 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
172 connect(job
, SIGNAL(failed(const KFileItem
&)),
173 this, SLOT(showIcon(const KFileItem
&)));
175 setNameLabelText(itemUrl
.fileName());
178 if (m_metaDataWidget
!= 0) {
179 m_metaDataWidget
->setItem(item
);
182 if (InformationPanelSettings::showPreview()) {
183 const QString mimeType
= item
.mimetype();
184 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
185 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
186 // thinks it supports PNG images
188 m_phononWidget
->show();
189 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
190 ? PhononWidget::Video
191 : PhononWidget::Audio
;
192 m_phononWidget
->setMode(mode
);
193 m_phononWidget
->setUrl(item
.url());
194 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
195 m_phononWidget
->setVideoSize(m_preview
->size());
198 m_phononWidget
->hide();
199 m_preview
->setVisible(true);
202 m_phononWidget
->hide();
208 void InformationPanelContent::showItems(const KFileItemList
& items
)
210 m_pendingPreview
= false;
212 KIconLoader iconLoader
;
213 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
214 KIconLoader::NoGroup
,
215 KIconLoader::SizeEnormous
);
216 m_preview
->setPixmap(icon
);
217 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
219 if (m_metaDataWidget
!= 0) {
220 m_metaDataWidget
->setItems(items
);
223 m_phononWidget
->hide();
225 m_item
= KFileItem();
228 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
230 if (event
->type() == QEvent::Resize
) {
231 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
232 if (obj
== m_metaDataArea
->viewport()) {
233 // The size of the meta text area has changed. Adjust the fixed
234 // width in a way that no horizontal scrollbar needs to be shown.
235 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
236 } else if (obj
== parent()) {
237 // If the text inside the name label or the info label cannot
238 // get wrapped, then the maximum width of the label is increased
239 // so that the width of the information panel gets increased.
240 // To prevent this, the maximum width is adjusted to
241 // the current width of the panel.
242 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
243 m_nameLabel
->setMaximumWidth(maxWidth
);
245 // The metadata widget also contains a text widget which may return
246 // a large preferred width.
247 if (m_metaDataWidget
!= 0) {
248 m_metaDataWidget
->setMaximumWidth(maxWidth
);
251 // try to increase the preview as large as possible
252 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
254 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
255 // assure that the size of the video player is the same as the preview size
256 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
260 return Panel::eventFilter(obj
, event
);
263 void InformationPanelContent::configureSettings()
267 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
268 previewAction
->setIcon(KIcon("view-preview"));
269 previewAction
->setCheckable(true);
270 previewAction
->setChecked(InformationPanelSettings::showPreview());
272 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
273 configureAction
->setIcon(KIcon("configure"));
275 // Open the popup and adjust the settings for the
277 QAction
* action
= popup
.exec(QCursor::pos());
282 const bool isChecked
= action
->isChecked();
283 if (action
== previewAction
) {
284 m_preview
->setVisible(isChecked
);
285 InformationPanelSettings::setShowPreview(isChecked
);
286 } else if (action
== configureAction
) {
287 QPointer
<KMetaDataConfigurationDialog
> dialog
= new KMetaDataConfigurationDialog(m_metaDataWidget
, this, Qt::Dialog
);
288 dialog
->setDescription(i18nc("@label::textbox",
289 "Configure which data should be shown in the Information Panel."));
294 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
299 void InformationPanelContent::showIcon(const KFileItem
& item
)
301 m_outdatedPreviewTimer
->stop();
302 m_pendingPreview
= false;
303 if (!applyPlace(item
.url())) {
304 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
308 void InformationPanelContent::showPreview(const KFileItem
& item
,
309 const QPixmap
& pixmap
)
311 m_outdatedPreviewTimer
->stop();
314 if (m_pendingPreview
) {
315 m_preview
->setPixmap(pixmap
);
316 m_pendingPreview
= false;
320 void InformationPanelContent::markOutdatedPreview()
322 KIconEffect iconEffect
;
323 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
324 KIconLoader::Desktop
,
325 KIconLoader::DisabledState
);
326 m_preview
->setPixmap(disabledPixmap
);
329 void InformationPanelContent::slotPlayingStarted()
331 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
334 void InformationPanelContent::slotPlayingStopped()
336 m_preview
->setVisible(true);
339 bool InformationPanelContent::applyPlace(const KUrl
& url
)
341 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
342 int count
= placesModel
->rowCount();
344 for (int i
= 0; i
< count
; ++i
) {
345 QModelIndex index
= placesModel
->index(i
, 0);
347 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
348 setNameLabelText(placesModel
->text(index
));
349 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
357 void InformationPanelContent::setNameLabelText(const QString
& text
)
359 QTextOption textOption
;
360 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
362 QTextLayout
textLayout(text
);
363 textLayout
.setFont(m_nameLabel
->font());
364 textLayout
.setTextOption(textOption
);
367 wrappedText
.reserve(text
.length());
369 // wrap the text to fit into the width of m_nameLabel
370 textLayout
.beginLayout();
371 QTextLine line
= textLayout
.createLine();
372 while (line
.isValid()) {
373 line
.setLineWidth(m_nameLabel
->width());
374 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
376 line
= textLayout
.createLine();
377 if (line
.isValid()) {
378 wrappedText
+= QChar::LineSeparator
;
381 textLayout
.endLayout();
383 m_nameLabel
->setText(wrappedText
);
386 #include "informationpanelcontent.moc"