]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
dispatch the preview queue immediately before resuming the preview generation
[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 KFileItem item = fileItem();
187 if (item.isNull()) {
188 return;
189 }
190 const KUrl itemUrl = item.url();
191
192 if (showMultipleSelectionInfo()) {
193 KIconLoader iconLoader;
194 QPixmap icon = iconLoader.loadIcon("dialog-information",
195 KIconLoader::NoGroup,
196 KIconLoader::SizeEnormous);
197 m_preview->setPixmap(icon);
198 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
199 } else if (!applyPlace(itemUrl)) {
200 // try to get a preview pixmap from the item...
201 m_pendingPreview = true;
202
203 // Mark the currently shown preview as outdated. This is done
204 // with a small delay to prevent a flickering when the next preview
205 // can be shown within a short timeframe.
206 m_outdatedPreviewTimer->start();
207
208 KIO::PreviewJob* job = KIO::filePreview(KUrl::List() << itemUrl,
209 m_preview->width(),
210 m_preview->height(),
211 0,
212 0,
213 true,
214 false);
215 job->setIgnoreMaximumSize(true);
216
217 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
218 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
219 connect(job, SIGNAL(failed(const KFileItem&)),
220 this, SLOT(showIcon(const KFileItem&)));
221
222 m_nameLabel->setText(itemUrl.fileName());
223 }
224
225 showMetaInfo();
226 }
227
228 void InfoSidebarPage::slotInfoTimeout()
229 {
230 m_shownUrl = m_urlCandidate;
231 showItemInfo();
232 }
233
234 void InfoSidebarPage::markOutdatedPreview()
235 {
236 KIconEffect iconEffect;
237 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
238 KIconLoader::Desktop,
239 KIconLoader::DisabledState);
240 m_preview->setPixmap(disabledPixmap);
241 }
242
243 void InfoSidebarPage::showIcon(const KFileItem& item)
244 {
245 m_outdatedPreviewTimer->stop();
246 m_pendingPreview = false;
247 if (!applyPlace(item.url())) {
248 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
249 }
250 }
251
252 void InfoSidebarPage::showPreview(const KFileItem& item,
253 const QPixmap& pixmap)
254 {
255 m_outdatedPreviewTimer->stop();
256
257 Q_UNUSED(item);
258 if (m_pendingPreview) {
259 m_preview->setPixmap(pixmap);
260 m_pendingPreview = false;
261 }
262 }
263
264 void InfoSidebarPage::slotFileRenamed(const QString& source, const QString& dest)
265 {
266 if (m_shownUrl == KUrl(source)) {
267 // the currently shown file has been renamed, hence update the item information
268 // for the renamed file
269 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
270 requestDelayedItemInfo(item);
271 }
272 }
273
274 void InfoSidebarPage::slotFilesAdded(const QString& directory)
275 {
276 if (m_shownUrl == KUrl(directory)) {
277 // If the 'trash' icon changes because the trash has been emptied or got filled,
278 // the signal filesAdded("trash:/") will be emitted.
279 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
280 requestDelayedItemInfo(item);
281 }
282 }
283
284 void InfoSidebarPage::slotFilesChanged(const QStringList& files)
285 {
286 foreach (const QString& fileName, files) {
287 if (m_shownUrl == KUrl(fileName)) {
288 showItemInfo();
289 break;
290 }
291 }
292 }
293
294 void InfoSidebarPage::slotFilesRemoved(const QStringList& files)
295 {
296 foreach (const QString& fileName, files) {
297 if (m_shownUrl == KUrl(fileName)) {
298 // the currently shown item has been removed, show
299 // the parent directory as fallback
300 m_shownUrl = url();
301 showItemInfo();
302 break;
303 }
304 }
305 }
306
307 void InfoSidebarPage::slotEnteredDirectory(const QString& directory)
308 {
309 if (m_shownUrl == KUrl(directory)) {
310 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
311 requestDelayedItemInfo(item);
312 }
313 }
314
315 void InfoSidebarPage::slotLeftDirectory(const QString& directory)
316 {
317 if (m_shownUrl == KUrl(directory)) {
318 // The signal 'leftDirectory' is also emitted when a media
319 // has been unmounted. In this case no directory change will be
320 // done in Dolphin, but the Information Panel must be updated to
321 // indicate an invalid directory.
322 m_shownUrl = url();
323 showItemInfo();
324 }
325 }
326
327 bool InfoSidebarPage::applyPlace(const KUrl& url)
328 {
329 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
330 int count = placesModel->rowCount();
331
332 for (int i = 0; i < count; ++i) {
333 QModelIndex index = placesModel->index(i, 0);
334
335 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
336 m_nameLabel->setText(placesModel->text(index));
337 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
338 return true;
339 }
340 }
341
342 return false;
343 }
344
345 void InfoSidebarPage::cancelRequest()
346 {
347 m_infoTimer->stop();
348 }
349
350 void InfoSidebarPage::showMetaInfo()
351 {
352 m_metaTextLabel->clear();
353
354 if (showMultipleSelectionInfo()) {
355 if (m_metaDataWidget != 0) {
356 KUrl::List urls;
357 foreach (const KFileItem& item, m_selection) {
358 urls.append(item.targetUrl());
359 }
360 m_metaDataWidget->setFiles(urls);
361 }
362
363 quint64 totalSize = 0;
364 foreach (const KFileItem& item, m_selection) {
365 // Only count the size of files, not dirs to match what
366 // DolphinViewContainer::selectionStatusBarText() does.
367 if (!item.isDir() && !item.isLink()) {
368 totalSize += item.size();
369 }
370 }
371 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
372 } else {
373 const KFileItem item = fileItem();
374 if (item.isDir()) {
375 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
376 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
377 } else {
378 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
379
380 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
381 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
382
383 if (item.isLocalFile()) {
384 // TODO: See convertMetaInfo below, find a way to display only interesting information
385 // in a readable way
386 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
387 KFileMetaInfo::TechnicalInfo |
388 KFileMetaInfo::ContentInfo;
389 const QString path = item.url().path();
390 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
391 if (fileMetaInfo.isValid()) {
392 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
393 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
394 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
395 QString labelText;
396 while (it != end) {
397 const KFileMetaInfoItem& metaInfoItem = it.value();
398 const QVariant& value = metaInfoItem.value();
399 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
400 m_metaTextLabel->add(labelText, value.toString());
401 }
402 ++it;
403 }
404 }
405 }
406 }
407
408 if (m_metaDataWidget != 0) {
409 m_metaDataWidget->setFile(item.targetUrl());
410 }
411 }
412 }
413
414 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
415 {
416 // TODO: This code prevents that interesting meta information might be hidden
417 // and only bypasses the current problem that not all the meta information should
418 // be shown to the user. Check whether it's possible with Nepomuk to show
419 // all "user relevant" information in a readable way...
420
421 struct MetaKey {
422 const char* key;
423 const char* text;
424 };
425
426 // sorted list of keys, where its data should be shown
427 static const MetaKey keys[] = {
428 { "audio.album", "Album:" },
429 { "audio.artist", "Artist:" },
430 { "audio.title", "Title:" },
431 };
432
433 // do a binary search for the key...
434 int top = 0;
435 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
436 while (top <= bottom) {
437 const int middle = (top + bottom) / 2;
438 const int result = key.compare(keys[middle].key);
439 if (result < 0) {
440 bottom = middle - 1;
441 } else if (result > 0) {
442 top = middle + 1;
443 } else {
444 text = keys[middle].text;
445 return true;
446 }
447 }
448
449 return false;
450 }
451
452 KFileItem InfoSidebarPage::fileItem() const
453 {
454 if (!m_fileItem.isNull()) {
455 return m_fileItem;
456 }
457
458 const KUrl url = m_selection.isEmpty() ? m_shownUrl : m_selection[0].url();
459 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
460 item.refresh();
461 return item;
462 }
463
464 bool InfoSidebarPage::showMultipleSelectionInfo() const
465 {
466 return m_fileItem.isNull() && (m_selection.count() > 1);
467 }
468
469 void InfoSidebarPage::init()
470 {
471 const int spacing = KDialog::spacingHint();
472
473 m_infoTimer = new QTimer(this);
474 m_infoTimer->setInterval(300);
475 m_infoTimer->setSingleShot(true);
476 connect(m_infoTimer, SIGNAL(timeout()),
477 this, SLOT(slotInfoTimeout()));
478
479 // Initialize timer for disabling an outdated preview with a small
480 // delay. This prevents flickering if the new preview can be generated
481 // within a very small timeframe.
482 m_outdatedPreviewTimer = new QTimer(this);
483 m_outdatedPreviewTimer->setInterval(300);
484 m_outdatedPreviewTimer->setSingleShot(true);
485 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
486 this, SLOT(markOutdatedPreview()));
487
488 QVBoxLayout* layout = new QVBoxLayout;
489 layout->setSpacing(spacing);
490
491 // name
492 m_nameLabel = new QLabel(this);
493 QFont font = m_nameLabel->font();
494 font.setBold(true);
495 m_nameLabel->setFont(font);
496 m_nameLabel->setAlignment(Qt::AlignHCenter);
497 m_nameLabel->setWordWrap(true);
498 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
499
500 // preview
501 m_preview = new PixmapViewer(this);
502 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
503 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
504
505 if (MetaDataWidget::metaDataAvailable()) {
506 // rating, comment and tags
507 m_metaDataWidget = new MetaDataWidget(this);
508 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
509 }
510
511 // general meta text information
512 m_metaTextLabel = new MetaTextLabel(this);
513 m_metaTextLabel->setMinimumWidth(spacing);
514 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
515
516 layout->addWidget(m_nameLabel);
517 layout->addWidget(new KSeparator(this));
518 layout->addWidget(m_preview);
519 layout->addWidget(new KSeparator(this));
520 if (m_metaDataWidget != 0) {
521 layout->addWidget(m_metaDataWidget);
522 layout->addWidget(new KSeparator(this));
523 }
524 layout->addWidget(m_metaTextLabel);
525
526 // ensure that widgets in the information side bar are aligned towards the top
527 layout->addStretch(1);
528 setLayout(layout);
529
530 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
531 QDBusConnection::sessionBus(), this);
532 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
533 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
534 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
535 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
536 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
537 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
538
539 m_initialized = true;
540 }
541
542 #include "infosidebarpage.moc"