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 <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-03-18 08:16:20 +01:00
parent 98cbdae527
commit 2fb0135efa
6 changed files with 96 additions and 10 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.
@ -381,6 +381,15 @@ QT_BEGIN_NAMESPACE
\sa QPoint::operator/=() \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 QPoint stream functions
*****************************************************************************/ *****************************************************************************/
@ -532,7 +541,7 @@ size_t qHash(QPoint key, size_t seed) noexcept
Constructs a copy of the given \a point. 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 Rounds the coordinates of this point to the nearest integer, and
returns a QPoint object with the rounded coordinates. returns a QPoint object with the rounded coordinates.
\sa QPointF() \sa QPointF(), QPoint::toPointF()
*/ */
/*! /*!

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.
@ -48,6 +48,8 @@ struct CGPoint;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPointF;
class QPoint class QPoint
{ {
public: public:
@ -110,6 +112,7 @@ public:
#if defined(Q_OS_DARWIN) || defined(Q_QDOC) #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
[[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept; [[nodiscard]] Q_CORE_EXPORT CGPoint toCGPoint() const noexcept;
#endif #endif
[[nodiscard]] constexpr inline QPointF toPointF() const noexcept;
private: private:
friend class QTransform; friend class QTransform;
@ -417,6 +420,8 @@ constexpr inline QPointF &QPointF::operator/=(qreal divisor)
return *this; return *this;
} }
constexpr QPointF QPoint::toPointF() const noexcept { return *this; }
constexpr inline QPoint QPointF::toPoint() const constexpr inline QPoint QPointF::toPoint() const
{ {
return QPoint(qRound(xp), qRound(yp)); return QPoint(qRound(xp), qRound(yp));

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.
@ -403,6 +403,15 @@ QSize QSize::scaled(const QSize &s, Qt::AspectRatioMode mode) const noexcept
\sa grownBy() \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 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 Constructs a size with floating point accuracy from the given \a
size. 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 Note that the coordinates in the returned size will be rounded to
the nearest integer. the nearest integer.
\sa QSizeF() \sa QSizeF(), QSize::toSizeF()
*/ */
/*! /*!

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.
@ -50,6 +50,7 @@ struct CGSize;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSizeF;
class Q_CORE_EXPORT QSize class Q_CORE_EXPORT QSize
{ {
@ -109,6 +110,8 @@ public:
[[nodiscard]] CGSize toCGSize() const noexcept; [[nodiscard]] CGSize toCGSize() const noexcept;
#endif #endif
[[nodiscard]] inline constexpr QSizeF toSizeF() const noexcept;
private: private:
int wd; int wd;
int ht; int ht;
@ -420,6 +423,8 @@ constexpr inline QSize QSizeF::toSize() const noexcept
return QSize(qRound(wd), qRound(ht)); return QSize(qRound(wd), qRound(ht));
} }
constexpr QSizeF QSize::toSizeF() const noexcept { return *this; }
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &); Q_CORE_EXPORT QDebug operator<<(QDebug, const QSizeF &);
#endif #endif

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.
@ -31,6 +31,8 @@
#include <qpoint.h> #include <qpoint.h>
#include <array>
class tst_QPoint : public QObject class tst_QPoint : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -45,6 +47,9 @@ private slots:
void transposed(); void transposed();
void toPointF_data();
void toPointF();
void rx(); void rx();
void ry(); void ry();
@ -131,6 +136,30 @@ void tst_QPoint::getSet()
QCOMPARE(point.y(), i); QCOMPARE(point.y(), i);
} }
void tst_QPoint::toPointF_data()
{
QTest::addColumn<QPoint>("input");
QTest::addColumn<QPointF>("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() void tst_QPoint::transposed()
{ {
QCOMPARE(QPoint(1, 2).transposed(), QPoint(2, 1)); QCOMPARE(QPoint(1, 2).transposed(), QPoint(2, 1));

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.
@ -29,6 +29,8 @@
#include <QTest> #include <QTest>
#include <qsize.h> #include <qsize.h>
#include <array>
Q_DECLARE_METATYPE(QMargins) Q_DECLARE_METATYPE(QMargins)
class tst_QSize : public QObject class tst_QSize : public QObject
@ -47,6 +49,9 @@ private slots:
void grownOrShrunkBy_data(); void grownOrShrunkBy_data();
void grownOrShrunkBy(); void grownOrShrunkBy();
void toSizeF_data();
void toSizeF();
void transpose_data(); void transpose_data();
void transpose(); void transpose();
@ -232,6 +237,30 @@ void tst_QSize::grownOrShrunkBy()
QCOMPARE(shrunk.grownBy(margins), input); QCOMPARE(shrunk.grownBy(margins), input);
} }
void tst_QSize::toSizeF_data()
{
QTest::addColumn<QSize>("input");
QTest::addColumn<QSizeF>("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() void tst_QSize::transpose_data()
{ {
QTest::addColumn<QSize>("input1"); QTest::addColumn<QSize>("input1");