]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/information/informationpanel.cpp
Simplify how Nepomuk comments can be edited by the user:
[dolphin.git] / src / panels / information / informationpanel.cpp
index 2b3124b3cff09427867029b6adae886bdd49f4ec..2150c64659862522d28342488551de169be23024 100644 (file)
@@ -86,8 +86,10 @@ InformationPanel::InformationPanel(QWidget* parent) :
     m_selection(),
     m_nameLabel(0),
     m_preview(0),
+    m_previewSeparator(0),
     m_phononWidget(0),
     m_metaDataWidget(0),
+    m_metaDataSeparator(0),
     m_metaTextArea(0),
     m_metaTextLabel(0)
 {
@@ -95,6 +97,7 @@ InformationPanel::InformationPanel(QWidget* parent) :
 
 InformationPanel::~InformationPanel()
 {
+    InformationPanelSettings::self()->writeConfig();
 }
 
 QSize InformationPanel::sizeHint() const
@@ -199,10 +202,21 @@ void InformationPanel::resizeEvent(QResizeEvent* event)
         const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
         m_nameLabel->setMaximumWidth(maxWidth);
 
+        // The metadata widget also contains a text widget which may return
+        // a large preferred width.
+        if (m_metaDataWidget != 0) {
+            m_metaDataWidget->setMaximumWidth(maxWidth);
+        }
+
         // try to increase the preview as large as possible
         m_preview->setSizeHint(QSize(maxWidth, maxWidth));
         m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
         m_infoTimer->start();
+
+        if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
+            // assure that the size of the video player is the same as the preview size
+            m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
+        }
     }
     Panel::resizeEvent(event);
 }
@@ -229,17 +243,29 @@ void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
 
     KMenu popup(this);
 
+    QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
+    previewAction->setIcon(KIcon("view-preview"));
+    previewAction->setCheckable(true);
+    previewAction->setChecked(InformationPanelSettings::showPreview());
+
+    const bool metaDataAvailable = MetaDataWidget::metaDataAvailable();
+
     QAction* ratingAction = popup.addAction(i18nc("@action:inmenu", "Rating"));
+    ratingAction->setIcon(KIcon("rating"));
     ratingAction->setCheckable(true);
     ratingAction->setChecked(InformationPanelSettings::showRating());
+    ratingAction->setEnabled(metaDataAvailable);
 
     QAction* commentAction = popup.addAction(i18nc("@action:inmenu", "Comment"));
+    commentAction->setIcon(KIcon("text-plain"));
     commentAction->setCheckable(true);
     commentAction->setChecked(InformationPanelSettings::showComment());
+    commentAction->setEnabled(metaDataAvailable);
 
     QAction* tagsAction = popup.addAction(i18nc("@action:inmenu", "Tags"));
     tagsAction->setCheckable(true);
     tagsAction->setChecked(InformationPanelSettings::showTags());
+    tagsAction->setEnabled(metaDataAvailable);
 
     KConfig config("kmetainformationrc", KConfig::NoGlobals);
     KConfigGroup settings = config.group("Show");
@@ -276,7 +302,7 @@ void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
         }
 
         if (!skip) {
-            const QString label = key; // TODO
+            const QString label = tunedLabel(key); // TODO
             QAction* action = new QAction(label, &popup);
             action->setCheckable(true);
             action->setChecked(settings.readEntry(key, true));
@@ -304,17 +330,33 @@ void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
         return;
     }
 
-    if (action == ratingAction) {
-        // TODO
+    const bool isChecked = action->isChecked();
+    if (action == previewAction) {
+        m_preview->setVisible(isChecked);
+        m_previewSeparator->setVisible(isChecked);
+        InformationPanelSettings::setShowPreview(isChecked);
+        updatePhononWidget();
+    } else if (action == ratingAction) {
+        m_metaDataWidget->setRatingVisible(isChecked);
+        InformationPanelSettings::setShowRating(isChecked);
     } else if (action == commentAction) {
-        // TODO
+        m_metaDataWidget->setCommentVisible(isChecked);
+        InformationPanelSettings::setShowComment(isChecked);
     } else if (action == tagsAction) {
-        // TODO
+        m_metaDataWidget->setTagsVisible(isChecked);
+        InformationPanelSettings::setShowTags(isChecked);
     } else {
         settings.writeEntry(action->data().toString(), action->isChecked());
         settings.sync();
         showMetaInfo();
     }
+
+    if (m_metaDataWidget != 0) {
+        const bool visible = m_metaDataWidget->isRatingVisible() ||
+                             m_metaDataWidget->isCommentVisible() ||
+                             m_metaDataWidget->areTagsVisible();
+        m_metaDataSeparator->setVisible(visible);
+    }
 #endif
 }
 
