Lomiri
Loading...
Searching...
No Matches
GlobalShortcutRegistry Class Reference

The GlobalShortcutRegistry class. More...

#include <plugins/GlobalShortcut/globalshortcutregistry.h>

Inherits QObject.

Public Member Functions

 GlobalShortcutRegistry (QObject *parent=nullptr)
 
GlobalShortcutList shortcuts () const
 
bool hasShortcut (const QVariant &seq) const
 
void addShortcut (const QVariant &seq, GlobalShortcut *sc)
 
void setupFilterOnWindow (QWindow *window)
 

Protected Member Functions

bool eventFilter (QObject *obj, QEvent *event) override
 

Detailed Description

The GlobalShortcutRegistry class.

Serves as a central point for shortcut registration.

Definition at line 34 of file globalshortcutregistry.h.

Constructor & Destructor Documentation

◆ GlobalShortcutRegistry()

GlobalShortcutRegistry::GlobalShortcutRegistry ( QObject *  parent = nullptr)

Definition at line 38 of file globalshortcutregistry.cpp.

39 : QObject(parent)
40{
41 connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &GlobalShortcutRegistry::setupFilterOnWindow);
42 setupFilterOnWindow(qGuiApp->focusWindow());
43}
void setupFilterOnWindow(QWindow *window)

Member Function Documentation

◆ addShortcut()

void GlobalShortcutRegistry::addShortcut ( const QVariant &  seq,
GlobalShortcut sc 
)

Adds a shortcut seq to the registry

Definition at line 55 of file globalshortcutregistry.cpp.

56{
57 if (sc) {
58 if (!m_shortcuts.contains(seq)) { // create a new entry
59 m_shortcuts.insert(seq, {sc});
60 } else { // append to an existing one
61 auto shortcuts = m_shortcuts[seq];
62 shortcuts.append(sc);
63 m_shortcuts.insert(seq, shortcuts);
64 }
65
66 connect(sc, &GlobalShortcut::destroyed, this, &GlobalShortcutRegistry::removeShortcut);
67 }
68}
GlobalShortcutList shortcuts() const

◆ eventFilter()

bool GlobalShortcutRegistry::eventFilter ( QObject *  obj,
QEvent *  event 
)
overrideprotected

Definition at line 85 of file globalshortcutregistry.cpp.

86{
87 Q_ASSERT(m_filteredWindow);
88 Q_ASSERT(obj == static_cast<QObject*>(m_filteredWindow.data()));
89
90 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
91
92 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
93
94 // Make a copy of the event so we don't alter it for passing on.
95 QKeyEvent eCopy(keyEvent->type(),
96 keyEvent->key(),
97 keyEvent->modifiers(),
98 keyEvent->text(),
99 keyEvent->isAutoRepeat(),
100 keyEvent->count());
101
102 int seq = keyEvent->key() + keyEvent->modifiers();
103 bool acceptedAtLeastOnce = false;
104 if (m_shortcuts.contains(seq)) {
105 const auto shortcuts = m_shortcuts.value(seq);
106 Q_FOREACH(const auto &shortcut, shortcuts) {
107 if (shortcut) {
108 auto window = windowForShortcut(shortcut);
109 if (!window || window == obj) { // accept shortcut if it's not attached to a window or it's window is active.
110 qApp->sendEvent(shortcut, &eCopy);
111 acceptedAtLeastOnce = acceptedAtLeastOnce || eCopy.isAccepted();
112 }
113 }
114 }
115 }
116
117 return acceptedAtLeastOnce;
118 }
119
120 return QObject::eventFilter(obj, event);
121}

◆ hasShortcut()

bool GlobalShortcutRegistry::hasShortcut ( const QVariant &  seq) const
Returns
whether shortcut seq is currently registered

Definition at line 50 of file globalshortcutregistry.cpp.

51{
52 return m_shortcuts.contains(seq);
53}

◆ setupFilterOnWindow()

void GlobalShortcutRegistry::setupFilterOnWindow ( QWindow *  window)

Sets up key events filtering on window window

Definition at line 123 of file globalshortcutregistry.cpp.

124{
125 if (m_filteredWindow) {
126 m_filteredWindow->removeEventFilter(this);
127 m_filteredWindow.clear();
128 }
129
130 if (window) {
131 m_filteredWindow = window;
132 window->installEventFilter(this);
133 }
134}

◆ shortcuts()

GlobalShortcutList GlobalShortcutRegistry::shortcuts ( ) const
Returns
the list of shortcuts currently registered

Definition at line 45 of file globalshortcutregistry.cpp.

46{
47 return m_shortcuts;
48}

The documentation for this class was generated from the following files: