1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #ifndef QLAYOUTPOLICY_H
41 #define QLAYOUTPOLICY_H
42
43 //
44 // W A R N I N G
45 // -------------
46 //
47 // This file is not part of the Qt API. It exists purely as an
48 // implementation detail. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53
54 #include <QtGui/private/qtguiglobal_p.h>
55 #include <QtCore/qobject.h>
56 #include <QtCore/qnamespace.h>
57
58 #ifndef QT_NO_DATASTREAM
59 # include <QtCore/qdatastream.h>
60 #endif
61
62 QT_BEGIN_NAMESPACE
63
64
65 class QVariant;
66
67 class Q_GUI_EXPORT QLayoutPolicy
68 {
69 Q_GADGET
70 Q_ENUMS(Policy)
71
72 public:
73 enum PolicyFlag {
74 GrowFlag = 1,
75 ExpandFlag = 2,
76 ShrinkFlag = 4,
77 IgnoreFlag = 8
78 };
79
80 enum Policy {
81 Fixed = 0,
82 Minimum = GrowFlag,
83 Maximum = ShrinkFlag,
84 Preferred = GrowFlag | ShrinkFlag,
85 MinimumExpanding = GrowFlag | ExpandFlag,
86 Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
87 Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
88 };
89
90 enum ControlType {
91 DefaultType = 0x00000001,
92 ButtonBox = 0x00000002,
93 CheckBox = 0x00000004,
94 ComboBox = 0x00000008,
95 Frame = 0x00000010,
96 GroupBox = 0x00000020,
97 Label = 0x00000040,
98 Line = 0x00000080,
99 LineEdit = 0x00000100,
100 PushButton = 0x00000200,
101 RadioButton = 0x00000400,
102 Slider = 0x00000800,
103 SpinBox = 0x00001000,
104 TabWidget = 0x00002000,
105 ToolButton = 0x00004000
106 };
Q_DECLARE_FLAGS(ControlTypes,ControlType)107 Q_DECLARE_FLAGS(ControlTypes, ControlType)
108
109 QLayoutPolicy() : data(0) { }
110
111 QLayoutPolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
112 : data(0) {
113 bits.horPolicy = horizontal;
114 bits.verPolicy = vertical;
115 setControlType(type);
116 }
horizontalPolicy()117 Policy horizontalPolicy() const { return static_cast<Policy>(bits.horPolicy); }
verticalPolicy()118 Policy verticalPolicy() const { return static_cast<Policy>(bits.verPolicy); }
119 ControlType controlType() const;
120
setHorizontalPolicy(Policy d)121 void setHorizontalPolicy(Policy d) { bits.horPolicy = d; }
setVerticalPolicy(Policy d)122 void setVerticalPolicy(Policy d) { bits.verPolicy = d; }
123 void setControlType(ControlType type);
124
expandingDirections()125 Qt::Orientations expandingDirections() const {
126 Qt::Orientations result;
127 if (verticalPolicy() & ExpandFlag)
128 result |= Qt::Vertical;
129 if (horizontalPolicy() & ExpandFlag)
130 result |= Qt::Horizontal;
131 return result;
132 }
133
setHeightForWidth(bool b)134 void setHeightForWidth(bool b) { bits.hfw = b; }
hasHeightForWidth()135 bool hasHeightForWidth() const { return bits.hfw; }
setWidthForHeight(bool b)136 void setWidthForHeight(bool b) { bits.wfh = b; }
hasWidthForHeight()137 bool hasWidthForHeight() const { return bits.wfh; }
138
139 bool operator==(const QLayoutPolicy& s) const { return data == s.data; }
140 bool operator!=(const QLayoutPolicy& s) const { return data != s.data; }
141
horizontalStretch()142 int horizontalStretch() const { return static_cast<int>(bits.horStretch); }
verticalStretch()143 int verticalStretch() const { return static_cast<int>(bits.verStretch); }
setHorizontalStretch(int stretchFactor)144 void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
setVerticalStretch(int stretchFactor)145 void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
146
147 void transpose();
148
149
150 private:
151 #ifndef QT_NO_DATASTREAM
152 friend QDataStream &operator<<(QDataStream &, const QLayoutPolicy &);
153 friend QDataStream &operator>>(QDataStream &, QLayoutPolicy &);
154 #endif
QLayoutPolicy(int i)155 QLayoutPolicy(int i) : data(i) { }
156
157 union {
158 struct {
159 quint32 horStretch : 8;
160 quint32 verStretch : 8;
161 quint32 horPolicy : 4;
162 quint32 verPolicy : 4;
163 quint32 ctype : 5;
164 quint32 hfw : 1;
165 quint32 wfh : 1;
166 quint32 padding : 1; // feel free to use
167 } bits;
168 quint32 data;
169 };
170 };
171
172 Q_DECLARE_OPERATORS_FOR_FLAGS(QLayoutPolicy::ControlTypes)
173
174 #ifndef QT_NO_DATASTREAM
175 QDataStream &operator<<(QDataStream &, const QLayoutPolicy &);
176 QDataStream &operator>>(QDataStream &, QLayoutPolicy &);
177 #endif
178
179 #ifndef QT_NO_DEBUG_STREAM
180 QDebug operator<<(QDebug dbg, const QLayoutPolicy &);
181 #endif
182
transpose()183 inline void QLayoutPolicy::transpose() {
184 Policy hData = horizontalPolicy();
185 Policy vData = verticalPolicy();
186 int hStretch = horizontalStretch();
187 int vStretch = verticalStretch();
188 setHorizontalPolicy(vData);
189 setVerticalPolicy(hData);
190 setHorizontalStretch(vStretch);
191 setVerticalStretch(hStretch);
192 }
193
194 QT_END_NAMESPACE
195
196 #endif // QLAYOUTPOLICY_H
197