Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_shortcut_matcher.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2012 Dmitry Kazakov <dimula73@gmail.com>
3 * SPDX-FileCopyrightText: 2026 Ayanami Kaine <personal@ayanamikaine.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
9
10#include <QEvent>
11#include <QMouseEvent>
12#include <QTabletEvent>
13
14#include "kis_assert.h"
16#include "kis_stroke_shortcut.h"
17#include "kis_touch_shortcut.h"
20#include "kis_config.h"
23#include <KoPointerEvent.h>
24
25#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
26// for QMutableEventPoint
27#include <QWindow>
28#include <QtGui/private/qeventpoint_p.h>
29#endif
30
31inline QString debugShortcutName(const KisAbstractShortcut *shortcut)
32{
33 auto action = shortcut->action();
34 return QString("%1/%2").arg(action->name()).arg(action->shortcutIndexes().key(shortcut->shortcutIndex(), "<unknown>"));
35}
36
37#define DEBUG_ACTION(text) dbgInputMatcher << __FUNCTION__ << "-" << text;
38#define DEBUG_SHORTCUT(text, shortcut) dbgInputMatcher << __FUNCTION__ << "-" << text << "act:" << debugShortcutName(shortcut);
39#define DEBUG_KEY(text) dbgInputMatcher << __FUNCTION__ << "-" << text << "keys:" << m_d->keys;
40#define DEBUG_BUTTON_ACTION(text, button) dbgInputMatcher << __FUNCTION__ << "-" << text << "button:" << button << "btns:" << m_d->buttons << "keys:" << m_d->keys;
41#define DEBUG_EVENT_ACTION(text, event) if (event) {dbgInputMatcher << __FUNCTION__ << "-" << text << "type:" << event->type();}
42#define DEBUG_TOUCH_ACTION(text, event) \
43 if (event) { \
44 dbgInputMatcher << __FUNCTION__ << "-" << text << "type:" << event->type() \
45 << "tps:" << KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::allTouchStates()) \
46 << "pressedTps:" << KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::pressedOnlyTouchStates()) \
47 << "maxTps:" << m_d->maxTouchPoints << "drag:" << m_d->isTouchDragDetected; \
48 }
49
50
51namespace
52{
53QTouchEvent generateFakeTouchEndEvent(const QTouchEvent *event)
54{
55#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
56 auto points = event->touchPoints();
57#else
58 auto points = event->points();
59#endif
60
61 for (auto it = points.begin(); it != points.end();) {
62#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
63 if (it->state() == Qt::TouchPointReleased) {
64 it = points.erase(it);
65 } else {
66 it->setState(Qt::TouchPointReleased);
67 ++it;
68 }
69#else
70 if (it->state() == QEventPoint::Released) {
71 it = points.erase(it);
72 } else {
73 QMutableEventPoint::setState(*it, QEventPoint::Released);
74 ++it;
75 }
76#endif
77 }
78
79#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 return QTouchEvent(QEvent::TouchEnd, event->device(), event->modifiers(), Qt::TouchPointReleased, points);
81#else
82 return QTouchEvent(QEvent::TouchEnd, event->pointingDevice(), event->modifiers(), points);
83#endif
84
85}
86
87} // namespace
88
89class Q_DECL_HIDDEN KisShortcutMatcher::Private
90{
91public:
93 : runningShortcut(0)
94 , readyShortcut(0)
95 , touchShortcut(0)
96 , nativeGestureShortcut(0)
97 , touchpadScrollShortcut(0)
98 , actionGroupMask([] () { return AllActionGroup; })
99 , suppressAllActions(false)
100 , suppressAllKeyboardActions(false)
101 , cursorEntered(false)
102 {}
103
105 {
106 qDeleteAll(singleActionShortcuts);
107 qDeleteAll(strokeShortcuts);
108 qDeleteAll(touchShortcuts);
109 }
110
112 QSet<KisSingleActionShortcut*> suppressedSingleActionShortcuts;
117
118 QSet<Qt::Key> keys; // Model of currently pressed keys
119 QSet<Qt::MouseButton> buttons; // Model of currently pressed buttons
120
121 QSet<Qt::Key> polledKeys; // Keys that were polled using native platform APIs and thus need to be treated carefully, as they may not generate QT key events.
122
126
130 std::optional<KisTouchHoldEventsPostponer> touchHoldEventPostponer;
131
132
133 int maxTouchPoints{0};
134 int matchingIteration{0};
135 bool isTouchDragDetected {false};
136 bool isTouchHeld {false};
137 QScopedPointer<QEvent> bestCandidateForTapTouchEvent;
138 QScopedPointer<QTouchEvent> lastProcessedTouchEvent;
139 bool touchActionTracked {false};
140 int touchHoldDelayMs {400};
141
142
143 // A workaround for some Android systems, which force-cancel all the
144 // actions with 3+ fingers. This workaround just converts the "cancel"
145 // into "end" event, letting Krita handle these events as "tap" events.
147 // NOTE: we cannot reuse maxTouchPoints for this workaround, because
148 // maxTouchPoints is used at the lower stages of the touch processing
149 // pipeline, **after** the touch events left the hold-postponer. And
150 // we need this workaround to cancel the postponer itself.
151 int lastRawTouchPointsCount = 0;
152 };
153 std::optional<IgnoreMultiFingerCancelWorkaround> ignoreMultiFingerCancelWorkaround;
154
155 std::function<KisInputActionGroupsMask()> actionGroupMask;
159
160 int recursiveCounter = 0;
161 int brokenByRecursion = 0;
162
163
166 : q(_q)
167 {
168 q->m_d->recursiveCounter++;
169 q->m_d->brokenByRecursion++;
170 }
171
173 q->m_d->recursiveCounter--;
174 }
175
176 bool isInRecursion() const {
177 return q->m_d->recursiveCounter > 1;
178 }
179
181 };
182
185 : q(_q)
186 {
187 q->m_d->brokenByRecursion = 0;
188 }
189
192
193 bool brokenByRecursion() const {
194 return q->m_d->brokenByRecursion > 0;
195 }
196
198 };
199
200 inline bool actionsSuppressed() const {
201#ifndef Q_OS_ANDROID
202 return suppressAllActions || !cursorEntered;
203#else
204 // when S-pen is not pointing the canvas, actions on canvas are disabled, till it points back to canvas.
205 return suppressAllActions;
206#endif
207 }
208
209 inline bool actionsSuppressedIgnoreFocus() const {
210 return suppressAllActions;
211 }
212
213 inline bool KeyboardActionsSuppressed() const {
214 return suppressAllKeyboardActions;
215 }
216};
217
221
226
228{
229 return m_d->runningShortcut || m_d->touchShortcut || m_d->nativeGestureShortcut || m_d->touchpadScrollShortcut;
230}
231
233{
234 for (const KisTouchShortcut *shortcut : m_d->touchShortcuts) {
235 if (shortcut->isHoldType() && shortcut->isAvailable(m_d->actionGroupMask())) {
236 return true;
237 }
238 }
239 return false;
240}
241
243{
244 m_d->singleActionShortcuts.append(shortcut);
245}
246
248{
249 m_d->strokeShortcuts.append(shortcut);
250}
251
253{
254 m_d->touchShortcuts.append(shortcut);
255}
256
258{
259 m_d->nativeGestureShortcuts.append(shortcut);
260}
261
263{
264 m_d->touchpadScrollShortcuts.append(shortcut);
265}
266
268{
269 return (m_d->runningShortcut && m_d->runningShortcut->action()
270 && m_d->runningShortcut->action()->supportsHiResInputEvents(m_d->runningShortcut->shortcutIndex()))
271 || (m_d->touchShortcut && m_d->touchShortcut->action()
272 && m_d->touchShortcut->action()->supportsHiResInputEvents(m_d->touchShortcut->shortcutIndex()))
273 || (m_d->nativeGestureShortcut && m_d->nativeGestureShortcut->action()
274 && m_d->nativeGestureShortcut->action()->supportsHiResInputEvents(m_d->nativeGestureShortcut->shortcutIndex()))
275 || (m_d->touchpadScrollShortcut && m_d->touchpadScrollShortcut->action()
276 && m_d->touchpadScrollShortcut->action()->supportsHiResInputEvents(m_d->touchpadScrollShortcut->shortcutIndex()));
277}
278
280{
281 Private::RecursionNotifier notifier(this);
282
283 bool retval = false;
284
285 if (m_d->keys.contains(key)) { DEBUG_ACTION("Peculiar, records show key was already pressed"); }
286
287 if (!hasRunningShortcut() && !notifier.isInRecursion()) {
288 retval = tryRunSingleActionShortcutImpl(key, (QEvent*)0, m_d->keys);
289 }
290
291 m_d->keys.insert(key);
292 DEBUG_KEY("Pressed");
293
294 if (notifier.isInRecursion()) {
296 } else if (!hasRunningShortcut()) {
299 }
300
301 return retval;
302}
303
305{
306 Private::RecursionNotifier notifier(this);
307
308
309 bool retval = false;
310
311 if (!m_d->keys.contains(key)) { DEBUG_ACTION("Peculiar, autorepeated key but can't remember it was pressed"); }
312
313 if (m_d->polledKeys.contains(key)) {
314 m_d->polledKeys.remove(key);
315 }
316
317 if (notifier.isInRecursion()) {
319 } else if (!hasRunningShortcut()) {
320 // Autorepeated key should not be included in the shortcut
321 QSet<Qt::Key> filteredKeys = m_d->keys;
322 filteredKeys.remove(key);
323 retval = tryRunSingleActionShortcutImpl(key, (QEvent*)0, filteredKeys);
324 }
325
326 return retval;
327}
328
330{
331 Private::RecursionNotifier notifier(this);
332
333 if (!m_d->keys.contains(key)) { DEBUG_ACTION("Peculiar, key released but can't remember it was pressed"); }
334 else m_d->keys.remove(key);
335
336 m_d->polledKeys.remove(key);
337
338 DEBUG_KEY("Released");
339
340 if (notifier.isInRecursion()) {
342 } else if (!hasRunningShortcut()) {
345 }
346
347 return false;
348}
349
350bool KisShortcutMatcher::buttonPressed(Qt::MouseButton button, QEvent *event)
351{
352 Private::RecursionNotifier notifier(this);
353 DEBUG_BUTTON_ACTION("entered", button);
354
355 // the tablet actions have the priority over any existing
356 // touch action, so we should cancel them first
358
359 bool retval = false;
360
361 if (m_d->buttons.contains(button)) { DEBUG_ACTION("Peculiar, button was already pressed."); }
362
363 if (!hasRunningShortcut() && !notifier.isInRecursion()) {
365 retval = tryRunReadyShortcut(button, event);
366 }
367
368 m_d->buttons.insert(button);
369
370 if (notifier.isInRecursion()) {
372 } else if (!hasRunningShortcut()) {
375 }
376
377 return retval;
378}
379
380bool KisShortcutMatcher::buttonReleased(Qt::MouseButton button, QEvent *event)
381{
382 Private::RecursionNotifier notifier(this);
383 DEBUG_BUTTON_ACTION("entered", button);
384
385 bool retval = false;
386
387 // here we check for the presence of the **stroke** shortcut only
388 if (m_d->runningShortcut) {
389 KIS_SAFE_ASSERT_RECOVER_NOOP(!notifier.isInRecursion());
390
391 retval = tryEndRunningShortcut(button, event);
392 DEBUG_BUTTON_ACTION("ended", button);
393 }
394
395 if (!m_d->buttons.contains(button)) reset("Peculiar, button released but we can't remember it was pressed");
396 else m_d->buttons.remove(button);
397
398 if (notifier.isInRecursion()) {
400 } else if (!hasRunningShortcut()) {
403 }
404
405 return retval;
406}
407
409{
410 Private::RecursionNotifier notifier(this);
411
412
413 if (hasRunningShortcut() || notifier.isInRecursion()) {
414 DEBUG_ACTION("Wheel event canceled.");
415 return false;
416 }
417
418 return tryRunWheelShortcut(wheelAction, event);
419}
420
422{
423 Private::RecursionNotifier notifier(this);
424
425 if (notifier.isInRecursion()) {
426 return false;
427 }
428
429 bool retval = false;
430
431 if (m_d->runningShortcut) {
432 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!m_d->touchShortcut && !m_d->nativeGestureShortcut, false);
433 m_d->runningShortcut->action()->inputEvent(event);
434 retval = true;
435 }
436
437 return retval;
438}
439
441{
442 Private::RecursionNotifier notifier(this);
443
444 m_d->cursorEntered = true;
445
446 if (notifier.isInRecursion()) {
448 } else if (!hasRunningShortcut()) {
451 }
452}
453
455{
456 Private::RecursionNotifier notifier(this);
457
458 m_d->cursorEntered = false;
459
460 if (notifier.isInRecursion()) {
462 } else if (!hasRunningShortcut()) {
465 }
466}
467
468bool KisShortcutMatcher::touchBeginEvent( QTouchEvent* event )
469{
470 DEBUG_TOUCH_ACTION("entered", event)
471
472 Private::RecursionNotifier notifier(this);
473
474 if (hasRunningShortcut()) {
475 DEBUG_ACTION("touch action rejected by a running shortcut");
476
477 // touch has been triggered while some tablet action is in progress,
478 // just consume and ignore it
479 return !notifier.isInRecursion();
480 }
481
482#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
483 QScopedPointer<QEvent> tmp;
484 KoPointerEvent::copyQtPointerEvent(event, tmp);
485 m_d->lastProcessedTouchEvent.reset(static_cast<QTouchEvent*>(tmp.take()));
486#else
487 m_d->lastProcessedTouchEvent.reset(event->clone());
488#endif
489
490 // reset state
492 if (m_d->ignoreMultiFingerCancelWorkaround) {
493 m_d->ignoreMultiFingerCancelWorkaround->lastRawTouchPointsCount = m_d->maxTouchPoints;
494 }
495 m_d->matchingIteration = 1;
496 m_d->isTouchDragDetected = false;
497 m_d->isTouchHeld = false;
498 m_d->touchActionTracked = true;
499#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
500 KoPointerEvent::copyQtPointerEvent(event, m_d->bestCandidateForTapTouchEvent);
501#else
502 m_d->bestCandidateForTapTouchEvent.reset(event->clone());
503#endif
504
505 // TODO: also check if the first point actually qualifies
506 if (hasTouchHoldShortcut()) {
507 m_d->touchHoldEventPostponer.emplace(TOUCH_SLOP, m_d->touchHoldDelayMs);
508 QObject::connect(&m_d->touchHoldEventPostponer.value(),
510 &m_d->touchHoldEventPostponer.value(),
511 [this]() {
512 slotTouchHoldCompleted();
513 });
514 m_d->touchHoldEventPostponer->pushThrough(event);
515 if (m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
516 m_d->touchHoldEventPostponer.reset();
517 } else {
518 DEBUG_ACTION("thouch-hold timer is set up");
519 }
520 }
521
522 return !notifier.isInRecursion();
523}
524
526{
527 // the touch action has been overridden by some tablet action,
528 // consume and ignore it.
529 if (!m_d->touchActionTracked) return false;
530
531 if (m_d->ignoreMultiFingerCancelWorkaround) {
532 m_d->ignoreMultiFingerCancelWorkaround->lastRawTouchPointsCount =
534 }
535
536 if (m_d->touchHoldEventPostponer) {
537 m_d->touchHoldEventPostponer->pushThrough(event);
538 if (m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {
539 // process all the postponed **update** events,
540 // the very first begin event is only used in
541 // the hold-completion operation
542 Q_FOREACH (QTouchEvent *event, m_d->touchHoldEventPostponer->postponedEvents()) {
543 if (event->type() == QEvent::TouchUpdate) {
544 (void) touchUpdateEventImpl(event);
545 }
546 }
547 m_d->touchHoldEventPostponer.reset();
548 } else {
549 // noop, we are waiting for hold
550 }
551 } else {
552 // TODO: don't enter for hold-drag events
553 return touchUpdateEventImpl(event);
554 }
555
556 return true;
557}
558
560{
561 KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->touchHoldEventPostponer);
563
564 // Can happen when multiple shortcut sources interact
565 KIS_SAFE_ASSERT_RECOVER_RETURN(!m_d->runningShortcut);
566
567 // take the very first begin event to start the action
568 QTouchEvent *event = m_d->touchHoldEventPostponer->postponedEvents().front();
569 KIS_SAFE_ASSERT_RECOVER_RETURN(event->type() == QEvent::TouchBegin);
570
571 m_d->isTouchHeld = true; // Must be set first, used in tryRunTouchShortcut.
572
573 DEBUG_TOUCH_ACTION("entered", event);
574
576 // noop, all is fine!
577 } else {
578 KIS_SAFE_ASSERT_RECOVER(0 && "should not happen")
579 {
580 // Shouldn't really happen, since KisInputManager checks whether a touch
581 // hold shortcut exists beforehand. We'll just handle this though.
582 m_d->isTouchHeld = false;
583 }
584 }
585
586 // remove the postponer to continue processing events in-place
587 m_d->touchHoldEventPostponer.reset();
588}
589
591{
592 if (_41020().isDebugEnabled()) {
593 const int oldNumPressedPoints = KisTouchShortcut::countTouchPoints(m_d->lastProcessedTouchEvent.data(), KisTouchShortcut::pressedOnlyTouchStates());
595 if (oldNumPressedPoints != newNumPressedPoints) {
596 DEBUG_TOUCH_ACTION("touch point state changed", event)
597 }
598 }
599
600#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
601 QScopedPointer<QEvent> tmp;
602 KoPointerEvent::copyQtPointerEvent(event, tmp);
603 m_d->lastProcessedTouchEvent.reset(static_cast<QTouchEvent*>(tmp.take()));
604#else
605 m_d->lastProcessedTouchEvent.reset(event->clone());
606#endif
607
608 if (m_d->isTouchHeld) {
609 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_d->touchShortcut, false);
610 m_d->touchShortcut->action()->inputEvent(event);
611 return true;
612 }
613
614 bool retval = false;
615
616 // Check whether the touchpoints are relatively stationary or have
617 // been moved for dragging. If the drag is detected, until the next
618 // TouchBegin event, we'll be assuming the gesture to be of
619 // dragging type.
620 if (!m_d->isTouchDragDetected) {
622 m_d->isTouchDragDetected = true;
623 }
624 }
625
626 // Different drag shortcuts can have different drag thresholds. E.g.
627 // touch-painting shortcut will have much lower threshold value. The
628 // shortcuts will calculate the distance themselves in
629 // KisTouchShortcut::matchDragType, so we should just ask them to do that.
630 const int numStillDraggedPoints =
632 if (!hasRunningShortcut() && numStillDraggedPoints >= m_d->maxTouchPoints) {
633 m_d->maxTouchPoints = numStillDraggedPoints;
635 m_d->isTouchDragDetected = true;
636 return true;
637 }
638 }
639
640 // for a first few events we don't process the events right away. But analyze and keep track of the event with most
641 // touchpoints. This is done to prevent conditions where in three-finger-tap, two-finger-tap be preceded due to
642 // latency
643 const int numIterations = 10;
644 if (m_d->matchingIteration <= numIterations && !m_d->isTouchDragDetected) {
645 m_d->matchingIteration++;
647 return matchTouchShortcut((QTouchEvent *)m_d->bestCandidateForTapTouchEvent.data(),
650 }
651
652 if (m_d->isTouchDragDetected) {
653 if (m_d->touchShortcut
654 // TODO: use a different check without threshold! split into match() and matchBegin()
655 && !m_d->touchShortcut->matchDragType(event, KisTouchShortcut::pressedOnlyTouchStates())) {
656 // we should end the event as an event with more touchpoints was received
657 retval = tryEndTouchShortcut(event);
658 } else if (m_d->touchShortcut) {
659 m_d->touchShortcut->action()->inputEvent(event);
660 retval = true;
661 }
662 } else { // !m_d->isTouchDragDetected
663 // triggered if a new finger was added, which might result in shortcut not matching the action
664#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
665 const auto FlagReleased = Qt::TouchPointReleased;
666#else
667 const auto FlagReleased = QEventPoint::Released;
668#endif
669 if (event->touchPointStates().testFlag(FlagReleased) && !hasRunningShortcut()) {
670 const int previousNumPoints = KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::allTouchStates());
671
672 // we should end the event as an event with more touchpoints was received
673 if (previousNumPoints >= m_d->maxTouchPoints) {
674 m_d->maxTouchPoints = previousNumPoints;
676 m_d->bestCandidateForTapTouchEvent.reset();
677 retval = true;
678 }
679 }
680 }
681 }
682 // TODO: update maxTouchPoints
683
684 return retval;
685}
686
687bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)
688{
689 Private::RecursionNotifier notifier(this);
690
691 DEBUG_TOUCH_ACTION("enter", event);
692
693 // the touch action has been overridden by some tablet action,
694 // consume and ignore it.
695 if (!m_d->touchActionTracked) {
696 DEBUG_ACTION("touch was not tracked");
697 KIS_SAFE_ASSERT_RECOVER(!m_d->touchHoldEventPostponer.has_value()) {
698 m_d->touchHoldEventPostponer->cancelHoldWait();
699 m_d->touchHoldEventPostponer.reset();
700 }
701 return true;
702 }
703
704 // flush all the touch-hold postiponed events if they were present
705 if (m_d->touchHoldEventPostponer) {
706 DEBUG_ACTION("flushing all postponed hold events");
707
709 m_d->touchHoldEventPostponer->cancelHoldWait();
710 // process all the postponed **update** events,
711 // the very first begin event is only used in
712 // the hold-completion operation
713 Q_FOREACH (QTouchEvent *event, m_d->touchHoldEventPostponer->postponedEvents()) {
714 if (event->type() == QEvent::TouchUpdate) {
715 (void) touchUpdateEventImpl(event);
716 }
717 }
718 m_d->touchHoldEventPostponer.reset();
719 }
720
721 m_d->touchActionTracked = false;
722
723 bool retval = false;
724
725 if (!m_d->isTouchDragDetected && m_d->bestCandidateForTapTouchEvent && !hasRunningShortcut()) {
726 retval = tryFireTapTouchShortcut(static_cast<QTouchEvent *>(m_d->bestCandidateForTapTouchEvent.data()),
728 }
729
730 // we should try and end the shortcut too (it might be that there is none? (sketch))
731 retval |= tryEndTouchShortcut(event);
732
733 if (notifier.isInRecursion()) {
735 } else if (!hasRunningShortcut()) {
738 }
739
740 return retval;
741}
742
744{
745 // discard all the postponed touch-hold events
746 if (m_d->touchHoldEventPostponer) {
748 m_d->touchHoldEventPostponer->cancelHoldWait();
749 m_d->touchHoldEventPostponer.reset();
750 }
751
752 // end the touch action if present
753 if (m_d->touchShortcut) {
754 DEBUG_ACTION("force-cancel touch action")
755
756 KisTouchShortcut *touchShortcut = m_d->touchShortcut;
757 m_d->touchShortcut = 0;
758
759 KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->lastProcessedTouchEvent);
760
761 // on some platforms touch-cancel event may have **no**
762 // touch points, which would greatly confuse our KoPointerEvent
763 // class, so we should just generate a normal touch-end from the
764 // last known event
765 QTouchEvent touchEvent = generateFakeTouchEndEvent(m_d->lastProcessedTouchEvent.data());
766 touchShortcut->action()->end(&touchEvent);
768 }
769
770 m_d->touchActionTracked = false;
771}
772
774{
775 Q_UNUSED(event)
776
777 // The design requirement of touch-cancel handling is the following:
778 //
779 // 1) If drag-action has already been started, it is finished normally
780 // 2) If tap-action is pending, it is cancelled
781 // 3) If hold-action is pending, it is cancelled
782 // 4) If hold-action has already been started, it is ended normally
783
784 // On some Android devices, such as Xiaomi Pads, the system always eats
785 // multitouch inputs with more than two fingers, even if the user
786 // disables all gestures related to them in their system settings or
787 // uses the game boost mode that is supposed to disable gestures. So we
788 // handle those inputs even when they are cancelled, if the user wants
789 // to use it for a system gesture, they can disable the Krita shortcut.
790 if (m_d->ignoreMultiFingerCancelWorkaround) {
791 // NOTE: we cannot use the number of touch points from the actual
792 // cancel event, since Qt sets touch points as empty for the cancel
793 // event
794 if (m_d->ignoreMultiFingerCancelWorkaround->lastRawTouchPointsCount >= 3) {
795 DEBUG_ACTION("IgnoreMultiFingerCancelWorkaround: converting a cancelled action into a normally finished action")
796 touchEndEvent(event);
797 return;
798 }
799 }
800
801 Private::RecursionNotifier notifier(this);
802
803 DEBUG_TOUCH_ACTION("enter", event);
804
805 // the touch action has been overridden by some tablet action,
806 // consume and ignore it.
807 if (!m_d->touchActionTracked) {
808 KIS_SAFE_ASSERT_RECOVER(!m_d->touchHoldEventPostponer.has_value()) {
809 m_d->touchHoldEventPostponer->cancelHoldWait();
810 m_d->touchHoldEventPostponer.reset();
811 }
812 return;
813 }
814
815 KIS_SAFE_ASSERT_RECOVER_NOOP(!m_d->runningShortcut || !m_d->touchShortcut);
816
818
819 if (notifier.isInRecursion()) {
821 } else if (!hasRunningShortcut()) {
824 }
825}
826
827bool KisShortcutMatcher::nativeGestureBeginEvent(QNativeGestureEvent *event)
828{
829 Q_UNUSED(event);
830
831 Private::RecursionNotifier notifier(this);
832
833 DEBUG_EVENT_ACTION("entered", event);
834
835 return !notifier.isInRecursion();
836}
837
838bool KisShortcutMatcher::nativeGestureEvent(QNativeGestureEvent *event)
839{
840 bool retval = false;
841
842 DEBUG_EVENT_ACTION("entered", event);
843
844 if (!hasRunningShortcut()) {
845 retval = tryRunNativeGestureShortcut( event );
846 }
847 else if (m_d->nativeGestureShortcut) {
848 m_d->nativeGestureShortcut->action()->inputEvent( event );
849 retval = true;
850 }
851
852 return retval;
853}
854
855bool KisShortcutMatcher::nativeGestureEndEvent(QNativeGestureEvent *event)
856{
857 Private::RecursionNotifier notifier(this);
858
859 DEBUG_EVENT_ACTION("entered", event);
860
861 if (m_d->nativeGestureShortcut) {
863 }
864
865 if (notifier.isInRecursion()) {
867 } else if (!hasRunningShortcut()) {
870 }
871
872 return true;
873}
874
876{
877 return !m_d->touchpadScrollShortcuts.isEmpty();
878}
879
881{
882 Private::RecursionNotifier notifier(this);
883
884 if (notifier.isInRecursion()) return false;
885
886 DEBUG_EVENT_ACTION("entered", event);
887
888 if (!hasRunningShortcut()) {
890 }
891
892 if (m_d->touchpadScrollShortcut) {
893 m_d->touchpadScrollShortcut->action()->inputEvent(event);
894 }
895
896 return bool(m_d->touchpadScrollShortcut);
897}
898
900{
901 Private::RecursionNotifier notifier(this);
902
903 if (notifier.isInRecursion()) return false;
904
905 DEBUG_EVENT_ACTION("entered", event);
906
907 if (m_d->touchpadScrollShortcut) {
908 m_d->touchpadScrollShortcut->action()->inputEvent(event);
909 }
910
911 return bool(m_d->touchpadScrollShortcut);
912}
913
915{
916 Private::RecursionNotifier notifier(this);
917
918 if (notifier.isInRecursion()) return false;
919
920 DEBUG_EVENT_ACTION("entered", event);
921
922 bool retval = false;
923
924 if (m_d->touchpadScrollShortcut) {
925 retval = tryEndTouchpadScrollShortcut(event);
926 }
927
928 if (notifier.isInRecursion()) {
930 } else if (!hasRunningShortcut()) {
933 }
934
935 return retval;
936}
937
938Qt::MouseButtons listToFlags(const QList<Qt::MouseButton> &list) {
939 Qt::MouseButtons flags;
940 Q_FOREACH (Qt::MouseButton b, list) {
941 flags |= b;
942 }
943 return flags;
944}
945
947{
948 Private::RecursionNotifier notifier(this);
949
950
951 reset("reinitialize");
952
953 if (notifier.isInRecursion()) {
955 } else if (!hasRunningShortcut()) {
958 }
959}
960
962{
963 Private::RecursionNotifier notifier(this);
964
965 m_d->buttons.clear();
966 DEBUG_ACTION("reinitializing buttons");
967
968 if (notifier.isInRecursion()) {
970 } else if (!hasRunningShortcut()) {
973 }
974}
975
977{
978 Q_FOREACH (Qt::Key key, m_d->keys) {
979 if (!keys.contains(key)) {
980 keyReleased(key);
981 }
982 }
983
984 Q_FOREACH (Qt::Key key, keys) {
985 if (!m_d->keys.contains(key)) {
986 keyPressed(key);
987 m_d->polledKeys << key;
988 }
989 }
990
991 Private::RecursionNotifier notifier(this);
992
993 if (notifier.isInRecursion()) {
995 } else if (!hasRunningShortcut()) {
998 }
999
1000 DEBUG_ACTION("recoverySyncModifiers");
1001}
1002
1003bool KisShortcutMatcher::sanityCheckModifiersCorrectness(Qt::KeyboardModifiers modifiers) const
1004{
1005 auto checkKey = [this, modifiers] (Qt::Key key, Qt::KeyboardModifier modifier) {
1006 return m_d->keys.contains(key) == bool(modifiers & modifier);
1007 };
1008
1009 return checkKey(Qt::Key_Shift, Qt::ShiftModifier) &&
1010 checkKey(Qt::Key_Control, Qt::ControlModifier) &&
1011 checkKey(Qt::Key_Alt, Qt::AltModifier) &&
1012 checkKey(Qt::Key_Meta, Qt::MetaModifier);
1013
1014}
1015
1017{
1019 std::copy(m_d->keys.begin(), m_d->keys.end(), std::back_inserter(keys));
1020 return keys;
1021}
1022
1024{
1025 return !m_d->polledKeys.empty();
1026}
1027
1028void KisShortcutMatcher::lostFocusEvent(const QPointF &localPos)
1029{
1030 Private::RecursionNotifier notifier(this);
1031
1032 DEBUG_ACTION("lostFocusEvent");
1033
1034 if (m_d->runningShortcut) {
1035 forceEndRunningShortcut(localPos);
1036 }
1037
1039
1047}
1048
1050{
1051 Private::RecursionNotifier notifier(this);
1052
1053 DEBUG_ACTION("toolHasBeenActivated");
1054
1055 if (notifier.isInRecursion()) {
1057 } else if (!hasRunningShortcut()) {
1060 }
1061}
1062
1064{
1065 m_d->keys.clear();
1066 m_d->buttons.clear();
1067 DEBUG_ACTION("reset!");
1068}
1069
1070
1072{
1073 m_d->keys.clear();
1074 m_d->buttons.clear();
1075 Q_UNUSED(msg);
1076 DEBUG_ACTION(msg);
1077}
1078
1080{
1081 m_d->suppressAllActions = value;
1082}
1083
1085{
1086 m_d->suppressedSingleActionShortcuts.clear();
1087
1088 Q_FOREACH (KisSingleActionShortcut *s, m_d->singleActionShortcuts) {
1089 Q_FOREACH (const QKeySequence &seq, shortcuts) {
1090 if (s->conflictsWith(seq)) {
1091 m_d->suppressedSingleActionShortcuts.insert(s);
1092 }
1093 }
1094 }
1095}
1096
1098{
1099 m_d->suppressAllKeyboardActions = value;
1100}
1101
1103{
1104 reset("Clearing shortcuts");
1105
1106 qDeleteAll(m_d->singleActionShortcuts);
1107 m_d->singleActionShortcuts.clear();
1108 m_d->suppressedSingleActionShortcuts.clear();
1109
1110 qDeleteAll(m_d->strokeShortcuts);
1111 m_d->strokeShortcuts.clear();
1112
1113 qDeleteAll(m_d->touchShortcuts);
1114 m_d->touchShortcuts.clear();
1115
1116 qDeleteAll(m_d->nativeGestureShortcuts);
1117 m_d->nativeGestureShortcuts.clear();
1118
1119 qDeleteAll(m_d->touchpadScrollShortcuts);
1120 m_d->touchpadScrollShortcuts.clear();
1121
1122 m_d->candidateShortcuts.clear();
1123 m_d->runningShortcut = 0;
1124 m_d->readyShortcut = 0;
1125 m_d->touchShortcut = 0;
1126 m_d->nativeGestureShortcut = 0;
1127 m_d->touchpadScrollShortcut = 0;
1128}
1129
1130void KisShortcutMatcher::setInputActionGroupsMaskCallback(std::function<KisInputActionGroupsMask ()> func)
1131{
1132 m_d->actionGroupMask = func;
1133}
1134
1136{
1137 return tryRunSingleActionShortcutImpl(wheelAction, event, m_d->keys, false);
1138}
1139
1140// Note: sometimes event can be zero!!
1141template<typename T, typename U>
1142bool KisShortcutMatcher::tryRunSingleActionShortcutImpl(T param, U *event, const QSet<Qt::Key> &keysState, bool keyboard)
1143{
1144 if (m_d->actionsSuppressedIgnoreFocus() || (keyboard && m_d->KeyboardActionsSuppressed())) {
1145 DEBUG_EVENT_ACTION("Event suppressed", event)
1146 return false;
1147 }
1148
1149 KisSingleActionShortcut *goodCandidate = 0;
1150
1151 Q_FOREACH (KisSingleActionShortcut *s, m_d->singleActionShortcuts) {
1152 if (!m_d->suppressedSingleActionShortcuts.contains(s) &&
1153 s->isAvailable(m_d->actionGroupMask()) &&
1154 s->match(keysState, param) &&
1155 (!goodCandidate || s->priority() > goodCandidate->priority())) {
1156
1157 goodCandidate = s;
1158 }
1159 }
1160
1161 if (goodCandidate) {
1162 DEBUG_EVENT_ACTION("Beginning action for event", event);
1163 goodCandidate->action()->begin(goodCandidate->shortcutIndex(), event);
1164 goodCandidate->action()->end(0);
1165 } else {
1166 DEBUG_EVENT_ACTION("Could not match a candidate for event", event)
1167 }
1168
1169 return goodCandidate;
1170}
1171
1173{
1174 m_d->candidateShortcuts.clear();
1175 if (m_d->actionsSuppressed()) return;
1176
1177 // Allow letting the modifiers to be matched so key_shift + middle mouse move can be matched, but key_v + mouse drag can not.
1178 bool containsOnlyModifiers = !m_d->keys.isEmpty();
1179 Q_FOREACH(const Qt::Key k, m_d->keys) {
1180 if (k != Qt::Key_Shift && k != Qt::Key_Control && k != Qt::Key_Alt && k != Qt::Key_Meta) {
1181 containsOnlyModifiers = false;
1182 break;
1183 }
1184 }
1185 if (m_d->KeyboardActionsSuppressed()
1186 && !containsOnlyModifiers && !m_d->keys.isEmpty()
1187 && m_d->buttons.isEmpty()) {
1188 return;
1189 }
1190
1191 Q_FOREACH (KisStrokeShortcut *s, m_d->strokeShortcuts) {
1192 if (s->matchReady(m_d->keys, m_d->buttons)) {
1193 m_d->candidateShortcuts.append(s);
1194 }
1195 }
1196}
1197
1198bool KisShortcutMatcher::tryRunReadyShortcut( Qt::MouseButton button, QEvent* event )
1199{
1200 KisStrokeShortcut *goodCandidate = 0;
1201
1202 Q_FOREACH (KisStrokeShortcut *s, m_d->candidateShortcuts) {
1203 if (s->isAvailable(m_d->actionGroupMask()) &&
1204 s->matchBegin(button) &&
1205 (!goodCandidate || s->priority() > goodCandidate->priority())) {
1206
1207 goodCandidate = s;
1208 }
1209 }
1210
1211 if (goodCandidate) {
1212 if (m_d->readyShortcut) {
1213 if (m_d->readyShortcut != goodCandidate) {
1214 if (goodCandidate->action() == m_d->readyShortcut->action() &&
1215 goodCandidate->action()->trySwitchShortcut(
1216 m_d->readyShortcut->shortcutIndex(), goodCandidate->shortcutIndex())) {
1217 m_d->readyShortcut = goodCandidate;
1218 } else {
1219 m_d->readyShortcut->action()->deactivate(m_d->readyShortcut->shortcutIndex());
1220 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1221 }
1222 }
1223 m_d->readyShortcut = 0;
1224 } else {
1225 DEBUG_EVENT_ACTION("Matched *new* shortcut for event", event);
1226 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1227 }
1228
1229 DEBUG_SHORTCUT("Starting new action", goodCandidate);
1230
1231 {
1232 m_d->runningShortcut = goodCandidate;
1233 Private::RecursionGuard guard(this);
1234 goodCandidate->action()->begin(goodCandidate->shortcutIndex(), event);
1235
1236 // the tool might have opened some dialog, which could break our event loop
1237 if (guard.brokenByRecursion()) {
1238 goodCandidate->action()->end(event);
1239 m_d->runningShortcut = 0;
1240
1242 }
1243 }
1244 }
1245
1246 return m_d->runningShortcut;
1247}
1248
1250{
1251 KisStrokeShortcut *goodCandidate = 0;
1252
1253 Q_FOREACH (KisStrokeShortcut *s, m_d->candidateShortcuts) {
1254 if (!goodCandidate || s->priority() > goodCandidate->priority()) {
1255 goodCandidate = s;
1256 }
1257 }
1258
1259 if (goodCandidate) {
1260 if (m_d->readyShortcut && m_d->readyShortcut != goodCandidate) {
1261 if (goodCandidate->action() == m_d->readyShortcut->action() &&
1262 goodCandidate->action()->trySwitchShortcut(
1263 m_d->readyShortcut->shortcutIndex(), goodCandidate->shortcutIndex())) {
1264 DEBUG_SHORTCUT("Switched previous shortcut action", m_d->readyShortcut);
1265 m_d->readyShortcut = goodCandidate;
1266 } else {
1267 DEBUG_SHORTCUT("Deactivated previous shortcut action", m_d->readyShortcut);
1268 m_d->readyShortcut->action()->deactivate(m_d->readyShortcut->shortcutIndex());
1269 m_d->readyShortcut = 0;
1270 }
1271 }
1272
1273 if (!m_d->readyShortcut) {
1274 DEBUG_SHORTCUT("Preparing new ready action", goodCandidate);
1275
1283 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1284 m_d->readyShortcut = goodCandidate;
1285 }
1286 } else if (m_d->readyShortcut) {
1287 DEBUG_SHORTCUT("Deactivating action", m_d->readyShortcut);
1288 m_d->readyShortcut->action()->deactivate(m_d->readyShortcut->shortcutIndex());
1289 m_d->readyShortcut = 0;
1290 }
1291}
1292
1293bool KisShortcutMatcher::tryEndRunningShortcut( Qt::MouseButton button, QEvent* event )
1294{
1295 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_d->runningShortcut, true);
1296 KIS_SAFE_ASSERT_RECOVER(!m_d->readyShortcut) {
1297 // it shouldn't have happened, running and ready shortcuts
1298 // at the same time should not be possible
1300 }
1301
1302 if (m_d->runningShortcut && m_d->runningShortcut->matchBegin(button)) {
1303
1304 // first reset running shortcut to avoid infinite recursion via end()
1305 KisStrokeShortcut *runningShortcut = m_d->runningShortcut;
1306 m_d->runningShortcut = 0;
1307
1308 if (runningShortcut->action()) {
1309 DEBUG_EVENT_ACTION("Ending running shortcut at event", event);
1311 int shortcutIndex = runningShortcut->shortcutIndex();
1312
1313 // Keep keyed stroke shortcuts in ready state between repeated
1314 // button strokes while their activation keys are still held.
1315 QSet<Qt::MouseButton> buttonsAfterRelease = m_d->buttons;
1316 buttonsAfterRelease.remove(button);
1317 const bool keepReadyShortcut =
1318 !m_d->keys.isEmpty() &&
1319 runningShortcut->isAvailable(m_d->actionGroupMask()) &&
1320 runningShortcut->matchReady(m_d->keys, buttonsAfterRelease);
1321
1322 action->end(event);
1323
1324 if (keepReadyShortcut) {
1325 m_d->readyShortcut = runningShortcut;
1326 } else {
1327 action->deactivate(shortcutIndex);
1328 }
1329 }
1330 }
1331
1332 return !m_d->runningShortcut;
1333}
1334
1336{
1337 KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->runningShortcut);
1338 KIS_SAFE_ASSERT_RECOVER(!m_d->readyShortcut) {
1339 // it shouldn't have happened, running and ready shortcuts
1340 // at the same time should not be possible
1342 }
1343
1344 // first reset running shortcut to avoid infinite recursion via end()
1345 KisStrokeShortcut *runningShortcut = m_d->runningShortcut;
1346 m_d->runningShortcut = 0;
1347
1348 if (runningShortcut->action()) {
1349 DEBUG_ACTION("Forced ending running shortcut at event");
1351 int shortcutIndex = runningShortcut->shortcutIndex();
1352
1353 QMouseEvent event = runningShortcut->fakeEndEvent(localPos);
1354
1355 action->end(&event);
1356 action->deactivate(shortcutIndex);
1357 }
1358}
1359
1361{
1362 if (m_d->readyShortcut) {
1363 DEBUG_SHORTCUT("Forcefully deactivating action", m_d->readyShortcut);
1364 m_d->readyShortcut->action()->deactivate(m_d->readyShortcut->shortcutIndex());
1365 m_d->readyShortcut = 0;
1366 }
1367}
1368
1370{
1371 int previousNumTouchPoints = -1;
1372
1373 if (m_d->bestCandidateForTapTouchEvent) {
1374 QTouchEvent *bestTouchEvent = static_cast<QTouchEvent*>(m_d->bestCandidateForTapTouchEvent.data());
1375 previousNumTouchPoints = KisTouchShortcut::countTouchPoints(bestTouchEvent, KisTouchShortcut::pressedOnlyTouchStates());
1376 }
1377
1379
1380 if (newNumTouchPoints >= previousNumTouchPoints) {
1381#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
1382 KoPointerEvent::copyQtPointerEvent(event, m_d->bestCandidateForTapTouchEvent);
1383#else
1384 m_d->bestCandidateForTapTouchEvent.reset(event->clone());
1385#endif
1386 }
1387}
1388
1389bool KisShortcutMatcher::tryFireTapTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates)
1390{
1396 if (m_d->actionsSuppressedIgnoreFocus())
1397 return false;
1398
1399 KisTouchShortcut *goodCandidate = matchTouchShortcut(event, allowedStates, TouchShortcutMode::Tap);
1400 if (goodCandidate) {
1401 DEBUG_SHORTCUT("Starting new touch-tap action", goodCandidate);
1402 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1403 goodCandidate->action()->begin(goodCandidate->shortcutIndex(), event);
1404
1405 goodCandidate->action()->end(event);
1406 goodCandidate->action()->deactivate(goodCandidate->shortcutIndex());
1407 }
1408
1409 return goodCandidate;
1410}
1411
1412KisTouchShortcut *KisShortcutMatcher::matchTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
1413{
1414 KisTouchShortcut *goodCandidate = nullptr;
1415 Q_FOREACH (KisTouchShortcut *shortcut, m_d->touchShortcuts) {
1416 if (shortcut->isAvailable(m_d->actionGroupMask())
1417 && matchTouchShortcutBasedOnState(event, shortcut, allowedStates, mode)
1418 && (!goodCandidate || shortcut->priority() > goodCandidate->priority())) {
1419
1420 goodCandidate = shortcut;
1421 }
1422 }
1423 return goodCandidate;
1424}
1425
1426bool KisShortcutMatcher::matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
1427{
1428 switch (mode) {
1430 return shortcut->matchHoldType(event, allowedStates);
1431 break;
1433 return shortcut->matchDragType(event, allowedStates);
1434 break;
1436 return shortcut->matchTapType(event, allowedStates);
1437 break;
1438 }
1439
1440#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1441 return false;
1442#else
1443 Q_UNREACHABLE_RETURN(false);
1444#endif
1445}
1446
1447bool KisShortcutMatcher::tryRunTouchShortcut(QTouchEvent* event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
1448{
1449 KisTouchShortcut *goodCandidate = matchTouchShortcut(event, allowedStates, mode);
1450
1456 if (m_d->actionsSuppressedIgnoreFocus())
1457 return false;
1458
1459 if (goodCandidate) {
1460 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!m_d->runningShortcut, false);
1461
1462 // Because we don't match keyboard or button based actions with touch system, we have to ensure that we first
1463 // deactivate an activated readyShortcut, to not throw other statemachines out of place.
1465
1466 m_d->touchShortcut = goodCandidate;
1467
1468 Private::RecursionGuard guard(this);
1469 DEBUG_SHORTCUT("Running a new touch action", goodCandidate)
1470
1471 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1472 goodCandidate->action()->begin(goodCandidate->shortcutIndex(), event);
1473
1474 // the tool might have opened some dialog, which could break our event loop
1475 if (guard.brokenByRecursion()) {
1476 goodCandidate->action()->end(event);
1477 m_d->touchShortcut = 0;
1478
1480 }
1481 }
1482
1483 return m_d->touchShortcut;
1484}
1485
1487{
1488 if (m_d->touchShortcut) {
1489 // first reset running shortcut to avoid infinite recursion via end()
1490 KisTouchShortcut *touchShortcut = m_d->touchShortcut;
1491
1492 DEBUG_SHORTCUT("ending", touchShortcut)
1493 touchShortcut->action()->end(event);
1494 touchShortcut->action()->deactivate(m_d->touchShortcut->shortcutIndex());
1495
1496 m_d->touchShortcut = 0; // empty it out now that we are done with it
1497
1498 return true;
1499 }
1500
1501 return false;
1502}
1503
1504template <typename EventType, typename ShortcutType>
1505bool KisShortcutMatcher::tryRunGestureBasedShortcutImpl(EventType *event, ShortcutType **resultShortcutPtr, const QList<ShortcutType*> allTypedShortcuts)
1506{
1507 ShortcutType *goodCandidate = 0;
1508
1509 if (m_d->actionsSuppressed())
1510 return false;
1511
1512 Q_FOREACH (ShortcutType* shortcut, allTypedShortcuts) {
1513 if (shortcut->match(event) && (!goodCandidate || shortcut->priority() > goodCandidate->priority())) {
1514 goodCandidate = shortcut;
1515 }
1516 }
1517
1518 if (goodCandidate) {
1519 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!m_d->runningShortcut, false);
1520
1521 // Because we don't match keyboard or button based actions with touch system, we have to ensure that we first
1522 // deactivate an activated readyShortcut, to not throw other statemachines out of place.
1524
1525 *resultShortcutPtr = goodCandidate;
1526
1527 Private::RecursionGuard guard(this);
1528
1529 DEBUG_SHORTCUT("starting", goodCandidate)
1530
1531 goodCandidate->action()->activate(goodCandidate->shortcutIndex());
1532 goodCandidate->action()->begin(goodCandidate->shortcutIndex(), event);
1533
1534 // the tool might have opened some dialog, which could break our event loop
1535 if (guard.brokenByRecursion()) {
1536 goodCandidate->action()->end(event);
1537 *resultShortcutPtr = 0;
1538
1540 }
1541 }
1542
1543 return bool(*resultShortcutPtr);
1544}
1545
1546template <typename EventType, typename ShortcutType>
1547bool KisShortcutMatcher::tryEndGestureBasedShortcutImpl(EventType *event, ShortcutType **resultShortcutPtr)
1548{
1549 Private::RecursionNotifier notifier(this);
1550
1551 if (*resultShortcutPtr) {
1552 // first reset running shortcut to avoid infinite recursion via end()
1553 ShortcutType *runningShortcut = *resultShortcutPtr;
1554 *resultShortcutPtr = nullptr;
1555
1557
1558 runningShortcut->action()->end(event);
1560
1561 return true;
1562 }
1563
1564 if (notifier.isInRecursion()) {
1566 } else if (!hasRunningShortcut()) {
1569 }
1570
1571 return false;
1572}
1573
1575{
1576 return tryRunGestureBasedShortcutImpl(event, &m_d->nativeGestureShortcut, m_d->nativeGestureShortcuts);
1577}
1578
1580{
1581 return tryEndGestureBasedShortcutImpl(event, &m_d->nativeGestureShortcut);
1582}
1583
1585{
1586 return tryRunGestureBasedShortcutImpl(event, &m_d->touchpadScrollShortcut, m_d->touchpadScrollShortcuts);
1587}
1588
1590{
1591 return tryEndGestureBasedShortcutImpl(event, &m_d->touchpadScrollShortcut);
1592}
1593
1595{
1596 return m_d->touchHoldDelayMs;
1597}
1598
1600{
1601 m_d->touchHoldDelayMs = value;
1602}
1603
1605{
1606 if (value) {
1607 m_d->ignoreMultiFingerCancelWorkaround = Private::IgnoreMultiFingerCancelWorkaround();
1608 } else {
1609 m_d->ignoreMultiFingerCancelWorkaround = std::nullopt;
1610 }
1611}
1612
1614{
1615 return m_d->ignoreMultiFingerCancelWorkaround.has_value();
1616}
float value(const T *src, size_t ch)
@ AllActionGroup
Abstract base class for input actions.
virtual void activate(int shortcut)
virtual bool trySwitchShortcut(int oldShortcut, int newShortcut)
virtual void deactivate(int shortcut)
virtual void end(QEvent *event)
virtual void begin(int shortcut, QEvent *event)
KisAbstractInputAction * action
virtual bool isAvailable(KisInputActionGroupsMask mask) const
bool matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
bool KeyboardActionsSuppressed() const
void lostFocusEvent(const QPointF &localPos)
bool tryEndTouchpadScrollShortcut(QWheelEvent *event)
QSet< Qt::MouseButton > buttons
QList< KisTouchpadScrollShortcut * > touchpadScrollShortcuts
KisTouchpadScrollShortcut * touchpadScrollShortcut
void forceEndRunningShortcut(const QPointF &localPos)
bool actionsSuppressedIgnoreFocus() const
QList< KisNativeGestureShortcut * > nativeGestureShortcuts
bool tryRunTouchpadScrollShortcut(QWheelEvent *event)
bool autoRepeatedKeyPressed(Qt::Key key)
bool buttonReleased(Qt::MouseButton button, QEvent *event)
QList< KisStrokeShortcut * > strokeShortcuts
bool touchpadScrollEndEvent(QWheelEvent *event)
bool hasTouchpadScrollShortcuts() const
std::optional< KisTouchHoldEventsPostponer > touchHoldEventPostponer
bool tryFireTapTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates)
bool tryEndGestureBasedShortcutImpl(EventType *event, ShortcutType **resultShortcutPtr)
void addShortcut(KisSingleActionShortcut *shortcut)
bool touchUpdateEventImpl(QTouchEvent *event)
void setTouchHoldDelay(int value)
QList< KisSingleActionShortcut * > singleActionShortcuts
bool tryRunNativeGestureShortcut(QNativeGestureEvent *event)
QList< KisStrokeShortcut * > candidateShortcuts
bool tryRunSingleActionShortcutImpl(T param, U *event, const QSet< Qt::Key > &keysState, bool keyboard=true)
bool nativeGestureEndEvent(QNativeGestureEvent *event)
bool tryEndTouchShortcut(QTouchEvent *event)
bool keyPressed(Qt::Key key)
bool tryRunTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
bool touchBeginEvent(QTouchEvent *event)
bool tryRunReadyShortcut(Qt::MouseButton button, QEvent *event)
bool touchpadScrollBeginEvent(QWheelEvent *event)
bool touchpadScrollEvent(QWheelEvent *event)
KisTouchShortcut * touchShortcut
bool tryRunGestureBasedShortcutImpl(EventType *event, ShortcutType **resultShortcutPtr, const QList< ShortcutType * > allTypedShortcuts)
void setInputActionGroupsMaskCallback(std::function< KisInputActionGroupsMask()> func)
void setIgnoreMultiFingerCancelWorkaroundEnalbed(bool value)
QScopedPointer< QEvent > bestCandidateForTapTouchEvent
static constexpr qreal TOUCH_SLOP
KisStrokeShortcut * readyShortcut
void touchCancelEvent(QTouchEvent *event)
QVector< Qt::Key > debugPressedKeys() const
std::optional< IgnoreMultiFingerCancelWorkaround > ignoreMultiFingerCancelWorkaround
bool touchEndEvent(QTouchEvent *event)
bool touchUpdateEvent(QTouchEvent *event)
KisTouchShortcut * matchTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode)
std::function< KisInputActionGroupsMask()> actionGroupMask
bool nativeGestureEvent(QNativeGestureEvent *event)
KisNativeGestureShortcut * nativeGestureShortcut
bool buttonPressed(Qt::MouseButton button, QEvent *event)
QList< KisTouchShortcut * > touchShortcuts
void handlePolledKeys(const QVector< Qt::Key > &keys)
bool tryEndRunningShortcut(Qt::MouseButton button, QEvent *event)
bool tryRunWheelShortcut(KisSingleActionShortcut::WheelAction wheelAction, QWheelEvent *event)
bool sanityCheckModifiersCorrectness(Qt::KeyboardModifiers modifiers) const
bool pointerMoved(QEvent *event)
bool nativeGestureBeginEvent(QNativeGestureEvent *event)
void setMaxTouchPointEvent(QTouchEvent *event)
void suppressConflictingKeyActions(const QVector< QKeySequence > &shortcuts)
KisStrokeShortcut * runningShortcut
bool keyReleased(Qt::Key key)
bool ignoreMultiFingerCancelWorkaroundEnalbed() const
QSet< KisSingleActionShortcut * > suppressedSingleActionShortcuts
bool wheelEvent(KisSingleActionShortcut::WheelAction wheelAction, QWheelEvent *event)
bool tryEndNativeGestureShortcut(QNativeGestureEvent *event)
QScopedPointer< QTouchEvent > lastProcessedTouchEvent
bool conflictsWith(const QKeySequence &seq)
bool match(const QSet< Qt::Key > &modifiers, Qt::Key key)
int priority() const override
QMouseEvent fakeEndEvent(const QPointF &localPos) const
bool matchReady(const QSet< Qt::Key > &modifiers, const QSet< Qt::MouseButton > &buttons)
bool matchBegin(Qt::MouseButton button)
@ HoldCompleted
the postponer is still accumulating the events
@ HoldCancelled
the hold action has been detected, the accumulated events should be dropped and the hold action shoul...
The KisTouchShortcut class only handles touch gestures it does not handle tool invocation i....
bool isAvailable(KisInputActionGroupsMask mask) const override
static Qt::TouchPointStates pressedOnlyTouchStates()
bool matchTapType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
bool matchDragType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static qreal touchDragDistance(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
bool matchHoldType(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static int countTouchPoints(const QTouchEvent *event, Qt::TouchPointStates allowedStates)
static Qt::TouchPointStates allTouchStates()
int priority() const override
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
const KRITAGLOBAL_EXPORT QLoggingCategory & _41020()
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
#define DEBUG_BUTTON_ACTION(text, button)
#define DEBUG_SHORTCUT(text, shortcut)
Qt::MouseButtons listToFlags(const QList< Qt::MouseButton > &list)
#define DEBUG_EVENT_ACTION(text, event)
QString debugShortcutName(const KisAbstractShortcut *shortcut)
#define DEBUG_KEY(text)
#define DEBUG_TOUCH_ACTION(text, event)
QString button(const QWheelEvent &ev)
#define DEBUG_ACTION(action)