1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
40 ****************************************************************************/
45 #include <QtCore/qobject.h>
55 class Q_GUI_EXPORT QSizePolicy
61 enum SizePolicyMasks
{
64 VMask
= HMask
<< HSize
,
67 CTMask
= ((0x1 << CTSize
) - 1) << CTShift
,
68 UnusedShift
= CTShift
+ CTSize
,
84 Preferred
= GrowFlag
| ShrinkFlag
,
85 MinimumExpanding
= GrowFlag
| ExpandFlag
,
86 Expanding
= GrowFlag
| ShrinkFlag
| ExpandFlag
,
87 Ignored
= ShrinkFlag
| GrowFlag
| IgnoreFlag
91 DefaultType
= 0x00000001,
92 ButtonBox
= 0x00000002,
93 CheckBox
= 0x00000004,
94 ComboBox
= 0x00000008,
96 GroupBox
= 0x00000020,
99 LineEdit
= 0x00000100,
100 PushButton
= 0x00000200,
101 RadioButton
= 0x00000400,
103 SpinBox
= 0x00001000,
104 TabWidget
= 0x00002000,
105 ToolButton
= 0x00004000
107 Q_DECLARE_FLAGS(ControlTypes
, ControlType
)
109 QSizePolicy() : data(0) { }
111 // ### Qt 5: merge these two constructors (with type == DefaultType)
112 QSizePolicy(Policy horizontal
, Policy vertical
)
113 : data(horizontal
| (vertical
<< HSize
)) { }
114 QSizePolicy(Policy horizontal
, Policy vertical
, ControlType type
)
115 : data(horizontal
| (vertical
<< HSize
)) { setControlType(type
); }
117 Policy
horizontalPolicy() const { return static_cast<Policy
>(data
& HMask
); }
118 Policy
verticalPolicy() const { return static_cast<Policy
>((data
& VMask
) >> HSize
); }
119 ControlType
controlType() const;
121 void setHorizontalPolicy(Policy d
) { data
= (data
& ~HMask
) | d
; }
122 void setVerticalPolicy(Policy d
) { data
= (data
& ~(HMask
<< HSize
)) | (d
<< HSize
); }
123 void setControlType(ControlType type
);
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
;
134 void setHeightForWidth(bool b
) { data
= b
? (data
| (1 << 2*HSize
)) : (data
& ~(1 << 2*HSize
)); }
135 bool hasHeightForWidth() const { return data
& (1 << 2*HSize
); }
137 bool operator==(const QSizePolicy
& s
) const { return data
== s
.data
; }
138 bool operator!=(const QSizePolicy
& s
) const { return data
!= s
.data
; }
139 operator QVariant() const; // implemented in qabstractlayout.cpp
141 int horizontalStretch() const { return data
>> 24; }
142 int verticalStretch() const { return (data
>> 16) & 0xff; }
143 void setHorizontalStretch(uchar stretchFactor
) { data
= (data
&0x00ffffff) | (uint(stretchFactor
)<<24); }
144 void setVerticalStretch(uchar stretchFactor
) { data
= (data
&0xff00ffff) | (uint(stretchFactor
)<<16); }
149 typedef Policy SizeType
;
151 typedef Qt::Orientations ExpandData
;
156 BothDirections
= Horizontally
| Vertically
167 inline QT3_SUPPORT
bool mayShrinkHorizontally() const
168 { return horizontalPolicy() & ShrinkFlag
; }
169 inline QT3_SUPPORT
bool mayShrinkVertically() const { return verticalPolicy() & ShrinkFlag
; }
170 inline QT3_SUPPORT
bool mayGrowHorizontally() const { return horizontalPolicy() & GrowFlag
; }
171 inline QT3_SUPPORT
bool mayGrowVertically() const { return verticalPolicy() & GrowFlag
; }
172 inline QT3_SUPPORT
Qt::Orientations
expanding() const { return expandingDirections(); }
174 QT3_SUPPORT_CONSTRUCTOR
QSizePolicy(Policy hor
, Policy ver
, bool hfw
)
175 : data(hor
| (ver
<<HSize
) | (hfw
? (1U<<2*HSize
) : 0)) { }
177 QT3_SUPPORT_CONSTRUCTOR
QSizePolicy(Policy hor
, Policy ver
, uchar hors
, uchar vers
, bool hfw
= false)
178 : data(hor
| (ver
<<HSize
) | (hfw
? (1U<<2*HSize
) : 0)) {
179 setHorizontalStretch(hors
);
180 setVerticalStretch(vers
);
183 inline QT3_SUPPORT Policy
horData() const { return static_cast<Policy
>(data
& HMask
); }
184 inline QT3_SUPPORT Policy
verData() const { return static_cast<Policy
>((data
& VMask
) >> HSize
); }
185 inline QT3_SUPPORT
void setHorData(Policy d
) { setHorizontalPolicy(d
); }
186 inline QT3_SUPPORT
void setVerData(Policy d
) { setVerticalPolicy(d
); }
188 inline QT3_SUPPORT uint
horStretch() const { return horizontalStretch(); }
189 inline QT3_SUPPORT uint
verStretch() const { return verticalStretch(); }
190 inline QT3_SUPPORT
void setHorStretch(uchar sf
) { setHorizontalStretch(sf
); }
191 inline QT3_SUPPORT
void setVerStretch(uchar sf
) { setVerticalStretch(sf
); }
195 #ifndef QT_NO_DATASTREAM
196 friend Q_GUI_EXPORT QDataStream
&operator<<(QDataStream
&, const QSizePolicy
&);
197 friend Q_GUI_EXPORT QDataStream
&operator>>(QDataStream
&, QSizePolicy
&);
199 QSizePolicy(int i
) : data(i
) { }
204 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes
)
206 #ifndef QT_NO_DATASTREAM
207 // implemented in qlayout.cpp
208 Q_GUI_EXPORT QDataStream
&operator<<(QDataStream
&, const QSizePolicy
&);
209 Q_GUI_EXPORT QDataStream
&operator>>(QDataStream
&, QSizePolicy
&);
212 inline void QSizePolicy::transpose() {
213 Policy hData
= horizontalPolicy();
214 Policy vData
= verticalPolicy();
215 uchar hStretch
= horizontalStretch();
216 uchar vStretch
= verticalStretch();
217 setHorizontalPolicy(vData
);
218 setVerticalPolicy(hData
);
219 setHorizontalStretch(vStretch
);
220 setVerticalStretch(hStretch
);
227 #endif // QSIZEPOLICY_H