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
qquickpointhandler.cpp
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
6#include <private/qquickdeliveryagent_p_p.h>
7#include <private/qquickwindow_p.h>
8#include <QDebug>
9#include <QtCore/qpointer.h>
10
12
13/*!
14 \qmltype PointHandler
15 \nativetype QQuickPointHandler
16 \inherits SinglePointHandler
17 \inqmlmodule QtQuick
18 \ingroup qtquick-input-handlers
19 \brief Handler for reacting to a single touchpoint.
20
21 PointHandler can be used to show feedback about a touchpoint or the mouse
22 position, or to otherwise react to pointer events.
23
24 When a press event occurs, each instance of PointHandler chooses a
25 single point which is not yet "taken" at that moment: if the press
26 occurs within the bounds of the \l {PointerHandler::parent}, and
27 no sibling PointHandler within the same \l {PointerHandler::parent}
28 has yet acquired a passive grab on that point, and if the other
29 constraints such as \l {PointerDeviceHandler::acceptedButtons}{acceptedButtons}, \l {PointerDeviceHandler::acceptedDevices}{acceptedDevices} etc.
30 are satisfied, it's
31 eligible, and the PointHandler then acquires a passive grab. In
32 this way, the \l {PointerHandler::parent} acts like an exclusive
33 group: there can be multiple instances of PointHandler, and the
34 set of pressed touchpoints will be distributed among them. Each
35 PointHandler which has chosen a point to track has its \l active
36 property \c true. It then continues to track its chosen point
37 until release: the properties of the \l point will be kept
38 up-to-date. Any Item can bind to these properties, and thereby
39 follow the point's movements.
40
41 By being only a passive grabber, it has the ability to keep independent
42 oversight of all movements. The passive grab cannot be stolen or overridden
43 even when other gestures are detected and exclusive grabs occur.
44
45 If your goal is orthogonal surveillance of eventpoints, an older
46 alternative was QObject::installEventFilter(), but that has never been a
47 built-in QtQuick feature: it requires some C++ code, such as a QQuickItem
48 subclass. PointHandler is more efficient than that, because only pointer
49 events will be delivered to it, during the course of normal event delivery
50 in QQuickWindow; whereas an event filter needs to filter all QEvents of all
51 types, and thus sets itself up as a potential event delivery bottleneck.
52
53 One possible use case is to add this handler to a transparent Item which is
54 on top of the rest of the scene (by having a high \l{Item::z} {z} value),
55 so that when a point is freshly pressed, it will be delivered to that Item
56 and its handlers first, providing the opportunity to take the passive grab
57 as early as possible. Such an item (like a pane of glass over the whole UI)
58 can be a convenient parent for other Items which visualize the kind of reactive
59 feedback which must always be on top; and likewise it can be the parent for
60 popups, popovers, dialogs and so on. If it will be used in that way, it can
61 be helpful for your main.cpp to use QQmlContext::setContextProperty() to
62 make the "glass pane" accessible by ID to the entire UI, so that other
63 Items and PointHandlers can be reparented to it.
64
65 \snippet pointerHandlers/pointHandler.qml 0
66
67 Like all input handlers, a PointHandler has a \l target property, which
68 may be used as a convenient place to put a point-tracking Item; but
69 PointHandler will not automatically manipulate the \c target item in any way.
70 You need to use bindings to make it react to the \l point.
71
72 \note On macOS, PointHandler does not react to multiple fingers on the
73 trackpad by default, although it does react to a pressed point (mouse position).
74 That is because macOS can provide either native gesture recognition, or raw
75 touchpoints, but not both. We prefer to use the native gesture event in
76 PinchHandler, so we do not want to disable it by enabling touch. However
77 MultiPointTouchArea does enable touch, thus disabling native gesture
78 recognition within the entire window; so it's an alternative if you only
79 want to react to all the touchpoints but do not require the smooth
80 native-gesture experience.
81
82 \sa MultiPointTouchArea, HoverHandler, {Qt Quick Examples - Pointer Handlers}
83*/
84
85QQuickPointHandler::QQuickPointHandler(QQuickItem *parent)
86 : QQuickSinglePointHandler(parent)
87{
88 setIgnoreAdditionalPoints();
89}
90
91bool QQuickPointHandler::wantsEventPoint(const QPointerEvent *event, const QEventPoint &point)
92{
93 // On press, we want it unless a sibling of the same type also does.
94 // If a synth-mouse press occurs, and we've already been interested in the original point, stay interested.
95 const bool trackedPointId = QQuickSinglePointHandler::point().id() == point.id() &&
96 QQuickSinglePointHandler::point().device() == point.device();
97 if ((point.state() == QEventPoint::Pressed && QQuickSinglePointHandler::wantsEventPoint(event, point))
98 || (QQuickDeliveryAgentPrivate::isSynthMouse(event) && trackedPointId)) {
99 for (const QObject *grabber : event->passiveGrabbers(point)) {
100 if (grabber && grabber != this && grabber->parent() == parent() &&
101 grabber->metaObject()->className() == metaObject()->className())
102 return false;
103 }
104 return true;
105 }
106 // If we've already been interested in a point, stay interested, even if it has strayed outside bounds.
107 return (point.state() != QEventPoint::Pressed && trackedPointId);
108}
109
110void QQuickPointHandler::handleEventPoint(QPointerEvent *event, QEventPoint &point)
111{
112 switch (point.state()) {
113 case QEventPoint::Pressed:
114 if (acceptedButtons() == Qt::NoButton || !event->isSinglePointEvent() ||
115 (static_cast<const QSinglePointEvent *>(event)->buttons() & acceptedButtons()) != Qt::NoButton) {
116 setPassiveGrab(event, point);
117 setActive(true);
118 }
119 break;
120 case QEventPoint::Released:
121 if (acceptedButtons() == Qt::NoButton || !event->isSinglePointEvent() ||
122 (static_cast<const QSinglePointEvent *>(event)->buttons() & acceptedButtons()) == Qt::NoButton)
123 setActive(false);
124 break;
125 default:
126 break;
127 }
128 point.setAccepted(false); // Just lurking... don't interfere with propagation
129 emit translationChanged();
130 if (!QQuickDeliveryAgentPrivate::isSynthMouse(event))
131 QQuickSinglePointHandler::handleEventPoint(event, point);
132}
133
134QVector2D QQuickPointHandler::translation() const
135{
136 return QVector2D(point().position() - point().pressPosition());
137}
138
139/*!
140 \qmlproperty flags PointHandler::acceptedButtons
141
142 The mouse buttons that can activate this PointHandler.
143
144 By default, this property is set to \l {QtQuick::MouseEvent::button} {Qt.LeftButton}.
145 It can be set to an OR combination of mouse buttons, and will ignore events
146 in which other buttons are pressed or held. If it is set to \c Qt.NoButton,
147 it means it does not care about buttons at all, and ignores synthetic
148 mouse events that come from any device for which it's already handling
149 an authentic \l eventPoint.
150
151 \snippet pointerHandlers/pointHandlerAcceptedButtons.qml 0
152
153 \note On a touchscreen, there are no buttons, so this property does not
154 prevent PointHandler from reacting to touchpoints.
155
156 \note By default, when this property holds \c Qt.LeftButton, if a
157 non-mouse \l {PointerDevice} (such as a touchscreen or graphics tablet
158 stylus) is allowed to generate synthetic mouse events, those usually
159 indicate that the left mouse button is pressed, and those events can
160 temporarily de-activate the PointHandler which was already reacting to an
161 authentic \l eventPoint from the device. It's useful to declare
162 \qml
163 acceptedButtons: \c Qt.NoButton
164 \endqml
165 to avoid this issue. See also
166 \l Qt::AA_SynthesizeMouseForUnhandledTouchEvents and
167 \l Qt::AA_SynthesizeMouseForUnhandledTabletEvents.
168*/
169
170/*!
171 \qmlproperty flags PointHandler::acceptedDevices
172
173 The types of pointing devices that can activate this PointHandler.
174
175 By default, this property is set to
176 \l{QInputDevice::DeviceType}{PointerDevice.AllDevices}.
177 If you set it to an OR combination of device types, it will ignore events
178 from non-matching \l {PointerDevice}{devices}:
179
180 \snippet pointerHandlers/pointHandler.qml 1
181*/
182
183/*!
184 \qmlproperty flags PointHandler::acceptedPointerTypes
185
186 The types of pointing instruments (finger, stylus, eraser, etc.)
187 that can activate this PointHandler.
188
189 By default, this property is set to
190 \l {QPointingDevice::PointerType} {PointerDevice.AllPointerTypes}.
191 If you set it to an OR combination of device types, it will ignore events
192 from non-matching \l {PointerDevice}{devices}:
193
194 \snippet pointerHandlers/pointHandlerCanvasDrawing.qml 0
195
196 The \l {Qt Quick Examples - Pointer Handlers} includes a more complex example for
197 drawing on a Canvas with a graphics tablet.
198*/
199
200/*!
201 \qmlproperty flags PointHandler::acceptedModifiers
202
203 If this property is set, PointHandler requires the given keyboard modifiers
204 to be pressed in order to react to \l {PointerEvent}{PointerEvents}, and
205 otherwise ignores them.
206
207 If this property is set to \c Qt.KeyboardModifierMask (the default value),
208 then PointHandler ignores the modifier keys.
209
210 For example, an \l [QML] Item could have two handlers, one of which is
211 enabled only if the required keyboard modifier is pressed:
212
213 \snippet pointerHandlers/pointHandlerAcceptedModifiers.qml 0
214
215 If you set \c acceptedModifiers to an OR combination of modifier keys,
216 it means \e all of those modifiers must be pressed to activate the handler.
217
218 The available modifiers are as follows:
219
220 \value NoModifier No modifier key is allowed.
221 \value ShiftModifier A Shift key on the keyboard must be pressed.
222 \value ControlModifier A Ctrl key on the keyboard must be pressed.
223 \value AltModifier An Alt key on the keyboard must be pressed.
224 \value MetaModifier A Meta key on the keyboard must be pressed.
225 \value KeypadModifier A keypad button must be pressed.
226 \value GroupSwitchModifier X11 only (unless activated on Windows by a command line argument).
227 A Mode_switch key on the keyboard must be pressed.
228 \value KeyboardModifierMask The handler does not care which modifiers are pressed.
229
230 \sa Qt::KeyboardModifier
231*/
232
233/*!
234 \readonly
235 \qmlproperty bool PointHandler::active
236
237 This holds \c true whenever the constraints are satisfied and this
238 PointHandler is reacting. This means that it is keeping its properties
239 up-to-date according to the movements of the \l {eventPoint}{eventPoints}
240 that satisfy the constraints.
241*/
242
243/*!
244 \internal
245 \qmlproperty flags PointHandler::dragThreshold
246
247 This property is not used in PointHandler.
248*/
249
250/*!
251 \qmlproperty real PointHandler::margin
252
253 The margin beyond the bounds of the \l {PointerHandler::parent}{parent}
254 item within which an \l eventPoint can activate this handler.
255
256 The default value is \c 0.
257
258 \snippet pointerHandlers/pointHandlerMargin.qml 0
259*/
260
261/*!
262 \qmlproperty real PointHandler::target
263
264 A property that can conveniently hold an Item to be manipulated or to show
265 feedback. Unlike other \l {Qt Quick Input Handlers}{Pointer Handlers},
266 PointHandler does not do anything with the \c target on its own: you
267 usually need to create reactive bindings to properties such as
268 \l SinglePointHandler::point and \l PointHandler::active. If you declare
269 an Item instance here, you need to explicitly set its \l {Item::}{parent},
270 because PointHandler is not an Item.
271
272 By default, it is the same as the \l {PointerHandler::}{parent}, the Item
273 within which the handler is declared.
274*/
275
276QT_END_NAMESPACE
277
278#include "moc_qquickpointhandler_p.cpp"