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