QRectF: add qFuzzyCompare and qFuzzyIsNull overloads

[ChangeLog][QtCore][QRectF] Added qFuzzyCompare and qFuzzyIsNull
overloads for QRectF

Task-number: QTBUG-120308
Change-Id: Ie2eee0a28b902bdfeb91be45a164be659aef1a20
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ivan Solovev 2024-04-05 17:48:28 +02:00
parent 30bf163355
commit 9d4e1b9f8d
3 changed files with 52 additions and 0 deletions

View File

@ -2437,6 +2437,22 @@ QRect QRectF::toAlignedRect() const noexcept
\sa marginsRemoved(), operator+=(), marginsAdded() \sa marginsRemoved(), operator+=(), marginsAdded()
*/ */
/*!
\fn bool QRectF::qFuzzyCompare(const QRectF &lhs, const QRectF &rhs)
\since 6.8
Returns \c true if the rectangle \a lhs is approximately equal to the
rectangle \a rhs; otherwise returns \c false.
*/
/*!
\fn bool QRectF::qFuzzyIsNull(const QRectF &rect)
\since 6.8
Returns \c true if both width and height of the rectangle \a rect are
approximately equal to zero; otherwise returns \c false.
*/
/***************************************************************************** /*****************************************************************************
QRectF stream functions QRectF stream functions
*****************************************************************************/ *****************************************************************************/

View File

@ -585,6 +585,17 @@ private:
{ return r1.topLeft() == r2.topLeft() && r1.size() == r2.size(); } { return r1.topLeft() == r2.topLeft() && r1.size() == r2.size(); }
Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QRectF, QRect) Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QRectF, QRect)
friend constexpr bool qFuzzyCompare(const QRectF &lhs, const QRectF &rhs) noexcept
{
return qFuzzyCompare(lhs.topLeft(), rhs.topLeft())
&& qFuzzyCompare(lhs.bottomRight(), rhs.bottomRight());
}
friend constexpr bool qFuzzyIsNull(const QRectF &rect) noexcept
{
return qFuzzyIsNull(rect.w) && qFuzzyIsNull(rect.h);
}
public: public:
[[nodiscard]] constexpr inline QRect toRect() const noexcept; [[nodiscard]] constexpr inline QRect toRect() const noexcept;
[[nodiscard]] QRect toAlignedRect() const noexcept; [[nodiscard]] QRect toAlignedRect() const noexcept;

View File

@ -38,8 +38,11 @@ private slots:
void comparisonCompiles(); void comparisonCompiles();
void comparison_data(); void comparison_data();
void comparison(); void comparison();
void fuzzyComparison_data();
void fuzzyComparison();
void isNull_data(); void isNull_data();
void isNull(); void isNull();
void fuzzyIsNull();
void newIsEmpty_data(); void newIsEmpty_data();
void newIsEmpty(); void newIsEmpty();
void newIsValid_data(); void newIsValid_data();
@ -309,6 +312,20 @@ void tst_QRect::comparison()
QT_TEST_EQUALITY_OPS(lhs, rhsF, mixedResult); QT_TEST_EQUALITY_OPS(lhs, rhsF, mixedResult);
} }
void tst_QRect::fuzzyComparison_data()
{
comparison_data();
}
void tst_QRect::fuzzyComparison()
{
QFETCH(const QRectF, lhsF);
QFETCH(const QRectF, rhsF);
QFETCH(const bool, floatResult);
QCOMPARE_EQ(qFuzzyCompare(lhsF, rhsF), floatResult);
}
void tst_QRect::isNull_data() void tst_QRect::isNull_data()
{ {
QTest::addColumn<QRect>("r"); QTest::addColumn<QRect>("r");
@ -338,6 +355,14 @@ void tst_QRect::isNull()
QVERIFY( rf.isNull() == isNull ); QVERIFY( rf.isNull() == isNull );
} }
void tst_QRect::fuzzyIsNull()
{
QRectF rf(QPointF(-qreal_min, qreal_min), QPointF(qreal_min, -qreal_min));
QVERIFY(!rf.isNull()); // QRectF::isNull() does strict comparison
QVERIFY(qFuzzyIsNull(rf));
}
void tst_QRect::newIsEmpty_data() void tst_QRect::newIsEmpty_data()
{ {
QTest::addColumn<QRect>("r"); QTest::addColumn<QRect>("r");