]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Fixed issue that invoking Dolphin with a specified path is ignored when another Dolph...
[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 #include <kdebug.h>
200 bool InformationPanel::eventFilter(QObject* obj, QEvent* event)
201 {
202 // Check whether the size of the meta text area has changed and adjust
203 // the fixed width in a way that no horizontal scrollbar needs to be shown.
204 if ((obj == m_metaTextArea->viewport()) && (event->type() == QEvent::Resize)) {
205 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
206 m_metaTextLabel->setFixedWidth(resizeEvent->size().width());
207 }
208 return Panel::eventFilter(obj, event);
209 }
210
211 void InformationPanel::showItemInfo()
212 {
213 if (!isVisible()) {
214 return;
215 }
216
217 cancelRequest();
218
219 if (showMultipleSelectionInfo()) {
220 KIconLoader iconLoader;
221 QPixmap icon = iconLoader.loadIcon("dialog-information",
222 KIconLoader::NoGroup,
223 KIconLoader::SizeEnormous);
224 m_preview->setPixmap(icon);
225 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
226 m_shownUrl = KUrl();
227 } else {
228 const KFileItem item = fileItem();
229 const KUrl itemUrl = item.url();
230 if (!applyPlace(itemUrl)) {
231 // try to get a preview pixmap from the item...
232 m_pendingPreview = true;
233
234 // Mark the currently shown preview as outdated. This is done
235 // with a small delay to prevent a flickering when the next preview
236 // can be shown within a short timeframe.
237 m_outdatedPreviewTimer->start();
238
239 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
240 m_preview->width(),
241 m_preview->height(),
242 0,
243 0,
244 false,
245 true);
246
247 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
248 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
249 connect(job, SIGNAL(failed(const KFileItem&)),
250 this, SLOT(showIcon(const KFileItem&)));
251
252 setNameLabelText(itemUrl.fileName());
253 }
254 }
255
256 showMetaInfo();
257 }
258
259 void InformationPanel::slotInfoTimeout()
260 {
261 m_shownUrl = m_urlCandidate;
262 showItemInfo();
263 }
264
265 void InformationPanel::markOutdatedPreview()
266 {
267 KIconEffect iconEffect;
268 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
269 KIconLoader::Desktop,
270 KIconLoader::DisabledState);
271 m_preview->setPixmap(disabledPixmap);
272 }
273
274 void InformationPanel::showIcon(const KFileItem& item)
275 {
276 m_outdatedPreviewTimer->stop();
277 m_pendingPreview = false;
278 if (!applyPlace(item.url())) {
279 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
280 }
281 }
282
283 void InformationPanel::showPreview(const KFileItem& item,
284 const QPixmap& pixmap)
285 {
286 m_outdatedPreviewTimer->stop();
287
288 Q_UNUSED(item);
289 if (m_pendingPreview) {
290 m_preview->setPixmap(pixmap);
291 m_pendingPreview = false;
292 }
293 }
294
295 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
296 {
297 if (m_shownUrl == KUrl(source)) {
298 // the currently shown file has been renamed, hence update the item information
299 // for the renamed file
300 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
301 requestDelayedItemInfo(item);
302 }
303 }
304
305 void InformationPanel::slotFilesAdded(const QString& directory)
306 {
307 if (m_shownUrl == KUrl(directory)) {
308 // If the 'trash' icon changes because the trash has been emptied or got filled,
309 // the signal filesAdded("trash:/") will be emitted.
310 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
311 requestDelayedItemInfo(item);
312 }
313 }
314
315 void InformationPanel::slotFilesChanged(const QStringList& files)
316 {
317 foreach (const QString& fileName, files) {
318 if (m_shownUrl == KUrl(fileName)) {
319 showItemInfo();
320 break;
321 }
322 }
323 }
324
325 void InformationPanel::slotFilesRemoved(const QStringList& files)
326 {
327 foreach (const QString& fileName, files) {
328 if (m_shownUrl == KUrl(fileName)) {
329 // the currently shown item has been removed, show
330 // the parent directory as fallback
331 m_shownUrl = url();
332 showItemInfo();
333 break;
334 }
335 }
336 }
337
338 void InformationPanel::slotEnteredDirectory(const QString& directory)
339 {
340 if (m_shownUrl == KUrl(directory)) {
341 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
342 requestDelayedItemInfo(item);
343 }
344 }
345
346 void InformationPanel::slotLeftDirectory(const QString& directory)
347 {
348 if (m_shownUrl == KUrl(directory)) {
349 // The signal 'leftDirectory' is also emitted when a media
350 // has been unmounted. In this case no directory change will be
351 // done in Dolphin, but the Information Panel must be updated to
352 // indicate an invalid directory.
353 m_shownUrl = url();
354 showItemInfo();
355 }
356 }
357
358 bool InformationPanel::applyPlace(const KUrl& url)
359 {
360 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
361 int count = placesModel->rowCount();
362
363 for (int i = 0; i < count; ++i) {
364 QModelIndex index = placesModel->index(i, 0);
365
366 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
367 setNameLabelText(placesModel->text(index));
368 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
369 return true;
370 }
371 }
372
373 return false;
374 }
375
376 void InformationPanel::cancelRequest()
377 {
378 m_infoTimer->stop();
379 }
380
381 void InformationPanel::showMetaInfo()
382 {
383 m_metaTextLabel->clear();
384
385 if (showMultipleSelectionInfo()) {
386 if (m_metaDataWidget != 0) {
387 KUrl::List urls;
388 foreach (const KFileItem& item, m_selection) {
389 urls.append(item.targetUrl());
390 }
391 m_metaDataWidget->setFiles(urls);
392 }
393
394 quint64 totalSize = 0;
395 foreach (const KFileItem& item, m_selection) {
396 // Only count the size of files, not dirs to match what
397 // DolphinViewContainer::selectionStatusBarText() does.
398 if (!item.isDir() && !item.isLink()) {
399 totalSize += item.size();
400 }
401 }
402 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
403 } else {
404 const KFileItem item = fileItem();
405 if (item.isDir()) {
406 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
407 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
408 } else {
409 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
410
411 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
412 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
413
414 #ifdef HAVE_NEPOMUK
415 Nepomuk::Resource res(item.url());
416
417 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
418 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
419 while (it != properties.constEnd()) {
420 Nepomuk::Types::Property prop(it.key());
421 // TODO: use Nepomuk::formatValue(res, prop) if available
422 // instead of it.value().toString()
423 m_metaTextLabel->add(prop.label(), it.value().toString());
424 ++it;
425 }
426 #endif
427 }
428
429 if (m_metaDataWidget != 0) {
430 m_metaDataWidget->setFile(item.targetUrl());
431 }
432
433 if (Phonon::BackendCapabilities::isMimeTypeAvailable(item.mimetype())) {
434 if (m_phononWidget == 0) {
435 m_phononWidget = new PhononWidget(this);
436
437 QVBoxLayout* vBoxLayout = qobject_cast<QVBoxLayout*>(layout());
438 Q_ASSERT(vBoxLayout != 0);
439 vBoxLayout->insertWidget(3, m_phononWidget);
440 }
441 m_phononWidget->setUrl(item.url());
442 } else {
443 delete m_phononWidget;
444 m_phononWidget = 0;
445 }
446 }
447 }
448
449 KFileItem InformationPanel::fileItem() const
450 {
451 if (!m_fileItem.isNull()) {
452 return m_fileItem;
453 }
454
455 if (!m_selection.isEmpty()) {
456 Q_ASSERT(m_selection.count() == 1);
457 return m_selection.first();
458 }
459
460 // no item is hovered and no selection has been done: provide
461 // an item for the directory represented by m_shownUrl
462 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
463 item.refresh();
464 return item;
465 }
466
467 bool InformationPanel::showMultipleSelectionInfo() const
468 {
469 return m_fileItem.isNull() && (m_selection.count() > 1);
470 }
471
472 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
473 {
474 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
475 }
476
477 void InformationPanel::setNameLabelText(const QString& text)
478 {
479 QTextOption textOption;
480 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
481
482 QTextLayout textLayout(text);
483 textLayout.setFont(m_nameLabel->font());
484 textLayout.setTextOption(textOption);
485
486 QString wrappedText;
487 wrappedText.reserve(text.length());
488
489 // wrap the text to fit into the width of m_nameLabel
490 textLayout.beginLayout();
491 QTextLine line = textLayout.createLine();
492 while (line.isValid()) {
493 line.setLineWidth(m_nameLabel->width());
494 wrappedText += text.mid(line.textStart(), line.textLength());
495
496 line = textLayout.createLine();
497 if (line.isValid()) {
498 wrappedText += QChar::LineSeparator;
499 }
500 }
501 textLayout.endLayout();
502
503 m_nameLabel->setText(wrappedText);
504 }
505
506 void InformationPanel::init()
507 {
508 const int spacing = KDialog::spacingHint();
509
510 m_infoTimer = new QTimer(this);
511 m_infoTimer->setInterval(300);
512 m_infoTimer->setSingleShot(true);
513 connect(m_infoTimer, SIGNAL(timeout()),
514 this, SLOT(slotInfoTimeout()));
515
516 // Initialize timer for disabling an outdated preview with a small
517 // delay. This prevents flickering if the new preview can be generated
518 // within a very small timeframe.
519 m_outdatedPreviewTimer = new QTimer(this);
520 m_outdatedPreviewTimer->setInterval(300);
521 m_outdatedPreviewTimer->setSingleShot(true);
522 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
523 this, SLOT(markOutdatedPreview()));
524
525 QVBoxLayout* layout = new QVBoxLayout;
526 layout->setSpacing(spacing);
527
528 // name
529 m_nameLabel = new QLabel(this);
530 QFont font = m_nameLabel->font();
531 font.setBold(true);
532 m_nameLabel->setFont(font);
533 m_nameLabel->setAlignment(Qt::AlignHCenter);
534 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
535
536 // preview
537 m_preview = new PixmapViewer(this);
538 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
539 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
540
541 if (MetaDataWidget::metaDataAvailable()) {
542 // rating, comment and tags
543 m_metaDataWidget = new MetaDataWidget(this);
544 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
545 }
546
547 // general meta text information
548 m_metaTextLabel = new MetaTextLabel(this);
549 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
550
551 m_metaTextArea = new QScrollArea(this);
552 m_metaTextArea->setWidget(m_metaTextLabel);
553 m_metaTextArea->setWidgetResizable(true);
554 m_metaTextArea->setFrameShape(QFrame::NoFrame);
555
556 QWidget* viewport = m_metaTextArea->viewport();
557 viewport->installEventFilter(this);
558
559 QPalette palette = viewport->palette();
560 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
561 viewport->setPalette(palette);
562
563 layout->addWidget(m_nameLabel);
564 layout->addWidget(new KSeparator(this));
565 layout->addWidget(m_preview);
566 layout->addWidget(new KSeparator(this));
567 if (m_metaDataWidget != 0) {
568 layout->addWidget(m_metaDataWidget);
569 layout->addWidget(new KSeparator(this));
570 }
571 layout->addWidget(m_metaTextArea);
572 setLayout(layout);
573
574 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
575 QDBusConnection::sessionBus(), this);
576 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
577 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
578 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
579 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
580 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
581 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
582
583 m_initialized = true;
584 }
585
586 #include "informationpanel.moc"