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