diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-03-18 08:16:20 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-03-19 12:02:24 +0100 |
commit | 5dc570f8b16debfda916b10bb1235aeea9650699 (patch) | |
tree | 3bfbd47a4b67f108fef975436d80341880488ccc | |
parent | 2fb0135efa97708025dab7654a8b2f1350dc30bf (diff) |
QLine/QMargins: add toLineF/toMarginsF()
For symmetry with QLineF::toLine().
[ChangeLog][QtCore][QLine] Added toLineF().
[ChangeLog][QtCore][QMargins] Added toMarginsF().
Task-number: QTBUG-73160
Change-Id: I69051cdd2fe4a3c0a000ab86e363a2918a7aea7c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r-- | src/corelib/tools/qline.cpp | 15 | ||||
-rw-r--r-- | src/corelib/tools/qline.h | 7 | ||||
-rw-r--r-- | src/corelib/tools/qmargins.cpp | 15 | ||||
-rw-r--r-- | src/corelib/tools/qmargins.h | 8 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qline/tst_qline.cpp | 37 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qmargins/tst_qmargins.cpp | 36 |
6 files changed, 109 insertions, 9 deletions
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index bbedfa948e..72a926689b 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 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. @@ -263,6 +263,15 @@ QT_BEGIN_NAMESPACE \sa setP1(), setP2(), p1(), p2() */ +/*! + \fn QLine::toLineF() const + \since 6.4 + + Returns this line as a line with floating point accuracy. + + \sa QLineF::toLine() +*/ + #ifndef QT_NO_DEBUG_STREAM @@ -423,7 +432,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line) Construct a QLineF object from the given integer-based \a line. - \sa toLine() + \sa toLine(), QLine::toLineF() */ /*! @@ -463,7 +472,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line) Note that the returned line's start and end points are rounded to the nearest integer. - \sa QLineF() + \sa QLineF(), QLine::toLineF() */ /*! \fn qreal QLineF::x1() const diff --git a/src/corelib/tools/qline.h b/src/corelib/tools/qline.h index 426d69cd6d..93f3dde3c7 100644 --- a/src/corelib/tools/qline.h +++ b/src/corelib/tools/qline.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 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. @@ -44,6 +44,7 @@ QT_BEGIN_NAMESPACE +class QLineF; /******************************************************************************* * class QLine @@ -86,6 +87,8 @@ public: constexpr inline bool operator==(const QLine &d) const noexcept; constexpr inline bool operator!=(const QLine &d) const noexcept { return !(*this == d); } + [[nodiscard]] constexpr inline QLineF toLineF() const noexcept; + private: QPoint pt1, pt2; }; @@ -386,6 +389,8 @@ constexpr inline QPointF QLineF::pointAt(qreal t) const return QPointF(pt1.x() + (pt2.x() - pt1.x()) * t, pt1.y() + (pt2.y() - pt1.y()) * t); } +constexpr inline QLineF QLine::toLineF() const noexcept { return *this; } + constexpr inline QLine QLineF::toLine() const { return QLine(pt1.toPoint(), pt2.toPoint()); diff --git a/src/corelib/tools/qmargins.cpp b/src/corelib/tools/qmargins.cpp index 5109cfddce..2276ffaf32 100644 --- a/src/corelib/tools/qmargins.cpp +++ b/src/corelib/tools/qmargins.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. @@ -406,6 +406,15 @@ QT_BEGIN_NAMESPACE \since 5.1 */ +/*! + \fn QMargins::toMarginsF() const + \since 6.4 + + Returns these margins as margins with floating point accuracy. + + \sa QMarginsF::toMargins() +*/ + /***************************************************************************** QMargins stream functions *****************************************************************************/ @@ -501,6 +510,8 @@ QDebug operator<<(QDebug dbg, const QMargins &m) \fn QMarginsF::QMarginsF(const QMargins &margins) Constructs margins copied from the given \a margins. + + \sa QMargins::toMarginsF() */ /*! @@ -768,7 +779,7 @@ QDebug operator<<(QDebug dbg, const QMargins &m) Note that the components in the returned margins will be rounded to the nearest integer. - \sa QMarginsF() + \sa QMarginsF(), QMargins::toMarginsF() */ /***************************************************************************** diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index cf40b89ee0..c67da13d03 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.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. @@ -44,6 +44,8 @@ QT_BEGIN_NAMESPACE +class QMarginsF; + /***************************************************************************** QMargins class *****************************************************************************/ @@ -75,6 +77,8 @@ public: constexpr QMargins &operator*=(qreal) noexcept; constexpr QMargins &operator/=(qreal); + [[nodiscard]] constexpr inline QMarginsF toMarginsF() const noexcept; + private: int m_left; int m_top; @@ -516,6 +520,8 @@ constexpr inline QMarginsF operator-(const QMarginsF &margins) noexcept return QMarginsF(-margins.left(), -margins.top(), -margins.right(), -margins.bottom()); } +constexpr QMarginsF QMargins::toMarginsF() const noexcept { return *this; } + constexpr inline QMargins QMarginsF::toMargins() const noexcept { return QMargins(qRound(m_left), qRound(m_top), qRound(m_right), qRound(m_bottom)); diff --git a/tests/auto/corelib/tools/qline/tst_qline.cpp b/tests/auto/corelib/tools/qline/tst_qline.cpp index 67e71dfb26..288962b3b6 100644 --- a/tests/auto/corelib/tools/qline/tst_qline.cpp +++ b/tests/auto/corelib/tools/qline/tst_qline.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 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. @@ -30,6 +30,8 @@ #include <qline.h> #include <qmath.h> +#include <array> + class tst_QLine : public QObject { Q_OBJECT @@ -58,6 +60,9 @@ private slots: void testAngleTo_data(); void testSet(); + + void toLineF_data(); + void toLineF(); }; const qreal epsilon = sizeof(qreal) == sizeof(double) ? 1e-8 : 1e-4; @@ -502,5 +507,35 @@ void tst_QLine::testAngleTo_data() } } +void tst_QLine::toLineF_data() +{ + QTest::addColumn<QLine>("input"); + QTest::addColumn<QLineF>("result"); + + auto row = [](int x1, int y1, int x2, int y2) { + QTest::addRow("((%d, %d)->(%d, %d))", x1, y1, x2, y2) + << QLine(x1, y1, x2, y2) << QLineF(x1, y1, x2, y2); + }; + constexpr std::array samples = {-1, 0, 1}; + for (int x1 : samples) { + for (int y1 : samples) { + for (int x2 : samples) { + for (int y2 : samples) { + row(x1, y1, x2, y2); + } + } + } + } +} + +void tst_QLine::toLineF() +{ + QFETCH(const QLine, input); + QFETCH(const QLineF, result); + + QCOMPARE(input.toLineF(), result); +} + + QTEST_MAIN(tst_QLine) #include "tst_qline.moc" diff --git a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp index 8eaa4edd3b..8267e5d0e0 100644 --- a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp +++ b/tests/auto/corelib/tools/qmargins/tst_qmargins.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 <QTest> #include <qmargins.h> +#include <array> + Q_DECLARE_METATYPE(QMargins) class tst_QMargins : public QObject @@ -54,6 +56,9 @@ private slots: #endif void structuredBinding(); + + void toMarginsF_data(); + void toMarginsF(); }; // Testing get/set functions @@ -339,5 +344,34 @@ void tst_QMargins::structuredBinding() } } +void tst_QMargins::toMarginsF_data() +{ + QTest::addColumn<QMargins>("input"); + QTest::addColumn<QMarginsF>("result"); + + auto row = [](int x1, int y1, int x2, int y2) { + QTest::addRow("(%d, %d, %d, %d)", x1, y1, x2, y2) + << QMargins(x1, y1, x2, y2) << QMarginsF(x1, y1, x2, y2); + }; + constexpr std::array samples = {-1, 0, 1}; + for (int x1 : samples) { + for (int y1 : samples) { + for (int x2 : samples) { + for (int y2 : samples) { + row(x1, y1, x2, y2); + } + } + } + } +} + +void tst_QMargins::toMarginsF() +{ + QFETCH(const QMargins, input); + QFETCH(const QMarginsF, result); + + QCOMPARE(input.toMarginsF(), result); +} + QTEST_APPLESS_MAIN(tst_QMargins) #include "tst_qmargins.moc" |