diff options
Diffstat (limited to 'src/gui/painting/qtransform.cpp')
-rw-r--r-- | src/gui/painting/qtransform.cpp | 281 |
1 files changed, 91 insertions, 190 deletions
diff --git a/src/gui/painting/qtransform.cpp b/src/gui/painting/qtransform.cpp index 5fa5d04e54..df57d2c190 100644 --- a/src/gui/painting/qtransform.cpp +++ b/src/gui/painting/qtransform.cpp @@ -54,7 +54,7 @@ static void nanWarning(const char *func) if (t == TxProject) { \ qreal w = (m_matrix[0][2] * FX_ + m_matrix[1][2] * FY_ + m_matrix[2][2]); \ if (w < qreal(Q_NEAR_CLIP)) w = qreal(Q_NEAR_CLIP); \ - w = 1./w; \ + w = qreal(1.)/w; \ nx *= w; \ ny *= w; \ } \ @@ -358,7 +358,7 @@ QTransform &QTransform::translate(qreal dx, qreal dy) if (dx == 0 && dy == 0) return *this; #ifndef QT_NO_DEBUG - if (qIsNaN(dx) | qIsNaN(dy)) { + if (qIsNaN(dx) || qIsNaN(dy)) { nanWarning("translate"); return *this; } @@ -401,7 +401,7 @@ QTransform &QTransform::translate(qreal dx, qreal dy) QTransform QTransform::fromTranslate(qreal dx, qreal dy) { #ifndef QT_NO_DEBUG - if (qIsNaN(dx) | qIsNaN(dy)) { + if (qIsNaN(dx) || qIsNaN(dy)) { nanWarning("fromTranslate"); return QTransform(); } @@ -426,7 +426,7 @@ QTransform & QTransform::scale(qreal sx, qreal sy) if (sx == 1 && sy == 1) return *this; #ifndef QT_NO_DEBUG - if (qIsNaN(sx) | qIsNaN(sy)) { + if (qIsNaN(sx) || qIsNaN(sy)) { nanWarning("scale"); return *this; } @@ -467,7 +467,7 @@ QTransform & QTransform::scale(qreal sx, qreal sy) QTransform QTransform::fromScale(qreal sx, qreal sy) { #ifndef QT_NO_DEBUG - if (qIsNaN(sx) | qIsNaN(sy)) { + if (qIsNaN(sx) || qIsNaN(sy)) { nanWarning("fromScale"); return QTransform(); } @@ -492,7 +492,7 @@ QTransform & QTransform::shear(qreal sh, qreal sv) if (sh == 0 && sv == 0) return *this; #ifndef QT_NO_DEBUG - if (qIsNaN(sh) | qIsNaN(sv)) { + if (qIsNaN(sh) || qIsNaN(sv)) { nanWarning("shear"); return *this; } @@ -533,28 +533,33 @@ QTransform & QTransform::shear(qreal sh, qreal sv) return *this; } -const qreal inv_dist_to_plane = 1. / 1024.; - /*! - \fn QTransform &QTransform::rotate(qreal angle, Qt::Axis axis) + \since 6.5 - Rotates the coordinate system counterclockwise by the given \a angle - about the specified \a axis and returns a reference to the matrix. + Rotates the coordinate system counterclockwise by the given angle \a a + about the specified \a axis at distance \a distanceToPlane from the + screen and returns a reference to the matrix. +//! [transform-rotate-note] Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards. The angle is specified in degrees. +//! [transform-rotate-note] + + If \a distanceToPlane is zero, it will be ignored. This is suitable + for implementing orthographic projections where the z coordinate should + be dropped rather than projected. \sa setMatrix() */ -QTransform & QTransform::rotate(qreal a, Qt::Axis axis) +QTransform & QTransform::rotate(qreal a, Qt::Axis axis, qreal distanceToPlane) { if (a == 0) return *this; #ifndef QT_NO_DEBUG - if (qIsNaN(a)) { + if (qIsNaN(a) || qIsNaN(distanceToPlane)) { nanWarning("rotate"); return *this; } @@ -617,13 +622,16 @@ QTransform & QTransform::rotate(qreal a, Qt::Axis axis) if (m_dirty < TxRotate) m_dirty = TxRotate; } else { + if (!qIsNull(distanceToPlane)) + sina /= distanceToPlane; + QTransform result; if (axis == Qt::YAxis) { result.m_matrix[0][0] = cosa; - result.m_matrix[0][2] = -sina * inv_dist_to_plane; + result.m_matrix[0][2] = -sina; } else { result.m_matrix[1][1] = cosa; - result.m_matrix[1][2] = -sina * inv_dist_to_plane; + result.m_matrix[1][2] = -sina; } result.m_type = TxProject; *this = result * *this; @@ -632,24 +640,49 @@ QTransform & QTransform::rotate(qreal a, Qt::Axis axis) return *this; } +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) +/*! + \overload + + Rotates the coordinate system counterclockwise by the given angle \a a + about the specified \a axis at distance 1024.0 from the screen and + returns a reference to the matrix. + + \include qtransform.cpp transform-rotate-note + + \sa setMatrix +*/ +QTransform &QTransform::rotate(qreal a, Qt::Axis axis) +{ + return rotate(a, axis, 1024.0); +} +#endif + /*! - \fn QTransform & QTransform::rotateRadians(qreal angle, Qt::Axis axis) + \since 6.5 - Rotates the coordinate system counterclockwise by the given \a angle - about the specified \a axis and returns a reference to the matrix. + Rotates the coordinate system counterclockwise by the given angle \a a + about the specified \a axis at distance \a distanceToPlane from the + screen and returns a reference to the matrix. +//! [transform-rotate-radians-note] Note that if you apply a QTransform to a point defined in widget coordinates, the direction of the rotation will be clockwise because the y-axis points downwards. The angle is specified in radians. +//! [transform-rotate-radians-note] + + If \a distanceToPlane is zero, it will be ignored. This is suitable + for implementing orthographic projections where the z coordinate should + be dropped rather than projected. \sa setMatrix() */ -QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis) +QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis, qreal distanceToPlane) { #ifndef QT_NO_DEBUG - if (qIsNaN(a)) { + if (qIsNaN(a) || qIsNaN(distanceToPlane)) { nanWarning("rotateRadians"); return *this; } @@ -700,13 +733,16 @@ QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis) if (m_dirty < TxRotate) m_dirty = TxRotate; } else { + if (!qIsNull(distanceToPlane)) + sina /= distanceToPlane; + QTransform result; if (axis == Qt::YAxis) { result.m_matrix[0][0] = cosa; - result.m_matrix[0][2] = -sina * inv_dist_to_plane; + result.m_matrix[0][2] = -sina; } else { result.m_matrix[1][1] = cosa; - result.m_matrix[1][2] = -sina * inv_dist_to_plane; + result.m_matrix[1][2] = -sina; } result.m_type = TxProject; *this = result * *this; @@ -714,6 +750,24 @@ QTransform & QTransform::rotateRadians(qreal a, Qt::Axis axis) return *this; } +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) +/*! + \overload + + Rotates the coordinate system counterclockwise by the given angle \a a + about the specified \a axis at distance 1024.0 from the screen and + returns a reference to the matrix. + + \include qtransform.cpp transform-rotate-radians-note + + \sa setMatrix() +*/ +QTransform &QTransform::rotateRadians(qreal a, Qt::Axis axis) +{ + return rotateRadians(a, axis, 1024.0); +} +#endif + /*! \fn bool QTransform::operator==(const QTransform &matrix) const Returns \c true if this matrix is equal to the given \a matrix, @@ -1091,30 +1145,8 @@ QPoint QTransform::map(const QPoint &p) const qreal x = 0, y = 0; TransformationType t = inline_type(); - switch(t) { - case TxNone: - x = fx; - y = fy; - break; - case TxTranslate: - x = fx + m_matrix[2][0]; - y = fy + m_matrix[2][1]; - break; - case TxScale: - x = m_matrix[0][0] * fx + m_matrix[2][0]; - y = m_matrix[1][1] * fy + m_matrix[2][1]; - break; - case TxRotate: - case TxShear: - case TxProject: - x = m_matrix[0][0] * fx + m_matrix[1][0] * fy + m_matrix[2][0]; - y = m_matrix[0][1] * fx + m_matrix[1][1] * fy + m_matrix[2][1]; - if (t == TxProject) { - qreal w = 1./(m_matrix[0][2] * fx + m_matrix[1][2] * fy + m_matrix[2][2]); - x *= w; - y *= w; - } - } + MAP(fx, fy, x, y); + return QPoint(qRound(x), qRound(y)); } @@ -1142,30 +1174,8 @@ QPointF QTransform::map(const QPointF &p) const qreal x = 0, y = 0; TransformationType t = inline_type(); - switch(t) { - case TxNone: - x = fx; - y = fy; - break; - case TxTranslate: - x = fx + m_matrix[2][0]; - y = fy + m_matrix[2][1]; - break; - case TxScale: - x = m_matrix[0][0] * fx + m_matrix[2][0]; - y = m_matrix[1][1] * fy + m_matrix[2][1]; - break; - case TxRotate: - case TxShear: - case TxProject: - x = m_matrix[0][0] * fx + m_matrix[1][0] * fy + m_matrix[2][0]; - y = m_matrix[0][1] * fx + m_matrix[1][1] * fy + m_matrix[2][1]; - if (t == TxProject) { - qreal w = 1./(m_matrix[0][2] * fx + m_matrix[1][2] * fy + m_matrix[2][2]); - x *= w; - y *= w; - } - } + MAP(fx, fy, x, y); + return QPointF(x, y); } @@ -1213,41 +1223,9 @@ QLine QTransform::map(const QLine &l) const qreal x1 = 0, y1 = 0, x2 = 0, y2 = 0; TransformationType t = inline_type(); - switch(t) { - case TxNone: - x1 = fx1; - y1 = fy1; - x2 = fx2; - y2 = fy2; - break; - case TxTranslate: - x1 = fx1 + m_matrix[2][0]; - y1 = fy1 + m_matrix[2][1]; - x2 = fx2 + m_matrix[2][0]; - y2 = fy2 + m_matrix[2][1]; - break; - case TxScale: - x1 = m_matrix[0][0] * fx1 + m_matrix[2][0]; - y1 = m_matrix[1][1] * fy1 + m_matrix[2][1]; - x2 = m_matrix[0][0] * fx2 + m_matrix[2][0]; - y2 = m_matrix[1][1] * fy2 + m_matrix[2][1]; - break; - case TxRotate: - case TxShear: - case TxProject: - x1 = m_matrix[0][0] * fx1 + m_matrix[1][0] * fy1 + m_matrix[2][0]; - y1 = m_matrix[0][1] * fx1 + m_matrix[1][1] * fy1 + m_matrix[2][1]; - x2 = m_matrix[0][0] * fx2 + m_matrix[1][0] * fy2 + m_matrix[2][0]; - y2 = m_matrix[0][1] * fx2 + m_matrix[1][1] * fy2 + m_matrix[2][1]; - if (t == TxProject) { - qreal w = 1./(m_matrix[0][2] * fx1 + m_matrix[1][2] * fy1 + m_matrix[2][2]); - x1 *= w; - y1 *= w; - w = 1./(m_matrix[0][2] * fx2 + m_matrix[1][2] * fy2 + m_matrix[2][2]); - x2 *= w; - y2 *= w; - } - } + MAP(fx1, fy1, x1, y1); + MAP(fx2, fy2, x2, y2); + return QLine(qRound(x1), qRound(y1), qRound(x2), qRound(y2)); } @@ -1272,66 +1250,12 @@ QLineF QTransform::map(const QLineF &l) const qreal x1 = 0, y1 = 0, x2 = 0, y2 = 0; TransformationType t = inline_type(); - switch(t) { - case TxNone: - x1 = fx1; - y1 = fy1; - x2 = fx2; - y2 = fy2; - break; - case TxTranslate: - x1 = fx1 + m_matrix[2][0]; - y1 = fy1 + m_matrix[2][1]; - x2 = fx2 + m_matrix[2][0]; - y2 = fy2 + m_matrix[2][1]; - break; - case TxScale: - x1 = m_matrix[0][0] * fx1 + m_matrix[2][0]; - y1 = m_matrix[1][1] * fy1 + m_matrix[2][1]; - x2 = m_matrix[0][0] * fx2 + m_matrix[2][0]; - y2 = m_matrix[1][1] * fy2 + m_matrix[2][1]; - break; - case TxRotate: - case TxShear: - case TxProject: - x1 = m_matrix[0][0] * fx1 + m_matrix[1][0] * fy1 + m_matrix[2][0]; - y1 = m_matrix[0][1] * fx1 + m_matrix[1][1] * fy1 + m_matrix[2][1]; - x2 = m_matrix[0][0] * fx2 + m_matrix[1][0] * fy2 + m_matrix[2][0]; - y2 = m_matrix[0][1] * fx2 + m_matrix[1][1] * fy2 + m_matrix[2][1]; - if (t == TxProject) { - qreal w = 1./(m_matrix[0][2] * fx1 + m_matrix[1][2] * fy1 + m_matrix[2][2]); - x1 *= w; - y1 *= w; - w = 1./(m_matrix[0][2] * fx2 + m_matrix[1][2] * fy2 + m_matrix[2][2]); - x2 *= w; - y2 *= w; - } - } - return QLineF(x1, y1, x2, y2); -} + MAP(fx1, fy1, x1, y1); + MAP(fx2, fy2, x2, y2); -static QPolygonF mapProjective(const QTransform &transform, const QPolygonF &poly) -{ - if (poly.size() == 0) - return poly; - - if (poly.size() == 1) - return QPolygonF() << transform.map(poly.at(0)); - - QPainterPath path; - path.addPolygon(poly); - - path = transform.map(path); - - QPolygonF result; - const int elementCount = path.elementCount(); - result.reserve(elementCount); - for (int i = 0; i < elementCount; ++i) - result << path.elementAt(i); - return result; + return QLineF(x1, y1, x2, y2); } - /*! \fn QPolygonF operator *(const QPolygonF &polygon, const QTransform &matrix) \since 4.3 @@ -1365,9 +1289,6 @@ QPolygonF QTransform::map(const QPolygonF &a) const if (t <= TxTranslate) return a.translated(m_matrix[2][0], m_matrix[2][1]); - if (t >= QTransform::TxProject) - return mapProjective(*this, a); - int size = a.size(); int i; QPolygonF p(size); @@ -1395,9 +1316,6 @@ QPolygon QTransform::map(const QPolygon &a) const if (t <= TxTranslate) return a.translated(qRound(m_matrix[2][0]), qRound(m_matrix[2][1])); - if (t >= QTransform::TxProject) - return mapProjective(*this, QPolygonF(a)).toPolygon(); - int size = a.size(); int i; QPolygon p(size); @@ -1459,7 +1377,7 @@ QRegion QTransform::map(const QRegion &r) const if (!nr.isEmpty()) rects.append(nr); } - res.setRects(rects.constData(), rects.count()); + res.setRects(rects.constData(), rects.size()); } return res; } @@ -1706,7 +1624,7 @@ QPolygon QTransform::mapToPolygon(const QRect &rect) const */ bool QTransform::squareToQuad(const QPolygonF &quad, QTransform &trans) { - if (quad.count() != 4) + if (quad.size() != 4) return false; qreal dx0 = quad[0].x(); @@ -1828,14 +1746,6 @@ void QTransform::setMatrix(qreal m11, qreal m12, qreal m13, m_dirty = TxProject; } -static inline bool needsPerspectiveClipping(const QRectF &rect, const QTransform &transform) -{ - const qreal wx = qMin(transform.m13() * rect.left(), transform.m13() * rect.right()); - const qreal wy = qMin(transform.m23() * rect.top(), transform.m23() * rect.bottom()); - - return wx + wy + transform.m33() < Q_NEAR_CLIP; -} - QRect QTransform::mapRect(const QRect &rect) const { TransformationType t = inline_type(); @@ -1856,8 +1766,7 @@ QRect QTransform::mapRect(const QRect &rect) const y -= h; } return QRect(x, y, w, h); - } else if (t < TxProject || !needsPerspectiveClipping(rect, *this)) { - // see mapToPolygon for explanations of the algorithm. + } else { qreal x = 0, y = 0; MAP(rect.left(), rect.top(), x, y); qreal xmin = x; @@ -1879,11 +1788,7 @@ QRect QTransform::mapRect(const QRect &rect) const ymin = qMin(ymin, y); xmax = qMax(xmax, x); ymax = qMax(ymax, y); - return QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin)); - } else { - QPainterPath path; - path.addRect(rect); - return map(path).boundingRect().toRect(); + return QRectF(xmin, ymin, xmax-xmin, ymax-ymin).toRect(); } } @@ -1926,7 +1831,7 @@ QRectF QTransform::mapRect(const QRectF &rect) const y -= h; } return QRectF(x, y, w, h); - } else if (t < TxProject || !needsPerspectiveClipping(rect, *this)) { + } else { qreal x = 0, y = 0; MAP(rect.x(), rect.y(), x, y); qreal xmin = x; @@ -1949,10 +1854,6 @@ QRectF QTransform::mapRect(const QRectF &rect) const xmax = qMax(xmax, x); ymax = qMax(ymax, y); return QRectF(xmin, ymin, xmax-xmin, ymax - ymin); - } else { - QPainterPath path; - path.addRect(rect); - return map(path).boundingRect(); } } |