diff options
Diffstat (limited to 'src/corelib/tools/qmargins.h')
-rw-r--r-- | src/corelib/tools/qmargins.h | 196 |
1 files changed, 119 insertions, 77 deletions
diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index de7a96e458..3b29860d66 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.h @@ -1,49 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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 QMARGINS_H #define QMARGINS_H +#include <QtCore/qcompare.h> #include <QtCore/qnamespace.h> +#include <QtCore/q20type_traits.h> +#include <QtCore/q23utility.h> + QT_BEGIN_NAMESPACE +QT_ENABLE_P0846_SEMANTICS_FOR(get) + +class QMarginsF; + /***************************************************************************** QMargins class *****************************************************************************/ @@ -75,17 +47,41 @@ public: constexpr QMargins &operator*=(qreal) noexcept; constexpr QMargins &operator/=(qreal); + [[nodiscard]] constexpr inline QMarginsF toMarginsF() const noexcept; + private: int m_left; int m_top; int m_right; int m_bottom; - friend constexpr inline bool operator==(const QMargins &, const QMargins &) noexcept; - friend constexpr inline bool operator!=(const QMargins &, const QMargins &) noexcept; + friend constexpr bool comparesEqual(const QMargins &lhs, const QMargins &rhs) noexcept + { + return lhs.m_left == rhs.m_left + && lhs.m_top == rhs.m_top + && lhs.m_right == rhs.m_right + && lhs.m_bottom == rhs.m_bottom; + } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMargins) + + template <std::size_t I, + typename M, + std::enable_if_t<(I < 4), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<M>, QMargins>, bool> = true> + friend constexpr decltype(auto) get(M &&m) noexcept + { + if constexpr (I == 0) + return q23::forward_like<M>(m.m_left); + else if constexpr (I == 1) + return q23::forward_like<M>(m.m_top); + else if constexpr (I == 2) + return q23::forward_like<M>(m.m_right); + else if constexpr (I == 3) + return q23::forward_like<M>(m.m_bottom); + } }; -Q_DECLARE_TYPEINFO(QMargins, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QMargins, Q_RELOCATABLE_TYPE); /***************************************************************************** QMargins stream functions @@ -132,24 +128,6 @@ constexpr inline void QMargins::setRight(int aright) noexcept constexpr inline void QMargins::setBottom(int abottom) noexcept { m_bottom = abottom; } -constexpr inline bool operator==(const QMargins &m1, const QMargins &m2) noexcept -{ - return - m1.m_left == m2.m_left && - m1.m_top == m2.m_top && - m1.m_right == m2.m_right && - m1.m_bottom == m2.m_bottom; -} - -constexpr inline bool operator!=(const QMargins &m1, const QMargins &m2) noexcept -{ - return - m1.m_left != m2.m_left || - m1.m_top != m2.m_top || - m1.m_right != m2.m_right || - m1.m_bottom != m2.m_bottom; -} - constexpr inline QMargins operator+(const QMargins &m1, const QMargins &m2) noexcept { return QMargins(m1.left() + m2.left(), m1.top() + m2.top(), @@ -302,10 +280,10 @@ public: constexpr qreal right() const noexcept; constexpr qreal bottom() const noexcept; - constexpr void setLeft(qreal left) noexcept; - constexpr void setTop(qreal top) noexcept; - constexpr void setRight(qreal right) noexcept; - constexpr void setBottom(qreal bottom) noexcept; + constexpr void setLeft(qreal aleft) noexcept; + constexpr void setTop(qreal atop) noexcept; + constexpr void setRight(qreal aright) noexcept; + constexpr void setBottom(qreal abottom) noexcept; constexpr QMarginsF &operator+=(const QMarginsF &margins) noexcept; constexpr QMarginsF &operator-=(const QMarginsF &margins) noexcept; @@ -321,9 +299,55 @@ private: qreal m_top; qreal m_right; qreal m_bottom; + + QT_WARNING_PUSH + QT_WARNING_DISABLE_FLOAT_COMPARE + friend constexpr bool qFuzzyCompare(const QMarginsF &lhs, const QMarginsF &rhs) noexcept + { + return ((!lhs.m_left || !rhs.m_left) ? qFuzzyIsNull(lhs.m_left - rhs.m_left) + : qFuzzyCompare(lhs.m_left, rhs.m_left)) + && ((!lhs.m_top || !rhs.m_top) ? qFuzzyIsNull(lhs.m_top - rhs.m_top) + : qFuzzyCompare(lhs.m_top, rhs.m_top)) + && ((!lhs.m_right || !rhs.m_right) ? qFuzzyIsNull(lhs.m_right - rhs.m_right) + : qFuzzyCompare(lhs.m_right, rhs.m_right)) + && ((!lhs.m_bottom || !rhs.m_bottom) ? qFuzzyIsNull(lhs.m_bottom - rhs.m_bottom) + : qFuzzyCompare(lhs.m_bottom, rhs.m_bottom)); + } + QT_WARNING_POP + friend constexpr bool qFuzzyIsNull(const QMarginsF &m) noexcept + { + return qFuzzyIsNull(m.m_left) && qFuzzyIsNull(m.m_top) + && qFuzzyIsNull(m.m_right) && qFuzzyIsNull(m.m_bottom); + } + + friend constexpr bool comparesEqual(const QMarginsF &lhs, const QMarginsF &rhs) noexcept + { + return qFuzzyCompare(lhs, rhs); + } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMarginsF) + + friend constexpr bool comparesEqual(const QMarginsF &lhs, const QMargins &rhs) noexcept + { return comparesEqual(lhs, rhs.toMarginsF()); } + Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QMarginsF, QMargins) + + template <std::size_t I, + typename M, + std::enable_if_t<(I < 4), bool> = true, + std::enable_if_t<std::is_same_v<q20::remove_cvref_t<M>, QMarginsF>, bool> = true> + friend constexpr decltype(auto) get(M &&m) noexcept + { + if constexpr (I == 0) + return q23::forward_like<M>(m.m_left); + else if constexpr (I == 1) + return q23::forward_like<M>(m.m_top); + else if constexpr (I == 2) + return q23::forward_like<M>(m.m_right); + else if constexpr (I == 3) + return q23::forward_like<M>(m.m_bottom); + } }; -Q_DECLARE_TYPEINFO(QMarginsF, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QMarginsF, Q_RELOCATABLE_TYPE); /***************************************************************************** QMarginsF stream functions @@ -375,19 +399,6 @@ constexpr inline void QMarginsF::setRight(qreal aright) noexcept constexpr inline void QMarginsF::setBottom(qreal abottom) noexcept { m_bottom = abottom; } -constexpr inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept -{ - return qFuzzyCompare(lhs.left(), rhs.left()) - && qFuzzyCompare(lhs.top(), rhs.top()) - && qFuzzyCompare(lhs.right(), rhs.right()) - && qFuzzyCompare(lhs.bottom(), rhs.bottom()); -} - -constexpr inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept -{ - return !operator==(lhs, rhs); -} - constexpr inline QMarginsF operator+(const QMarginsF &lhs, const QMarginsF &rhs) noexcept { return QMarginsF(lhs.left() + rhs.left(), lhs.top() + rhs.top(), @@ -432,6 +443,7 @@ constexpr inline QMarginsF operator*(qreal lhs, const QMarginsF &rhs) noexcept constexpr inline QMarginsF operator/(const QMarginsF &lhs, qreal divisor) { + Q_ASSERT(divisor < 0 || divisor > 0); return QMarginsF(lhs.left() / divisor, lhs.top() / divisor, lhs.right() / divisor, lhs.bottom() / divisor); } @@ -490,6 +502,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)); @@ -501,4 +515,32 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, const QMarginsF &); QT_END_NAMESPACE +/***************************************************************************** + QMargins/QMarginsF tuple protocol + *****************************************************************************/ + +namespace std { + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QMargins)> : public integral_constant<size_t, 4> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + template <> + class tuple_element<3, QT_PREPEND_NAMESPACE(QMargins)> { public: using type = int; }; + + template <> + class tuple_size<QT_PREPEND_NAMESPACE(QMarginsF)> : public integral_constant<size_t, 4> {}; + template <> + class tuple_element<0, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<1, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<2, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; + template <> + class tuple_element<3, QT_PREPEND_NAMESPACE(QMarginsF)> { public: using type = QT_PREPEND_NAMESPACE(qreal); }; +} + #endif // QMARGINS_H |