QSizePolicy: add a transposed() method

In some situations, this allows for nicer code. It's also possible to
make this constexpr in C++11, whereas the mutable transpose() would
require C++14-style constexpr.

The new function should also be faster, since it just swaps the member
variables.

Because of constexpr-function limitations, the way the return value is
constructed needs to depend on the level of the compiler's C++11 support.
This is not the only class that requires uniform init to provide a fully
constexpr interface (QUuid and QBasicAtomic come to mind), so this should
probably be generalized across Qt at some point.

Added tests.

[ChangeLog][QtWidgets][QSizePolicy] Added transposed() method.

Change-Id: Ic1077a0d5a861e7c63bd1daeeb42b97c3a2f71ef
Reviewed-by: Sérgio Martins <sergio.martins@kdab.com>
This commit is contained in:
Marc Mutz 2014-12-17 16:19:27 +01:00
parent ce3402b5ef
commit 8b9b27bced
3 changed files with 72 additions and 0 deletions

View File

@ -395,6 +395,18 @@ void QSizePolicy::setControlType(ControlType type)
\fn void QSizePolicy::transpose()
Swaps the horizontal and vertical policies and stretches.
\sa transposed()
*/
/*!
\fn QSizePolicy QSizePolicy::transposed()
\since 5.9
Returns a size policy object with the horizontal and vertical
policies and stretches swapped.
\sa transpose()
*/
/*!

View File

@ -49,11 +49,25 @@ QT_BEGIN_NAMESPACE
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54922
#if !defined(Q_CC_GNU) || defined(Q_CC_INTEL) || defined(Q_CC_CLANG) || Q_CC_GNU >= 408
# define QT_SIZEPOLICY_CONSTEXPR Q_DECL_CONSTEXPR
# if defined(Q_COMPILER_UNIFORM_INIT)
# define QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT Q_DECL_CONSTEXPR
# if defined(Q_COMPILER_CONSTEXPR)
# define QT_SIZEPOLICY_RETURN_BITS(E1, E2, E3, E4, E5, E6, E7, E8) \
return Bits{ E1, E2, E3, E4, E5, E6, E7, E8 }
# endif // constexpr && uniform-init
# endif // uniform-init
#endif
#ifndef QT_SIZEPOLICY_CONSTEXPR
# define QT_SIZEPOLICY_CONSTEXPR
#endif
#ifndef QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT
# define QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT
#endif
#ifndef QT_SIZEPOLICY_RETURN_BITS
# define QT_SIZEPOLICY_RETURN_BITS(E1, E2, E3, E4, E5, E6, E7, E8) \
const Bits result = { E1, E2, E3, E4, E5, E6, E7, E8 }; return result
#endif
class QVariant;
class QSizePolicy;
@ -145,6 +159,13 @@ public:
void setRetainSizeWhenHidden(bool retainSize) { bits.retainSizeWhenHidden = retainSize; }
void transpose();
#ifndef Q_QDOC
QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT
#endif
QSizePolicy transposed() const Q_DECL_NOTHROW Q_REQUIRED_RESULT
{
return QSizePolicy(bits.transposed());
}
private:
#ifndef QT_NO_DATASTREAM
@ -152,6 +173,8 @@ private:
friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
#endif
QT_SIZEPOLICY_CONSTEXPR QSizePolicy(int i) : data(i) { }
struct Bits;
QT_SIZEPOLICY_CONSTEXPR explicit QSizePolicy(Bits b) Q_DECL_NOTHROW : bits(b) { }
struct Bits {
quint32 horStretch : 8;
@ -162,6 +185,19 @@ private:
quint32 hfw : 1;
quint32 wfh : 1;
quint32 retainSizeWhenHidden : 1;
QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT
Bits transposed() const Q_DECL_NOTHROW
{
QT_SIZEPOLICY_RETURN_BITS(verStretch, // \ swap
horStretch, // /
verPolicy, // \ swap
horPolicy, // /
ctype,
hfw, // \ don't swap (historic behavior)
wfh, // /
retainSizeWhenHidden);
}
};
union {
Bits bits;
@ -196,6 +232,8 @@ inline void QSizePolicy::transpose() {
}
#undef QT_SIZEPOLICY_CONSTEXPR
#undef QT_SIZEPOLICY_CONSTEXPR_AND_UNIFORM_INIT
#undef QT_SIZEPOLICY_RETURN_BITS
QT_END_NAMESPACE

View File

@ -45,6 +45,8 @@ private Q_SLOTS:
void defaultValues();
void getSetCheck_data() { data(); }
void getSetCheck();
void transposed_data() { data(); }
void transposed();
void dataStream();
void horizontalStretch();
void verticalStretch();
@ -161,6 +163,26 @@ void tst_QSizePolicy::getSetCheck()
QCOMPARE(sp.expandingDirections(), ed);
}
void tst_QSizePolicy::transposed()
{
FETCH_TEST_DATA;
const QSizePolicy tr = sp.transposed();
QCOMPARE(tr.horizontalPolicy(), vp); // swapped
QCOMPARE(tr.verticalPolicy(), hp); // swapped
QCOMPARE(tr.horizontalStretch(), vst); // swapped
QCOMPARE(tr.verticalStretch(), hst); // swapped
QCOMPARE(tr.controlType(), ct); // not swapped
QCOMPARE(tr.hasHeightForWidth(), hfw); // not swapped (historic behavior)
QCOMPARE(tr.hasWidthForHeight(), wfh); // not swapped (historic behavior)
QCOMPARE(tr.expandingDirections(), ed); // swapped
// destructive test - keep last:
sp.transpose();
QCOMPARE(sp, tr);
}
static void makeRow(QSizePolicy sp, QSizePolicy::Policy hp, QSizePolicy::Policy vp,
int hst, int vst, QSizePolicy::ControlType ct, bool hfw, bool wfh,
Qt::Orientations orients)