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