]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Use sub-menus for the "Sort By"- and "Additional Information"-menu
authorPeter Penz <peter.penz19@gmail.com>
Wed, 14 Mar 2012 15:51:21 +0000 (16:51 +0100)
committerPeter Penz <peter.penz19@gmail.com>
Wed, 14 Mar 2012 15:53:31 +0000 (16:53 +0100)
This allows doing some grouping for the visible roles by e.g. Image,
Document, Music or whatever.

src/kitemviews/kfileitemmodel.cpp
src/kitemviews/kfileitemmodel.h
src/views/dolphinview.cpp
src/views/dolphinviewactionhandler.cpp
src/views/dolphinviewactionhandler.h

index 428587e734ec2214ecd8a9cd5a3ca1d3e1d70ca4..0fef47787763ff746157884977beb93a4e62bb24 100644 (file)
@@ -1977,15 +1977,24 @@ const KFileItemModel::RoleInfoMap* KFileItemModel::rolesInfoMap(int& count)
         { "name",        NameRole,        I18N_NOOP2_NOSTRIP("@label", "Name"),             0, 0 },
         { "size",        SizeRole,        I18N_NOOP2_NOSTRIP("@label", "Size"),             0, 0 },
         { "date",        DateRole,        I18N_NOOP2_NOSTRIP("@label", "Date"),             0, 0 },
-        { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"),      0, 0 },
-        { "owner",       OwnerRole,       I18N_NOOP2_NOSTRIP("@label", "Owner"),            0, 0 },
-        { "group",       GroupRole,       I18N_NOOP2_NOSTRIP("@label", "Group"),            0, 0 },
         { "type",        TypeRole,        I18N_NOOP2_NOSTRIP("@label", "Type"),             0, 0 },
-        { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), 0, 0 },
-        { "path",        PathRole,        I18N_NOOP2_NOSTRIP("@label", "Path"),             0, 0 },
-        { "comment",     CommentRole,     I18N_NOOP2_NOSTRIP("@label", "Comment"),          0, 0 },
+        { "rating",      RatingRole,      I18N_NOOP2_NOSTRIP("@label", "Rating"),           0, 0 },
         { "tags",        TagsRole,        I18N_NOOP2_NOSTRIP("@label", "Tags"),             0, 0 },
-        { "rating",      RatingRole,      I18N_NOOP2_NOSTRIP("@label", "Rating"),           0, 0 }
+        { "comment",     CommentRole,     I18N_NOOP2_NOSTRIP("@label", "Comment"),          0, 0 },
+        { "wordCount",   WordCountRole,   I18N_NOOP2_NOSTRIP("@label", "Word Count"),       I18N_NOOP2_NOSTRIP("@label", "Document") },
+        { "lineCount",   LineCountRole,   I18N_NOOP2_NOSTRIP("@label", "Line Count"),       I18N_NOOP2_NOSTRIP("@label", "Document") },
+        { "imageSize",   ImageSizeRole,   I18N_NOOP2_NOSTRIP("@label", "Image Size"),       I18N_NOOP2_NOSTRIP("@label", "Image") },
+        { "orientation", OrientationRole, I18N_NOOP2_NOSTRIP("@label", "Orientation"),      I18N_NOOP2_NOSTRIP("@label", "Image") },
+        { "artist",      ArtistRole,      I18N_NOOP2_NOSTRIP("@label", "Artist"),           I18N_NOOP2_NOSTRIP("@label", "Music") },
+        { "album",       AlbumRole,       I18N_NOOP2_NOSTRIP("@label", "Album"),            I18N_NOOP2_NOSTRIP("@label", "Music") },
+        { "duration",    DurationRole,    I18N_NOOP2_NOSTRIP("@label", "Duration"),         I18N_NOOP2_NOSTRIP("@label", "Music") },
+        { "track",       TrackRole,       I18N_NOOP2_NOSTRIP("@label", "Track"),            I18N_NOOP2_NOSTRIP("@label", "Music") },
+        { "path",        PathRole,        I18N_NOOP2_NOSTRIP("@label", "Path"),             I18N_NOOP2_NOSTRIP("@label", "Other") },
+        { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), I18N_NOOP2_NOSTRIP("@label", "Other") },
+        { "copiedFrom",  CopiedFromRole,  I18N_NOOP2_NOSTRIP("@label", "Copied From"),      I18N_NOOP2_NOSTRIP("@label", "Other") },
+        { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"),      I18N_NOOP2_NOSTRIP("@label", "Other") },
+        { "owner",       OwnerRole,       I18N_NOOP2_NOSTRIP("@label", "Owner"),            I18N_NOOP2_NOSTRIP("@label", "Other") },
+        { "group",       GroupRole,       I18N_NOOP2_NOSTRIP("@label", "Group"),            I18N_NOOP2_NOSTRIP("@label", "Other") },
     };
 
     count = sizeof(rolesInfoMap) / sizeof(RoleInfoMap);
index 5d7a7fc8b6044e319b50b1aaaaa5113497364219..d48f600dfcf2a0a92a732b72ba6138af1269a06d 100644 (file)
@@ -201,24 +201,17 @@ private slots:
 
 private:
     enum RoleType {
-        NoRole,
-        NameRole,
-        SizeRole,
-        DateRole,
-        PermissionsRole,
-        OwnerRole,
-        GroupRole,
-        TypeRole,
-        DestinationRole,
-        PathRole,
-        CommentRole,
-        TagsRole,
-        RatingRole,
-        IsDirRole,
-        IsExpandedRole,
-        IsExpandableRole,
-        ExpandedParentsCountRole,
-        RolesCount // Mandatory last entry
+        // User visible roles:
+        NoRole, NameRole, SizeRole, DateRole, PermissionsRole, OwnerRole,
+        GroupRole, TypeRole, DestinationRole, PathRole,
+        // User visible roles available with Nepomuk:
+        CommentRole, TagsRole, RatingRole, ImageSizeRole, OrientationRole,
+        WordCountRole, LineCountRole, ArtistRole, AlbumRole, DurationRole, TrackRole,
+        CopiedFromRole,
+        // Non-visible roles:
+        IsDirRole, IsExpandedRole, IsExpandableRole, ExpandedParentsCountRole,
+        // Mandatory last entry:
+        RolesCount
     };
 
     struct ItemData
index 890a39d898f574ea72d0e0a5298baaa7d214fe29..29a132184264c523ef3e03ad2bd044a085b2c0c8 100644 (file)
@@ -779,17 +779,33 @@ void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
     KItemListView* view = m_container->controller()->view();
     const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
 
+    QString groupName;
+    QMenu* groupMenu = 0;
+
     // Add all roles to the menu that can be shown or hidden by the user
     const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
     foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
-        if (info.role != "name") {
-            const QString text = fileItemModel()->roleDescription(info.role);
+        if (info.role == "name") {
+            // It should not be possible to hide the "name" role
+            continue;
+        }
+
+        const QString text = fileItemModel()->roleDescription(info.role);
+        QAction* action = 0;
+        if (info.group.isEmpty()) {
+            action = menu.data()->addAction(text);
+        } else {
+            if (!groupMenu || info.group != groupName) {
+                groupName = info.group;
+                groupMenu = menu.data()->addMenu(groupName);
+            }
 
-            QAction* action = menu.data()->addAction(text);
-            action->setCheckable(true);
-            action->setChecked(visibleRolesSet.contains(info.role));
-            action->setData(info.role);
+            action = groupMenu->addAction(text);
         }
+
+        action->setCheckable(true);
+        action->setChecked(visibleRolesSet.contains(info.role));
+        action->setData(info.role);
     }
 
     QAction* action = menu.data()->exec(pos.toPoint());
index d87e5753204f55e13d6599f19329c9a3788dacc9..aead11d2502854e4631bac8e23a11609eda854b3 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2008 by David Faure <faure@kde.org>                     *
+ *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
@@ -27,9 +28,9 @@
 #include <KAction>
 #include <KActionCollection>
 #include <KActionMenu>
-#include <KFileItemDelegate>
 #include <kitemviews/kfileitemmodel.h>
 #include <KLocale>
+#include <KMenu>
 #include <KNewFileMenu>
 #include <KSelectAction>
 #include <KToggleAction>
 
 #include <KDebug>
 
-DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent)
-    : QObject(parent),
-      m_actionCollection(collection),
-      m_currentView(0)
+DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection* collection, QObject* parent) :
+    QObject(parent),
+    m_actionCollection(collection),
+    m_currentView(0),
+    m_sortByActions(),
+    m_visibleRoles()
 {
     Q_ASSERT(m_actionCollection);
     createActions();
@@ -167,8 +170,7 @@ void DolphinViewActionHandler::createActions()
     connect(sortFoldersFirst, SIGNAL(triggered()), this, SLOT(toggleSortFoldersFirst()));
 
     // View -> Sort By
-    QActionGroup* sortByActionGroup = createSortByActionGroup();
-    connect(sortByActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotSortTriggered(QAction*)));
+    QActionGroup* sortByActionGroup = createFileItemRolesActionGroup("sort_by_");
 
     KActionMenu* sortByActionMenu = m_actionCollection->add<KActionMenu>("sort");
     sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
@@ -182,14 +184,14 @@ void DolphinViewActionHandler::createActions()
     sortByActionMenu->addAction(sortFoldersFirst);
 
     // View -> Additional Information
