1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QMATRIX_H
41#define QMATRIX_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qpolygon.h>
45#include <QtGui/qregion.h>
46#include <QtGui/qwindowdefs.h>
47#include <QtCore/qline.h>
48#include <QtCore/qpoint.h>
49#include <QtCore/qrect.h>
50
51QT_BEGIN_NAMESPACE
52
53
54class QPainterPath;
55class QVariant;
56
57class Q_GUI_EXPORT QMatrix // 2D transform matrix
58{
59public:
60 inline explicit QMatrix(Qt::Initialization) {}
61 QMatrix();
62 QMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
63 qreal dx, qreal dy);
64
65#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
66 // ### Qt 6: remove; the compiler-generated ones are fine!
67 QMatrix &operator=(QMatrix &&other) noexcept // = default
68 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QMatrix)); return *this; }
69 QMatrix &operator=(const QMatrix &) noexcept; // = default
70 QMatrix(QMatrix &&other) noexcept // = default
71 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QMatrix)); }
72 QMatrix(const QMatrix &other) noexcept; // = default
73#endif
74
75 void setMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
76 qreal dx, qreal dy);
77
78 qreal m11() const { return _m11; }
79 qreal m12() const { return _m12; }
80 qreal m21() const { return _m21; }
81 qreal m22() const { return _m22; }
82 qreal dx() const { return _dx; }
83 qreal dy() const { return _dy; }
84
85 void map(int x, int y, int *tx, int *ty) const;
86 void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
87 QRect mapRect(const QRect &) const;
88 QRectF mapRect(const QRectF &) const;
89
90 QPoint map(const QPoint &p) const;
91 QPointF map(const QPointF&p) const;
92 QLine map(const QLine &l) const;
93 QLineF map(const QLineF &l) const;
94 QPolygonF map(const QPolygonF &a) const;
95 QPolygon map(const QPolygon &a) const;
96 QRegion map(const QRegion &r) const;
97 QPainterPath map(const QPainterPath &p) const;
98 QPolygon mapToPolygon(const QRect &r) const;
99
100 void reset();
101 inline bool isIdentity() const;
102
103 QMatrix &translate(qreal dx, qreal dy);
104 QMatrix &scale(qreal sx, qreal sy);
105 QMatrix &shear(qreal sh, qreal sv);
106 QMatrix &rotate(qreal a);
107
108 bool isInvertible() const { return !qFuzzyIsNull(_m11*_m22 - _m12*_m21); }
109 qreal determinant() const { return _m11*_m22 - _m12*_m21; }
110
111 Q_REQUIRED_RESULT QMatrix inverted(bool *invertible = nullptr) const;
112
113 bool operator==(const QMatrix &) const;
114 bool operator!=(const QMatrix &) const;
115
116 QMatrix &operator*=(const QMatrix &);
117 QMatrix operator*(const QMatrix &o) const;
118
119 operator QVariant() const;
120
121private:
122 inline QMatrix(bool)
123 : _m11(1.)
124 , _m12(0.)
125 , _m21(0.)
126 , _m22(1.)
127 , _dx(0.)
128 , _dy(0.) {}
129 inline QMatrix(qreal am11, qreal am12, qreal am21, qreal am22, qreal adx, qreal ady, bool)
130 : _m11(am11)
131 , _m12(am12)
132 , _m21(am21)
133 , _m22(am22)
134 , _dx(adx)
135 , _dy(ady) {}
136 friend class QTransform;
137 qreal _m11, _m12;
138 qreal _m21, _m22;
139 qreal _dx, _dy;
140};
141Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE);
142
143Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QMatrix &key, uint seed = 0) noexcept;
144
145// mathematical semantics
146inline QPoint operator*(const QPoint &p, const QMatrix &m)
147{ return m.map(p); }
148inline QPointF operator*(const QPointF &p, const QMatrix &m)
149{ return m.map(p); }
150inline QLineF operator*(const QLineF &l, const QMatrix &m)
151{ return m.map(l); }
152inline QLine operator*(const QLine &l, const QMatrix &m)
153{ return m.map(l); }
154inline QPolygon operator *(const QPolygon &a, const QMatrix &m)
155{ return m.map(a); }
156inline QPolygonF operator *(const QPolygonF &a, const QMatrix &m)
157{ return m.map(a); }
158inline QRegion operator *(const QRegion &r, const QMatrix &m)
159{ return m.map(r); }
160Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m);
161
162inline bool QMatrix::isIdentity() const
163{
164 return qFuzzyIsNull(_m11 - 1) && qFuzzyIsNull(_m22 - 1) && qFuzzyIsNull(_m12)
165 && qFuzzyIsNull(_m21) && qFuzzyIsNull(_dx) && qFuzzyIsNull(_dy);
166}
167
168inline bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
169{
170 return qFuzzyCompare(m1.m11(), m2.m11())
171 && qFuzzyCompare(m1.m12(), m2.m12())
172 && qFuzzyCompare(m1.m21(), m2.m21())
173 && qFuzzyCompare(m1.m22(), m2.m22())
174 && qFuzzyCompare(m1.dx(), m2.dx())
175 && qFuzzyCompare(m1.dy(), m2.dy());
176}
177
178
179/*****************************************************************************
180 QMatrix stream functions
181 *****************************************************************************/
182
183#ifndef QT_NO_DATASTREAM
184Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QMatrix &);
185Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QMatrix &);
186#endif
187
188#ifndef QT_NO_DEBUG_STREAM
189Q_GUI_EXPORT QDebug operator<<(QDebug, const QMatrix &);
190#endif
191
192QT_END_NAMESPACE
193
194#endif // QMATRIX_H
195