aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Edmundson <davidedmundson@kde.org>2022-10-13 13:26:21 +0100
committerDavid Edmundson <davidedmundson@kde.org>2022-12-07 10:20:04 +0000
commit139bae68b611b6377288dd00e497b6e25444cc68 (patch)
treef9847fe47dad1b4d67c3403df5d29205b7eaf5b2
parentfc0d46971869e88c3ff82f090c57fb888e9787f5 (diff)
Expose QMatrix mapping functions to JS space
When using a matrix in JS code, being able to map points and rectangles through it is a reasnoble task. We have everything available in the C++ side just not exposed. Change-Id: I64cc3f0b5fef8eeabc618dad24de19321a96b2fc Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r--src/quick/doc/src/qmltypereference.qdoc23
-rw-r--r--src/quick/util/qquickvaluetypes.cpp10
-rw-r--r--src/quick/util/qquickvaluetypes_p.h3
-rw-r--r--tests/auto/qml/qqmlvaluetypes/data/matrix4x4_invokables.qml15
4 files changed, 51 insertions, 0 deletions
diff --git a/src/quick/doc/src/qmltypereference.qdoc b/src/quick/doc/src/qmltypereference.qdoc
index 5f3e220045..a2c913de49 100644
--- a/src/quick/doc/src/qmltypereference.qdoc
+++ b/src/quick/doc/src/qmltypereference.qdoc
@@ -1077,6 +1077,29 @@ console.log(b.toString());
\endcode
\row
+ \li rect mapRect(rect)
+ \li Maps the provided rectangle into the coordinate system defined by this matrix.
+ If rotation or shearing has been specified, this function returns the bounding rectangle.
+ This function was introduced in Qt 6.5.
+ \li \code
+var a = Qt.matrix4x4(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1);
+var b = a.mapRect(Qt.rect(10, 20, 30, 40));
+console.log(b.toString());
+// Qt.rect(20, 40, 60, 80)
+ \endcode
+
+ \row
+ \li point map(point)
+ \li Maps the provided point into the coordinate system defined by this matrix.
+ This function was introduced in Qt 6.5.
+ \li \code
+var a = Qt.matrix4x4(2,0,0,0,0,2,0,0,0,0,1,0,0,0,0,1);
+var b = a.map(10, 20);
+console.log(b.toString());
+// Qt.point(20, 40)
+ \endcode
+
+ \row
\li bool fuzzyEquals(matrix4x4 other, real epsilon)
\li Returns true if \c this matrix4x4 is approximately equal to the \c other matrix4x4.
The approximation will be true if each attribute of \c this is within \c epsilon
diff --git a/src/quick/util/qquickvaluetypes.cpp b/src/quick/util/qquickvaluetypes.cpp
index 2c21c12e98..a5a9424597 100644
--- a/src/quick/util/qquickvaluetypes.cpp
+++ b/src/quick/util/qquickvaluetypes.cpp
@@ -762,6 +762,16 @@ QMatrix4x4 QQuickMatrix4x4ValueType::transposed() const
return v.transposed();
}
+QPointF QQuickMatrix4x4ValueType::map(const QPointF p) const
+{
+ return v.map(p);
+}
+
+QRectF QQuickMatrix4x4ValueType::mapRect(const QRectF r) const
+{
+ return v.mapRect(r);
+}
+
bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const
{
qreal absEps = qAbs(epsilon);
diff --git a/src/quick/util/qquickvaluetypes_p.h b/src/quick/util/qquickvaluetypes_p.h
index 653c488e34..70ad5c2b0d 100644
--- a/src/quick/util/qquickvaluetypes_p.h
+++ b/src/quick/util/qquickvaluetypes_p.h
@@ -334,6 +334,9 @@ public:
Q_INVOKABLE QMatrix4x4 inverted() const;
Q_INVOKABLE QMatrix4x4 transposed() const;
+ Q_INVOKABLE QPointF map(const QPointF p) const;
+ Q_INVOKABLE QRectF mapRect(const QRectF r) const;
+
Q_INVOKABLE bool fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const;
Q_INVOKABLE bool fuzzyEquals(const QMatrix4x4 &m) const;
};
diff --git a/tests/auto/qml/qqmlvaluetypes/data/matrix4x4_invokables.qml b/tests/auto/qml/qqmlvaluetypes/data/matrix4x4_invokables.qml
index 332a09c152..c28901956d 100644
--- a/tests/auto/qml/qqmlvaluetypes/data/matrix4x4_invokables.qml
+++ b/tests/auto/qml/qqmlvaluetypes/data/matrix4x4_invokables.qml
@@ -70,6 +70,20 @@ Item {
return true;
}
+ function testMatrixMapping()
+ {
+ let m = Qt.matrix4x4();
+ m.scale(1, 2, 3);
+
+ if (m.mapRect(Qt.rect(10, 15, 20, 20)) !== Qt.rect(10, 30, 20, 40))
+ return false;
+
+ if (m.map(Qt.point(10, 15)) !== Qt.point(10, 30))
+ return false;
+
+ return true;
+ }
+
Component.onCompleted: {
success = true;
if (m1.times(m2) != Qt.matrix4x4(26, 26, 26, 26, 52, 52, 52, 52, 78, 78, 78, 78, 104, 104, 104, 104)) success = false;
@@ -88,5 +102,6 @@ Item {
if (m1.fuzzyEquals(m2)) success = false;
if (!m1.fuzzyEquals(m2, 10)) success = false;
if (!testTransformation()) success = false;
+ if (!testMatrixMapping()) success = false;
}
}