qsizepolicy.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QSIZEPOLICY_H
00043 #define QSIZEPOLICY_H
00044 
00045 #include <QtCore/qobject.h>
00046 
00047 QT_BEGIN_HEADER
00048 
00049 QT_BEGIN_NAMESPACE
00050 
00051 QT_MODULE(Gui)
00052 
00053 class QVariant;
00054 
00055 class Q_GUI_EXPORT QSizePolicy
00056 {
00057     Q_GADGET
00058     Q_ENUMS(Policy)
00059 
00060 private:
00061     enum SizePolicyMasks {
00062         HSize = 4,
00063         HMask = 0x0f,
00064         VMask = HMask << HSize,
00065         CTShift = 9,
00066         CTSize = 5,
00067                 WFHShift = CTShift + CTSize,
00068         CTMask = ((0x1 << CTSize) - 1) << CTShift,
00069         UnusedShift = CTShift + CTSize,
00070         UnusedSize = 2
00071     };
00072 
00073 public:
00074     enum PolicyFlag {
00075         GrowFlag = 1,
00076         ExpandFlag = 2,
00077         ShrinkFlag = 4,
00078         IgnoreFlag = 8
00079     };
00080 
00081     enum Policy {
00082         Fixed = 0,
00083         Minimum = GrowFlag,
00084         Maximum = ShrinkFlag,
00085         Preferred = GrowFlag | ShrinkFlag,
00086         MinimumExpanding = GrowFlag | ExpandFlag,
00087         Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
00088         Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
00089     };
00090 
00091     enum ControlType {
00092         DefaultType      = 0x00000001,
00093         ButtonBox        = 0x00000002,
00094         CheckBox         = 0x00000004,
00095         ComboBox         = 0x00000008,
00096         Frame            = 0x00000010,
00097         GroupBox         = 0x00000020,
00098         Label            = 0x00000040,
00099         Line             = 0x00000080,
00100         LineEdit         = 0x00000100,
00101         PushButton       = 0x00000200,
00102         RadioButton      = 0x00000400,
00103         Slider           = 0x00000800,
00104         SpinBox          = 0x00001000,
00105         TabWidget        = 0x00002000,
00106         ToolButton       = 0x00004000
00107     };
00108     Q_DECLARE_FLAGS(ControlTypes, ControlType)
00109 
00110     QSizePolicy() : data(0) { }
00111 
00112     // ### Qt 5: merge these two constructors (with type == DefaultType)
00113     QSizePolicy(Policy horizontal, Policy vertical)
00114         : data(horizontal | (vertical << HSize)) { }
00115     QSizePolicy(Policy horizontal, Policy vertical, ControlType type)
00116         : data(horizontal | (vertical << HSize)) { setControlType(type); }
00117 
00118     Policy horizontalPolicy() const { return static_cast<Policy>(data & HMask); }
00119     Policy verticalPolicy() const { return static_cast<Policy>((data & VMask) >> HSize); }
00120     ControlType controlType() const;
00121 
00122     void setHorizontalPolicy(Policy d) { data = (data & ~HMask) | d; }
00123     void setVerticalPolicy(Policy d) { data = (data & ~(HMask << HSize)) | (d << HSize); }
00124     void setControlType(ControlType type);
00125 
00126     Qt::Orientations expandingDirections() const {
00127         Qt::Orientations result;
00128         if (verticalPolicy() & ExpandFlag)
00129             result |= Qt::Vertical;
00130         if (horizontalPolicy() & ExpandFlag)
00131             result |= Qt::Horizontal;
00132         return result;
00133     }
00134 
00135     void setHeightForWidth(bool b) { data = b ? (data | (1 << 2*HSize)) : (data & ~(1 << 2*HSize));  }
00136     bool hasHeightForWidth() const { return data & (1 << 2*HSize); }
00137 
00138     bool operator==(const QSizePolicy& s) const { return data == s.data; }
00139     bool operator!=(const QSizePolicy& s) const { return data != s.data; }
00140     operator QVariant() const; // implemented in qabstractlayout.cpp
00141 
00142     int horizontalStretch() const { return data >> 24; }
00143     int verticalStretch() const { return (data >> 16) & 0xff; }
00144     void setHorizontalStretch(uchar stretchFactor) { data = (data&0x00ffffff) | (uint(stretchFactor)<<24); }
00145     void setVerticalStretch(uchar stretchFactor) { data = (data&0xff00ffff) | (uint(stretchFactor)<<16); }
00146 
00147     void transpose();
00148 
00149 #ifdef QT3_SUPPORT
00150     typedef Policy SizeType;
00151 #ifndef qdoc
00152     typedef Qt::Orientations ExpandData;
00153     enum {
00154         NoDirection = 0,
00155         Horizontally = 1,
00156         Vertically = 2,
00157         BothDirections = Horizontally | Vertically
00158     };
00159 #else
00160     enum ExpandData {
00161         NoDirection = 0x0,
00162         Horizontally = 0x1,
00163         Vertically = 0x2,
00164         BothDirections = 0x3
00165     };
00166 #endif // qdoc
00167 
00168     inline QT3_SUPPORT bool mayShrinkHorizontally() const
00169         { return horizontalPolicy() & ShrinkFlag; }
00170     inline QT3_SUPPORT bool mayShrinkVertically() const { return verticalPolicy() & ShrinkFlag; }
00171     inline QT3_SUPPORT bool mayGrowHorizontally() const { return horizontalPolicy() & GrowFlag; }
00172     inline QT3_SUPPORT bool mayGrowVertically() const { return verticalPolicy() & GrowFlag; }
00173     inline QT3_SUPPORT Qt::Orientations expanding() const { return expandingDirections(); }
00174 
00175     QT3_SUPPORT_CONSTRUCTOR QSizePolicy(Policy hor, Policy ver, bool hfw)
00176         : data(hor | (ver<<HSize) | (hfw ? (1U<<2*HSize) : 0)) { }
00177 
00178     QT3_SUPPORT_CONSTRUCTOR QSizePolicy(Policy hor, Policy ver, uchar hors, uchar vers, bool hfw = false)
00179         : data(hor | (ver<<HSize) | (hfw ? (1U<<2*HSize) : 0)) {
00180         setHorizontalStretch(hors);
00181         setVerticalStretch(vers);
00182     }
00183 
00184     inline QT3_SUPPORT Policy horData() const { return static_cast<Policy>(data & HMask); }
00185     inline QT3_SUPPORT Policy verData() const { return static_cast<Policy>((data & VMask) >> HSize); }
00186     inline QT3_SUPPORT void setHorData(Policy d) { setHorizontalPolicy(d); }
00187     inline QT3_SUPPORT void setVerData(Policy d) { setVerticalPolicy(d); }
00188 
00189     inline QT3_SUPPORT uint horStretch() const { return horizontalStretch(); }
00190     inline QT3_SUPPORT uint verStretch() const { return verticalStretch(); }
00191     inline QT3_SUPPORT void setHorStretch(uchar sf) { setHorizontalStretch(sf); }
00192     inline QT3_SUPPORT void setVerStretch(uchar sf) { setVerticalStretch(sf); }
00193 #endif
00194 
00195 private:
00196 #ifndef QT_NO_DATASTREAM
00197     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
00198     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
00199 #endif
00200     QSizePolicy(int i) : data(i) { }
00201 
00202     quint32 data;
00203 /*  use bit flags instead, keep it here for improved readability for now
00204     quint32 horzPolicy : 4;
00205     quint32 vertPolicy : 4;
00206     quint32 hfw : 1;
00207     quint32 ctype : 5;
00208     quint32 wfh : 1;
00209     quint32 padding : 1;   // we cannot use the highest bit
00210     quint32 horStretch : 8;
00211     quint32 verStretch : 8;
00212 */
00213 
00214 };
00215 
00216 Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
00217 
00218 #ifndef QT_NO_DATASTREAM
00219 // implemented in qlayout.cpp
00220 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
00221 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
00222 #endif
00223 
00224 inline void QSizePolicy::transpose() {
00225     Policy hData = horizontalPolicy();
00226     Policy vData = verticalPolicy();
00227     uchar hStretch = uchar(horizontalStretch());
00228     uchar vStretch = uchar(verticalStretch());
00229     setHorizontalPolicy(vData);
00230     setVerticalPolicy(hData);
00231     setHorizontalStretch(vStretch);
00232     setVerticalStretch(hStretch);
00233 }
00234 
00235 QT_END_NAMESPACE
00236 
00237 QT_END_HEADER
00238 
00239 #endif // QSIZEPOLICY_H