Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qmatrix.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
42 #ifndef QMATRIX_H
43 #define QMATRIX_H
44
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
52 QT_BEGIN_HEADER
53
54 QT_BEGIN_NAMESPACE
55
56
57 class QPainterPath;
58 class QVariant;
59
60 class Q_GUI_EXPORT QMatrix // 2D transform matrix
61 {
62 public:
63     inline explicit QMatrix(Qt::Initialization) {}
64     QMatrix();
65     QMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
66             qreal dx, qreal dy);
67     QMatrix(const QMatrix &matrix);
68
69     void setMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
70                    qreal dx, qreal dy);
71
72     qreal m11() const { return _m11; }
73     qreal m12() const { return _m12; }
74     qreal m21() const { return _m21; }
75     qreal m22() const { return _m22; }
76     qreal dx() const { return _dx; }
77     qreal dy() const { return _dy; }
78
79     void map(int x, int y, int *tx, int *ty) const;
80     void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
81     QRect mapRect(const QRect &) const;
82     QRectF mapRect(const QRectF &) const;
83
84     QPoint map(const QPoint &p) const;
85     QPointF map(const QPointF&p) const;
86     QLine map(const QLine &l) const;
87     QLineF map(const QLineF &l) const;
88     QPolygonF map(const QPolygonF &a) const;
89     QPolygon map(const QPolygon &a) const;
90     QRegion map(const QRegion &r) const;
91     QPainterPath map(const QPainterPath &p) const;
92     QPolygon mapToPolygon(const QRect &r) const;
93
94     void reset();
95     inline bool isIdentity() const;
96
97     QMatrix &translate(qreal dx, qreal dy);
98     QMatrix &scale(qreal sx, qreal sy);
99     QMatrix &shear(qreal sh, qreal sv);
100     QMatrix &rotate(qreal a);
101
102     bool isInvertible() const { return !qFuzzyIsNull(_m11*_m22 - _m12*_m21); }
103     qreal determinant() const { return _m11*_m22 - _m12*_m21; }
104
105     QMatrix inverted(bool *invertible = 0) const;
106
107     bool operator==(const QMatrix &) const;
108     bool operator!=(const QMatrix &) const;
109
110     QMatrix &operator*=(const QMatrix &);
111     QMatrix operator*(const QMatrix &o) const;
112
113     QMatrix &operator=(const QMatrix &);
114
115     operator QVariant() const;
116
117 private:
118     inline QMatrix(bool)
119             : _m11(1.)
120             , _m12(0.)
121             , _m21(0.)
122             , _m22(1.)
123             , _dx(0.)
124             , _dy(0.) {}
125     inline QMatrix(qreal am11, qreal am12, qreal am21, qreal am22, qreal adx, qreal ady, bool)
126             : _m11(am11)
127             , _m12(am12)
128             , _m21(am21)
129             , _m22(am22)
130             , _dx(adx)
131             , _dy(ady) {}
132     friend class QTransform;
133     qreal _m11, _m12;
134     qreal _m21, _m22;
135     qreal _dx, _dy;
136 };
137 Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE);
138
139 // mathematical semantics
140 inline QPoint operator*(const QPoint &p, const QMatrix &m)
141 { return m.map(p); }
142 inline QPointF operator*(const QPointF &p, const QMatrix &m)
143 { return m.map(p); }
144 inline QLineF operator*(const QLineF &l, const QMatrix &m)
145 { return m.map(l); }
146 inline QLine operator*(const QLine &l, const QMatrix &m)
147 { return m.map(l); }
148 inline QPolygon operator *(const QPolygon &a, const QMatrix &m)
149 { return m.map(a); }
150 inline QPolygonF operator *(const QPolygonF &a, const QMatrix &m)
151 { return m.map(a); }
152 inline QRegion operator *(const QRegion &r, const QMatrix &m)
153 { return m.map(r); }
154 Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m);
155
156 inline bool QMatrix::isIdentity() const
157 {
158     return qFuzzyIsNull(_m11 - 1) && qFuzzyIsNull(_m22 - 1) && qFuzzyIsNull(_m12)
159            && qFuzzyIsNull(_m21) && qFuzzyIsNull(_dx) && qFuzzyIsNull(_dy);
160 }
161
162 inline bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
163 {
164     return qFuzzyCompare(m1.m11(), m2.m11())
165         && qFuzzyCompare(m1.m12(), m2.m12())
166         && qFuzzyCompare(m1.m21(), m2.m21())
167         && qFuzzyCompare(m1.m22(), m2.m22())
168         && qFuzzyCompare(m1.dx(), m2.dx())
169         && qFuzzyCompare(m1.dy(), m2.dy());
170 }
171
172
173 /*****************************************************************************
174  QMatrix stream functions
175  *****************************************************************************/
176
177 #ifndef QT_NO_DATASTREAM
178 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QMatrix &);
179 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QMatrix &);
180 #endif
181
182 #ifndef QT_NO_DEBUG_STREAM
183 Q_GUI_EXPORT QDebug operator<<(QDebug, const QMatrix &);
184 #endif
185
186 QT_END_NAMESPACE
187
188 QT_END_HEADER
189
190 #endif // QMATRIX_H