@@ -404,25 +446,9 @@ void InformationPanel::showPreview(const KFileItem& item,
 
 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
 {
-    const KUrl sourceUrl = KUrl(source);
-
-    // Verify whether the renamed item is selected. If this is the case, the
-    // selection must be updated with the renamed item.
-    bool isSelected = false;
-    for (int i = m_selection.size() - 1; i >= 0; --i) {
-        if (m_selection[i].url() == sourceUrl) {
-            m_selection.removeAt(i);
-            isSelected = true;
-            break;
-        }
-    }
-
-    if ((m_shownUrl == sourceUrl) || isSelected) {
+    if (m_shownUrl == KUrl(source)) {
         m_shownUrl = KUrl(dest);
         m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
-        if (isSelected) {
-            m_selection.append(m_fileItem);
-        }
         showItemInfo();
     }
 }
@@ -478,6 +504,16 @@ void InformationPanel::slotLeftDirectory(const QString& directory)
     }
 }
 
+void InformationPanel::slotPlayingStarted()
+{
+    m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
+}
+
+void InformationPanel::slotPlayingStopped()
+{
+    m_preview->setVisible(true);
+}
+
 bool InformationPanel::applyPlace(const KUrl& url)
 {
     KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
@@ -523,9 +559,6 @@ void InformationPanel::showMetaInfo()
             }
         }
         m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
-
-        delete m_phononWidget;
-        m_phononWidget = 0;
     } else {
         const KFileItem item = fileItem();
         if (item.isDir()) {
@@ -550,9 +583,11 @@ void InformationPanel::showMetaInfo()
                 Nepomuk::Types::Property prop(it.key());
                 const QString label = prop.label();
                 if (settings.readEntry(label, true)) {
-                    // TODO: use Nepomuk::formatValue(res, prop) if available
+                    // TODO #1: use Nepomuk::formatValue(res, prop) if available
                     // instead of it.value().toString()
-                    m_metaTextLabel->add(label, it.value().toString());
+                    // TODO #2: using tunedLabel() is a workaround for KDE 4.3 until
+                    // we get translated labels
+                    m_metaTextLabel->add(tunedLabel(label) + ':', it.value().toString());
                 }
                 ++it;
             }
@@ -562,21 +597,9 @@ void InformationPanel::showMetaInfo()
         if (m_metaDataWidget != 0) {
             m_metaDataWidget->setFile(item.targetUrl());
         }
-
-        if (Phonon::BackendCapabilities::isMimeTypeAvailable(item.mimetype())) {
-            if (m_phononWidget == 0) {
-                m_phononWidget = new PhononWidget(this);
-
-                QVBoxLayout* vBoxLayout = qobject_cast<QVBoxLayout*>(layout());
-                Q_ASSERT(vBoxLayout != 0);
-                vBoxLayout->insertWidget(3, m_phononWidget);
-            }
-            m_phononWidget->setUrl(item.url());
-        } else {
-            delete m_phononWidget;
-            m_phononWidget = 0;
-        }
     }
+
+    updatePhononWidget();
 }
 
 KFileItem InformationPanel::fileItem() const
@@ -649,24 +672,77 @@ void InformationPanel::initMetaInfoSettings(KConfigGroup& group)
     if (!group.readEntry("initialized", false)) {
         // The resource file is read the first time. Assure
         // that some meta information is disabled per default.
-        group.writeEntry("fileExtension", false);
-        group.writeEntry("url", false);
-        group.writeEntry("sourceModified", false);
-        group.writeEntry("parentUrl", false);
-        group.writeEntry("size", false);
-        group.writeEntry("mime type", false);
-        group.writeEntry("depth", false);
-        group.writeEntry("name", false);
+
+        static const char* disabledProperties[] = {
+            "asText", "contentSize", "depth", "fileExtension",
+            "fileName", "fileSize", "isPartOf", "mimetype", "name",
+            "parentUrl", "plainTextContent", "sourceModified",
+            "size", "url",
+            0 // mandatory last entry
+        };
+
+        int i = 0;
+        while (disabledProperties[i] != 0) {
+            group.writeEntry(disabledProperties[i], false);
+            ++i;
+        }
 
         // mark the group as initialized
         group.writeEntry("initialized", true);
     }
 }
 
