1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSIZEPOLICY_H |
5 | #define QSIZEPOLICY_H |
6 | |
7 | #include <QtWidgets/qtwidgetsglobal.h> |
8 | #include <QtCore/qobject.h> |
9 | #include <QtCore/qalgorithms.h> |
10 | #include <QtCore/qhashfunctions.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | class QVariant; |
15 | class QSizePolicy; |
16 | |
17 | class Q_WIDGETS_EXPORT QSizePolicy |
18 | { |
19 | Q_GADGET |
20 | |
21 | public: |
22 | enum PolicyFlag { |
23 | GrowFlag = 1, |
24 | ExpandFlag = 2, |
25 | ShrinkFlag = 4, |
26 | IgnoreFlag = 8 |
27 | }; |
28 | |
29 | enum Policy { |
30 | Fixed = 0, |
31 | Minimum = GrowFlag, |
32 | Maximum = ShrinkFlag, |
33 | Preferred = GrowFlag | ShrinkFlag, |
34 | MinimumExpanding = GrowFlag | ExpandFlag, |
35 | Expanding = GrowFlag | ShrinkFlag | ExpandFlag, |
36 | Ignored = ShrinkFlag | GrowFlag | IgnoreFlag |
37 | }; |
38 | Q_ENUM(Policy) |
39 | |
40 | enum ControlType { |
41 | DefaultType = 0x00000001, |
42 | ButtonBox = 0x00000002, |
43 | CheckBox = 0x00000004, |
44 | ComboBox = 0x00000008, |
45 | Frame = 0x00000010, |
46 | GroupBox = 0x00000020, |
47 | Label = 0x00000040, |
48 | Line = 0x00000080, |
49 | LineEdit = 0x00000100, |
50 | PushButton = 0x00000200, |
51 | RadioButton = 0x00000400, |
52 | Slider = 0x00000800, |
53 | SpinBox = 0x00001000, |
54 | TabWidget = 0x00002000, |
55 | ToolButton = 0x00004000 |
56 | }; |
57 | Q_DECLARE_FLAGS(ControlTypes, ControlType) |
58 | Q_FLAG(ControlTypes) |
59 | |
60 | constexpr QSizePolicy() noexcept : data(0) { } |
61 | |
62 | constexpr QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) noexcept |
63 | : bits{.horStretch: 0, .verStretch: 0, .horPolicy: quint32(horizontal), .verPolicy: quint32(vertical), |
64 | .ctype: type == DefaultType ? 0 : toControlTypeFieldValue(type), .hfw: 0, .wfh: 0, .retainSizeWhenHidden: 0} |
65 | {} |
66 | constexpr Policy horizontalPolicy() const noexcept { return static_cast<Policy>(bits.horPolicy); } |
67 | constexpr Policy verticalPolicy() const noexcept { return static_cast<Policy>(bits.verPolicy); } |
68 | ControlType controlType() const noexcept; |
69 | |
70 | constexpr void setHorizontalPolicy(Policy d) noexcept { bits.horPolicy = d; } |
71 | constexpr void setVerticalPolicy(Policy d) noexcept { bits.verPolicy = d; } |
72 | void setControlType(ControlType type) noexcept; |
73 | |
74 | // ### Qt 7: consider making Policy a QFlags and removing these casts |
75 | constexpr Qt::Orientations expandingDirections() const noexcept { |
76 | return ( (verticalPolicy() & static_cast<Policy>(ExpandFlag)) ? Qt::Vertical : Qt::Orientations() ) |
77 | | ( (horizontalPolicy() & static_cast<Policy>(ExpandFlag)) ? Qt::Horizontal : Qt::Orientations() ) ; |
78 | } |
79 | |
80 | constexpr void setHeightForWidth(bool b) noexcept { bits.hfw = b; } |
81 | constexpr bool hasHeightForWidth() const noexcept { return bits.hfw; } |
82 | constexpr void setWidthForHeight(bool b) noexcept { bits.wfh = b; } |
83 | constexpr bool hasWidthForHeight() const noexcept { return bits.wfh; } |
84 | |
85 | constexpr bool operator==(const QSizePolicy& s) const noexcept { return data == s.data; } |
86 | constexpr bool operator!=(const QSizePolicy& s) const noexcept { return data != s.data; } |
87 | |
88 | friend Q_DECL_CONST_FUNCTION size_t qHash(QSizePolicy key, size_t seed = 0) noexcept { return qHash(key: key.data, seed); } |
89 | |
90 | operator QVariant() const; |
91 | |
92 | constexpr int horizontalStretch() const noexcept { return static_cast<int>(bits.horStretch); } |
93 | constexpr int verticalStretch() const noexcept { return static_cast<int>(bits.verStretch); } |
94 | constexpr void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(min: 0, val: stretchFactor, max: 255)); } |
95 | constexpr void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(min: 0, val: stretchFactor, max: 255)); } |
96 | |
97 | constexpr bool retainSizeWhenHidden() const noexcept { return bits.retainSizeWhenHidden; } |
98 | constexpr void setRetainSizeWhenHidden(bool retainSize) noexcept { bits.retainSizeWhenHidden = retainSize; } |
99 | |
100 | constexpr void transpose() noexcept { *this = transposed(); } |
101 | [[nodiscard]] constexpr QSizePolicy transposed() const noexcept |
102 | { |
103 | return QSizePolicy(bits.transposed()); |
104 | } |
105 | |
106 | private: |
107 | #ifndef QT_NO_DATASTREAM |
108 | friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); |
109 | friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); |
110 | #endif |
111 | constexpr QSizePolicy(int i) noexcept : data(i) { } |
112 | struct Bits; |
113 | constexpr explicit QSizePolicy(Bits b) noexcept : bits(b) { } |
114 | |
115 | static constexpr quint32 toControlTypeFieldValue(ControlType type) noexcept |
116 | { |
117 | /* |
118 | The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10, |
119 | etc. In memory, we pack it onto the available bits (CTSize) in |
120 | setControlType(), and unpack it here. |
121 | |
122 | Example: |
123 | |
124 | 0x00000001 maps to 0 |
125 | 0x00000002 maps to 1 |
126 | 0x00000004 maps to 2 |
127 | 0x00000008 maps to 3 |
128 | etc. |
129 | */ |
130 | |
131 | return qCountTrailingZeroBits(v: static_cast<quint32>(type)); |
132 | } |
133 | |
134 | struct Bits { |
135 | quint32 horStretch : 8; |
136 | quint32 verStretch : 8; |
137 | quint32 horPolicy : 4; |
138 | quint32 verPolicy : 4; |
139 | quint32 ctype : 5; |
140 | quint32 hfw : 1; |
141 | quint32 wfh : 1; |
142 | quint32 retainSizeWhenHidden : 1; |
143 | |
144 | constexpr Bits transposed() const noexcept |
145 | { |
146 | return {.horStretch: verStretch, // \ swap |
147 | .verStretch: horStretch, // / |
148 | .horPolicy: verPolicy, // \ swap |
149 | .verPolicy: horPolicy, // / |
150 | .ctype: ctype, |
151 | .hfw: hfw, // \ don't swap (historic behavior) |
152 | .wfh: wfh, // / |
153 | .retainSizeWhenHidden: retainSizeWhenHidden}; |
154 | } |
155 | }; |
156 | union { |
157 | Bits bits; |
158 | quint32 data; |
159 | }; |
160 | }; |
161 | |
162 | Q_DECLARE_TYPEINFO(QSizePolicy, Q_PRIMITIVE_TYPE); |
163 | |
164 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes) |
165 | Q_DECLARE_MIXED_ENUM_OPERATORS(int, QSizePolicy::Policy, QSizePolicy::PolicyFlag) |
166 | |
167 | #ifndef QT_NO_DATASTREAM |
168 | Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); |
169 | Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); |
170 | #endif |
171 | |
172 | #ifndef QT_NO_DEBUG_STREAM |
173 | Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &); |
174 | #endif |
175 | |
176 | QT_END_NAMESPACE |
177 | |
178 | #endif // QSIZEPOLICY_H |
179 | |