1 | // Copyright (C) 2021 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 QQUICKDELIVERYAGENT_P_P_H |
5 | #define QQUICKDELIVERYAGENT_P_P_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 <QtQuick/private/qquickdeliveryagent_p.h> |
19 | #include <QtGui/qevent.h> |
20 | #include <QtCore/qstack.h> |
21 | |
22 | #include <private/qevent_p.h> |
23 | #include <private/qpointingdevice_p.h> |
24 | |
25 | #include <QtCore/qpointer.h> |
26 | #include <private/qobject_p.h> |
27 | |
28 | #include <memory> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class QQuickDragGrabber; |
33 | class QQuickItem; |
34 | class QQuickPointerHandler; |
35 | class QQuickWindow; |
36 | |
37 | /*! \internal |
38 | Extra device-specific data to be stored in QInputDevicePrivate::qqExtra |
39 | */ |
40 | struct { |
41 | // used in QQuickPointerHandlerPrivate::deviceDeliveryTargets |
42 | QVector<QObject *> ; |
43 | }; |
44 | |
45 | class Q_QUICK_EXPORT QQuickDeliveryAgentPrivate : public QObjectPrivate |
46 | { |
47 | public: |
48 | Q_DECLARE_PUBLIC(QQuickDeliveryAgent) |
49 | QQuickDeliveryAgentPrivate(QQuickItem *root); |
50 | ~QQuickDeliveryAgentPrivate(); |
51 | |
52 | QQuickItem *rootItem = nullptr; |
53 | |
54 | QQuickItem *activeFocusItem = nullptr; |
55 | |
56 | void deliverKeyEvent(QKeyEvent *e); |
57 | |
58 | enum FocusOption { |
59 | DontChangeFocusProperty = 0x01, |
60 | DontChangeSubFocusItem = 0x02 |
61 | }; |
62 | Q_DECLARE_FLAGS(FocusOptions, FocusOption) |
63 | |
64 | void setFocusInScope(QQuickItem *scope, QQuickItem *item, Qt::FocusReason reason, FocusOptions = { }); |
65 | void clearFocusInScope(QQuickItem *scope, QQuickItem *item, Qt::FocusReason reason, FocusOptions = { }); |
66 | static void notifyFocusChangesRecur(QQuickItem **item, int remaining, Qt::FocusReason reason); |
67 | void clearFocusObject(); |
68 | void updateFocusItemTransform(); |
69 | |
70 | QQuickItem *focusTargetItem() const; |
71 | |
72 | // Keeps track of the item currently receiving mouse events |
73 | #if QT_CONFIG(quick_draganddrop) |
74 | QQuickDragGrabber *dragGrabber = nullptr; |
75 | #endif |
76 | QQuickItem *lastUngrabbed = nullptr; |
77 | QStack<QPointerEvent *> eventsInDelivery; |
78 | QFlatMap<QPointer<QQuickItem>, uint> hoverItems; |
79 | QVector<QQuickItem *> hasFiltered; // during event delivery to a single receiver, the filtering parents for which childMouseEventFilter was already called |
80 | QVector<QQuickItem *> skipDelivery; // during delivery of one event to all receivers, Items to which we know delivery is no longer necessary |
81 | |
82 | std::unique_ptr<QMutableTouchEvent> delayedTouch; |
83 | QList<const QPointingDevice *> knownPointingDevices; |
84 | |
85 | uint currentHoverId = 0; |
86 | #if QT_CONFIG(wheelevent) |
87 | uint lastWheelEventAccepted = 0; |
88 | #endif |
89 | uchar compressedTouchCount = 0; |
90 | bool allowChildEventFiltering = true; |
91 | bool frameSynchronousHoverEnabled = true; |
92 | bool hoveredLeafItemFound = false; |
93 | |
94 | bool isSubsceneAgent = false; |
95 | static bool subsceneAgentsExist; |
96 | // QQuickDeliveryAgent::event() sets this to the one that's currently (trying to) handle the event |
97 | static QQuickDeliveryAgent *currentEventDeliveryAgent; |
98 | static QQuickDeliveryAgent *currentOrItemDeliveryAgent(const QQuickItem *item); |
99 | |
100 | Qt::FocusReason lastFocusReason = Qt::OtherFocusReason; |
101 | int pointerEventRecursionGuard = 0; |
102 | |
103 | int touchMouseId = -1; // only for obsolete stuff like QQuickItem::grabMouse() |
104 | // TODO get rid of these |
105 | const QPointingDevice *touchMouseDevice = nullptr; |
106 | ulong touchMousePressTimestamp = 0; |
107 | QPoint touchMousePressPos; // in screen coordinates |
108 | |
109 | QQuickDeliveryAgent::Transform *sceneTransform = nullptr; |
110 | |
111 | bool isDeliveringTouchAsMouse() const { return touchMouseId != -1 && touchMouseDevice; } |
112 | void cancelTouchMouseSynthesis(); |
113 | |
114 | bool checkIfDoubleTapped(ulong newPressEventTimestamp, const QPoint &newPressPos); |
115 | void resetIfDoubleTapPrevented(const QEventPoint &pressedPoint); |
116 | QPointingDevicePrivate::EventPointData *mousePointData(); |
117 | QPointerEvent *eventInDelivery() const; |
118 | |
119 | // Mouse positions are saved in widget coordinates |
120 | QPointF lastMousePosition; |
121 | bool deliverTouchAsMouse(QQuickItem *item, QTouchEvent *pointerEvent); |
122 | void translateTouchEvent(QTouchEvent *touchEvent); |
123 | void removeGrabber(QQuickItem *grabber, bool mouse = true, bool touch = true, bool cancel = false); |
124 | void clearGrabbers(QPointerEvent *pointerEvent); |
125 | void onGrabChanged(QObject *grabber, QPointingDevice::GrabTransition transition, const QPointerEvent *event, const QEventPoint &point); |
126 | static QPointerEvent *clonePointerEvent(QPointerEvent *event, std::optional<QPointF> transformedLocalPos = std::nullopt); |
127 | void deliverToPassiveGrabbers(const QVector<QPointer<QObject> > &passiveGrabbers, QPointerEvent *pointerEvent); |
128 | bool sendFilteredMouseEvent(QEvent *event, QQuickItem *receiver, QQuickItem *filteringParent); |
129 | bool sendFilteredPointerEvent(QPointerEvent *event, QQuickItem *receiver, QQuickItem *filteringParent = nullptr); |
130 | bool sendFilteredPointerEventImpl(QPointerEvent *event, QQuickItem *receiver, QQuickItem *filteringParent); |
131 | bool deliverSinglePointEventUntilAccepted(QPointerEvent *); |
132 | |
133 | // entry point of events to the window |
134 | void handleTouchEvent(QTouchEvent *); |
135 | void handleMouseEvent(QMouseEvent *); |
136 | bool compressTouchEvent(QTouchEvent *); |
137 | void flushFrameSynchronousEvents(QQuickWindow *win); |
138 | void deliverDelayedTouchEvent(); |
139 | void handleWindowDeactivate(QQuickWindow *win); |
140 | void handleWindowHidden(QQuickWindow *win); |
141 | |
142 | // utility functions that used to be in QQuickPointerEvent et al. |
143 | bool allUpdatedPointsAccepted(const QPointerEvent *ev); |
144 | static void localizePointerEvent(QPointerEvent *ev, const QQuickItem *dest); |
145 | QList<QObject *> exclusiveGrabbers(QPointerEvent *ev); |
146 | static bool anyPointGrabbed(const QPointerEvent *ev); |
147 | static bool allPointsGrabbed(const QPointerEvent *ev); |
148 | static bool isMouseEvent(const QPointerEvent *ev); |
149 | static bool isMouseOrWheelEvent(const QPointerEvent *ev); |
150 | static bool isHoverEvent(const QPointerEvent *ev); |
151 | static bool isTouchEvent(const QPointerEvent *ev); |
152 | static bool isTabletEvent(const QPointerEvent *ev); |
153 | static bool isEventFromMouseOrTouchpad(const QPointerEvent *ev); |
154 | static bool isSynthMouse(const QPointerEvent *ev); |
155 | static bool isWithinDoubleClickInterval(ulong timeInterval); |
156 | static bool isWithinDoubleTapDistance(const QPoint &distanceBetweenPresses); |
157 | static bool isSinglePointDevice(const QInputDevice *dev); |
158 | static QQuickPointingDeviceExtra *(const QInputDevice *device); |
159 | |
160 | // delivery of pointer events: |
161 | void touchToMouseEvent(QEvent::Type type, const QEventPoint &p, const QTouchEvent *touchEvent, QMutableSinglePointEvent *mouseEvent); |
162 | void ensureDeviceConnected(const QPointingDevice *dev); |
163 | void deliverPointerEvent(QPointerEvent *); |
164 | bool deliverTouchCancelEvent(QTouchEvent *); |
165 | bool deliverPressOrReleaseEvent(QPointerEvent *, bool handlersOnly = false); |
166 | void deliverUpdatedPoints(QPointerEvent *event); |
167 | void deliverMatchingPointsToItem(QQuickItem *item, bool isGrabber, QPointerEvent *pointerEvent, bool handlersOnly = false); |
168 | |
169 | QVector<QQuickItem *> pointerTargets(QQuickItem *, const QPointerEvent *event, const QEventPoint &point, |
170 | bool checkMouseButtons, bool checkAcceptsTouch) const; |
171 | QVector<QQuickItem *> mergePointerTargets(const QVector<QQuickItem *> &list1, const QVector<QQuickItem *> &list2) const; |
172 | |
173 | // hover delivery |
174 | enum class HoverChange : uint8_t { |
175 | Clear, |
176 | Set, |
177 | }; |
178 | bool deliverHoverEvent(const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, ulong timestamp); |
179 | bool deliverHoverEventRecursive(QQuickItem *, const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, ulong timestamp); |
180 | bool deliverHoverEventToItem(QQuickItem *item, const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, ulong timestamp, |
181 | HoverChange hoverChange); |
182 | bool sendHoverEvent(QEvent::Type, QQuickItem *, const QPointF &scenePos, const QPointF &lastScenePos, |
183 | Qt::KeyboardModifiers modifiers, ulong timestamp); |
184 | bool clearHover(ulong timestamp = 0); |
185 | |
186 | #if QT_CONFIG(quick_draganddrop) |
187 | void deliverDragEvent(QQuickDragGrabber *, QEvent *); |
188 | bool deliverDragEvent(QQuickDragGrabber *, QQuickItem *, QDragMoveEvent *, |
189 | QVarLengthArray<QQuickItem *, 64> *currentGrabItems = nullptr, |
190 | QObject *formerTarget = nullptr); |
191 | #endif |
192 | |
193 | static bool dragOverThreshold(qreal d, Qt::Axis axis, QMouseEvent *event, int startDragThreshold = -1); |
194 | |
195 | static bool dragOverThreshold(qreal d, Qt::Axis axis, const QEventPoint &tp, int startDragThreshold = -1); |
196 | |
197 | static bool dragOverThreshold(QVector2D delta); |
198 | }; |
199 | |
200 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickDeliveryAgentPrivate::FocusOptions) |
201 | |
202 | QT_END_NAMESPACE |
203 | |
204 | #endif // QQUICKDELIVERYAGENT_P_P_H |
205 | |