diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-04-17 17:57:35 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-05-14 14:33:21 +0200 |
commit | 3ca9877f8cae41e1b1cb3c72d4a7872d97fb8254 (patch) | |
tree | 6f378b9f46ebb0a14698167276cf2a4ef04f00c6 | |
parent | c2826805de3cb25b2056891067669ecce736078f (diff) |
QSizeF: add qFuzzyCompare and qFuzzyIsNull overloads
[ChangeLog][QtCore][QSizeF] Added qFuzzyCompare and qFuzzyIsNull
overloads for QSizeF
Drop the custom qFuzzyCompare() implementation from
tst_qgraphicsgridlayout.cpp
Task-number: QTBUG-120308
Change-Id: I32d11c1c81d0503914a8168b5e960a8c6ce943d9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qsize.cpp | 17 | ||||
-rw-r--r-- | src/corelib/tools/qsize.h | 8 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qsizef/tst_qsizef.cpp | 48 | ||||
-rw-r--r-- | tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp | 5 |
4 files changed, 72 insertions, 6 deletions
diff --git a/src/corelib/tools/qsize.cpp b/src/corelib/tools/qsize.cpp index 0165a4095c..27ff1d164d 100644 --- a/src/corelib/tools/qsize.cpp +++ b/src/corelib/tools/qsize.cpp @@ -808,7 +808,24 @@ QSizeF QSizeF::scaled(const QSizeF &s, Qt::AspectRatioMode mode) const noexcept \sa expandedTo(), scale() */ +/*! + \fn bool QSizeF::qFuzzyCompare(const QSizeF &lhs, const QSizeF &rhs) + \since 6.8 + + Returns \c true if the size \a lhs is approximately equal to the + size \a rhs; otherwise returns \c false. + + The sizes are considered approximately equal if their width and + height are approximately equal. +*/ +/*! + \fn bool QSizeF::qFuzzyIsNull(const QSizeF &size) + \since 6.8 + + Returns \c true if both width and height of the size \a size + are approximately equal to zero. +*/ /***************************************************************************** QSizeF stream functions diff --git a/src/corelib/tools/qsize.h b/src/corelib/tools/qsize.h index 604f110ebe..67f7146201 100644 --- a/src/corelib/tools/qsize.h +++ b/src/corelib/tools/qsize.h @@ -246,12 +246,18 @@ public: private: QT_WARNING_PUSH QT_WARNING_DISABLE_FLOAT_COMPARE - friend constexpr bool comparesEqual(const QSizeF &s1, const QSizeF &s2) noexcept + friend constexpr bool qFuzzyCompare(const QSizeF &s1, const QSizeF &s2) noexcept { + // Cannot use qFuzzyCompare(), because it will give incorrect results + // if one of the arguments is 0.0. return ((!s1.wd || !s2.wd) ? qFuzzyIsNull(s1.wd - s2.wd) : qFuzzyCompare(s1.wd, s2.wd)) && ((!s1.ht || !s2.ht) ? qFuzzyIsNull(s1.ht - s2.ht) : qFuzzyCompare(s1.ht, s2.ht)); } QT_WARNING_POP + friend constexpr bool qFuzzyIsNull(const QSizeF &size) noexcept + { return qFuzzyIsNull(size.wd) && qFuzzyIsNull(size.ht); } + friend constexpr bool comparesEqual(const QSizeF &lhs, const QSizeF &rhs) noexcept + { return qFuzzyCompare(lhs, rhs); } Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QSizeF) friend constexpr bool comparesEqual(const QSizeF &lhs, const QSize &rhs) noexcept { return comparesEqual(lhs, rhs.toSizeF()); } diff --git a/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp b/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp index 12e30dfa57..bb087e89de 100644 --- a/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp +++ b/tests/auto/corelib/tools/qsizef/tst_qsizef.cpp @@ -39,9 +39,15 @@ private slots: void compare_data(); void compare(); + void fuzzyCompare_data(); + void fuzzyCompare(); + void isNull_data(); void isNull(); + void fuzzyIsNull_data(); + void fuzzyIsNull(); + void scale(); void expandedTo(); @@ -100,6 +106,20 @@ void tst_QSizeF::compare() QT_TEST_EQUALITY_OPS(lhs, rhsFixed, mixedResult); } +void tst_QSizeF::fuzzyCompare_data() +{ + compare_data(); +} + +void tst_QSizeF::fuzzyCompare() +{ + QFETCH(QSizeF, lhs); + QFETCH(QSizeF, rhs); + QFETCH(bool, result); + + QCOMPARE_EQ(qFuzzyCompare(lhs, rhs), result); +} + void tst_QSizeF::isNull_data() { QTest::addColumn<qreal>("width"); @@ -114,6 +134,7 @@ void tst_QSizeF::isNull_data() QTest::newRow("0, -0.1") << qreal(0) << qreal(-0.1) << false; QTest::newRow("0.1, 0") << qreal(0.1) << qreal(0) << false; QTest::newRow("0, 0.1") << qreal(0) << qreal(0.1) << false; + QTest::newRow("qreal_min, -qreal_min") << qreal_min << -qreal_min << false; } void tst_QSizeF::isNull() @@ -128,6 +149,33 @@ void tst_QSizeF::isNull() QCOMPARE(size.isNull(), isNull); } +void tst_QSizeF::fuzzyIsNull_data() +{ + QTest::addColumn<qreal>("width"); + QTest::addColumn<qreal>("height"); + QTest::addColumn<bool>("fuzzyNull"); + + QTest::newRow("0, 0") << qreal(0.0) << qreal(0.0) << true; + QTest::newRow("-0, -0") << qreal(-0.0) << qreal(-0.0) << true; + QTest::newRow("0, -0") << qreal(0) << qreal(-0.0) << true; + QTest::newRow("-0, 0") << qreal(-0.0) << qreal(0) << true; + QTest::newRow("-0.1, 0") << qreal(-0.1) << qreal(0) << false; + QTest::newRow("0, -0.1") << qreal(0) << qreal(-0.1) << false; + QTest::newRow("0.1, 0") << qreal(0.1) << qreal(0) << false; + QTest::newRow("0, 0.1") << qreal(0) << qreal(0.1) << false; + QTest::newRow("qreal_min, -qreal_min") << qreal_min << -qreal_min << true; +} + +void tst_QSizeF::fuzzyIsNull() +{ + QFETCH(qreal, width); + QFETCH(qreal, height); + QFETCH(bool, fuzzyNull); + + QSizeF size(width, height); + QCOMPARE(qFuzzyIsNull(size), fuzzyNull); +} + void tst_QSizeF::scale() { QSizeF t1(10.4, 12.8); t1.scale(60.6, 60.6, Qt::IgnoreAspectRatio); diff --git a/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp b/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp index c91a0803ee..fad75ec045 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsgridlayout/tst_qgraphicsgridlayout.cpp @@ -2976,11 +2976,6 @@ static QSizeF wfh(Qt::SizeHint /*which*/, const QSizeF &constraint) return result; } -bool qFuzzyCompare(const QSizeF &a, const QSizeF &b) -{ - return qFuzzyCompare(a.width(), b.width()) && qFuzzyCompare(a.height(), b.height()); -} - void tst_QGraphicsGridLayout::heightForWidth() { QGraphicsWidget *widget = new QGraphicsWidget; |