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