Fix Qt 5 todo issues for QSizePolicy.
[profile/ivi/qtbase.git] / src / widgets / kernel / qsizepolicy.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QSIZEPOLICY_H
43 #define QSIZEPOLICY_H
44
45 #include <QtCore/qobject.h>
46
47 QT_BEGIN_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51
52 class QVariant;
53
54 class Q_WIDGETS_EXPORT QSizePolicy
55 {
56     Q_GADGET
57     Q_ENUMS(Policy)
58
59 public:
60     enum PolicyFlag {
61         GrowFlag = 1,
62         ExpandFlag = 2,
63         ShrinkFlag = 4,
64         IgnoreFlag = 8
65     };
66
67     enum Policy {
68         Fixed = 0,
69         Minimum = GrowFlag,
70         Maximum = ShrinkFlag,
71         Preferred = GrowFlag | ShrinkFlag,
72         MinimumExpanding = GrowFlag | ExpandFlag,
73         Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
74         Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
75     };
76
77     enum ControlType {
78         DefaultType      = 0x00000001,
79         ButtonBox        = 0x00000002,
80         CheckBox         = 0x00000004,
81         ComboBox         = 0x00000008,
82         Frame            = 0x00000010,
83         GroupBox         = 0x00000020,
84         Label            = 0x00000040,
85         Line             = 0x00000080,
86         LineEdit         = 0x00000100,
87         PushButton       = 0x00000200,
88         RadioButton      = 0x00000400,
89         Slider           = 0x00000800,
90         SpinBox          = 0x00001000,
91         TabWidget        = 0x00002000,
92         ToolButton       = 0x00004000
93     };
94     Q_DECLARE_FLAGS(ControlTypes, ControlType)
95
96     QSizePolicy() : data(0) { }
97
98     // ### Qt 5: merge these two constructors (with type == DefaultType)
99     QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
100         : data(0) {
101         bits.horPolicy = horizontal;
102         bits.verPolicy = vertical;
103         setControlType(type);
104     }
105     Policy horizontalPolicy() const { return static_cast<Policy>(bits.horPolicy); }
106     Policy verticalPolicy() const { return static_cast<Policy>(bits.verPolicy); }
107     ControlType controlType() const;
108
109     void setHorizontalPolicy(Policy d) { bits.horPolicy = d; }
110     void setVerticalPolicy(Policy d) { bits.verPolicy = d; }
111     void setControlType(ControlType type);
112
113     Qt::Orientations expandingDirections() const {
114         Qt::Orientations result;
115         if (verticalPolicy() & ExpandFlag)
116             result |= Qt::Vertical;
117         if (horizontalPolicy() & ExpandFlag)
118             result |= Qt::Horizontal;
119         return result;
120     }
121
122     void setHeightForWidth(bool b) { bits.hfw = b;  }
123     bool hasHeightForWidth() const { return bits.hfw; }
124     void setWidthForHeight(bool b) { bits.wfh = b;  }
125     bool hasWidthForHeight() const { return bits.wfh; }
126
127     bool operator==(const QSizePolicy& s) const { return data == s.data; }
128     bool operator!=(const QSizePolicy& s) const { return data != s.data; }
129     operator QVariant() const; // implemented in qlayoutitem.cpp
130
131     int horizontalStretch() const { return static_cast<int>(bits.horStretch); }
132     int verticalStretch() const { return static_cast<int>(bits.verStretch); }
133     void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
134     void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
135
136     void transpose();
137
138
139 private:
140 #ifndef QT_NO_DATASTREAM
141     friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
142     friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
143 #endif
144     QSizePolicy(int i) : data(i) { }
145
146     union {
147         struct {
148             quint32 horStretch : 8;
149             quint32 verStretch : 8;
150             quint32 horPolicy : 4;
151             quint32 verPolicy : 4;
152             quint32 ctype : 5;
153             quint32 hfw : 1;
154             quint32 wfh : 1;
155             quint32 padding : 1;   // feel free to use
156         } bits;
157         quint32 data;
158     };
159 };
160
161 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
162
163 #ifndef QT_NO_DATASTREAM
164 // implemented in qlayout.cpp
165 Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
166 Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
167 #endif
168
169 #ifndef QT_NO_DEBUG_STREAM
170 Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &);
171 #endif
172
173 inline void QSizePolicy::transpose() {
174     Policy hData = horizontalPolicy();
175     Policy vData = verticalPolicy();
176     int hStretch = horizontalStretch();
177     int vStretch = verticalStretch();
178     setHorizontalPolicy(vData);
179     setVerticalPolicy(hData);
180     setHorizontalStretch(vStretch);
181     setVerticalStretch(hStretch);
182 }
183
184 QT_END_NAMESPACE
185
186 QT_END_HEADER
187
188 #endif // QSIZEPOLICY_H