QRect: add toRectF()

For symmetry with QRectF::toRect().

[ChangeLog][QtCore][QRect] Added toRectF().

Fixes: QTBUG-73160
Change-Id: If2bda64b8fe4bc113191dda927e9bb86ebcb4c69
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2022-03-18 08:16:20 +01:00
parent 5dc570f8b1
commit c52ebb3aba
3 changed files with 62 additions and 5 deletions

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -1232,6 +1232,18 @@ bool QRect::intersects(const QRect &r) const noexcept
\since 6.0 \since 6.0
*/ */
/*!
\fn QRect::toRectF() const
\since 6.4
Returns this rectangle as a rectangle with floating point accuracy.
\note This function, like the QRectF(QRect) constructor, preserves the
size() of the rectangle, not its bottomRight() corner.
\sa QRectF::toRect()
*/
/***************************************************************************** /*****************************************************************************
QRect stream functions QRect stream functions
*****************************************************************************/ *****************************************************************************/
@ -1480,7 +1492,10 @@ QDebug operator<<(QDebug dbg, const QRect &r)
Constructs a QRectF rectangle from the given QRect \a rectangle. Constructs a QRectF rectangle from the given QRect \a rectangle.
\sa toRect() \note This function, like QRect::toRectF(), preserves the size() of
\a rectangle, not its bottomRight() corner.
\sa toRect(), QRect::toRectF()
*/ */
/*! /*!
@ -2334,7 +2349,7 @@ bool QRectF::intersects(const QRectF &r) const noexcept
Returns a QRect based on the values of this rectangle. Note that the Returns a QRect based on the values of this rectangle. Note that the
coordinates in the returned rectangle are rounded to the nearest integer. coordinates in the returned rectangle are rounded to the nearest integer.
\sa QRectF(), toAlignedRect() \sa QRectF(), toAlignedRect(), QRect::toRectF()
*/ */
/*! /*!

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -55,6 +55,8 @@ struct CGRect;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QRectF;
class Q_CORE_EXPORT QRect class Q_CORE_EXPORT QRect
{ {
public: public:
@ -157,6 +159,7 @@ public:
#if defined(Q_OS_DARWIN) || defined(Q_QDOC) #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
[[nodiscard]] CGRect toCGRect() const noexcept; [[nodiscard]] CGRect toCGRect() const noexcept;
#endif #endif
[[nodiscard]] constexpr inline QRectF toRectF() const noexcept;
private: private:
int x1; int x1;
@ -863,6 +866,8 @@ inline QRectF QRectF::united(const QRectF &r) const noexcept
return *this | r; return *this | r;
} }
constexpr QRectF QRect::toRectF() const noexcept { return *this; }
constexpr inline QRect QRectF::toRect() const noexcept constexpr inline QRect QRectF::toRect() const noexcept
{ {
// This rounding is designed to minimize the maximum possible difference // This rounding is designed to minimize the maximum possible difference

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the test suite of the Qt Toolkit. ** This file is part of the test suite of the Qt Toolkit.
@ -32,6 +32,7 @@
#include <limits.h> #include <limits.h>
#include <qdebug.h> #include <qdebug.h>
#include <array>
class tst_QRect : public QObject class tst_QRect : public QObject
{ {
@ -124,6 +125,9 @@ private slots:
void margins(); void margins();
void marginsf(); void marginsf();
void toRectF_data();
void toRectF();
void translate_data(); void translate_data();
void translate(); void translate();
@ -3536,6 +3540,39 @@ void tst_QRect::marginsf()
QCOMPARE(a, rectangle.marginsRemoved(margins)); QCOMPARE(a, rectangle.marginsRemoved(margins));
} }
void tst_QRect::toRectF_data()
{
QTest::addColumn<QRect>("input");
QTest::addColumn<QRectF>("result");
auto row = [](int x1, int y1, int w, int h) {
// QRectF -> QRect conversion tries to maintain size(), not bottomRight(),
// so compare in (topLeft(), size()) space
QTest::addRow("((%d, %d) (%dx%d))", x1, y1, w, h)
<< QRect({x1, y1}, QSize{w, h}) << QRectF(QPointF(x1, y1), QSizeF(w, h));
};
constexpr std::array samples = {-1, 0, 1};
for (int x1 : samples) {
for (int y1 : samples) {
for (int w : samples) {
for (int h : samples) {
row(x1, y1, w, h);
}
}
}
}
}
void tst_QRect::toRectF()
{
QFETCH(const QRect, input);
QFETCH(const QRectF, result);
QCOMPARE(result.toRect(), input); // consistency check
QCOMPARE(input.toRectF(), result);
}
void tst_QRect::translate_data() void tst_QRect::translate_data()
{ {
QTest::addColumn<QRect>("r"); QTest::addColumn<QRect>("r");