KDeclarative

mouseeventlistener.h
1 /*
2  SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2013 Sebastian K├╝gler <[email protected]>
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 
23 {
24  Q_OBJECT
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 
36 public:
37  KDeclarativeMouseEvent(int x, int y, int screenX, int screenY,
38  Qt::MouseButton button,
39  Qt::MouseButtons buttons,
40  Qt::KeyboardModifiers modifiers,
41  QScreen* screen,
42  Qt::MouseEventSource source)
43  : m_x(x),
44  m_y(y),
45  m_screenX(screenX),
46  m_screenY(screenY),
47  m_button(button),
48  m_buttons(buttons),
49  m_modifiers(modifiers),
50  m_screen(screen),
51  m_source(source)
52  {}
53 
54 
55  int x() const { return m_x; }
56  int y() const { return m_y; }
57  int screenX() const { return m_screenX; }
58  int screenY() const { return m_screenY; }
59  int button() const { return m_button; }
60  Qt::MouseButtons buttons() const { return m_buttons; }
61  Qt::KeyboardModifiers modifiers() const { return m_modifiers; }
62  QScreen* screen() const { return m_screen; }
63  int source() const { return m_source; }
64 
65  bool isAccepted() const { return m_accepted; }
66  void setAccepted(bool accepted) {
67  if (m_accepted != accepted) {
68  m_accepted = accepted;
69  Q_EMIT acceptedChanged();
70  }
71  }
72 
73  // only for internal usage
74  void setX(int x) { m_x = x; }
75  void setY(int y) { m_y = y; }
76 
77 Q_SIGNALS:
78  void acceptedChanged();
79 
80 private:
81  int m_x;
82  int m_y;
83  int m_screenX;
84  int m_screenY;
85  Qt::MouseButton m_button;
86  Qt::MouseButtons m_buttons;
87  Qt::KeyboardModifiers m_modifiers;
88  QScreen *m_screen;
89  bool m_accepted = false;
90  int m_source;
91 };
92 
93 class KDeclarativeWheelEvent : public QObject
94 {
95  Q_OBJECT
96  Q_PROPERTY(int x READ x CONSTANT)
97  Q_PROPERTY(int y READ y CONSTANT)
98  Q_PROPERTY(int screenX READ screenX CONSTANT)
99  Q_PROPERTY(int screenY READ screenY CONSTANT)
100  Q_PROPERTY(int deltaX READ deltaX CONSTANT)
101  Q_PROPERTY(int deltaY READ deltaY CONSTANT)
102  Q_PROPERTY(int delta READ deltaY CONSTANT) // deprecated in favor of deltaY. TODO KF6: remove
103  Q_PROPERTY(Qt::MouseButtons buttons READ buttons CONSTANT)
104  Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers CONSTANT)
105  Q_PROPERTY(Qt::Orientation orientation READ orientation CONSTANT) // deprecated. TODO KF6: remove
106 
107 public:
108  KDeclarativeWheelEvent(QPointF pos, QPoint screenPos, QPoint angleDelta,
109  Qt::MouseButtons buttons,
110  Qt::KeyboardModifiers modifiers,
111  Qt::Orientation orientation)
112  : m_x(pos.x()),
113  m_y(pos.y()),
114  m_screenX(screenPos.x()),
115  m_screenY(screenPos.y()),
116  m_angleDelta(angleDelta),
117  m_buttons(buttons),
118  m_modifiers(modifiers),
119  m_orientation(orientation)
120  {}
121 
122  int x() const { return m_x; }
123  int y() const { return m_y; }
124  int screenX() const { return m_screenX; }
125  int screenY() const { return m_screenY; }
126  int deltaX() const { return m_angleDelta.x(); }
127  int deltaY() const { return m_angleDelta.y(); }
128  Qt::MouseButtons buttons() const { return m_buttons; }
129  Qt::KeyboardModifiers modifiers() const { return m_modifiers; }
130  Qt::Orientation orientation() { return m_orientation; } // TODO KF6: remove
131 
132  // only for internal usage
133  void setX(int x) { m_x = x; }
134  void setY(int y) { m_y = y; }
135 
136 private:
137  int m_x;
138  int m_y;
139  int m_screenX;
140  int m_screenY;
141  QPoint m_angleDelta;
142  Qt::MouseButtons m_buttons;
143  Qt::KeyboardModifiers m_modifiers;
144  Qt::Orientation m_orientation;
145 };
146 
147 class MouseEventListener : public QQuickItem
148 {
149  Q_OBJECT
154  Q_PROPERTY(bool hoverEnabled READ hoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged)
155 
156 
159  Q_PROPERTY(bool containsMouse READ containsMouse NOTIFY containsMouseChanged)
160 
161  Q_PROPERTY(Qt::MouseButtons acceptedButtons READ acceptedButtons WRITE setAcceptedButtons NOTIFY acceptedButtonsChanged)
162 
163 
167  Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape RESET unsetCursor NOTIFY cursorShapeChanged)
168 
169 
172  Q_PROPERTY(bool pressed READ isPressed NOTIFY pressedChanged)
173 
174 public:
175  MouseEventListener(QQuickItem *parent=nullptr);
176  ~MouseEventListener() override;
177 
178  bool containsMouse() const;
179  void setHoverEnabled(bool enable);
180  bool hoverEnabled() const;
181  bool isPressed() const;
182 
183  Qt::MouseButtons acceptedButtons() const;
184  void setAcceptedButtons(Qt::MouseButtons buttons);
185 
186  Qt::CursorShape cursorShape() const;
187  void setCursorShape(Qt::CursorShape shape);
188 
189 protected:
190  void hoverEnterEvent(QHoverEvent *event) override;
191  void hoverLeaveEvent(QHoverEvent *event) override;
192  void hoverMoveEvent(QHoverEvent * event) override;
193  void mousePressEvent(QMouseEvent *event) override;
194  void mouseMoveEvent(QMouseEvent *event) override;
195  void mouseReleaseEvent(QMouseEvent *event) override;
196  void wheelEvent(QWheelEvent *event) override;
197  bool childMouseEventFilter(QQuickItem *item, QEvent *event) override;
198  void mouseUngrabEvent() override;
199  void touchUngrabEvent() override;
200 
201 Q_SIGNALS:
202  void pressed(KDeclarativeMouseEvent *mouse);
203  void positionChanged(KDeclarativeMouseEvent *mouse);
204  void released(KDeclarativeMouseEvent *mouse);
205  void clicked(KDeclarativeMouseEvent *mouse);
206  void pressAndHold(KDeclarativeMouseEvent *mouse);
207  void wheelMoved(KDeclarativeWheelEvent *wheel);
208  void containsMouseChanged(bool containsMouseChanged);
209  void hoverEnabledChanged(bool hoverEnabled);
210  void acceptedButtonsChanged();
211  void cursorShapeChanged();
212  void pressedChanged();
213  void canceled();
214 
215 private Q_SLOTS:
216  void handlePressAndHold();
217  void handleUngrab();
218 
219 private:
220  static QScreen* screenForGlobalPos(const QPoint &globalPos);
221 
222  bool m_pressed;
223  KDeclarativeMouseEvent* m_pressAndHoldEvent;
224  QPointF m_buttonDownPos;
225  //Important: used only for comparison. If you will ever need to access this pointer, make it a QWeakPointer
226  QEvent *m_lastEvent;
227  QTimer *m_pressAndHoldTimer;
228  bool m_containsMouse;
229  Qt::MouseButtons m_acceptedButtons;
230 };
231 
232 #endif
typedef KeyboardModifiers
typedef MouseButtons
Q_SIGNALSQ_SIGNALS
int x() const const
int y() const const
virtual bool event(QEvent *e)
qreal x() const const
qreal y() const const
Q_OBJECTQ_OBJECT
CursorShape
Q_PROPERTY(...)
This item spies on mouse events from all child objects including child MouseAreas regardless of wheth...
MouseEventSource
Orientation
Q_SLOTSQ_SLOTS
QObject * parent() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Jan 19 2021 22:41:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.