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