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

KDE's Doxygen guidelines are available online.