]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/panels/information/informationpanel.cpp
allow to hide the rating, comments and tags of the Information Panel
[dolphin.git] / src / panels / information / informationpanel.cpp
index c586742cf88e404d08c74aa996e6f22345ec2a28..2b3124b3cff09427867029b6adae886bdd49f4ec 100644 (file)
 
 #include <kdialog.h>
 #include <kdirnotify.h>
-#include <kfileplacesmodel.h>
-#include <klocale.h>
-#include <kstandarddirs.h>
-#include <kio/previewjob.h>
 #include <kfileitem.h>
-#include <kglobalsettings.h>
 #include <kfilemetainfo.h>
+#include <kfileplacesmodel.h>
+#include <kglobalsettings.h>
+#include <kio/previewjob.h>
 #include <kiconeffect.h>
-#include <kseparator.h>
 #include <kiconloader.h>
+#include <klocale.h>
+#include <kmenu.h>
+#include <kseparator.h>
 
 #ifdef HAVE_NEPOMUK
 #include <Nepomuk/Resource>
 #include <QScrollBar>
 #include <QVBoxLayout>
 
+#include "dolphin_informationpanelsettings.h"
 #include "settings/dolphinsettings.h"
 #include "metadatawidget.h"
 #include "metatextlabel.h"
 #include "phononwidget.h"
 #include "pixmapviewer.h"
 
+/**
+ * Helper function for sorting items with qSort() in
+ * InformationPanel::contextMenu().
+ */
+bool lessThan(const QAction* action1, const QAction* action2)
+{
+    return action1->text() < action2->text();
+}
+
+
 InformationPanel::InformationPanel(QWidget* parent) :
     Panel(parent),
     m_initialized(false),
@@ -207,6 +218,106 @@ bool InformationPanel::eventFilter(QObject* obj, QEvent* event)
     return Panel::eventFilter(obj, event);
 }
 
+void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
+{
+    Panel::contextMenuEvent(event);
+
+#ifdef HAVE_NEPOMUK
+    if (showMultipleSelectionInfo()) {
+        return;
+    }
+
+    KMenu popup(this);
+
+    QAction* ratingAction = popup.addAction(i18nc("@action:inmenu", "Rating"));
+    ratingAction->setCheckable(true);
+    ratingAction->setChecked(InformationPanelSettings::showRating());
+
+    QAction* commentAction = popup.addAction(i18nc("@action:inmenu", "Comment"));
+    commentAction->setCheckable(true);
+    commentAction->setChecked(InformationPanelSettings::showComment());
+
+    QAction* tagsAction = popup.addAction(i18nc("@action:inmenu", "Tags"));
+    tagsAction->setCheckable(true);
+    tagsAction->setChecked(InformationPanelSettings::showTags());
+
+    KConfig config("kmetainformationrc", KConfig::NoGlobals);
+    KConfigGroup settings = config.group("Show");
+    initMetaInfoSettings(settings);
+
+    QList<QAction*> actions;
+
+    // Get all meta information labels that are available for
+    // the currently shown file item and add them to the popup.
+    Nepomuk::Resource res(fileItem().url());
+    QHash<QUrl, Nepomuk::Variant> properties = res.properties();
+    QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
+    while (it != properties.constEnd()) {
+        Nepomuk::Types::Property prop(it.key());
+        const QString key = prop.label();
+
+        // Meta information provided by Nepomuk that is already
+        // available from KFileItem should not be configurable.
+        bool skip = (key == "fileExtension") ||
+                    (key == "name") ||
+                    (key == "sourceModified") ||
+                    (key == "size") ||
+                    (key == "mime type");
+        if (!skip) {
+            // Check whether there is already a meta information
+            // having the same label. In this case don't show it
+            // twice in the menu.
+            foreach (const QAction* action, actions) {
+                if (action->data().toString() == key) {
+                    skip = true;
+                    break;
+                }
+            }
+        }
+
+        if (!skip) {
+            const QString label = key; // TODO
+            QAction* action = new QAction(label, &popup);
+            action->setCheckable(true);
+            action->setChecked(settings.readEntry(key, true));
+            action->setData(key);
+            actions.append(action);
+        }
+
+        ++it;
+    }
+
+    if (actions.count() > 0) {
+        popup.addSeparator();
+
+        // add all items alphabetically sorted to the popup
+        qSort(actions.begin(), actions.end(), lessThan);
+        foreach (QAction* action, actions) {
+            popup.addAction(action);
+        }
+    }
+
+    // Open the popup and adjust the settings for the
+    // selected action.
+    QAction* action = popup.exec(QCursor::pos());
+    if (action == 0) {
+        return;
+    }
+
+    if (action == ratingAction) {
+        // TODO
+    } else if (action == commentAction) {
+        // TODO
+    } else if (action == tagsAction) {
+        // TODO
+    } else {
+        settings.writeEntry(action->data().toString(), action->isChecked());
+        settings.sync();
+        showMetaInfo();
+    }
+#endif
+}
+
 void InformationPanel::showItemInfo()
 {
     if (!isVisible()) {
@@ -293,11 +404,26 @@ void InformationPanel::showPreview(const KFileItem& item,
 
 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
 {
-    if (m_shownUrl == KUrl(source)) {
-        // the currently shown file has been renamed, hence update the item information
-        // for the renamed file
-        KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
-        requestDelayedItemInfo(item);
+    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) {
+        m_shownUrl = KUrl(dest);
+        m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
+        if (isSelected) {
+            m_selection.append(m_fileItem);
+        }
+        showItemInfo();
     }
 }
 
@@ -412,15 +538,22 @@ void InformationPanel::showMetaInfo()
             m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
 
 #ifdef HAVE_NEPOMUK
+            KConfig config("kmetainformationrc", KConfig::NoGlobals);
+            KConfigGroup settings = config.group("Show");
+            initMetaInfoSettings(settings);
+
             Nepomuk::Resource res(item.url());
 
             QHash<QUrl, Nepomuk::Variant> properties = res.properties();
             QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
             while (it != properties.constEnd()) {
                 Nepomuk::Types::Property prop(it.key());
-                // TODO: use Nepomuk::formatValue(res, prop) if available
-                // instead of it.value().toString()
-                m_metaTextLabel->add(prop.label(), it.value().toString());
+                const QString label = prop.label();
+                if (settings.readEntry(label, true)) {
+                    // TODO: use Nepomuk::formatValue(res, prop) if available
+                    // instead of it.value().toString()
+                    m_metaTextLabel->add(label, it.value().toString());
+                }
                 ++it;
             }
 #endif
@@ -511,6 +644,25 @@ void InformationPanel::reset()
     showItemInfo();
 }
 
+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);
+
+        // mark the group as initialized
+        group.writeEntry("initialized", true);
+    }
+}
+
 void InformationPanel::init()
 {
     const int spacing = KDialog::spacingHint();