qmatrix.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QMATRIX_H
  40. #define QMATRIX_H
  41. #include <QtGui/qpolygon.h>
  42. #include <QtGui/qregion.h>
  43. #include <QtGui/qwindowdefs.h>
  44. #include <QtCore/qline.h>
  45. #include <QtCore/qpoint.h>
  46. #include <QtCore/qrect.h>
  47. QT_BEGIN_NAMESPACE
  48. class QPainterPath;
  49. class QVariant;
  50. class Q_GUI_EXPORT QMatrix // 2D transform matrix
  51. {
  52. public:
  53. inline explicit QMatrix(Qt::Initialization) {}
  54. QMatrix();
  55. QMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
  56. qreal dx, qreal dy);
  57. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  58. // ### Qt 6: remove; the compiler-generated ones are fine!
  59. QMatrix &operator=(QMatrix &&other) Q_DECL_NOTHROW // = default
  60. { memcpy(this, &other, sizeof(QMatrix)); return *this; }
  61. QMatrix &operator=(const QMatrix &) Q_DECL_NOTHROW; // = default
  62. QMatrix(QMatrix &&other) Q_DECL_NOTHROW // = default
  63. { memcpy(this, &other, sizeof(QMatrix)); }
  64. QMatrix(const QMatrix &other) Q_DECL_NOTHROW; // = default
  65. #endif
  66. void setMatrix(qreal m11, qreal m12, qreal m21, qreal m22,
  67. qreal dx, qreal dy);
  68. qreal m11() const { return _m11; }
  69. qreal m12() const { return _m12; }
  70. qreal m21() const { return _m21; }
  71. qreal m22() const { return _m22; }
  72. qreal dx() const { return _dx; }
  73. qreal dy() const { return _dy; }
  74. void map(int x, int y, int *tx, int *ty) const;
  75. void map(qreal x, qreal y, qreal *tx, qreal *ty) const;
  76. QRect mapRect(const QRect &) const;
  77. QRectF mapRect(const QRectF &) const;
  78. QPoint map(const QPoint &p) const;
  79. QPointF map(const QPointF&p) const;
  80. QLine map(const QLine &l) const;
  81. QLineF map(const QLineF &l) const;
  82. QPolygonF map(const QPolygonF &a) const;
  83. QPolygon map(const QPolygon &a) const;
  84. QRegion map(const QRegion &r) const;
  85. QPainterPath map(const QPainterPath &p) const;
  86. QPolygon mapToPolygon(const QRect &r) const;
  87. void reset();
  88. inline bool isIdentity() const;
  89. QMatrix &translate(qreal dx, qreal dy);
  90. QMatrix &scale(qreal sx, qreal sy);
  91. QMatrix &shear(qreal sh, qreal sv);
  92. QMatrix &rotate(qreal a);
  93. bool isInvertible() const { return !qFuzzyIsNull(_m11*_m22 - _m12*_m21); }
  94. qreal determinant() const { return _m11*_m22 - _m12*_m21; }
  95. QMatrix inverted(bool *invertible = Q_NULLPTR) const Q_REQUIRED_RESULT;
  96. bool operator==(const QMatrix &) const;
  97. bool operator!=(const QMatrix &) const;
  98. QMatrix &operator*=(const QMatrix &);
  99. QMatrix operator*(const QMatrix &o) const;
  100. operator QVariant() const;
  101. private:
  102. inline QMatrix(bool)
  103. : _m11(1.)
  104. , _m12(0.)
  105. , _m21(0.)
  106. , _m22(1.)
  107. , _dx(0.)
  108. , _dy(0.) {}
  109. inline QMatrix(qreal am11, qreal am12, qreal am21, qreal am22, qreal adx, qreal ady, bool)
  110. : _m11(am11)
  111. , _m12(am12)
  112. , _m21(am21)
  113. , _m22(am22)
  114. , _dx(adx)
  115. , _dy(ady) {}
  116. friend class QTransform;
  117. qreal _m11, _m12;
  118. qreal _m21, _m22;
  119. qreal _dx, _dy;
  120. };
  121. Q_DECLARE_TYPEINFO(QMatrix, Q_MOVABLE_TYPE);
  122. Q_GUI_EXPORT Q_DECL_CONST_FUNCTION uint qHash(const QMatrix &key, uint seed = 0) Q_DECL_NOTHROW;
  123. // mathematical semantics
  124. inline QPoint operator*(const QPoint &p, const QMatrix &m)
  125. { return m.map(p); }
  126. inline QPointF operator*(const QPointF &p, const QMatrix &m)
  127. { return m.map(p); }
  128. inline QLineF operator*(const QLineF &l, const QMatrix &m)
  129. { return m.map(l); }
  130. inline QLine operator*(const QLine &l, const QMatrix &m)
  131. { return m.map(l); }
  132. inline QPolygon operator *(const QPolygon &a, const QMatrix &m)
  133. { return m.map(a); }
  134. inline QPolygonF operator *(const QPolygonF &a, const QMatrix &m)
  135. { return m.map(a); }
  136. inline QRegion operator *(const QRegion &r, const QMatrix &m)
  137. { return m.map(r); }
  138. Q_GUI_EXPORT QPainterPath operator *(const QPainterPath &p, const QMatrix &m);
  139. inline bool QMatrix::isIdentity() const
  140. {
  141. return qFuzzyIsNull(_m11 - 1) && qFuzzyIsNull(_m22 - 1) && qFuzzyIsNull(_m12)
  142. && qFuzzyIsNull(_m21) && qFuzzyIsNull(_dx) && qFuzzyIsNull(_dy);
  143. }
  144. inline bool qFuzzyCompare(const QMatrix& m1, const QMatrix& m2)
  145. {
  146. return qFuzzyCompare(m1.m11(), m2.m11())
  147. && qFuzzyCompare(m1.m12(), m2.m12())
  148. && qFuzzyCompare(m1.m21(), m2.m21())
  149. && qFuzzyCompare(m1.m22(), m2.m22())
  150. && qFuzzyCompare(m1.dx(), m2.dx())
  151. && qFuzzyCompare(m1.dy(), m2.dy());
  152. }
  153. /*****************************************************************************
  154. QMatrix stream functions
  155. *****************************************************************************/
  156. #ifndef QT_NO_DATASTREAM
  157. Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QMatrix &);
  158. Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QMatrix &);
  159. #endif
  160. #ifndef QT_NO_DEBUG_STREAM
  161. Q_GUI_EXPORT QDebug operator<<(QDebug, const QMatrix &);
  162. #endif
  163. QT_END_NAMESPACE
  164. #endif // QMATRIX_H