From c52ebb3aba3d771b908c03e3b171af64b6f9058a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 18 Mar 2022 08:16:20 +0100 Subject: [PATCH] QRect: add toRectF() For symmetry with QRectF::toRect(). [ChangeLog][QtCore][QRect] Added toRectF(). Fixes: QTBUG-73160 Change-Id: If2bda64b8fe4bc113191dda927e9bb86ebcb4c69 Reviewed-by: Thiago Macieira --- src/corelib/tools/qrect.cpp | 21 +++++++++-- src/corelib/tools/qrect.h | 7 +++- tests/auto/corelib/tools/qrect/tst_qrect.cpp | 39 +++++++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qrect.cpp b/src/corelib/tools/qrect.cpp index 64ccdb20ffb..cdde38946fe 100644 --- a/src/corelib/tools/qrect.cpp +++ b/src/corelib/tools/qrect.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** 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 */ +/*! + \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 *****************************************************************************/ @@ -1480,7 +1492,10 @@ QDebug operator<<(QDebug dbg, const QRect &r) 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 coordinates in the returned rectangle are rounded to the nearest integer. - \sa QRectF(), toAlignedRect() + \sa QRectF(), toAlignedRect(), QRect::toRectF() */ /*! diff --git a/src/corelib/tools/qrect.h b/src/corelib/tools/qrect.h index 9e64f4d6e81..f034be7007c 100644 --- a/src/corelib/tools/qrect.h +++ b/src/corelib/tools/qrect.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -55,6 +55,8 @@ struct CGRect; QT_BEGIN_NAMESPACE +class QRectF; + class Q_CORE_EXPORT QRect { public: @@ -157,6 +159,7 @@ public: #if defined(Q_OS_DARWIN) || defined(Q_QDOC) [[nodiscard]] CGRect toCGRect() const noexcept; #endif + [[nodiscard]] constexpr inline QRectF toRectF() const noexcept; private: int x1; @@ -863,6 +866,8 @@ inline QRectF QRectF::united(const QRectF &r) const noexcept return *this | r; } +constexpr QRectF QRect::toRectF() const noexcept { return *this; } + constexpr inline QRect QRectF::toRect() const noexcept { // This rounding is designed to minimize the maximum possible difference diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp index 4057eb67fbc..181cc8bbdb0 100644 --- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp +++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. @@ -32,6 +32,7 @@ #include #include +#include class tst_QRect : public QObject { @@ -124,6 +125,9 @@ private slots: void margins(); void marginsf(); + void toRectF_data(); + void toRectF(); + void translate_data(); void translate(); @@ -3536,6 +3540,39 @@ void tst_QRect::marginsf() QCOMPARE(a, rectangle.marginsRemoved(margins)); } +void tst_QRect::toRectF_data() +{ + QTest::addColumn("input"); + QTest::addColumn("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() { QTest::addColumn("r");