Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qwasmwindowclientarea.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
5
6#include "qwasmdom.h"
7#include "qwasmevent.h"
8#include "qwasmscreen.h"
9#include "qwasmwindow.h"
10
11#include <QtGui/private/qguiapplication_p.h>
12#include <QtGui/qpointingdevice.h>
13
14#include <QtCore/qassert.h>
15
17
19 : m_screen(screen), m_window(window), m_element(element)
20{
21 const auto callback = std::function([this](emscripten::val event) {
22 processPointer(*PointerEvent::fromWeb(event));
23 event.call<void>("preventDefault");
24 event.call<void>("stopPropagation");
25 });
26
27 m_pointerDownCallback =
28 std::make_unique<qstdweb::EventCallback>(element, "pointerdown", callback);
29 m_pointerMoveCallback =
30 std::make_unique<qstdweb::EventCallback>(element, "pointermove", callback);
31 m_pointerUpCallback = std::make_unique<qstdweb::EventCallback>(element, "pointerup", callback);
32 m_pointerCancelCallback =
33 std::make_unique<qstdweb::EventCallback>(element, "pointercancel", callback);
34}
35
36bool ClientArea::processPointer(const PointerEvent &event)
37{
38
39 switch (event.type) {
41 m_element.call<void>("setPointerCapture", event.pointerId);
42 m_window->window()->requestActivate();
43 break;
45 m_element.call<void>("releasePointerCapture", event.pointerId);
46 break;
47 default:
48 break;
49 };
50
51 const bool eventAccepted = deliverEvent(event);
52 if (!eventAccepted && event.type == EventType::PointerDown)
54 return eventAccepted;
55}
56
57bool ClientArea::deliverEvent(const PointerEvent &event)
58{
59 const auto pointInScreen = m_screen->mapFromLocal(
60 dom::mapPoint(event.target, m_screen->element(), event.localPoint));
61
62 const auto geometryF = m_screen->geometry().toRectF();
63 const QPointF targetPointClippedToScreen(
64 qBound(geometryF.left(), pointInScreen.x(), geometryF.right()),
65 qBound(geometryF.top(), pointInScreen.y(), geometryF.bottom()));
66
67 if (event.pointerType == PointerType::Mouse) {
68 const QEvent::Type eventType =
70
71 return eventType != QEvent::None
74 m_window->window()->mapFromGlobal(targetPointClippedToScreen),
75 targetPointClippedToScreen, event.mouseButtons, event.mouseButton,
76 eventType, event.modifiers);
77 }
78
80
81 QPointF pointInTargetWindowCoords =
82 QPointF(m_window->window()->mapFromGlobal(targetPointClippedToScreen));
83 QPointF normalPosition(pointInTargetWindowCoords.x() / m_window->window()->width(),
84 pointInTargetWindowCoords.y() / m_window->window()->height());
85
86 const auto tp = m_pointerIdToTouchPoints.find(event.pointerId);
87 if (tp != m_pointerIdToTouchPoints.end()) {
88 touchPoint = &tp.value();
89 } else {
90 touchPoint = &m_pointerIdToTouchPoints
92 .value();
93
94 // Assign touch point id. TouchPoint::id is int, but QGuiApplicationPrivate::processTouchEvent()
95 // will not synthesize mouse events for touch points with negative id; use the absolute value for
96 // the touch point id.
97 touchPoint->id = qAbs(event.pointerId);
98
100 }
101
102 const bool stationaryTouchPoint = (normalPosition == touchPoint->normalPosition);
103 touchPoint->normalPosition = normalPosition;
104 touchPoint->area = QRectF(targetPointClippedToScreen, QSizeF(event.width, event.height))
105 .translated(-event.width / 2, -event.height / 2);
106 touchPoint->pressure = event.pressure;
107
108 switch (event.type) {
111 break;
113 touchPoint->state = (stationaryTouchPoint ? QEventPoint::State::Stationary
115 break;
116 default:
117 break;
118 }
119
121 touchPointList.reserve(m_pointerIdToTouchPoints.size());
122 std::transform(m_pointerIdToTouchPoints.begin(), m_pointerIdToTouchPoints.end(),
123 std::back_inserter(touchPointList),
124 [](const QWindowSystemInterface::TouchPoint &val) { return val; });
125
126 if (event.type == EventType::PointerUp)
127 m_pointerIdToTouchPoints.remove(event.pointerId);
128
129 return event.type == EventType::PointerCancel
131 m_window->window(), QWasmIntegration::getTimestamp(), m_screen->touchDevice(),
132 event.modifiers)
133 : QWindowSystemInterface::handleTouchEvent(
134 m_window->window(), QWasmIntegration::getTimestamp(), m_screen->touchDevice(),
135 touchPointList, event.modifiers);
136}
137
ClientArea(QWasmWindow *window, QWasmScreen *screen, emscripten::val element)
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
static QGuiApplicationPrivate * instance()
Definition qlist.h:74
void reserve(qsizetype size)
Definition qlist.h:746
iterator insert(const Key &key, const T &value)
Definition qmap.h:687
size_type remove(const Key &key)
Definition qmap.h:299
iterator find(const Key &key)
Definition qmap.h:640
iterator begin()
Definition qmap.h:597
iterator end()
Definition qmap.h:601
size_type size() const
Definition qmap.h:266
\inmodule QtCore\reentrant
Definition qpoint.h:214
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:333
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:338
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr QRectF translated(qreal dx, qreal dy) const noexcept
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis,...
Definition qrect.h:748
constexpr QRectF toRectF() const noexcept
Definition qrect.h:843
\inmodule QtCore
Definition qsize.h:207
static quint64 getTimestamp()
emscripten::val element() const
QPointF mapFromLocal(const QPointF &p) const
QPointingDevice * touchDevice()
Definition qwasmscreen.h:41
QRect geometry() const override
Reimplement in subclass to return the pixel geometry of the screen.
QWindow * window() const
Definition qwasmwindow.h:94
The QWindowSystemInterface provides an event queue for the QPA platform.
static bool handleTouchCancelEvent(QWindow *window, const QPointingDevice *device, Qt::KeyboardModifiers mods=Qt::NoModifier)
static bool handleMouseEvent(QWindow *window, const QPointF &local, const QPointF &global, Qt::MouseButtons state, Qt::MouseButton button, QEvent::Type type, Qt::KeyboardModifiers mods=Qt::NoModifier, Qt::MouseEventSource source=Qt::MouseEventNotSynthesized)
int width
the width of the window's geometry
Definition qwindow.h:82
int height
the height of the window's geometry
Definition qwindow.h:83
EGLImageKHR int int EGLuint64KHR * modifiers
Combined button and popup list for selecting options.
QPointF mapPoint(emscripten::val source, emscripten::val target, const QPointF &point)
Definition qwasmdom.cpp:28
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
constexpr T qAbs(const T &t)
Definition qnumeric.h:328
struct _cl_event * event
GLuint GLfloat * val
QScreen * screen
[1]
Definition main.cpp:29
aWidget window() -> setWindowTitle("New Window Title")
[2]
static constexpr QEvent::Type mouseEventTypeFromEventType(EventType eventType, WindowArea windowArea)
Definition qwasmevent.h:178
static std::optional< PointerEvent > fromWeb(emscripten::val webEvent)