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