1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtGui/qmatrix.h>
45 #include <QtGui/qpainterpath.h>
46 #include <QtGui/qpolygon.h>
47 #include <QtGui/qregion.h>
48 #include <QtGui/qwindowdefs.h>
49 #include <QtCore/qline.h>
50 #include <QtCore/qpoint.h>
51 #include <QtCore/qrect.h>
53 #if defined(Q_OS_VXWORKS) && defined(m_type)
65 class Q_GUI_EXPORT QTransform
68 enum TransformationType {
77 inline explicit QTransform(Qt::Initialization) : affine(Qt::Uninitialized) {}
79 QTransform(qreal h11, qreal h12, qreal h13,
80 qreal h21, qreal h22, qreal h23,
81 qreal h31, qreal h32, qreal h33 = 1.0);
82 QTransform(qreal h11, qreal h12, qreal h21,
83 qreal h22, qreal dx, qreal dy);
84 explicit QTransform(const QMatrix &mtx);
86 bool isAffine() const;
87 bool isIdentity() const;
88 bool isInvertible() const;
89 bool isScaling() const;
90 bool isRotating() const;
91 bool isTranslating() const;
93 TransformationType type() const;
95 inline qreal determinant() const;
110 void setMatrix(qreal m11, qreal m12, qreal m13,
111 qreal m21, qreal m22, qreal m23,
112 qreal m31, qreal m32, qreal m33);
114 QTransform inverted(bool *invertible = 0) const;
115 QTransform adjoint() const;
116 QTransform transposed() const;
118 QTransform &translate(qreal dx, qreal dy);
119 QTransform &scale(qreal sx, qreal sy);
120 QTransform &shear(qreal sh, qreal sv);
121 QTransform &rotate(qreal a, Qt::Axis axis = Qt::ZAxis);
122 QTransform &rotateRadians(qreal a, Qt::Axis axis = Qt::ZAxis);
124 static bool squareToQuad(const QPolygonF &square, QTransform &result);
125 static bool quadToSquare(const QPolygonF &quad, QTransform &result);
126 static bool quadToQuad(const QPolygonF &one,
127 const QPolygonF &two,
130 bool operator==(const QTransform &) const;
131 bool operator!=(const QTransform &) const;
133 QTransform &operator*=(const QTransform &);
134 QTransform operator*(const QTransform &o) const;
136 QTransform &operator=(const QTransform &);
138 operator QVariant() const;
141 QPoint map(const QPoint &p) const;
142 QPointF map(const QPointF &p) const;
143 QLine map(const QLine &l) const;
144 QLineF map(const QLineF &l) const;
145 QPolygonF map(const QPolygonF &a) const;
146 QPolygon map(const QPolygon &a) const;
147 QRegion map(const QRegion &r) const;
148 QPainterPath map(const QPainterPath &p) const;
149 QPolygon mapToPolygon(const QRect &r) const;
150 QRect mapRect(const QRect &) const;
151 QRectF mapRect(const QRectF &) const;
152 void map(int x, int y, int *tx, int *ty) const;
153 void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
155 const QMatrix &toAffine() const;
157 QTransform &operator*=(qreal div);
158 QTransform &operator/=(qreal div);
159 QTransform &operator+=(qreal div);
160 QTransform &operator-=(qreal div);
162 static QTransform fromTranslate(qreal dx, qreal dy);
163 static QTransform fromScale(qreal dx, qreal dy);
166 inline QTransform(qreal h11, qreal h12, qreal h13,
167 qreal h21, qreal h22, qreal h23,
168 qreal h31, qreal h32, qreal h33, bool)
169 : affine(h11, h12, h21, h22, h31, h32, true)
170 , m_13(h13), m_23(h23), m_33(h33)
172 , m_dirty(TxProject) {}
173 inline QTransform(bool)
175 , m_13(0), m_23(0), m_33(1)
178 inline TransformationType inline_type() const;
184 mutable uint m_type : 5;
185 mutable uint m_dirty : 5;
190 Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE);
192 /******* inlines *****/
193 inline QTransform::TransformationType QTransform::inline_type() const
195 if (m_dirty == TxNone)
196 return static_cast<TransformationType>(m_type);
200 inline bool QTransform::isAffine() const
202 return inline_type() < TxProject;
204 inline bool QTransform::isIdentity() const
206 return inline_type() == TxNone;
209 inline bool QTransform::isInvertible() const
211 return !qFuzzyIsNull(determinant());
214 inline bool QTransform::isScaling() const
216 return type() >= TxScale;
218 inline bool QTransform::isRotating() const
220 return inline_type() >= TxRotate;
223 inline bool QTransform::isTranslating() const
225 return inline_type() >= TxTranslate;
228 inline qreal QTransform::determinant() const
230 return affine._m11*(m_33*affine._m22-affine._dy*m_23) -
231 affine._m21*(m_33*affine._m12-affine._dy*m_13)+affine._dx*(m_23*affine._m12-affine._m22*m_13);
233 inline qreal QTransform::det() const
235 return determinant();
237 inline qreal QTransform::m11() const
241 inline qreal QTransform::m12() const
245 inline qreal QTransform::m13() const
249 inline qreal QTransform::m21() const
253 inline qreal QTransform::m22() const
257 inline qreal QTransform::m23() const
261 inline qreal QTransform::m31() const
265 inline qreal QTransform::m32() const
269 inline qreal QTransform::m33() const
273 inline qreal QTransform::dx() const
277 inline qreal QTransform::dy() const
282 inline QTransform &QTransform::operator*=(qreal num)
295 if (m_dirty < TxScale)
299 inline QTransform &QTransform::operator/=(qreal div)
304 return operator*=(div);
306 inline QTransform &QTransform::operator+=(qreal num)
322 inline QTransform &QTransform::operator-=(qreal num)
339 inline bool qFuzzyCompare(const QTransform& t1, const QTransform& t2)
341 return qFuzzyCompare(t1.m11(), t2.m11())
342 && qFuzzyCompare(t1.m12(), t2.m12())
343 && qFuzzyCompare(t1.m13(), t2.m13())
344 && qFuzzyCompare(t1.m21(), t2.m21())
345 && qFuzzyCompare(t1.m22(), t2.m22())
346 && qFuzzyCompare(t1.m23(), t2.m23())
347 && qFuzzyCompare(t1.m31(), t2.m31())
348 && qFuzzyCompare(t1.m32(), t2.m32())
349 && qFuzzyCompare(t1.m33(), t2.m33());
353 /****** stream functions *******************/
354 #ifndef QT_NO_DATASTREAM
355 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTransform &);
356 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTransform &);
359 #ifndef QT_NO_DEBUG_STREAM
360 Q_GUI_EXPORT QDebug operator<<(QDebug, const QTransform &);
362 /****** end stream functions *******************/
364 // mathematical semantics
365 Q_GUI_EXPORT_INLINE QPoint operator*(const QPoint &p, const QTransform &m)
367 Q_GUI_EXPORT_INLINE QPointF operator*(const QPointF &p, const QTransform &m)
369 Q_GUI_EXPORT_INLINE QLineF operator*(const QLineF &l, const QTransform &m)
371 Q_GUI_EXPORT_INLINE QLine operator*(const QLine &l, const QTransform &m)
373 Q_GUI_EXPORT_INLINE QPolygon operator *(const QPolygon &a, const QTransform &m)
375 Q_GUI_EXPORT_INLINE QPolygonF operator *(const QPolygonF &a, const QTransform &m)
377 Q_GUI_EXPORT_INLINE QRegion operator *(const QRegion &r, const QTransform &m)
379 Q_GUI_EXPORT_INLINE QPainterPath operator *(const QPainterPath &p, const QTransform &m)
382 Q_GUI_EXPORT_INLINE QTransform operator *(const QTransform &a, qreal n)
383 { QTransform t(a); t *= n; return t; }
384 Q_GUI_EXPORT_INLINE QTransform operator /(const QTransform &a, qreal n)
385 { QTransform t(a); t /= n; return t; }
386 Q_GUI_EXPORT_INLINE QTransform operator +(const QTransform &a, qreal n)
387 { QTransform t(a); t += n; return t; }
388 Q_GUI_EXPORT_INLINE QTransform operator -(const QTransform &a, qreal n)
389 { QTransform t(a); t -= n; return t; }