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
25QT_BEGIN_NAMESPACE
26
27class Q_QUICK_PRIVATE_EXPORT QQuickTapHandler : public QQuickSinglePointHandler
28{
29 Q_OBJECT
30 Q_PROPERTY(bool pressed READ isPressed NOTIFY pressedChanged FINAL)
31 Q_PROPERTY(int tapCount READ tapCount NOTIFY tapCountChanged FINAL)
32 Q_PROPERTY(qreal timeHeld READ timeHeld NOTIFY timeHeldChanged FINAL)
33 Q_PROPERTY(qreal longPressThreshold READ longPressThreshold WRITE setLongPressThreshold NOTIFY longPressThresholdChanged FINAL)
34 Q_PROPERTY(GesturePolicy gesturePolicy READ gesturePolicy WRITE setGesturePolicy NOTIFY gesturePolicyChanged FINAL)
35 Q_PROPERTY(QQuickTapHandler::ExclusiveSignals exclusiveSignals READ exclusiveSignals WRITE setExclusiveSignals NOTIFY exclusiveSignalsChanged REVISION(6, 5) FINAL)
36
37 QML_NAMED_ELEMENT(TapHandler)
38 QML_ADDED_IN_VERSION(2, 12)
39
40public:
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
67 GesturePolicy gesturePolicy() const { return m_gesturePolicy; }
68 void setGesturePolicy(GesturePolicy gesturePolicy);
69
70 QQuickTapHandler::ExclusiveSignals exclusiveSignals() const { return m_exclusiveSignals; }
71 void setExclusiveSignals(QQuickTapHandler::ExclusiveSignals newexclusiveSignals);
72
73Q_SIGNALS:
74 void pressedChanged();
75 void tapCountChanged();
76 void timeHeldChanged();
77 void longPressThresholdChanged();
78 void gesturePolicyChanged();
79 Q_REVISION(6, 5) void exclusiveSignalsChanged();
80 // the second argument (Qt::MouseButton) was added in 6.2: avoid name clashes with IDs by not naming it for now
81 void tapped(QEventPoint eventPoint, Qt::MouseButton /* button */);
82 void singleTapped(QEventPoint eventPoint, Qt::MouseButton /* button */);
83 void doubleTapped(QEventPoint eventPoint, Qt::MouseButton /* button */);
84 void longPressed();
85
86protected:
87 void onGrabChanged(QQuickPointerHandler *grabber, QPointingDevice::GrabTransition transition,
88 QPointerEvent *ev, QEventPoint &point) override;
89 void timerEvent(QTimerEvent *event) override;
90 bool wantsEventPoint(const QPointerEvent *event, const QEventPoint &point) override;
91 void handleEventPoint(QPointerEvent *event, QEventPoint &point) override;
92
93private:
94 void setPressed(bool press, bool cancel, QPointerEvent *event, QEventPoint &point);
95 int longPressThresholdMilliseconds() const;
96 void connectPreRenderSignal(bool conn = true);
97 void updateTimeHeld();
98
99private:
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 Qt::MouseButton m_singleTapReleasedButton;
107 int m_tapCount = 0;
108 int m_longPressThreshold = -1;
109 GesturePolicy m_gesturePolicy = GesturePolicy::DragThreshold;
110 ExclusiveSignals m_exclusiveSignals = NotExclusive;
111 bool m_pressed = false;
112
113 static quint64 m_multiTapInterval;
114 static int m_mouseMultiClickDistanceSquared;
115 static int m_touchMultiTapDistanceSquared;
116};
117
118Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickTapHandler::ExclusiveSignals)
119
120QT_END_NAMESPACE
121
122#endif // QQUICKTAPHANDLER_H
123

source code of qtdeclarative/src/quick/handlers/qquicktaphandler_p.h