From 81952293be3328dc2f62f557478042d1e8d04e0d Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Fri, 5 Mar 2010 09:46:34 +1000 Subject: Add mapFromItem() and mapToItem() in QDeclarativeItem. Task-number: QT-2385 --- src/declarative/graphicsitems/qdeclarativeitem.cpp | 54 ++++++++++++++++++++ src/declarative/graphicsitems/qdeclarativeitem.h | 3 ++ .../qdeclarativeitem/data/mapCoordinates.qml | 43 ++++++++++++++++ .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 58 ++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 tests/auto/declarative/qdeclarativeitem/data/mapCoordinates.qml diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index 5014fd8..cb3f542 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -43,6 +43,7 @@ #include "qdeclarativeitem.h" #include "qdeclarativeevents_p_p.h" +#include #include #include @@ -51,6 +52,7 @@ #include #include #include +#include #include #include @@ -2162,6 +2164,58 @@ void QDeclarativeItem::setKeepMouseGrab(bool keep) } /*! + \qmlmethod object Item::mapFromItem(Item item, int x, int y) + + Maps the point (\a x, \a y), which is in \a item's coordinate system, to + this item's coordinate system, and returns an object with \c x and \c y + properties matching the mapped cooordinate. + + If \a item is a \c null value, this maps the point from the coordinate + system of the root QML view. +*/ +QScriptValue QDeclarativeItem::mapFromItem(const QScriptValue &item, int x, int y) const +{ + QScriptValue sv = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(this))->newObject(); + QDeclarativeItem *itemObj = qobject_cast(item.toQObject()); + if (!itemObj && !item.isNull()) { + qWarning().nospace() << "mapFromItem() given argument " << item.toString() << " which is neither null nor an Item"; + return 0; + } + + // If QGraphicsItem::mapFromItem() is called with 0, behaves the same as mapFromScene() + QPointF p = qobject_cast(this)->mapFromItem(itemObj, x, y); + sv.setProperty("x", p.x()); + sv.setProperty("y", p.y()); + return sv; +} + +/*! + \qmlmethod object Item::mapToItem(Item item, int x, int y) + + Maps the point (\a x, \a y), which is in this item's coordinate system, to + \a item's coordinate system, and returns an object with \c x and \c y + properties matching the mapped cooordinate. + + If \a item is a \c null value, this maps \a x and \a y to the coordinate + system of the root QML view. +*/ +QScriptValue QDeclarativeItem::mapToItem(const QScriptValue &item, int x, int y) const +{ + QScriptValue sv = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(this))->newObject(); + QDeclarativeItem *itemObj = qobject_cast(item.toQObject()); + if (!itemObj && !item.isNull()) { + qWarning().nospace() << "mapToItem() given argument " << item.toString() << " which is neither null nor an Item"; + return 0; + } + + // If QGraphicsItem::mapToItem() is called with 0, behaves the same as mapToScene() + QPointF p = qobject_cast(this)->mapToItem(itemObj, x, y); + sv.setProperty("x", p.x()); + sv.setProperty("y", p.y()); + return sv; +} + +/*! \internal This function emits the \e focusChanged signal. diff --git a/src/declarative/graphicsitems/qdeclarativeitem.h b/src/declarative/graphicsitems/qdeclarativeitem.h index 3ae404d..f6fedc7 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.h +++ b/src/declarative/graphicsitems/qdeclarativeitem.h @@ -159,6 +159,9 @@ public: bool keepMouseGrab() const; void setKeepMouseGrab(bool); + Q_INVOKABLE QScriptValue mapFromItem(const QScriptValue &item, int x, int y) const; + Q_INVOKABLE QScriptValue mapToItem(const QScriptValue &item, int x, int y) const; + QDeclarativeAnchorLine left() const; QDeclarativeAnchorLine right() const; QDeclarativeAnchorLine horizontalCenter() const; diff --git a/tests/auto/declarative/qdeclarativeitem/data/mapCoordinates.qml b/tests/auto/declarative/qdeclarativeitem/data/mapCoordinates.qml new file mode 100644 index 0000000..40a2106 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/mapCoordinates.qml @@ -0,0 +1,43 @@ +import Qt 4.6 + +Item { + id: root; objectName: "root" + width: 200; height: 200 + + Item { id: itemA; objectName: "itemA"; x: 50; y: 50 } + + Item { + x: 50; y: 50 + Item { id: itemB; objectName: "itemB"; x: 100; y: 100 } + } + + function mapAToB(x, y) { + var pos = itemA.mapToItem(itemB, x, y) + return Qt.point(pos.x, pos.y) + } + + function mapAFromB(x, y) { + var pos = itemA.mapFromItem(itemB, x, y) + return Qt.point(pos.x, pos.y) + } + + function mapAToNull(x, y) { + var pos = itemA.mapToItem(null, x, y) + return Qt.point(pos.x, pos.y) + } + + function mapAFromNull(x, y) { + var pos = itemA.mapFromItem(null, x, y) + return Qt.point(pos.x, pos.y) + } + + function checkMapAToInvalid(x, y) { + var pos = itemA.mapToItem(1122, x, y) + return pos.x == undefined && pos.y == undefined + } + + function checkMapAFromInvalid(x, y) { + var pos = itemA.mapFromItem(1122, x, y) + return pos.x == undefined && pos.y == undefined + } +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index 36dcf1f..3e69db9 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -58,6 +58,8 @@ private slots: void keyNavigation(); void smooth(); void clip(); + void mapCoordinates(); + void mapCoordinates_data(); private: template @@ -288,6 +290,62 @@ void tst_QDeclarativeItem::clip() QCOMPARE(spy.count(),2); } +void tst_QDeclarativeItem::mapCoordinates() +{ + QFETCH(int, x); + QFETCH(int, y); + + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setFixedSize(300, 300); + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/mapCoordinates.qml")); + canvas->show(); + qApp->processEvents(); + + QDeclarativeItem *root = qobject_cast(canvas->rootObject()); + QVERIFY(root != 0); + QDeclarativeItem *a = findItem(canvas->rootObject(), "itemA"); + QVERIFY(a != 0); + QDeclarativeItem *b = findItem(canvas->rootObject(), "itemB"); + QVERIFY(b != 0); + + QVariant result; + + QVERIFY(QMetaObject::invokeMethod(root, "mapAToB", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value(), qobject_cast(a)->mapToItem(b, x, y)); + + QVERIFY(QMetaObject::invokeMethod(root, "mapAFromB", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value(), qobject_cast(a)->mapFromItem(b, x, y)); + + QVERIFY(QMetaObject::invokeMethod(root, "mapAToNull", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value(), qobject_cast(a)->mapToScene(x, y)); + + QVERIFY(QMetaObject::invokeMethod(root, "mapAFromNull", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value(), qobject_cast(a)->mapFromScene(x, y)); + + QTest::ignoreMessage(QtWarningMsg, "mapToItem() given argument \"1122\" which is neither null nor an Item"); + QVERIFY(QMetaObject::invokeMethod(root, "checkMapAToInvalid", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QVERIFY(result.toBool()); + + QTest::ignoreMessage(QtWarningMsg, "mapFromItem() given argument \"1122\" which is neither null nor an Item"); + QVERIFY(QMetaObject::invokeMethod(root, "checkMapAFromInvalid", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QVERIFY(result.toBool()); +} + +void tst_QDeclarativeItem::mapCoordinates_data() +{ + QTest::addColumn("x"); + QTest::addColumn("y"); + + for (int i=-20; i<=20; i+=10) + QTest::newRow(QTest::toString(i)) << i << i; +} + template T *tst_QDeclarativeItem::findItem(QGraphicsObject *parent, const QString &objectName) { -- cgit v0.12