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 "metadatawidget.h"
51 #include "phononwidget.h"
52 #include "pixmapviewer.h"
55 * Helper function for sorting items with qSort() in
56 * InformationPanelContent::contextMenu().
58 bool lessThan(const QAction
* action1
, const QAction
* action2
)
60 return action1
->text() < action2
->text();
64 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(0),
71 m_previewSeparator(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
;
88 layout
->setSpacing(KDialog::spacingHint());
91 m_nameLabel
= new QLabel(parent
);
92 QFont font
= m_nameLabel
->font();
94 m_nameLabel
->setFont(font
);
95 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
96 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
97 m_nameLabel
->setMaximumWidth(KIconLoader::SizeEnormous
);
100 const int minPreviewWidth
= KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
;
102 m_preview
= new PixmapViewer(parent
);
103 m_preview
->setMinimumWidth(minPreviewWidth
);
104 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
106 m_phononWidget
= new PhononWidget(parent
);
107 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
108 connect(m_phononWidget
, SIGNAL(playingStarted()),
109 this, SLOT(slotPlayingStarted()));
110 connect(m_phononWidget
, SIGNAL(playingStopped()),
111 this, SLOT(slotPlayingStopped()));
113 m_previewSeparator
= new KSeparator(parent
);
115 const bool showPreview
= InformationPanelSettings::showPreview();
116 m_preview
->setVisible(showPreview
);
117 m_previewSeparator
->setVisible(showPreview
);
119 m_metaDataWidget
= new MetaDataWidget(parent
);
120 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
122 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
123 // at the bottom. This prevents that the meta data widget gets vertically stretched
124 // in the case where the height of m_metaDataArea > m_metaDataWidget.
125 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
126 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
127 containerLayout
->setContentsMargins(0, 0, 0, 0);
128 containerLayout
->setSpacing(0);
129 containerLayout
->addWidget(m_metaDataWidget
);
130 QWidget
* stretchWidget
= new QWidget(metaDataWidgetContainer
);
131 stretchWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
132 containerLayout
->addWidget(stretchWidget
);
134 m_metaDataArea
= new QScrollArea(parent
);
135 m_metaDataArea
->setWidget(metaDataWidgetContainer
);
136 m_metaDataArea
->setWidgetResizable(true);
137 m_metaDataArea
->setFrameShape(QFrame::NoFrame
);
139 QWidget
* viewport
= m_metaDataArea
->viewport();
140 viewport
->installEventFilter(this);
142 QPalette palette
= viewport
->palette();
143 palette
.setColor(viewport
->backgroundRole(), QColor(Qt::transparent
));
144 viewport
->setPalette(palette
);
146 layout
->addWidget(m_nameLabel
);
147 layout
->addWidget(new KSeparator(this));
148 layout
->addWidget(m_preview
);
149 layout
->addWidget(m_phononWidget
);
150 layout
->addWidget(m_previewSeparator
);
151 layout
->addWidget(m_metaDataArea
);
152 parent
->setLayout(layout
);
155 InformationPanelContent::~InformationPanelContent()
157 InformationPanelSettings::self()->writeConfig();
160 void InformationPanelContent::showItem(const KFileItem
& item
)
162 m_pendingPreview
= false;
164 const KUrl itemUrl
= item
.url();
165 if (!applyPlace(itemUrl
)) {
166 // try to get a preview pixmap from the item...
167 m_pendingPreview
= true;
169 // Mark the currently shown preview as outdated. This is done
170 // with a small delay to prevent a flickering when the next preview
171 // can be shown within a short timeframe. This timer is not started
172 // for directories, as directory previews might fail and return the
175 m_outdatedPreviewTimer
->start();
178 KIO::PreviewJob
* job
= KIO::filePreview(KFileItemList() << item
,
186 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
187 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
188 connect(job
, SIGNAL(failed(const KFileItem
&)),
189 this, SLOT(showIcon(const KFileItem
&)));
191 setNameLabelText(itemUrl
.fileName());
194 if (m_metaDataWidget
!= 0) {
195 m_metaDataWidget
->setItem(item
);
198 if (InformationPanelSettings::showPreview()) {
199 const QString mimeType
= item
.mimetype();
200 const bool usePhonon
= Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType
) &&
201 (mimeType
!= "image/png"); // TODO: workaround, as Phonon
202 // thinks it supports PNG images
204 m_phononWidget
->show();
205 PhononWidget::Mode mode
= mimeType
.startsWith(QLatin1String("video"))
206 ? PhononWidget::Video
207 : PhononWidget::Audio
;
208 m_phononWidget
->setMode(mode
);
209 m_phononWidget
->setUrl(item
.url());
210 if ((mode
== PhononWidget::Video
) && m_preview
->isVisible()) {
211 m_phononWidget
->setVideoSize(m_preview
->size());
214 m_phononWidget
->hide();
215 m_preview
->setVisible(true);
218 m_phononWidget
->hide();
224 void InformationPanelContent::showItems(const KFileItemList
& items
)
226 m_pendingPreview
= false;
228 KIconLoader iconLoader
;
229 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
230 KIconLoader::NoGroup
,
231 KIconLoader::SizeEnormous
);
232 m_preview
->setPixmap(icon
);
233 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items
.count()));
235 if (m_metaDataWidget
!= 0) {
236 m_metaDataWidget
->setItems(items
);
239 m_phononWidget
->hide();
241 m_item
= KFileItem();
244 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
246 if (event
->type() == QEvent::Resize
) {
247 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
248 if (obj
== m_metaDataArea
->viewport()) {
249 // The size of the meta text area has changed. Adjust the fixed
250 // width in a way that no horizontal scrollbar needs to be shown.
251 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
252 } else if (obj
== parent()) {
253 // If the text inside the name label or the info label cannot
254 // get wrapped, then the maximum width of the label is increased
255 // so that the width of the information panel gets increased.
256 // To prevent this, the maximum width is adjusted to
257 // the current width of the panel.
258 const int maxWidth
= resizeEvent
->size().width() - KDialog::spacingHint() * 4;
259 m_nameLabel
->setMaximumWidth(maxWidth
);
261 // The metadata widget also contains a text widget which may return
262 // a large preferred width.
263 if (m_metaDataWidget
!= 0) {
264 m_metaDataWidget
->setMaximumWidth(maxWidth
);
267 // try to increase the preview as large as possible
268 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
270 if (m_phononWidget
->isVisible() && (m_phononWidget
->mode() == PhononWidget::Video
)) {
271 // assure that the size of the video player is the same as the preview size
272 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
276 return Panel::eventFilter(obj
, event
);
279 void InformationPanelContent::configureSettings()
281 if (m_item
.isNull() ||
282 !m_item
.nepomukUri().isValid()) {
288 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
289 previewAction
->setIcon(KIcon("view-preview"));
290 previewAction
->setCheckable(true);
291 previewAction
->setChecked(InformationPanelSettings::showPreview());
293 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
294 configureAction
->setIcon(KIcon("configure"));
296 // Open the popup and adjust the settings for the
298 QAction
* action
= popup
.exec(QCursor::pos());
303 const bool isChecked
= action
->isChecked();
304 if (action
== previewAction
) {
305 m_preview
->setVisible(isChecked
);
306 m_previewSeparator
->setVisible(isChecked
);
307 InformationPanelSettings::setShowPreview(isChecked
);
308 } else if (action
== configureAction
) {
309 m_metaDataWidget
->openConfigurationDialog();
315 void InformationPanelContent::showIcon(const KFileItem
& item
)
317 m_outdatedPreviewTimer
->stop();
318 m_pendingPreview
= false;
319 if (!applyPlace(item
.url())) {
320 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
324 void InformationPanelContent::showPreview(const KFileItem
& item
,
325 const QPixmap
& pixmap
)
327 m_outdatedPreviewTimer
->stop();
330 if (m_pendingPreview
) {
331 m_preview
->setPixmap(pixmap
);
332 m_pendingPreview
= false;
336 void InformationPanelContent::markOutdatedPreview()
338 KIconEffect iconEffect
;
339 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(),
340 KIconLoader::Desktop
,
341 KIconLoader::DisabledState
);
342 m_preview
->setPixmap(disabledPixmap
);
345 void InformationPanelContent::slotPlayingStarted()
347 m_preview
->setVisible(m_phononWidget
->mode() != PhononWidget::Video
);
350 void InformationPanelContent::slotPlayingStopped()
352 m_preview
->setVisible(true);
355 bool InformationPanelContent::applyPlace(const KUrl
& url
)
357 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
358 int count
= placesModel
->rowCount();
360 for (int i
= 0; i
< count
; ++i
) {
361 QModelIndex index
= placesModel
->index(i
, 0);
363 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
364 setNameLabelText(placesModel
->text(index
));
365 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
373 void InformationPanelContent::setNameLabelText(const QString
& text
)
375 QTextOption textOption
;
376 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
378 QTextLayout
textLayout(text
);
379 textLayout
.setFont(m_nameLabel
->font());
380 textLayout
.setTextOption(textOption
);
383 wrappedText
.reserve(text
.length());
385 // wrap the text to fit into the width of m_nameLabel
386 textLayout
.beginLayout();
387 QTextLine line
= textLayout
.createLine();
388 while (line
.isValid()) {
389 line
.setLineWidth(m_nameLabel
->width());
390 wrappedText
+= text
.mid(line
.textStart(), line
.textLength());
392 line
= textLayout
.createLine();
393 if (line
.isValid()) {
394 wrappedText
+= QChar::LineSeparator
;
397 textLayout
.endLayout();
399 m_nameLabel
->setText(wrappedText
);
402 #include "informationpanelcontent.moc"