From bc5a2336ab9a681411a81a53d8639b4b79f80073 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 10 Dec 2012 16:53:16 +0100 Subject: 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 Reviewed-by: Mitch Curtis --- src/corelib/tools/qmargins.cpp | 175 +++++++++++++++++++++++++++++++++++++++++ src/corelib/tools/qmargins.h | 92 ++++++++++++++++++++++ 2 files changed, 267 insertions(+) (limited to 'src') 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 -- cgit v1.2.3