diff options
author | Bea Lam <bea.lam@nokia.com> | 2010-03-04 23:46:34 (GMT) |
---|---|---|
committer | Bea Lam <bea.lam@nokia.com> | 2010-03-04 23:46:34 (GMT) |
commit | 81952293be3328dc2f62f557478042d1e8d04e0d (patch) | |
tree | f1d12875e61e6a530bb0f3abfde1e8e07203780c /src/declarative | |
parent | 564baee7e729f1c6f766cecabc4fcc62291e0d0f (diff) | |
download | Qt-81952293be3328dc2f62f557478042d1e8d04e0d.zip Qt-81952293be3328dc2f62f557478042d1e8d04e0d.tar.gz Qt-81952293be3328dc2f62f557478042d1e8d04e0d.tar.bz2 |
Add mapFromItem() and mapToItem() in QDeclarativeItem.
Task-number: QT-2385
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativeitem.cpp | 54 | ||||
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativeitem.h | 3 |
2 files changed, 57 insertions, 0 deletions
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 <private/qdeclarativeengine_p.h> #include <qfxperf_p_p.h> #include <qdeclarativeengine.h> @@ -51,6 +52,7 @@ #include <qdeclarativeview.h> #include <qdeclarativestategroup_p.h> #include <qdeclarativecomponent.h> +#include <qdeclarativeinfo.h> #include <QDebug> #include <QPen> @@ -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<QDeclarativeItem*>(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<QGraphicsItem*>(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<QDeclarativeItem*>(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<QGraphicsItem*>(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; |