diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-09-28 11:00:40 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-13 17:24:41 +0000 |
commit | 1e3df38bcb61054d53b0853fa2648aae87f1044b (patch) | |
tree | a7579d53b693b5c1449bbed5817328f8fb6fe00e /src/quick/items/qquickitem.cpp | |
parent | b4cdfc4a12d2b9ebc79fe17a2f1aff67bd940d71 (diff) |
Item: sort out mapFromGlobal() and mapToGlobal()
The commit message of 08327da, and the change log of Qt 5.7.0 promised
that mapFromGlobal() and mapToGlobal() were available in QML. But since
the revision 7 of QQuickItem was not registered, this was not entirely
true.
Due to a little quirk in the QML engine's handling of revisioned
methods, mapFromGlobal() and mapToGlobal() were only accessible via
an identifier or property, but not directly:
// works
MouseArea { id: ma; onClicked: console.log(ma.mapToGlobal(Qt.point(mouse.x, mouse.y))) }
// ReferenceError: mapToGlobal is not defined
MouseArea { onClicked: console.log(mapToGlobal(Qt.point(mouse.x, mouse.y))) }
Furhermore, this is inconsistent with how mapFromItem() and mapToItem()
are exposed to QML. Even though the C++ versions of these methods take
QPointF and QRectF, the QML versions take 2-4 number specifying x, y,
width and height:
object mapFromItem(Item item, real x, real y)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, real x, real y)
object mapToItem(Item item, real x, real y, real width, real height)
Therefore the signature of mapFromGlobal() and mapToGlobal() should be:
object mapFromGlobal(real x, real y)
object mapToGlobal(real x, real y)
This change implements the QML versions of these methods using
QQmlV4Function, and adds the missing documentation for the QML API.
NOTE: This is QML-only API.
Change-Id: I2ced4836d274c7d1e644ea29fc25dbdd2045001b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/quick/items/qquickitem.cpp')
-rw-r--r-- | src/quick/items/qquickitem.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index 8e53e8b029..fe4d84b334 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -4507,6 +4507,76 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const } /*! + \since 5.7 + \qmlmethod object QtQuick::Item::mapFromGlobal(real x, real y) + + Maps the point (\a x, \a y), which is in the global coordinate system, to the + item's coordinate system, and returns a \l point matching the mapped coordinate. +*/ +/*! + \internal + */ +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(); + return; + } + + qreal x = vx->asDouble(); + qreal y = vy->asDouble(); + QVariant result = mapFromGlobal(QPointF(x, y)); + + QV4::ScopedObject rv(scope, v4->fromVariant(result)); + args->setReturnValue(rv.asReturnedValue()); +} + +/*! + \since 5.7 + \qmlmethod object QtQuick::Item::mapToGlobal(real x, real y) + + Maps the point (\a x, \a y), which is in this item's coordinate system, to the + global coordinate system, and returns a \l point matching the mapped coordinate. +*/ +/*! + \internal + */ +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(); + return; + } + + qreal x = vx->asDouble(); + qreal y = vy->asDouble(); + QVariant result = mapToGlobal(QPointF(x, y)); + + QV4::ScopedObject rv(scope, v4->fromVariant(result)); + args->setReturnValue(rv.asReturnedValue()); +} + +/*! \qmlmethod QtQuick::Item::forceActiveFocus() Forces active focus on the item. |