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#ifndef QTRANSFORM_H
40#define QTRANSFORM_H
41
42#include <QtGui/qtguiglobal.h>
43#include <QtGui/qmatrix.h>
44#include <QtGui/qpainterpath.h>
45#include <QtGui/qpolygon.h>
46#include <QtGui/qregion.h>
47#include <QtGui/qwindowdefs.h>
48#include <QtCore/qline.h>
49#include <QtCore/qpoint.h>
50#include <QtCore/qrect.h>
51
52QT_BEGIN_NAMESPACE
53
54
55class QVariant;
56
57class Q_GUI_EXPORT QTransform
58{
59public:
60 enum TransformationType {
61 TxNone = 0x00,
62 TxTranslate = 0x01,
63 TxScale = 0x02,
64 TxRotate = 0x04,
65 TxShear = 0x08,
66 TxProject = 0x10
67 };
68
69 inline explicit QTransform(Qt::Initialization) : affine(Qt::Uninitialized) {}
70 QTransform();
71 QTransform(qreal h11, qreal h12, qreal h13,
72 qreal h21, qreal h22, qreal h23,
73 qreal h31, qreal h32, qreal h33 = 1.0);
74 QTransform(qreal h11, qreal h12, qreal h21,
75 qreal h22, qreal dx, qreal dy);
76 explicit QTransform(const QMatrix &mtx);
77
78#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
79 // ### Qt 6: remove; the compiler-generated ones are fine!
80 QTransform &operator=(QTransform &&other) Q_DECL_NOTHROW // = default
81 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QTransform)); return *this; }
82 QTransform &operator=(const QTransform &) Q_DECL_NOTHROW; // = default
83 QTransform(QTransform &&other) Q_DECL_NOTHROW // = default
84 : affine(Qt::Uninitialized)
85 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QTransform)); }
86 QTransform(const QTransform &other) Q_DECL_NOTHROW // = default
87 : affine(Qt::Uninitialized)
88 { memcpy(static_cast<void *>(this), static_cast<const void *>(&other), sizeof(QTransform)); }
89#endif
90
91 bool isAffine() const;
92 bool isIdentity() const;
93 bool isInvertible() const;
94 bool isScaling() const;
95 bool isRotating() const;
96 bool isTranslating() const;
97
98 TransformationType type() const;
99
100 inline qreal determinant() const;
101#if QT_DEPRECATED_SINCE(5, 13)
102 QT_DEPRECATED_X("Use determinant() instead")
103 qreal det() const;
104#endif
105
106 qreal m11() const;
107 qreal m12() const;
108 qreal m13() const;
109 qreal m21() const;
110 qreal m22() const;
111 qreal m23() const;
112 qreal m31() const;
113 qreal m32() const;
114 qreal m33() const;
115 qreal dx() const;
116 qreal dy() const;
117
118 void setMatrix(qreal m11, qreal m12, qreal m13,
119 qreal m21, qreal m22, qreal m23,
120 qreal m31, qreal m32, qreal m33);
121
122 Q_REQUIRED_RESULT QTransform inverted(bool *invertible = nullptr) const;
123 Q_REQUIRED_RESULT QTransform adjoint() const;
124 Q_REQUIRED_RESULT QTransform transposed() const;
125
126 QTransform &translate(qreal dx, qreal dy);
127 QTransform &scale(qreal sx, qreal sy);
128 QTransform &shear(qreal sh, qreal sv);
129 QTransform &rotate(qreal a, Qt::Axis axis = Qt::ZAxis);
130 QTransform &rotateRadians(qreal a, Qt::Axis axis = Qt::ZAxis);
131
132 static bool squareToQuad(const QPolygonF &square, QTransform &result);
133 static bool quadToSquare(const QPolygonF &quad, QTransform &result);
134 static bool quadToQuad(const QPolygonF &one,
135 const QPolygonF &two,
136 QTransform &result);
137
138 bool operator==(const QTransform &) const;
139 bool operator!=(const QTransform &) const;
140
141 QTransform &operator*=(const QTransform &);
142 QTransform operator*(const QTransform &o) const;
143
144 operator QVariant() const;
145
146 void reset();
147 QPoint map(const QPoint &p) const;
148 QPointF map(const QPointF &p) const;
149 QLine map(const QLine &l) const;
150 QLineF map(const QLineF &l) const;
151 QPolygonF map(const QPolygonF &a) const;
152 QPolygon map(const QPolygon &a) const;
153 QRegion map(const QRegion &r) const;
154 QPainterPath map(const QPainterPath &p) const;
155 QPolygon mapToPolygon(const QRect &r) const;
156 QRect mapRect(const QRect &) const;
157 QRectF mapRect(const QRectF &) const;
158 void map(int x, int y, int *tx, int *ty) const;
159 void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
160
161 const QMatrix &toAffine() const;
162
163 QTransform &operator*=(qreal div);
164 QTransform &operator/=(qreal div);
165 QTransform &operator+=(qreal div);
166 QTransform &operator-=(qreal div);
167
168 static QTransform fromTranslate(qreal dx, qreal dy);
169 static QTransform fromScale(qreal dx, qreal dy);
170
171private:
172 inline QTransform(qreal h11, qreal h12, qreal h13,
173 qreal h21, qreal h22, qreal h23,
174 qreal h31, qreal h32, qreal h33, bool)
175 : affine(h11, h12, h21, h22, h31, h32, true)
176 , m_13(h13), m_23(h23), m_33(h33)
177 , m_type(TxNone)
178 , m_dirty(TxProject)
179 , d(nullptr)
180 {
181 }
182 inline QTransform(bool)
183 : affine(true)
184 , m_13(0), m_23(0), m_33(1)
185 , m_type(TxNone)
186 , m_dirty(TxNone)
187 , d(nullptr)
188 {
189 }
190 inline TransformationType inline_type() const;
191 QMatrix affine;
192 qreal m_13;
193 qreal m_23;
194 qreal m_33;
195
196 mutable uint m_type : 5;
197 mutable uint m_dirty : 5;
198#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
199 class Private;
200 Private *d;
201#endif
202};
203Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE);
204
205Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QTransform &key, uint seed = 0) Q_DECL_NOTHROW;
206
207/******* inlines *****/
208inline QTransform::TransformationType QTransform::inline_type() const
209{
210 if (m_dirty == TxNone)
211 return static_cast<TransformationType>(m_type);
212 return type();
213}
214
215inline bool QTransform::isAffine() const
216{
217 return inline_type() < TxProject;
218}
219inline bool QTransform::isIdentity() const
220{
221 return inline_type() == TxNone;
222}
223
224inline bool QTransform::isInvertible() const
225{
226 return !qFuzzyIsNull(determinant());
227}
228
229inline bool QTransform::isScaling() const
230{
231 return type() >= TxScale;
232}
233inline bool QTransform::isRotating() const
234{
235 return inline_type() >= TxRotate;
236}
237
238inline bool QTransform::isTranslating() const
239{
240 return inline_type() >= TxTranslate;
241}
242
243inline qreal QTransform::determinant() const
244{
245 return affine._m11*(m_33*affine._m22-affine._dy*m_23) -
246 affine._m21*(m_33*affine._m12-affine._dy*m_13)+affine._dx*(m_23*affine._m12-affine._m22*m_13);
247}
248inline qreal QTransform::det() const
249{
250 return determinant();
251}
252inline qreal QTransform::m11() const
253{
254 return affine._m11;
255}
256inline qreal QTransform::m12() const
257{
258 return affine._m12;
259}
260inline qreal QTransform::m13() const
261{
262 return m_13;
263}
264inline qreal QTransform::m21() const
265{
266 return affine._m21;
267}
268inline qreal QTransform::m22() const
269{
270 return affine._m22;
271}
272inline qreal QTransform::m23() const
273{
274 return m_23;
275}
276inline qreal QTransform::m31() const
277{
278 return affine._dx;
279}
280inline qreal QTransform::m32() const
281{
282 return affine._dy;
283}
284inline qreal QTransform::m33() const
285{
286 return m_33;
287}
288inline qreal QTransform::dx() const
289{
290 return affine._dx;
291}
292inline qreal QTransform::dy() const
293{
294 return affine._dy;
295}
296
297QT_WARNING_PUSH
298QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
299QT_WARNING_DISABLE_GCC("-Wfloat-equal")
300
301inline QTransform &QTransform::operator*=(qreal num)
302{
303 if (num == 1.)
304 return *this;
305 affine._m11 *= num;
306 affine._m12 *= num;
307 m_13 *= num;
308 affine._m21 *= num;
309 affine._m22 *= num;
310 m_23 *= num;
311 affine._dx *= num;
312 affine._dy *= num;
313 m_33 *= num;
314 if (m_dirty < TxScale)
315 m_dirty = TxScale;
316 return *this;
317}
318inline QTransform &QTransform::operator/=(qreal div)
319{
320 if (div == 0)
321 return *this;
322 div = 1/div;
323 return operator*=(div);
324}
325inline QTransform &QTransform::operator+=(qreal num)
326{
327 if (num == 0)
328 return *this;
329 affine._m11 += num;
330 affine._m12 += num;
331 m_13 += num;
332 affine._m21 += num;
333 affine._m22 += num;
334 m_23 += num;
335 affine._dx += num;
336 affine._dy += num;
337 m_33 += num;
338 m_dirty = TxProject;
339 return *this;
340}
341inline QTransform &QTransform::operator-=(qreal num)
342{
343 if (num == 0)
344 return *this;
345 affine._m11 -= num;
346 affine._m12 -= num;
347 m_13 -= num;
348 affine._m21 -= num;
349 affine._m22 -= num;
350 m_23 -= num;
351 affine._dx -= num;
352 affine._dy -= num;
353 m_33 -= num;
354 m_dirty = TxProject;
355 return *this;
356}
357
358QT_WARNING_POP
359
360inline bool qFuzzyCompare(const QTransform& t1, const QTransform& t2)
361{
362 return qFuzzyCompare(t1.m11(), t2.m11())
363 && qFuzzyCompare(t1.m12(), t2.m12())
364 && qFuzzyCompare(t1.m13(), t2.m13())
365 && qFuzzyCompare(t1.m21(), t2.m21())
366 && qFuzzyCompare(t1.m22(), t2.m22())
367 && qFuzzyCompare(t1.m23(), t2.m23())
368 && qFuzzyCompare(t1.m31(), t2.m31())
369 && qFuzzyCompare(t1.m32(), t2.m32())
370 && qFuzzyCompare(t1.m33(), t2.m33());
371}
372
373
374/****** stream functions *******************/
375#ifndef QT_NO_DATASTREAM
376Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTransform &);
377Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTransform &);
378#endif
379
380#ifndef QT_NO_DEBUG_STREAM
381Q_GUI_EXPORT QDebug operator<<(QDebug, const QTransform &);
382#endif
383/****** end stream functions *******************/
384
385// mathematical semantics
386inline QPoint operator*(const QPoint &p, const QTransform &m)
387{ return m.map(p); }
388inline QPointF operator*(const QPointF &p, const QTransform &m)
389{ return m.map(p); }
390inline QLineF operator*(const QLineF &l, const QTransform &m)
391{ return m.map(l); }
392inline QLine operator*(const QLine &l, const QTransform &m)
393{ return m.map(l); }
394inline QPolygon operator *(const QPolygon &a, const QTransform &m)
395{ return m.map(a); }
396inline QPolygonF operator *(const QPolygonF &a, const QTransform &m)
397{ return m.map(a); }
398inline QRegion operator *(const QRegion &r, const QTransform &m)
399{ return m.map(r); }
400inline QPainterPath operator *(const QPainterPath &p, const QTransform &m)
401{ return m.map(p); }
402
403inline QTransform operator *(const QTransform &a, qreal n)
404{ QTransform t(a); t *= n; return t; }
405inline QTransform operator /(const QTransform &a, qreal n)
406{ QTransform t(a); t /= n; return t; }
407inline QTransform operator +(const QTransform &a, qreal n)
408{ QTransform t(a); t += n; return t; }
409inline QTransform operator -(const QTransform &a, qreal n)
410{ QTransform t(a); t -= n; return t; }
411
412QT_END_NAMESPACE
413
414#endif // QTRANSFORM_H
415