From a1903eb94121e5cff29b70ee0f59044bdc7d5980 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 9 May 2022 17:09:53 +0200 Subject: [PATCH] QPolygon: add toPolygonF() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was forgotten when implementing QTBUG-73160, but suggested in passing in QTBUG-64. [ChangeLog][QtGui][QPolygon] Added toPolygonF(). Task-number: QTBUG-73160 Task-number: QTBUG-64 Change-Id: I9b33cf47a0d432aa842ab0f8337001c66e4ca41c Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- src/gui/painting/qpolygon.cpp | 10 ++++++++++ src/gui/painting/qpolygon.h | 6 ++++++ tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/gui/painting/qpolygon.cpp b/src/gui/painting/qpolygon.cpp index f124691f025..fd775de4a4b 100644 --- a/src/gui/painting/qpolygon.cpp +++ b/src/gui/painting/qpolygon.cpp @@ -442,6 +442,16 @@ QRect QPolygon::boundingRect() const return QRect(QPoint(minx,miny), QPoint(maxx,maxy)); } +/*! + \fn QPolygon::toPolygonF() const + \since 6.4 + + Returns this polygon as a polygon with floating point accuracy. + + \sa QPolygonF::toPolygon() +*/ + + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug dbg, const QPolygon &a) { diff --git a/src/gui/painting/qpolygon.h b/src/gui/painting/qpolygon.h index f7153a9f373..eeceff97ed8 100644 --- a/src/gui/painting/qpolygon.h +++ b/src/gui/painting/qpolygon.h @@ -51,6 +51,8 @@ class QTransform; class QRect; class QVariant; +class QPolygonF; + // We export each out-of-line method individually to prevent MSVC from // exporting the whole QList class. class QPolygon : public QList @@ -91,6 +93,8 @@ public: [[nodiscard]] Q_GUI_EXPORT QPolygon subtracted(const QPolygon &r) const; Q_GUI_EXPORT bool intersects(const QPolygon &r) const; + + [[nodiscard]] inline QPolygonF toPolygonF() const; }; Q_DECLARE_SHARED(QPolygon) @@ -159,6 +163,8 @@ public: }; Q_DECLARE_SHARED(QPolygonF) +QPolygonF QPolygon::toPolygonF() const { return QPolygonF(*this); } + #ifndef QT_NO_DEBUG_STREAM Q_GUI_EXPORT QDebug operator<<(QDebug, const QPolygonF &); #endif diff --git a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp index 4cde0aa5491..0ab9397b51a 100644 --- a/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp +++ b/tests/auto/gui/painting/qpolygon/tst_qpolygon.cpp @@ -41,6 +41,7 @@ class tst_QPolygon : public QObject private slots: void constructors(); + void toPolygonF(); void boundingRect_data(); void boundingRect(); void boundingRectF_data(); @@ -80,6 +81,17 @@ void tst_QPolygon::constructors() constructors_helperF(QRectF(1, 2, 3, 4)); } +void tst_QPolygon::toPolygonF() +{ + const QPolygon p = {{1, 1}, {-1, 1}, {-1, -1}, {1, -1}}; + auto pf = p.toPolygonF(); + static_assert(std::is_same_v); + QCOMPARE(pf.size(), p.size()); + auto p2 = pf.toPolygon(); + static_assert(std::is_same_v); + QCOMPARE(p, p2); +} + void tst_QPolygon::boundingRect_data() { QTest::addColumn("poly");