From 9c6b8ea2907ac97392b521a1c68e4dd03c2028e1 Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Mon, 3 Oct 2022 16:19:42 +0200 Subject: [PATCH] Add the toDOMRect/fromDOMRect functions to QRectF These are very helpful when converting to and from DOMRect. Change-Id: I4a7fc6318f45bed8e2b82fd5d6ec174dc1762326 Fixes: QTBUG-107740 Reviewed-by: Paul Wicking --- src/corelib/CMakeLists.txt | 4 +++ src/corelib/kernel/qcore_wasm.cpp | 45 +++++++++++++++++++++++++++++++ src/corelib/tools/qrect.h | 10 +++++++ 3 files changed, 59 insertions(+) create mode 100644 src/corelib/kernel/qcore_wasm.cpp diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index f06c02279b6..1405d383685 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -574,6 +574,10 @@ qt_internal_extend_target(Core CONDITION WIN32 userenv ) +qt_internal_extend_target(Core CONDITION WASM + SOURCES + kernel/qcore_wasm.cpp) + qt_internal_extend_target(Core CONDITION APPLE SOURCES global/qoperatingsystemversion_darwin.mm diff --git a/src/corelib/kernel/qcore_wasm.cpp b/src/corelib/kernel/qcore_wasm.cpp new file mode 100644 index 00000000000..5c8fac59b0e --- /dev/null +++ b/src/corelib/kernel/qcore_wasm.cpp @@ -0,0 +1,45 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include + +#include + +#if !defined(Q_OS_WASM) +static_assert(false, "This is a wasm-only file."); +#endif // !defined(Q_OS_WASM) + +QT_BEGIN_NAMESPACE + +/*! + Converts the DOMRect (https://www.w3.org/TR/geometry-1/) \a domRect to QRectF. The behavior is + undefined if the provided parameter is not a DOMRect. + + \since 6.5 + \ingroup platform-type-conversions + + \sa toDOMRect() +*/ +QRectF QRectF::fromDOMRect(emscripten::val domRect) +{ + Q_ASSERT_X(domRect["constructor"]["name"].as() == "DOMRect", Q_FUNC_INFO, + "Passed object is not a DOMRect"); + + return QRectF(domRect["left"].as(), domRect["top"].as(), + domRect["width"].as(), domRect["height"].as()); +} + +/*! + Converts this object to a DOMRect (https://www.w3.org/TR/geometry-1/). + + \since 6.5 + \ingroup platform-type-conversions + + \sa fromDOMRect() +*/ +emscripten::val QRectF::toDOMRect() const +{ + return emscripten::val::global("DOMRect").new_(left(), top(), width(), height()); +} + +QT_END_NAMESPACE diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h index d114fa7ae01..e69a217f48a 100644 --- a/src/corelib/tools/qrect.h +++ b/src/corelib/tools/qrect.h @@ -16,6 +16,11 @@ #if defined(Q_OS_DARWIN) || defined(Q_QDOC) struct CGRect; #endif +#if defined(Q_OS_WASM) || defined(Q_QDOC) +namespace emscripten { +class val; +} +#endif QT_BEGIN_NAMESPACE @@ -586,6 +591,11 @@ public: [[nodiscard]] CGRect toCGRect() const noexcept; #endif +#if defined(Q_OS_WASM) || defined(Q_QDOC) + [[nodiscard]] static QRectF fromDOMRect(emscripten::val domRect); + [[nodiscard]] emscripten::val toDOMRect() const; +#endif + private: qreal xp; qreal yp;