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