summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@digia.com>2012-12-10 16:53:16 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-01-07 16:11:19 +0100
commitbc5a2336ab9a681411a81a53d8639b4b79f80073 (patch)
tree17961ae7f53f8f96c567f4b3cde2f4bbf774e5f2
parent6ecc3e76e822b1545bcbfa78f5caf94156a026d2 (diff)
Provide operators for QMargins.
Provide addition/subtraction for QMargins as well as multiplication and division for int/qreal similar to QPoint. Add unary minus. Change-Id: If4eb831cfd610b34b5ca361619b1636031811d0a Reviewed-by: Oliver Wolff <oliver.wolff@digia.com> Reviewed-by: Mitch Curtis <mitch.curtis@digia.com>
-rw-r--r--dist/changes-5.1.03
-rw-r--r--src/corelib/tools/qmargins.cpp175
-rw-r--r--src/corelib/tools/qmargins.h92
-rw-r--r--tests/auto/corelib/tools/qmargins/tst_qmargins.cpp44
4 files changed, 314 insertions, 0 deletions
diff --git a/dist/changes-5.1.0 b/dist/changes-5.1.0
index 1124c9b9c5..4aa06cf342 100644
--- a/dist/changes-5.1.0
+++ b/dist/changes-5.1.0
@@ -50,6 +50,9 @@ QtCore
* Added marginsAdded(), marginsRemoved() and operators +, -, +=, -=
taking a QMargins object allowing for conveniently adding or removing
margins.
+ - QMargins:
+ * Added operators for adding and subtracting QMargins objects,
+ multiplication and division for int/qreal and unary minus.
-
diff --git a/src/corelib/tools/qmargins.cpp b/src/corelib/tools/qmargins.cpp
index 214466194a..e30675dc63 100644
--- a/src/corelib/tools/qmargins.cpp
+++ b/src/corelib/tools/qmargins.cpp
@@ -216,6 +216,181 @@ QT_BEGIN_NAMESPACE
\since 5.1
*/
+/*!
+ \fn const QMargins operator+(const QMargins &m1, const QMargins &m2)
+ \relates QMargins
+
+ Returns a QMargins object that is the sum of the given margins, \a m1
+ and \a m2; each component is added separately.
+
+ \sa QMargins::operator+=(), QMargins::operator-=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator-(const QMargins &m1, const QMargins &m2)
+ \relates QMargins
+
+ Returns a QMargins object that is formed by subtracting \a m2 from
+ \a m1; each component is subtracted separately.
+
+ \sa QMargins::operator+=(), QMargins::operator-=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator*(const QMargins &margins, int factor)
+ \relates QMargins
+
+ Returns a QMargins object that is formed by multiplying each component
+ of the given \a margins by \a factor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator*(int factor, const QMargins &margins)
+ \relates QMargins
+ \overload
+
+ Returns a QMargins object that is formed by multiplying each component
+ of the given \a margins by \a factor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator*(const QMargins &margins, qreal factor)
+ \relates QMargins
+ \overload
+
+ Returns a QMargins object that is formed by multiplying each component
+ of the given \a margins by \a factor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator*(qreal factor, const QMargins &margins)
+ \relates QMargins
+ \overload
+
+ Returns a QMargins object that is formed by multiplying each component
+ of the given \a margins by \a factor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator/(const QMargins &margins, int divisor)
+ \relates QMargins
+
+ Returns a QMargins object that is formed by dividing the components of
+ the given \a margins by the given \a divisor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn const QMargins operator/(const QMargins &, qreal)
+ \relates QMargins
+ \overload
+
+ Returns a QMargins object that is formed by dividing the components of
+ the given \a margins by the given \a divisor.
+
+ \sa QMargins::operator*=(), QMargins::operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins operator-(const QMargins &margins)
+ \relates QMargins
+
+ Returns a QMargin object that is formed by negating all components of \a margins.
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator+=(const QMargins &margins)
+
+ Add each component of \a margins to the respective component of this object
+ and returns a reference to it.
+
+ \sa operator-=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator-=(const QMargins &margins)
+
+ Subtract each component of \a margins from the respective component of this object
+ and returns a reference to it.
+
+ \sa operator+=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator*=(int factor)
+
+ Multiplies each component of this object by \a factor
+ and returns a reference to it.
+
+ \sa operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator*=(qreal factor)
+ \overload
+
+ Multiplies each component of this object by \a factor
+ and returns a reference to it.
+
+ \sa operator/=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator/=(int divisor)
+
+ Divides each component of this object by \a divisor
+ and returns a reference to it.
+
+ \sa operator*=()
+
+ \since 5.1
+*/
+
+/*!
+ \fn QMargins &operator/=(qreal divisor)
+
+ \overload
+
+ \sa operator*=()
+
+ \since 5.1
+*/
+
/*****************************************************************************
QMargins stream functions
*****************************************************************************/
diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h
index a933a60fed..dd6fb0261a 100644
--- a/src/corelib/tools/qmargins.h
+++ b/src/corelib/tools/qmargins.h
@@ -67,6 +67,15 @@ public:
void setRight(int right);
void setBottom(int bottom);
+ QMargins &operator+=(const QMargins &margins);
+ QMargins &operator-=(const QMargins &margins);
+ QMargins &operator+=(int);
+ QMargins &operator-=(int);
+ QMargins &operator*=(int);
+ QMargins &operator/=(int);
+ QMargins &operator*=(qreal);
+ QMargins &operator/=(qreal);
+
private:
int m_left;
int m_top;
@@ -177,6 +186,89 @@ inline QRect &QRect::operator-=(const QMargins &margins)
return *this;
}
+Q_DECL_CONSTEXPR inline QMargins operator+(const QMargins &m1, const QMargins &m2)
+{
+ return QMargins(m1.left() + m2.left(), m1.top() + m2.top(),
+ m1.right() + m2.right(), m1.bottom() + m2.bottom());
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator-(const QMargins &m1, const QMargins &m2)
+{
+ return QMargins(m1.left() - m2.left(), m1.top() - m2.top(),
+ m1.right() - m2.right(), m1.bottom() - m2.bottom());
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator*(const QMargins &margins, int factor)
+{
+ return QMargins(margins.left() * factor, margins.top() * factor,
+ margins.right() * factor, margins.bottom() * factor);
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator*(int factor, const QMargins &margins)
+{
+ return QMargins(margins.left() * factor, margins.top() * factor,
+ margins.right() * factor, margins.bottom() * factor);
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator*(const QMargins &margins, qreal factor)
+{
+ return QMargins(qRound(margins.left() * factor), qRound(margins.top() * factor),
+ qRound(margins.right() * factor), qRound(margins.bottom() * factor));
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator*(qreal factor, const QMargins &margins)
+{
+ return QMargins(qRound(margins.left() * factor), qRound(margins.top() * factor),
+ qRound(margins.right() * factor), qRound(margins.bottom() * factor));
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator/(const QMargins &margins, int divisor)
+{
+ return QMargins(margins.left() / divisor, margins.top() / divisor,
+ margins.right() / divisor, margins.bottom() / divisor);
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator/(const QMargins &margins, qreal divisor)
+{
+ return QMargins(qRound(margins.left() / divisor), qRound(margins.top() / divisor),
+ qRound(margins.right() / divisor), qRound(margins.bottom() / divisor));
+}
+
+inline QMargins &QMargins::operator+=(const QMargins &margins)
+{
+ return *this = *this + margins;
+}
+
+inline QMargins &QMargins::operator-=(const QMargins &margins)
+{
+ return *this = *this - margins;
+}
+
+inline QMargins &QMargins::operator*=(int factor)
+{
+ return *this = *this * factor;
+}
+
+inline QMargins &QMargins::operator/=(int divisor)
+{
+ return *this = *this / divisor;
+}
+
+inline QMargins &QMargins::operator*=(qreal factor)
+{
+ return *this = *this * factor;
+}
+
+inline QMargins &QMargins::operator/=(qreal divisor)
+{
+ return *this = *this / divisor;
+}
+
+Q_DECL_CONSTEXPR inline QMargins operator-(const QMargins &margins)
+{
+ return QMargins(-margins.left(), -margins.top(), -margins.right(), -margins.bottom());
+}
+
#ifndef QT_NO_DEBUG_STREAM
Q_CORE_EXPORT QDebug operator<<(QDebug, const QMargins &);
#endif
diff --git a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp
index 5e3b3d221e..b74fc53ed0 100644
--- a/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp
+++ b/tests/auto/corelib/tools/qmargins/tst_qmargins.cpp
@@ -50,6 +50,7 @@ class tst_QMargins : public QObject
private slots:
void getSetCheck();
void dataStreamCheck();
+ void operators();
};
// Testing get/set functions
@@ -75,6 +76,49 @@ void tst_QMargins::getSetCheck()
QCOMPARE(margins, QMargins(5, 0, 5, 0));
}
+void tst_QMargins::operators()
+{
+ const QMargins m1(12, 14, 16, 18);
+ const QMargins m2(2, 3, 4, 5);
+
+ const QMargins added = m1 + m2;
+ QCOMPARE(added, QMargins(14, 17, 20, 23));
+ QMargins a = m1;
+ a += m2;
+ QCOMPARE(a, added);
+
+ const QMargins subtracted = m1 - m2;
+ QCOMPARE(subtracted, QMargins(10, 11, 12, 13));
+ a = m1;
+ a -= m2;
+ QCOMPARE(a, subtracted);
+
+ const QMargins doubled = m1 * 2;
+ QCOMPARE(doubled, QMargins(24, 28, 32, 36));
+ QCOMPARE(2 * m1, doubled);
+ QCOMPARE(qreal(2) * m1, doubled);
+ QCOMPARE(m1 * qreal(2), doubled);
+
+ a = m1;
+ a *= 2;
+ QCOMPARE(a, doubled);
+ a = m1;
+ a *= qreal(2);
+ QCOMPARE(a, doubled);
+
+ const QMargins halved = m1 / 2;
+ QCOMPARE(halved, QMargins(6, 7, 8, 9));
+
+ a = m1;
+ a /= 2;
+ QCOMPARE(a, halved);
+ a = m1;
+ a /= qreal(2);
+ QCOMPARE(a, halved);
+
+ QCOMPARE(m1 + (-m1), QMargins());
+}
+
// Testing QDataStream operators
void tst_QMargins::dataStreamCheck()
{