1// Copyright (C) 2016 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 QQUICKMULTIPOINTTOUCHAREA_H
5#define QQUICKMULTIPOINTTOUCHAREA_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 <private/qtquickglobal_p.h>
19
20#include <QtQuick/qquickitem.h>
21
22#include <QtGui/qevent.h>
23#include <QtGui/qguiapplication.h>
24#include <QtGui/qstylehints.h>
25
26#include <QtCore/qlist.h>
27#include <QtCore/qmap.h>
28#include <QtCore/qpointer.h>
29
30#include <QtQml/qqmllist.h>
31
32QT_BEGIN_NAMESPACE
33
34class QQuickMultiPointTouchArea;
35class Q_QUICK_EXPORT QQuickTouchPoint : public QObject
36{
37 Q_OBJECT
38 Q_PROPERTY(int pointId READ pointId NOTIFY pointIdChanged)
39 Q_PROPERTY(QPointingDeviceUniqueId uniqueId READ uniqueId NOTIFY uniqueIdChanged REVISION(2, 9))
40 Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged)
41 Q_PROPERTY(qreal x READ x NOTIFY xChanged)
42 Q_PROPERTY(qreal y READ y NOTIFY yChanged)
43 Q_PROPERTY(QSizeF ellipseDiameters READ ellipseDiameters NOTIFY ellipseDiametersChanged REVISION(2, 9))
44 Q_PROPERTY(qreal pressure READ pressure NOTIFY pressureChanged)
45 Q_PROPERTY(qreal rotation READ rotation NOTIFY rotationChanged REVISION(2, 9))
46 Q_PROPERTY(QVector2D velocity READ velocity NOTIFY velocityChanged)
47 Q_PROPERTY(QRectF area READ area NOTIFY areaChanged)
48
49 Q_PROPERTY(qreal startX READ startX NOTIFY startXChanged)
50 Q_PROPERTY(qreal startY READ startY NOTIFY startYChanged)
51 Q_PROPERTY(qreal previousX READ previousX NOTIFY previousXChanged)
52 Q_PROPERTY(qreal previousY READ previousY NOTIFY previousYChanged)
53 Q_PROPERTY(qreal sceneX READ sceneX NOTIFY sceneXChanged)
54 Q_PROPERTY(qreal sceneY READ sceneY NOTIFY sceneYChanged)
55 QML_NAMED_ELEMENT(TouchPoint)
56 QML_ADDED_IN_VERSION(2, 0)
57
58public:
59 QQuickTouchPoint(bool qmlDefined = true)
60 : _qmlDefined(qmlDefined)
61 {}
62
63 int pointId() const { return _id; }
64 void setPointId(int id);
65
66 QPointingDeviceUniqueId uniqueId() const { return _uniqueId; }
67 void setUniqueId(const QPointingDeviceUniqueId &id);
68
69 qreal x() const { return _x; }
70 qreal y() const { return _y; }
71 void setPosition(QPointF pos);
72
73 QSizeF ellipseDiameters() const { return _ellipseDiameters; }
74 void setEllipseDiameters(const QSizeF &d);
75
76 qreal pressure() const { return _pressure; }
77 void setPressure(qreal pressure);
78
79 qreal rotation() const { return _rotation; }
80 void setRotation(qreal r);
81
82 QVector2D velocity() const { return _velocity; }
83 void setVelocity(const QVector2D &velocity);
84
85 QRectF area() const { return _area; }
86 void setArea(const QRectF &area);
87
88 bool isQmlDefined() const { return _qmlDefined; }
89
90 bool inUse() const { return _inUse; }
91 void setInUse(bool inUse) { _inUse = inUse; }
92
93 bool pressed() const { return _pressed; }
94 void setPressed(bool pressed);
95
96 qreal startX() const { return _startX; }
97 void setStartX(qreal startX);
98
99 qreal startY() const { return _startY; }
100 void setStartY(qreal startY);
101
102 qreal previousX() const { return _previousX; }
103 void setPreviousX(qreal previousX);
104
105 qreal previousY() const { return _previousY; }
106 void setPreviousY(qreal previousY);
107
108 qreal sceneX() const { return _sceneX; }
109 void setSceneX(qreal sceneX);
110
111 qreal sceneY() const { return _sceneY; }
112 void setSceneY(qreal sceneY);
113
114Q_SIGNALS:
115 void pressedChanged();
116 void pointIdChanged();
117 Q_REVISION(2, 9) void uniqueIdChanged();
118 void xChanged();
119 void yChanged();
120 Q_REVISION(2, 9) void ellipseDiametersChanged();
121 void pressureChanged();
122 Q_REVISION(2, 9) void rotationChanged();
123 void velocityChanged();
124 void areaChanged();
125 void startXChanged();
126 void startYChanged();
127 void previousXChanged();
128 void previousYChanged();
129 void sceneXChanged();
130 void sceneYChanged();
131
132private:
133 friend class QQuickMultiPointTouchArea;
134 int _id = 0;
135 qreal _x = 0.0;
136 qreal _y = 0.0;
137 qreal _pressure = 0.0;
138 qreal _rotation = 0;
139 QSizeF _ellipseDiameters;
140 QVector2D _velocity;
141 QRectF _area;
142 bool _qmlDefined;
143 bool _inUse = false; //whether the point is currently in use (only valid when _qmlDefined == true)
144 bool _pressed = false;
145 qreal _startX = 0.0;
146 qreal _startY = 0.0;
147 qreal _previousX = 0.0;
148 qreal _previousY = 0.0;
149 qreal _sceneX = 0.0;
150 qreal _sceneY = 0.0;
151 QPointingDeviceUniqueId _uniqueId;
152};
153
154class QQuickGrabGestureEvent : public QObject
155{
156 Q_OBJECT
157 Q_PROPERTY(QQmlListProperty<QObject> touchPoints READ touchPoints CONSTANT FINAL)
158 Q_PROPERTY(qreal dragThreshold READ dragThreshold CONSTANT FINAL)
159 QML_NAMED_ELEMENT(GestureEvent)
160 QML_ADDED_IN_VERSION(2, 0)
161 QML_UNCREATABLE("GestureEvent is only available in the context of handling the gestureStarted signal from MultiPointTouchArea.")
162
163public:
164 QQuickGrabGestureEvent() : _dragThreshold(QGuiApplication::styleHints()->startDragDistance()) {}
165
166 Q_INVOKABLE void grab() { _grab = true; }
167 bool wantsGrab() const { return _grab; }
168
169 QQmlListProperty<QObject> touchPoints() {
170 return QQmlListProperty<QObject>(this, &_touchPoints);
171 }
172 qreal dragThreshold() const { return _dragThreshold; }
173
174private:
175 friend class QQuickMultiPointTouchArea;
176 bool _grab = false;
177 qreal _dragThreshold;
178 QList<QObject*> _touchPoints;
179};
180
181class Q_QUICK_EXPORT QQuickMultiPointTouchArea : public QQuickItem
182{
183 Q_OBJECT
184 Q_DISABLE_COPY_MOVE(QQuickMultiPointTouchArea)
185
186 Q_PROPERTY(QQmlListProperty<QQuickTouchPoint> touchPoints READ touchPoints CONSTANT)
187 Q_PROPERTY(int minimumTouchPoints READ minimumTouchPoints WRITE setMinimumTouchPoints NOTIFY minimumTouchPointsChanged)
188 Q_PROPERTY(int maximumTouchPoints READ maximumTouchPoints WRITE setMaximumTouchPoints NOTIFY maximumTouchPointsChanged)
189 Q_PROPERTY(bool mouseEnabled READ mouseEnabled WRITE setMouseEnabled NOTIFY mouseEnabledChanged)
190 QML_NAMED_ELEMENT(MultiPointTouchArea)
191 QML_ADDED_IN_VERSION(2, 0)
192
193public:
194 QQuickMultiPointTouchArea(QQuickItem *parent=nullptr);
195 ~QQuickMultiPointTouchArea();
196
197 int minimumTouchPoints() const;
198 void setMinimumTouchPoints(int num);
199 int maximumTouchPoints() const;
200 void setMaximumTouchPoints(int num);
201 bool mouseEnabled() const { return _mouseEnabled; }
202 void setMouseEnabled(bool arg);
203
204 QQmlListProperty<QQuickTouchPoint> touchPoints() {
205 return QQmlListProperty<QQuickTouchPoint>(this, nullptr, QQuickMultiPointTouchArea::touchPoint_append, QQuickMultiPointTouchArea::touchPoint_count, QQuickMultiPointTouchArea::touchPoint_at, nullptr);
206 }
207
208 static void touchPoint_append(QQmlListProperty<QQuickTouchPoint> *list, QQuickTouchPoint* touch) {
209 QQuickMultiPointTouchArea *q = static_cast<QQuickMultiPointTouchArea*>(list->object);
210 q->addTouchPrototype(prototype: touch);
211 }
212
213 static qsizetype touchPoint_count(QQmlListProperty<QQuickTouchPoint> *list) {
214 QQuickMultiPointTouchArea *q = static_cast<QQuickMultiPointTouchArea*>(list->object);
215 return q->_touchPrototypes.size();
216 }
217
218 static QQuickTouchPoint* touchPoint_at(QQmlListProperty<QQuickTouchPoint> *list, qsizetype index) {
219 QQuickMultiPointTouchArea *q = static_cast<QQuickMultiPointTouchArea*>(list->object);
220 return q->_touchPrototypes.value(key: index);
221 }
222
223Q_SIGNALS:
224#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
225 void pressed(const QList<QObject*> &touchPoints);
226 void updated(const QList<QObject*> &touchPoints);
227 void released(const QList<QObject*> &touchPoints);
228 void canceled(const QList<QObject*> &touchPoints);
229#else
230 void pressed(const QList<QObject*> &points);
231 void updated(const QList<QObject*> &points);
232 void released(const QList<QObject*> &points);
233 void canceled(const QList<QObject*> &points);
234#endif
235 void gestureStarted(QQuickGrabGestureEvent *gesture);
236#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
237 void touchUpdated(const QList<QObject*> &touchPoints);
238#else
239 void touchUpdated(const QList<QObject*> &points);
240#endif
241 void minimumTouchPointsChanged();
242 void maximumTouchPointsChanged();
243 void mouseEnabledChanged();
244
245protected:
246 void touchEvent(QTouchEvent *) override;
247 bool childMouseEventFilter(QQuickItem *receiver, QEvent *event) override;
248 void mousePressEvent(QMouseEvent *event) override;
249 void mouseReleaseEvent(QMouseEvent *event) override;
250 void mouseMoveEvent(QMouseEvent *event) override;
251 void mouseUngrabEvent() override;
252 void touchUngrabEvent() override;
253
254 void addTouchPrototype(QQuickTouchPoint* prototype);
255 void addTouchPoint(const QEventPoint *p);
256 void addTouchPoint(const QMouseEvent *e);
257 void clearTouchLists();
258
259 void updateTouchPoint(QQuickTouchPoint*, const QEventPoint*);
260 void updateTouchPoint(QQuickTouchPoint *dtp, const QMouseEvent *e);
261 enum class RemapEventPoints { No, ToLocal };
262 void updateTouchData(QEvent*, RemapEventPoints remap = RemapEventPoints::No);
263
264 bool sendMouseEvent(QMouseEvent *event);
265 bool shouldFilter(QEvent *event);
266 void grabGesture(QPointingDevice *dev);
267 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
268#ifdef Q_OS_MACOS
269 void hoverEnterEvent(QHoverEvent *event) override;
270 void hoverLeaveEvent(QHoverEvent *event) override;
271 void setTouchEventsEnabled(bool enable);
272 void itemChange(ItemChange change, const ItemChangeData &data) override;
273#endif
274
275private:
276 void ungrab(bool normalRelease = false);
277 QMap<int,QQuickTouchPoint*> _touchPrototypes; //TouchPoints defined in QML
278 QMap<int,QObject*> _touchPoints; //All current touch points
279 QList<QObject*> _releasedTouchPoints;
280 QList<QObject*> _pressedTouchPoints;
281 QList<QObject*> _movedTouchPoints;
282 int _minimumTouchPoints;
283 int _maximumTouchPoints;
284 QVector<int> _lastFilterableTouchPointIds;
285 QPointer<QQuickTouchPoint> _mouseTouchPoint; // exists when mouse button is down and _mouseEnabled is true; null otherwise
286 QEventPoint _mouseQpaTouchPoint; // synthetic QPA touch point to hold state and position of the mouse
287 const QPointingDevice *_touchMouseDevice;
288 QPointF _mousePos;
289 bool _stealMouse;
290 bool _mouseEnabled;
291};
292
293QT_END_NAMESPACE
294
295#endif // QQUICKMULTIPOINTTOUCHAREA_H
296

source code of qtdeclarative/src/quick/items/qquickmultipointtoucharea_p.h