diff options
author | Nikita Krupenko <krnekit@gmail.com> | 2015-09-26 03:59:00 +0300 |
---|---|---|
committer | Nikita Krupenko <krnekit@gmail.com> | 2015-12-10 13:17:46 +0000 |
commit | 08327da795ee8c4b26441cc0de1a5c49bd5f2b39 (patch) | |
tree | 6eb2a84169fe1f06523ad66de9ad15bf747714d3 /src | |
parent | 3f7b86b563eb92a813135cb35285dec1f05b51ef (diff) |
Add support for mapping between item's and global screen coordinates
Added to Item mapToGlobal() and mapFromGlobal() methods, that maps from
item's coordinate system to global screen coordinates and vise versa
and accessible from QML.
This allow to align window (which is a global object) in relation to
Qt Quick item object. For example, this may be helpful to add a popup to
a Qt Quick component.
[ChangeLog][QtQuick][QQuickItem] Added support for mapping item's
coordinates to global screen coordinates and vise versa.
Task-number: QTBUG-28668
Change-Id: I0a90838f26c53b16d7a76953577492a942674572
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/quick/items/qquickitem.cpp | 64 | ||||
-rw-r--r-- | src/quick/items/qquickitem.h | 2 | ||||
-rw-r--r-- | src/quick/items/qquickitem_p.h | 2 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index ae7b197619..bea0e7bd57 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -3045,6 +3045,28 @@ void QQuickItemPrivate::itemToParentTransform(QTransform &t) const } /*! + Returns a transform that maps points from window space into global space. +*/ +QTransform QQuickItemPrivate::windowToGlobalTransform() const +{ + QPoint quickWidgetOffset; + QWindow *renderWindow = QQuickRenderControl::renderWindowFor(window, &quickWidgetOffset); + QPointF pos = (renderWindow ? renderWindow : window)->mapToGlobal(quickWidgetOffset); + return QTransform::fromTranslate(pos.x(), pos.y()); +} + +/*! + Returns a transform that maps points from global space into window space. +*/ +QTransform QQuickItemPrivate::globalToWindowTransform() const +{ + QPoint quickWidgetOffset; + QWindow *renderWindow = QQuickRenderControl::renderWindowFor(window, &quickWidgetOffset); + QPointF pos = (renderWindow ? renderWindow : window)->mapToGlobal(quickWidgetOffset); + return QTransform::fromTranslate(-pos.x(), -pos.y()); +} + +/*! Returns true if construction of the QML component is complete; otherwise returns false. @@ -7281,6 +7303,27 @@ QPointF QQuickItem::mapToScene(const QPointF &point) const } /*! + Maps the given \a point in this item's coordinate system to the equivalent + point within global screen coordinate system, and returns the mapped + coordinate. + + For example, this may be helpful to add a popup to a Qt Quick component. + + \note Window positioning is done by the window manager and this value is + treated only as a hint. So, the resulting window position may differ from + what is expected. + + \since 5.7 + + \sa {Concepts - Visual Coordinates in Qt Quick} +*/ +QPointF QQuickItem::mapToGlobal(const QPointF &point) const +{ + Q_D(const QQuickItem); + return d->windowToGlobalTransform().map(mapToScene(point)); +} + +/*! Maps the given \a rect in this item's coordinate system to the equivalent rectangular area within \a item's coordinate system, and returns the mapped rectangle value. @@ -7342,6 +7385,27 @@ QPointF QQuickItem::mapFromScene(const QPointF &point) const } /*! + Maps the given \a point in the global screen coordinate system to the + equivalent point within this item's coordinate system, and returns the + mapped coordinate. + + For example, this may be helpful to add a popup to a Qt Quick component. + + \note Window positioning is done by the window manager and this value is + treated only as a hint. So, the resulting window position may differ from + what is expected. + + \since 5.7 + + \sa {Concepts - Visual Coordinates in Qt Quick} +*/ +QPointF QQuickItem::mapFromGlobal(const QPointF &point) const +{ + Q_D(const QQuickItem); + return mapFromScene(d->globalToWindowTransform().map(point)); +} + +/*! Maps the given \a rect in \a item's coordinate system to the equivalent rectangular area within this item's coordinate system, and returns the mapped rectangle value. diff --git a/src/quick/items/qquickitem.h b/src/quick/items/qquickitem.h index 50d691b1b6..013128e95f 100644 --- a/src/quick/items/qquickitem.h +++ b/src/quick/items/qquickitem.h @@ -313,10 +313,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; 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; QRectF mapRectFromItem(const QQuickItem *item, const QRectF &rect) const; QRectF mapRectFromScene(const QRectF &rect) const; diff --git a/src/quick/items/qquickitem_p.h b/src/quick/items/qquickitem_p.h index 634aa2b658..127352aaa7 100644 --- a/src/quick/items/qquickitem_p.h +++ b/src/quick/items/qquickitem_p.h @@ -503,6 +503,8 @@ public: QTransform windowToItemTransform() const; QTransform itemToWindowTransform() const; void itemToParentTransform(QTransform &) const; + QTransform globalToWindowTransform() const; + QTransform windowToGlobalTransform() const; static bool focusNextPrev(QQuickItem *item, bool forward); static QQuickItem *nextTabChildItem(const QQuickItem *item, int start); |