]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Don't grey out an invalid preview immediately, but with a small timeout. This prevent...
[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() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
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 m_shownUrl = url();
112 showItemInfo();
113 } else {
114 if ((count == 1) && !selection.first().url().isEmpty()) {
115 m_urlCandidate = selection.first().url();
116 }
117 m_infoTimer->start();
118 }
119 }
120
121 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
122 {
123 if (!m_initialized) {
124 return;
125 }
126
127 cancelRequest();
128
129 m_fileItem = KFileItem();
130 if (item.isNull()) {
131 // The cursor is above the viewport. If files are selected,
132 // show information regarding the selection.
133 if (m_selection.size() > 0) {
134 m_pendingPreview = false;
135 m_infoTimer->start();
136 }
137 } else if (!item.url().isEmpty()) {
138 m_urlCandidate = item.url();
139 m_fileItem = item;
140 m_infoTimer->start();
141 }
142 }
143
144 void InfoSidebarPage::showEvent(QShowEvent* event)
145 {
146 SidebarPage::showEvent(event);
147 if (!event->spontaneous()) {
148 if (!m_initialized) {
149 // do a delayed initialization so that no performance
150 // penalty is given when Dolphin is started with a closed
151 // Information Panel
152 init();
153 }
154 showItemInfo();
155 }
156 }
157
158 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
159 {
160 if (m_initialized) {
161 // If the text inside the name label or the info label cannot
162 // get wrapped, then the maximum width of the label is increased
163 // so that the width of the information sidebar gets increased.
164 // To prevent this, the maximum width is adjusted to
165 // the current width of the sidebar.
166 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
167 m_nameLabel->setMaximumWidth(maxWidth);
168
169 // try to increase the preview as large as possible
170 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
171 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
172 m_infoTimer->start();
173 }
174
175 SidebarPage::resizeEvent(event);
176 }
177
178 void InfoSidebarPage::showItemInfo()
179 {
180 if (!isVisible()) {
181 return;
182 }
183
184 cancelRequest();
185
186 const KUrl file = fileUrl();
187 if (!file.isValid()) {
188 return;
189 }
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 } else if (!applyPlace(file)) {
199 // try to get a preview pixmap from the item...
200 KUrl::List list;
201 list.append(file);
202
203 m_pendingPreview = true;
204
205 // Mark the currently shown preview as outdated. This is done
206 // with a small delay to prevent a flickering when the next preview
207 // can be shown within a short timeframe.
208 m_outdatedPreviewTimer->start();
209
210 KIO::PreviewJob* job = KIO::filePreview(list,
211 m_preview->width(),
212 m_preview->height(),
213 0,
214 0,
215 true,
216 false);
217 job->setIgnoreMaximumSize(true);
218
219 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
220 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
221 connect(job, SIGNAL(failed(const KFileItem&)),
222 this, SLOT(showIcon(const KFileItem&)));
223
224 m_nameLabel->setText(file.fileName());
225 }
226
227 showMetaInfo();
228 }
229
230 void InfoSidebarPage::slotInfoTimeout()
231 {
232 m_shownUrl = m_urlCandidate;
233 showItemInfo();
234 }
235
236 void InfoSidebarPage::markOutdatedPreview()
237 {
238 KIconEffect iconEffect;
239 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
240 KIconLoader::Desktop,
241 KIconLoader::DisabledState);
242 m_preview->setPixmap(disabledPixmap);
243 }
244
245 void InfoSidebarPage::showIcon(const KFileItem& item)
246 {
247 m_outdatedPreviewTimer->stop();
248 m_pendingPreview = false;
249 if (!applyPlace(item.url())) {
250 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
251 }
252 }
253
254 void InfoSidebarPage::showPreview(const KFileItem& item,
255 const QPixmap& pixmap)
256 {
257 m_outdatedPreviewTimer->stop();
258
259 Q_UNUSED(item);
260 if (m_pendingPreview) {
261 m_preview->setPixmap(pixmap);
262 m_pendingPreview = false;
263 }
264 }
265
266 void InfoSidebarPage::slotFileRenamed(const QString& source, const QString& dest)
267 {
268 if (m_shownUrl == KUrl(source)) {
269 // the currently shown file has been renamed, hence update the item information
270 // for the renamed file
271 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
272 requestDelayedItemInfo(item);
273 }
274 }
275
276 void InfoSidebarPage::slotFilesAdded(const QString& directory)
277 {
278 if (m_shownUrl == KUrl(directory)) {
279 // If the 'trash' icon changes because the trash has been emptied or got filled,
280 // the signal filesAdded("trash:/") will be emitted.
281 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
282 requestDelayedItemInfo(item);
283 }
284 }
285
286 void InfoSidebarPage::slotFilesChanged(const QStringList& files)
287 {
288 foreach (const QString& fileName, files) {
289 if (m_shownUrl == KUrl(fileName)) {
290 showItemInfo();
291 break;
292 }
293 }
294 }
295
296 void InfoSidebarPage::slotFilesRemoved(const QStringList& files)
297 {
298 foreach (const QString& fileName, files) {
299 if (m_shownUrl == KUrl(fileName)) {
300 // the currently shown item has been removed, show
301 // the parent directory as fallback
302 m_shownUrl = url();
303 showItemInfo();
304 break;
305 }
306 }
307 }
308
309 void InfoSidebarPage::slotEnteredDirectory(const QString& directory)
310 {
311 if (m_shownUrl == KUrl(directory)) {
312 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
313 requestDelayedItemInfo(item);
314 }
315 }
316
317 void InfoSidebarPage::slotLeftDirectory(const QString& directory)
318 {
319 if (m_shownUrl == KUrl(directory)) {
320 // The signal 'leftDirectory' is also emitted when a media
321 // has been unmounted. In this case no directory change will be
322 // done in Dolphin, but the Information Panel must be updated to
323 // indicate an invalid directory.
324 m_shownUrl = url();
325 showItemInfo();
326 }
327 }
328
329 bool InfoSidebarPage::applyPlace(const KUrl& url)
330 {
331 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
332 int count = placesModel->rowCount();
333
334 for (int i = 0; i < count; ++i) {
335 QModelIndex index = placesModel->index(i, 0);
336
337 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
338 m_nameLabel->setText(placesModel->text(index));
339 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
340 return true;
341 }
342 }
343
344 return false;
345 }
346
347 void InfoSidebarPage::cancelRequest()
348 {
349 m_infoTimer->stop();
350 }
351
352 void InfoSidebarPage::showMetaInfo()
353 {
354 m_metaTextLabel->clear();
355
356 if (showMultipleSelectionInfo()) {
357 if (m_metaDataWidget != 0) {
358 KUrl::List urls;
359 foreach (const KFileItem& item, m_selection) {
360 urls.append(item.targetUrl());
361 }
362 m_metaDataWidget->setFiles(urls);
363 }
364
365 quint64 totalSize = 0;
366 foreach (const KFileItem& item, m_selection) {
367 // Only count the size of files, not dirs to match what
368 // DolphinViewContainer::selectionStatusBarText() does.
369 if (!item.isDir() && !item.isLink()) {
370 totalSize += item.size();
371 }
372 }
373 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
374 } else {
375 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, fileUrl());
376 fileItem.refresh();
377
378 if (fileItem.isDir()) {
379 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
380 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
381 } else {
382 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
383
384 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
385 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
386
387 if (fileItem.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 = fileItem.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(fileItem.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 KUrl InfoSidebarPage::fileUrl() const
457 {
458 return (!m_fileItem.isNull() || m_selection.isEmpty()) ? m_shownUrl : m_selection[0].url();
459 }
460
461 bool InfoSidebarPage::showMultipleSelectionInfo() const
462 {
463 return m_fileItem.isNull() && (m_selection.count() > 1);
464 }
465
466 void InfoSidebarPage::init()
467 {
468 const int spacing = KDialog::spacingHint();
469
470 m_infoTimer = new QTimer(this);
471 m_infoTimer->setInterval(300);
472 m_infoTimer->setSingleShot(true);
473 connect(m_infoTimer, SIGNAL(timeout()),
474 this, SLOT(slotInfoTimeout()));
475
476 // Initialize timer for disabling an outdated preview with a small
477 // delay. This prevents flickering if the new preview can be generated
478 // within a very small timeframe.
479 m_outdatedPreviewTimer = new QTimer(this);
480 m_outdatedPreviewTimer->setInterval(300);
481 m_outdatedPreviewTimer->setSingleShot(true);
482 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
483 this, SLOT(markOutdatedPreview()));
484
485 QVBoxLayout* layout = new QVBoxLayout;
486 layout->setSpacing(spacing);
487
488 // name
489 m_nameLabel = new QLabel(this);
490 QFont font = m_nameLabel->font();
491 font.setBold(true);
492 m_nameLabel->setFont(font);
493 m_nameLabel->setAlignment(Qt::AlignHCenter);
494 m_nameLabel->setWordWrap(true);
495 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
496
497 // preview
498 m_preview = new PixmapViewer(this);
499 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
500 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
501
502 if (MetaDataWidget::metaDataAvailable()) {
503 // rating, comment and tags
504 m_metaDataWidget = new MetaDataWidget(this);
505 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
506 }
507
508 // general meta text information
509 m_metaTextLabel = new MetaTextLabel(this);
510 m_metaTextLabel->setMinimumWidth(spacing);
511 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
512
513 layout->addWidget(m_nameLabel);
514 layout->addWidget(new KSeparator(this));
515 layout->addWidget(m_preview);
516 layout->addWidget(new KSeparator(this));
517 if (m_metaDataWidget != 0) {
518 layout->addWidget(m_metaDataWidget);
519 layout->addWidget(new KSeparator(this));
520 }
521 layout->addWidget(m_metaTextLabel);
522
523 // ensure that widgets in the information side bar are aligned towards the top
524 layout->addStretch(1);
525 setLayout(layout);
526
527 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
528 QDBusConnection::sessionBus(), this);
529 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
530 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
531 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
532 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
533 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
534 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
535
536 m_initialized = true;
537 }
538
539 #include "infosidebarpage.moc"