Krita Source Code Documentation
Loading...
Searching...
No Matches
KisCustomModifiersCatcher Class Reference

The KisCustomModifiersCatcher class is a special utility class that tracks custom modifiers pressed. Its main purpose is to avoid manual tracking of KeyPress/KeyRelease/FocusIn events in the class and reuse the common code in multiple widgets. More...

#include <kis_custom_modifiers_catcher.h>

+ Inheritance diagram for KisCustomModifiersCatcher:

Classes

struct  Private
 

Public Member Functions

void addModifier (const QString &id, Qt::Key modifier)
 addModifier registers a custom modifier
 
bool eventFilter (QObject *object, QEvent *event) override
 
 KisCustomModifiersCatcher (QObject *parent)
 
bool modifierPressed (const QString &id)
 modifierPressed returns the state of the tracked modifier
 
 ~KisCustomModifiersCatcher () override
 

Private Attributes

const QScopedPointer< Privatem_d
 

Detailed Description

The KisCustomModifiersCatcher class is a special utility class that tracks custom modifiers pressed. Its main purpose is to avoid manual tracking of KeyPress/KeyRelease/FocusIn events in the class and reuse the common code in multiple widgets.

// in the c-tor of your widget create the catcher, it will automatically
// connect to the passed parent
// Register a tracked modifier
catcher->addModifier("pan-zoom", Qt::Key_Space);
// in the pointer tracking event handlers just check
// if the modifier is pressed or not
bool isPressed = catcher->modifierPressed("pan-zoom");
The KisCustomModifiersCatcher class is a special utility class that tracks custom modifiers pressed....
bool modifierPressed(const QString &id)
modifierPressed returns the state of the tracked modifier
void addModifier(const QString &id, Qt::Key modifier)
addModifier registers a custom modifier

Definition at line 33 of file kis_custom_modifiers_catcher.h.

Constructor & Destructor Documentation

◆ KisCustomModifiersCatcher()

KisCustomModifiersCatcher::KisCustomModifiersCatcher ( QObject * parent)

Create the catcher and connect to the passed widget/object to track its key events

Definition at line 34 of file kis_custom_modifiers_catcher.cpp.

35 : QObject(parent),
36 m_d(new Private(parent))
37{
38 if (m_d->trackedObject) {
39 parent->installEventFilter(this);
40 }
41}
const QScopedPointer< Private > m_d
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References m_d.

◆ ~KisCustomModifiersCatcher()

KisCustomModifiersCatcher::~KisCustomModifiersCatcher ( )
override

Definition at line 43 of file kis_custom_modifiers_catcher.cpp.

44{
45}

Member Function Documentation

◆ addModifier()

void KisCustomModifiersCatcher::addModifier ( const QString & id,
Qt::Key modifier )

addModifier registers a custom modifier

Parameters
ida unique id string associated with the modifier. Later, you will use this string to fetch the modifier state.
modifierthe key to track

Definition at line 47 of file kis_custom_modifiers_catcher.cpp.

48{
49 m_d->idToKeyMap.insert(id, modifier);
50 m_d->trackedKeys.insert(modifier);
51
52 m_d->reset();
53}

References m_d.

◆ eventFilter()

bool KisCustomModifiersCatcher::eventFilter ( QObject * object,
QEvent * event )
override

Definition at line 64 of file kis_custom_modifiers_catcher.cpp.

65{
66 if (object != m_d->trackedObject) return false;
67
68 switch (event->type()) {
69 case QEvent::ShortcutOverride: {
70 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
71
72 if (keyEvent->isAutoRepeat()) break;
73
75
76 if (m_d->trackedKeys.contains(key)) {
77 if (m_d->pressedKeys.contains(key)) {
78 m_d->reset();
79 } else {
80 m_d->pressedKeys.insert(key);
81 }
82 }
83 break;
84 }
85 case QEvent::KeyRelease: {
86 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
87
88 if (keyEvent->isAutoRepeat()) break;
89
91
92 if (m_d->trackedKeys.contains(key)) {
93 if (!m_d->pressedKeys.contains(key)) {
94 m_d->reset();
95 } else {
96 m_d->pressedKeys.remove(key);
97 }
98 }
99
100 break;
101 }
102 case QEvent::FocusIn: {
103 m_d->reset();
104
105 { // Emulate pressing of the key that are already pressed
107
108 Qt::KeyboardModifiers modifiers = mapper.queryStandardModifiers();
109 Q_FOREACH (Qt::Key key, mapper.queryExtendedModifiers()) {
110 QKeyEvent kevent(QEvent::ShortcutOverride, key, modifiers);
111 eventFilter(object, &kevent);
112 }
113 }
114 }
115 default:
116 break;
117 }
118
119 return false;
120}
bool eventFilter(QObject *object, QEvent *event) override
static Qt::Key workaroundShiftAltMetaHell(const QKeyEvent *keyEvent)

References eventFilter(), m_d, KisExtendedModifiersMapper::queryExtendedModifiers(), KisExtendedModifiersMapper::queryStandardModifiers(), and KisExtendedModifiersMapper::workaroundShiftAltMetaHell().

◆ modifierPressed()

bool KisCustomModifiersCatcher::modifierPressed ( const QString & id)

modifierPressed returns the state of the tracked modifier

Definition at line 55 of file kis_custom_modifiers_catcher.cpp.

56{
57 if (!m_d->idToKeyMap.contains(id)) {
58 qWarning() << "KisCustomModifiersCatcher::modifierPressed(): unexpected modifier id:" << id;
59 return false;
60 }
61 return m_d->pressedKeys.contains(m_d->idToKeyMap[id]);
62}

References m_d.

Member Data Documentation

◆ m_d

const QScopedPointer<Private> KisCustomModifiersCatcher::m_d
private

Definition at line 59 of file kis_custom_modifiers_catcher.h.


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