KDE/kdelibs/kdeui

Michael Jansen kde at michael-jansen.biz
Sun Sep 14 02:03:20 BST 2008


SVN commit 860728 by mjansen:

Introducing KActionCategory. A class that is meant to bring order into
KShortcutsDialog.

Please have a look at the apidoc.

I only implemented a minimal api. Please tell me if you need more to work
with that class.

TODO: React to removing actions from a collection. Remove it from the
category too.

CCMAIL: kde at michael-jansen.biz
CCMAIL: kde-core-devel at kde.org

 M  +6 -1      actions/kactioncategory.cpp  
 M  +54 -1     actions/kactioncategory.h  
 M  +34 -3     dialogs/kshortcutseditor.cpp  


--- trunk/KDE/kdelibs/kdeui/actions/kactioncategory.cpp #860727:860728
@@ -18,7 +18,6 @@
 
 #include "kactioncategory.h"
 
-#include <QtCore/QList>
 #include <QtGui/QAction>
 
 #include "kaction.h"
@@ -58,6 +57,12 @@
     }
 
 
+const QList<QAction*> KActionCategory::actions() const
+    {
+    return d->actions;
+    }
+
+
 QAction * KActionCategory::addAction(const QString &name, QAction *action)
     {
     collection()->addAction(name, action);
--- trunk/KDE/kdelibs/kdeui/actions/kactioncategory.h #860727:860728
@@ -22,6 +22,7 @@
 
 #include <QtCore/QObject>
 #include <QtCore/QString>
+#include <QtCore/QList>
 
 #include "kstandardaction.h"
 #include "kactioncollection.h"
@@ -35,7 +36,55 @@
 
 
 /**
- * TODO
+ * Categorize actions for KShortcutsEditor.
+ *
+ * KActionCategory provides a second level to organize the actions in
+ * KShortcutsEditor.
+ *
+ * The first possibility is using more than one action collection. Each
+ * actions collection becomes a top level node.
+ *
+ * + action collection 1
+ *   + first action
+ *   + second action
+ *   + third action
+ * + action collection 2
+ *   + first action
+ *   + second action
+ *   + third action
+ *
+ * Using KActionCategory it's possible to group the actions of one collection.
+ * + action collection 1
+ *   + first action
+ *   + first category
+ *     + action 1 in category
+ *     + action 2 in category
+ *   + second action
+ *
+ * \section Usage
+ *
+ * The usage is analog to action collections. Just create a category and use
+ * it instead of the collection to create the actions.
+ *
+ * \code
+ *
+ * KActionCategory *file = KActionCategory(i18n("File"), actionCollection())
+ * file->addAction(
+ *      KStandardAction::New,   //< see KStandardAction
+ *      this,                   //< Receiver
+ *      SLOT(fileNew()));       //< SLOT
+ *
+ * ... more actions added to file ...
+ *
+ * KActionCategory *edit = KActionCategory(i18n("Edit"), actionCollection())
+ * edit->addAction(
+ *      KStandardAction::Copy,  //< see KStandardAction
+ *      this,                   //< Receiver
+ *      SLOT(fileNew()));       //< SLOT
+ *
+ * ...
+ *
+ * \endcode
  */
 class KDEUI_EXPORT KActionCategory : public QObject
     {
@@ -97,6 +146,10 @@
 
     //@}
 
+    /**
+     * Returns the actions belonging to this category
+      */
+    const QList<QAction*> actions() const;
 
     /**
      * The action collection this category is associated with.
--- trunk/KDE/kdelibs/kdeui/dialogs/kshortcutseditor.cpp #860727:860728
@@ -29,6 +29,8 @@
 #include "kshortcutsdialog_p.h"
 
 #include <QHeaderView>
+#include <QList>
+#include <QObject>
 #include <QTimer>
 #include <QTextDocument>
 #include <QTextTable>
@@ -39,6 +41,7 @@
 
 #include "kaction.h"
 #include "kactioncollection.h"
+#include "kactioncategory.h"
 #include "kdebug.h"
 #include "kglobalaccel.h"
 #include "kmessagebox.h"
@@ -103,11 +106,40 @@
     enum hierarchyLevel {Root = 0, Program, Action/*unused*/};
     KAction *kact;
     QTreeWidgetItem *hier[2];
-    uint l = Program;
     hier[Root] = d->ui.list->invisibleRootItem();
     hier[Program] = d->findOrMakeItem(hier[Root], title.isEmpty() ? i18n("Shortcuts") : title);
 
+    // Set to remember which actions we have seen.
+    QSet<QAction*> actionsSeen;
+
+    // Add all categories in their own subtree below the collections root node
+    QList<KActionCategory*> categories = collection->findChildren<KActionCategory*>();
+    foreach (KActionCategory *category, categories) {
+        hier[Action] = d->findOrMakeItem(hier[Program], category->text());
+        kDebug() << category->text();
+        foreach(QAction *action, category->actions()) {
+            // Set a marker that we have seen this action
+            actionsSeen.insert(action);
+            // This code doesn't allow editing of QAction. I have no idea why :-(
+            // But i guess it's for a good reason so i won't change that. If
+            // anyone know why please add a comment explaining the reason.
+            if ((kact = qobject_cast<KAction *>(action)) && kact->isShortcutConfigurable()) {
+                // If the shortcut is not configurable skip it
+                if (!kact->isShortcutConfigurable()) {
+                    continue;
+                }
+                // Create the editor
+                new KShortcutsEditorItem((hier[Action]), kact);
+            }
+        }
+    }
+
+    // The rest of the shortcuts is added as a direct shild of the action
+    // collections root node
     foreach (QAction *action, collection->actions()) {
+        if (actionsSeen.contains(action)) {
+            continue;
+        }
         // This code doesn't allow editing of QAction. I have no idea why :-(
         // But i guess it's for a good reason so i won't change that. If
         // anyone know why please add a comment explaining the reason.
@@ -117,7 +149,7 @@
                 continue;
             }
             // Create the editor
-            new KShortcutsEditorItem((hier[l]), kact);
+            new KShortcutsEditorItem((hier[Program]), kact);
         }
     }
 
@@ -243,7 +275,6 @@
 
 void KShortcutsEditorPrivate::initGUI( KShortcutsEditor::ActionTypes types, KShortcutsEditor::LetterShortcuts allowLetterShortcuts )
 {
-
     actionTypes = types;
 
     ui.setupUi(q);




More information about the kde-core-devel mailing list