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>
35 #include <KFileMetaDataWidget>
37 #include <nepomuk2/filemetadatawidget.h>
40 #include <panels/places/placesitem.h>
41 #include <panels/places/placesitemmodel.h>
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextDocument>
54 #include <QTextLayout>
57 #include <QVBoxLayout>
59 #include "dolphin_informationpanelsettings.h"
60 #include "filemetadataconfigurationdialog.h"
61 #include "phononwidget.h"
62 #include "pixmapviewer.h"
64 InformationPanelContent::InformationPanelContent(QWidget
* parent
) :
68 m_outdatedPreviewTimer(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(this);
88 layout
->setSpacing(KDialog::spacingHint());
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
->hide();
99 m_phononWidget
->setMinimumWidth(minPreviewWidth
);
100 connect(m_phononWidget
, SIGNAL(hasVideoChanged(bool)),
101 this, SLOT(slotHasVideoChanged(bool)));
104 m_nameLabel
= new QLabel(parent
);
105 QFont font
= m_nameLabel
->font();
107 m_nameLabel
->setFont(font
);
108 m_nameLabel
->setTextFormat(Qt::PlainText
);
109 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
110 m_nameLabel
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Fixed
);
112 const bool previewsShown
= InformationPanelSettings::previewsShown();
113 m_preview
->setVisible(previewsShown
);
116 m_metaDataWidget
= new KFileMetaDataWidget(parent
);
118 m_metaDataWidget
= new Nepomuk2::FileMetaDataWidget(parent
);
120 m_metaDataWidget
->setFont(KGlobalSettings::smallestReadableFont());
121 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Minimum
);
122 connect(m_metaDataWidget
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
124 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
125 // at the bottom. This prevents that the meta data widget gets vertically stretched
126 // in the case where the height of m_metaDataArea > m_metaDataWidget.
127 QWidget
* metaDataWidgetContainer
= new QWidget(parent
);
128 QVBoxLayout
* containerLayout
= new QVBoxLayout(metaDataWidgetContainer
);
129 containerLayout
->setContentsMargins(0, 0, 0, 0);
130 containerLayout
->setSpacing(0);
131 containerLayout
->addWidget(m_metaDataWidget
);
132 containerLayout
->addStretch();
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_preview
);
147 layout
->addWidget(m_phononWidget
);
148 layout
->addWidget(m_nameLabel
);
149 layout
->addWidget(new KSeparator());
150 layout
->addWidget(m_metaDataArea
);
152 m_placesItemModel
= new PlacesItemModel(this);
155 InformationPanelContent::~InformationPanelContent()
157 InformationPanelSettings::self()->writeConfig();
160 void InformationPanelContent::showItem(const KFileItem
& item
)
162 // If there is a preview job, kill it to prevent that we have jobs for
163 // multiple items running, and thus a race condition (bug 250787).
165 m_previewJob
->kill();
168 const KUrl itemUrl
= item
.url();
169 const bool isSearchUrl
= itemUrl
.protocol().contains("search") && item
.nepomukUri().isEmpty();
170 if (!applyPlace(itemUrl
)) {
171 setNameLabelText(item
.text());
173 // in the case of a search-URL the URL is not readable for humans
174 // (at least not useful to show in the Information Panel)
175 KIconLoader iconLoader
;
176 QPixmap icon
= iconLoader
.loadIcon("nepomuk",
177 KIconLoader::NoGroup
,
178 KIconLoader::SizeEnormous
);
179 m_preview
->setPixmap(icon
);
181 // try to get a preview pixmap from the item...
183 // Mark the currently shown preview as outdated. This is done
184 // with a small delay to prevent a flickering when the next preview
185 // can be shown within a short timeframe. This timer is not started
186 // for directories, as directory previews might fail and return the
189 m_outdatedPreviewTimer
->start();
192 m_previewJob
= new KIO::PreviewJob(KFileItemList() << item
, QSize(m_preview
->width(), m_preview
->height()));
193 m_previewJob
->setScaleType(KIO::PreviewJob::Unscaled
);
194 m_previewJob
->setIgnoreMaximumSize(item
.isLocalFile());
195 if (m_previewJob
->ui()) {
196 m_previewJob
->ui()->setWindow(this);
199 connect(m_previewJob
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
200 this, SLOT(showPreview(KFileItem
,QPixmap
)));
201 connect(m_previewJob
, SIGNAL(failed(KFileItem
)),
202 this, SLOT(showIcon(KFileItem
)));
206 if (m_metaDataWidget
) {
207 m_metaDataWidget
->show();
208 m_metaDataWidget
->setItems(KFileItemList() << item
);
211 if (InformationPanelSettings::previewsShown()) {
212 const QString mimeType
= item
.mimetype();
213 const bool usePhonon
= mimeType
.startsWith("audio/") || mimeType
.startsWith("video/");
215 m_phononWidget
->show();
216 m_phononWidget
->setUrl(item
.targetUrl());
217 if (m_preview
->isVisible()) {
218 m_phononWidget
->setVideoSize(m_preview
->size());
221 m_phononWidget
->hide();
222 m_preview
->setVisible(true);
225 m_phononWidget
->hide();
231 void InformationPanelContent::showItems(const KFileItemList
& items
)
233 // If there is a preview job, kill it to prevent that we have jobs for
234 // multiple items running, and thus a race condition (bug 250787).
236 m_previewJob
->kill();
239 KIconLoader iconLoader
;
240 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
241 KIconLoader::NoGroup
,
242 KIconLoader::SizeEnormous
);
243 m_preview
->setPixmap(icon
);
244 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items
.count()));
246 if (m_metaDataWidget
) {
247 m_metaDataWidget
->setItems(items
);
250 m_phononWidget
->hide();
252 m_item
= KFileItem();
255 bool InformationPanelContent::eventFilter(QObject
* obj
, QEvent
* event
)
257 switch (event
->type()) {
258 case QEvent::Resize
: {
259 QResizeEvent
* resizeEvent
= static_cast<QResizeEvent
*>(event
);
260 if (obj
== m_metaDataArea
->viewport()) {
261 // The size of the meta text area has changed. Adjust the fixed
262 // width in a way that no horizontal scrollbar needs to be shown.
263 m_metaDataWidget
->setFixedWidth(resizeEvent
->size().width());
264 } else if (obj
== parent()) {
265 adjustWidgetSizes(resizeEvent
->size().width());
271 adjustWidgetSizes(parentWidget()->width());
278 return QWidget::eventFilter(obj
, event
);
281 void InformationPanelContent::configureSettings(const QList
<QAction
*>& customContextMenuActions
)
285 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
286 previewAction
->setIcon(KIcon("view-preview"));
287 previewAction
->setCheckable(true);
288 previewAction
->setChecked(InformationPanelSettings::previewsShown());
290 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
291 configureAction
->setIcon(KIcon("configure"));
293 popup
.addSeparator();
294 foreach (QAction
* action
, customContextMenuActions
) {
295 popup
.addAction(action
);
298 // Open the popup and adjust the settings for the
300 QAction
* action
= popup
.exec(QCursor::pos());
305 const bool isChecked
= action
->isChecked();
306 if (action
== previewAction
) {
307 m_preview
->setVisible(isChecked
);
308 InformationPanelSettings::setPreviewsShown(isChecked
);
309 } else if (action
== configureAction
) {
310 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog();
311 dialog
->setDescription(i18nc("@label::textbox",
312 "Select which data should be shown in the information panel:"));
313 dialog
->setItems(m_metaDataWidget
->items());
314 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
317 dialog
->activateWindow();
318 connect(dialog
, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
322 void InformationPanelContent::showIcon(const KFileItem
& item
)
324 m_outdatedPreviewTimer
->stop();
325 if (!applyPlace(item
.targetUrl())) {
326 KIcon
icon(item
.iconName(), KIconLoader::global(), item
.overlays());
327 m_preview
->setPixmap(icon
.pixmap(KIconLoader::SizeEnormous
));
331 void InformationPanelContent::showPreview(const KFileItem
& item
,
332 const QPixmap
& pixmap
)
334 m_outdatedPreviewTimer
->stop();
338 KIconLoader::global()->drawOverlays(item
.overlays(), p
, KIconLoader::Desktop
);
339 m_preview
->setPixmap(p
);
342 void InformationPanelContent::markOutdatedPreview()
344 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
345 QPixmap disabledPixmap
= iconEffect
->apply(m_preview
->pixmap(),
346 KIconLoader::Desktop
,
347 KIconLoader::DisabledState
);
348 m_preview
->setPixmap(disabledPixmap
);
351 void InformationPanelContent::slotHasVideoChanged(bool hasVideo
)
353 m_preview
->setVisible(!hasVideo
);
356 void InformationPanelContent::refreshMetaData()
358 if (!m_item
.isNull() && m_item
.nepomukUri().isValid()) {
363 bool InformationPanelContent::applyPlace(const KUrl
& url
)
365 const int count
= m_placesItemModel
->count();
366 for (int i
= 0; i
< count
; ++i
) {
367 const PlacesItem
* item
= m_placesItemModel
->placesItem(i
);
368 if (item
->url().equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
369 setNameLabelText(item
->text());
370 m_preview
->setPixmap(KIcon(item
->icon()).pixmap(128, 128));
378 void InformationPanelContent::setNameLabelText(const QString
& text
)
380 QTextOption textOption
;
381 textOption
.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere
);
383 const QString processedText
= Qt::mightBeRichText(text
) ? text
: KStringHandler::preProcessWrap(text
);
385 QTextLayout
textLayout(processedText
);
386 textLayout
.setFont(m_nameLabel
->font());
387 textLayout
.setTextOption(textOption
);
390 wrappedText
.reserve(processedText
.length());
392 // wrap the text to fit into the width of m_nameLabel
393 textLayout
.beginLayout();
394 QTextLine line
= textLayout
.createLine();
395 while (line
.isValid()) {
396 line
.setLineWidth(m_nameLabel
->width());
397 wrappedText
+= processedText
.mid(line
.textStart(), line
.textLength());
399 line
= textLayout
.createLine();
400 if (line
.isValid()) {
401 wrappedText
+= QChar::LineSeparator
;
404 textLayout
.endLayout();
406 m_nameLabel
->setText(wrappedText
);
409 void InformationPanelContent::adjustWidgetSizes(int width
)
411 // If the text inside the name label or the info label cannot
412 // get wrapped, then the maximum width of the label is increased
413 // so that the width of the information panel gets increased.
414 // To prevent this, the maximum width is adjusted to
415 // the current width of the panel.
416 const int maxWidth
= width
- KDialog::spacingHint() * 4;
417 m_nameLabel
->setMaximumWidth(maxWidth
);
419 // The metadata widget also contains a text widget which may return
420 // a large preferred width.
421 if (m_metaDataWidget
) {
422 m_metaDataWidget
->setMaximumWidth(maxWidth
);
425 // try to increase the preview as large as possible
426 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
428 if (m_phononWidget
->isVisible()) {
429 // assure that the size of the video player is the same as the preview size
430 m_phononWidget
->setVideoSize(QSize(maxWidth
, maxWidth
));
434 #include "informationpanelcontent.moc"