Action enhances QAction with a subset of KDE's KAction capabilities
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 19 Sep 2008 22:49:45 +0000 (00:49 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sat, 20 Sep 2008 02:16:07 +0000 (04:16 +0200)
This mostly adds configurable default and custom shortcuts to QAction. The API
we use has been taken from KAction, in order to make KDE integration easier later on.
Note that we only define/implement a subset of KAction's API, since we don't have
global shortcuts and don't support alternate shortcuts (yet?).

src/uisupport/CMakeLists.txt
src/uisupport/action.cpp
src/uisupport/action.h

index 7c235f8..8ce0190 100644 (file)
@@ -7,6 +7,7 @@ include(${QT_USE_FILE})
 set(SOURCES
     abstractbuffercontainer.cpp
     abstractitemview.cpp
+    action.cpp
     bufferview.cpp
     bufferviewfilter.cpp
     clearablelineedit.cpp
@@ -23,6 +24,7 @@ set(SOURCES
 set(MOC_HDRS
     abstractbuffercontainer.h
     abstractitemview.h
+    action.h
     bufferview.h
     bufferviewfilter.h
     clearablelineedit.h
index c98c321..700cce5 100644 (file)
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************
+ * Parts of this implementation are taken from KDE's kaction.cpp           *
  ***************************************************************************/
+
+#include <QApplication>
+
+#include "action.h"
+
+Action::Action(QObject *parent) : QWidgetAction(parent) {
+  init();
+}
+
+Action::Action(const QString &text, QObject *parent) : QWidgetAction(parent) {
+  init();
+  setText(text);
+}
+
+Action::Action(const QIcon &icon, const QString &text, QObject *parent) : QWidgetAction(parent) {
+  init();
+  setIcon(icon);
+  setText(text);
+}
+
+void Action::init() {
+  connect(this, SIGNAL(triggered(bool)), this, SLOT(slotTriggered()));
+
+  setProperty("isShortcutConfigurable", true);
+}
+
+void Action::slotTriggered() {
+  emit triggered(QApplication::mouseButtons(), QApplication::keyboardModifiers());
+}
+
+bool Action::isShortcutConfigurable() const {
+  return property("isShortcutConfigurable").toBool();
+}
+
+void Action::setShortcutConfigurable(bool b) {
+  setProperty("isShortcutConfigurable", b);
+}
+
+QKeySequence Action::shortcut(ShortcutTypes type) const {
+  Q_ASSERT(type);
+  if(type == DefaultShortcut)
+    return property("defaultShortcut").value<QKeySequence>();
+
+  if(shortcuts().count()) return shortcuts().value(0);
+  return QKeySequence();
+}
+
+void Action::setShortcut(const QShortcut &shortcut, ShortcutTypes type) {
+  setShortcut(shortcut.key(), type);
+}
+
+void Action::setShortcut(const QKeySequence &key, ShortcutTypes type) {
+  Q_ASSERT(type);
+
+  if(type & DefaultShortcut)
+    setProperty("defaultShortcut", key);
+
+  if(type & ActiveShortcut)
+    QAction::setShortcut(key);
+}
index 8748ae9..378d45d 100644 (file)
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************
+ * Parts of this API have been shamelessly stolen from KDE's kaction.h     *
  ***************************************************************************/
 
 #ifndef ACTION_H_
 #define ACTION_H_
 
+#include <QShortcut>
 #include <QWidgetAction>
 
+/// A specialized QWidgetAction, enhanced by some KDE features
+/** This declares/implements a subset of KAction's API (notably we've left out global shortcuts
+ *  for now), which should make it easy to plug in KDE support later on.
+ */
 class Action : public QWidgetAction {
   Q_OBJECT
 
-  Q_PROPERTY(QShortcut shortcut READ shortcut WRITE setShortcut)
+  Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut)
   Q_PROPERTY(bool shortcutConfigurable READ isShortcutConfigurable WRITE setShortcutConfigurable)
   Q_FLAGS(ShortcutType)
 
@@ -41,10 +48,23 @@ class Action : public QWidgetAction {
     Action(const QString &text, QObject *parent);
     Action(const QIcon &icon, const QString &text, QObject *parent);
 
-    QShortcut shortcut(ShortcutTypes types = ActiveShortcut) const;
-    void setShortcut(const QShortcut &shortcut, ShortcutTypes type = ActiveShortcut);
-    void setShortcut(const QKeySequence &shortcut, ShortcutTypes type = ActiveShortcut);
+    QKeySequence shortcut(ShortcutTypes types = ActiveShortcut) const;
+    void setShortcut(const QShortcut &shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
+    void setShortcut(const QKeySequence &shortcut, ShortcutTypes type = ShortcutTypes(ActiveShortcut | DefaultShortcut));
 
+    bool isShortcutConfigurable() const;
+    void setShortcutConfigurable(bool configurable);
+
+  signals:
+    void triggered(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers);
+
+  private:
+    void init();
+
+  private slots:
+    void slotTriggered();
 };
 
-Q_DECLARE_OPERATORS_FOR_FLAGS(ShortcutTypes)
+Q_DECLARE_OPERATORS_FOR_FLAGS(Action::ShortcutTypes)
+
+#endif