1 /*
2  * DQt - D bindings for the Qt Toolkit
3  *
4  * GNU Lesser General Public License Usage
5  * This file may be used under the terms of the GNU Lesser
6  * General Public License version 3 as published by the Free Software
7  * Foundation and appearing in the file LICENSE.LGPL3 included in the
8  * packaging of this file. Please review the following information to
9  * ensure the GNU Lesser General Public License version 3 requirements
10  * will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
11  */
12 module qt.widgets.sizepolicy;
13 extern(C++):
14 
15 import qt.config;
16 import qt.core.flags;
17 import qt.core.global;
18 import qt.core.metamacros;
19 import qt.core.namespace;
20 import qt.core.typeinfo;
21 import qt.core.variant;
22 import qt.helpers;
23 
24 
25 /+ Q_DECL_CONST_FUNCTION inline size_t qHash(QSizePolicy key, size_t seed = 0) noexcept; +/
26 
27 /// Binding for C++ class [QSizePolicy](https://doc.qt.io/qt-6/qsizepolicy.html).
28 @Q_PRIMITIVE_TYPE extern(C++, class) struct /+ Q_WIDGETS_EXPORT +/ QSizePolicy
29 {
30     mixin(Q_GADGET);
31 
32 public:
33     enum PolicyFlag {
34         GrowFlag = 1,
35         ExpandFlag = 2,
36         ShrinkFlag = 4,
37         IgnoreFlag = 8
38     }
39 
40     enum Policy {
41         Fixed = 0,
42         Minimum = PolicyFlag.GrowFlag,
43         Maximum = PolicyFlag.ShrinkFlag,
44         Preferred = PolicyFlag.GrowFlag | PolicyFlag.ShrinkFlag,
45         MinimumExpanding = PolicyFlag.GrowFlag | PolicyFlag.ExpandFlag,
46         Expanding = PolicyFlag.GrowFlag | PolicyFlag.ShrinkFlag | PolicyFlag.ExpandFlag,
47         Ignored = PolicyFlag.ShrinkFlag | PolicyFlag.GrowFlag | PolicyFlag.IgnoreFlag
48     }
49     /+ Q_ENUM(Policy) +/
50 
51     enum ControlType {
52         DefaultType      = 0x00000001,
53         ButtonBox        = 0x00000002,
54         CheckBox         = 0x00000004,
55         ComboBox         = 0x00000008,
56         Frame            = 0x00000010,
57         GroupBox         = 0x00000020,
58         Label            = 0x00000040,
59         Line             = 0x00000080,
60         LineEdit         = 0x00000100,
61         PushButton       = 0x00000200,
62         RadioButton      = 0x00000400,
63         Slider           = 0x00000800,
64         SpinBox          = 0x00001000,
65         TabWidget        = 0x00002000,
66         ToolButton       = 0x00004000
67     }
68     /+ Q_DECLARE_FLAGS(ControlTypes, ControlType) +/
69 alias ControlTypes = QFlags!(ControlType);    /+ Q_FLAG(ControlTypes) +/
70 
71     @disable this();
72     /+this()/+ noexcept+/
73     {
74         this.data = 0;
75     }+/
76 
77     this(Policy horizontal, Policy vertical, ControlType type = ControlType.DefaultType)/+ noexcept+/
78     {
79         this.bits.bitfieldData_horStretch = 0;
80         this.bits.horPolicy = horizontal;
81         this.bits.verPolicy = vertical;
82         this.bits.ctype = type == ControlType.DefaultType ? 0 : toControlTypeFieldValue(type);
83     }
84     Policy horizontalPolicy() const/+ noexcept+/ { return static_cast!(Policy)(bits.horPolicy); }
85     Policy verticalPolicy() const/+ noexcept+/ { return static_cast!(Policy)(bits.verPolicy); }
86     ControlType controlType() const/+ noexcept+/;
87 
88     void setHorizontalPolicy(Policy d)/+ noexcept+/ { bits.horPolicy = d; }
89     void setVerticalPolicy(Policy d)/+ noexcept+/ { bits.verPolicy = d; }
90     void setControlType(ControlType type)/+ noexcept+/;
91 
92     // ### Qt 7: consider making Policy a QFlags and removing these casts
93     /+ Qt:: +/qt.core.namespace.Orientations expandingDirections() const/+ noexcept+/ {
94         return ( (verticalPolicy()   & static_cast!(Policy)(PolicyFlag.ExpandFlag)) ? /+ Qt:: +/qt.core.namespace.Orientations.Vertical   : /+ Qt:: +/qt.core.namespace.Orientations() )
95              | ( (horizontalPolicy() & static_cast!(Policy)(PolicyFlag.ExpandFlag)) ? /+ Qt:: +/qt.core.namespace.Orientations.Horizontal : /+ Qt:: +/qt.core.namespace.Orientations() ) ;
96     }
97 
98     void setHeightForWidth(bool b)/+ noexcept+/ { bits.hfw = b;  }
99     bool hasHeightForWidth() const/+ noexcept+/ { return !!bits.hfw; }
100     void setWidthForHeight(bool b)/+ noexcept+/ { bits.wfh = b;  }
101     bool hasWidthForHeight() const/+ noexcept+/ { return !!bits.wfh; }
102 
103     /+bool operator ==(ref const(QSizePolicy) s) const/+ noexcept+/ { return data == s.data; }+/
104     /+bool operator !=(ref const(QSizePolicy) s) const/+ noexcept+/ { return data != s.data; }+/
105 
106     /+ friend Q_DECL_CONST_FUNCTION size_t qHash(QSizePolicy key, size_t seed) noexcept { return qHash(key.data, seed); } +/
107 
108     /+auto opCast(T : QVariant)() const;+/
109 
110     int horizontalStretch() const/+ noexcept+/ { return static_cast!(int)(bits.horStretch); }
111     int verticalStretch() const/+ noexcept+/ { return static_cast!(int)(bits.verStretch); }
112     void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast!(quint32)(qBound(0, stretchFactor, 255)); }
113     void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast!(quint32)(qBound(0, stretchFactor, 255)); }
114 
115 //    bool retainSizeWhenHidden() const/+ noexcept+/ { return bits.retainSizeWhenHidden; }
116 //    void setRetainSizeWhenHidden(bool retainSize)/+ noexcept+/ { bits.retainSizeWhenHidden = retainSize; }
117 
118 //    void transpose()/+ noexcept+/ { this = transposed(); }
119 /+    /+ [[nodiscard]] +/ QSizePolicy transposed() const/+ noexcept+/
120     {
121         return QSizePolicy(bits.transposed());
122     }+/
123 
124 private:
125     version(QT_NO_DATASTREAM){}else
126     {
127         /+ friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); +/
128         /+ friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); +/
129     }
130     this(int i)/+ noexcept+/
131     {
132         this.data = i;
133     }
134     /+
135     struct Bits;
136     +/
137     /+/+ explicit +/this(Bits b)/+ noexcept+/
138     {
139         this.bits = b;
140     }+/
141 
142     static quint32 toControlTypeFieldValue(ControlType type)/+ noexcept+/
143     {
144         import qt.core.algorithms;
145 
146         /*
147           The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10,
148           etc. In memory, we pack it onto the available bits (CTSize) in
149           setControlType(), and unpack it here.
150 
151           Example:
152 
153           0x00000001 maps to 0
154           0x00000002 maps to 1
155           0x00000004 maps to 2
156           0x00000008 maps to 3
157           etc.
158         */
159 
160         return qCountTrailingZeroBits(static_cast!(quint32)(type));
161     }
162 
163     struct Bits {
164         /+ quint32 horStretch : 8; +/
165         uint bitfieldData_horStretch;
166         final quint32 horStretch() const
167         {
168             return (bitfieldData_horStretch >> 0) & 0xff;
169         }
170         final quint32 horStretch(quint32 value)
171         {
172             bitfieldData_horStretch = (bitfieldData_horStretch & ~0xff) | ((value & 0xff) << 0);
173             return value;
174         }
175         /+ quint32 verStretch : 8; +/
176         final quint32 verStretch() const
177         {
178             return (bitfieldData_horStretch >> 8) & 0xff;
179         }
180         final quint32 verStretch(quint32 value)
181         {
182             bitfieldData_horStretch = (bitfieldData_horStretch & ~0xff00) | ((value & 0xff) << 8);
183             return value;
184         }
185         /+ quint32 horPolicy : 4; +/
186         final quint32 horPolicy() const
187         {
188             return (bitfieldData_horStretch >> 16) & 0xf;
189         }
190         final quint32 horPolicy(quint32 value)
191         {
192             bitfieldData_horStretch = (bitfieldData_horStretch & ~0xf0000) | ((value & 0xf) << 16);
193             return value;
194         }
195         /+ quint32 verPolicy : 4; +/
196         final quint32 verPolicy() const
197         {
198             return (bitfieldData_horStretch >> 20) & 0xf;
199         }
200         final quint32 verPolicy(quint32 value)
201         {
202             bitfieldData_horStretch = (bitfieldData_horStretch & ~0xf00000) | ((value & 0xf) << 20);
203             return value;
204         }
205         /+ quint32 ctype : 5; +/
206         final quint32 ctype() const
207         {
208             return (bitfieldData_horStretch >> 24) & 0x1f;
209         }
210         final quint32 ctype(quint32 value)
211         {
212             bitfieldData_horStretch = (bitfieldData_horStretch & ~0x1f000000) | ((value & 0x1f) << 24);
213             return value;
214         }
215         /+ quint32 hfw : 1; +/
216         final quint32 hfw() const
217         {
218             return (bitfieldData_horStretch >> 29) & 0x1;
219         }
220         final quint32 hfw(quint32 value)
221         {
222             bitfieldData_horStretch = (bitfieldData_horStretch & ~0x20000000) | ((value & 0x1) << 29);
223             return value;
224         }
225         /+ quint32 wfh : 1; +/
226         final quint32 wfh() const
227         {
228             return (bitfieldData_horStretch >> 30) & 0x1;
229         }
230         final quint32 wfh(quint32 value)
231         {
232             bitfieldData_horStretch = (bitfieldData_horStretch & ~0x40000000) | ((value & 0x1) << 30);
233             return value;
234         }
235         /+ quint32 retainSizeWhenHidden : 1; +/
236         final quint32 retainSizeWhenHidden() const
237         {
238             return (bitfieldData_horStretch >> 31) & 0x1;
239         }
240         final quint32 retainSizeWhenHidden(quint32 value)
241         {
242             bitfieldData_horStretch = (bitfieldData_horStretch & ~0x80000000) | ((value & 0x1) << 31);
243             return value;
244         }
245 
246 /+        Bits transposed() const/+ noexcept+/
247         {
248             return Bits(verStretch, // \ swap
249                     horStretch, // /
250                     verPolicy, // \ swap
251                     horPolicy, // /
252                     ctype,
253                     hfw, // \ don't swap (historic behavior)
254                     wfh, // /
255                     retainSizeWhenHidden);
256         }+/
257     }
258     union {
259         Bits bits;
260         quint32 data;
261     }
262     mixin(CREATE_CONVENIENCE_WRAPPERS);
263 }
264 /+pragma(inline, true) QFlags!(QSizePolicy.ControlTypes.enum_type) operator |(QSizePolicy.ControlTypes.enum_type f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/{return QFlags!(QSizePolicy.ControlTypes.enum_type)(f1)|f2;}+/
265 /+pragma(inline, true) QFlags!(QSizePolicy.ControlTypes.enum_type) operator |(QSizePolicy.ControlTypes.enum_type f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/{return f2|f1;}+/
266 /+pragma(inline, true) QFlags!(QSizePolicy.ControlTypes.enum_type) operator &(QSizePolicy.ControlTypes.enum_type f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/{return QFlags!(QSizePolicy.ControlTypes.enum_type)(f1)&f2;}+/
267 /+pragma(inline, true) QFlags!(QSizePolicy.ControlTypes.enum_type) operator &(QSizePolicy.ControlTypes.enum_type f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/{return f2&f1;}+/
268 /+pragma(inline, true) void operator +(QSizePolicy.ControlTypes.enum_type f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/;+/
269 /+pragma(inline, true) void operator +(QSizePolicy.ControlTypes.enum_type f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/;+/
270 /+pragma(inline, true) void operator +(int f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/;+/
271 /+pragma(inline, true) void operator -(QSizePolicy.ControlTypes.enum_type f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/;+/
272 /+pragma(inline, true) void operator -(QSizePolicy.ControlTypes.enum_type f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/;+/
273 /+pragma(inline, true) void operator -(int f1, QFlags!(QSizePolicy.ControlTypes.enum_type) f2)/+noexcept+/;+/
274 /+pragma(inline, true) QIncompatibleFlag operator |(QSizePolicy.ControlTypes.enum_type f1, int f2)/+noexcept+/{return QIncompatibleFlag(int(f1)|f2);}+/
275 /+pragma(inline, true) void operator +(int f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/;+/
276 /+pragma(inline, true) void operator +(QSizePolicy.ControlTypes.enum_type f1, int f2)/+noexcept+/;+/
277 /+pragma(inline, true) void operator -(int f1, QSizePolicy.ControlTypes.enum_type f2)/+noexcept+/;+/
278 /+pragma(inline, true) void operator -(QSizePolicy.ControlTypes.enum_type f1, int f2)/+noexcept+/;+/
279 
280 /+ Q_DECLARE_TYPEINFO(QSizePolicy, Q_PRIMITIVE_TYPE);
281 
282 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
283 #ifndef QT_NO_DATASTREAM
284 Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
285 Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
286 #endif
287 
288 #ifndef QT_NO_DEBUG_STREAM
289 Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &);
290 #endif +/
291