aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2016-09-28 11:00:40 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2016-10-13 17:24:41 +0000
commit1e3df38bcb61054d53b0853fa2648aae87f1044b (patch)
treea7579d53b693b5c1449bbed5817328f8fb6fe00e /src
parentb4cdfc4a12d2b9ebc79fe17a2f1aff67bd940d71 (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')
-rw-r--r--src/quick/items/qquickitem.cpp70
-rw-r--r--src/quick/items/qquickitem.h6
-rw-r--r--src/quick/items/qquickitemsmodule.cpp1
3 files changed, 75 insertions, 2 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.
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<QQuickShaderEffectSource, 1>(uri, 2, 6, "ShaderEffectSource");
+ qmlRegisterType<QQuickItem, 7>(uri, 2, 7, "Item");
qmlRegisterType<QQuickListView, 7>(uri, 2, 7, "ListView");
qmlRegisterType<QQuickGridView, 7>(uri, 2, 7, "GridView");
qmlRegisterType<QQuickTextInput, 7>(uri, 2, 7, "TextInput");