src/gui/painting/qmatrix.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    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 QT_MODULE(Gui)
       
    57 
       
    58 class QPainterPath;
       
    59 class QVariant;
       
    60 
       
    61 class Q_GUI_EXPORT QMatrix // 2D transform matrix
       
    62 {
       
    63 public:
       
    64     inline explicit QMatrix(Qt::Initialization) {}
       
    65     QMatrix();
       
    66     QMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
       
    67             qreal dx, qreal dy);
       
    68     QMatrix(const QMatrix &matrix);
       
    69 
       
    70     void setMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
       
    71                    qreal dx, qreal dy);
       
    72 
       
    73     qreal m11() const { return _m11; }
       
    74     qreal m12() const { return _m12; }
       
    75     qreal m21() const { return _m21; }
       
    76     qreal m22() const { return _m22; }
       
    77     qreal dx() const { return _dx; }
       
    78     qreal dy() const { return _dy; }
       
    79 
       
    80     void map(int x, int y, int *tx, int *ty) const;
       
    81     void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
       
    82     QRect mapRect(const QRect &) const;
       
    83     QRectF mapRect(const QRectF &) const;
       
    84 
       
    85     QPoint map(const QPoint &p) const;
       
    86     QPointF map(const QPointF&p) const;
       
    87     QLine map(const QLine &l) const;
       
    88     QLineF map(const QLineF &l) const;
       
    89     QPolygonF map(const QPolygonF &a) const;
       
    90     QPolygon map(const QPolygon &a) const;
       
    91     QRegion map(const QRegion &r) const;
       
    92     QPainterPath map(const QPainterPath &p) const;
       
    93     QPolygon mapToPolygon(const QRect &r) const;
       
    94 
       
    95     void reset();
       
    96     inline bool isIdentity() const;
       
    97 
       
    98     QMatrix &translate(qreal dx, qreal dy);
       
    99     QMatrix &scale(qreal sx, qreal sy);
       
   100     QMatrix &shear(qreal sh, qreal sv);
       
   101     QMatrix &rotate(qreal a);
       
   102 
       
   103     bool isInvertible() const { return !qFuzzyIsNull(_m11*_m22 - _m12*_m21); }
       
   104     qreal det() const { return _m11*_m22 - _m12*_m21; }
       
   105 
       
   106     QMatrix inverted(bool *invertible = 0) const;
       
   107 
       
   108     bool operator==(const QMatrix &) const;
       
   109     bool operator!=(const QMatrix &) const;
       
   110 
       
   111     QMatrix &operator*=(const QMatrix &);
       
   112     QMatrix operator*(const QMatrix &o) const;
       
   113 
       
   114     QMatrix &operator=(const QMatrix &);
       
   115 
       
   116     operator QVariant() const;
       
   117 
       
   118 #ifdef QT3_SUPPORT
       
   119     inline QT3_SUPPORT QMatrix invert(bool *invertible=0) const { return inverted(invertible); }
       
   120     inline QT3_SUPPORT QRect map(const QRect &r) const { return mapRect(r); }
       
   121     QT3_SUPPORT QRegion mapToRegion(const QRect &r) const;
       
   122 #endif
       
   123 
       
   124 private:
       
   125     inline QMatrix(bool)
       
   126             : _m11(1.)
       
   127             , _m12(0.)
       
   128             , _m21(0.)
       
   129             , _m22(1.)
       
   130             , _dx(0.)
       
   131             , _dy(0.) {}
       
   132     inline QMatrix(qreal am11, qreal am12, qreal am21, qreal am22, qreal adx, qreal ady, bool)
       
   133             : _m11(am11)
       
   134             , _m12(am12)
       
   135             , _m21(am21)
       
   136             , _m22(am22)
       
   137             , _dx(adx)
       
   138             , _dy(ady) {}
       
   139     friend class QTransform;
       
   140     qreal _m11, _m12;
       
   141     qreal _m21, _m22;
       
   142     qreal _dx, _dy;
       
   143 };
       
   144 Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE);
       
   145 
       
   146 // mathematical semantics
       
   147 Q_GUI_EXPORT_INLINE QPoint operator*(const QPoint &p, const QMatrix &m)
       
   148 { return m.map(p); }
       
   149 Q_GUI_EXPORT_INLINE QPointF operator*(const QPointF &p, const QMatrix &m)
       
   150 { return m.map(p); }
       
   151 Q_GUI_EXPORT_INLINE QLineF operator*(const QLineF &l, const QMatrix &m)
       
   152 { return m.map(l); }
       
   153 Q_GUI_EXPORT_INLINE QLine operator*(const QLine &l, const QMatrix &m)
       
   154 { return m.map(l); }
       
   155 Q_GUI_EXPORT_INLINE QPolygon operator *(const QPolygon &a, const QMatrix &m)
       
   156 { return m.map(a); }
       
   157 Q_GUI_EXPORT_INLINE QPolygonF operator *(const QPolygonF &a, const QMatrix &m)
       
   158 { return m.map(a); }
       
   159 Q_GUI_EXPORT_INLINE QRegion operator *(const QRegion &r, const QMatrix &m)
       
   160 { return m.map(r); }
       
   161 Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m);
       
   162 
       
   163 inline bool QMatrix::isIdentity() const
       
   164 {
       
   165     return qFuzzyIsNull(_m11 - 1) && qFuzzyIsNull(_m22 - 1) && qFuzzyIsNull(_m12)
       
   166            && qFuzzyIsNull(_m21) && qFuzzyIsNull(_dx) && qFuzzyIsNull(_dy);
       
   167 }
       
   168 
       
   169 inline bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
       
   170 {
       
   171     return qFuzzyCompare(m1.m11(), m2.m11())
       
   172         && qFuzzyCompare(m1.m12(), m2.m12())
       
   173         && qFuzzyCompare(m1.m21(), m2.m21())
       
   174         && qFuzzyCompare(m1.m22(), m2.m22())
       
   175         && qFuzzyCompare(m1.dx(), m2.dx())
       
   176         && qFuzzyCompare(m1.dy(), m2.dy());
       
   177 }
       
   178 
       
   179 
       
   180 /*****************************************************************************
       
   181  QMatrix stream functions
       
   182  *****************************************************************************/
       
   183 
       
   184 #ifndef QT_NO_DATASTREAM
       
   185 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QMatrix &);
       
   186 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QMatrix &);
       
   187 #endif
       
   188 
       
   189 #ifndef QT_NO_DEBUG_STREAM
       
   190 Q_GUI_EXPORT QDebug operator<<(QDebug, const QMatrix &);
       
   191 #endif
       
   192 
       
   193 #ifdef QT3_SUPPORT
       
   194 QT_BEGIN_INCLUDE_NAMESPACE
       
   195 #include <QtGui/qwmatrix.h>
       
   196 QT_END_INCLUDE_NAMESPACE
       
   197 #endif
       
   198 
       
   199 QT_END_NAMESPACE
       
   200 
       
   201 QT_END_HEADER
       
   202 
       
   203 #endif // QMATRIX_H