]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Use the name of the property instead of the label, otherwise the stored keys would...
[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 <kfileitem.h>
27 #include <kfilemetainfo.h>
28 #include <kfileplacesmodel.h>
29 #include <kglobalsettings.h>
30 #include <kio/previewjob.h>
31 #include <kiconeffect.h>
32 #include <kiconloader.h>
33 #include <klocale.h>
34 #include <kmenu.h>
35 #include <kseparator.h>
36
37 #ifdef HAVE_NEPOMUK
38
39 #define DISABLE_NEPOMUK_LEGACY
40
41 #include <Nepomuk/Resource>
42 #include <Nepomuk/Types/Property>
43 #include <Nepomuk/Variant>
44 #endif
45
46 #include <Phonon/BackendCapabilities>
47 #include <Phonon/MediaObject>
48 #include <Phonon/SeekSlider>
49
50 #include <QEvent>
51 #include <QInputDialog>
52 #include <QLabel>
53 #include <QPainter>
54 #include <QPixmap>
55 #include <QResizeEvent>
56 #include <QScrollArea>
57 #include <QTextLayout>
58 #include <QTextLine>
59 #include <QTimer>
60 #include <QScrollBar>
61 #include <QVBoxLayout>
62
63 #include "dolphin_informationpanelsettings.h"
64 #include "settings/dolphinsettings.h"
65 #include "metadatawidget.h"
66 #include "metatextlabel.h"
67 #include "phononwidget.h"
68 #include "pixmapviewer.h"
69
70 /**
71 * Helper function for sorting items with qSort() in
72 * InformationPanel::contextMenu().
73 */
74 bool lessThan(const QAction* action1, const QAction* action2)
75 {
76 return action1->text() < action2->text();
77 }
78
79
80 InformationPanel::InformationPanel(QWidget* parent) :
81 Panel(parent),
82 m_initialized(false),
83 m_pendingPreview(false),
84 m_infoTimer(0),
85 m_outdatedPreviewTimer(0),
86 m_shownUrl(),
87 m_urlCandidate(),
88 m_fileItem(),
89 m_selection(),
90 m_nameLabel(0),
91 m_preview(0),
92 m_previewSeparator(0),
93 m_phononWidget(0),
94 m_metaDataWidget(0),
95 m_metaDataSeparator(0),
96 m_metaTextArea(0),
97 m_metaTextLabel(0)
98 {
99 }
100
101 InformationPanel::~InformationPanel()
102 {
103 InformationPanelSettings::self()->writeConfig();
104 }
105
106 QSize InformationPanel::sizeHint() const
107 {
108 QSize size = Panel::sizeHint();
109 size.setWidth(minimumSizeHint().width());
110 return size;
111 }
112
113 void InformationPanel::setUrl(const KUrl& url)
114 {
115 Panel::setUrl(url);
116 if (url.isValid() && !isEqualToShownUrl(url)) {
117 if (isVisible()) {
118 cancelRequest();
119 m_shownUrl = url;
120 showItemInfo();
121 } else {
122 m_shownUrl = url;
123 }
124 }
125 }
126
127 void InformationPanel::setSelection(const KFileItemList& selection)
128 {
129 if (!isVisible()) {
130 return;
131 }
132
133 if ((selection.count() == 0) && (m_selection.count() == 0)) {
134 // The selection has not really changed, only the current index.
135 // QItemSelectionModel emits a signal in this case and it is less
136 // expensive doing the check this way instead of patching
137 // DolphinView::emitSelectionChanged().
138 return;
139 }
140
141 m_selection = selection;
142
143 const int count = selection.count();
144 if (count == 0) {
145 if (!isEqualToShownUrl(url())) {
146 m_shownUrl = url();
147 showItemInfo();
148 }
149 } else {
150 if ((count == 1) && !selection.first().url().isEmpty()) {
151 m_urlCandidate = selection.first().url();
152 }
153 m_infoTimer->start();
154 }
155 }
156
157 void InformationPanel::requestDelayedItemInfo(const KFileItem& item)
158 {
159 if (!isVisible()) {
160 return;
161 }
162
163 cancelRequest();
164
165 m_fileItem = KFileItem();
166 if (item.isNull()) {
167 // The cursor is above the viewport. If files are selected,
168 // show information regarding the selection.
169 if (m_selection.size() > 0) {
170 m_pendingPreview = false;
171 m_infoTimer->start();
172 }
173 } else {
174 const KUrl url = item.url();
175 if (url.isValid() && !isEqualToShownUrl(url)) {
176 m_urlCandidate = item.url();
177 m_fileItem = item;
178 m_infoTimer->start();
179 }
180 }
181 }
182
183 void InformationPanel::showEvent(QShowEvent* event)
184 {
185 Panel::showEvent(event);
186 if (!event->spontaneous()) {
187 if (!m_initialized) {
188 // do a delayed initialization so that no performance
189 // penalty is given when Dolphin is started with a closed
190 // Information Panel
191 init();
192 }
193 showItemInfo();
194 }
195 }
196
197 void InformationPanel::resizeEvent(QResizeEvent* event)
198 {
199 if (isVisible()) {
200 // If the text inside the name label or the info label cannot
201 // get wrapped, then the maximum width of the label is increased
202 // so that the width of the information panel gets increased.
203 // To prevent this, the maximum width is adjusted to
204 // the current width of the panel.
205 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
206 m_nameLabel->setMaximumWidth(maxWidth);
207
208 // The metadata widget also contains a text widget which may return
209 // a large preferred width.
210 if (m_metaDataWidget != 0) {
211 m_metaDataWidget->setMaximumWidth(maxWidth);
212 }
213
214 // try to increase the preview as large as possible
215 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
216 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
217 m_infoTimer->start();
218
219 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
220 // assure that the size of the video player is the same as the preview size
221 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
222 }
223 }
224 Panel::resizeEvent(event);
225 }
226
227 bool InformationPanel::eventFilter(QObject* obj, QEvent* event)
228 {
229 // Check whether the size of the meta text area has changed and adjust
230 // the fixed width in a way that no horizontal scrollbar needs to be shown.
231 if ((obj == m_metaTextArea->viewport()) && (event->type() == QEvent::Resize)) {
232 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
233 m_metaTextLabel->setFixedWidth(resizeEvent->size().width());
234 }
235 return Panel::eventFilter(obj, event);
236 }
237
238 void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
239 {
240 Panel::contextMenuEvent(event);
241
242 #ifdef HAVE_NEPOMUK
243 if (showMultipleSelectionInfo()) {
244 return;
245 }
246
247 KMenu popup(this);
248
249 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
250 previewAction->setIcon(KIcon("view-preview"));
251 previewAction->setCheckable(true);
252 previewAction->setChecked(InformationPanelSettings::showPreview());
253
254 const bool metaDataAvailable = MetaDataWidget::metaDataAvailable();
255
256 QAction* ratingAction = popup.addAction(i18nc("@action:inmenu", "Rating"));
257 ratingAction->setIcon(KIcon("rating"));
258 ratingAction->setCheckable(true);
259 ratingAction->setChecked(InformationPanelSettings::showRating());
260 ratingAction->setEnabled(metaDataAvailable);
261
262 QAction* commentAction = popup.addAction(i18nc("@action:inmenu", "Comment"));
263 commentAction->setIcon(KIcon("text-plain"));
264 commentAction->setCheckable(true);
265 commentAction->setChecked(InformationPanelSettings::showComment());
266 commentAction->setEnabled(metaDataAvailable);
267
268 QAction* tagsAction = popup.addAction(i18nc("@action:inmenu", "Tags"));
269 tagsAction->setCheckable(true);
270 tagsAction->setChecked(InformationPanelSettings::showTags());
271 tagsAction->setEnabled(metaDataAvailable);
272
273 KConfig config("kmetainformationrc", KConfig::NoGlobals);
274 KConfigGroup settings = config.group("Show");
275 initMetaInfoSettings(settings);
276
277 QList<QAction*> actions;
278
279 // Get all meta information labels that are available for
280 // the currently shown file item and add them to the popup.
281 Nepomuk::Resource res(fileItem().url());
282 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
283 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
284 while (it != properties.constEnd()) {
285 Nepomuk::Types::Property prop(it.key());
286 const QString key = prop.name();
287
288 // Meta information provided by Nepomuk that is already
289 // available from KFileItem should not be configurable.
290 bool skip = (key == "fileExtension") ||
291 (key == "name") ||
292 (key == "sourceModified") ||
293 (key == "size") ||
294 (key == "mime type");
295 if (!skip) {
296 // Check whether there is already a meta information
297 // having the same label. In this case don't show it
298 // twice in the menu.
299 foreach (const QAction* action, actions) {
300 if (action->data().toString() == key) {
301 skip = true;
302 break;
303 }
304 }
305 }
306
307 if (!skip) {
308 const QString label = tunedLabel(prop.label());
309 QAction* action = new QAction(label, &popup);
310 action->setCheckable(true);
311 action->setChecked(settings.readEntry(key, true));
312 action->setData(key);
313 actions.append(action);
314 }
315
316 ++it;
317 }
318
319 if (actions.count() > 0) {
320 popup.addSeparator();
321
322 // add all items alphabetically sorted to the popup
323 qSort(actions.begin(), actions.end(), lessThan);
324 foreach (QAction* action, actions) {
325 popup.addAction(action);
326 }
327 }
328
329 // Open the popup and adjust the settings for the
330 // selected action.
331 QAction* action = popup.exec(QCursor::pos());
332 if (action == 0) {
333 return;
334 }
335
336 const bool isChecked = action->isChecked();
337 if (action == previewAction) {
338 m_preview->setVisible(isChecked);
339 m_previewSeparator->setVisible(isChecked);
340 InformationPanelSettings::setShowPreview(isChecked);
341 updatePhononWidget();
342 } else if (action == ratingAction) {
343 m_metaDataWidget->setRatingVisible(isChecked);
344 InformationPanelSettings::setShowRating(isChecked);
345 } else if (action == commentAction) {
346 m_metaDataWidget->setCommentVisible(isChecked);
347 InformationPanelSettings::setShowComment(isChecked);
348 } else if (action == tagsAction) {
349 m_metaDataWidget->setTagsVisible(isChecked);
350 InformationPanelSettings::setShowTags(isChecked);
351 } else {
352 settings.writeEntry(action->data().toString(), action->isChecked());
353 settings.sync();
354 showMetaInfo();
355 }
356
357 if (m_metaDataWidget != 0) {
358 const bool visible = m_metaDataWidget->isRatingVisible() ||
359 m_metaDataWidget->isCommentVisible() ||
360 m_metaDataWidget->areTagsVisible();
361 m_metaDataSeparator->setVisible(visible);
362 }
363 #endif
364 }
365
366 void InformationPanel::showItemInfo()
367 {
368 if (!isVisible()) {
369 return;
370 }
371
372 cancelRequest();
373
374 if (showMultipleSelectionInfo()) {
375 KIconLoader iconLoader;
376 QPixmap icon = iconLoader.loadIcon("dialog-information",
377 KIconLoader::NoGroup,
378 KIconLoader::SizeEnormous);
379 m_preview->setPixmap(icon);
380 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
381 m_shownUrl = KUrl();
382 } else {
383 const KFileItem item = fileItem();
384 const KUrl itemUrl = item.url();
385 if (!applyPlace(itemUrl)) {
386 // try to get a preview pixmap from the item...
387 m_pendingPreview = true;
388
389 // Mark the currently shown preview as outdated. This is done
390 // with a small delay to prevent a flickering when the next preview
391 // can be shown within a short timeframe.
392 m_outdatedPreviewTimer->start();
393
394 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
395 m_preview->width(),
396 m_preview->height(),
397 0,
398 0,
399 false,
400 true);
401
402 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
403 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
404 connect(job, SIGNAL(failed(const KFileItem&)),
405 this, SLOT(showIcon(const KFileItem&)));
406
407 setNameLabelText(itemUrl.fileName());
408 }
409 }
410
411 showMetaInfo();
412 }
413
414 void InformationPanel::slotInfoTimeout()
415 {
416 m_shownUrl = m_urlCandidate;
417 showItemInfo();
418 }
419
420 void InformationPanel::markOutdatedPreview()
421 {
422 KIconEffect iconEffect;
423 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
424 KIconLoader::Desktop,
425 KIconLoader::DisabledState);
426 m_preview->setPixmap(disabledPixmap);
427 }
428
429 void InformationPanel::showIcon(const KFileItem& item)
430 {
431 m_outdatedPreviewTimer->stop();
432 m_pendingPreview = false;
433 if (!applyPlace(item.url())) {
434 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
435 }
436 }
437
438 void InformationPanel::showPreview(const KFileItem& item,
439 const QPixmap& pixmap)
440 {
441 m_outdatedPreviewTimer->stop();
442
443 Q_UNUSED(item);
444 if (m_pendingPreview) {
445 m_preview->setPixmap(pixmap);
446 m_pendingPreview = false;
447 }
448 }
449
450 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
451 {
452 if (m_shownUrl == KUrl(source)) {
453 m_shownUrl = KUrl(dest);
454 m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
455 showItemInfo();
456 }
457 }
458
459 void InformationPanel::slotFilesAdded(const QString& directory)
460 {
461 if (m_shownUrl == KUrl(directory)) {
462 // If the 'trash' icon changes because the trash has been emptied or got filled,
463 // the signal filesAdded("trash:/") will be emitted.
464 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
465 requestDelayedItemInfo(item);
466 }
467 }
468
469 void InformationPanel::slotFilesChanged(const QStringList& files)
470 {
471 foreach (const QString& fileName, files) {
472 if (m_shownUrl == KUrl(fileName)) {
473 showItemInfo();
474 break;
475 }
476 }
477 }
478
479 void InformationPanel::slotFilesRemoved(const QStringList& files)
480 {
481 foreach (const QString& fileName, files) {
482 if (m_shownUrl == KUrl(fileName)) {
483 // the currently shown item has been removed, show
484 // the parent directory as fallback
485 reset();
486 break;
487 }
488 }
489 }
490
491 void InformationPanel::slotEnteredDirectory(const QString& directory)
492 {
493 if (m_shownUrl == KUrl(directory)) {
494 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
495 requestDelayedItemInfo(item);
496 }
497 }
498
499 void InformationPanel::slotLeftDirectory(const QString& directory)
500 {
501 if (m_shownUrl == KUrl(directory)) {
502 // The signal 'leftDirectory' is also emitted when a media
503 // has been unmounted. In this case no directory change will be
504 // done in Dolphin, but the Information Panel must be updated to
505 // indicate an invalid directory.
506 reset();
507 }
508 }
509
510 void InformationPanel::slotPlayingStarted()
511 {
512 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
513 }
514
515 void InformationPanel::slotPlayingStopped()
516 {
517 m_preview->setVisible(true);
518 }
519
520 bool InformationPanel::applyPlace(const KUrl& url)
521 {
522 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
523 int count = placesModel->rowCount();
524
525 for (int i = 0; i < count; ++i) {
526 QModelIndex index = placesModel->index(i, 0);
527
528 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
529 setNameLabelText(placesModel->text(index));
530 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
531 return true;
532 }
533 }
534
535 return false;
536 }
537
538 void InformationPanel::cancelRequest()
539 {
540 m_infoTimer->stop();
541 }
542
543 void InformationPanel::showMetaInfo()
544 {
545 m_metaTextLabel->clear();
546
547 if (showMultipleSelectionInfo()) {
548 if (m_metaDataWidget != 0) {
549 KUrl::List urls;
550 foreach (const KFileItem& item, m_selection) {
551 urls.append(item.targetUrl());
552 }
553 m_metaDataWidget->setFiles(urls);
554 }
555
556 quint64 totalSize = 0;
557 foreach (const KFileItem& item, m_selection) {
558 // Only count the size of files, not dirs to match what
559 // DolphinViewContainer::selectionStatusBarText() does.
560 if (!item.isDir() && !item.isLink()) {
561 totalSize += item.size();
562 }
563 }
564 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
565 } else {
566 const KFileItem item = fileItem();
567 if (item.isDir()) {
568 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
569 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
570 } else {
571 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
572
573 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
574 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
575
576 #ifdef HAVE_NEPOMUK
577 KConfig config("kmetainformationrc", KConfig::NoGlobals);
578 KConfigGroup settings = config.group("Show");
579 initMetaInfoSettings(settings);
580
581 Nepomuk::Resource res(item.url());
582
583 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
584 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
585 while (it != properties.constEnd()) {
586 Nepomuk::Types::Property prop(it.key());
587 if (settings.readEntry(prop.name(), true)) {
588 // TODO #1: use Nepomuk::formatValue(res, prop) if available
589 // instead of it.value().toString()
590 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 until
591 // we get translated labels
592 m_metaTextLabel->add(tunedLabel(prop.label()) + ':', it.value().toString());
593 }
594 ++it;
595 }
596 #endif
597 }
598
599 if (m_metaDataWidget != 0) {
600 m_metaDataWidget->setFile(item.targetUrl());
601 }
602 }
603
604 updatePhononWidget();
605 }
606
607 KFileItem InformationPanel::fileItem() const
608 {
609 if (!m_fileItem.isNull()) {
610 return m_fileItem;
611 }
612
613 if (!m_selection.isEmpty()) {
614 Q_ASSERT(m_selection.count() == 1);
615 return m_selection.first();
616 }
617
618 // no item is hovered and no selection has been done: provide
619 // an item for the directory represented by m_shownUrl
620 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
621 item.refresh();
622 return item;
623 }
624
625 bool InformationPanel::showMultipleSelectionInfo() const
626 {
627 return m_fileItem.isNull() && (m_selection.count() > 1);
628 }
629
630 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
631 {
632 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
633 }
634
635 void InformationPanel::setNameLabelText(const QString& text)
636 {
637 QTextOption textOption;
638 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
639
640 QTextLayout textLayout(text);
641 textLayout.setFont(m_nameLabel->font());
642 textLayout.setTextOption(textOption);
643
644 QString wrappedText;
645 wrappedText.reserve(text.length());
646
647 // wrap the text to fit into the width of m_nameLabel
648 textLayout.beginLayout();
649 QTextLine line = textLayout.createLine();
650 while (line.isValid()) {
651 line.setLineWidth(m_nameLabel->width());
652 wrappedText += text.mid(line.textStart(), line.textLength());
653
654 line = textLayout.createLine();
655 if (line.isValid()) {
656 wrappedText += QChar::LineSeparator;
657 }
658 }
659 textLayout.endLayout();
660
661 m_nameLabel->setText(wrappedText);
662 }
663
664 void InformationPanel::reset()
665 {
666 m_selection.clear();
667 m_shownUrl = url();
668 m_fileItem = KFileItem();
669 showItemInfo();
670 }
671
672 void InformationPanel::initMetaInfoSettings(KConfigGroup& group)
673 {
674 if (!group.readEntry("initialized", false)) {
675 // The resource file is read the first time. Assure
676 // that some meta information is disabled per default.
677
678 static const char* disabledProperties[] = {
679 "asText", "contentSize", "depth", "fileExtension",
680 "fileName", "fileSize", "isPartOf", "mimetype", "name",
681 "parentUrl", "plainTextContent", "sourceModified",
682 "size", "url",
683 0 // mandatory last entry
684 };
685
686 int i = 0;
687 while (disabledProperties[i] != 0) {
688 group.writeEntry(disabledProperties[i], false);
689 ++i;
690 }
691
692 // mark the group as initialized
693 group.writeEntry("initialized", true);
694 }
695 }
696
697 void InformationPanel::updatePhononWidget()
698 {
699 const bool multipleSelections = showMultipleSelectionInfo();
700 const bool showPreview = InformationPanelSettings::showPreview();
701
702 if (multipleSelections || !showPreview) {
703 m_phononWidget->hide();
704 } else if (!multipleSelections && showPreview) {
705 const KFileItem item = fileItem();
706 const QString mimeType = item.mimetype();
707 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
708 (mimeType != "image/png"); // TODO: workaround, as Phonon
709 // thinks it supports PNG images
710 if (usePhonon) {
711 m_phononWidget->show();
712 PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
713 ? PhononWidget::Video
714 : PhononWidget::Audio;
715 m_phononWidget->setMode(mode);
716 m_phononWidget->setUrl(item.url());
717 if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
718 m_phononWidget->setVideoSize(m_preview->size());
719 }
720 } else {
721 m_phononWidget->hide();
722 m_preview->setVisible(true);
723 }
724 }
725 }
726
727 QString InformationPanel::tunedLabel(const QString& label) const
728 {
729 QString tunedLabel;
730 const int labelLength = label.length();
731 if (labelLength > 0) {
732 tunedLabel.reserve(labelLength);
733 tunedLabel = label[0].toUpper();
734 for (int i = 1; i < labelLength; ++i) {
735 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
736 tunedLabel += ' ';
737 tunedLabel += label[i].toLower();
738 } else {
739 tunedLabel += label[i];
740 }
741 }
742 }
743 return tunedLabel;
744 }
745
746 void InformationPanel::init()
747 {
748 m_infoTimer = new QTimer(this);
749 m_infoTimer->setInterval(300);
750 m_infoTimer->setSingleShot(true);
751 connect(m_infoTimer, SIGNAL(timeout()),
752 this, SLOT(slotInfoTimeout()));
753
754 // Initialize timer for disabling an outdated preview with a small
755 // delay. This prevents flickering if the new preview can be generated
756 // within a very small timeframe.
757 m_outdatedPreviewTimer = new QTimer(this);
758 m_outdatedPreviewTimer->setInterval(300);
759 m_outdatedPreviewTimer->setSingleShot(true);
760 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
761 this, SLOT(markOutdatedPreview()));
762
763 QVBoxLayout* layout = new QVBoxLayout;
764 layout->setSpacing(KDialog::spacingHint());
765
766 // name
767 m_nameLabel = new QLabel(this);
768 QFont font = m_nameLabel->font();
769 font.setBold(true);
770 m_nameLabel->setFont(font);
771 m_nameLabel->setAlignment(Qt::AlignHCenter);
772 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
773 m_nameLabel->setMaximumWidth(KIconLoader::SizeEnormous);
774
775 // preview
776 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
777
778 m_preview = new PixmapViewer(this);
779 m_preview->setMinimumWidth(minPreviewWidth);
780 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
781
782 m_phononWidget = new PhononWidget(this);
783 m_phononWidget->setMinimumWidth(minPreviewWidth);
784 connect(m_phononWidget, SIGNAL(playingStarted()),
785 this, SLOT(slotPlayingStarted()));
786 connect(m_phononWidget, SIGNAL(playingStopped()),
787 this, SLOT(slotPlayingStopped()));
788
789 m_previewSeparator = new KSeparator(this);
790
791 const bool showPreview = InformationPanelSettings::showPreview();
792 m_preview->setVisible(showPreview);
793 m_previewSeparator->setVisible(showPreview);
794
795 if (MetaDataWidget::metaDataAvailable()) {
796 // rating, comment and tags
797 m_metaDataWidget = new MetaDataWidget(this);
798 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
799 m_metaDataWidget->setMaximumWidth(KIconLoader::SizeEnormous);
800
801 const bool showRating = InformationPanelSettings::showRating();
802 const bool showComment = InformationPanelSettings::showComment();
803 const bool showTags = InformationPanelSettings::showTags();
804
805 m_metaDataWidget->setRatingVisible(showRating);
806 m_metaDataWidget->setCommentVisible(showComment);
807 m_metaDataWidget->setTagsVisible(showTags);
808
809 m_metaDataSeparator = new KSeparator(this);
810 m_metaDataSeparator->setVisible(showRating || showComment || showTags);
811 }
812
813 // general meta text information
814 m_metaTextLabel = new MetaTextLabel(this);
815 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
816
817 m_metaTextArea = new QScrollArea(this);
818 m_metaTextArea->setWidget(m_metaTextLabel);
819 m_metaTextArea->setWidgetResizable(true);
820 m_metaTextArea->setFrameShape(QFrame::NoFrame);
821
822 QWidget* viewport = m_metaTextArea->viewport();
823 viewport->installEventFilter(this);
824
825 QPalette palette = viewport->palette();
826 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
827 viewport->setPalette(palette);
828
829 layout->addWidget(m_nameLabel);
830 layout->addWidget(new KSeparator(this));
831 layout->addWidget(m_preview);
832 layout->addWidget(m_phononWidget);
833 layout->addWidget(m_previewSeparator);
834 if (m_metaDataWidget != 0) {
835 layout->addWidget(m_metaDataWidget);
836 layout->addWidget(m_metaDataSeparator);
837 }
838 layout->addWidget(m_metaTextArea);
839 setLayout(layout);
840
841 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
842 QDBusConnection::sessionBus(), this);
843 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
844 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
845 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
846 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
847 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
848 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
849
850 m_initialized = true;
851 }
852
853 #include "informationpanel.moc"