From 2fb0135efa97708025dab7654a8b2f1350dc30bf Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 18 Mar 2022 08:16:20 +0100 Subject: [PATCH] QSize/QPoint: add toSizeF/toPointF() For symmetry with QSizeF::toSize(). [ChangeLog][QtCore][QSize] Added toSizeF(). [ChangeLog][QtCore][QPoint] Added toPointF(). Task-number: QTBUG-73160 Change-Id: I65b088b4f7365ab671ef2f0c75821b707f5ac26d Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- src/corelib/tools/qpoint.cpp | 15 +++++++-- src/corelib/tools/qpoint.h | 7 ++++- src/corelib/tools/qsize.cpp | 15 +++++++-- src/corelib/tools/qsize.h | 7 ++++- .../auto/corelib/tools/qpoint/tst_qpoint.cpp | 31 ++++++++++++++++++- tests/auto/corelib/tools/qsize/tst_qsize.cpp | 31 ++++++++++++++++++- 6 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qpoint.cpp b/src/corelib/tools/qpoint.cpp index 80599a4caa5..6edb054d0a4 100644 --- a/src/corelib/tools/qpoint.cpp +++ b/src/corelib/tools/qpoint.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. @@ -381,6 +381,15 @@ QT_BEGIN_NAMESPACE \sa QPoint::operator/=() */ +/*! + \fn QPoint::toPointF() const + \since 6.4 + + Returns this point as a point with floating point accuracy. + + \sa QPointF::toPoint() +*/ + /***************************************************************************** QPoint stream functions *****************************************************************************/ @@ -532,7 +541,7 @@ size_t qHash(QPoint key, size_t seed) noexcept Constructs a copy of the given \a point. - \sa toPoint() + \sa toPoint(), QPoint::toPointF() */ /*! @@ -744,7 +753,7 @@ size_t qHash(QPoint key, size_t seed) noexcept Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rounded coordinates. - \sa QPointF() + \sa QPointF(), QPoint::toPointF() */ /*! diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index f2c562393ab..94870a72116 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.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. @@ -48,6 +48,8 @@ struct CGPoint; QT_BEGIN_NAMESPACE +class QPointF; + class QPoint { public: @@ -110,6 +112,7 @@ public: #if defined(Q_OS_DARWIN) || defined(Q_QDOC) [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept; #endif + [[nodiscard]] constexpr inline QPointF toPointF() const noexcept; private: friend class QTransform; @@ -417,6 +420,8 @@ constexpr inline QPointF &QPointF::operator/=(qreal divisor) return *this; } +constexpr QPointF QPoint::toPointF() const noexcept { return *this; } + constexpr inline QPoint QPointF::toPoint() const { return QPoint(qRound(xp), qRound(yp)); diff --git a/src/corelib/tools/qsize.cpp b/src/corelib/tools/qsize.cpp index 2779948aaec..4f4ae9e75b5 100644 --- a/src/corelib/tools/qsize.cpp +++ b/src/corelib/tools/qsize.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. @@ -403,6 +403,15 @@ QSize QSize::scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept \sa grownBy() */ +/*! + \fn QSize::toSizeF() const + \since 6.4 + + Returns this size as a size with floating point accuracy. + + \sa QSizeF::toSize() +*/ + /***************************************************************************** QSize stream functions *****************************************************************************/ @@ -520,7 +529,7 @@ QDebug operator<<(QDebug dbg, const QSize &s) Constructs a size with floating point accuracy from the given \a size. - \sa toSize() + \sa toSize(), QSize::toSizeF() */ /*! @@ -596,7 +605,7 @@ QDebug operator<<(QDebug dbg, const QSize &s) Note that the coordinates in the returned size will be rounded to the nearest integer. - \sa QSizeF() + \sa QSizeF(), QSize::toSizeF() */ /*! diff --git a/src/corelib/tools/qsize.h b/src/corelib/tools/qsize.h index bab193031cd..3411fef33d8 100644 --- a/src/corelib/tools/qsize.h +++ b/src/corelib/tools/qsize.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. @@ -50,6 +50,7 @@ struct CGSize; QT_BEGIN_NAMESPACE +class QSizeF; class Q_CORE_EXPORT QSize { @@ -109,6 +110,8 @@ public: [[nodiscard]] CGSize toCGSize() const noexcept; #endif + [[nodiscard]] inline constexpr QSizeF toSizeF() const noexcept; + private: int wd; int ht; @@ -420,6 +423,8 @@ constexpr inline QSize QSizeF::toSize() const noexcept return QSize(qRound(wd), qRound(ht)); } +constexpr QSizeF QSize::toSizeF() const noexcept { return *this; } + #ifndef QT_NO_DEBUG_STREAM Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &); #endif diff --git a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp b/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp index 3ce8c3942d2..30c4181d5f4 100644 --- a/tests/auto/corelib/tools/qpoint/tst_qpoint.cpp +++ b/tests/auto/corelib/tools/qpoint/tst_qpoint.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. @@ -31,6 +31,8 @@ #include +#include + class tst_QPoint : public QObject { Q_OBJECT @@ -45,6 +47,9 @@ private slots: void transposed(); + void toPointF_data(); + void toPointF(); + void rx(); void ry(); @@ -131,6 +136,30 @@ void tst_QPoint::getSet() QCOMPARE(point.y(), i); } +void tst_QPoint::toPointF_data() +{ + QTest::addColumn("input"); + QTest::addColumn("result"); + + auto row = [](int x, int y) { + QTest::addRow("(%d, %d)", x, y) << QPoint(x, y) << QPointF(x, y); + }; + constexpr std::array samples = {-1, 0, 1}; + for (int x : samples) { + for (int y : samples) { + row(x, y); + } + } +} + +void tst_QPoint::toPointF() +{ + QFETCH(const QPoint, input); + QFETCH(const QPointF, result); + + QCOMPARE(input.toPointF(), result); +} + void tst_QPoint::transposed() { QCOMPARE(QPoint(1, 2).transposed(), QPoint(2, 1)); diff --git a/tests/auto/corelib/tools/qsize/tst_qsize.cpp b/tests/auto/corelib/tools/qsize/tst_qsize.cpp index 83b4f1bd344..80dc4ebaee8 100644 --- a/tests/auto/corelib/tools/qsize/tst_qsize.cpp +++ b/tests/auto/corelib/tools/qsize/tst_qsize.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. @@ -29,6 +29,8 @@ #include #include +#include + Q_DECLARE_METATYPE(QMargins) class tst_QSize : public QObject @@ -47,6 +49,9 @@ private slots: void grownOrShrunkBy_data(); void grownOrShrunkBy(); + void toSizeF_data(); + void toSizeF(); + void transpose_data(); void transpose(); @@ -232,6 +237,30 @@ void tst_QSize::grownOrShrunkBy() QCOMPARE(shrunk.grownBy(margins), input); } +void tst_QSize::toSizeF_data() +{ + QTest::addColumn("input"); + QTest::addColumn("result"); + + auto row = [](int w, int h) { + QTest::addRow("(%d, %d)", w, h) << QSize(w, h) << QSizeF(w, h); + }; + constexpr std::array samples = {-1, 0, 1}; + for (int w : samples) { + for (int h : samples) { + row(w, h); + } + } +} + +void tst_QSize::toSizeF() +{ + QFETCH(const QSize, input); + QFETCH(const QSizeF, result); + + QCOMPARE(input.toSizeF(), result); +} + void tst_QSize::transpose_data() { QTest::addColumn("input1");