diff options
Diffstat (limited to 'src/corelib/tools/qline.h')
-rw-r--r-- | src/corelib/tools/qline.h | 129 |
1 files changed, 62 insertions, 67 deletions
diff --git a/src/corelib/tools/qline.h b/src/corelib/tools/qline.h index c3fb2fd5f5..03dac30e16 100644 --- a/src/corelib/tools/qline.h +++ b/src/corelib/tools/qline.h @@ -1,51 +1,14 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QLINE_H #define QLINE_H -#include <QtCore/qmath.h> - #include <QtCore/qpoint.h> QT_BEGIN_NAMESPACE +class QLineF; /******************************************************************************* * class QLine @@ -75,23 +38,33 @@ public: inline void translate(const QPoint &p); inline void translate(int dx, int dy); - Q_REQUIRED_RESULT constexpr inline QLine translated(const QPoint &p) const; - Q_REQUIRED_RESULT constexpr inline QLine translated(int dx, int dy) const; + [[nodiscard]] constexpr inline QLine translated(const QPoint &p) const; + [[nodiscard]] constexpr inline QLine translated(int dx, int dy) const; - Q_REQUIRED_RESULT constexpr inline QPoint center() const; + [[nodiscard]] constexpr inline QPoint center() const; inline void setP1(const QPoint &p1); inline void setP2(const QPoint &p2); inline void setPoints(const QPoint &p1, const QPoint &p2); inline void setLine(int x1, int y1, int x2, int y2); - constexpr inline bool operator==(const QLine &d) const; - constexpr inline bool operator!=(const QLine &d) const { return !(*this == d); } +#if QT_CORE_REMOVED_SINCE(6, 8) + constexpr inline bool operator==(const QLine &d) const noexcept; + constexpr inline bool operator!=(const QLine &d) const noexcept { return !operator==(d); } +#endif + + [[nodiscard]] constexpr inline QLineF toLineF() const noexcept; private: + friend constexpr bool comparesEqual(const QLine &lhs, const QLine &rhs) noexcept + { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; } +#if !QT_CORE_REMOVED_SINCE(6, 8) + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLine) +#endif + QPoint pt1, pt2; }; -Q_DECLARE_TYPEINFO(QLine, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QLine, Q_PRIMITIVE_TYPE); /******************************************************************************* * class QLine inline members @@ -196,10 +169,12 @@ inline void QLine::setLine(int aX1, int aY1, int aX2, int aY2) pt2 = QPoint(aX2, aY2); } -constexpr inline bool QLine::operator==(const QLine &d) const +#if QT_CORE_REMOVED_SINCE(6, 8) +constexpr inline bool QLine::operator==(const QLine &d) const noexcept { - return pt1 == d.pt1 && pt2 == d.pt2; + return comparesEqual(*this, d); } +#endif #ifndef QT_NO_DEBUG_STREAM Q_CORE_EXPORT QDebug operator<<(QDebug d, const QLine &p); @@ -225,7 +200,7 @@ public: constexpr inline QLineF(qreal x1, qreal y1, qreal x2, qreal y2); constexpr inline QLineF(const QLine &line) : pt1(line.p1()), pt2(line.p2()) { } - Q_REQUIRED_RESULT static QLineF fromPolar(qreal length, qreal angle); + [[nodiscard]] static QLineF fromPolar(qreal length, qreal angle); constexpr bool isNull() const; @@ -249,8 +224,8 @@ public: qreal angleTo(const QLineF &l) const; - Q_REQUIRED_RESULT QLineF unitVector() const; - Q_REQUIRED_RESULT constexpr inline QLineF normalVector() const; + [[nodiscard]] QLineF unitVector() const; + [[nodiscard]] constexpr inline QLineF normalVector() const; IntersectionType intersects(const QLineF &l, QPointF *intersectionPoint = nullptr) const; @@ -258,25 +233,43 @@ public: inline void translate(const QPointF &p); inline void translate(qreal dx, qreal dy); - Q_REQUIRED_RESULT constexpr inline QLineF translated(const QPointF &p) const; - Q_REQUIRED_RESULT constexpr inline QLineF translated(qreal dx, qreal dy) const; + [[nodiscard]] constexpr inline QLineF translated(const QPointF &p) const; + [[nodiscard]] constexpr inline QLineF translated(qreal dx, qreal dy) const; - Q_REQUIRED_RESULT constexpr inline QPointF center() const; + [[nodiscard]] constexpr inline QPointF center() const; inline void setP1(const QPointF &p1); inline void setP2(const QPointF &p2); inline void setPoints(const QPointF &p1, const QPointF &p2); inline void setLine(qreal x1, qreal y1, qreal x2, qreal y2); +#if QT_CORE_REMOVED_SINCE(6, 8) constexpr inline bool operator==(const QLineF &d) const; - constexpr inline bool operator!=(const QLineF &d) const { return !(*this == d); } + constexpr inline bool operator!=(const QLineF &d) const { return !operator==(d); } +#endif constexpr QLine toLine() const; private: + friend constexpr bool comparesEqual(const QLineF &lhs, const QLineF &rhs) noexcept + { return lhs.pt1 == rhs.pt1 && lhs.pt2 == rhs.pt2; } +#if !QT_CORE_REMOVED_SINCE(6, 8) + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF) +#endif + + friend constexpr bool comparesEqual(const QLineF &lhs, const QLine &rhs) noexcept + { return comparesEqual(lhs, rhs.toLineF()); } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QLineF, QLine) + + friend constexpr bool qFuzzyCompare(const QLineF &lhs, const QLineF &rhs) noexcept + { return qFuzzyCompare(lhs.pt1, rhs.pt1) && qFuzzyCompare(lhs.pt2, rhs.pt2); } + + friend constexpr bool qFuzzyIsNull(const QLineF &line) noexcept + { return qFuzzyCompare(line.pt1, line.pt2); } + QPointF pt1, pt2; }; -Q_DECLARE_TYPEINFO(QLineF, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QLineF, Q_PRIMITIVE_TYPE); /******************************************************************************* * class QLineF inline members @@ -318,7 +311,7 @@ constexpr inline qreal QLineF::y2() const constexpr inline bool QLineF::isNull() const { - return qFuzzyCompare(pt1.x(), pt2.x()) && qFuzzyCompare(pt1.y(), pt2.y()); + return qFuzzyCompare(pt1, pt2); } constexpr inline QPointF QLineF::p1() const @@ -372,15 +365,15 @@ constexpr inline QPointF QLineF::center() const return QPointF(0.5 * pt1.x() + 0.5 * pt2.x(), 0.5 * pt1.y() + 0.5 * pt2.y()); } -QT_WARNING_DISABLE_FLOAT_COMPARE - inline void QLineF::setLength(qreal len) { - const qreal oldlength = qSqrt(dx() * dx() + dy() * dy()); - if (!oldlength) - return; - const qreal factor = len / oldlength; - pt2 = QPointF(pt1.x() + dx() * factor, pt1.y() + dy() * factor); + Q_ASSERT(qIsFinite(len)); + const qreal oldLength = length(); + Q_ASSERT(qIsFinite(oldLength)); + // Scale len by dx() / length() and dy() / length(), two O(1) quantities, + // rather than scaling dx() and dy() by len / length(), which might overflow. + if (oldLength > 0) + pt2 = QPointF(pt1.x() + len * (dx() / oldLength), pt1.y() + len * (dy() / oldLength)); } constexpr inline QPointF QLineF::pointAt(qreal t) const @@ -388,6 +381,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()); @@ -416,12 +411,12 @@ inline void QLineF::setLine(qreal aX1, qreal aY1, qreal aX2, qreal aY2) pt2 = QPointF(aX2, aY2); } - +#if QT_CORE_REMOVED_SINCE(6, 8) constexpr inline bool QLineF::operator==(const QLineF &d) const { - return pt1 == d.pt1 && pt2 == d.pt2; + return comparesEqual(*this, d); } - +#endif #ifndef QT_NO_DEBUG_STREAM |