diff options
Diffstat (limited to 'src/corelib/tools/qmargins.h')
-rw-r--r-- | src/corelib/tools/qmargins.h | 120 |
1 files changed, 54 insertions, 66 deletions
diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index cf40b89ee0..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,40 +47,37 @@ 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 &m1, const QMargins &m2) noexcept + friend constexpr bool comparesEqual(const QMargins &lhs, const QMargins &rhs) 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; - } - - friend constexpr inline bool operator!=(const QMargins &m1, const QMargins &m2) noexcept - { - return !(m1 == m2); + 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<std::decay_t<M>, QMargins>, 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 (std::forward<M>(m).m_left); + return q23::forward_like<M>(m.m_left); else if constexpr (I == 1) - return (std::forward<M>(m).m_top); + return q23::forward_like<M>(m.m_top); else if constexpr (I == 2) - return (std::forward<M>(m).m_right); + return q23::forward_like<M>(m.m_right); else if constexpr (I == 3) - return (std::forward<M>(m).m_bottom); + return q23::forward_like<M>(m.m_bottom); } }; @@ -331,33 +300,50 @@ private: qreal m_right; qreal m_bottom; - friend constexpr inline bool operator==(const QMarginsF &lhs, const QMarginsF &rhs) noexcept + 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 qFuzzyCompare(lhs.left(), rhs.left()) - && qFuzzyCompare(lhs.top(), rhs.top()) - && qFuzzyCompare(lhs.right(), rhs.right()) - && qFuzzyCompare(lhs.bottom(), rhs.bottom()); + return qFuzzyIsNull(m.m_left) && qFuzzyIsNull(m.m_top) + && qFuzzyIsNull(m.m_right) && qFuzzyIsNull(m.m_bottom); } - friend constexpr inline bool operator!=(const QMarginsF &lhs, const QMarginsF &rhs) noexcept + friend constexpr bool comparesEqual(const QMarginsF &lhs, const QMarginsF &rhs) noexcept { - return !(lhs == rhs); + 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<std::decay_t<M>, QMarginsF>, 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 (std::forward<M>(m).m_left); + return q23::forward_like<M>(m.m_left); else if constexpr (I == 1) - return (std::forward<M>(m).m_top); + return q23::forward_like<M>(m.m_top); else if constexpr (I == 2) - return (std::forward<M>(m).m_right); + return q23::forward_like<M>(m.m_right); else if constexpr (I == 3) - return (std::forward<M>(m).m_bottom); + return q23::forward_like<M>(m.m_bottom); } }; @@ -516,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)); |