Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwasmevent.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qwasmevent.h"
5
7
8#include <QtCore/private/qmakearray_p.h>
9#include <QtCore/private/qstringiterator_p.h>
10#include <QtCore/qregularexpression.h>
11
13
14namespace {
15constexpr std::string_view WebDeadKeyValue = "Dead";
16
17bool isDeadKeyEvent(const char *key)
18{
19 return qstrncmp(key, WebDeadKeyValue.data(), WebDeadKeyValue.size()) == 0;
20}
21
22Qt::Key getKeyFromCode(const std::string &code)
23{
24 if (auto mapping = QWasmKeyTranslator::mapWebKeyTextToQtKey(code.c_str()))
25 return *mapping;
26
27 static QRegularExpression regex(QString(QStringLiteral(R"re((?:Key|Digit)(\w))re")));
28 const auto codeQString = QString::fromStdString(code);
29 const auto match = regex.match(codeQString);
30
31 if (!match.hasMatch())
32 return Qt::Key_unknown;
33
34 constexpr size_t CharacterIndex = 1;
35 return static_cast<Qt::Key>(match.capturedView(CharacterIndex).at(0).toLatin1());
36}
37
38Qt::Key webKeyToQtKey(const std::string &code, const std::string &key, bool isDeadKey,
39 QFlags<Qt::KeyboardModifier> modifiers)
40{
41 if (isDeadKey) {
42 auto mapped = getKeyFromCode(code);
43 switch (mapped) {
44 case Qt::Key_U:
45 return Qt::Key_Dead_Diaeresis;
46 case Qt::Key_E:
47 return Qt::Key_Dead_Acute;
48 case Qt::Key_I:
49 return Qt::Key_Dead_Circumflex;
50 case Qt::Key_N:
51 return Qt::Key_Dead_Tilde;
52 case Qt::Key_QuoteLeft:
53 return modifiers.testFlag(Qt::ShiftModifier) ? Qt::Key_Dead_Tilde : Qt::Key_Dead_Grave;
54 case Qt::Key_6:
55 return Qt::Key_Dead_Circumflex;
56 case Qt::Key_Apostrophe:
57 return modifiers.testFlag(Qt::ShiftModifier) ? Qt::Key_Dead_Diaeresis
58 : Qt::Key_Dead_Acute;
59 case Qt::Key_AsciiTilde:
60 return Qt::Key_Dead_Tilde;
61 default:
62 return Qt::Key_unknown;
63 }
64 } else if (auto mapping = QWasmKeyTranslator::mapWebKeyTextToQtKey(key.c_str())) {
65 return *mapping;
66 }
67
68 // cast to unicode key
69 QString str = QString::fromUtf8(key.c_str()).toUpper();
70 if (str.length() > 1)
71 return Qt::Key_unknown;
72
73 QStringIterator i(str);
74 return static_cast<Qt::Key>(i.next(0));
75}
76} // namespace
77
78namespace KeyboardModifier
79{
80template <>
83{
86}
87} // namespace KeyboardModifier
88
89Event::Event(EventType type, emscripten::val webEvent)
90 : webEvent(webEvent), type(type)
91{
92}
93
94Event::~Event() = default;
95
96Event::Event(const Event &other) = default;
97
98Event::Event(Event &&other) = default;
99
100Event &Event::operator=(const Event &other) = default;
101
102Event &Event::operator=(Event &&other) = default;
103
104KeyEvent::KeyEvent(EventType type, emscripten::val event) : Event(type, event)
105{
106 const auto code = event["code"].as<std::string>();
107 const auto webKey = event["key"].as<std::string>();
108 deadKey = isDeadKeyEvent(webKey.c_str());
109 autoRepeat = event["repeat"].as<bool>();
110 modifiers = KeyboardModifier::getForEvent(event);
111 key = webKeyToQtKey(code, webKey, deadKey, modifiers);
112
113 text = QString::fromUtf8(webKey);
114 if (text.size() > 1)
115 text.clear();
116
117 if (key == Qt::Key_Tab)
118 text = "\t";
119}
120
121KeyEvent::~KeyEvent() = default;
122
123KeyEvent::KeyEvent(const KeyEvent &other) = default;
124
125KeyEvent::KeyEvent(KeyEvent &&other) = default;
126
127KeyEvent &KeyEvent::operator=(const KeyEvent &other) = default;
128
129KeyEvent &KeyEvent::operator=(KeyEvent &&other) = default;
130
131std::optional<KeyEvent> KeyEvent::fromWebWithDeadKeyTranslation(emscripten::val event,
132 QWasmDeadKeySupport *deadKeySupport)
133{
134 const auto eventType = ([&event]() -> std::optional<EventType> {
135 const auto eventTypeString = event["type"].as<std::string>();
136
137 if (eventTypeString == "keydown")
138 return EventType::KeyDown;
139 else if (eventTypeString == "keyup")
140 return EventType::KeyUp;
141 return std::nullopt;
142 })();
143 if (!eventType)
144 return std::nullopt;
145
146 auto result = KeyEvent(*eventType, event);
147 deadKeySupport->applyDeadKeyTranslations(&result);
148
149 return result;
150}
151
152MouseEvent::MouseEvent(EventType type, emscripten::val event) : Event(type, event)
153{
154 mouseButton = MouseEvent::buttonFromWeb(event["button"].as<int>());
155 mouseButtons = MouseEvent::buttonsFromWeb(event["buttons"].as<unsigned short>());
156 // The current button state (event.buttons) may be out of sync for some PointerDown
157 // events where the "down" state is very brief, for example taps on Apple trackpads.
158 // Qt expects that the current button state is in sync with the event, so we sync
159 // it up here.
160 if (type == EventType::PointerDown)
161 mouseButtons |= mouseButton;
162 localPoint = QPointF(event["offsetX"].as<qreal>(), event["offsetY"].as<qreal>());
163 pointInPage = QPointF(event["pageX"].as<qreal>(), event["pageY"].as<qreal>());
164 pointInViewport = QPointF(event["clientX"].as<qreal>(), event["clientY"].as<qreal>());
165 modifiers = KeyboardModifier::getForEvent(event);
166}
167
168MouseEvent::~MouseEvent() = default;
169
170MouseEvent::MouseEvent(const MouseEvent &other) = default;
171
172MouseEvent::MouseEvent(MouseEvent &&other) = default;
173
174MouseEvent &MouseEvent::operator=(const MouseEvent &other) = default;
175
176MouseEvent &MouseEvent::operator=(MouseEvent &&other) = default;
177
178PointerEvent::PointerEvent(EventType type, emscripten::val event) : MouseEvent(type, event)
179{
180 pointerId = event["pointerId"].as<int>();
181 pointerType = ([type = event["pointerType"].as<std::string>()]() {
182 if (type == "mouse")
183 return PointerType::Mouse;
184 if (type == "touch")
185 return PointerType::Touch;
186 if (type == "pen")
187 return PointerType::Pen;
188 return PointerType::Other;
189 })();
190 width = event["width"].as<qreal>();
191 height = event["height"].as<qreal>();
192 pressure = event["pressure"].as<qreal>();
193 tiltX = event["tiltX"].as<qreal>();
194 tiltY = event["tiltY"].as<qreal>();
195 tangentialPressure = event["tangentialPressure"].as<qreal>();
196 twist = event["twist"].as<qreal>();
197 isPrimary = event["isPrimary"].as<bool>();
198}
199
200PointerEvent::~PointerEvent() = default;
201
202PointerEvent::PointerEvent(const PointerEvent &other) = default;
203
204PointerEvent::PointerEvent(PointerEvent &&other) = default;
205
206PointerEvent &PointerEvent::operator=(const PointerEvent &other) = default;
207
208PointerEvent &PointerEvent::operator=(PointerEvent &&other) = default;
209
210std::optional<PointerEvent> PointerEvent::fromWeb(emscripten::val event)
211{
212 const auto eventType = ([&event]() -> std::optional<EventType> {
213 const auto eventTypeString = event["type"].as<std::string>();
214
215 if (eventTypeString == "pointermove")
216 return EventType::PointerMove;
217 else if (eventTypeString == "pointerup")
218 return EventType::PointerUp;
219 else if (eventTypeString == "pointerdown")
220 return EventType::PointerDown;
221 else if (eventTypeString == "pointerenter")
222 return EventType::PointerEnter;
223 else if (eventTypeString == "pointerleave")
224 return EventType::PointerLeave;
225 return std::nullopt;
226 })();
227 if (!eventType)
228 return std::nullopt;
229
230 return PointerEvent(*eventType, event);
231}
232
233DragEvent::DragEvent(EventType type, emscripten::val event, QWindow *window)
234 : MouseEvent(type, event), dataTransfer(event["dataTransfer"]), targetWindow(window)
235{
236 dropAction = ([event]() {
237 const std::string effect = event["dataTransfer"]["dropEffect"].as<std::string>();
238
239 if (effect == "copy")
240 return Qt::CopyAction;
241 else if (effect == "move")
242 return Qt::MoveAction;
243 else if (effect == "link")
244 return Qt::LinkAction;
245 return Qt::IgnoreAction;
246 })();
247}
248
249DragEvent::~DragEvent() = default;
250
251DragEvent::DragEvent(const DragEvent &other) = default;
252
253DragEvent::DragEvent(DragEvent &&other) = default;
254
255DragEvent &DragEvent::operator=(const DragEvent &other) = default;
256
257DragEvent &DragEvent::operator=(DragEvent &&other) = default;
258
259std::optional<DragEvent> DragEvent::fromWeb(emscripten::val event, QWindow *targetWindow)
260{
261 const auto eventType = ([&event]() -> std::optional<EventType> {
262 const auto eventTypeString = event["type"].as<std::string>();
263 if (eventTypeString == "dragend")
264 return EventType::DragEnd;
265 if (eventTypeString == "dragover")
266 return EventType::DragOver;
267 if (eventTypeString == "dragstart")
268 return EventType::DragStart;
269 if (eventTypeString == "drop")
270 return EventType::Drop;
271 if (eventTypeString == "dragleave")
272 return EventType::DragLeave;
273 return std::nullopt;
274 })();
275 if (!eventType)
276 return std::nullopt;
277 return DragEvent(*eventType, event, targetWindow);
278}
279
281{
282 Q_ASSERT_X(type == EventType::DragStart, Q_FUNC_INFO, "Only supported for DragStart");
283 webEvent.call<void>("preventDefault");
284}
285
287{
288 Q_ASSERT_X(type == EventType::DragOver, Q_FUNC_INFO, "Only supported for DragOver");
289 webEvent.call<void>("preventDefault");
290}
291
293{
294 Q_ASSERT_X(type == EventType::Drop, Q_FUNC_INFO, "Only supported for Drop");
295 webEvent.call<void>("preventDefault");
296}
297
298WheelEvent::WheelEvent(EventType type, emscripten::val event) : MouseEvent(type, event)
299{
300 deltaMode = ([event]() {
301 const int deltaMode = event["deltaMode"].as<int>();
302 const auto jsWheelEventType = emscripten::val::global("WheelEvent");
303 if (deltaMode == jsWheelEventType["DOM_DELTA_PIXEL"].as<int>())
304 return DeltaMode::Pixel;
305 else if (deltaMode == jsWheelEventType["DOM_DELTA_LINE"].as<int>())
306 return DeltaMode::Line;
307 return DeltaMode::Page;
308 })();
309
310 delta = QPointF(event["deltaX"].as<qreal>(), event["deltaY"].as<qreal>());
311
312 webkitDirectionInvertedFromDevice = event["webkitDirectionInvertedFromDevice"].as<bool>();
313}
314
315WheelEvent::~WheelEvent() = default;
316
317WheelEvent::WheelEvent(const WheelEvent &other) = default;
318
319WheelEvent::WheelEvent(WheelEvent &&other) = default;
320
321WheelEvent &WheelEvent::operator=(const WheelEvent &other) = default;
322
323WheelEvent &WheelEvent::operator=(WheelEvent &&other) = default;
324
325std::optional<WheelEvent> WheelEvent::fromWeb(emscripten::val event)
326{
327 const auto eventType = ([&event]() -> std::optional<EventType> {
328 const auto eventTypeString = event["type"].as<std::string>();
329
330 if (eventTypeString == "wheel")
331 return EventType::Wheel;
332 return std::nullopt;
333 })();
334 if (!eventType)
335 return std::nullopt;
336 return WheelEvent(*eventType, event);
337}
338
339QT_END_NAMESPACE
void applyDeadKeyTranslations(KeyEvent *event)
Combined button and popup list for selecting options.
constexpr std::string_view WebDeadKeyValue
bool isDeadKeyEvent(const char *key)
Qt::Key webKeyToQtKey(const std::string &code, const std::string &key, bool isDeadKey, QFlags< Qt::KeyboardModifier > modifiers)
Qt::Key getKeyFromCode(const std::string &code)
DeltaMode
Definition qwasmevent.h:53
PointerType
Definition qwasmevent.h:41
EventType
Definition qwasmevent.h:24
DragEvent & operator=(const DragEvent &other)
DragEvent(EventType type, emscripten::val webEvent, QWindow *targetQWindow)
void acceptDrop()
void acceptDragOver()
void cancelDragStart()
DragEvent(const DragEvent &other)
QWindow * targetWindow
Definition qwasmevent.h:252
DragEvent(DragEvent &&other)
DragEvent & operator=(DragEvent &&other)
Event & operator=(const Event &other)
Event & operator=(Event &&other)
Event(EventType type, emscripten::val webEvent)
Event(const Event &other)
EventType type
Definition qwasmevent.h:137
Event(Event &&other)
KeyEvent(KeyEvent &&other)
KeyEvent & operator=(const KeyEvent &other)
bool deadKey
Definition qwasmevent.h:155
bool autoRepeat
Definition qwasmevent.h:157
KeyEvent(EventType type, emscripten::val webEvent)
KeyEvent & operator=(KeyEvent &&other)
KeyEvent(const KeyEvent &other)
MouseEvent(MouseEvent &&other)
MouseEvent(const MouseEvent &other)
MouseEvent(EventType type, emscripten::val webEvent)
MouseEvent & operator=(MouseEvent &&other)
MouseEvent & operator=(const MouseEvent &other)
PointerType pointerType
Definition qwasmevent.h:223
PointerEvent(const PointerEvent &other)
PointerEvent(PointerEvent &&other)
PointerEvent(EventType type, emscripten::val webEvent)
PointerEvent & operator=(PointerEvent &&other)
PointerEvent & operator=(const PointerEvent &other)
WheelEvent & operator=(WheelEvent &&other)
WheelEvent(const WheelEvent &other)
DeltaMode deltaMode
Definition qwasmevent.h:266
WheelEvent(WheelEvent &&other)
WheelEvent(EventType type, emscripten::val webEvent)
bool webkitDirectionInvertedFromDevice
Definition qwasmevent.h:267
WheelEvent & operator=(const WheelEvent &other)