Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Fix conflicts between mouse and touch actions requested concurrently

Dmitry Kazakovbad0dd9cd86a
diff --git a/libs/ui/input/kis_input_manager.cpp b/libs/ui/input/kis_input_manager.cpp
index 9ef3147eca..d91da96296 100644
--- a/libs/ui/input/kis_input_manager.cpp
+++ b/libs/ui/input/kis_input_manager.cpp
@@ -781,8 +781,6 @@ bool KisInputManager::eventFilterImpl(QEvent * event)
retval = d->matcher.buttonReleased(Qt::LeftButton, touchEvent);
d->previousPos = {0, 0};
d->touchStrokeStarted = false; // stroke ended
- } else {
- d->matcher.touchResetStateForPointerEvents();
}
// if the event isn't handled, Qt starts to send MouseEvents
diff --git a/libs/ui/input/kis_shortcut_matcher.cpp b/libs/ui/input/kis_shortcut_matcher.cpp
index 053cbe3970..4dc0730ac6 100644
--- a/libs/ui/input/kis_shortcut_matcher.cpp
+++ b/libs/ui/input/kis_shortcut_matcher.cpp
@@ -55,8 +55,6 @@ public:
, suppressAllActions(false)
, suppressAllKeyboardActions(false)
, cursorEntered(false)
- , usingTouch(false)
- , usingNativeGesture(false)
{}
~Private()
@@ -94,8 +92,6 @@ public:
bool suppressAllActions;
bool suppressAllKeyboardActions;
bool cursorEntered;
- bool usingTouch;
- bool usingNativeGesture;
int recursiveCounter = 0;
int brokenByRecursion = 0;
@@ -153,11 +149,6 @@ public:
inline bool KeyboardActionsSuppressed() const {
return suppressAllKeyboardActions;
}
-
- // only for touch events with touchPoints count >= 2
- inline bool isUsingTouch() const {
- return usingTouch || usingNativeGesture;
- }
};
KisShortcutMatcher::KisShortcutMatcher()
@@ -171,7 +162,7 @@ KisShortcutMatcher::~KisShortcutMatcher()
bool KisShortcutMatcher::hasRunningShortcut() const
{
- return m_d->runningShortcut;
+ return m_d->runningShortcut || m_d->touchShortcut || m_d->nativeGestureShortcut;
}
void KisShortcutMatcher::addShortcut(KisSingleActionShortcut *shortcut)
@@ -198,7 +189,9 @@ bool KisShortcutMatcher::supportsHiResInputEvents()
return (m_d->runningShortcut && m_d->runningShortcut->action()
&& m_d->runningShortcut->action()->supportsHiResInputEvents(m_d->runningShortcut->shortcutIndex()))
|| (m_d->touchShortcut && m_d->touchShortcut->action()
- && m_d->touchShortcut->action()->supportsHiResInputEvents(m_d->touchShortcut->shortcutIndex()));
+ && m_d->touchShortcut->action()->supportsHiResInputEvents(m_d->touchShortcut->shortcutIndex()))
+ || (m_d->nativeGestureShortcut && m_d->nativeGestureShortcut->action()
+ && m_d->nativeGestureShortcut->action()->supportsHiResInputEvents(m_d->nativeGestureShortcut->shortcutIndex()));
}
bool KisShortcutMatcher::keyPressed(Qt::Key key)
@@ -209,7 +202,7 @@ bool KisShortcutMatcher::keyPressed(Qt::Key key)
if (m_d->keys.contains(key)) { DEBUG_ACTION("Peculiar, records show key was already pressed"); }
- if (!m_d->runningShortcut && !notifier.isInRecursion()) {
+ if (!hasRunningShortcut() && !notifier.isInRecursion()) {
retval = tryRunSingleActionShortcutImpl(key, (QEvent*)0, m_d->keys);
}
@@ -218,7 +211,7 @@ bool KisShortcutMatcher::keyPressed(Qt::Key key)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -241,7 +234,7 @@ bool KisShortcutMatcher::autoRepeatedKeyPressed(Qt::Key key)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
// Autorepeated key should not be included in the shortcut
QSet<Qt::Key> filteredKeys = m_d->keys;
filteredKeys.remove(key);
@@ -264,7 +257,7 @@ bool KisShortcutMatcher::keyReleased(Qt::Key key)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -279,13 +272,9 @@ bool KisShortcutMatcher::buttonPressed(Qt::MouseButton button, QEvent *event)
bool retval = false;
- if (m_d->isUsingTouch()) {
- return retval;
- }
-
if (m_d->buttons.contains(button)) { DEBUG_ACTION("Peculiar, button was already pressed."); }
- if (!m_d->runningShortcut && !notifier.isInRecursion()) {
+ if (!hasRunningShortcut() && !notifier.isInRecursion()) {
prepareReadyShortcuts();
retval = tryRunReadyShortcut(button, event);
}
@@ -294,7 +283,7 @@ bool KisShortcutMatcher::buttonPressed(Qt::MouseButton button, QEvent *event)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -309,10 +298,7 @@ bool KisShortcutMatcher::buttonReleased(Qt::MouseButton button, QEvent *event)
bool retval = false;
- if (m_d->isUsingTouch()) {
- return retval;
- }
-
+ // here we check for the presence of the **stroke** shortcut only
if (m_d->runningShortcut) {
KIS_SAFE_ASSERT_RECOVER_NOOP(!notifier.isInRecursion());
@@ -325,7 +311,7 @@ bool KisShortcutMatcher::buttonReleased(Qt::MouseButton button, QEvent *event)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -338,7 +324,7 @@ bool KisShortcutMatcher::wheelEvent(KisSingleActionShortcut::WheelAction wheelAc
Private::RecursionNotifier notifier(this);
- if (m_d->runningShortcut || m_d->isUsingTouch() || notifier.isInRecursion()) {
+ if (hasRunningShortcut() || notifier.isInRecursion()) {
DEBUG_ACTION("Wheel event canceled.");
return false;
}
@@ -350,11 +336,12 @@ bool KisShortcutMatcher::pointerMoved(QEvent *event)
{
Private::RecursionNotifier notifier(this);
-
- if (m_d->isUsingTouch() || !m_d->runningShortcut || notifier.isInRecursion()) {
+ if (!m_d->runningShortcut || notifier.isInRecursion()) {
return false;
}
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!m_d->touchShortcut && !m_d->nativeGestureShortcut, false);
+
m_d->runningShortcut->action()->inputEvent(event);
return true;
}
@@ -367,7 +354,7 @@ void KisShortcutMatcher::enterEvent()
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -381,7 +368,7 @@ void KisShortcutMatcher::leaveEvent()
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -439,7 +426,7 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
// we should end the event as an event with more touchpoints was received
retval = tryEndTouchShortcut(event);
}
- if (!m_d->touchShortcut && touchPointCount >= m_d->maxTouchPoints) {
+ if (!hasRunningShortcut() && touchPointCount >= m_d->maxTouchPoints) {
m_d->maxTouchPoints = touchPointCount;
DEBUG_TOUCH_ACTION("starting", event);
retval = tryRunTouchShortcut(event);
@@ -462,7 +449,7 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
}
} else {
// triggered if a new finger was added, which might result in shortcut not matching the action
- if ((event->touchPointStates() & Qt::TouchPointReleased) == Qt::TouchPointReleased) {
+ if ((event->touchPointStates() & Qt::TouchPointReleased) == Qt::TouchPointReleased && !hasRunningShortcut()) {
// we should end the event as an event with more touchpoints was received
if (m_d->maxTouchPoints <= touchPointCount) {
m_d->maxTouchPoints = touchPointCount;
@@ -478,10 +465,9 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)
bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)
{
- m_d->usingTouch = false; // we need to say we are done because qt will not send further event
m_d->maxTouchPoints = 0;
- if (!m_d->isTouchDragDetected && m_d->bestCandidateTouchEvent) {
+ if (!m_d->isTouchDragDetected && m_d->bestCandidateTouchEvent && !hasRunningShortcut()) {
fireReadyTouchShortcut(static_cast<QTouchEvent *>(m_d->bestCandidateTouchEvent.data()));
}
@@ -492,15 +478,9 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)
void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event, const QPointF &localPos)
{
- m_d->usingTouch = false;
m_d->maxTouchPoints = 0;
- // TODO(sh_zam): maybe try to combine KisStrokeShortcut with KisTouchShortcut?
-
- // this should end the stroke based actions
- if (m_d->runningShortcut) {
- forceEndRunningShortcut(localPos);
- }
+ KIS_SAFE_ASSERT_RECOVER_NOOP(!m_d->runningShortcut || !m_d->touchShortcut);
// end the stroke types
if (m_d->touchShortcut) {
@@ -517,14 +497,6 @@ void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event, const QPointF &loc
}
}
-void KisShortcutMatcher::touchResetStateForPointerEvents()
-{
- // we reset canvas back to the "default" pointer state
- m_d->readyShortcut = 0;
- prepareReadyShortcuts();
- tryActivateReadyShortcut();
-}
-
bool KisShortcutMatcher::nativeGestureBeginEvent(QNativeGestureEvent *event)
{
Q_UNUSED(event);
@@ -537,10 +509,10 @@ bool KisShortcutMatcher::nativeGestureBeginEvent(QNativeGestureEvent *event)
bool KisShortcutMatcher::nativeGestureEvent(QNativeGestureEvent *event)
{
bool retval = false;
- if ( !m_d->nativeGestureShortcut ) {
+ if (hasRunningShortcut()) {
retval = tryRunNativeGestureShortcut( event );
}
- else {
+ else if (m_d->nativeGestureShortcut) {
m_d->nativeGestureShortcut->action()->inputEvent( event );
retval = true;
}
@@ -553,7 +525,6 @@ bool KisShortcutMatcher::nativeGestureEndEvent(QNativeGestureEvent *event)
if ( m_d->nativeGestureShortcut && !m_d->nativeGestureShortcut->match( event ) ) {
tryEndNativeGestureShortcut( event );
}
- m_d->usingNativeGesture = false;
return true;
}
@@ -574,7 +545,7 @@ void KisShortcutMatcher::reinitialize()
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -589,7 +560,7 @@ void KisShortcutMatcher::reinitializeButtons()
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -614,7 +585,7 @@ void KisShortcutMatcher::handlePolledKeys(const QVector<Qt::Key> &keys)
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -658,6 +629,8 @@ void KisShortcutMatcher::lostFocusEvent(const QPointF &localPos)
}
forceDeactivateAllActions();
+
+ // TODO: also deactivate all touch and native gestures on focus out
}
void KisShortcutMatcher::toolHasBeenActivated()
@@ -668,7 +641,7 @@ void KisShortcutMatcher::toolHasBeenActivated()
if (notifier.isInRecursion()) {
forceDeactivateAllActions();
- } else if (!m_d->runningShortcut) {
+ } else if (!hasRunningShortcut()) {
prepareReadyShortcuts();
tryActivateReadyShortcut();
}
@@ -859,6 +832,14 @@ void KisShortcutMatcher::tryActivateReadyShortcut()
if (!m_d->readyShortcut) {
DEBUG_SHORTCUT("Preparing new ready action", goodCandidate);
+
+ /**
+ * It is important that we first activate the action, and only after
+ * that assign it to m_d->readyShortcut. It makes is possible to activate
+ * another tool in KisToolInvocationAction and survive the call to
+ * forceDeactivateAllActions() from lostFocusEvent(), which would
+ * enter infinite loop otherwise.
+ */
goodCandidate->action()->activate(goodCandidate->shortcutIndex());
m_d->readyShortcut = goodCandidate;
}
@@ -977,25 +958,13 @@ bool KisShortcutMatcher::tryRunTouchShortcut( QTouchEvent* event )
return false;
if (goodCandidate) {
- if (m_d->runningShortcut) {
- QTouchEvent touchEvent(QEvent::TouchEnd,
- event->device(),
- event->modifiers(),
- Qt::TouchPointReleased,
- event->touchPoints());
- tryEndRunningShortcut(Qt::LeftButton, &touchEvent);
- }
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!m_d->runningShortcut, false);
// Because we don't match keyboard or button based actions with touch system, we have to ensure that we first
// deactivate an activated readyShortcut, to not throw other statemachines out of place.
- if (m_d->readyShortcut) {
- DEBUG_SHORTCUT("Deactivating readyShortcut action for touch shortcut", m_d->readyShortcut);
- m_d->readyShortcut->action()->deactivate(m_d->readyShortcut->shortcutIndex());
- m_d->readyShortcut = nullptr;
- }
+ forceDeactivateAllActions();
m_d->touchShortcut = goodCandidate;
- m_d->usingTouch = true;
Private::RecursionGuard guard(this);
DEBUG_SHORTCUT("Running a touch shortcut", goodCandidate)
@@ -1017,7 +986,7 @@ bool KisShortcutMatcher::tryRunTouchShortcut( QTouchEvent* event )
bool KisShortcutMatcher::tryEndTouchShortcut( QTouchEvent* event )
{
- if(m_d->touchShortcut) {
+ if (m_d->touchShortcut) {
// first reset running shortcut to avoid infinite recursion via end()
KisTouchShortcut *touchShortcut = m_d->touchShortcut;
@@ -1047,8 +1016,13 @@ bool KisShortcutMatcher::tryRunNativeGestureShortcut(QNativeGestureEvent* event)
}
if (goodCandidate) {
+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_d->runningShortcut, false);
+
+ // Because we don't match keyboard or button based actions with touch system, we have to ensure that we first
+ // deactivate an activated readyShortcut, to not throw other statemachines out of place.
+ forceDeactivateAllActions();
+
m_d->nativeGestureShortcut = goodCandidate;
- m_d->usingNativeGesture = true;
Private::RecursionGuard guard(this);
goodCandidate->action()->activate(goodCandidate->shortcutIndex());
diff --git a/libs/ui/input/kis_shortcut_matcher.h b/libs/ui/input/kis_shortcut_matcher.h
index 510925b946..fb4abb78d5 100644
--- a/libs/ui/input/kis_shortcut_matcher.h
+++ b/libs/ui/input/kis_shortcut_matcher.h
@@ -184,9 +184,6 @@ public:
*/
void touchCancelEvent(QTouchEvent *event, const QPointF &localPos);
- void touchResetStateForPointerEvents();
-
-
bool nativeGestureBeginEvent(QNativeGestureEvent *event);
bool nativeGestureEvent(QNativeGestureEvent *event);
bool nativeGestureEndEvent(QNativeGestureEvent *event);