]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "informationpanelcontent.h"
21
22 #include <KDialog>
23 #include <KFileItem>
24 #include <KGlobalSettings>
25 #include <KIO/JobUiDelegate>
26 #include <KIO/PreviewJob>
27 #include <KIconEffect>
28 #include <KIconLoader>
29 #include <KLocale>
30 #include <KMenu>
31 #include <kseparator.h>
32 #include <KStringHandler>
33
34 #ifndef HAVE_NEPOMUK
35 #include <KFileMetaDataWidget>
36 #else
37 #include <nepomuk2/filemetadatawidget.h>
38 #endif
39
40 #include <panels/places/placesitem.h>
41 #include <panels/places/placesitemmodel.h>
42
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
46
47 #include <QEvent>
48 #include <QLabel>
49 #include <QPixmap>
50 #include <QPointer>
51 #include <QResizeEvent>
52 #include <QScrollArea>
53 #include <QTextDocument>
54 #include <QTextLayout>
55 #include <QTextLine>
56 #include <QTimer>
57 #include <QVBoxLayout>
58
59 #include "dolphin_informationpanelsettings.h"
60 #include "filemetadataconfigurationdialog.h"
61 #include "phononwidget.h"
62 #include "pixmapviewer.h"
63
64 InformationPanelContent::InformationPanelContent(QWidget* parent) :
65 QWidget(parent),
66 m_item(),
67 m_pendingPreview(false),
68 m_outdatedPreviewTimer(0),
69 m_preview(0),
70 m_phononWidget(0),
71 m_nameLabel(0),
72 m_metaDataWidget(0),
73 m_metaDataArea(0),
74 m_placesItemModel(0)
75 {
76 parent->installEventFilter(this);
77
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()));
86
87 QVBoxLayout* layout = new QVBoxLayout(this);
88 layout->setSpacing(KDialog::spacingHint());
89
90 // preview
91 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
92
93 m_preview = new PixmapViewer(parent);
94 m_preview->setMinimumWidth(minPreviewWidth);
95 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
96
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)));
102
103 // name
104 m_nameLabel = new QLabel(parent);
105 QFont font = m_nameLabel->font();
106 font.setBold(true);
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);
111
112 const bool previewsShown = InformationPanelSettings::previewsShown();
113 m_preview->setVisible(previewsShown);
114
115 #ifndef HAVE_NEPOMUK
116 m_metaDataWidget = new KFileMetaDataWidget(parent);
117 #else
118 m_metaDataWidget = new Nepomuk2::FileMetaDataWidget(parent);
119 #endif
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)));
123
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();
133
134 m_metaDataArea = new QScrollArea(parent);
135 m_metaDataArea->setWidget(metaDataWidgetContainer);
136 m_metaDataArea->setWidgetResizable(true);
137 m_metaDataArea->setFrameShape(QFrame::NoFrame);
138
139 QWidget* viewport = m_metaDataArea->viewport();
140 viewport->installEventFilter(this);
141
142 QPalette palette = viewport->palette();
143 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
144 viewport->setPalette(palette);
145
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);
151
152 m_placesItemModel = new PlacesItemModel(this);
153 }
154
155 InformationPanelContent::~InformationPanelContent()
156 {
157 InformationPanelSettings::self()->writeConfig();
158 }
159
160 void InformationPanelContent::showItem(const KFileItem& item)
161 {
162 m_pendingPreview = false;
163
164 const KUrl itemUrl = item.url();
165 const bool isSearchUrl = itemUrl.protocol().contains("search") && item.nepomukUri().isEmpty();
166 if (!applyPlace(itemUrl)) {
167 setNameLabelText(item.text());
168 if (isSearchUrl) {
169 // in the case of a search-URL the URL is not readable for humans
170 // (at least not useful to show in the Information Panel)
171 KIconLoader iconLoader;
172 QPixmap icon = iconLoader.loadIcon("nepomuk",
173 KIconLoader::NoGroup,
174 KIconLoader::SizeEnormous);
175 m_preview->setPixmap(icon);
176 } else {
177 // try to get a preview pixmap from the item...
178 m_pendingPreview = true;
179
180 // Mark the currently shown preview as outdated. This is done
181 // with a small delay to prevent a flickering when the next preview
182 // can be shown within a short timeframe. This timer is not started
183 // for directories, as directory previews might fail and return the
184 // same icon.
185 if (!item.isDir()) {
186 m_outdatedPreviewTimer->start();
187 }
188
189 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
190 job->setScaleType(KIO::PreviewJob::Unscaled);
191 job->setIgnoreMaximumSize(item.isLocalFile());
192 if (job->ui()) {
193 job->ui()->setWindow(this);
194 }
195
196 connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)),
197 this, SLOT(showPreview(KFileItem,QPixmap)));
198 connect(job, SIGNAL(failed(KFileItem)),
199 this, SLOT(showIcon(KFileItem)));
200 }
201 }
202
203 if (m_metaDataWidget) {
204 m_metaDataWidget->show();
205 m_metaDataWidget->setItems(KFileItemList() << item);
206 }
207
208 if (InformationPanelSettings::previewsShown()) {
209 const QString mimeType = item.mimetype();
210 const bool usePhonon = mimeType.startsWith("audio/") || mimeType.startsWith("video/");
211 if (usePhonon) {
212 m_phononWidget->show();
213 m_phononWidget->setUrl(item.targetUrl());
214 if (m_preview->isVisible()) {
215 m_phononWidget->setVideoSize(m_preview->size());
216 }
217 } else {
218 m_phononWidget->hide();
219 m_preview->setVisible(true);
220 }
221 } else {
222 m_phononWidget->hide();
223 }
224
225 m_item = item;
226 }
227
228 void InformationPanelContent::showItems(const KFileItemList& items)
229 {
230 m_pendingPreview = false;
231
232 KIconLoader iconLoader;
233 QPixmap icon = iconLoader.loadIcon("dialog-information",
234 KIconLoader::NoGroup,
235 KIconLoader::SizeEnormous);
236 m_preview->setPixmap(icon);
237 setNameLabelText(i18ncp("@label", "%1 item selected", "%1 items selected", items.count()));
238
239 if (m_metaDataWidget) {
240 m_metaDataWidget->setItems(items);
241 }
242
243 m_phononWidget->hide();
244
245 m_item = KFileItem();
246 }
247
248 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
249 {
250 switch (event->type()) {
251 case QEvent::Resize: {
252 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
253 if (obj == m_metaDataArea->viewport()) {
254 // The size of the meta text area has changed. Adjust the fixed
255 // width in a way that no horizontal scrollbar needs to be shown.
256 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
257 } else if (obj == parent()) {
258 adjustWidgetSizes(resizeEvent->size().width());
259 }
260 break;
261 }
262
263 case QEvent::Polish:
264 adjustWidgetSizes(parentWidget()->width());
265 break;
266
267 default:
268 break;
269 }
270
271 return QWidget::eventFilter(obj, event);
272 }
273
274 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
275 {
276 KMenu popup(this);
277
278 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
279 previewAction->setIcon(KIcon("view-preview"));
280 previewAction->setCheckable(true);
281 previewAction->setChecked(InformationPanelSettings::previewsShown());
282
283 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
284 configureAction->setIcon(KIcon("configure"));
285
286 popup.addSeparator();
287 foreach (QAction* action, customContextMenuActions) {
288 popup.addAction(action);
289 }
290
291 // Open the popup and adjust the settings for the
292 // selected action.
293 QAction* action = popup.exec(QCursor::pos());
294 if (!action) {
295 return;
296 }
297
298 const bool isChecked = action->isChecked();
299 if (action == previewAction) {
300 m_preview->setVisible(isChecked);
301 InformationPanelSettings::setPreviewsShown(isChecked);
302 } else if (action == configureAction) {
303 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog();
304 dialog->setDescription(i18nc("@label::textbox",
305 "Select which data should be shown in the information panel:"));
306 dialog->setItems(m_metaDataWidget->items());
307 dialog->setAttribute(Qt::WA_DeleteOnClose);
308 dialog->show();
309 dialog->raise();
310 dialog->activateWindow();
311 connect(dialog, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
312 }
313 }
314
315 void InformationPanelContent::showIcon(const KFileItem& item)
316 {
317 m_outdatedPreviewTimer->stop();
318 m_pendingPreview = false;
319 if (!applyPlace(item.targetUrl())) {
320 KIcon icon(item.iconName(), KIconLoader::global(), item.overlays());
321 m_preview->setPixmap(icon.pixmap(KIconLoader::SizeEnormous));
322 }
323 }
324
325 void InformationPanelContent::showPreview(const KFileItem& item,
326 const QPixmap& pixmap)
327 {
328 m_outdatedPreviewTimer->stop();
329 Q_UNUSED(item);
330 if (m_pendingPreview) {
331 QPixmap p = pixmap;
332 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
333 m_preview->setPixmap(p);
334 m_pendingPreview = false;
335 }
336 }
337
338 void InformationPanelContent::markOutdatedPreview()
339 {
340 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
341 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
342 KIconLoader::Desktop,
343 KIconLoader::DisabledState);
344 m_preview->setPixmap(disabledPixmap);
345 }
346
347 void InformationPanelContent::slotHasVideoChanged(bool hasVideo)
348 {
349 m_preview->setVisible(!hasVideo);
350 }
351
352 void InformationPanelContent::refreshMetaData()
353 {
354 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
355 showItem(m_item);
356 }
357 }
358
359 bool InformationPanelContent::applyPlace(const KUrl& url)
360 {
361 const int count = m_placesItemModel->count();
362 for (int i = 0; i < count; ++i) {
363 const PlacesItem* item = m_placesItemModel->placesItem(i);
364 if (item->url().equals(url, KUrl::CompareWithoutTrailingSlash)) {
365 setNameLabelText(item->text());
366 m_preview->setPixmap(KIcon(item->icon()).pixmap(128, 128));
367 return true;
368 }
369 }
370
371 return false;
372 }
373
374 void InformationPanelContent::setNameLabelText(const QString& text)
375 {
376 QTextOption textOption;
377 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
378
379 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
380
381 QTextLayout textLayout(processedText);
382 textLayout.setFont(m_nameLabel->font());
383 textLayout.setTextOption(textOption);
384
385 QString wrappedText;
386 wrappedText.reserve(processedText.length());
387
388 // wrap the text to fit into the width of m_nameLabel
389 textLayout.beginLayout();
390 QTextLine line = textLayout.createLine();
391 while (line.isValid()) {
392 line.setLineWidth(m_nameLabel->width());
393 wrappedText += processedText.mid(line.textStart(), line.textLength());
394
395 line = textLayout.createLine();
396 if (line.isValid()) {
397 wrappedText += QChar::LineSeparator;
398 }
399 }
400 textLayout.endLayout();
401
402 m_nameLabel->setText(wrappedText);
403 }
404
405 void InformationPanelContent::adjustWidgetSizes(int width)
406 {
407 // If the text inside the name label or the info label cannot
408 // get wrapped, then the maximum width of the label is increased
409 // so that the width of the information panel gets increased.
410 // To prevent this, the maximum width is adjusted to
411 // the current width of the panel.
412 const int maxWidth = width - KDialog::spacingHint() * 4;
413 m_nameLabel->setMaximumWidth(maxWidth);
414
415 // The metadata widget also contains a text widget which may return
416 // a large preferred width.
417 if (m_metaDataWidget) {
418 m_metaDataWidget->setMaximumWidth(maxWidth);
419 }
420
421 // try to increase the preview as large as possible
422 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
423
424 if (m_phononWidget->isVisible()) {
425 // assure that the size of the video player is the same as the preview size
426 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
427 }
428 }
429
430 #include "informationpanelcontent.moc"