diff --git a/src/corelib/tools/qrect.cpp b/src/corelib/tools/qrect.cpp index 6215c534f8..4b6183646b 100644 --- a/src/corelib/tools/qrect.cpp +++ b/src/corelib/tools/qrect.cpp @@ -706,6 +706,20 @@ QRect QRect::normalized() const Q_DECL_NOTHROW current position. */ +/*! + \fn QRect QRect::transposed() const + \since 5.7 + + Returns a copy of the rectangle that has its width and height + exchanged: + + \code + QRect r = {15, 51, 42, 24}; + r = r.transposed(); // r == {15, 51, 24, 42} + \endcode + + \sa QSize::transposed() +*/ /*! \fn void QRect::setRect(int x, int y, int width, int height) @@ -1842,6 +1856,20 @@ QRectF QRectF::normalized() const Q_DECL_NOTHROW current position. */ +/*! + \fn QRectF QRectF::transposed() const + \since 5.7 + + Returns a copy of the rectangle that has its width and height + exchanged: + + \code + QRectF r = {1.5, 5.1, 4.2, 2.4}; + r = r.transposed(); // r == {1.5, 5.1, 2.4, 4.2} + \endcode + + \sa QSizeF::transposed() +*/ /*! \fn void QRectF::setRect(qreal x, qreal y, qreal width, qreal height) diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h index e90bb8a21e..31fdc8ce6b 100644 --- a/src/corelib/tools/qrect.h +++ b/src/corelib/tools/qrect.h @@ -102,6 +102,7 @@ public: Q_DECL_RELAXED_CONSTEXPR inline void translate(const QPoint &p) Q_DECL_NOTHROW; Q_DECL_CONSTEXPR inline QRect translated(int dx, int dy) const Q_DECL_NOTHROW Q_REQUIRED_RESULT; Q_DECL_CONSTEXPR inline QRect translated(const QPoint &p) const Q_DECL_NOTHROW Q_REQUIRED_RESULT; + Q_DECL_CONSTEXPR inline QRect transposed() const Q_DECL_NOTHROW Q_REQUIRED_RESULT; Q_DECL_RELAXED_CONSTEXPR inline void moveTo(int x, int t) Q_DECL_NOTHROW; Q_DECL_RELAXED_CONSTEXPR inline void moveTo(const QPoint &p) Q_DECL_NOTHROW; @@ -284,6 +285,9 @@ Q_DECL_CONSTEXPR inline QRect QRect::translated(int dx, int dy) const Q_DECL_NOT Q_DECL_CONSTEXPR inline QRect QRect::translated(const QPoint &p) const Q_DECL_NOTHROW { return QRect(QPoint(x1 + p.x(), y1 + p.y()), QPoint(x2 + p.x(), y2 + p.y())); } +Q_DECL_CONSTEXPR inline QRect QRect::transposed() const Q_DECL_NOTHROW +{ return QRect(topLeft(), size().transposed()); } + Q_DECL_RELAXED_CONSTEXPR inline void QRect::moveTo(int ax, int ay) Q_DECL_NOTHROW { x2 += ax - x1; @@ -551,6 +555,8 @@ public: Q_DECL_CONSTEXPR inline QRectF translated(qreal dx, qreal dy) const Q_DECL_NOTHROW Q_REQUIRED_RESULT; Q_DECL_CONSTEXPR inline QRectF translated(const QPointF &p) const Q_DECL_NOTHROW Q_REQUIRED_RESULT; + Q_DECL_CONSTEXPR inline QRectF transposed() const Q_DECL_NOTHROW Q_REQUIRED_RESULT; + Q_DECL_RELAXED_CONSTEXPR inline void moveTo(qreal x, qreal y) Q_DECL_NOTHROW; Q_DECL_RELAXED_CONSTEXPR inline void moveTo(const QPointF &p) Q_DECL_NOTHROW; @@ -751,6 +757,9 @@ Q_DECL_CONSTEXPR inline QRectF QRectF::translated(qreal dx, qreal dy) const Q_DE Q_DECL_CONSTEXPR inline QRectF QRectF::translated(const QPointF &p) const Q_DECL_NOTHROW { return QRectF(xp + p.x(), yp + p.y(), w, h); } +Q_DECL_CONSTEXPR inline QRectF QRectF::transposed() const Q_DECL_NOTHROW +{ return QRectF(topLeft(), size().transposed()); } + Q_DECL_RELAXED_CONSTEXPR inline void QRectF::getRect(qreal *ax, qreal *ay, qreal *aaw, qreal *aah) const { *ax = this->xp; diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp index e8b259168d..9b35cdec30 100644 --- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp +++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp @@ -127,6 +127,9 @@ private slots: void translate_data(); void translate(); + void transposed_data(); + void transposed(); + void moveTop(); void moveBottom(); void moveLeft(); @@ -3562,6 +3565,41 @@ void tst_QRect::translate() } +void tst_QRect::transposed_data() +{ + QTest::addColumn("r"); + + QTest::newRow("InvalidQRect") << getQRectCase(InvalidQRect); + QTest::newRow("SmallestQRect") << getQRectCase(SmallestQRect); + QTest::newRow("MiddleQRect") << getQRectCase(MiddleQRect); + QTest::newRow("LargestQRect") << getQRectCase(LargestQRect); + QTest::newRow("SmallestCoordQRect") << getQRectCase(SmallestCoordQRect); + QTest::newRow("LargestCoordQRect") << getQRectCase(LargestCoordQRect); + QTest::newRow("RandomQRect") << getQRectCase(RandomQRect); + QTest::newRow("NegativeSizeQRect") << getQRectCase(NegativeSizeQRect); + QTest::newRow("NegativePointQRect") << getQRectCase(NegativePointQRect); + QTest::newRow("NullQRect") << getQRectCase(NullQRect); + QTest::newRow("EmptyQRect") << getQRectCase(EmptyQRect); +} + +void tst_QRect::transposed() +{ + QFETCH(QRect, r); + + const QRect rt = r.transposed(); + QCOMPARE(rt.height(), r.width()); + QCOMPARE(rt.width(), r.height()); + QCOMPARE(rt.topLeft(), r.topLeft()); + + const QRectF rf = r; + + const QRectF rtf = rf.transposed(); + QCOMPARE(rtf.height(), rf.width()); + QCOMPARE(rtf.width(), rf.height()); + QCOMPARE(rtf.topLeft(), rf.topLeft()); + + QCOMPARE(rtf, QRectF(rt)); +} void tst_QRect::moveTop() {