-    QActionGroup* additionalInfoGroup = createAdditionalInformationActionGroup();
-    connect(additionalInfoGroup, SIGNAL(triggered(QAction*)), this, SLOT(toggleAdditionalInfo(QAction*)));
-
-    KActionMenu* additionalInfoMenu = m_actionCollection->add<KActionMenu>("additional_info");
-    additionalInfoMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
-    additionalInfoMenu->setDelayed(false);
-    foreach (QAction* action, additionalInfoGroup->actions()) {
-        additionalInfoMenu->addAction(action);
+    QActionGroup* visibleRolesGroup = createFileItemRolesActionGroup("show_");
+
+    KActionMenu* visibleRolesMenu = m_actionCollection->add<KActionMenu>("additional_info");
+    visibleRolesMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
+    visibleRolesMenu->setDelayed(false);
+
+    foreach (QAction* action, visibleRolesGroup->actions()) {
+        visibleRolesMenu->addAction(action);
     }
 
     KToggleAction* showInGroups = m_actionCollection->add<KToggleAction>("show_in_groups");
@@ -207,47 +209,70 @@ void DolphinViewActionHandler::createActions()
     connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(slotAdjustViewProperties()));
 }
 
-QActionGroup* DolphinViewActionHandler::createAdditionalInformationActionGroup()
+QActionGroup* DolphinViewActionHandler::createFileItemRolesActionGroup(const QString& groupPrefix)
 {
-    QActionGroup* additionalInfoGroup = new QActionGroup(m_actionCollection);
-    additionalInfoGroup->setExclusive(false);
+    const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
+    Q_ASSERT(isSortGroup || (!isSortGroup && groupPrefix == QLatin1String("show_")));
+
+    QActionGroup* rolesActionGroup = new QActionGroup(m_actionCollection);
+    rolesActionGroup->setExclusive(isSortGroup);
+    if (isSortGroup) {
+        connect(rolesActionGroup, SIGNAL(triggered(QAction*)),
+                this, SLOT(slotSortTriggered(QAction*)));
+    } else {
+        connect(rolesActionGroup, SIGNAL(triggered(QAction*)),
+                this, SLOT(toggleVisibleRole(QAction*)));
+    }
 
-    KActionMenu* showInformationMenu = m_actionCollection->add<KActionMenu>("additional_info");
-    showInformationMenu->setText(i18nc("@action:inmenu View", "Additional Information"));
-    showInformationMenu->setDelayed(false);
+    QString groupName;
+    KActionMenu* groupMenu = 0;
+    QActionGroup* groupMenuGroup = 0;
 
     const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
     foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
-        if (info.role == "name") {
+        if (!isSortGroup && info.role == "name") {
             // It should not be possible to hide the "name" role
             continue;
         }
 
-        const QString name = QLatin1String("show_") + info.role;
-        KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
+        KToggleAction* action = 0;
+        const QString name = groupPrefix + info.role;
+        if (info.group.isEmpty()) {
+            action = m_actionCollection->add<KToggleAction>(name);
+            action->setActionGroup(rolesActionGroup);
+        } else {
+            if (!groupMenu || info.group != groupName) {
+                groupName = info.group;
+                groupMenu = m_actionCollection->add<KActionMenu>(groupName);
+                groupMenu->setText(groupName);
+                groupMenu->setActionGroup(rolesActionGroup);
+
+                groupMenuGroup = new QActionGroup(groupMenu);
+                groupMenuGroup->setExclusive(isSortGroup);
+                if (isSortGroup) {
+                    connect(groupMenuGroup, SIGNAL(triggered(QAction*)),
+                            this, SLOT(slotSortTriggered(QAction*)));
+                } else {
+                    connect(groupMenuGroup, SIGNAL(triggered(QAction*)),
+                            this, SLOT(toggleVisibleRole(QAction*)));
+                }
+            }
+
+            action = new KToggleAction(groupMenu);
+            action->setActionGroup(groupMenuGroup);
+            groupMenu->addAction(action);
+        }
         action->setText(info.translation);
         action->setData(info.role);
-        action->setActionGroup(additionalInfoGroup);
-    }
-
-    return additionalInfoGroup;
-}
-
-QActionGroup* DolphinViewActionHandler::createSortByActionGroup()
-{
-    QActionGroup* sortByActionGroup = new QActionGroup(m_actionCollection);
-    sortByActionGroup->setExclusive(true);
 
-    const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
-    foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
-        const QString name = QLatin1String("sort_by_") + info.role;
-        KToggleAction* action = m_actionCollection->add<KToggleAction>(name);
-        action->setText(info.translation);
-        action->setData(info.role);
-        sortByActionGroup->addAction(action);
+        if (isSortGroup) {
+            m_sortByActions.insert(info.role, action);
+        } else {
+            m_visibleRoles.insert(info.role, action);
+        }
     }
 
