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 <paul.wicking@qt.io>
This commit is contained in:
Mikolaj Boc 2022-10-03 16:19:42 +02:00
parent fda5061abd
commit 9c6b8ea290
3 changed files with 59 additions and 0 deletions

View File

@ -574,6 +574,10 @@ qt_internal_extend_target(Core CONDITION WIN32
userenv userenv
) )
qt_internal_extend_target(Core CONDITION WASM
SOURCES
kernel/qcore_wasm.cpp)
qt_internal_extend_target(Core CONDITION APPLE qt_internal_extend_target(Core CONDITION APPLE
SOURCES SOURCES
global/qoperatingsystemversion_darwin.mm global/qoperatingsystemversion_darwin.mm

View File

@ -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 <QtCore/qrect.h>
#include <emscripten/val.h>
#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<std::string>() == "DOMRect", Q_FUNC_INFO,
"Passed object is not a DOMRect");
return QRectF(domRect["left"].as<qreal>(), domRect["top"].as<qreal>(),
domRect["width"].as<qreal>(), domRect["height"].as<qreal>());
}
/*!
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

View File

@ -16,6 +16,11 @@
#if defined(Q_OS_DARWIN) || defined(Q_QDOC) #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
struct CGRect; struct CGRect;
#endif #endif
#if defined(Q_OS_WASM) || defined(Q_QDOC)
namespace emscripten {
class val;
}
#endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -586,6 +591,11 @@ public:
[[nodiscard]] CGRect toCGRect() const noexcept; [[nodiscard]] CGRect toCGRect() const noexcept;
#endif #endif
#if defined(Q_OS_WASM) || defined(Q_QDOC)
[[nodiscard]] static QRectF fromDOMRect(emscripten::val domRect);
[[nodiscard]] emscripten::val toDOMRect() const;
#endif
private: private:
qreal xp; qreal xp;
qreal yp; qreal yp;