Qt 4.8
qsizepolicy.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSIZEPOLICY_H
43 #define QSIZEPOLICY_H
44 
45 #include <QtCore/qobject.h>
46 
48 
50 
51 QT_MODULE(Gui)
52 
53 class QVariant;
54 
56 {
57  Q_GADGET
59 
60 private:
62  HSize = 4,
63  HMask = 0x0f,
64  VMask = HMask << HSize,
65  CTShift = 9,
66  CTSize = 5,
67  CTMask = ((0x1 << CTSize) - 1) << CTShift,
68  WFHShift = CTShift + CTSize,
69  UnusedShift = WFHShift + 1,
70  UnusedSize = 1
71  };
72 
73 public:
74  enum PolicyFlag {
75  GrowFlag = 1,
76  ExpandFlag = 2,
77  ShrinkFlag = 4,
78  IgnoreFlag = 8
79  };
80 
81  enum Policy {
82  Fixed = 0,
83  Minimum = GrowFlag,
84  Maximum = ShrinkFlag,
85  Preferred = GrowFlag | ShrinkFlag,
86  MinimumExpanding = GrowFlag | ExpandFlag,
87  Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
88  Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
89  };
90 
91  enum ControlType {
92  DefaultType = 0x00000001,
93  ButtonBox = 0x00000002,
94  CheckBox = 0x00000004,
95  ComboBox = 0x00000008,
96  Frame = 0x00000010,
97  GroupBox = 0x00000020,
98  Label = 0x00000040,
99  Line = 0x00000080,
100  LineEdit = 0x00000100,
101  PushButton = 0x00000200,
102  RadioButton = 0x00000400,
103  Slider = 0x00000800,
104  SpinBox = 0x00001000,
105  TabWidget = 0x00002000,
106  ToolButton = 0x00004000
107  };
108  Q_DECLARE_FLAGS(ControlTypes, ControlType)
109 
110  QSizePolicy() : data(0) { }
111 
112  // ### Qt 5: merge these two constructors (with type == DefaultType)
113  QSizePolicy(Policy horizontal, Policy vertical)
114  : data(horizontal | (vertical << HSize)) { }
115  QSizePolicy(Policy horizontal, Policy vertical, ControlType type)
116  : data(horizontal | (vertical << HSize)) { setControlType(type); }
117 
118  Policy horizontalPolicy() const { return static_cast<Policy>(data & HMask); }
119  Policy verticalPolicy() const { return static_cast<Policy>((data & VMask) >> HSize); }
120  ControlType controlType() const;
121 
122  void setHorizontalPolicy(Policy d) { data = (data & ~HMask) | d; }
123  void setVerticalPolicy(Policy d) { data = (data & ~(HMask << HSize)) | (d << HSize); }
124  void setControlType(ControlType type);
125 
126  Qt::Orientations expandingDirections() const {
127  Qt::Orientations result;
128  if (verticalPolicy() & ExpandFlag)
129  result |= Qt::Vertical;
130  if (horizontalPolicy() & ExpandFlag)
131  result |= Qt::Horizontal;
132  return result;
133  }
134 
135  void setHeightForWidth(bool b) { data = b ? (data | (1 << 2*HSize)) : (data & ~(1 << 2*HSize)); }
136  bool hasHeightForWidth() const { return data & (1 << 2*HSize); }
137  void setWidthForHeight(bool b) { data = b ? (data | (1 << (WFHShift))) : (data & ~(1 << (WFHShift))); }
138  bool hasWidthForHeight() const { return data & (1 << (WFHShift)); }
139 
140  bool operator==(const QSizePolicy& s) const { return data == s.data; }
141  bool operator!=(const QSizePolicy& s) const { return data != s.data; }
142  operator QVariant() const; // implemented in qabstractlayout.cpp
143 
144  int horizontalStretch() const { return data >> 24; }
145  int verticalStretch() const { return (data >> 16) & 0xff; }
146  void setHorizontalStretch(uchar stretchFactor) { data = (data&0x00ffffff) | (uint(stretchFactor)<<24); }
147  void setVerticalStretch(uchar stretchFactor) { data = (data&0xff00ffff) | (uint(stretchFactor)<<16); }
148 
149  void transpose();
150 
151 #ifdef QT3_SUPPORT
152  typedef Policy SizeType;
153 #ifndef qdoc
154  typedef Qt::Orientations ExpandData;
155  enum {
156  NoDirection = 0,
157  Horizontally = 1,
158  Vertically = 2,
159  BothDirections = Horizontally | Vertically
160  };
161 #else
162  enum ExpandData {
163  NoDirection = 0x0,
164  Horizontally = 0x1,
165  Vertically = 0x2,
166  BothDirections = 0x3
167  };
168 #endif // qdoc
169 
170  inline QT3_SUPPORT bool mayShrinkHorizontally() const
171  { return horizontalPolicy() & ShrinkFlag; }
172  inline QT3_SUPPORT bool mayShrinkVertically() const { return verticalPolicy() & ShrinkFlag; }
173  inline QT3_SUPPORT bool mayGrowHorizontally() const { return horizontalPolicy() & GrowFlag; }
174  inline QT3_SUPPORT bool mayGrowVertically() const { return verticalPolicy() & GrowFlag; }
175  inline QT3_SUPPORT Qt::Orientations expanding() const { return expandingDirections(); }
176 
177  QT3_SUPPORT_CONSTRUCTOR QSizePolicy(Policy hor, Policy ver, bool hfw)
178  : data(hor | (ver<<HSize) | (hfw ? (1U<<2*HSize) : 0)) { }
179 
180  QT3_SUPPORT_CONSTRUCTOR QSizePolicy(Policy hor, Policy ver, uchar hors, uchar vers, bool hfw = false)
181  : data(hor | (ver<<HSize) | (hfw ? (1U<<2*HSize) : 0)) {
182  setHorizontalStretch(hors);
183  setVerticalStretch(vers);
184  }
185 
186  inline QT3_SUPPORT Policy horData() const { return static_cast<Policy>(data & HMask); }
187  inline QT3_SUPPORT Policy verData() const { return static_cast<Policy>((data & VMask) >> HSize); }
188  inline QT3_SUPPORT void setHorData(Policy d) { setHorizontalPolicy(d); }
189  inline QT3_SUPPORT void setVerData(Policy d) { setVerticalPolicy(d); }
190 
191  inline QT3_SUPPORT uint horStretch() const { return horizontalStretch(); }
192  inline QT3_SUPPORT uint verStretch() const { return verticalStretch(); }
193  inline QT3_SUPPORT void setHorStretch(uchar sf) { setHorizontalStretch(sf); }
194  inline QT3_SUPPORT void setVerStretch(uchar sf) { setVerticalStretch(sf); }
195 #endif
196 
197 private:
198 #ifndef QT_NO_DATASTREAM
199  friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
200  friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
201 #endif
202  QSizePolicy(int i) : data(i) { }
203 
205 /* Qt5: Use bit flags instead, keep it here for improved readability for now.
206  We can maybe change it for Qt4, but we'd have to be careful, since the behaviour
207  is implementation defined. It usually varies between little- and big-endian compilers, but
208  it might also not vary.
209  quint32 horzPolicy : 4;
210  quint32 vertPolicy : 4;
211  quint32 hfw : 1;
212  quint32 ctype : 5;
213  quint32 wfh : 1;
214  quint32 padding : 1; // we cannot use the highest bit
215  quint32 verStretch : 8;
216  quint32 horStretch : 8;
217 */
218 
219 };
220 
221 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
222 
223 #ifndef QT_NO_DATASTREAM
224 // implemented in qlayout.cpp
227 #endif
228 
229 inline void QSizePolicy::transpose() {
230  Policy hData = horizontalPolicy();
231  Policy vData = verticalPolicy();
232  uchar hStretch = uchar(horizontalStretch());
233  uchar vStretch = uchar(verticalStretch());
234  setHorizontalPolicy(vData);
235  setVerticalPolicy(hData);
236  setHorizontalStretch(vStretch);
237  setVerticalStretch(hStretch);
238 }
239 
241 
243 
244 #endif // QSIZEPOLICY_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
void setHorizontalStretch(uchar stretchFactor)
Definition: qsizepolicy.h:146
QSizePolicy(int i)
Definition: qsizepolicy.h:202
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
bool operator==(const QSizePolicy &s) const
Definition: qsizepolicy.h:140
QSizePolicy(Policy horizontal, Policy vertical)
Definition: qsizepolicy.h:113
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_DECLARE_FLAGS(Flags, Enum)
The Q_DECLARE_FLAGS() macro expands to.
Definition: qglobal.h:2348
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
bool hasWidthForHeight() const
Definition: qsizepolicy.h:138
Policy horizontalPolicy() const
Definition: qsizepolicy.h:118
QSizePolicy(Policy horizontal, Policy vertical, ControlType type)
Definition: qsizepolicy.h:115
bool operator!=(const QSizePolicy &s) const
Definition: qsizepolicy.h:141
int horizontalStretch() const
Definition: qsizepolicy.h:144
#define Q_ENUMS(x)
Definition: qobjectdefs.h:84
void setHeightForWidth(bool b)
Definition: qsizepolicy.h:135
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
quint32 data
Definition: qsizepolicy.h:204
bool hasHeightForWidth() const
Definition: qsizepolicy.h:136
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags...
Definition: qglobal.h:2355
Qt::Orientations expandingDirections() const
Definition: qsizepolicy.h:126
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
int verticalStretch() const
Definition: qsizepolicy.h:145
#define Q_GADGET
Definition: qobjectdefs.h:173
void setHorizontalPolicy(Policy d)
Definition: qsizepolicy.h:122
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QSizePolicy &)
unsigned int quint32
Definition: qglobal.h:938
Policy verticalPolicy() const
Definition: qsizepolicy.h:119
void transpose()
Definition: qsizepolicy.h:229
void setVerticalPolicy(Policy d)
Definition: qsizepolicy.h:123
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QSizePolicy &)
void setWidthForHeight(bool b)
Definition: qsizepolicy.h:137
void setVerticalStretch(uchar stretchFactor)
Definition: qsizepolicy.h:147
#define QT_END_HEADER
Definition: qglobal.h:137