From 6cf349e278771e079fcd847eed065f6553a5709b Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 14 Mar 2024 08:23:43 +0200 Subject: [PATCH] rework QSizePolicy performance after the changes unknown but the class is subject to change Signed-off-by: Ivailo Monev --- src/gui/CMakeLists.txt | 1 + src/gui/kernel/qlayout.cpp | 59 --------- src/gui/kernel/qlayoutitem.cpp | 8 -- .../{qsizepolicy.qdoc => qsizepolicy.cpp} | 118 +++++++++++++++--- src/gui/kernel/qsizepolicy.h | 109 +++++++--------- src/gui/painting/qbrush.cpp | 2 +- 6 files changed, 148 insertions(+), 149 deletions(-) rename src/gui/kernel/{qsizepolicy.qdoc => qsizepolicy.cpp} (80%) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index ff0132a7a..5aacf109d 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -691,6 +691,7 @@ set(GUI_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qx11info_x11.cpp ${CMAKE_CURRENT_SOURCE_DIR}/kernel/qkeymapper_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}/math3d/qgenericmatrix.cpp ${CMAKE_CURRENT_SOURCE_DIR}/math3d/qmatrix4x4.cpp diff --git a/src/gui/kernel/qlayout.cpp b/src/gui/kernel/qlayout.cpp index c2f339712..0596c0549 100644 --- a/src/gui/kernel/qlayout.cpp +++ b/src/gui/kernel/qlayout.cpp @@ -1383,65 +1383,6 @@ QSize QLayout::closestAcceptableSize(const QWidget *widget, const QSize &size) 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 #include "moc_qlayout.h" diff --git a/src/gui/kernel/qlayoutitem.cpp b/src/gui/kernel/qlayoutitem.cpp index 17696f226..b00cbc18f 100644 --- a/src/gui/kernel/qlayoutitem.cpp +++ b/src/gui/kernel/qlayoutitem.cpp @@ -54,14 +54,6 @@ inline static QSize toLayoutItemSize(QWidgetPrivate *priv, const QSize &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 \brief The QLayoutItem class provides an abstract item that a diff --git a/src/gui/kernel/qsizepolicy.qdoc b/src/gui/kernel/qsizepolicy.cpp similarity index 80% rename from src/gui/kernel/qsizepolicy.qdoc rename to src/gui/kernel/qsizepolicy.cpp index 3139dd904..ebbc24b57 100644 --- a/src/gui/kernel/qsizepolicy.qdoc +++ b/src/gui/kernel/qsizepolicy.cpp @@ -3,20 +3,27 @@ ** Copyright (C) 2015 The Qt Company Ltd. ** 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$ ** ****************************************************************************/ +#include "qsizepolicy.h" +#include "qvariant.h" + +QT_BEGIN_NAMESPACE + /*! \class QSizePolicy \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 false. \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 @@ -326,12 +343,6 @@ \sa verticalStretch(), setHorizontalStretch(), setVerticalPolicy() */ -/*! - \fn void QSizePolicy::transpose() - - Swaps the horizontal and vertical policies and stretches. -*/ - /*! \enum QSizePolicy::ControlType \since 4.3 @@ -357,3 +368,76 @@ \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(policy.horzPolicy); + stream << static_cast(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(horzPolicy); + policy.vertPolicy = static_cast(vertPolicy); + stream >> policy.hfw; + stream >> policy.wfh; + stream >> policy.verStretch; + stream >> policy.horStretch; + stream >> ctype; + policy.ctype = static_cast(ctype); + return stream; +} +#endif // QT_NO_DATASTREAM + +QT_END_NAMESPACE diff --git a/src/gui/kernel/qsizepolicy.h b/src/gui/kernel/qsizepolicy.h index 6f472b953..1c51aecef 100644 --- a/src/gui/kernel/qsizepolicy.h +++ b/src/gui/kernel/qsizepolicy.h @@ -35,19 +35,6 @@ class Q_GUI_EXPORT QSizePolicy Q_GADGET 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: enum PolicyFlag { GrowFlag = 1, @@ -85,21 +72,38 @@ public: }; 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) - : data(horizontal | (vertical << HSize)) { setControlType(type); } + QSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical, QSizePolicy::ControlType type = QSizePolicy::DefaultType) + : horzPolicy(horizontal), + vertPolicy(vertical), + hfw(false), + wfh(false), + verStretch(0), + horStretch(0), + ctype(type) + { + } - Policy horizontalPolicy() const { return static_cast(data & HMask); } - Policy verticalPolicy() const { return static_cast((data & VMask) >> HSize); } - ControlType controlType() const; + inline QSizePolicy::Policy horizontalPolicy() const { return horzPolicy; } + inline QSizePolicy::Policy verticalPolicy() const { return vertPolicy; } + inline QSizePolicy::ControlType controlType() const { return ctype; } - void setHorizontalPolicy(Policy d) { data = (data & ~HMask) | d; } - void setVerticalPolicy(Policy d) { data = (data & ~(HMask << HSize)) | (d << HSize); } - void setControlType(ControlType type); + inline void setHorizontalPolicy(QSizePolicy::Policy d) { horzPolicy = d; } + inline void setVerticalPolicy(QSizePolicy::Policy d) { vertPolicy = d; } + inline void setControlType(ControlType type) { ctype = type; } Qt::Orientations expandingDirections() const { - Qt::Orientations result; + Qt::Orientations result = 0; if (verticalPolicy() & ExpandFlag) result |= Qt::Vertical; if (horizontalPolicy() & ExpandFlag) @@ -107,66 +111,43 @@ public: return result; } - void setHeightForWidth(bool b) { data = b ? (data | (1 << 2*HSize)) : (data & ~(1 << 2*HSize)); } - bool hasHeightForWidth() const { return data & (1 << 2*HSize); } - void setWidthForHeight(bool b) { data = b ? (data | (1 << (WFHShift))) : (data & ~(1 << (WFHShift))); } - bool hasWidthForHeight() const { return data & (1 << (WFHShift)); } + inline void setHeightForWidth(bool b) { hfw = b; } + inline bool hasHeightForWidth() const { return hfw; } + inline void setWidthForHeight(bool b) { wfh = b; } + inline bool hasWidthForHeight() const { return wfh; } - bool operator==(const QSizePolicy& s) const { return data == s.data; } - bool operator!=(const QSizePolicy& s) const { return data != s.data; } - operator QVariant() const; // implemented in qabstractlayout.cpp + bool operator==(const QSizePolicy &s) const; + inline bool operator!=(const QSizePolicy &s) const { return !(operator==(s)); } + operator QVariant() const; - int horizontalStretch() const { return data >> 24; } - int verticalStretch() const { return (data >> 16) & 0xff; } - void setHorizontalStretch(uchar stretchFactor) { data = (data&0x00ffffff) | (uint(stretchFactor)<<24); } - void setVerticalStretch(uchar stretchFactor) { data = (data&0xff00ffff) | (uint(stretchFactor)<<16); } + inline int horizontalStretch() const { return horStretch; } + inline int verticalStretch() const { return verStretch; } + inline void setHorizontalStretch(quint8 stretchFactor) { horStretch = stretchFactor; } + inline void setVerticalStretch(quint8 stretchFactor) { verStretch = stretchFactor; } void transpose(); - private: #ifndef QT_NO_DATASTREAM friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); #endif - QSizePolicy(int i) : data(i) { } - - quint32 data; -/* Qt5: Use bit flags instead, keep it here for improved readability for now. - We can maybe change it for Qt4, but we'd have to be careful, since the behaviour - is implementation defined. It usually varies between little- and big-endian compilers, but - it might also not vary. - 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; -*/ - + QSizePolicy::Policy horzPolicy; + QSizePolicy::Policy vertPolicy; + bool hfw; + bool wfh; + quint8 verStretch; + quint8 horStretch; + QSizePolicy::ControlType ctype; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes) #ifndef QT_NO_DATASTREAM -// implemented in qlayout.cpp Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); #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 diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index deab1031f..d6839b9d2 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -509,7 +509,7 @@ QBrush &QBrush::operator=(const QBrush &other) */ /*! - Returns the brush as a QVariant + Returns the brush as a QVariant */ QBrush::operator QVariant() const {