source: trunk/src/gui/painting/qtransform.h@ 811

Last change on this file since 811 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 11.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41#ifndef QTRANSFORM_H
42#define QTRANSFORM_H
43
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>
52
53#if defined(Q_OS_VXWORKS) && defined(m_type)
54# undef m_type
55#endif
56
57QT_BEGIN_HEADER
58
59QT_BEGIN_NAMESPACE
60
61QT_MODULE(Gui)
62
63class QVariant;
64
65class Q_GUI_EXPORT QTransform
66{
67 Q_ENUMS(TransformationType)
68public:
69 enum TransformationType {
70 TxNone = 0x00,
71 TxTranslate = 0x01,
72 TxScale = 0x02,
73 TxRotate = 0x04,
74 TxShear = 0x08,
75 TxProject = 0x10
76 };
77
78 inline explicit QTransform(Qt::Initialization) : affine(Qt::Uninitialized) {}
79 QTransform();
80 QTransform(qreal h11, qreal h12, qreal h13,
81 qreal h21, qreal h22, qreal h23,
82 qreal h31, qreal h32, qreal h33 = 1.0);
83 QTransform(qreal h11, qreal h12, qreal h21,
84 qreal h22, qreal dx, qreal dy);
85 explicit QTransform(const QMatrix &mtx);
86
87 bool isAffine() const;
88 bool isIdentity() const;
89 bool isInvertible() const;
90 bool isScaling() const;
91 bool isRotating() const;
92 bool isTranslating() const;
93
94 TransformationType type() const;
95
96 inline qreal determinant() const;
97 qreal det() const;
98
99 qreal m11() const;
100 qreal m12() const;
101 qreal m13() const;
102 qreal m21() const;
103 qreal m22() const;
104 qreal m23() const;
105 qreal m31() const;
106 qreal m32() const;
107 qreal m33() const;
108 qreal dx() const;
109 qreal dy() const;
110
111 void setMatrix(qreal m11, qreal m12, qreal m13,
112 qreal m21, qreal m22, qreal m23,
113 qreal m31, qreal m32, qreal m33);
114
115 QTransform inverted(bool *invertible = 0) const;
116 QTransform adjoint() const;
117 QTransform transposed() const;
118
119 QTransform &translate(qreal dx, qreal dy);
120 QTransform &scale(qreal sx, qreal sy);
121 QTransform &shear(qreal sh, qreal sv);
122 QTransform &rotate(qreal a, Qt::Axis axis = Qt::ZAxis);
123 QTransform &rotateRadians(qreal a, Qt::Axis axis = Qt::ZAxis);
124
125 static bool squareToQuad(const QPolygonF &square, QTransform &result);
126 static bool quadToSquare(const QPolygonF &quad, QTransform &result);
127 static bool quadToQuad(const QPolygonF &one,
128 const QPolygonF &two,
129 QTransform &result);
130
131 bool operator==(const QTransform &) const;
132 bool operator!=(const QTransform &) const;
133
134 QTransform &operator*=(const QTransform &);
135 QTransform operator*(const QTransform &o) const;
136
137 QTransform &operator=(const QTransform &);
138
139 operator QVariant() const;
140
141 void reset();
142 QPoint map(const QPoint &p) const;
143 QPointF map(const QPointF &p) const;
144 QLine map(const QLine &l) const;
145 QLineF map(const QLineF &l) const;
146 QPolygonF map(const QPolygonF &a) const;
147 QPolygon map(const QPolygon &a) const;
148 QRegion map(const QRegion &r) const;
149 QPainterPath map(const QPainterPath &p) const;
150 QPolygon mapToPolygon(const QRect &r) const;
151 QRect mapRect(const QRect &) const;
152 QRectF mapRect(const QRectF &) const;
153 void map(int x, int y, int *tx, int *ty) const;
154 void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
155
156 const QMatrix &toAffine() const;
157
158 QTransform &operator*=(qreal div);
159 QTransform &operator/=(qreal div);
160 QTransform &operator+=(qreal div);
161 QTransform &operator-=(qreal div);
162
163 static QTransform fromTranslate(qreal dx, qreal dy);
164 static QTransform fromScale(qreal dx, qreal dy);
165
166private:
167 inline QTransform(qreal h11, qreal h12, qreal h13,
168 qreal h21, qreal h22, qreal h23,
169 qreal h31, qreal h32, qreal h33, bool)
170 : affine(h11, h12, h21, h22, h31, h32, true)
171 , m_13(h13), m_23(h23), m_33(h33)
172 , m_type(TxNone)
173 , m_dirty(TxProject) {}
174 inline QTransform(bool)
175 : affine(true)
176 , m_13(0), m_23(0), m_33(1)
177 , m_type(TxNone)
178 , m_dirty(TxNone) {}
179 inline TransformationType inline_type() const;
180 QMatrix affine;
181 qreal m_13;
182 qreal m_23;
183 qreal m_33;
184
185 mutable uint m_type : 5;
186 mutable uint m_dirty : 5;
187
188 class Private;
189 Private *d;
190};
191Q_DECLARE_TYPEINFO(QTransform, Q_MOVABLE_TYPE);
192
193/******* inlines *****/
194inline QTransform::TransformationType QTransform::inline_type() const
195{
196 if (m_dirty == TxNone)
197 return static_cast<TransformationType>(m_type);
198 return type();
199}
200
201inline bool QTransform::isAffine() const
202{
203 return inline_type() < TxProject;
204}
205inline bool QTransform::isIdentity() const
206{
207 return inline_type() == TxNone;
208}
209
210inline bool QTransform::isInvertible() const
211{
212 return !qFuzzyIsNull(determinant());
213}
214
215inline bool QTransform::isScaling() const
216{
217 return type() >= TxScale;
218}
219inline bool QTransform::isRotating() const
220{
221 return inline_type() >= TxRotate;
222}
223
224inline bool QTransform::isTranslating() const
225{
226 return inline_type() >= TxTranslate;
227}
228
229inline qreal QTransform::determinant() const
230{
231 return affine._m11*(m_33*affine._m22-affine._dy*m_23) -
232 affine._m21*(m_33*affine._m12-affine._dy*m_13)+affine._dx*(m_23*affine._m12-affine._m22*m_13);
233}
234inline qreal QTransform::det() const
235{
236 return determinant();
237}
238inline qreal QTransform::m11() const
239{
240 return affine._m11;
241}
242inline qreal QTransform::m12() const
243{
244 return affine._m12;
245}
246inline qreal QTransform::m13() const
247{
248 return m_13;
249}
250inline qreal QTransform::m21() const
251{
252 return affine._m21;
253}
254inline qreal QTransform::m22() const
255{
256 return affine._m22;
257}
258inline qreal QTransform::m23() const
259{
260 return m_23;
261}
262inline qreal QTransform::m31() const
263{
264 return affine._dx;
265}
266inline qreal QTransform::m32() const
267{
268 return affine._dy;
269}
270inline qreal QTransform::m33() const
271{
272 return m_33;
273}
274inline qreal QTransform::dx() const
275{
276 return affine._dx;
277}
278inline qreal QTransform::dy() const
279{
280 return affine._dy;
281}
282
283inline QTransform &QTransform::operator*=(qreal num)
284{
285 if (num == 1.)
286 return *this;
287 affine._m11 *= num;
288 affine._m12 *= num;
289 m_13 *= num;
290 affine._m21 *= num;
291 affine._m22 *= num;
292 m_23 *= num;
293 affine._dx *= num;
294 affine._dy *= num;
295 m_33 *= num;
296 if (m_dirty < TxScale)
297 m_dirty = TxScale;
298 return *this;
299}
300inline QTransform &QTransform::operator/=(qreal div)
301{
302 if (div == 0)
303 return *this;
304 div = 1/div;
305 return operator*=(div);
306}
307inline QTransform &QTransform::operator+=(qreal num)
308{
309 if (num == 0)
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 m_dirty = TxProject;
321 return *this;
322}
323inline QTransform &QTransform::operator-=(qreal num)
324{
325 if (num == 0)
326 return *this;
327 affine._m11 -= num;
328 affine._m12 -= num;
329 m_13 -= num;
330 affine._m21 -= num;
331 affine._m22 -= num;
332 m_23 -= num;
333 affine._dx -= num;
334 affine._dy -= num;
335 m_33 -= num;
336 m_dirty = TxProject;
337 return *this;
338}
339
340inline bool qFuzzyCompare(const QTransform& t1, const QTransform& t2)
341{
342 return qFuzzyCompare(t1.m11(), t2.m11())
343 && qFuzzyCompare(t1.m12(), t2.m12())
344 && qFuzzyCompare(t1.m13(), t2.m13())
345 && qFuzzyCompare(t1.m21(), t2.m21())
346 && qFuzzyCompare(t1.m22(), t2.m22())
347 && qFuzzyCompare(t1.m23(), t2.m23())
348 && qFuzzyCompare(t1.m31(), t2.m31())
349 && qFuzzyCompare(t1.m32(), t2.m32())
350 && qFuzzyCompare(t1.m33(), t2.m33());
351}
352
353
354/****** stream functions *******************/
355#ifndef QT_NO_DATASTREAM
356Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTransform &);
357Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTransform &);
358#endif
359
360#ifndef QT_NO_DEBUG_STREAM
361Q_GUI_EXPORT QDebug operator<<(QDebug, const QTransform &);
362#endif
363/****** end stream functions *******************/
364
365// mathematical semantics
366Q_GUI_EXPORT_INLINE QPoint operator*(const QPoint &p, const QTransform &m)
367{ return m.map(p); }
368Q_GUI_EXPORT_INLINE QPointF operator*(const QPointF &p, const QTransform &m)
369{ return m.map(p); }
370Q_GUI_EXPORT_INLINE QLineF operator*(const QLineF &l, const QTransform &m)
371{ return m.map(l); }
372Q_GUI_EXPORT_INLINE QLine operator*(const QLine &l, const QTransform &m)
373{ return m.map(l); }
374Q_GUI_EXPORT_INLINE QPolygon operator *(const QPolygon &a, const QTransform &m)
375{ return m.map(a); }
376Q_GUI_EXPORT_INLINE QPolygonF operator *(const QPolygonF &a, const QTransform &m)
377{ return m.map(a); }
378Q_GUI_EXPORT_INLINE QRegion operator *(const QRegion &r, const QTransform &m)
379{ return m.map(r); }
380Q_GUI_EXPORT_INLINE QPainterPath operator *(const QPainterPath &p, const QTransform &m)
381{ return m.map(p); }
382
383Q_GUI_EXPORT_INLINE QTransform operator *(const QTransform &a, qreal n)
384{ QTransform t(a); t *= n; return t; }
385Q_GUI_EXPORT_INLINE QTransform operator /(const QTransform &a, qreal n)
386{ QTransform t(a); t /= n; return t; }
387Q_GUI_EXPORT_INLINE QTransform operator +(const QTransform &a, qreal n)
388{ QTransform t(a); t += n; return t; }
389Q_GUI_EXPORT_INLINE QTransform operator -(const QTransform &a, qreal n)
390{ QTransform t(a); t -= n; return t; }
391
392QT_END_NAMESPACE
393
394QT_END_HEADER
395
396#endif
Note: See TracBrowser for help on using the repository browser.