Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qwasmevent.h
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#ifndef QWASMEVENT_H
5#define QWASMEVENT_H
6
7#include "qwasmplatform.h"
8
9#include <QtCore/qglobal.h>
10#include <QtCore/qnamespace.h>
11#include <QtGui/qevent.h>
12
13#include <QPoint>
14
15#include <emscripten/html5.h>
16#include <emscripten/val.h>
17
19
21
22enum class EventType {
23 Drop,
24 KeyDown,
25 KeyUp,
32 Wheel,
33};
34
35enum class PointerType {
36 Mouse,
37 Touch,
38 Other,
39};
40
41enum class WindowArea {
43 Client,
44};
45
46enum class DeltaMode { Pixel, Line, Page };
47
48namespace KeyboardModifier {
49namespace internal
50{
51 // Check for the existence of shiftKey, ctrlKey, altKey and metaKey in a type.
52 // Based on that, we can safely assume we are dealing with an emscripten event type.
53 template<typename T>
55 {
56 template<typename U, EM_BOOL U::*, EM_BOOL U::*, EM_BOOL U::*, EM_BOOL U::*>
57 struct SFINAE {};
58 template<typename U> static char Test(
60 template<typename U> static int Test(...);
61 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
62 };
63
64 template<class T, typename Enable = void>
65 struct Helper;
66
67 template<class T>
68 struct Helper<T, std::enable_if_t<IsEmscriptenEvent<T>::value>>
69 {
72 if (event.shiftKey)
73 keyModifier |= Qt::ShiftModifier;
74 if (event.ctrlKey)
76 if (event.altKey)
77 keyModifier |= Qt::AltModifier;
78 if (event.metaKey)
80
81 return keyModifier;
82 }
83 };
84
85 template<>
87 {
90 if (event["shiftKey"].as<bool>())
91 keyModifier |= Qt::ShiftModifier;
92 if (event["ctrlKey"].as<bool>())
94 if (event["altKey"].as<bool>())
95 keyModifier |= Qt::AltModifier;
96 if (event["metaKey"].as<bool>())
98 if (event["constructor"]["name"].as<std::string>() == "KeyboardEvent" &&
99 event["location"].as<unsigned int>() == DOM_KEY_LOCATION_NUMPAD) {
100 keyModifier |= Qt::KeypadModifier;
101 }
102
103 return keyModifier;
104 }
105 };
106} // namespace internal
107
108template <typename Event>
110{
112}
113
114template <>
116 const EmscriptenKeyboardEvent& event);
117
118} // namespace KeyboardModifier
119
120struct Event
121{
122 Event(EventType type, emscripten::val target);
128
130 emscripten::val target = emscripten::val::undefined();
131};
132
133struct KeyEvent : public Event
134{
135 static std::optional<KeyEvent>
136 fromWebWithDeadKeyTranslation(emscripten::val webEvent, QWasmDeadKeySupport *deadKeySupport);
137
138 KeyEvent(EventType type, emscripten::val webEvent);
144
149};
150
151struct MouseEvent : public Event
152{
153 MouseEvent(EventType type, emscripten::val webEvent);
159
160 static constexpr Qt::MouseButton buttonFromWeb(int webButton) {
161 switch (webButton) {
162 case 0:
163 return Qt::LeftButton;
164 case 1:
165 return Qt::MiddleButton;
166 case 2:
167 return Qt::RightButton;
168 default:
169 return Qt::NoButton;
170 }
171 }
172
173 static constexpr Qt::MouseButtons buttonsFromWeb(unsigned short webButtons) {
174 // Coincidentally, Qt and web bitfields match.
175 return Qt::MouseButtons::fromInt(webButtons);
176 }
177
179 EventType eventType, WindowArea windowArea) {
180 switch (eventType) {
182 return windowArea == WindowArea::Client ?
185 return windowArea == WindowArea::Client ?
188 return windowArea == WindowArea::Client ?
190 default:
191 return QEvent::None;
192 }
193 }
194
199 Qt::MouseButtons mouseButtons;
201};
202
204{
205 static std::optional<PointerEvent> fromWeb(emscripten::val webEvent);
206
207 PointerEvent(EventType type, emscripten::val webEvent);
213
220};
221
222struct DragEvent : public MouseEvent
223{
224 static std::optional<DragEvent> fromWeb(emscripten::val webEvent);
225
226 DragEvent(EventType type, emscripten::val webEvent);
232
234 emscripten::val dataTransfer;
235};
236
237struct WheelEvent : public MouseEvent
238{
239 static std::optional<WheelEvent> fromWeb(emscripten::val webEvent);
240
241 WheelEvent(EventType type, emscripten::val webEvent);
247
251};
252
254
255#endif // QWASMEVENT_H
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
@ MouseMove
Definition qcoreevent.h:63
@ MouseButtonPress
Definition qcoreevent.h:60
@ NonClientAreaMouseMove
Definition qcoreevent.h:212
@ NonClientAreaMouseButtonRelease
Definition qcoreevent.h:214
@ NonClientAreaMouseButtonPress
Definition qcoreevent.h:213
@ MouseButtonRelease
Definition qcoreevent.h:61
\inmodule QtCore\reentrant
Definition qpoint.h:214
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QFlags< Qt::KeyboardModifier > getForEvent< EmscriptenKeyboardEvent >(const EmscriptenKeyboardEvent &event)
QFlags< Qt::KeyboardModifier > getForEvent(const Event &event)
Definition qwasmevent.h:109
Combined button and popup list for selecting options.
MouseButton
Definition qnamespace.h:55
@ LeftButton
Definition qnamespace.h:57
@ RightButton
Definition qnamespace.h:58
@ MiddleButton
Definition qnamespace.h:59
@ NoButton
Definition qnamespace.h:56
@ ShiftModifier
@ ControlModifier
@ MetaModifier
@ KeypadModifier
@ NoModifier
@ AltModifier
DropAction
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLenum type
GLenum target
struct _cl_event * event
GLuint GLfloat * val
double qreal
Definition qtypes.h:92
DeltaMode
Definition qwasmevent.h:46
PointerType
Definition qwasmevent.h:35
EventType
Definition qwasmevent.h:22
WindowArea
Definition qwasmevent.h:41
QT_BEGIN_NAMESPACE Platform platform()
QSharedPointer< T > other(t)
[5]
DragEvent & operator=(const DragEvent &other)
static std::optional< DragEvent > fromWeb(emscripten::val webEvent)
emscripten::val dataTransfer
Definition qwasmevent.h:234
DragEvent(const DragEvent &other)
DragEvent(DragEvent &&other)
Qt::DropAction dropAction
Definition qwasmevent.h:233
DragEvent & operator=(DragEvent &&other)
Event & operator=(const Event &other)
Event & operator=(Event &&other)
Event(const Event &other)
EventType type
Definition qwasmevent.h:129
Event(Event &&other)
KeyEvent(KeyEvent &&other)
KeyEvent & operator=(const KeyEvent &other)
bool deadKey
Definition qwasmevent.h:147
QString text
Definition qwasmevent.h:148
static std::optional< KeyEvent > fromWebWithDeadKeyTranslation(emscripten::val webEvent, QWasmDeadKeySupport *deadKeySupport)
Qt::Key key
Definition qwasmevent.h:145
KeyEvent & operator=(KeyEvent &&other)
KeyEvent(const KeyEvent &other)
QFlags< Qt::KeyboardModifier > modifiers
Definition qwasmevent.h:146
static QFlags< Qt::KeyboardModifier > getModifierForEvent(const T &event)
Definition qwasmevent.h:70
static QFlags< Qt::KeyboardModifier > getModifierForEvent(const emscripten::val &event)
Definition qwasmevent.h:88
static char Test(SFINAE< U, &U::shiftKey, &U::ctrlKey, &U::altKey, &U::metaKey > *)
static constexpr Qt::MouseButton buttonFromWeb(int webButton)
Definition qwasmevent.h:160
MouseEvent(MouseEvent &&other)
QPointF localPoint
Definition qwasmevent.h:195
static constexpr QEvent::Type mouseEventTypeFromEventType(EventType eventType, WindowArea windowArea)
Definition qwasmevent.h:178
QFlags< Qt::KeyboardModifier > modifiers
Definition qwasmevent.h:200
MouseEvent(const MouseEvent &other)
QPointF pointInViewport
Definition qwasmevent.h:197
Qt::MouseButton mouseButton
Definition qwasmevent.h:198
static constexpr Qt::MouseButtons buttonsFromWeb(unsigned short webButtons)
Definition qwasmevent.h:173
Qt::MouseButtons mouseButtons
Definition qwasmevent.h:199
QPointF pointInPage
Definition qwasmevent.h:196
MouseEvent & operator=(MouseEvent &&other)
MouseEvent & operator=(const MouseEvent &other)
PointerType pointerType
Definition qwasmevent.h:214
PointerEvent(const PointerEvent &other)
PointerEvent(PointerEvent &&other)
static std::optional< PointerEvent > fromWeb(emscripten::val webEvent)
PointerEvent & operator=(PointerEvent &&other)
PointerEvent & operator=(const PointerEvent &other)
WheelEvent & operator=(WheelEvent &&other)
WheelEvent(const WheelEvent &other)
static std::optional< WheelEvent > fromWeb(emscripten::val webEvent)
DeltaMode deltaMode
Definition qwasmevent.h:248
WheelEvent(WheelEvent &&other)
bool webkitDirectionInvertedFromDevice
Definition qwasmevent.h:249
WheelEvent & operator=(const WheelEvent &other)
QPointF delta
Definition qwasmevent.h:250