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