Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-25 09:29:39

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QSIZEPOLICY_H
0006 #define QSIZEPOLICY_H
0007 
0008 #include <QtWidgets/qtwidgetsglobal.h>
0009 #include <QtCore/qobject.h>
0010 #include <QtCore/qalgorithms.h>
0011 #include <QtCore/qhashfunctions.h>
0012 
0013 QT_BEGIN_NAMESPACE
0014 
0015 class QVariant;
0016 class QSizePolicy;
0017 
0018 class Q_WIDGETS_EXPORT QSizePolicy
0019 {
0020     Q_GADGET
0021 
0022 public:
0023     enum PolicyFlag {
0024         GrowFlag = 1,
0025         ExpandFlag = 2,
0026         ShrinkFlag = 4,
0027         IgnoreFlag = 8
0028     };
0029 
0030     enum Policy {
0031         Fixed = 0,
0032         Minimum = GrowFlag,
0033         Maximum = ShrinkFlag,
0034         Preferred = GrowFlag | ShrinkFlag,
0035         MinimumExpanding = GrowFlag | ExpandFlag,
0036         Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
0037         Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
0038     };
0039     Q_ENUM(Policy)
0040 
0041     enum ControlType {
0042         DefaultType      = 0x00000001,
0043         ButtonBox        = 0x00000002,
0044         CheckBox         = 0x00000004,
0045         ComboBox         = 0x00000008,
0046         Frame            = 0x00000010,
0047         GroupBox         = 0x00000020,
0048         Label            = 0x00000040,
0049         Line             = 0x00000080,
0050         LineEdit         = 0x00000100,
0051         PushButton       = 0x00000200,
0052         RadioButton      = 0x00000400,
0053         Slider           = 0x00000800,
0054         SpinBox          = 0x00001000,
0055         TabWidget        = 0x00002000,
0056         ToolButton       = 0x00004000
0057     };
0058     Q_DECLARE_FLAGS(ControlTypes, ControlType)
0059     Q_FLAG(ControlTypes)
0060 
0061     constexpr QSizePolicy() noexcept : data(0) { }
0062 
0063     constexpr QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) noexcept
0064         : bits{0, 0, quint32(horizontal), quint32(vertical),
0065                type == DefaultType ? 0 : toControlTypeFieldValue(type), 0, 0, 0}
0066     {}
0067     constexpr Policy horizontalPolicy() const noexcept { return static_cast<Policy>(bits.horPolicy); }
0068     constexpr Policy verticalPolicy() const noexcept { return static_cast<Policy>(bits.verPolicy); }
0069     ControlType controlType() const noexcept;
0070 
0071     constexpr void setHorizontalPolicy(Policy d) noexcept { bits.horPolicy = d; }
0072     constexpr void setVerticalPolicy(Policy d) noexcept { bits.verPolicy = d; }
0073     void setControlType(ControlType type) noexcept;
0074 
0075     // ### Qt 7: consider making Policy a QFlags and removing these casts
0076     constexpr Qt::Orientations expandingDirections() const noexcept {
0077         return ( (verticalPolicy()   & static_cast<Policy>(ExpandFlag)) ? Qt::Vertical   : Qt::Orientations() )
0078              | ( (horizontalPolicy() & static_cast<Policy>(ExpandFlag)) ? Qt::Horizontal : Qt::Orientations() ) ;
0079     }
0080 
0081     constexpr void setHeightForWidth(bool b) noexcept { bits.hfw = b;  }
0082     constexpr bool hasHeightForWidth() const noexcept { return bits.hfw; }
0083     constexpr void setWidthForHeight(bool b) noexcept { bits.wfh = b;  }
0084     constexpr bool hasWidthForHeight() const noexcept { return bits.wfh; }
0085 
0086     constexpr bool operator==(const QSizePolicy& s) const noexcept { return data == s.data; }
0087     constexpr bool operator!=(const QSizePolicy& s) const noexcept { return data != s.data; }
0088 
0089     friend Q_DECL_CONST_FUNCTION size_t qHash(QSizePolicy key, size_t seed = 0) noexcept { return qHash(key.data, seed); }
0090 
0091     operator QVariant() const;
0092 
0093     constexpr int horizontalStretch() const noexcept { return static_cast<int>(bits.horStretch); }
0094     constexpr int verticalStretch() const noexcept { return static_cast<int>(bits.verStretch); }
0095     constexpr void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
0096     constexpr void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
0097 
0098     constexpr bool retainSizeWhenHidden() const noexcept { return bits.retainSizeWhenHidden; }
0099     constexpr void setRetainSizeWhenHidden(bool retainSize) noexcept { bits.retainSizeWhenHidden = retainSize; }
0100 
0101     constexpr void transpose() noexcept { *this = transposed(); }
0102     [[nodiscard]] constexpr QSizePolicy transposed() const noexcept
0103     {
0104         return QSizePolicy(bits.transposed());
0105     }
0106 
0107 private:
0108 #ifndef QT_NO_DATASTREAM
0109     friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
0110     friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
0111 #endif
0112     constexpr QSizePolicy(int i) noexcept : data(i) { }
0113     struct Bits;
0114     constexpr explicit QSizePolicy(Bits b) noexcept : bits(b) { }
0115 
0116     static constexpr quint32 toControlTypeFieldValue(ControlType type) noexcept
0117     {
0118         /*
0119           The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
0120           etc. In memory, we pack it onto the available bits (CTSize) in
0121           setControlType(), and unpack it here.
0122 
0123           Example:
0124 
0125           0x00000001 maps to 0
0126           0x00000002 maps to 1
0127           0x00000004 maps to 2
0128           0x00000008 maps to 3
0129           etc.
0130         */
0131 
0132         return qCountTrailingZeroBits(static_cast<quint32>(type));
0133     }
0134 
0135     struct Bits {
0136         quint32 horStretch : 8;
0137         quint32 verStretch : 8;
0138         quint32 horPolicy : 4;
0139         quint32 verPolicy : 4;
0140         quint32 ctype : 5;
0141         quint32 hfw : 1;
0142         quint32 wfh : 1;
0143         quint32 retainSizeWhenHidden : 1;
0144 
0145         constexpr Bits transposed() const noexcept
0146         {
0147             return {verStretch, // \ swap
0148                     horStretch, // /
0149                     verPolicy, // \ swap
0150                     horPolicy, // /
0151                     ctype,
0152                     hfw, // \ don't swap (historic behavior)
0153                     wfh, // /
0154                     retainSizeWhenHidden};
0155         }
0156     };
0157     union {
0158         Bits bits;
0159         quint32 data;
0160     };
0161 };
0162 
0163 Q_DECLARE_TYPEINFO(QSizePolicy, Q_PRIMITIVE_TYPE);
0164 
0165 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
0166 Q_DECLARE_MIXED_ENUM_OPERATORS(int, QSizePolicy::Policy, QSizePolicy::PolicyFlag)
0167 
0168 #ifndef QT_NO_DATASTREAM
0169 Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
0170 Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
0171 #endif
0172 
0173 #ifndef QT_NO_DEBUG_STREAM
0174 Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &);
0175 #endif
0176 
0177 QT_END_NAMESPACE
0178 
0179 #endif // QSIZEPOLICY_H