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) noexcept // = default
81 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QTransform)); return *this; }
82 QTransform &operator=(const QTransform &) noexcept; // = default
83 QTransform(QTransform &&other) noexcept // = default
84 : affine(Qt::Uninitialized)
85 { memcpy(static_cast<void *>(this), static_cast<void *>(&other), sizeof(QTransform)); }
86 QTransform(const QTransform &other) noexcept // = 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#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
180 , d(nullptr)
181#endif
182 {
183 }
184 inline QTransform(bool)
185 : affine(true)
186 , m_13(0), m_23(0), m_33(1)
187 , m_type(TxNone)
188 , m_dirty(TxNone)
189#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
190 , d(nullptr)
191#endif
192 {
193 }
194 inline TransformationType inline_type() const;
195 QMatrix affine;
196 qreal m_13;
197 qreal m_23;
198 qreal m_33;
199
200 mutable uint m_type : 5;
201 mutable uint m_dirty : 5;
202#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
203 class Private;
204 Private *d;
205#endif
206};
207Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE);
208
209Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QTransform &key, uint seed = 0) noexcept;
210
211/******* inlines *****/
212inline QTransform::TransformationType QTransform::inline_type() const
213{
214 if (m_dirty == TxNone)
215 return static_cast<TransformationType>(m_type);
216 return type();
217}
218
219inline bool QTransform::isAffine() const
220{
221 return inline_type() < TxProject;
222}
223inline bool QTransform::isIdentity() const
224{
225 return inline_type() == TxNone;
226}
227
228inline bool QTransform::isInvertible() const
229{
230 return !qFuzzyIsNull(determinant());
231}
232
233inline bool QTransform::isScaling() const
234{
235 return type() >= TxScale;
236}
237inline bool QTransform::isRotating() const
238{
239 return inline_type() >= TxRotate;
240}
241
242inline bool QTransform::isTranslating() const
243{
244 return inline_type() >= TxTranslate;
245}
246
247inline qreal QTransform::determinant() const
248{
249 return affine._m11*(m_33*affine._m22-affine._dy*m_23) -
250 affine._m21*(m_33*affine._m12-affine._dy*m_13)+affine._dx*(m_23*affine._m12-affine._m22*m_13);
251}
252#if QT_DEPRECATED_SINCE(5, 13)
253inline qreal QTransform::det() const
254{
255 return determinant();
256}
257#endif
258inline qreal QTransform::m11() const
259{
260 return affine._m11;
261}
262inline qreal QTransform::m12() const
263{
264 return affine._m12;
265}
266inline qreal QTransform::m13() const
267{
268 return m_13;
269}
270inline qreal QTransform::m21() const
271{
272 return affine._m21;
273}
274inline qreal QTransform::m22() const
275{
276 return affine._m22;
277}
278inline qreal QTransform::m23() const
279{
280 return m_23;
281}
282inline qreal QTransform::m31() const
283{
284 return affine._dx;
285}
286inline qreal QTransform::m32() const
287{
288 return affine._dy;
289}
290inline qreal QTransform::m33() const
291{
292 return m_33;
293}
294inline qreal QTransform::dx() const
295{
296 return affine._dx;
297}
298inline qreal QTransform::dy() const
299{
300 return affine._dy;
301}
302
303QT_WARNING_PUSH
304QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
305QT_WARNING_DISABLE_GCC("-Wfloat-equal")
306
307inline QTransform &QTransform::operator*=(qreal num)
308{
309 if (num == 1.)
310 return *this;
311 affine._m11 *= num;
312 affine._m12 *= num;
313 m_13 *= num;
314 affine._m21 *= num;
315 affine._m22 *= num;
316 m_23 *= num;
317 affine._dx *= num;
318 affine._dy *= num;
319 m_33 *= num;
320 if (m_dirty < TxScale)
321 m_dirty = TxScale;
322 return *this;
323}
324inline QTransform &QTransform::operator/=(qreal div)
325{
326 if (div == 0)
327 return *this;
328 div = 1/div;
329 return operator*=(div);
330}
331inline QTransform &QTransform::operator+=(qreal num)
332{
333 if (num == 0)
334 return *this;
335 affine._m11 += num;
336 affine._m12 += num;
337 m_13 += num;
338 affine._m21 += num;
339 affine._m22 += num;
340 m_23 += num;
341 affine._dx += num;
342 affine._dy += num;
343 m_33 += num;
344 m_dirty = TxProject;
345 return *this;
346}
347inline QTransform &QTransform::operator-=(qreal num)
348{
349 if (num == 0)
350 return *this;
351 affine._m11 -= num;
352 affine._m12 -= num;
353 m_13 -= num;
354 affine._m21 -= num;
355 affine._m22 -= num;
356 m_23 -= num;
357 affine._dx -= num;
358 affine._dy -= num;
359 m_33 -= num;
360 m_dirty = TxProject;
361 return *this;
362}
363
364QT_WARNING_POP
365
366inline bool qFuzzyCompare(const QTransform& t1, const QTransform& t2)
367{
368 return qFuzzyCompare(t1.m11(), t2.m11())
369 && qFuzzyCompare(t1.m12(), t2.m12())
370 && qFuzzyCompare(t1.m13(), t2.m13())
371 && qFuzzyCompare(t1.m21(), t2.m21())
372 && qFuzzyCompare(t1.m22(), t2.m22())
373 && qFuzzyCompare(t1.m23(), t2.m23())
374 && qFuzzyCompare(t1.m31(), t2.m31())
375 && qFuzzyCompare(t1.m32(), t2.m32())
376 && qFuzzyCompare(t1.m33(), t2.m33());
377}
378
379
380/****** stream functions *******************/
381#ifndef QT_NO_DATASTREAM
382Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTransform &);
383Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTransform &);
384#endif
385
386#ifndef QT_NO_DEBUG_STREAM
387Q_GUI_EXPORT QDebug operator<<(QDebug, const QTransform &);
388#endif
389/****** end stream functions *******************/
390
391// mathematical semantics
392inline QPoint operator*(const QPoint &p, const QTransform &m)
393{ return m.map(p); }
394inline QPointF operator*(const QPointF &p, const QTransform &m)
395{ return m.map(p); }
396inline QLineF operator*(const QLineF &l, const QTransform &m)
397{ return m.map(l); }
398inline QLine operator*(const QLine &l, const QTransform &m)
399{ return m.map(l); }
400inline QPolygon operator *(const QPolygon &a, const QTransform &m)
401{ return m.map(a); }
402inline QPolygonF operator *(const QPolygonF &a, const QTransform &m)
403{ return m.map(a); }
404inline QRegion operator *(const QRegion &r, const QTransform &m)
405{ return m.map(r); }
406inline QPainterPath operator *(const QPainterPath &p, const QTransform &m)
407{ return m.map(p); }
408
409inline QTransform operator *(const QTransform &a, qreal n)
410{ QTransform t(a); t *= n; return t; }
411inline QTransform operator /(const QTransform &a, qreal n)
412{ QTransform t(a); t /= n; return t; }
413inline QTransform operator +(const QTransform &a, qreal n)
414{ QTransform t(a); t += n; return t; }
415inline QTransform operator -(const QTransform &a, qreal n)
416{ QTransform t(a); t -= n; return t; }
417
418QT_END_NAMESPACE
419
420#endif // QTRANSFORM_H
421