Change in plasma-framework[master]: Make keyboard shortcuts work

Marco Martin (Code Review) noreply at kde.org
Fri Jan 23 17:02:09 UTC 2015


Marco Martin has uploaded a new change for review.

  https://gerrit.vesnicky.cesnet.cz/r/342

Change subject: Make keyboard shortcuts work
......................................................................

Make keyboard shortcuts work

QAction keyboard shortcuts cannot work with QML2 (and probably newver will
since in Qt qtquick and qwidgets cannot depend from each other in any way)
so do a simple keyboard shortcut matching here

BUG:336203
Change-Id: I2d7ada7dfcb0e326e63ce7f1e39573709f6fe560
---
M src/scriptengines/qml/plasmoid/appletinterface.cpp
M src/scriptengines/qml/plasmoid/appletinterface.h
2 files changed, 47 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.vesnicky.cesnet.cz:29418/plasma-framework refs/changes/42/342/1

diff --git a/src/scriptengines/qml/plasmoid/appletinterface.cpp b/src/scriptengines/qml/plasmoid/appletinterface.cpp
index a27258b..0080b0d 100644
--- a/src/scriptengines/qml/plasmoid/appletinterface.cpp
+++ b/src/scriptengines/qml/plasmoid/appletinterface.cpp
@@ -60,6 +60,7 @@
       m_backgroundHints(Plasma::Types::StandardBackground),
       m_busy(false),
       m_hideOnDeactivate(true),
+      m_oldKeyboardShortcut(0),
       m_positionBeforeRemoval(QPointF(-1, -1))
 {
     qmlRegisterType<QAction>();
@@ -599,6 +600,49 @@
     }
 }
 
+bool AppletInterface::event(QEvent *event)
+{
+    // QAction keyboard shortcuts cannot work with QML2 (and probably newver will
+    // since in Qt qtquick and qwidgets cannot depend from each other in any way)
+    // so do a simple keyboard shortcut matching here
+    if (event->type() == QEvent::KeyPress) {
+        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
+        QKeySequence seq(ke->key()|ke->modifiers());
+
+        for (auto a : applet()->actions()->actions()) {
+
+            if (a->shortcut().isEmpty()) {
+                continue;
+            }
+
+            //this will happen on a normal, non emacs shortcut
+            if (seq.matches(a->shortcut()) == QKeySequence::ExactMatch) {
+                event->accept();
+                a->trigger();
+                m_oldKeyboardShortcut = 0;
+                return true;
+
+            //first part of an emacs style shortcut?
+            } else if (seq.matches(a->shortcut()) == QKeySequence::PartialMatch) {
+                m_oldKeyboardShortcut = ke->key()|ke->modifiers();
+
+            //no match at all, but it can be the second part of an emacs style shortcut
+            } else {
+                QKeySequence seq(m_oldKeyboardShortcut, ke->key()|ke->modifiers());
+
+                if (seq.matches(a->shortcut()) == QKeySequence::ExactMatch) {
+                    event->accept();
+                    a->trigger();
+                    m_oldKeyboardShortcut = 0;
+                    return true;
+                }
+            }
+        }
+    }
+
+    return AppletQuickItem::event(event);
+}
+
 bool AppletInterface::eventFilter(QObject *watched, QEvent *event)
 {
     if (event->type() == QEvent::MouseButtonPress) {
diff --git a/src/scriptengines/qml/plasmoid/appletinterface.h b/src/scriptengines/qml/plasmoid/appletinterface.h
index 281a3a0..be19a6c 100644
--- a/src/scriptengines/qml/plasmoid/appletinterface.h
+++ b/src/scriptengines/qml/plasmoid/appletinterface.h
@@ -351,6 +351,7 @@
     virtual void init();
 
 protected:
+    bool event(QEvent *event);
     bool eventFilter(QObject *watched, QEvent *event);
 
 private Q_SLOTS:
@@ -372,6 +373,8 @@
     Plasma::Types::BackgroundHints m_backgroundHints;
     bool m_busy : 1;
     bool m_hideOnDeactivate : 1;
+    //this is used to build an emacs style shortcut
+    int m_oldKeyboardShortcut;
 
     friend class ContainmentInterface;
     //This is used by ContainmentInterface

-- 
To view, visit https://gerrit.vesnicky.cesnet.cz/r/342
To unsubscribe, visit https://gerrit.vesnicky.cesnet.cz/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2d7ada7dfcb0e326e63ce7f1e39573709f6fe560
Gerrit-PatchSet: 1
Gerrit-Project: plasma-framework
Gerrit-Branch: master
Gerrit-Owner: Marco Martin <notmart at gmail.com>


More information about the Plasma-devel mailing list