]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
don't show the phonon widget, if more than one file is selected
[dolphin.git] / src / panels / information / informationpanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 "informationpanel.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kdialog.h>
25 #include <kdirnotify.h>
26 #include <kfileplacesmodel.h>
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 #include <kio/previewjob.h>
30 #include <kfileitem.h>
31 #include <kglobalsettings.h>
32 #include <kfilemetainfo.h>
33 #include <kiconeffect.h>
34 #include <kseparator.h>
35 #include <kiconloader.h>
36
37 #ifdef HAVE_NEPOMUK
38 #include <Nepomuk/Resource>
39 #include <Nepomuk/Types/Property>
40 #include <Nepomuk/Variant>
41 #endif
42
43 #include <Phonon/BackendCapabilities>
44 #include <Phonon/MediaObject>
45 #include <Phonon/SeekSlider>
46
47 #include <QEvent>
48 #include <QInputDialog>
49 #include <QLabel>
50 #include <QPainter>
51 #include <QPixmap>
52 #include <QResizeEvent>
53 #include <QScrollArea>
54 #include <QTextLayout>
55 #include <QTextLine>
56 #include <QTimer>
57 #include <QScrollBar>
58 #include <QVBoxLayout>
59
60 #include "settings/dolphinsettings.h"
61 #include "metadatawidget.h"
62 #include "metatextlabel.h"
63 #include "phononwidget.h"
64 #include "pixmapviewer.h"
65
66 InformationPanel::InformationPanel(QWidget* parent) :
67 Panel(parent),
68 m_initialized(false),
69 m_pendingPreview(false),
70 m_infoTimer(0),
71 m_outdatedPreviewTimer(0),
72 m_shownUrl(),
73 m_urlCandidate(),
74 m_fileItem(),
75 m_selection(),
76 m_nameLabel(0),
77 m_preview(0),
78 m_phononWidget(0),
79 m_metaDataWidget(0),
80 m_metaTextArea(0),
81 m_metaTextLabel(0)
82 {
83 }
84
85 InformationPanel::~InformationPanel()
86 {
87 }
88
89 QSize InformationPanel::sizeHint() const
90 {
91 QSize size = Panel::sizeHint();
92 size.setWidth(minimumSizeHint().width());
93 return size;
94 }
95
96 void InformationPanel::setUrl(const KUrl& url)
97 {
98 Panel::setUrl(url);
99 if (url.isValid() && !isEqualToShownUrl(url)) {
100 if (isVisible()) {
101 cancelRequest();
102 m_shownUrl = url;
103 showItemInfo();
104 } else {
105 m_shownUrl = url;
106 }
107 }
108 }
109
110 void InformationPanel::setSelection(const KFileItemList& selection)
111 {
112 if (!isVisible()) {
113 return;
114 }
115
116 if ((selection.count() == 0) && (m_selection.count() == 0)) {
117 // The selection has not really changed, only the current index.
118 // QItemSelectionModel emits a signal in this case and it is less
119 // expensive doing the check this way instead of patching
120 // DolphinView::emitSelectionChanged().
121 return;
122 }
123
124 m_selection = selection;
125
126 const int count = selection.count();
127 if (count == 0) {
128 if (!isEqualToShownUrl(url())) {
129 m_shownUrl = url();
130 showItemInfo();
131 }
132 } else {
133 if ((count == 1) && !selection.first().url().isEmpty()) {
134 m_urlCandidate = selection.first().url();
135 }
136 m_infoTimer->start();
137 }
138 }
139
140 void InformationPanel::requestDelayedItemInfo(const KFileItem& item)
141 {
142 if (!isVisible()) {
143 return;
144 }
145
146 cancelRequest();
147
148 m_fileItem = KFileItem();
149 if (item.isNull()) {
150 // The cursor is above the viewport. If files are selected,
151 // show information regarding the selection.
152 if (m_selection.size() > 0) {
153 m_pendingPreview = false;
154 m_infoTimer->start();
155 }
156 } else {
157 const KUrl url = item.url();
158 if (url.isValid() && !isEqualToShownUrl(url)) {
159 m_urlCandidate = item.url();
160 m_fileItem = item;
161 m_infoTimer->start();
162 }
163 }
164 }
165
166 void InformationPanel::showEvent(QShowEvent* event)
167 {
168 Panel::showEvent(event);
169 if (!event->spontaneous()) {
170 if (!m_initialized) {
171 // do a delayed initialization so that no performance
172 // penalty is given when Dolphin is started with a closed
173 // Information Panel
174 init();
175 }
176 showItemInfo();
177 }
178 }
179
180 void InformationPanel::resizeEvent(QResizeEvent* event)
181 {
182 if (isVisible()) {
183 // If the text inside the name label or the info label cannot
184 // get wrapped, then the maximum width of the label is increased
185 // so that the width of the information panel gets increased.
186 // To prevent this, the maximum width is adjusted to
187 // the current width of the panel.
188 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
189 m_nameLabel->setMaximumWidth(maxWidth);
190
191 // try to increase the preview as large as possible
192 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
193 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
194 m_infoTimer->start();
195 }
196 Panel::resizeEvent(event);
197 }
198
199 bool InformationPanel::eventFilter(QObject* obj, QEvent* event)
200 {
201 // Check whether the size of the meta text area has changed and adjust
202 // the fixed width in a way that no horizontal scrollbar needs to be shown.
203 if ((obj == m_metaTextArea->viewport()) && (event->type() == QEvent::Resize)) {
204 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
205 m_metaTextLabel->setFixedWidth(resizeEvent->size().width());
206 }
207 return Panel::eventFilter(obj, event);
208 }
209
210 void InformationPanel::showItemInfo()
211 {
212 if (!isVisible()) {
213 return;
214 }
215
216 cancelRequest();
217
218 if (showMultipleSelectionInfo()) {
219 KIconLoader iconLoader;
220 QPixmap icon = iconLoader.loadIcon("dialog-information",
221 KIconLoader::NoGroup,
222 KIconLoader::SizeEnormous);
223 m_preview->setPixmap(icon);
224 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
225 m_shownUrl = KUrl();
226 } else {
227 const KFileItem item = fileItem();
228 const KUrl itemUrl = item.url();
229 if (!applyPlace(itemUrl)) {
230 // try to get a preview pixmap from the item...
231 m_pendingPreview = true;
232
233 // Mark the currently shown preview as outdated. This is done
234 // with a small delay to prevent a flickering when the next preview
235 // can be shown within a short timeframe.
236 m_outdatedPreviewTimer->start();
237
238 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
239 m_preview->width(),
240 m_preview->height(),
241 0,
242 0,
243 false,
244 true);
245
246 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
247 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
248 connect(job, SIGNAL(failed(const KFileItem&)),
249 this, SLOT(showIcon(const KFileItem&)));
250
251 setNameLabelText(itemUrl.fileName());
252 }
253 }
254
255 showMetaInfo();
256 }
257
258 void InformationPanel::slotInfoTimeout()
259 {
260 m_shownUrl = m_urlCandidate;
261 showItemInfo();
262 }
263
264 void InformationPanel::markOutdatedPreview()
265 {
266 KIconEffect iconEffect;
267 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
268 KIconLoader::Desktop,
269 KIconLoader::DisabledState);
270 m_preview->setPixmap(disabledPixmap);
271 }
272
273 void InformationPanel::showIcon(const KFileItem& item)
274 {
275 m_outdatedPreviewTimer->stop();
276 m_pendingPreview = false;
277 if (!applyPlace(item.url())) {
278 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
279 }
280 }
281
282 void InformationPanel::showPreview(const KFileItem& item,
283 const QPixmap& pixmap)
284 {
285 m_outdatedPreviewTimer->stop();
286
287 Q_UNUSED(item);
288 if (m_pendingPreview) {
289 m_preview->setPixmap(pixmap);
290 m_pendingPreview = false;
291 }
292 }
293
294 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
295 {
296 if (m_shownUrl == KUrl(source)) {
297 // the currently shown file has been renamed, hence update the item information
298 // for the renamed file
299 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
300 requestDelayedItemInfo(item);
301 }
302 }
303
304 void InformationPanel::slotFilesAdded(const QString& directory)
305 {
306 if (m_shownUrl == KUrl(directory)) {
307 // If the 'trash' icon changes because the trash has been emptied or got filled,
308 // the signal filesAdded("trash:/") will be emitted.
309 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
310 requestDelayedItemInfo(item);
311 }
312 }
313
314 void InformationPanel::slotFilesChanged(const QStringList& files)
315 {
316 foreach (const QString& fileName, files) {
317 if (m_shownUrl == KUrl(fileName)) {
318 showItemInfo();
319 break;
320 }
321 }
322 }
323
324 void InformationPanel::slotFilesRemoved(const QStringList& files)
325 {
326 foreach (const QString& fileName, files) {
327 if (m_shownUrl == KUrl(fileName)) {
328 // the currently shown item has been removed, show
329 // the parent directory as fallback
330 reset();
331 break;
332 }
333 }
334 }
335
336 void InformationPanel::slotEnteredDirectory(const QString& directory)
337 {
338 if (m_shownUrl == KUrl(directory)) {
339 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
340 requestDelayedItemInfo(item);
341 }
342 }
343
344 void InformationPanel::slotLeftDirectory(const QString& directory)
345 {
346 if (m_shownUrl == KUrl(directory)) {
347 // The signal 'leftDirectory' is also emitted when a media
348 // has been unmounted. In this case no directory change will be
349 // done in Dolphin, but the Information Panel must be updated to
350 // indicate an invalid directory.
351 reset();
352 }
353 }
354
355 bool InformationPanel::applyPlace(const KUrl& url)
356 {
357 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
358 int count = placesModel->rowCount();
359
360 for (int i = 0; i < count; ++i) {
361 QModelIndex index = placesModel->index(i, 0);
362
363 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
364 setNameLabelText(placesModel->text(index));
365 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
366 return true;
367 }
368 }
369
370 return false;
371 }
372
373 void InformationPanel::cancelRequest()
374 {
375 m_infoTimer->stop();
376 }
377
378 void InformationPanel::showMetaInfo()
379 {
380 m_metaTextLabel->clear();
381
382 if (showMultipleSelectionInfo()) {
383 if (m_metaDataWidget != 0) {
384 KUrl::List urls;
385 foreach (const KFileItem& item, m_selection) {
386 urls.append(item.targetUrl());
387 }
388 m_metaDataWidget->setFiles(urls);
389 }
390
391 quint64 totalSize = 0;
392 foreach (const KFileItem& item, m_selection) {
393 // Only count the size of files, not dirs to match what
394 // DolphinViewContainer::selectionStatusBarText() does.
395 if (!item.isDir() && !item.isLink()) {
396 totalSize += item.size();
397 }
398 }
399 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
400
401 delete m_phononWidget;
402 m_phononWidget = 0;
403 } else {
404 const KFileItem item = fileItem();
405 if (item.isDir()) {
406 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
407 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
408 } else {
409 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
410
411 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
412 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
413
414 #ifdef HAVE_NEPOMUK
415 Nepomuk::Resource res(item.url());
416
417 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
418 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
419 while (it != properties.constEnd()) {
420 Nepomuk::Types::Property prop(it.key());
421 // TODO: use Nepomuk::formatValue(res, prop) if available
422 // instead of it.value().toString()
423 m_metaTextLabel->add(prop.label(), it.value().toString());
424 ++it;
425 }
426 #endif
427 }
428
429 if (m_metaDataWidget != 0) {
430 m_metaDataWidget->setFile(item.targetUrl());
431 }
432
433 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item.mimetype())) {
434 if (m_phononWidget == 0) {
435 m_phononWidget = new PhononWidget(this);
436
437 QVBoxLayout* vBoxLayout = qobject_cast<QVBoxLayout*>(layout());
438 Q_ASSERT(vBoxLayout != 0);
439 vBoxLayout->insertWidget(3, m_phononWidget);
440 }
441 m_phononWidget->setUrl(item.url());
442 } else {
443 delete m_phononWidget;
444 m_phononWidget = 0;
445 }
446 }
447 }
448
449 KFileItem InformationPanel::fileItem() const
450 {
451 if (!m_fileItem.isNull()) {
452 return m_fileItem;
453 }
454
455 if (!m_selection.isEmpty()) {
456 Q_ASSERT(m_selection.count() == 1);
457 return m_selection.first();
458 }
459
460 // no item is hovered and no selection has been done: provide
461 // an item for the directory represented by m_shownUrl
462 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
463 item.refresh();
464 return item;
465 }
466
467 bool InformationPanel::showMultipleSelectionInfo() const
468 {
469 return m_fileItem.isNull() && (m_selection.count() > 1);
470 }
471
472 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
473 {
474 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
475 }
476
477 void InformationPanel::setNameLabelText(const QString& text)
478 {
479 QTextOption textOption;
480 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
481
482 QTextLayout textLayout(text);
483 textLayout.setFont(m_nameLabel->font());
484 textLayout.setTextOption(textOption);
485
486 QString wrappedText;
487 wrappedText.reserve(text.length());
488
489 // wrap the text to fit into the width of m_nameLabel
490 textLayout.beginLayout();
491 QTextLine line = textLayout.createLine();
492 while (line.isValid()) {
493 line.setLineWidth(m_nameLabel->width());
494 wrappedText += text.mid(line.textStart(), line.textLength());
495
496 line = textLayout.createLine();
497 if (line.isValid()) {
498 wrappedText += QChar::LineSeparator;
499 }
500 }
501 textLayout.endLayout();
502
503 m_nameLabel->setText(wrappedText);
504 }
505
506 void InformationPanel::reset()
507 {
508 m_selection.clear();
509 m_shownUrl = url();
510 m_fileItem = KFileItem();
511 showItemInfo();
512 }
513
514 void InformationPanel::init()
515 {
516 const int spacing = KDialog::spacingHint();
517
518 m_infoTimer = new QTimer(this);
519 m_infoTimer->setInterval(300);
520 m_infoTimer->setSingleShot(true);
521 connect(m_infoTimer, SIGNAL(timeout()),
522 this, SLOT(slotInfoTimeout()));
523
524 // Initialize timer for disabling an outdated preview with a small
525 // delay. This prevents flickering if the new preview can be generated
526 // within a very small timeframe.
527 m_outdatedPreviewTimer = new QTimer(this);
528 m_outdatedPreviewTimer->setInterval(300);
529 m_outdatedPreviewTimer->setSingleShot(true);
530 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
531 this, SLOT(markOutdatedPreview()));
532
533 QVBoxLayout* layout = new QVBoxLayout;
534 layout->setSpacing(spacing);
535
536 // name
537 m_nameLabel = new QLabel(this);
538 QFont font = m_nameLabel->font();
539 font.setBold(true);
540 m_nameLabel->setFont(font);
541 m_nameLabel->setAlignment(Qt::AlignHCenter);
542 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
543
544 // preview
545 m_preview = new PixmapViewer(this);
546 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
547 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
548
549 if (MetaDataWidget::metaDataAvailable()) {
550 // rating, comment and tags
551 m_metaDataWidget = new MetaDataWidget(this);
552 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
553 }
554
555 // general meta text information
556 m_metaTextLabel = new MetaTextLabel(this);
557 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
558
559 m_metaTextArea = new QScrollArea(this);
560 m_metaTextArea->setWidget(m_metaTextLabel);
561 m_metaTextArea->setWidgetResizable(true);
562 m_metaTextArea->setFrameShape(QFrame::NoFrame);
563
564 QWidget* viewport = m_metaTextArea->viewport();
565 viewport->installEventFilter(this);
566
567 QPalette palette = viewport->palette();
568 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
569 viewport->setPalette(palette);
570
571 layout->addWidget(m_nameLabel);
572 layout->addWidget(new KSeparator(this));
573 layout->addWidget(m_preview);
574 layout->addWidget(new KSeparator(this));
575 if (m_metaDataWidget != 0) {
576 layout->addWidget(m_metaDataWidget);
577 layout->addWidget(new KSeparator(this));
578 }
579 layout->addWidget(m_metaTextArea);
580 setLayout(layout);
581
582 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
583 QDBusConnection::sessionBus(), this);
584 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
585 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
586 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
587 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
588 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
589 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
590
591 m_initialized = true;
592 }
593
594 #include "informationpanel.moc"