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