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>
This commit is contained in:
parent
6ecc3e76e8
commit
bc5a2336ab
3
dist/changes-5.1.0
vendored
3
dist/changes-5.1.0
vendored
@ -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.
|
||||
|
||||
-
|
||||
|
||||
|
@ -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
|
||||
*****************************************************************************/
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user