-    return sortByActionGroup;
+    return rolesActionGroup;
 }
 
 void DolphinViewActionHandler::slotViewModeActionTriggered(QAction* action)
@@ -383,7 +408,7 @@ void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
     m_actionCollection->action("folders_first")->setChecked(foldersFirst);
 }
 
-void DolphinViewActionHandler::toggleAdditionalInfo(QAction* action)
+void DolphinViewActionHandler::toggleVisibleRole(QAction* action)
 {
     emit actionBeingHandled();
 
@@ -411,13 +436,12 @@ void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray>&
     Q_UNUSED(previous);
 
     const QSet<QByteArray> checkedRoles = current.toSet();
-    const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
-    foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
-        const QString name = QLatin1String("show_") + info.role;
-        QAction* action = m_actionCollection->action(name);
-        if (action) {
-            action->setChecked(checkedRoles.contains(info.role));
-        }
+    QHashIterator<QByteArray, KToggleAction*> it(m_visibleRoles);
+    while (it.hasNext()) {
+        it.next();
+        const QByteArray& role = it.key();
+        KToggleAction* action = it.value();
+        action->setChecked(checkedRoles.contains(role));
     }
 }
 
@@ -479,8 +503,7 @@ KToggleAction* DolphinViewActionHandler::detailsModeAction()
 
 void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray& role)
 {
-    const QString name = QLatin1String("sort_by_") + role;
-    QAction* action = m_actionCollection->action(name);
+    KToggleAction* action = m_sortByActions.value(role);
     if (action) {
         action->setChecked(true);
 
@@ -506,6 +529,25 @@ void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
 
 void DolphinViewActionHandler::slotSortTriggered(QAction* action)
 {
+    // The radiobuttons of the "Sort By"-menu are split between the main-menu
+    // and several sub-menus. Because of this they don't have a common
+    // action-group that assures an exclusive toggle-state between the main-menu
+    // actions and the sub-menu-actions. If an action gets checked, it must
+    // be assured that all other actions get unchecked.
+    QAction* sortByMenu =  m_actionCollection->action("sort");
+    foreach (QAction* groupAction, sortByMenu->menu()->actions()) {
+        KActionMenu* actionMenu = qobject_cast<KActionMenu*>(groupAction);
+        if (actionMenu) {
+            foreach (QAction* subAction, actionMenu->menu()->actions()) {
+                subAction->setChecked(false);
+            }
+        } else if (groupAction->actionGroup()) {
+            groupAction->setChecked(false);
+        }
+    }
+    action->setChecked(true);
+
+    // Apply the activated sort-role to the view
     const QByteArray role = action->data().toByteArray();
     m_currentView->setSortRole(role);
 }
index ff72d95301ee2b9afd2dd9f8900c843fc0b8810c..edbe41b9009d9a04cf86ddb65b221be98aaa83e1 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2008 by David Faure <faure@kde.org>                     *
+ *   Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com>             *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
@@ -162,7 +163,7 @@ private Q_SLOTS:
      * Switches on or off the displaying of additional information
      * as specified by \a action.
      */
-    void toggleAdditionalInfo(QAction* action);
+    void toggleVisibleRole(QAction* action);
 
     /**
      * Changes the sorting of the current view.
@@ -217,16 +218,14 @@ private:
     void createActions();
 
     /**
-     * Creates an action group with all the "show additional information" actions in it.
-     * Helper method for createActions();
-     */
-    QActionGroup* createAdditionalInformationActionGroup();
-
-    /**
-     * Creates an action group with all the "sort by" actions in it.
-     * Helper method for createActions();
+     * Creates an action-group out of all roles from KFileItemModel.
+     * Dependent on the group-prefix either a radiobutton-group is
+     * created for sorting (prefix is "sort_by_") or a checkbox-group
+     * is created for additional information (prefix is "show_").
+     * The changes of actions are reported to slotSortTriggered() or
+     * toggleAdditionalInfo().
      */
-    QActionGroup* createSortByActionGroup();
+    QActionGroup* createFileItemRolesActionGroup(const QString& groupPrefix);
 
     /**
      * Returns the "switch to icons mode" action.
@@ -248,6 +247,9 @@ private:
 
     KActionCollection* m_actionCollection;
     DolphinView* m_currentView;
+
+    QHash<QByteArray, KToggleAction*> m_sortByActions;
+    QHash<QByteArray, KToggleAction*> m_visibleRoles;
 };
 
 #endif /* DOLPHINVIEWACTIONHANDLER_H */