From 1e3df38bcb61054d53b0853fa2648aae87f1044b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 28 Sep 2016 11:00:40 +0200 Subject: 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 --- src/quick/items/qquickitem.cpp | 70 +++++++++++++++++++++++++++++++++++ src/quick/items/qquickitem.h | 6 ++- src/quick/items/qquickitemsmodule.cpp | 1 + 3 files changed, 75 insertions(+), 2 deletions(-) (limited to 'src/quick/items') 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 @@ -4506,6 +4506,76 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const args->setReturnValue(rv.asReturnedValue()); } +/*! + \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() diff --git a/src/quick/items/qquickitem.h b/src/quick/items/qquickitem.h index c4ee48fdbd..5f5a141922 100644 --- a/src/quick/items/qquickitem.h +++ b/src/quick/items/qquickitem.h @@ -321,12 +321,12 @@ public: QTransform itemTransform(QQuickItem *, bool *) const; QPointF mapToItem(const QQuickItem *item, const QPointF &point) const; QPointF mapToScene(const QPointF &point) const; - Q_REVISION(7) Q_INVOKABLE QPointF mapToGlobal(const QPointF &point) const; + QPointF mapToGlobal(const QPointF &point) const; QRectF mapRectToItem(const QQuickItem *item, const QRectF &rect) const; QRectF mapRectToScene(const QRectF &rect) const; QPointF mapFromItem(const QQuickItem *item, const QPointF &point) const; QPointF mapFromScene(const QPointF &point) const; - Q_REVISION(7) Q_INVOKABLE QPointF mapFromGlobal(const QPointF &point) const; + QPointF mapFromGlobal(const QPointF &point) const; QRectF mapRectFromItem(const QQuickItem *item, const QRectF &rect) const; QRectF mapRectFromScene(const QRectF &rect) const; @@ -334,6 +334,8 @@ public: Q_INVOKABLE void mapFromItem(QQmlV4Function*) const; Q_INVOKABLE void mapToItem(QQmlV4Function*) const; + Q_REVISION(7) Q_INVOKABLE void mapFromGlobal(QQmlV4Function*) const; + Q_REVISION(7) Q_INVOKABLE void mapToGlobal(QQmlV4Function*) const; Q_INVOKABLE void forceActiveFocus(); Q_INVOKABLE void forceActiveFocus(Qt::FocusReason reason); Q_REVISION(1) Q_INVOKABLE QQuickItem *nextItemInFocusChain(bool forward = true); diff --git a/src/quick/items/qquickitemsmodule.cpp b/src/quick/items/qquickitemsmodule.cpp index 08d95119d7..efb86aff11 100644 --- a/src/quick/items/qquickitemsmodule.cpp +++ b/src/quick/items/qquickitemsmodule.cpp @@ -281,6 +281,7 @@ static void qt_quickitems_defineModule(const char *uri, int major, int minor) QQuickEnterKeyAttached::tr("EnterKey is only available via attached properties")); qmlRegisterType(uri, 2, 6, "ShaderEffectSource"); + qmlRegisterType(uri, 2, 7, "Item"); qmlRegisterType(uri, 2, 7, "ListView"); qmlRegisterType(uri, 2, 7, "GridView"); qmlRegisterType(uri, 2, 7, "TextInput"); -- cgit v1.2.3 From 56cf625ad3cf86c4417acf463c4bfb369c7f0916 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 13 Oct 2016 10:57:54 +0200 Subject: QQuickItem: eliminate repeated access to QQmlV4Function::v4engine() Change-Id: Ifc33c17d125ca794a157a87dc5cc1be51c2aaefb Reviewed-by: Simon Hausmann --- src/quick/items/qquickitem.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index fe4d84b334..f371138ea3 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -4363,12 +4363,12 @@ void QQuickItem::polish() */ void QQuickItem::mapFromItem(QQmlV4Function *args) const { + QV4::ExecutionEngine *v4 = args->v4engine(); if (args->length() != 3 && args->length() != 5) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } - QV4::ExecutionEngine *v4 = args->v4engine(); QV4::Scope scope(v4); QV4::ScopedValue item(scope, (*args)[0]); @@ -4382,7 +4382,7 @@ void QQuickItem::mapFromItem(QQmlV4Function *args) const if (!itemObj && !item->isNull()) { qmlInfo(this) << "mapFromItem() given argument \"" << item->toQStringNoThrow() << "\" which is neither null nor an Item"; - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } @@ -4390,7 +4390,7 @@ void QQuickItem::mapFromItem(QQmlV4Function *args) const QV4::ScopedValue vy(scope, (*args)[2]); if (!vx->isNumber() || !vy->isNumber()) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } @@ -4403,7 +4403,7 @@ void QQuickItem::mapFromItem(QQmlV4Function *args) const QV4::ScopedValue vw(scope, (*args)[3]); QV4::ScopedValue vh(scope, (*args)[4]); if (!vw->isNumber() || !vh->isNumber()) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } qreal w = vw->asDouble(); @@ -4451,12 +4451,12 @@ QTransform QQuickItem::itemTransform(QQuickItem *other, bool *ok) const */ void QQuickItem::mapToItem(QQmlV4Function *args) const { + QV4::ExecutionEngine *v4 = args->v4engine(); if (args->length() != 3 && args->length() != 5) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } - QV4::ExecutionEngine *v4 = args->v4engine(); QV4::Scope scope(v4); QV4::ScopedValue item(scope, (*args)[0]); @@ -4470,7 +4470,7 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const if (!itemObj && !item->isNull()) { qmlInfo(this) << "mapToItem() given argument \"" << item->toQStringNoThrow() << "\" which is neither null nor an Item"; - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } @@ -4478,7 +4478,7 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const QV4::ScopedValue vy(scope, (*args)[2]); if (!vx->isNumber() || !vy->isNumber()) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } @@ -4491,7 +4491,7 @@ void QQuickItem::mapToItem(QQmlV4Function *args) const QV4::ScopedValue vw(scope, (*args)[3]); QV4::ScopedValue vh(scope, (*args)[4]); if (!vw->isNumber() || !vh->isNumber()) { - args->v4engine()->throwTypeError(); + v4->throwTypeError(); return; } qreal w = vw->asDouble(); -- cgit v1.2.3 From 0bfcff9d9bd20f3efef6146bd1da6f263b0e24c4 Mon Sep 17 00:00:00 2001 From: Nico Vertriest Date: Wed, 19 Oct 2016 13:57:36 +0200 Subject: Doc: minor typo in doc TextInput QML type Change-Id: Ia04ea9a6afefcc1acda3eb6fb466a1faddfb3309 Reviewed-by: Venugopal Shivashankar Reviewed-by: Frederik Gladhorn --- src/quick/items/qquicktextinput.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/quick/items') diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index 6543a64624..2ad3cd049f 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -766,7 +766,7 @@ void QQuickTextInput::setMaxLength(int ml) (but without actually giving it active focus). It should not be set directly on the item, like in the below QML, - as the specified value will be overridden an lost on focus changes. + as the specified value will be overridden and lost on focus changes. \code TextInput { -- cgit v1.2.3