/src/qtbase/src/gui/kernel/qevent_p.h
Line | Count | Source |
1 | | // Copyright (C) 2020 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 | | // Qt-Security score:significant reason:default |
4 | | |
5 | | #ifndef QEVENT_P_H |
6 | | #define QEVENT_P_H |
7 | | |
8 | | // |
9 | | // W A R N I N G |
10 | | // ------------- |
11 | | // |
12 | | // This file is not part of the Qt API. It exists for the convenience |
13 | | // of other Qt classes. This header file may change from version to |
14 | | // version without notice, or even be removed. |
15 | | // |
16 | | // We mean it. |
17 | | // |
18 | | |
19 | | #include <QtGui/private/qtguiglobal_p.h> |
20 | | #include <QtCore/qurl.h> |
21 | | #include <QtGui/qevent.h> |
22 | | #include <QtGui/qwindow.h> |
23 | | |
24 | | QT_BEGIN_NAMESPACE |
25 | | |
26 | | class QPointingDevice; |
27 | | |
28 | | /*! |
29 | | \internal |
30 | | \since 6.9 |
31 | | |
32 | | This class provides a way to store copies of QEvents. QEvents can otherwise |
33 | | only be copied by \l{QEvent::clone()}{cloning}, which allocates the copy on |
34 | | the heap. By befriending concrete QEvent subclasses, QEventStorage gains |
35 | | access to their protected copy constructors. |
36 | | |
37 | | It is similar to std::optional, but with a more targeted API. |
38 | | |
39 | | Note that by storing an event in QEventStorage, it may be sliced. |
40 | | */ |
41 | | template <typename Event> |
42 | | class QEventStorage |
43 | | { |
44 | | Q_DISABLE_COPY_MOVE(QEventStorage) |
45 | | static_assert(std::is_base_of_v<QEvent, Event>); |
46 | | union { |
47 | | char m_eventNotSet; // could be std::monostate, but don't want to pay for <variant> include |
48 | | Event m_event; |
49 | | }; |
50 | | bool m_engaged = false; |
51 | | |
52 | | void engage(const Event &e) |
53 | | { |
54 | | Q_ASSERT(!m_engaged); |
55 | | new (&m_event) Event(e); |
56 | | m_engaged = true; |
57 | | } |
58 | | |
59 | | void disengage() |
60 | | { |
61 | | Q_ASSERT(m_engaged); |
62 | | m_event.~Event(); |
63 | | m_engaged = false; |
64 | | } |
65 | | |
66 | | public: |
67 | | QEventStorage() noexcept : m_eventNotSet{} {} |
68 | | explicit QEventStorage(const Event &e) |
69 | | { |
70 | | engage(e); |
71 | | } |
72 | | |
73 | | ~QEventStorage() |
74 | | { |
75 | | if (m_engaged) |
76 | | disengage(); |
77 | | } |
78 | | |
79 | | explicit operator bool() const noexcept { return m_engaged; } |
80 | | bool operator!() const noexcept { return !m_engaged; } |
81 | | |
82 | | Event &store(const Event &e) |
83 | | { |
84 | | if (m_engaged) |
85 | | disengage(); |
86 | | engage(e); |
87 | | return m_event; |
88 | | } |
89 | | |
90 | | Event &storeUnlessAlias(const Event &e) |
91 | | { |
92 | | if (m_engaged && &e == &m_event) |
93 | | return m_event; |
94 | | return store(e); |
95 | | } |
96 | | |
97 | | const Event &operator*() const |
98 | | { |
99 | | Q_PRE(m_engaged); |
100 | | return m_event; |
101 | | } |
102 | | |
103 | | Event &operator*() |
104 | | { |
105 | | Q_PRE(m_engaged); |
106 | | return m_event; |
107 | | } |
108 | | |
109 | | const Event *operator->() const |
110 | | { |
111 | | Q_PRE(m_engaged); |
112 | | return &m_event; |
113 | | } |
114 | | |
115 | | Event *operator->() |
116 | | { |
117 | | Q_PRE(m_engaged); |
118 | | return &m_event; |
119 | | } |
120 | | }; |
121 | | |
122 | | class Q_GUI_EXPORT QMutableTouchEvent : public QTouchEvent |
123 | | { |
124 | | public: |
125 | | QMutableTouchEvent(QEvent::Type eventType = QEvent::TouchBegin, |
126 | | const QPointingDevice *device = nullptr, |
127 | | Qt::KeyboardModifiers modifiers = Qt::NoModifier, |
128 | | const QList<QEventPoint> &touchPoints = QList<QEventPoint>()) : |
129 | 0 | QTouchEvent(eventType, device, modifiers, touchPoints) { } |
130 | | ~QMutableTouchEvent() override; |
131 | | |
132 | 0 | void setTarget(QObject *target) { m_target = target; } |
133 | | void addPoint(const QEventPoint &point); |
134 | | |
135 | 0 | static void setTarget(QTouchEvent *e, QObject *target) { e->m_target = target; } |
136 | | static void addPoint(QTouchEvent *e, const QEventPoint &point); |
137 | | }; |
138 | | |
139 | | class Q_GUI_EXPORT QMutableSinglePointEvent : public QSinglePointEvent |
140 | | { |
141 | | public: |
142 | 0 | QMutableSinglePointEvent(const QSinglePointEvent &other) : QSinglePointEvent(other) {} |
143 | | QMutableSinglePointEvent(Type type = QEvent::None, const QPointingDevice *device = nullptr, const QEventPoint &point = QEventPoint(), |
144 | | Qt::MouseButton button = Qt::NoButton, Qt::MouseButtons buttons = Qt::NoButton, |
145 | | Qt::KeyboardModifiers modifiers = Qt::NoModifier, |
146 | | Qt::MouseEventSource source = Qt::MouseEventSynthesizedByQt) : |
147 | 0 | QSinglePointEvent(type, device, point, button, buttons, modifiers, source) { } |
148 | | ~QMutableSinglePointEvent() override; |
149 | | |
150 | 0 | void setSource(Qt::MouseEventSource s) { m_source = s; } |
151 | | |
152 | 0 | bool isDoubleClick() { return m_doubleClick; } |
153 | | |
154 | 0 | void setDoubleClick(bool d = true) { m_doubleClick = d; } |
155 | | |
156 | | static bool isDoubleClick(const QSinglePointEvent *ev) |
157 | 0 | { |
158 | 0 | return ev->m_doubleClick; |
159 | 0 | } |
160 | | |
161 | | static void setDoubleClick(QSinglePointEvent *ev, bool d) |
162 | 0 | { |
163 | 0 | ev->m_doubleClick = d; |
164 | 0 | } |
165 | | }; |
166 | | |
167 | | QT_END_NAMESPACE |
168 | | |
169 | | #endif // QEVENT_P_H |