KDeclarative

mouseeventlistener.h
1/*
2 SPDX-FileCopyrightText: 2011 Marco Martin <notmart@gmail.com>
3 SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef MOUSEEVENTLISTENER_H
9#define MOUSEEVENTLISTENER_H
10
11#include <QQuickItem>
12
13/**
14 * This item spies on mouse events from all child objects including child MouseAreas regardless
15 * of whether the child MouseArea propagates events. It does not accept the event.
16 *
17 * In addition unlike MouseArea events include the mouse position in global coordinates and provides
18 * the screen the mouse is in.
19 */
20
22{
24 QML_ELEMENT
25 QML_ANONYMOUS
26 Q_PROPERTY(int x READ x)
27 Q_PROPERTY(int y READ y)
28 Q_PROPERTY(int screenX READ screenX)
29 Q_PROPERTY(int screenY READ screenY)
30 Q_PROPERTY(int button READ button)
31 Q_PROPERTY(Qt::MouseButtons buttons READ buttons)
32 Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers)
33 Q_PROPERTY(QScreen *screen READ screen CONSTANT)
34 Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted NOTIFY acceptedChanged)
35 Q_PROPERTY(int source READ source)
36
37public:
39 int y,
40 int screenX,
41 int screenY,
42 Qt::MouseButton button,
43 Qt::MouseButtons buttons,
44 Qt::KeyboardModifiers modifiers,
45 QScreen *screen,
47 : m_x(x)
48 , m_y(y)
49 , m_screenX(screenX)
50 , m_screenY(screenY)
51 , m_button(button)
52 , m_buttons(buttons)
53 , m_modifiers(modifiers)
54 , m_screen(screen)
55 , m_source(source)
56 {
57 }
58
59 int x() const
60 {
61 return m_x;
62 }
63 int y() const
64 {
65 return m_y;
66 }
67 int screenX() const
68 {
69 return m_screenX;
70 }
71 int screenY() const
72 {
73 return m_screenY;
74 }
75 int button() const
76 {
77 return m_button;
78 }
79 Qt::MouseButtons buttons() const
80 {
81 return m_buttons;
82 }
83 Qt::KeyboardModifiers modifiers() const
84 {
85 return m_modifiers;
86 }
87 QScreen *screen() const
88 {
89 return m_screen;
90 }
91 int source() const
92 {
93 return m_source;
94 }
95
96 bool isAccepted() const
97 {
98 return m_accepted;
99 }
100 void setAccepted(bool accepted)
101 {
102 if (m_accepted != accepted) {
103 m_accepted = accepted;
104 Q_EMIT acceptedChanged();
105 }
106 }
107
108 // only for internal usage
109 void setX(int x)
110 {
111 m_x = x;
112 }
113 void setY(int y)
114 {
115 m_y = y;
116 }
117
119 void acceptedChanged();
120
121private:
122 int m_x;
123 int m_y;
124 int m_screenX;
125 int m_screenY;
126 Qt::MouseButton m_button;
127 Qt::MouseButtons m_buttons;
128 Qt::KeyboardModifiers m_modifiers;
129 QScreen *m_screen;
130 bool m_accepted = false;
131 int m_source;
132};
133
134class KDeclarativeWheelEvent : public QObject
135{
137 QML_ELEMENT
138 QML_ANONYMOUS
139 Q_PROPERTY(int x READ x CONSTANT)
140 Q_PROPERTY(int y READ y CONSTANT)
141 Q_PROPERTY(int screenX READ screenX CONSTANT)
142 Q_PROPERTY(int screenY READ screenY CONSTANT)
143 Q_PROPERTY(int deltaX READ deltaX CONSTANT)
144 Q_PROPERTY(int deltaY READ deltaY CONSTANT)
145 Q_PROPERTY(int delta READ deltaY CONSTANT) // deprecated in favor of deltaY. TODO KF6: remove
146 Q_PROPERTY(Qt::MouseButtons buttons READ buttons CONSTANT)
147 Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers CONSTANT)
148 Q_PROPERTY(Qt::Orientation orientation READ orientation CONSTANT) // deprecated. TODO KF6: remove
149
150public:
151 KDeclarativeWheelEvent(QPointF pos,
152 QPoint screenPos,
153 QPoint angleDelta,
154 Qt::MouseButtons buttons,
155 Qt::KeyboardModifiers modifiers,
156 Qt::Orientation orientation)
157 : m_x(pos.x())
158 , m_y(pos.y())
159 , m_screenX(screenPos.x())
160 , m_screenY(screenPos.y())
161 , m_angleDelta(angleDelta)
162 , m_buttons(buttons)
163 , m_modifiers(modifiers)
164 , m_orientation(orientation)
165 {
166 }
167
168 int x() const
169 {
170 return m_x;
171 }
172 int y() const
173 {
174 return m_y;
175 }
176 int screenX() const
177 {
178 return m_screenX;
179 }
180 int screenY() const
181 {
182 return m_screenY;
183 }
184 int deltaX() const
185 {
186 return m_angleDelta.x();
187 }
188 int deltaY() const
189 {
190 return m_angleDelta.y();
191 }
192 Qt::MouseButtons buttons() const
193 {
194 return m_buttons;
195 }
196 Qt::KeyboardModifiers modifiers() const
197 {
198 return m_modifiers;
199 }
200 Qt::Orientation orientation()
201 {
202 return m_orientation;
203 } // TODO KF6: remove
204
205 // only for internal usage
206 void setX(int x)
207 {
208 m_x = x;
209 }
210 void setY(int y)
211 {
212 m_y = y;
213 }
214
215private:
216 int m_x;
217 int m_y;
218 int m_screenX;
219 int m_screenY;
220 QPoint m_angleDelta;
221 Qt::MouseButtons m_buttons;
222 Qt::KeyboardModifiers m_modifiers;
223 Qt::Orientation m_orientation;
224};
225
226class MouseEventListener : public QQuickItem
227{
229 QML_ELEMENT
230 /**
231 * This property holds whether hover events are handled.
232 * By default hover events are disabled
233 */
234 Q_PROPERTY(bool hoverEnabled READ hoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged)
235
236 /**
237 * True if this MouseEventListener or any of its children contains the mouse cursor:
238 * this property will change only when the mouse button is pressed if hoverEnabled is false.
239 */
240 Q_PROPERTY(bool containsMouse READ containsMouse NOTIFY containsMouseChanged)
241
242 Q_PROPERTY(Qt::MouseButtons acceptedButtons READ acceptedButtons WRITE setAcceptedButtons NOTIFY acceptedButtonsChanged)
243
244 /**
245 * This property holds the cursor shape for this mouse area.
246 * Note that on platforms that do not display a mouse cursor this may have no effect.
247 */
248 Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape RESET unsetCursor NOTIFY cursorShapeChanged)
249
250 /**
251 * True if the mouse is pressed in the item or any of its children
252 */
253 Q_PROPERTY(bool pressed READ isPressed NOTIFY pressedChanged)
254
255public:
256 MouseEventListener(QQuickItem *parent = nullptr);
257 ~MouseEventListener() override;
258
259 bool containsMouse() const;
260 void setHoverEnabled(bool enable);
261 bool hoverEnabled() const;
262 bool isPressed() const;
263
264 Qt::MouseButtons acceptedButtons() const;
265 void setAcceptedButtons(Qt::MouseButtons buttons);
266
267 Qt::CursorShape cursorShape() const;
268 void setCursorShape(Qt::CursorShape shape);
269
270protected:
271 void hoverEnterEvent(QHoverEvent *event) override;
272 void hoverLeaveEvent(QHoverEvent *event) override;
273 void hoverMoveEvent(QHoverEvent *event) override;
274 void mousePressEvent(QMouseEvent *event) override;
275 void mouseMoveEvent(QMouseEvent *event) override;
276 void mouseReleaseEvent(QMouseEvent *event) override;
277 void wheelEvent(QWheelEvent *event) override;
278 bool childMouseEventFilter(QQuickItem *item, QEvent *event) override;
279 void mouseUngrabEvent() override;
280 void touchUngrabEvent() override;
281
283 void pressed(KDeclarativeMouseEvent *mouse);
284 void positionChanged(KDeclarativeMouseEvent *mouse);
285 void released(KDeclarativeMouseEvent *mouse);
286 void clicked(KDeclarativeMouseEvent *mouse);
287 void pressAndHold(KDeclarativeMouseEvent *mouse);
288 void wheelMoved(KDeclarativeWheelEvent *wheel);
289 void containsMouseChanged(bool containsMouseChanged);
290 void hoverEnabledChanged(bool hoverEnabled);
291 void acceptedButtonsChanged();
292 void cursorShapeChanged();
293 void pressedChanged();
294 void canceled();
295
296private Q_SLOTS:
297 void handlePressAndHold();
298 void handleUngrab();
299
300private:
301 static QScreen *screenForGlobalPos(const QPointF &globalPos);
302
303 bool m_pressed;
304 KDeclarativeMouseEvent *m_pressAndHoldEvent;
305 QPointF m_buttonDownPos;
306 // Important: used only for comparison. If you will ever need to access this pointer, make it a QWeakPointer
307 QEvent *m_lastEvent;
308 QTimer *m_pressAndHoldTimer;
309 bool m_containsMouse = false;
310 bool m_childContainsMouse = false;
311 Qt::MouseButtons m_acceptedButtons;
312};
313
314#endif
This item spies on mouse events from all child objects including child MouseAreas regardless of wheth...
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
int x() const const
int y() const const
virtual bool event(QEvent *ev) override
void unsetCursor()
CursorShape
typedef KeyboardModifiers
typedef MouseButtons
MouseEventSource
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.