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