]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Details view related fixes: all columns except the name column should act as viewport.
[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 if ((m_selection.size() <= 1) || !m_fileItem.isNull()) {
343 KFileItem fileItem;
344 if (m_fileItem.isNull()) {
345 // no pending request is ongoing
346 const KUrl url = (m_selection.size() == 1) ? m_selection.first().url() : m_shownUrl;
347 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
348 fileItem.refresh();
349 } else {
350 fileItem = m_fileItem;
351 }
352
353 if (fileItem.isDir()) {
354 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
355 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
356 } else {
357 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
358
359 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
360 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
361
362 if (fileItem.isLocalFile()) {
363 // TODO: See convertMetaInfo below, find a way to display only interesting information
364 // in a readable way
365 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
366 KFileMetaInfo::TechnicalInfo |
367 KFileMetaInfo::ContentInfo;
368 const QString path = fileItem.url().path();
369 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
370 if (fileMetaInfo.isValid()) {
371 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
372 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
373 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
374 QString labelText;
375 while (it != end) {
376 const KFileMetaInfoItem& metaInfoItem = it.value();
377 const QVariant& value = metaInfoItem.value();
378 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
379 m_metaTextLabel->add(labelText, value.toString());
380 }
381 ++it;
382 }
383 }
384 }
385 }
386
387 if (m_metaDataWidget != 0) {
388 m_metaDataWidget->setFile(fileItem.targetUrl());
389 }
390 } else {
391 if (m_metaDataWidget != 0) {
392 KUrl::List urls;
393 foreach (const KFileItem& item, m_selection) {
394 urls.append(item.targetUrl());
395 }
396 m_metaDataWidget->setFiles(urls);
397 }
398
399 quint64 totalSize = 0;
400 foreach (const KFileItem& item, m_selection) {
401 // Only count the size of files, not dirs to match what
402 // DolphinViewContainer::selectionStatusBarText() does.
403 if (!item.isDir() && !item.isLink()) {
404 totalSize += item.size();
405 }
406 }
407 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
408 }
409 }
410
411 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
412 {
413 // TODO: This code prevents that interesting meta information might be hidden
414 // and only bypasses the current problem that not all the meta information should
415 // be shown to the user. Check whether it's possible with Nepomuk to show
416 // all "user relevant" information in a readable way...
417
418 struct MetaKey {
419 const char* key;
420 const char* text;
421 };
422
423 // sorted list of keys, where its data should be shown
424 static const MetaKey keys[] = {
425 { "audio.album", "Album:" },
426 { "audio.artist", "Artist:" },
427 { "audio.title", "Title:" },
428 };
429
430 // do a binary search for the key...
431 int top = 0;
432 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
433 while (top <= bottom) {
434 const int middle = (top + bottom) / 2;
435 const int result = key.compare(keys[middle].key);
436 if (result < 0) {
437 bottom = middle - 1;
438 } else if (result > 0) {
439 top = middle + 1;
440 } else {
441 text = keys[middle].text;
442 return true;
443 }
444 }
445
446 return false;
447 }
448
449 void InfoSidebarPage::init()
450 {
451 const int spacing = KDialog::spacingHint();
452
453 m_timer = new QTimer(this);
454 m_timer->setSingleShot(true);
455 connect(m_timer, SIGNAL(timeout()),
456 this, SLOT(slotTimeout()));
457
458 QVBoxLayout* layout = new QVBoxLayout;
459 layout->setSpacing(spacing);
460
461 // name
462 m_nameLabel = new QLabel(this);
463 QFont font = m_nameLabel->font();
464 font.setBold(true);
465 m_nameLabel->setFont(font);
466 m_nameLabel->setAlignment(Qt::AlignHCenter);
467 m_nameLabel->setWordWrap(true);
468 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
469
470 // preview
471 m_preview = new PixmapViewer(this);
472 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
473 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
474
475 if (MetaDataWidget::metaDataAvailable()) {
476 // rating, comment and tags
477 m_metaDataWidget = new MetaDataWidget(this);
478 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
479 }
480
481 // general meta text information
482 m_metaTextLabel = new MetaTextLabel(this);
483 m_metaTextLabel->setMinimumWidth(spacing);
484 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
485
486 layout->addWidget(m_nameLabel);
487 layout->addWidget(new KSeparator(this));
488 layout->addWidget(m_preview);
489 layout->addWidget(new KSeparator(this));
490 if (m_metaDataWidget != 0) {
491 layout->addWidget(m_metaDataWidget);
492 layout->addWidget(new KSeparator(this));
493 }
494 layout->addWidget(m_metaTextLabel);
495
496 // ensure that widgets in the information side bar are aligned towards the top
497 layout->addStretch(1);
498 setLayout(layout);
499
500 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
501 QDBusConnection::sessionBus(), this);
502 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
503 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
504 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
505 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
506 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
507 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
508
509 m_initialized = true;
510 }
511
512 #include "infosidebarpage.moc"