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++
Implement a basic unittest for touch processing
Dmitry Kazakova040350c8b4e
diff --git a/libs/ui/input/kis_shortcut_matcher.cpp b/libs/ui/input/kis_shortcut_matcher.cppindex 24c5590207..4dedaaedd5 100644--- a/libs/ui/input/kis_shortcut_matcher.cpp+++ b/libs/ui/input/kis_shortcut_matcher.cpp@@ -10,6 +10,7 @@#include <QMouseEvent>#include <QTabletEvent>+#include <kis_algebra_2d.h>#include "kis_assert.h"#include "kis_abstract_input_action.h"#include "kis_stroke_shortcut.h"@@ -87,7 +88,7 @@ public:int matchingIteration{0};bool isTouchDragDetected {false};bool isTouchHeld {false};- QScopedPointer<QEvent> bestCandidateTouchEvent;+ QScopedPointer<QEvent> bestCandidateForTapTouchEvent;std::function<KisInputActionGroupsMask()> actionGroupMask;bool suppressAllActions;@@ -406,7 +407,7 @@ bool KisShortcutMatcher::touchBeginEvent( QTouchEvent* event )#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))KoPointerEvent::copyQtPointerEvent(event, m_d->bestCandidateTouchEvent);#else- m_d->bestCandidateTouchEvent.reset(event->clone());+ m_d->bestCandidateForTapTouchEvent.reset(event->clone());#endifreturn !notifier.isInRecursion();@@ -424,15 +425,26 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)bool retval = false;- const int touchPointCount = event->touchPoints().size();- // check whether the touchpoints are relatively stationary or have been moved for dragging.- for (int i = 0; i < event->touchPoints().size() && !m_d->isTouchDragDetected; ++i) {- const QTouchEvent::TouchPoint &touchPoint = event->touchPoints().at(i);- const QPointF delta = touchPoint.pos() - touchPoint.startPos();- const qreal deltaSquared = delta.x() * delta.x() + delta.y() * delta.y();- // if the drag is detected, until the next TouchBegin even, we'll be assuming the gesture to be of dragging- // type.- m_d->isTouchDragDetected = deltaSquared > TOUCH_SLOP_SQUARED;+#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ auto points = event->touchPoints();+ using TouchPoint = QTouchEvent::TouchPoint;+#else+ auto points = event->points();+ using TouchPoint = QEventPoint;+#endif++ // Check whether the touchpoints are relatively stationary or have+ // been moved for dragging. If the drag is detected, until the next+ // TouchBegin event, we'll be assuming the gesture to be of+ // dragging type.+ if (!m_d->isTouchDragDetected) {+ for (auto it = points.begin(); it != points.end(); ++it) {+ const QPointF delta = it->pos() - it->startPos();+ if (KisAlgebra2D::normSquared(delta) > TOUCH_SLOP_SQUARED) {+ m_d->isTouchDragDetected = true;+ break;+ }+ }}// for a first few events we don't process the events right away. But analyze and keep track of the event with most@@ -443,45 +455,39 @@ bool KisShortcutMatcher::touchUpdateEvent(QTouchEvent *event)m_d->matchingIteration++;setMaxTouchPointEvent(event);DEBUG_TOUCH_ACTION("return best", event)- return matchTouchShortcut((QTouchEvent *)m_d->bestCandidateTouchEvent.data());+ return matchTouchShortcut((QTouchEvent *)m_d->bestCandidateForTapTouchEvent.data(), KisTouchShortcut::allTouchStates());}if (m_d->isTouchDragDetected) {- if (m_d->touchShortcut && !m_d->touchShortcut->matchDragType(event)) {+ const int numStillDraggedPoints =+ KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::pressedOnlyTouchStates());++ if (m_d->touchShortcut && !m_d->touchShortcut->matchDragType(event, KisTouchShortcut::pressedOnlyTouchStates())) {DEBUG_TOUCH_ACTION("ending", event)// we should end the event as an event with more touchpoints was receivedretval = tryEndTouchShortcut(event);- }- if (!hasRunningShortcut() && touchPointCount >= m_d->maxTouchPoints) {- m_d->maxTouchPoints = touchPointCount;+ } else if (!hasRunningShortcut() && numStillDraggedPoints >= m_d->maxTouchPoints) {+ m_d->maxTouchPoints = numStillDraggedPoints;DEBUG_TOUCH_ACTION("starting", event);- retval = tryRunTouchShortcut(event);+ retval = tryRunTouchShortcut(event, KisTouchShortcut::pressedOnlyTouchStates());} else if (m_d->touchShortcut) {- // The typical assumption when we get here is that the shortcut has been matched, for which we use- // the events with TouchPointPressed state. But there may be instances where shortcut is never- // un-matched (meaning: never being tryEndTouchShortcut called on it) even when the finger is- // released, and when the next contact is made, the shortcut proceeds assuming continuity -- which- // is a false assumption.- // So, if we see a TouchPointPressed, we should know that somewhere previously finger was lifted- // and we should let the action know this.- if (event->touchPointStates() & Qt::TouchPointPressed) {- m_d->touchShortcut->action()->begin(m_d->touchShortcut->shortcutIndex(), event);- } else if (event->touchPointStates() & Qt::TouchPointReleased) {- m_d->touchShortcut->action()->end(event);- } else {- m_d->touchShortcut->action()->inputEvent(event);- }+ m_d->touchShortcut->action()->inputEvent(event);retval = true;}} else {// triggered if a new finger was added, which might result in shortcut not matching the action- if ((event->touchPointStates() & Qt::TouchPointReleased) == Qt::TouchPointReleased && !hasRunningShortcut()) {+ if (event->touchPointStates().testFlag(TouchPoint::Released) && !hasRunningShortcut()) {+ const int previousNumPoints =+ KisTouchShortcut::countTouchPoints(event, KisTouchShortcut::allTouchStates());+// we should end the event as an event with more touchpoints was received- if (m_d->maxTouchPoints <= touchPointCount) {- m_d->maxTouchPoints = touchPointCount;- DEBUG_TOUCH_ACTION("firing", event);- fireReadyTouchShortcut(event);- m_d->bestCandidateTouchEvent.reset();+ if (previousNumPoints >= m_d->maxTouchPoints) {+ m_d->maxTouchPoints = previousNumPoints;+ DEBUG_TOUCH_ACTION("firing tap on release", event);+ if (tryFireTapTouchShortcut(event, KisTouchShortcut::allTouchStates())) {+ m_d->bestCandidateForTapTouchEvent.reset();+ retval = true;+ }}}}@@ -496,13 +502,17 @@ bool KisShortcutMatcher::touchEndEvent(QTouchEvent *event)m_d->maxTouchPoints = 0;m_d->isTouchHeld = false;- if (!m_d->isTouchDragDetected && m_d->bestCandidateTouchEvent && !hasRunningShortcut()) {- fireReadyTouchShortcut(static_cast<QTouchEvent *>(m_d->bestCandidateTouchEvent.data()));+ bool retval = false;++ if (!m_d->isTouchDragDetected && m_d->bestCandidateForTapTouchEvent && !hasRunningShortcut()) {+ DEBUG_TOUCH_ACTION("firing tap on end", event);+ retval = tryFireTapTouchShortcut(static_cast<QTouchEvent *>(m_d->bestCandidateForTapTouchEvent.data()),+ KisTouchShortcut::allTouchStates());}DEBUG_TOUCH_ACTION("ending", event)// we should try and end the shortcut too (it might be that there is none? (sketch))- const bool retval = tryEndTouchShortcut(event);+ retval |= tryEndTouchShortcut(event);if (notifier.isInRecursion()) {forceDeactivateAllActions();@@ -567,7 +577,7 @@ bool KisShortcutMatcher::touchHoldBeginEvent(QTouchEvent *event)}m_d->isTouchHeld = true; // Must be set first, used in tryRunTouchShortcut.- if (tryRunTouchShortcut(event)) {+ if (tryRunTouchShortcut(event, KisTouchShortcut::pressedOnlyTouchStates())) {DEBUG_ACTION("touch shortcut found");return true;} else {@@ -1027,15 +1037,15 @@ void KisShortcutMatcher::setMaxTouchPointEvent(QTouchEvent *event)#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))KoPointerEvent::copyQtPointerEvent(event, m_d->bestCandidateTouchEvent);#else- m_d->bestCandidateTouchEvent.reset(event->clone());+ m_d->bestCandidateForTapTouchEvent.reset(event->clone());#endif}}-void KisShortcutMatcher::fireReadyTouchShortcut(QTouchEvent *event)+bool KisShortcutMatcher::tryFireTapTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates){- KisTouchShortcut *goodCandidate = matchTouchShortcut(event);+ KisTouchShortcut *goodCandidate = matchTouchShortcut(event, allowedStates);if (goodCandidate) {DEBUG_TOUCH_ACTION("starting", event)goodCandidate->action()->activate(goodCandidate->shortcutIndex());@@ -1044,14 +1054,16 @@ void KisShortcutMatcher::fireReadyTouchShortcut(QTouchEvent *event)goodCandidate->action()->end(event);goodCandidate->action()->deactivate(goodCandidate->shortcutIndex());}++ return goodCandidate;}-KisTouchShortcut *KisShortcutMatcher::matchTouchShortcut(QTouchEvent *event)+KisTouchShortcut *KisShortcutMatcher::matchTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates){KisTouchShortcut *goodCandidate = nullptr;Q_FOREACH (KisTouchShortcut *shortcut, m_d->touchShortcuts) {if (shortcut->isAvailable(m_d->actionGroupMask())- && matchTouchShortcutBasedOnState(event, shortcut)+ && matchTouchShortcutBasedOnState(event, shortcut, allowedStates)&& (!goodCandidate || shortcut->priority() > goodCandidate->priority())) {goodCandidate = shortcut;@@ -1060,20 +1072,20 @@ KisTouchShortcut *KisShortcutMatcher::matchTouchShortcut(QTouchEvent *event)return goodCandidate;}-bool KisShortcutMatcher::matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut)+bool KisShortcutMatcher::matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut, Qt::TouchPointStates allowedStates){if (m_d->isTouchHeld) {- return shortcut->matchHoldType(event);+ return shortcut->matchHoldType(event, allowedStates);} else if (m_d->isTouchDragDetected) {- return shortcut->matchDragType(event);+ return shortcut->matchDragType(event, allowedStates);} else {- return shortcut->matchTapType(event);+ return shortcut->matchTapType(event, allowedStates);}}-bool KisShortcutMatcher::tryRunTouchShortcut( QTouchEvent* event )+bool KisShortcutMatcher::tryRunTouchShortcut(QTouchEvent* event, Qt::TouchPointStates allowedStates){- KisTouchShortcut *goodCandidate = matchTouchShortcut(event);+ KisTouchShortcut *goodCandidate = matchTouchShortcut(event, allowedStates);if (m_d->actionsSuppressed())return false;diff --git a/libs/ui/input/kis_shortcut_matcher.h b/libs/ui/input/kis_shortcut_matcher.hindex a6a7f4c95c..1fa10182db 100644--- a/libs/ui/input/kis_shortcut_matcher.h+++ b/libs/ui/input/kis_shortcut_matcher.h@@ -288,6 +288,7 @@ public:void setInputActionGroupsMaskCallback(std::function<KisInputActionGroupsMask()> func);private:+friend class KisInputManagerTest;void reset();@@ -305,10 +306,10 @@ private:void forceDeactivateAllActions();void setMaxTouchPointEvent(QTouchEvent *event);- void fireReadyTouchShortcut(QTouchEvent *event);- KisTouchShortcut *matchTouchShortcut(QTouchEvent *event);- bool matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut);- bool tryRunTouchShortcut(QTouchEvent *event);+ bool tryFireTapTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates);+ KisTouchShortcut *matchTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates);+ bool matchTouchShortcutBasedOnState(QTouchEvent *event, KisTouchShortcut *shortcut, Qt::TouchPointStates allowedStates);+ bool tryRunTouchShortcut(QTouchEvent *event, Qt::TouchPointStates allowedStates);bool tryEndTouchShortcut(QTouchEvent *event);bool tryRunNativeGestureShortcut(QNativeGestureEvent *event);diff --git a/libs/ui/input/kis_touch_shortcut.cpp b/libs/ui/input/kis_touch_shortcut.cppindex b2879b4538..2cfffa7476 100644--- a/libs/ui/input/kis_touch_shortcut.cpp+++ b/libs/ui/input/kis_touch_shortcut.cpp@@ -69,31 +69,52 @@ void KisTouchShortcut::setDisableOnTouchPainting(bool disableOnTouchPainting)d->disableOnTouchPainting = disableOnTouchPainting;}-bool KisTouchShortcut::matchTapType(QTouchEvent *event)+bool KisTouchShortcut::matchTapType(QTouchEvent *event, Qt::TouchPointStates allowedStates){- return matchTouchPoint(event)+ return matchTouchPoint(event, allowedStates)#ifndef Q_OS_MACOS&& (d->type >= KisShortcutConfiguration::OneFingerTap && d->type <= KisShortcutConfiguration::FiveFingerTap)#endif;}-bool KisTouchShortcut::matchDragType(QTouchEvent *event)+bool KisTouchShortcut::matchDragType(QTouchEvent *event, Qt::TouchPointStates allowedStates){- return matchTouchPoint(event)+ return matchTouchPoint(event, allowedStates)#ifndef Q_OS_MACOS&& (d->type >= KisShortcutConfiguration::OneFingerDrag && d->type <= KisShortcutConfiguration::FiveFingerDrag)#endif;}-bool KisTouchShortcut::matchHoldType(QTouchEvent *event)+bool KisTouchShortcut::matchHoldType(QTouchEvent *event, Qt::TouchPointStates allowedStates){- return isHoldType() && matchTouchPoint(event);+ return isHoldType() && matchTouchPoint(event, allowedStates);}-bool KisTouchShortcut::matchTouchPoint(QTouchEvent *event)+int KisTouchShortcut::countTouchPoints(QTouchEvent *event, Qt::TouchPointStates allowedStates){+ #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)+ auto points = event->touchPoints();+ using TouchPoint = QTouchEvent::TouchPoint;+#else+ auto points = event->points();+ using TouchPoint = QEventPoint;+#endif++ const int count =+ std::count_if(points.begin(), points.end(), [=] (const TouchPoint &point) {+ auto state = static_cast<Qt::TouchPointState>(point.state());+ return allowedStates.testFlag(state);+ });++ return count;+}++bool KisTouchShortcut::matchTouchPoint(QTouchEvent *event, Qt::TouchPointStates allowedStates)+{+ const int numStillActivePoints = countTouchPoints(event, allowedStates);+return (!d->disableOnTouchPainting || KisConfig(true).disableTouchOnCanvas())- && event->touchPoints().count() >= d->minTouchPoints && event->touchPoints().count() <= d->maxTouchPoints;+ && numStillActivePoints >= d->minTouchPoints && numStillActivePoints <= d->maxTouchPoints;}diff --git a/libs/ui/input/kis_touch_shortcut.h b/libs/ui/input/kis_touch_shortcut.hindex d31c94a6c3..846ea15c1e 100644--- a/libs/ui/input/kis_touch_shortcut.h+++ b/libs/ui/input/kis_touch_shortcut.h@@ -18,7 +18,7 @@ class QTouchEvent;* it _does not_ handle tool invocation i.e painting (which is being* handled in KisShortcutMatcher).*/-class KisTouchShortcut : public KisAbstractShortcut+class KRITAUI_EXPORT KisTouchShortcut : public KisAbstractShortcut{using GestureAction = KisShortcutConfiguration::GestureAction;@@ -33,10 +33,20 @@ class KisTouchShortcut : public KisAbstractShortcutvoid setMaximumTouchPoints( int max );void setDisableOnTouchPainting(bool disableOnTouchPainting);- bool matchTapType(QTouchEvent *event);- bool matchDragType(QTouchEvent *event);- bool matchHoldType(QTouchEvent *event);- bool matchTouchPoint(QTouchEvent *event);+ bool matchTapType(QTouchEvent *event, Qt::TouchPointStates allowedStates);+ bool matchDragType(QTouchEvent *event, Qt::TouchPointStates allowedStates);+ bool matchHoldType(QTouchEvent *event, Qt::TouchPointStates allowedStates);+ bool matchTouchPoint(QTouchEvent *event, Qt::TouchPointStates allowedStates);++ static inline Qt::TouchPointStates allTouchStates() {+ return Qt::TouchPointStationary | Qt::TouchPointPressed | Qt::TouchPointMoved | Qt::TouchPointReleased;+ }++ static inline Qt::TouchPointStates pressedOnlyTouchStates() {+ return Qt::TouchPointStationary | Qt::TouchPointPressed | Qt::TouchPointMoved;+ }++ static int countTouchPoints(QTouchEvent *event, Qt::TouchPointStates allowedStates);private:class Private;diff --git a/libs/ui/tests/CMakeLists.txt b/libs/ui/tests/CMakeLists.txtindex e2277eb1f0..5bdc6bc284 100644--- a/libs/ui/tests/CMakeLists.txt+++ b/libs/ui/tests/CMakeLists.txt@@ -82,6 +82,12 @@ kis_add_test( kis_node_juggler_compressed_test.cpp ../../../sdk/tests/testutil.LINK_LIBRARIES kritaui kritatestsdk)+set_source_files_properties(+ kis_input_manager_test.cpp+ PROPERTIES+ INCLUDE_DIRECTORIES "${Qt${QT_MAJOR_VERSION}Gui_PRIVATE_INCLUDE_DIRS}"+ )+kis_add_test(kis_input_manager_test.cpp ../../../sdk/tests/testutil.cppTEST_NAME KisInputManagerTestdiff --git a/libs/ui/tests/kis_input_manager_test.cpp b/libs/ui/tests/kis_input_manager_test.cppindex 036e2a06f2..84a01a2e75 100644--- a/libs/ui/tests/kis_input_manager_test.cpp+++ b/libs/ui/tests/kis_input_manager_test.cpp@@ -8,13 +8,18 @@#include <simpletest.h>+// for QMutableEventPoint+#include <QtGui/private/qeventpoint_p.h>#include <QMouseEvent>#include "input/kis_single_action_shortcut.h"#include "input/kis_stroke_shortcut.h"+#include "input/kis_touch_shortcut.h"#include "input/kis_abstract_input_action.h"#include "input/kis_shortcut_matcher.h"+#include <kis_debug.h>+#include <kis_config.h>void KisInputManagerTest::testSingleActionShortcut(){@@ -66,7 +71,13 @@ struct TestingAction : public KisAbstractInputActionRunning};- TestingAction() : KisAbstractInputAction("TestingAction"), m_isHighResolution(false) { reset(); }+ TestingAction()+ : KisAbstractInputAction("TestingAction")+ , m_isHighResolution(false)+ {+ setName("testing touch action");+ reset();+ }~TestingAction() {}void activate(int shortcut) override@@ -94,11 +105,14 @@ struct TestingAction : public KisAbstractInputActionm_stateBeforeRunning = m_state;m_state = Running;- m_beginIndex = shortcut; m_beginNonNull = event;+ m_beginIndex = shortcut;+ m_begunIndexes.append(shortcut);+ m_beginNonNull = event;}void end(QEvent *event) override {KIS_ASSERT(m_state == Running);m_state = m_stateBeforeRunning;+ m_endedIndexes.append(m_beginIndex);m_ended = true; m_endNonNull = event;}@@ -110,6 +124,7 @@ struct TestingAction : public KisAbstractInputActionvoid reset() {m_beginIndex = -1;+ m_begunIndexes.clear();m_ended = false;m_gotInput = false;m_beginNonNull = false;@@ -134,6 +149,8 @@ struct TestingAction : public KisAbstractInputActionbool m_isHighResolution;State m_state {Deactivated};State m_stateBeforeRunning {Deactivated};+ QList<int> m_begunIndexes;+ QList<int> m_endedIndexes;};@@ -157,6 +174,53 @@ KisStrokeShortcut* createStrokeShortcut(KisAbstractInputAction *action,return s;}+KisTouchShortcut* createTouchShortcut(KisAbstractInputAction *action,+ int shortcutIndex,+ KisShortcutConfiguration::GestureAction gesture)+{+ KisTouchShortcut *s = new KisTouchShortcut(action, shortcutIndex, gesture);++ // TODO: move the initialization to the shortcut itself+ switch(gesture) {+#ifndef Q_OS_MACOS+ case KisShortcutConfiguration::OneFingerTap:+ case KisShortcutConfiguration::OneFingerDrag:+ // Touch painting takes precedence over one-finger touch shortcuts, so+ // disable this type of shortcut when touch painting is active. Except+ // touch hold shortcuts, since touching and holding in one spot does+ // nothing otherwise and is therefore unambiguous.+ s->setDisableOnTouchPainting(true);+ Q_FALLTHROUGH();+ case KisShortcutConfiguration::OneFingerHold:+ s->setMinimumTouchPoints(1);+ s->setMaximumTouchPoints(1);+ break;+ case KisShortcutConfiguration::TwoFingerTap:+ case KisShortcutConfiguration::TwoFingerDrag:+ s->setMinimumTouchPoints(2);+ s->setMaximumTouchPoints(2);+ break;+ case KisShortcutConfiguration::ThreeFingerTap:+ case KisShortcutConfiguration::ThreeFingerDrag:+ s->setMinimumTouchPoints(3);+ s->setMaximumTouchPoints(3);+ break;+ case KisShortcutConfiguration::FourFingerTap:+ case KisShortcutConfiguration::FourFingerDrag:+ s->setMinimumTouchPoints(4);+ s->setMaximumTouchPoints(4);+ break;+ case KisShortcutConfiguration::FiveFingerTap:+ case KisShortcutConfiguration::FiveFingerDrag:+ s->setMinimumTouchPoints(5);+ s->setMaximumTouchPoints(5);+#endif+ default:+ break;+ }+ return s;+}+void KisInputManagerTest::testKeyEvents(){KisShortcutMatcher m;@@ -409,6 +473,366 @@ void KisInputManagerTest::testMouseMoves()m.leaveEvent();}+/**********************************************************************/+/* A framework for testing touch processing state automata */+/**********************************************************************/++struct TouchSequenceGeneratorBase+{+ TouchSequenceGeneratorBase(int fingerCount, int maxEventIndex, const QPointF pointOffset)+ : m_fingerCount(fingerCount)+ , m_maxEventIndex(maxEventIndex)+ , m_pointOffset(pointOffset)+ {+ }++ virtual ~TouchSequenceGeneratorBase() {}++ void updatePoints(int eventIndex, QList<QEventPoint> &touchPoints)+ {+ /**+ * First, remove all the points marked as "Release" at the previous step+ */+ for (auto it = touchPoints.begin(); it != touchPoints.end();) {+ if (it->state() == QEventPoint::Released) {+ it = touchPoints.erase(it);+ } else {+ ++it;+ }+ }++ updatePointsSet(eventIndex, touchPoints);++ for (auto it = touchPoints.begin(); it != touchPoints.end(); ++it) {+ /**+ * Now update all points positions+ */+ QMutableEventPoint::setGlobalLastPosition(*it, it->globalPosition());+ QMutableEventPoint::setGlobalPosition(*it, globalPositionForPoint(it->id(), eventIndex));+ QMutableEventPoint::setPosition(*it, it->globalPosition());++ if (it->state() == QEventPoint::Pressed && it->globalLastPosition() != it->globalPosition()) {+ /**+ * If a "Pressed" point has been offset, then we should change+ * its state to "Updated"+ */+ QMutableEventPoint::setState(*it, QEventPoint::Updated);+ } else if (eventIndex == m_maxEventIndex) {+ /**+ * At the very last iteration, mark all the points as+ * "Released".+ */+ QMutableEventPoint::setState(*it, QEventPoint::Released);+ }++ // ENTER_FUNCTION() << ppVar(eventIndex) << ppVar(it->id()) << ppVar(it->state()) <<+ // ppVar(it->globalPosition()) << ppVar(it->globalLastPosition());+ }+ }++protected:+ QPointF globalPositionForPoint(int id, int eventIndex)+ {+ return QPointF(10 * id + eventIndex * m_pointOffset.x(), eventIndex * m_pointOffset.y());+ };++ void appendNewPoint(QList<QEventPoint> &touchPoints, int eventIndex)+ {+ const int newIndex = touchPoints.size();+ const QPointF pos = globalPositionForPoint(10 * newIndex, eventIndex);+ touchPoints.emplace_back(10 * newIndex, QEventPoint::Pressed, pos, pos);+ QMutableEventPoint::setGlobalPressPosition(touchPoints.back(), pos);+ };++ /**+ * Modify the points set in \p touchPoints accorting to \p eventIndex+ *+ * 1) To add a point call `appendNewPoint(touchPoints, eventIndex)`, the state+ * will automatically initialized correctly.+ *+ * 2) To remove a point just change its state to "Released" and+ * it will be removed at the next iteration automatically.+ */+ virtual void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) = 0;++protected:+ int m_fingerCount = 0;+ int m_maxEventIndex = 0;+ const QPointF m_pointOffset;+};++struct TouchCleanDrag : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ if (eventIndex == 0) {+ for (int i = 0; i < m_fingerCount; i++) {+ appendNewPoint(touchPoints, eventIndex);+ }+ }+ }+};++struct TouchDragDirtyStart : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ KIS_ASSERT(m_fingerCount > 1);+ KIS_ASSERT(m_maxEventIndex > 6);++ if (eventIndex == 0) {+ appendNewPoint(touchPoints, eventIndex);+ }++ if (eventIndex == 1) {+ // noop+ }++ if (eventIndex == 2) {+ appendNewPoint(touchPoints, eventIndex);+ }++ if (eventIndex == 3) {+ // noop+ }++ if (eventIndex == 4 && m_fingerCount > 2) {+ appendNewPoint(touchPoints, eventIndex);+ }+ }+};++struct TouchDragDirtyEnd : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ if (eventIndex == 0) {+ for (int i = 0; i < m_fingerCount; ++i) {+ appendNewPoint(touchPoints, eventIndex);+ }+ }++ for (int i = 0; i < m_fingerCount; i++) {+ if (eventIndex == m_maxEventIndex - i * 2) {+ QMutableEventPoint::setState(touchPoints.back(), QEventPoint::Released);+ }+ }+ }+};++struct TouchDragTwoThenThree : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ KIS_ASSERT(m_fingerCount == 2);+ KIS_ASSERT(m_maxEventIndex > 10);++ if (eventIndex == 0) {+ appendNewPoint(touchPoints, eventIndex);+ appendNewPoint(touchPoints, eventIndex);+ }++ if (eventIndex == m_maxEventIndex / 2) {+ appendNewPoint(touchPoints, eventIndex);+ }+ }+};++struct TouchDragThreeThenTwo : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ KIS_ASSERT(m_fingerCount == 3);+ KIS_ASSERT(m_maxEventIndex > 10);++ if (eventIndex == 0) {+ appendNewPoint(touchPoints, eventIndex);+ appendNewPoint(touchPoints, eventIndex);+ appendNewPoint(touchPoints, eventIndex);+ }++ if (eventIndex == m_maxEventIndex / 2) {+ QMutableEventPoint::setState(touchPoints.back(), QEventPoint::Released);+ }+ }+};++struct TouchDragThreeThenTwoThenThree : TouchSequenceGeneratorBase+{+ using TouchSequenceGeneratorBase::TouchSequenceGeneratorBase;++protected:+ void updatePointsSet(int eventIndex, QList<QEventPoint> &touchPoints) override+ {+ KIS_ASSERT(m_fingerCount == 3);+ KIS_ASSERT(m_maxEventIndex > 10);++ if (eventIndex == 0) {+ appendNewPoint(touchPoints, eventIndex);+ appendNewPoint(touchPoints, eventIndex);+ appendNewPoint(touchPoints, eventIndex);+ }++ if (eventIndex == m_maxEventIndex / 3) {+ QMutableEventPoint::setState(touchPoints.back(), QEventPoint::Released);+ }++ if (eventIndex == 2 * m_maxEventIndex / 3) {+ appendNewPoint(touchPoints, eventIndex);+ }+ }+};++TouchSequenceGeneratorBase* createTouchSequenceGenerator(const QString sequenceName, int fingerCount, int maxEventIndex, const QPointF pointOffset)+{+ if (sequenceName == "touchCleanDrag") {+ return new TouchCleanDrag(fingerCount, maxEventIndex, pointOffset);+ } else if (sequenceName == "touchDragDirtyStart") {+ return new TouchDragDirtyStart(fingerCount, maxEventIndex, pointOffset);+ } else if (sequenceName == "touchDragDirtyEnd") {+ return new TouchDragDirtyEnd(fingerCount, maxEventIndex, pointOffset);+ } else if (sequenceName == "touchDragTwoThenThree") {+ return new TouchDragTwoThenThree(fingerCount, maxEventIndex, pointOffset);+ } else if (sequenceName == "touchDragThreeThenTwo") {+ return new TouchDragThreeThenTwo(fingerCount, maxEventIndex, pointOffset);+ } else if (sequenceName == "touchDragThreeThenTwoThenThree") {+ return new TouchDragThreeThenTwoThenThree(fingerCount, maxEventIndex, pointOffset);+ } else {+ qFatal("Unknown touch sequence name");+ }++ Q_UNREACHABLE_RETURN(nullptr);+}++void KisInputManagerTest::testTouchMoves_data()+{+ QTest::addColumn<QString>("sequenceName");+ QTest::addColumn<int>("fingerCount");+ QTest::addColumn<int>("eventsCount");+ QTest::addColumn<QPointF>("pointOffset");+ QTest::addColumn<QList<int>>("triggeredShortcuts");++ const QPointF dragOffset(10,10);+ const QPointF dirtyDragOffset(1,1);+ const QPointF tapOffset(0.5,0.5);++ QTest::addRow("clean-1p-drag") << "touchCleanDrag" << 1 << 100 << dragOffset << QList<int>{15};+ QTest::addRow("clean-2p-drag") << "touchCleanDrag" << 2 << 100 << dragOffset << QList<int>{16};+ QTest::addRow("clean-3p-drag") << "touchCleanDrag" << 3 << 100 << dragOffset << QList<int>{17};++ QTest::addRow("clean-1p-tap") << "touchCleanDrag" << 1 << 10 << tapOffset << QList<int>{20};+ QTest::addRow("clean-2p-tap") << "touchCleanDrag" << 2 << 10 << tapOffset << QList<int>{21};+ QTest::addRow("clean-3p-tap") << "touchCleanDrag" << 3 << 10 << tapOffset << QList<int>{22};++ // 16px is the threshold for distinguishing taps and drags (touch-begin + 16 steps + touch-end)+ QTest::addRow("clean-1p-tap-upper-bound") << "touchCleanDrag" << 1 << 18 << QPointF(1, 0) << QList<int>{20};+ QTest::addRow("clean-1p-drag-lower-bound") << "touchCleanDrag" << 1 << 19 << QPointF(1, 0) << QList<int>{15};++ QTest::addRow("dirty-start-2p-drag") << "touchDragDirtyStart" << 2 << 100 << dirtyDragOffset << QList<int>{16};+ QTest::addRow("dirty-start-3p-drag") << "touchDragDirtyStart" << 3 << 100 << dirtyDragOffset << QList<int>{17};++ QTest::addRow("dirty-end-2p-drag") << "touchDragDirtyEnd" << 2 << 100 << dirtyDragOffset << QList<int>{16};+ QTest::addRow("dirty-end-3p-drag") << "touchDragDirtyEnd" << 3 << 100 << dirtyDragOffset << QList<int>{17};++ QTest::addRow("dirty-end-2p-tap") << "touchDragDirtyEnd" << 2 << 10 << tapOffset << QList<int>{21};+ QTest::addRow("dirty-end-3p-tap") << "touchDragDirtyEnd" << 3 << 10 << tapOffset << QList<int>{22};++ // the current implementation is expected to switch to a "more-fingers-rich" shortcut immetiately+ QTest::addRow("clean-2p-then-3p-drag") << "touchDragTwoThenThree" << 2 << 100 << dragOffset << QList<int>{16, 17};++ // the current implementation is expected to stay on the "finger-richest" shortcut and,+ // not to switch to a "finger-poorer" shortcut+ QTest::addRow("clean-3p-then-2p-drag") << "touchDragThreeThenTwo" << 3 << 100 << dragOffset << QList<int>{17};++ // the 3-point gesture should be run twice during the same run+ QTest::addRow("clean-3p-then-2p-then-3p-drag") << "touchDragThreeThenTwoThenThree" << 3 << 100 << dragOffset << QList<int>{17, 17};+}++void KisInputManagerTest::testTouchMoves()+{+ KisShortcutMatcher m;+ m.enterEvent();++ TestingAction *a = new TestingAction();++ m.addShortcut(+ createKeyShortcut(a, 10,+ QSet<Qt::Key>() << Qt::Key_Shift,+ Qt::Key_Enter));++ m.addShortcut(+ createStrokeShortcut(a, 13,+ QSet<Qt::Key>(),+ Qt::LeftButton));++ // TODO: add a separate test for that!+ // let single finger gestures to work+ KisConfig(false).setTouchPainting(KisConfig::TOUCH_PAINTING_DISABLED);++ m.addShortcut(+ createTouchShortcut(a, 15, KisShortcutConfiguration::OneFingerDrag));+ m.addShortcut(+ createTouchShortcut(a, 16, KisShortcutConfiguration::TwoFingerDrag));+ m.addShortcut(+ createTouchShortcut(a, 17, KisShortcutConfiguration::ThreeFingerDrag));++ m.addShortcut(+ createTouchShortcut(a, 20, KisShortcutConfiguration::OneFingerTap));+ m.addShortcut(+ createTouchShortcut(a, 21, KisShortcutConfiguration::TwoFingerTap));+ m.addShortcut(+ createTouchShortcut(a, 22, KisShortcutConfiguration::ThreeFingerTap));+++ QFETCH(QString, sequenceName);+ QFETCH(int, fingerCount);+ QFETCH(int, eventsCount);+ QFETCH(QPointF, pointOffset);+ QFETCH(QList<int>, triggeredShortcuts);++ std::unique_ptr<TouchSequenceGeneratorBase> strokeGenerator(+ createTouchSequenceGenerator(sequenceName, fingerCount, eventsCount - 1, pointOffset));++ QPointingDevice device;+ QList<QEventPoint> touchPoints;++ for (int eventIndex = 0; eventIndex < eventsCount; eventIndex++) {+ strokeGenerator->updatePoints(eventIndex, touchPoints);+ if (eventIndex == 0) {+ QTouchEvent e(QEvent::TouchBegin, &device, Qt::NoModifier, touchPoints);+ m.touchBeginEvent(&e);+ } else if (eventIndex > 0 && eventIndex < eventsCount - 1) {+ QTouchEvent e(QEvent::TouchUpdate, &device, Qt::NoModifier, touchPoints);+ m.touchUpdateEvent(&e);+ } else if (eventIndex == eventsCount - 1) {+ QTouchEvent e(QEvent::TouchEnd, &device, Qt::NoModifier, touchPoints);+ m.touchEndEvent(&e);+ } else {+ qFatal("invalid step value");+ }+ }++ QCOMPARE(a->m_begunIndexes, triggeredShortcuts);+ QCOMPARE(a->m_endedIndexes, triggeredShortcuts);+ QCOMPARE(a->m_ended, true);+ //QCOMPARE(a->m_gotInput, true); // TODO!+}+#include "../input/wintab/kis_incremental_average.h"void KisInputManagerTest::testIncrementalAverage()diff --git a/libs/ui/tests/kis_input_manager_test.h b/libs/ui/tests/kis_input_manager_test.hindex 628d79f458..8267cb78bc 100644--- a/libs/ui/tests/kis_input_manager_test.h+++ b/libs/ui/tests/kis_input_manager_test.h@@ -19,6 +19,9 @@ private Q_SLOTS:void testReleaseUnnecessaryModifiers();void testMouseMoves();+ void testTouchMoves_data();+ void testTouchMoves();+void testIncrementalAverage();};