From 906a235c9d37f0e813f814d61fa342ffa09b9f6d Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Thu, 16 Jan 2020 11:13:36 +0100 Subject: Make QtQuick::Item::mapFrom/ToGlobal also accept point [ChangeLog][QtQuick][Item] Item.mapToGlobal() and mapFromGlobal() now work with QPointF as well as with raw numeric coordinates. Change-Id: I6a335c95a014bc77a927c781586619e62dce2f02 Reviewed-by: Shawn Rutledge --- src/quick/items/qquickitem.cpp | 74 ++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 24 deletions(-) (limited to 'src/quick/items/qquickitem.cpp') diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index b5932c6f80..59684d15a8 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -4638,6 +4638,52 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const args->setReturnValue(rv.asReturnedValue()); } +static bool unwrapMapFromToFromGlobalArgs(QQmlV4Function *args, const QQuickItem *itemForWarning, const QString &functionNameForWarning, qreal *x, qreal *y) +{ + QV4::ExecutionEngine *v4 = args->v4engine(); + if (args->length() != 1 && args->length() != 2) { + v4->throwTypeError(); + return false; + } + + QV4::Scope scope(v4); + + if (args->length() == 1) { + QV4::ScopedValue sv(scope, (*args)[0]); + if (sv->isNull()) { + qmlWarning(itemForWarning) << functionNameForWarning << "given argument \"" << sv->toQStringNoThrow() + << "\" which is not a point"; + v4->throwTypeError(); + return false; + } + const QV4::Scoped variantWrapper(scope, sv->as()); + const QVariant v = variantWrapper ? variantWrapper->toVariant() : QVariant(); + if (v.canConvert()) { + const QPointF p = v.toPointF(); + *x = p.x(); + *y = p.y(); + } else { + qmlWarning(itemForWarning) << functionNameForWarning << "given argument \"" << sv->toQStringNoThrow() + << "\" which is not a point"; + v4->throwTypeError(); + return false; + } + } else { + QV4::ScopedValue vx(scope, (*args)[0]); + QV4::ScopedValue vy(scope, (*args)[1]); + + if (!vx->isNumber() || !vy->isNumber()) { + v4->throwTypeError(); + return false; + } + + *x = vx->asDouble(); + *y = vy->asDouble(); + } + + return true; +} + /*! \since 5.7 \qmlmethod object QtQuick::Item::mapFromGlobal(real x, real y) @@ -4653,22 +4699,12 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const void QQuickItem::mapFromGlobal(QQmlV4Function *args) const { QV4::ExecutionEngine *v4 = args->v4engine(); - if (args->length() != 2) { - v4->throwTypeError(); - return; - } - QV4::Scope scope(v4); - QV4::ScopedValue vx(scope, (*args)[0]); - QV4::ScopedValue vy(scope, (*args)[1]); - if (!vx->isNumber() || !vy->isNumber()) { - v4->throwTypeError(); + qreal x, y; + if (!unwrapMapFromToFromGlobalArgs(args, this, QStringLiteral("mapFromGlobal()"), &x, &y)) return; - } - qreal x = vx->asDouble(); - qreal y = vy->asDouble(); QVariant result = mapFromGlobal(QPointF(x, y)); QV4::ScopedObject rv(scope, v4->fromVariant(result)); @@ -4690,22 +4726,12 @@ void QQuickItem::mapFromGlobal(QQmlV4Function *args) const void QQuickItem::mapToGlobal(QQmlV4Function *args) const { QV4::ExecutionEngine *v4 = args->v4engine(); - if (args->length() != 2) { - v4->throwTypeError(); - return; - } - QV4::Scope scope(v4); - QV4::ScopedValue vx(scope, (*args)[0]); - QV4::ScopedValue vy(scope, (*args)[1]); - if (!vx->isNumber() || !vy->isNumber()) { - v4->throwTypeError(); + qreal x, y; + if (!unwrapMapFromToFromGlobalArgs(args, this, QStringLiteral("mapFromGlobal()"), &x, &y)) return; - } - qreal x = vx->asDouble(); - qreal y = vy->asDouble(); QVariant result = mapToGlobal(QPointF(x, y)); QV4::ScopedObject rv(scope, v4->fromVariant(result)); -- cgit v1.2.3