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