| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QQUICKTAPHANDLER_H |
| 5 | #define QQUICKTAPHANDLER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qbasictimer.h> |
| 19 | #include <QtCore/qelapsedtimer.h> |
| 20 | #include <QtGui/qevent.h> |
| 21 | #include <QtQuick/qquickitem.h> |
| 22 | |
| 23 | #include "qquicksinglepointhandler_p.h" |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class Q_QUICK_EXPORT QQuickTapHandler : public QQuickSinglePointHandler |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | Q_PROPERTY(bool pressed READ isPressed NOTIFY pressedChanged) |
| 31 | Q_PROPERTY(int tapCount READ tapCount NOTIFY tapCountChanged) |
| 32 | Q_PROPERTY(qreal timeHeld READ timeHeld NOTIFY timeHeldChanged) |
| 33 | Q_PROPERTY(qreal longPressThreshold READ longPressThreshold WRITE setLongPressThreshold NOTIFY longPressThresholdChanged RESET resetLongPressThreshold) |
| 34 | Q_PROPERTY(GesturePolicy gesturePolicy READ gesturePolicy WRITE setGesturePolicy NOTIFY gesturePolicyChanged) |
| 35 | Q_PROPERTY(QQuickTapHandler::ExclusiveSignals exclusiveSignals READ exclusiveSignals WRITE setExclusiveSignals NOTIFY exclusiveSignalsChanged REVISION(6, 5)) |
| 36 | |
| 37 | QML_NAMED_ELEMENT(TapHandler) |
| 38 | QML_ADDED_IN_VERSION(2, 12) |
| 39 | |
| 40 | public: |
| 41 | enum GesturePolicy { |
| 42 | DragThreshold, |
| 43 | WithinBounds, |
| 44 | ReleaseWithinBounds, |
| 45 | DragWithinBounds |
| 46 | }; |
| 47 | Q_ENUM(GesturePolicy) |
| 48 | |
| 49 | enum ExclusiveSignal { |
| 50 | NotExclusive = 0, |
| 51 | SingleTap = 1 << 0, |
| 52 | DoubleTap = 1 << 1 |
| 53 | }; |
| 54 | Q_DECLARE_FLAGS(ExclusiveSignals, ExclusiveSignal) |
| 55 | Q_FLAG(ExclusiveSignals) |
| 56 | |
| 57 | explicit QQuickTapHandler(QQuickItem *parent = nullptr); |
| 58 | |
| 59 | bool isPressed() const { return m_pressed; } |
| 60 | |
| 61 | int tapCount() const { return m_tapCount; } |
| 62 | qreal timeHeld() const { return (m_holdTimer.isValid() ? m_holdTimer.elapsed() / 1000.0 : -1.0); } |
| 63 | |
| 64 | qreal longPressThreshold() const; |
| 65 | void setLongPressThreshold(qreal longPressThreshold); |
| 66 | void resetLongPressThreshold(); |
| 67 | |
| 68 | GesturePolicy gesturePolicy() const { return m_gesturePolicy; } |
| 69 | void setGesturePolicy(GesturePolicy gesturePolicy); |
| 70 | |
| 71 | QQuickTapHandler::ExclusiveSignals exclusiveSignals() const { return m_exclusiveSignals; } |
| 72 | void setExclusiveSignals(QQuickTapHandler::ExclusiveSignals newexclusiveSignals); |
| 73 | |
| 74 | Q_SIGNALS: |
| 75 | void pressedChanged(); |
| 76 | void tapCountChanged(); |
| 77 | void timeHeldChanged(); |
| 78 | void longPressThresholdChanged(); |
| 79 | void gesturePolicyChanged(); |
| 80 | Q_REVISION(6, 5) void exclusiveSignalsChanged(); |
| 81 | // the second argument (Qt::MouseButton) was added in 6.2: avoid name clashes with IDs by not naming it for now |
| 82 | void tapped(QEventPoint eventPoint, Qt::MouseButton /* button */); |
| 83 | void singleTapped(QEventPoint eventPoint, Qt::MouseButton /* button */); |
| 84 | void doubleTapped(QEventPoint eventPoint, Qt::MouseButton /* button */); |
| 85 | void longPressed(); |
| 86 | |
| 87 | protected: |
| 88 | void onGrabChanged(QQuickPointerHandler *grabber, QPointingDevice::GrabTransition transition, |
| 89 | QPointerEvent *ev, QEventPoint &point) override; |
| 90 | void timerEvent(QTimerEvent *event) override; |
| 91 | bool wantsEventPoint(const QPointerEvent *event, const QEventPoint &point) override; |
| 92 | void handleEventPoint(QPointerEvent *event, QEventPoint &point) override; |
| 93 | |
| 94 | private: |
| 95 | void setPressed(bool press, bool cancel, QPointerEvent *event, QEventPoint &point); |
| 96 | void connectPreRenderSignal(bool conn = true); |
| 97 | void updateTimeHeld(); |
| 98 | |
| 99 | private: |
| 100 | QPointF m_lastTapPos; |
| 101 | quint64 m_lastTapTimestamp = 0; |
| 102 | QElapsedTimer m_holdTimer; |
| 103 | QBasicTimer m_longPressTimer; |
| 104 | QBasicTimer m_doubleTapTimer; |
| 105 | QEventPoint m_singleTapReleasedPoint; |
| 106 | QMetaObject::Connection m_preRenderSignalConnection; |
| 107 | Qt::MouseButton m_singleTapReleasedButton; |
| 108 | int m_tapCount = 0; |
| 109 | int m_longPressThreshold = -1; |
| 110 | GesturePolicy m_gesturePolicy = GesturePolicy::DragThreshold; |
| 111 | ExclusiveSignals m_exclusiveSignals = NotExclusive; |
| 112 | bool m_pressed = false; |
| 113 | bool m_longPressed = false; |
| 114 | |
| 115 | static quint64 m_multiTapInterval; |
| 116 | static int m_mouseMultiClickDistanceSquared; |
| 117 | static int m_touchMultiTapDistanceSquared; |
| 118 | }; |
| 119 | |
| 120 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickTapHandler::ExclusiveSignals) |
| 121 | |
| 122 | QT_END_NAMESPACE |
| 123 | |
| 124 | #endif // QQUICKTAPHANDLER_H |
| 125 | |