| 1 | /* |
| 2 | * Copyright (C) 2015 Canonical Ltd. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 3. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifndef UCSWIPEAREA_P_P_H |
| 18 | #define UCSWIPEAREA_P_P_H |
| 19 | |
| 20 | #include <QtGlobal> |
| 21 | |
| 22 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 23 | #include <LomiriGestures-Qt6/private/ucswipearea_p.h> |
| 24 | #else |
| 25 | #include <LomiriGestures/private/ucswipearea_p.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <QtQuick/private/qquickitem_p.h> |
| 29 | |
| 30 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 31 | #include <LomiriGestures-Qt6/private/damper_p.h> |
| 32 | #else |
| 33 | #include <LomiriGestures/private/damper_p.h> |
| 34 | #endif |
| 35 | |
| 36 | UG_NAMESPACE_BEGIN |
| 37 | |
| 38 | // Information about an active touch point |
| 39 | struct LOMIRIGESTURES_EXPORT ActiveTouchInfo { |
| 40 | ActiveTouchInfo() : id(-1), startTime(-1) {} |
| 41 | bool isValid() const { return id != -1; } |
| 42 | void reset() { id = -1; } |
| 43 | int id; |
| 44 | qint64 startTime; |
| 45 | }; |
| 46 | class LOMIRIGESTURES_EXPORT ActiveTouchesInfo { |
| 47 | public: |
| 48 | ActiveTouchesInfo(const UG_PREPEND_NAMESPACE(SharedTimeSource) &timeSource); |
| 49 | void update(QTouchEvent *event); |
| 50 | qint64 touchStartTime(int id); |
| 51 | bool isEmpty() const { return m_touchInfoPool.isEmpty(); } |
| 52 | qint64 mostRecentStartTime(); |
| 53 | UG_PREPEND_NAMESPACE(SharedTimeSource) m_timeSource; |
| 54 | private: |
| 55 | void addTouchPoint(int touchId); |
| 56 | void removeTouchPoint(int touchId); |
| 57 | QString toString(); |
| 58 | |
| 59 | Pool<ActiveTouchInfo> m_touchInfoPool; |
| 60 | }; |
| 61 | |
| 62 | class UCSwipeAreaStatusListener; |
| 63 | class LOMIRIGESTURES_EXPORT UCSwipeAreaPrivate : public QQuickItemPrivate |
| 64 | { |
| 65 | Q_DECLARE_PUBLIC(UCSwipeArea) |
| 66 | |
| 67 | Q_ENUMS(Status) |
| 68 | public: |
| 69 | UCSwipeAreaPrivate(); |
| 70 | void init(); |
| 71 | static UCSwipeAreaPrivate *get(UCSwipeArea *area) |
| 72 | { |
| 73 | return area->d_func(); |
| 74 | } |
| 75 | |
| 76 | void setMaxTime(int value); |
| 77 | |
| 78 | void setCompositionTime(int value); |
| 79 | |
| 80 | // Replaces the existing Timer with the given one. |
| 81 | // |
| 82 | // Useful for providing a fake timer when testing. |
| 83 | void setRecognitionTimer(UG_PREPEND_NAMESPACE(AbstractTimer) *timer); |
| 84 | |
| 85 | // Useful for testing, where a fake time source can be supplied |
| 86 | void setTimeSource(const UG_PREPEND_NAMESPACE(SharedTimeSource) &timeSource); |
| 87 | |
| 88 | // Describes the state of the directional drag gesture. |
| 89 | enum Status { |
| 90 | // Waiting for a new touch point to land on this area. No gesture is being processed |
| 91 | // or tracked. |
| 92 | WaitingForTouch, |
| 93 | |
| 94 | // A touch point has landed on this area but it's not know yet whether it is |
| 95 | // performing a drag in the correct direction. |
| 96 | // If it's decided that the touch point is not performing a directional drag gesture, |
| 97 | // it will be rejected/ignored and status will return to WaitingForTouch. |
| 98 | Undecided, //Recognizing, |
| 99 | |
| 100 | // There's a touch point in this area and it performed a drag in the correct |
| 101 | // direction. |
| 102 | // |
| 103 | // Once recognized, the gesture state will move back to WaitingForTouch only once |
| 104 | // that touch point ends. The gesture will remain in the Recognized state even if |
| 105 | // the touch point starts moving in other directions or halts. |
| 106 | Recognized, |
| 107 | }; |
| 108 | |
| 109 | void touchEvent_absent(QTouchEvent *event); |
| 110 | void touchEvent_undecided(QTouchEvent *event); |
| 111 | void touchEvent_recognized(QTouchEvent *event); |
| 112 | bool movingInRightDirection() const; |
| 113 | bool movedFarEnoughAlongGestureAxis() const; |
| 114 | bool isPastMaxDistance() const; |
| 115 | const QTouchEvent::TouchPoint *fetchTargetTouchPoint(QTouchEvent *event); |
| 116 | void setStatus(Status newStatus); |
| 117 | void updatePosition(const QPointF &point); |
| 118 | void setPublicScenePos(const QPointF &point); |
| 119 | bool isWithinTouchCompositionWindow(); |
| 120 | void updateSceneDirectionVector(); |
| 121 | // returns the scalar projection between the given vector (in scene coordinates) |
| 122 | // and m_sceneDirectionVector |
| 123 | qreal projectOntoDirectionVector(const QPointF &sceneVector) const; |
| 124 | void touchOwnershipEvent(TouchOwnershipEvent *event); |
| 125 | void unownedTouchEvent(UnownedTouchEvent *event); |
| 126 | void unownedTouchEvent_undecided(UnownedTouchEvent *unownedTouchEvent); |
| 127 | void watchPressedTouchPoints(const QList<QTouchEvent::TouchPoint> &touchPoints); |
| 128 | bool recognitionIsDisabled() const; |
| 129 | bool sanityCheckRecognitionProperties(); |
| 130 | void setDistanceThreshold(qreal value); |
| 131 | void setPixelsPerMm(qreal pixelsPerMm); |
| 132 | QString objectName() const { return q_func()->objectName(); } |
| 133 | |
| 134 | // manage status change listeners |
| 135 | void addStatusChangeListener(UCSwipeAreaStatusListener *listener); |
| 136 | void removeStatusChangeListener(UCSwipeAreaStatusListener *listener); |
| 137 | |
| 138 | QPointF startScenePos; |
| 139 | // The touch position exposed in the public API. |
| 140 | // It only starts to move once the gesture gets recognized. |
| 141 | QPointF publicScenePos; |
| 142 | // A movement damper is used in some of the gesture recognition calculations |
| 143 | // to get rid of noise or small oscillations in the touch position. |
| 144 | DampedPointF dampedScenePos; |
| 145 | QPointF previousDampedScenePos; |
| 146 | // Unit vector in scene coordinates describing the direction of the gesture recognition |
| 147 | QPointF sceneDirectionVector; |
| 148 | UG_PREPEND_NAMESPACE(SharedTimeSource) timeSource; |
| 149 | ActiveTouchesInfo activeTouches; |
| 150 | |
| 151 | // status change listeners |
| 152 | QList<UCSwipeAreaStatusListener*> statusChangeListeners; |
| 153 | |
| 154 | UG_PREPEND_NAMESPACE(AbstractTimer) *recognitionTimer; |
| 155 | |
| 156 | // How far a touch point has to move from its initial position along the gesture axis in order |
| 157 | // for it to be recognized as a directional drag. |
| 158 | qreal distanceThreshold; |
| 159 | qreal distanceThresholdSquared; // it's pow(distanceThreshold, 2) |
| 160 | // Maximum distance the gesture can go without crossing the axis-aligned distance threshold |
| 161 | qreal maxDistance; |
| 162 | qreal sceneDistance; |
| 163 | |
| 164 | int touchId; |
| 165 | // Maximum time (in milliseconds) the gesture can take to go beyond the distance threshold |
| 166 | int maxTime; |
| 167 | // Maximum time (in milliseconds) after the start of a given touch point where |
| 168 | // subsequent touch starts are grouped with the first one into an N-touches gesture |
| 169 | // (e.g. a two-fingers tap or drag). |
| 170 | int compositionTime; |
| 171 | |
| 172 | // The current status of the directional drag gesture area. |
| 173 | Status status; |
| 174 | UCSwipeArea::Direction direction; |
| 175 | |
| 176 | bool immediateRecognition; |
| 177 | bool grabGesture; |
| 178 | }; |
| 179 | |
| 180 | class LOMIRIGESTURES_EXPORT UCSwipeAreaStatusListener |
| 181 | { |
| 182 | public: |
| 183 | virtual void swipeStatusChanged(UCSwipeAreaPrivate::Status /*old*/, UCSwipeAreaPrivate::Status /*new*/) {} |
| 184 | }; |
| 185 | |
| 186 | UG_NAMESPACE_END |
| 187 | |
| 188 | #endif // UCSWIPEAREA_P_P_H |