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

KDE's Doxygen guidelines are available online.