rework QSizePolicy

performance after the changes unknown but the class is subject to change

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-14 08:23:43 +02:00
parent 04d4b6c4b4
commit 6cf349e278
6 changed files with 148 additions and 149 deletions

View file

@ -691,6 +691,7 @@ set(GUI_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/kernel/qx11info_x11.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qx11info_x11.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel/qkeymapper_x11.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qkeymapper_x11.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel/qeventdispatcher_x11.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qeventdispatcher_x11.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel/qsizepolicy.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel/qt_x11.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qt_x11.cpp
${CMAKE_CURRENT_SOURCE_DIR}/math3d/qgenericmatrix.cpp ${CMAKE_CURRENT_SOURCE_DIR}/math3d/qgenericmatrix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/math3d/qmatrix4x4.cpp ${CMAKE_CURRENT_SOURCE_DIR}/math3d/qmatrix4x4.cpp

View file

@ -1383,65 +1383,6 @@ QSize QLayout::closestAcceptableSize(const QWidget *widget, const QSize &size)
Use sizeConstraint() instead. Use sizeConstraint() instead.
*/ */
void QSizePolicy::setControlType(ControlType type)
{
/*
The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
etc. In memory, we pack it onto the available bits (CTSize) in
setControlType(), and unpack it here.
Example:
0x00000001 maps to 0x00000000
0x00000002 maps to 0x00000200
0x00000004 maps to 0x00000400
0x00000008 maps to 0x00000600
etc.
*/
int i = 0;
while (true) {
if (type & (0x1 << i)) {
data = (data & ~CTMask) | (i << CTShift);
return;
}
++i;
}
}
QSizePolicy::ControlType QSizePolicy::controlType() const
{
return QSizePolicy::ControlType(0x1 << ((data & CTMask) >> CTShift));
}
#ifndef QT_NO_DATASTREAM
/*!
\relates QSizePolicy
\since 4.2
Writes the size \a policy to the data stream \a stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator<<(QDataStream &stream, const QSizePolicy &policy)
{
return stream << policy.data;
}
/*!
\relates QSizePolicy
\since 4.2
Reads the size \a policy from the data stream \a stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator>>(QDataStream &stream, QSizePolicy &policy)
{
return stream >> policy.data;
}
#endif // QT_NO_DATASTREAM
QT_END_NAMESPACE QT_END_NAMESPACE
#include "moc_qlayout.h" #include "moc_qlayout.h"

View file

@ -54,14 +54,6 @@ inline static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &size)
return toLayoutItemRect(priv, QRect(QPoint(0, 0), size)).size(); return toLayoutItemRect(priv, QRect(QPoint(0, 0), size)).size();
} }
/*!
Returns a QVariant storing this QSizePolicy.
*/
QSizePolicy::operator QVariant() const
{
return QVariant(QVariant::SizePolicy, this);
}
/*! /*!
\class QLayoutItem \class QLayoutItem
\brief The QLayoutItem class provides an abstract item that a \brief The QLayoutItem class provides an abstract item that a

View file

@ -3,20 +3,27 @@
** Copyright (C) 2015 The Qt Company Ltd. ** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2016 Ivailo Monev ** Copyright (C) 2016 Ivailo Monev
** **
** This file is part of the documentation of the Katie Toolkit. ** This file is part of the QtGui module of the Katie Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** **
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License Usage
** This file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$ ** $QT_END_LICENSE$
** **
****************************************************************************/ ****************************************************************************/
#include "qsizepolicy.h"
#include "qvariant.h"
QT_BEGIN_NAMESPACE
/*! /*!
\class QSizePolicy \class QSizePolicy
\brief The QSizePolicy class is a layout attribute describing horizontal \brief The QSizePolicy class is a layout attribute describing horizontal
@ -275,13 +282,23 @@
*/ */
/*! /*!
\fn bool QSizePolicy::operator==(const QSizePolicy &other) const
Returns true if this policy is equal to \a other; otherwise Returns true if this policy is equal to \a other; otherwise
returns false. returns false.
\sa operator!=() \sa operator!=()
*/ */
bool QSizePolicy::operator==(const QSizePolicy &s) const
{
return (
horzPolicy == s.horzPolicy &&
vertPolicy == s.vertPolicy &&
hfw == s.hfw &&
wfh == s.wfh &&
verStretch == s.verStretch &&
horStretch == s.horStretch &&
ctype == s.ctype
);
}
/*! /*!
\fn bool QSizePolicy::operator!=(const QSizePolicy &other) const \fn bool QSizePolicy::operator!=(const QSizePolicy &other) const
@ -326,12 +343,6 @@
\sa verticalStretch(), setHorizontalStretch(), setVerticalPolicy() \sa verticalStretch(), setHorizontalStretch(), setVerticalPolicy()
*/ */
/*!
\fn void QSizePolicy::transpose()
Swaps the horizontal and vertical policies and stretches.
*/
/*! /*!
\enum QSizePolicy::ControlType \enum QSizePolicy::ControlType
\since 4.3 \since 4.3
@ -357,3 +368,76 @@
\sa setControlType(), controlType() \sa setControlType(), controlType()
*/ */
/*!
Returns a QVariant storing this QSizePolicy.
*/
QSizePolicy::operator QVariant() const
{
return QVariant(QVariant::SizePolicy, this);
}
/*!
Swaps the horizontal and vertical policies and stretches.
*/
void QSizePolicy::transpose()
{
Policy hData = horizontalPolicy();
Policy vData = verticalPolicy();
int hStretch = horizontalStretch();
int vStretch = verticalStretch();
setHorizontalPolicy(vData);
setVerticalPolicy(hData);
setHorizontalStretch(vStretch);
setVerticalStretch(hStretch);
}
#ifndef QT_NO_DATASTREAM
/*!
\relates QSizePolicy
\since 4.2
Writes the size \a policy to the data stream \a stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator<<(QDataStream &stream, const QSizePolicy &policy)
{
stream << static_cast<quint32>(policy.horzPolicy);
stream << static_cast<quint32>(policy.vertPolicy);
stream << policy.hfw;
stream << policy.wfh;
stream << policy.verStretch;
stream << policy.horStretch;
stream << policy.ctype;
return stream;
}
/*!
\relates QSizePolicy
\since 4.2
Reads the size \a policy from the data stream \a stream.
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
*/
QDataStream &operator>>(QDataStream &stream, QSizePolicy &policy)
{
quint32 horzPolicy = 0;
quint32 vertPolicy = 0;
quint32 ctype = 0;
stream >> horzPolicy;
stream >> vertPolicy;
policy.horzPolicy = static_cast<QSizePolicy::Policy>(horzPolicy);
policy.vertPolicy = static_cast<QSizePolicy::Policy>(vertPolicy);
stream >> policy.hfw;
stream >> policy.wfh;
stream >> policy.verStretch;
stream >> policy.horStretch;
stream >> ctype;
policy.ctype = static_cast<QSizePolicy::ControlType>(ctype);
return stream;
}
#endif // QT_NO_DATASTREAM
QT_END_NAMESPACE

View file

@ -35,19 +35,6 @@ class Q_GUI_EXPORT QSizePolicy
Q_GADGET Q_GADGET
Q_ENUMS(Policy) Q_ENUMS(Policy)
private:
enum SizePolicyMasks {
HSize = 4,
HMask = 0x0f,
VMask = HMask << HSize,
CTShift = 9,
CTSize = 5,
CTMask = ((0x1 << CTSize) - 1) << CTShift,
WFHShift = CTShift + CTSize,
UnusedShift = WFHShift + 1,
UnusedSize = 1
};
public: public:
enum PolicyFlag { enum PolicyFlag {
GrowFlag = 1, GrowFlag = 1,
@ -85,21 +72,38 @@ public:
}; };
Q_DECLARE_FLAGS(ControlTypes, ControlType) Q_DECLARE_FLAGS(ControlTypes, ControlType)
QSizePolicy() : data(0) { } QSizePolicy()
: horzPolicy(QSizePolicy::Fixed),
vertPolicy(QSizePolicy::Fixed),
hfw(false),
wfh(false),
verStretch(0),
horStretch(0),
ctype(QSizePolicy::DefaultType)
{
}
QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) QSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical, QSizePolicy::ControlType type = QSizePolicy::DefaultType)
: data(horizontal | (vertical << HSize)) { setControlType(type); } : horzPolicy(horizontal),
vertPolicy(vertical),
hfw(false),
wfh(false),
verStretch(0),
horStretch(0),
ctype(type)
{
}
Policy horizontalPolicy() const { return static_cast<Policy>(data & HMask); } inline QSizePolicy::Policy horizontalPolicy() const { return horzPolicy; }
Policy verticalPolicy() const { return static_cast<Policy>((data & VMask) >> HSize); } inline QSizePolicy::Policy verticalPolicy() const { return vertPolicy; }
ControlType controlType() const; inline QSizePolicy::ControlType controlType() const { return ctype; }
void setHorizontalPolicy(Policy d) { data = (data & ~HMask) | d; } inline void setHorizontalPolicy(QSizePolicy::Policy d) { horzPolicy = d; }
void setVerticalPolicy(Policy d) { data = (data & ~(HMask << HSize)) | (d << HSize); } inline void setVerticalPolicy(QSizePolicy::Policy d) { vertPolicy = d; }
void setControlType(ControlType type); inline void setControlType(ControlType type) { ctype = type; }
Qt::Orientations expandingDirections() const { Qt::Orientations expandingDirections() const {
Qt::Orientations result; Qt::Orientations result = 0;
if (verticalPolicy() & ExpandFlag) if (verticalPolicy() & ExpandFlag)
result |= Qt::Vertical; result |= Qt::Vertical;
if (horizontalPolicy() & ExpandFlag) if (horizontalPolicy() & ExpandFlag)
@ -107,66 +111,43 @@ public:
return result; return result;
} }
void setHeightForWidth(bool b) { data = b ? (data | (1 << 2*HSize)) : (data & ~(1 << 2*HSize)); } inline void setHeightForWidth(bool b) { hfw = b; }
bool hasHeightForWidth() const { return data & (1 << 2*HSize); } inline bool hasHeightForWidth() const { return hfw; }
void setWidthForHeight(bool b) { data = b ? (data | (1 << (WFHShift))) : (data & ~(1 << (WFHShift))); } inline void setWidthForHeight(bool b) { wfh = b; }
bool hasWidthForHeight() const { return data & (1 << (WFHShift)); } inline bool hasWidthForHeight() const { return wfh; }
bool operator==(const QSizePolicy& s) const { return data == s.data; } bool operator==(const QSizePolicy &s) const;
bool operator!=(const QSizePolicy& s) const { return data != s.data; } inline bool operator!=(const QSizePolicy &s) const { return !(operator==(s)); }
operator QVariant() const; // implemented in qabstractlayout.cpp operator QVariant() const;
int horizontalStretch() const { return data >> 24; } inline int horizontalStretch() const { return horStretch; }
int verticalStretch() const { return (data >> 16) & 0xff; } inline int verticalStretch() const { return verStretch; }
void setHorizontalStretch(uchar stretchFactor) { data = (data&0x00ffffff) | (uint(stretchFactor)<<24); } inline void setHorizontalStretch(quint8 stretchFactor) { horStretch = stretchFactor; }
void setVerticalStretch(uchar stretchFactor) { data = (data&0xff00ffff) | (uint(stretchFactor)<<16); } inline void setVerticalStretch(quint8 stretchFactor) { verStretch = stretchFactor; }
void transpose(); void transpose();
private: private:
#ifndef QT_NO_DATASTREAM #ifndef QT_NO_DATASTREAM
friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
#endif #endif
QSizePolicy(int i) : data(i) { } QSizePolicy::Policy horzPolicy;
QSizePolicy::Policy vertPolicy;
quint32 data; bool hfw;
/* Qt5: Use bit flags instead, keep it here for improved readability for now. bool wfh;
We can maybe change it for Qt4, but we'd have to be careful, since the behaviour quint8 verStretch;
is implementation defined. It usually varies between little- and big-endian compilers, but quint8 horStretch;
it might also not vary. QSizePolicy::ControlType ctype;
quint32 horzPolicy : 4;
quint32 vertPolicy : 4;
quint32 hfw : 1;
quint32 ctype : 5;
quint32 wfh : 1;
quint32 padding : 1; // we cannot use the highest bit
quint32 verStretch : 8;
quint32 horStretch : 8;
*/
}; };
Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes) Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
#ifndef QT_NO_DATASTREAM #ifndef QT_NO_DATASTREAM
// implemented in qlayout.cpp
Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
#endif #endif
inline void QSizePolicy::transpose() {
Policy hData = horizontalPolicy();
Policy vData = verticalPolicy();
uchar hStretch = uchar(horizontalStretch());
uchar vStretch = uchar(verticalStretch());
setHorizontalPolicy(vData);
setVerticalPolicy(hData);
setHorizontalStretch(vStretch);
setVerticalStretch(hStretch);
}
QT_END_NAMESPACE QT_END_NAMESPACE