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