on xenial help sign in
protect the GlobalShortcut pointer with QPointer
94be131Lukáš Tinkl
plugins/GlobalShortcut/globalshortcutregistry.cpp+30−10
@@ -40,12 +40,31 @@ bool GlobalShortcutRegistry::hasShortcut(const QVariant &seq) const
40
40
41void GlobalShortcutRegistry::addShortcut(const QVariant &seq, GlobalShortcut *sc)
41void GlobalShortcutRegistry::addShortcut(const QVariant &seq, GlobalShortcut *sc)
42{
42{
43+ if (sc) {
43− if (!m_shortcuts.keys().contains(seq)) { // create a new entry
44+ if (!m_shortcuts.keys().contains(seq)) { // create a new entry
44− m_shortcuts.insert(seq, {sc});
45+ m_shortcuts.insert(seq, {sc});
45− } else { // append to an existing one
46+ } else { // append to an existing one
46− auto shortcuts = m_shortcuts[seq];
47+ auto shortcuts = m_shortcuts[seq];
47− shortcuts.append(sc);
48+ shortcuts.append(sc);
48− m_shortcuts.insert(seq, shortcuts);
49+ m_shortcuts.insert(seq, shortcuts);
50+ }
51+
52+ connect(sc, &GlobalShortcut::destroyed, this, &GlobalShortcutRegistry::removeShortcut);
53+ }
54+}
55+
56+void GlobalShortcutRegistry::removeShortcut(QObject *obj)
57+{
58+ QMutableMapIterator<QVariant, QVector<QPointer<GlobalShortcut>>> it(m_shortcuts);
59+ while (it.hasNext()) {
60+ it.next();
61+ GlobalShortcut * scObj = static_cast<GlobalShortcut *>(obj);
62+ if (scObj && it.value().contains(scObj)) {
63+ it.value().removeAll(scObj);
64+ if (it.value().isEmpty()) {
65+ it.remove();
66+ }
67+ }
49 }
68 }
50}
69}
51
70
@@ -56,14 +75,15 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
56
75
57 if (event->type() == QEvent::KeyPress) {
76 if (event->type() == QEvent::KeyPress) {
58 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
77 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
59−
60 QKeySequence seq = QKeySequence(keyEvent->key() + keyEvent->modifiers());
78 QKeySequence seq = QKeySequence(keyEvent->key() + keyEvent->modifiers());
61 if (m_shortcuts.keys().contains(seq)) {
79 if (m_shortcuts.keys().contains(seq)) {
62 const auto shortcuts = m_shortcuts.value(seq);
80 const auto shortcuts = m_shortcuts.value(seq);
63− Q_FOREACH(auto shortcut, shortcuts) {
81+ Q_FOREACH(const auto &shortcut, shortcuts) {
82+ if (shortcut) {
64− qApp->sendEvent(shortcut, keyEvent);
83+ qApp->sendEvent(shortcut, keyEvent);
84+ event->accept();
85+ }
65 }
86 }
66− event->accept();
67 }
87 }
68
88
69 return event->isAccepted();
89 return event->isAccepted();
plugins/GlobalShortcut/globalshortcutregistry.h+4−1
@@ -24,7 +24,7 @@
24
24
25#include "globalshortcut.h"
25#include "globalshortcut.h"
26
26
27−typedef QMap<QVariant, QVector<GlobalShortcut *>> GlobalShortcutList;
27+typedef QMap<QVariant, QVector<QPointer<GlobalShortcut>>> GlobalShortcutList;
28
28
29/**
29/**
30 * @brief The GlobalShortcutRegistry class
30 * @brief The GlobalShortcutRegistry class
@@ -59,6 +59,9 @@ public:
59protected:
59protected:
60 bool eventFilter(QObject *obj, QEvent *event) override;
60 bool eventFilter(QObject *obj, QEvent *event) override;
61
61
62+private Q_SLOTS:
63+ void removeShortcut(QObject *obj);
64+
62private:
65private:
63 GlobalShortcutList m_shortcuts;
66 GlobalShortcutList m_shortcuts;
64 QPointer<QWindow> m_filteredWindow = nullptr;
67 QPointer<QWindow> m_filteredWindow = nullptr;