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++
Make tablet events have higher priority over touch events
Dmitry Kazakovdc2e811e7404
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txtindex d158b36681..9cd011523b 100644--- a/libs/ui/CMakeLists.txt+++ b/libs/ui/CMakeLists.txt@@ -778,6 +778,12 @@ if (QT_MAJOR_VERSION STREQUAL "6")Qt6::SvgWidgetsKF6::ColorScheme)++ set_source_files_properties(+ input/kis_shortcut_matcher.cpp+ PROPERTIES+ INCLUDE_DIRECTORIES "${Qt${QT_MAJOR_VERSION}Gui_PRIVATE_INCLUDE_DIRS}"+ )endif()if (Mlt7_FOUND)diff --git a/libs/ui/input/kis_shortcut_matcher.cpp b/libs/ui/input/kis_shortcut_matcher.cppindex 98a8274a21..64d2982755 100644--- a/libs/ui/input/kis_shortcut_matcher.cpp+++ b/libs/ui/input/kis_shortcut_matcher.cpp@@ -20,6 +20,12 @@#include "KisTouchHoldEventsPostponer.h"#include <KoPointerEvent.h>+#if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)+// for QMutableEventPoint+#include <QWindow>+#include <QtGui/private/qeventpoint_p.h>+#endif+//#define DEBUG_MATCHER#ifdef DEBUG_MATCHER@@ -43,6 +49,36 @@#define DEBUG_TOUCH_ACTION(text, event)#endif+namespace+{+QTouchEvent generateFakeTouchEndEvent(const QTouchEvent *event)+{+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ auto points = event->touchPoints();+#else+ auto points = event->points();+#endif++ for (auto it = points.begin(); it != points.end();) {+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ if (it->state() == Qt::TouchPointReleased) {+ it = points.erase(it);+ } else {+ it->setState(Qt::TouchPointReleased);+ }+#else+ if (it->state() == QEventPoint::Released) {+ it = points.erase(it);+ } else {+ QMutableEventPoint::setState(*it, QEventPoint::Released);+ }+#endif+ }++ return QTouchEvent(QEvent::TouchEnd, event->pointingDevice(), event->modifiers(), points);+}++} // namespaceclass Q_DECL_HIDDEN KisShortcutMatcher::Private{@@ -82,7 +118,6 @@ public:KisTouchShortcut *touchShortcut;KisNativeGestureShortcut *nativeGestureShortcut;- QList<QTouchEvent::TouchPoint> lastTouchPoints;std::optional<KisTouchHoldEventsPostponer> touchHoldEventPostponer;int maxTouchPoints{0};@@ -90,6 +125,8 @@ public:bool isTouchDragDetected {false};bool isTouchHeld {false};QScopedPointer<QEvent> bestCandidateForTapTouchEvent;+ QScopedPointer<QTouchEvent> lastProcessedTouchEvent;+ bool touchActionTracked {false};std::function<KisInputActionGroupsMask()> actionGroupMask;bool suppressAllActions;@@ -283,6 +320,10 @@ bool KisShortcutMatcher::buttonPressed(Qt::MouseButton button, QEvent *event)Private::RecursionNotifier notifier(this);DEBUG_BUTTON_ACTION("entered", button);+ // the tablet actions have the priority over any existing+ // touch action, so we should cancel them first+ tryCancelAllCurrentTouchActionsImpl();+bool retval = false;if (m_d->buttons.contains(button)) { DEBUG_ACTION("Peculiar, button was already pressed."); }@@ -398,13 +439,18 @@ bool KisShortcutMatcher::touchBeginEvent( QTouchEvent* event )Private::RecursionNotifier notifier(this);- m_d->lastTouchPoints = event->touchPoints();+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))+ KoPointerEvent::copyQtPointerEvent(event, m_d->lastProcessedTouchEvent);+#else+ m_d->lastProcessedTouchEvent.reset(event->clone());+#endif// reset statem_d->maxTouchPoints = event->touchPoints().size();m_d->matchingIteration = 1;m_d->isTouchDragDetected = false;m_d->isTouchHeld = false;+ m_d->touchActionTracked = true;#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))KoPointerEvent::copyQtPointerEvent(event, m_d->bestCandidateForTapTouchEvent);#else@@ -432,6 +478,10 @@ bool KisShortcutMatcher::touchBeginEvent( QTouchEvent* event )bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event){+ // the touch action has been overridden by some tablet action,+ // consume and ignore it.+ if (!m_d->touchActionTracked) return true;+if (m_d->touchHoldEventPostponer) {m_d->touchHoldEventPostponer->pushThrough(event);if (m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::HoldCancelled) {@@ -488,6 +538,12 @@ bool KisShortcutMatcher::touchUpdateEventImpl(QTouchEvent *event){DEBUG_TOUCH_ACTION("entered", event)+#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))+ KoPointerEvent::copyQtPointerEvent(event, m_d->lastProcessedTouchEvent);+#else+ m_d->lastProcessedTouchEvent.reset(event->clone());+#endif+if (m_d->isTouchHeld) {KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(m_d->touchShortcut, false);m_d->touchShortcut->action()->inputEvent(event);@@ -514,8 +570,9 @@ bool KisShortcutMatcher::touchUpdateEventImpl(QTouchEvent *event)KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::pressedOnlyTouchStates());if (!hasRunningShortcut() && numStillDraggedPoints >= m_d->maxTouchPoints) {m_d->maxTouchPoints = numStillDraggedPoints;- DEBUG_TOUCH_ACTION("starting", event);+ DEBUG_TOUCH_ACTION("trying to start a touch-drag shortcut", event);if (tryRunTouchShortcut(event, KisTouchShortcut::pressedOnlyTouchStates(), TouchShortcutMode::Drag)) {+ DEBUG_TOUCH_ACTION("started a touch-drag shortcut", event);m_d->isTouchDragDetected = true;return true;}@@ -528,7 +585,7 @@ bool KisShortcutMatcher::touchUpdateEventImpl(QTouchEvent *event)if (m_d->matchingIteration <= numIterations && !m_d->isTouchDragDetected) {m_d->matchingIteration++;setMaxTouchPointEvent(event);- DEBUG_TOUCH_ACTION("return best", event)+ DEBUG_TOUCH_ACTION("return best tap shortcut", event)return matchTouchShortcut((QTouchEvent *)m_d->bestCandidateForTapTouchEvent.data(),KisTouchShortcut::allTouchStates(),TouchShortcutMode::Tap);@@ -575,6 +632,16 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event){Private::RecursionNotifier notifier(this);+ // the touch action has been overridden by some tablet action,+ // consume and ignore it.+ if (!m_d->touchActionTracked) {+ KIS_SAFE_ASSERT_RECOVER(!m_d->touchHoldEventPostponer.has_value()) {+ m_d->touchHoldEventPostponer->cancelHoldWait();+ m_d->touchHoldEventPostponer.reset();+ }+ return true;+ }+// flush all the touch-hold postiponed events if they were presentif (m_d->touchHoldEventPostponer) {KIS_SAFE_ASSERT_RECOVER_NOOP(m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::WaitingForHold);@@ -590,8 +657,7 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)m_d->touchHoldEventPostponer.reset();}- m_d->maxTouchPoints = 0;- m_d->isTouchHeld = false;+ m_d->touchActionTracked = false;bool retval = false;@@ -615,10 +681,8 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)return retval;}-void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event)+void KisShortcutMatcher::tryCancelAllCurrentTouchActionsImpl(){- Private::RecursionNotifier notifier(this);-// discard all the postponed touch-hold eventsif (m_d->touchHoldEventPostponer) {KIS_SAFE_ASSERT_RECOVER_NOOP(m_d->touchHoldEventPostponer->state() == KisTouchHoldEventsPostponer::WaitingForHold);@@ -626,37 +690,45 @@ void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event)m_d->touchHoldEventPostponer.reset();}- m_d->maxTouchPoints = 0;- m_d->isTouchHeld = false;-- KIS_SAFE_ASSERT_RECOVER_NOOP(!m_d->runningShortcut || !m_d->touchShortcut);-- // end the stroke types+ // end the touch action if presentif (m_d->touchShortcut) {KisTouchShortcut *touchShortcut = m_d->touchShortcut;m_d->touchShortcut = 0;- QScopedPointer<QEvent> dstEvent;-#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))- KoPointerEvent::copyQtPointerEvent(event, dstEvent);-#else- dstEvent.reset(event->clone());-#endif- // HACK: Because TouchEvents in KoPointerEvent need to contain at least one touchpoint- QTouchEvent* touchEvent = dynamic_cast<QTouchEvent *>(dstEvent.data());- KIS_ASSERT(touchEvent);-#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))- touchEvent->setTouchPoints(m_d->lastTouchPoints);-#else- dstEvent.reset(new QTouchEvent(event->type(),- event->pointingDevice(),- event->modifiers(),- m_d->lastTouchPoints));-#endif- touchShortcut->action()->end(dstEvent.data());+ KIS_SAFE_ASSERT_RECOVER_RETURN(m_d->lastProcessedTouchEvent);++ // on some platforms touch-cancel event may have **no**+ // touch points, which would greatly confuse our KoPointerEvent+ // class, so we should just generate a normal touch-end from the+ // last known event+ QTouchEvent touchEvent = generateFakeTouchEndEvent(m_d->lastProcessedTouchEvent.data());+ touchShortcut->action()->end(&touchEvent);touchShortcut->action()->deactivate(touchShortcut->shortcutIndex());}+ m_d->touchActionTracked = false;+}++void KisShortcutMatcher::touchCancelEvent(QTouchEvent *event)+{+ Q_UNUSED(event)++ Private::RecursionNotifier notifier(this);++ // the touch action has been overridden by some tablet action,+ // consume and ignore it.+ if (!m_d->touchActionTracked) {+ KIS_SAFE_ASSERT_RECOVER(!m_d->touchHoldEventPostponer.has_value()) {+ m_d->touchHoldEventPostponer->cancelHoldWait();+ m_d->touchHoldEventPostponer.reset();+ }+ return;+ }++ KIS_SAFE_ASSERT_RECOVER_NOOP(!m_d->runningShortcut || !m_d->touchShortcut);++ tryCancelAllCurrentTouchActionsImpl();+if (notifier.isInRecursion()) {forceDeactivateAllActions();} else if (!hasRunningShortcut()) {diff --git a/libs/ui/input/kis_shortcut_matcher.h b/libs/ui/input/kis_shortcut_matcher.hindex 093283457b..b3302c1251 100644--- a/libs/ui/input/kis_shortcut_matcher.h+++ b/libs/ui/input/kis_shortcut_matcher.h@@ -314,6 +314,7 @@ private:bool matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut, Qt::TouchPointStates allowedStates, TouchShortcutMode mode);bool tryRunTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates, TouchShortcutMode mode);bool tryEndTouchShortcut(QTouchEvent *event);+ void tryCancelAllCurrentTouchActionsImpl();bool tryRunNativeGestureShortcut(QNativeGestureEvent *event);bool tryEndNativeGestureShortcut(QNativeGestureEvent *event);diff --git a/libs/ui/tests/kis_input_manager_test.cpp b/libs/ui/tests/kis_input_manager_test.cppindex 4f71e5ed9c..e41b951f29 100644--- a/libs/ui/tests/kis_input_manager_test.cpp+++ b/libs/ui/tests/kis_input_manager_test.cpp@@ -1188,8 +1188,165 @@ void KisInputManagerTest::testTouchHoldPostponer()QCOMPARE(postponer.state(), KisTouchHoldEventsPostponer::WaitingForHold);}}+}++void KisInputManagerTest::testTouchOverriddenByTablet_data()+{+ QTest::addColumn<QString>("sequenceName");+ QTest::addColumn<QPointF>("pointOffset");+ QTest::addColumn<int>("holdTimeout");+ QTest::addColumn<int>("tabletStartEventIndex");+ QTest::addColumn<QList<int>>("triggeredShortcuts");++ const QPointF dragOffset(10,0);+ const QPointF holdOffset(1,0);+ const int holdTimeout = 170 /* ms */; // the timeout should happen on the fourth event after the first (with a 30 ms margin)++ QTest::addRow("clean-1p-drag-no-tablet") << "touchCleanDrag" << dragOffset << holdTimeout << 200 << QList<int>{15};+ QTest::addRow("clean-1p-hold-no-tablet") << "touchHoldFirstFourUpdateEventsFor50ms" << holdOffset << holdTimeout << 200 << QList<int>{40};+ QTest::addRow("clean-1p-drag-slow-start-no-tablet") << "touchCleanDrag" << holdOffset << holdTimeout << 200 << QList<int>{15};++ /// tablet action has been started while touch drag was running,+ /// the touch action is ended and a tablet action is started+ QTest::addRow("clean-1p-drag") << "touchCleanDrag" << dragOffset << holdTimeout << 2 << QList<int>{15, 50};++ /// tablet action has been started while touch-hold was being waited for,+ /// the touch-hold action should never start until the next touch-begin+ QTest::addRow("clean-1p-hold") << "touchHoldFirstFourUpdateEventsFor50ms" << holdOffset << holdTimeout << 2 << QList<int>{50};++ /// tablet action has been started before the touch-drag hasn't reached the+ /// drash threshold, the touch-drag action should never start until+ /// the next touch-begin+ QTest::addRow("clean-1p-drag-slow-start") << "touchCleanDrag" << holdOffset << holdTimeout << 2 << QList<int>{50};+}++void KisInputManagerTest::testTouchOverriddenByTablet()+{+ QFETCH(QString, sequenceName);+ QFETCH(QPointF, pointOffset);+ QFETCH(int, holdTimeout); // TODO: unused!+ QFETCH(int, tabletStartEventIndex);+ QFETCH(QList<int>, triggeredShortcuts);++ const int eventsCount = 100;++ std::unique_ptr<TestingAction> paintAction(new TestingAction("paint-action"));+ std::unique_ptr<TestingAction> a(new TestingAction("touch-action"));+ KisShortcutMatcher m;+ m.enterEvent();++ m.addShortcut(+ createTouchShortcut(a.get(), 15, KisShortcutConfiguration::OneFingerDrag));++ m.addShortcut(+ createTouchShortcut(a.get(), 40, KisShortcutConfiguration::OneFingerHold));++ m.addShortcut(+ createStrokeShortcut(a.get(), 50, {}, Qt::LeftButton));++ KisConfig(false).setTouchPainting(KisConfig::TOUCH_PAINTING_DISABLED);++ std::unique_ptr<TouchSequenceGeneratorBase> strokeGenerator(+ createTouchSequenceGenerator(sequenceName, 1, eventsCount - 1, pointOffset));++#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ QTouchDevice device;+ auto calcTouchPointStates = [] (const QList<TouchSequenceGeneratorBase::TouchPoint> &touchPoints) -> Qt::TouchPointStates {+ Qt::TouchPointStates result;++ for (auto it = touchPoints.begin(); it != touchPoints.end(); ++it) {+ result |= it->state();+ }++ return result;+ };+#else+ QPointingDevice device;+#endif++ QList<TouchSequenceGeneratorBase::TouchPoint> touchPoints;++ for (int eventIndex = 0; eventIndex < eventsCount; eventIndex++) {+ strokeGenerator->updatePoints(eventIndex, touchPoints);+ if (eventIndex == 0) {+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ QTouchEvent e(QEvent::TouchBegin, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints);+#else+ QTouchEvent e(QEvent::TouchBegin, &device, Qt::NoModifier, touchPoints);+#endif+ m.touchBeginEvent(&e);+ } else if (eventIndex > 0 && eventIndex < eventsCount - 1) {+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ QTouchEvent e(QEvent::TouchUpdate, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints);+#else+ QTouchEvent e(QEvent::TouchUpdate, &device, Qt::NoModifier, touchPoints);+#endif+ m.touchUpdateEvent(&e);+ } else if (eventIndex == eventsCount - 1) {+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ QTouchEvent e(QEvent::TouchEnd, &device, Qt::NoModifier, calcTouchPointStates(touchPoints), touchPoints);+#else+ QTouchEvent e(QEvent::TouchEnd, &device, Qt::NoModifier, touchPoints);+#endif+ m.touchEndEvent(&e);+ } else {+ qFatal("invalid step value");+ }++ if (eventIndex == tabletStartEventIndex) {+ const QPointF pos(eventIndex, 0.0);+ QTabletEvent event(QEvent::TabletPress,+ &device,+ pos,+ pos,+ 1.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ Qt::NoModifier,+ Qt::LeftButton,+ Qt::LeftButton);+ m.buttonPressed(Qt::LeftButton, &event);+ } else if (eventIndex > tabletStartEventIndex && eventIndex < tabletStartEventIndex + 10) {+ const QPointF pos(eventIndex, 0.0);+ QTabletEvent event(QEvent::TabletMove,+ &device,+ pos,+ pos,+ 1.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ Qt::NoModifier,+ Qt::NoButton,+ Qt::LeftButton);+ m.pointerMoved(&event);+ } else if (eventIndex == tabletStartEventIndex + 10) {+ const QPointF pos(eventIndex, 0.0);+ QTabletEvent event(QEvent::TabletRelease,+ &device,+ pos,+ pos,+ 1.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ 0.0,+ Qt::NoModifier,+ Qt::LeftButton,+ Qt::NoButton);+ m.buttonReleased(Qt::LeftButton, &event);+ }+ }+ QCOMPARE(a->m_begunIndexes, triggeredShortcuts);+ QCOMPARE(a->m_endedIndexes, triggeredShortcuts);}#include "../input/wintab/kis_incremental_average.h"diff --git a/libs/ui/tests/kis_input_manager_test.h b/libs/ui/tests/kis_input_manager_test.hindex efe6ec9e95..5d08fb54bf 100644--- a/libs/ui/tests/kis_input_manager_test.h+++ b/libs/ui/tests/kis_input_manager_test.h@@ -25,6 +25,9 @@ private Q_SLOTS:void testTouchHoldPostponer_data();void testTouchHoldPostponer();+ void testTouchOverriddenByTablet_data();+ void testTouchOverriddenByTablet();+void testIncrementalAverage();};