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 QLAYOUTPOLICY_H
5#define QLAYOUTPOLICY_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19#include <QtCore/qobject.h>
20#include <QtCore/qnamespace.h>
21
22#ifndef QT_NO_DATASTREAM
23# include <QtCore/qdatastream.h>
24#endif
25
26QT_BEGIN_NAMESPACE
27
28
29class QVariant;
30
31class QLayoutPolicy
32{
33 Q_GADGET_EXPORT(Q_GUI_EXPORT)
34
35public:
36 enum PolicyFlag {
37 GrowFlag = 1,
38 ExpandFlag = 2,
39 ShrinkFlag = 4,
40 IgnoreFlag = 8
41 };
42 Q_DECLARE_FLAGS(Policy, PolicyFlag)
43 Q_FLAG(Policy)
44
45 static constexpr inline Policy Fixed = {};
46 static constexpr inline Policy Minimum = GrowFlag;
47 static constexpr inline Policy Maximum = ShrinkFlag;
48 static constexpr inline Policy Preferred = Minimum | Maximum;
49 static constexpr inline Policy MinimumExpanding = Minimum | ExpandFlag;
50 static constexpr inline Policy Expanding = Preferred | ExpandFlag;
51 static constexpr inline Policy Ignored = Preferred | IgnoreFlag;
52
53 enum ControlType {
54 DefaultType = 0x00000001,
55 ButtonBox = 0x00000002,
56 CheckBox = 0x00000004,
57 ComboBox = 0x00000008,
58 Frame = 0x00000010,
59 GroupBox = 0x00000020,
60 Label = 0x00000040,
61 Line = 0x00000080,
62 LineEdit = 0x00000100,
63 PushButton = 0x00000200,
64 RadioButton = 0x00000400,
65 Slider = 0x00000800,
66 SpinBox = 0x00001000,
67 TabWidget = 0x00002000,
68 ToolButton = 0x00004000
69 };
70 Q_DECLARE_FLAGS(ControlTypes, ControlType)
71
72 QLayoutPolicy() : data(0) { }
73
74 QLayoutPolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
75 : data(0) {
76 bits.horPolicy = horizontal;
77 bits.verPolicy = vertical;
78 setControlType(type);
79 }
80 Policy horizontalPolicy() const { return static_cast<Policy>(bits.horPolicy); }
81 Policy verticalPolicy() const { return static_cast<Policy>(bits.verPolicy); }
82 Q_GUI_EXPORT ControlType controlType() const;
83
84 void setHorizontalPolicy(Policy d) { bits.horPolicy = d; }
85 void setVerticalPolicy(Policy d) { bits.verPolicy = d; }
86 Q_GUI_EXPORT void setControlType(ControlType type);
87
88 Qt::Orientations expandingDirections() const {
89 Qt::Orientations result;
90 if (verticalPolicy() & ExpandFlag)
91 result |= Qt::Vertical;
92 if (horizontalPolicy() & ExpandFlag)
93 result |= Qt::Horizontal;
94 return result;
95 }
96
97 void setHeightForWidth(bool b) { bits.hfw = b; }
98 bool hasHeightForWidth() const { return bits.hfw; }
99 void setWidthForHeight(bool b) { bits.wfh = b; }
100 bool hasWidthForHeight() const { return bits.wfh; }
101
102 bool operator==(const QLayoutPolicy& s) const { return data == s.data; }
103 bool operator!=(const QLayoutPolicy& s) const { return data != s.data; }
104
105 int horizontalStretch() const { return static_cast<int>(bits.horStretch); }
106 int verticalStretch() const { return static_cast<int>(bits.verStretch); }
107 void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(min: 0, val: stretchFactor, max: 255)); }
108 void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(min: 0, val: stretchFactor, max: 255)); }
109
110 inline void transpose();
111
112
113private:
114#ifndef QT_NO_DATASTREAM
115 friend QDataStream &operator<<(QDataStream &, const QLayoutPolicy &);
116 friend QDataStream &operator>>(QDataStream &, QLayoutPolicy &);
117#endif
118 QLayoutPolicy(int i) : data(i) { }
119
120 union {
121 struct {
122 quint32 horStretch : 8;
123 quint32 verStretch : 8;
124 quint32 horPolicy : 4;
125 quint32 verPolicy : 4;
126 quint32 ctype : 5;
127 quint32 hfw : 1;
128 quint32 wfh : 1;
129 quint32 padding : 1; // feel free to use
130 } bits;
131 quint32 data;
132 };
133};
134
135Q_DECLARE_OPERATORS_FOR_FLAGS(QLayoutPolicy::Policy)
136Q_DECLARE_OPERATORS_FOR_FLAGS(QLayoutPolicy::ControlTypes)
137
138#ifndef QT_NO_DATASTREAM
139QDataStream &operator<<(QDataStream &, const QLayoutPolicy &);
140QDataStream &operator>>(QDataStream &, QLayoutPolicy &);
141#endif
142
143#ifndef QT_NO_DEBUG_STREAM
144QDebug operator<<(QDebug dbg, const QLayoutPolicy &);
145#endif
146
147inline void QLayoutPolicy::transpose() {
148 Policy hData = horizontalPolicy();
149 Policy vData = verticalPolicy();
150 int hStretch = horizontalStretch();
151 int vStretch = verticalStretch();
152 setHorizontalPolicy(vData);
153 setVerticalPolicy(hData);
154 setHorizontalStretch(vStretch);
155 setVerticalStretch(hStretch);
156}
157
158QT_END_NAMESPACE
159
160#endif // QLAYOUTPOLICY_H
161

source code of qtbase/src/gui/util/qlayoutpolicy_p.h