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