QRect(F): add transposed()

I didn't add a transpose(), because r = r.transposed() is
perfectly capable of filling that role, and just as
efficient. Existing API mistakes are no excuse to create
more of them.

[ChangeLog][QtCore][QRect/QRectF] Added transposed().

Change-Id: Ic38721e9028496fc9b50f4d4cef2e7a60532eed8
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2015-11-02 22:05:58 +01:00
parent afc7da09ec
commit 8ce34c2e81
3 changed files with 75 additions and 0 deletions

View File

@ -706,6 +706,20 @@ QRect QRect::normalized() const Q_DECL_NOTHROW
current position. 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) \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. 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) \fn void QRectF::setRect(qreal x, qreal y, qreal width, qreal height)

View File

@ -102,6 +102,7 @@ public:
Q_DECL_RELAXED_CONSTEXPR inline void translate(const QPoint &p) Q_DECL_NOTHROW; 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(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 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(int x, int t) Q_DECL_NOTHROW;
Q_DECL_RELAXED_CONSTEXPR inline void moveTo(const QPoint &p) 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 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())); } { 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 Q_DECL_RELAXED_CONSTEXPR inline void QRect::moveTo(int ax, int ay) Q_DECL_NOTHROW
{ {
x2 += ax - x1; 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(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 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(qreal x, qreal y) Q_DECL_NOTHROW;
Q_DECL_RELAXED_CONSTEXPR inline void moveTo(const QPointF &p) 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 Q_DECL_CONSTEXPR inline QRectF QRectF::translated(const QPointF &p) const Q_DECL_NOTHROW
{ return QRectF(xp + p.x(), yp + p.y(), w, h); } { 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 Q_DECL_RELAXED_CONSTEXPR inline void QRectF::getRect(qreal *ax, qreal *ay, qreal *aaw, qreal *aah) const
{ {
*ax = this->xp; *ax = this->xp;

View File

@ -127,6 +127,9 @@ private slots:
void translate_data(); void translate_data();
void translate(); void translate();
void transposed_data();
void transposed();
void moveTop(); void moveTop();
void moveBottom(); void moveBottom();
void moveLeft(); void moveLeft();
@ -3562,6 +3565,41 @@ void tst_QRect::translate()
} }
void tst_QRect::transposed_data()
{
QTest::addColumn<QRect>("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() void tst_QRect::moveTop()
{ {