-void InformationPanel::init()
+void InformationPanel::updatePhononWidget()
+{
+    const bool multipleSelections = showMultipleSelectionInfo();
+    const bool showPreview = InformationPanelSettings::showPreview();
+
+    if (multipleSelections || !showPreview) {
+        m_phononWidget->hide();
+    } else if (!multipleSelections && showPreview) {
+        const KFileItem item = fileItem();
+        const QString mimeType = item.mimetype();
+        const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
+                               (mimeType != "image/png");  // TODO: workaround, as Phonon
+                                                           // thinks it supports PNG images
+        if (usePhonon) {
+            m_phononWidget->show();
+            PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
+                                      ? PhononWidget::Video
+                                      : PhononWidget::Audio;
+            m_phononWidget->setMode(mode);
+            m_phononWidget->setUrl(item.url());
+            if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
+                m_phononWidget->setVideoSize(m_preview->size());
+            }
+        } else {
+            m_phononWidget->hide();
+            m_preview->setVisible(true);
+        }
+    }
+}
+
+QString InformationPanel::tunedLabel(const QString& label) const
 {
-    const int spacing = KDialog::spacingHint();
+    QString tunedLabel;
+    const int labelLength = label.length();
+    if (labelLength > 0) {
+        tunedLabel.reserve(labelLength);
+        tunedLabel = label[0].toUpper();
+        for (int i = 1; i < labelLength; ++i) {
+            if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
+                tunedLabel += ' ';
+                tunedLabel += label[i].toLower();
+            } else {
+                tunedLabel += label[i];
+            }
+        }
+    }
+    return tunedLabel;
+}
 
+void InformationPanel::init()
+{
     m_infoTimer = new QTimer(this);
     m_infoTimer->setInterval(300);
     m_infoTimer->setSingleShot(true);
@@ -683,7 +759,7 @@ void InformationPanel::init()
             this, SLOT(markOutdatedPreview()));
 
     QVBoxLayout* layout = new QVBoxLayout;
-    layout->setSpacing(spacing);
+    layout->setSpacing(KDialog::spacingHint());
 
     // name
     m_nameLabel = new QLabel(this);
@@ -692,16 +768,44 @@ void InformationPanel::init()
     m_nameLabel->setFont(font);
     m_nameLabel->setAlignment(Qt::AlignHCenter);
     m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
+    m_nameLabel->setMaximumWidth(KIconLoader::SizeEnormous);
 
     // preview
+    const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
+
     m_preview = new PixmapViewer(this);
-    m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
+    m_preview->setMinimumWidth(minPreviewWidth);
     m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
 
+    m_phononWidget = new PhononWidget(this);
+    m_phononWidget->setMinimumWidth(minPreviewWidth);
+    connect(m_phononWidget, SIGNAL(playingStarted()),
+            this, SLOT(slotPlayingStarted()));
+    connect(m_phononWidget, SIGNAL(playingStopped()),
+            this, SLOT(slotPlayingStopped()));
+
+    m_previewSeparator = new KSeparator(this);
+
+    const bool showPreview = InformationPanelSettings::showPreview();
+    m_preview->setVisible(showPreview);
+    m_previewSeparator->setVisible(showPreview);
+
     if (MetaDataWidget::metaDataAvailable()) {
         // rating, comment and tags
         m_metaDataWidget = new MetaDataWidget(this);
         m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
+        m_metaDataWidget->setMaximumWidth(KIconLoader::SizeEnormous);
+
+        const bool showRating  = InformationPanelSettings::showRating();
+        const bool showComment = InformationPanelSettings::showComment();
+        const bool showTags    = InformationPanelSettings::showTags();
+
+        m_metaDataWidget->setRatingVisible(showRating);
+        m_metaDataWidget->setCommentVisible(showComment);
+        m_metaDataWidget->setTagsVisible(showTags);
+
+        m_metaDataSeparator = new KSeparator(this);
+        m_metaDataSeparator->setVisible(showRating || showComment || showTags);
     }
 
     // general meta text information
@@ -723,10 +827,11 @@ void InformationPanel::init()
     layout->addWidget(m_nameLabel);
     layout->addWidget(new KSeparator(this));
     layout->addWidget(m_preview);
-    layout->addWidget(new KSeparator(this));
+    layout->addWidget(m_phononWidget);
+    layout->addWidget(m_previewSeparator);
     if (m_metaDataWidget != 0) {
         layout->addWidget(m_metaDataWidget);
-        layout->addWidget(new KSeparator(this));
+        layout->addWidget(m_metaDataSeparator);
     }
     layout->addWidget(m_metaTextArea);
     setLayout(layout);