summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qmargins.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qmargins.h')
-rw-r--r--src/corelib/tools/qmargins.h129
1 files changed, 59 insertions, 70 deletions
diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h
index d947e2da8e..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);
}
};
@@ -311,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;
@@ -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);
}
};
@@ -457,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);
}
@@ -515,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));