qsizepolicy.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 QtWidgets 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. #ifndef QSIZEPOLICY_H
  40. #define QSIZEPOLICY_H
  41. #include <QtCore/qobject.h>
  42. QT_BEGIN_NAMESPACE
  43. class QVariant;
  44. class QSizePolicy;
  45. Q_DECL_CONST_FUNCTION inline uint qHash(QSizePolicy key, uint seed = 0) Q_DECL_NOTHROW;
  46. class Q_WIDGETS_EXPORT QSizePolicy
  47. {
  48. Q_GADGET
  49. Q_FLAGS(ControlTypes)
  50. public:
  51. enum PolicyFlag {
  52. GrowFlag = 1,
  53. ExpandFlag = 2,
  54. ShrinkFlag = 4,
  55. IgnoreFlag = 8
  56. };
  57. enum Policy {
  58. Fixed = 0,
  59. Minimum = GrowFlag,
  60. Maximum = ShrinkFlag,
  61. Preferred = GrowFlag | ShrinkFlag,
  62. MinimumExpanding = GrowFlag | ExpandFlag,
  63. Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
  64. Ignored = ShrinkFlag | GrowFlag | IgnoreFlag
  65. };
  66. Q_ENUM(Policy)
  67. enum ControlType {
  68. DefaultType = 0x00000001,
  69. ButtonBox = 0x00000002,
  70. CheckBox = 0x00000004,
  71. ComboBox = 0x00000008,
  72. Frame = 0x00000010,
  73. GroupBox = 0x00000020,
  74. Label = 0x00000040,
  75. Line = 0x00000080,
  76. LineEdit = 0x00000100,
  77. PushButton = 0x00000200,
  78. RadioButton = 0x00000400,
  79. Slider = 0x00000800,
  80. SpinBox = 0x00001000,
  81. TabWidget = 0x00002000,
  82. ToolButton = 0x00004000
  83. };
  84. Q_DECLARE_FLAGS(ControlTypes, ControlType)
  85. QSizePolicy() : data(0) { }
  86. QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
  87. : data(0) {
  88. bits.horPolicy = horizontal;
  89. bits.verPolicy = vertical;
  90. setControlType(type);
  91. }
  92. Policy horizontalPolicy() const { return static_cast<Policy>(bits.horPolicy); }
  93. Policy verticalPolicy() const { return static_cast<Policy>(bits.verPolicy); }
  94. ControlType controlType() const;
  95. void setHorizontalPolicy(Policy d) { bits.horPolicy = d; }
  96. void setVerticalPolicy(Policy d) { bits.verPolicy = d; }
  97. void setControlType(ControlType type);
  98. Qt::Orientations expandingDirections() const {
  99. return ( (verticalPolicy() & ExpandFlag) ? Qt::Vertical : Qt::Orientations() )
  100. | ( (horizontalPolicy() & ExpandFlag) ? Qt::Horizontal : Qt::Orientations() ) ;
  101. }
  102. void setHeightForWidth(bool b) { bits.hfw = b; }
  103. bool hasHeightForWidth() const { return bits.hfw; }
  104. void setWidthForHeight(bool b) { bits.wfh = b; }
  105. bool hasWidthForHeight() const { return bits.wfh; }
  106. bool operator==(const QSizePolicy& s) const { return data == s.data; }
  107. bool operator!=(const QSizePolicy& s) const { return data != s.data; }
  108. friend Q_DECL_CONST_FUNCTION uint qHash(QSizePolicy key, uint seed) Q_DECL_NOTHROW { return qHash(key.data, seed); }
  109. operator QVariant() const;
  110. int horizontalStretch() const { return static_cast<int>(bits.horStretch); }
  111. int verticalStretch() const { return static_cast<int>(bits.verStretch); }
  112. void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
  113. void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast<quint32>(qBound(0, stretchFactor, 255)); }
  114. bool retainSizeWhenHidden() const { return bits.retainSizeWhenHidden; }
  115. void setRetainSizeWhenHidden(bool retainSize) { bits.retainSizeWhenHidden = retainSize; }
  116. void transpose();
  117. private:
  118. #ifndef QT_NO_DATASTREAM
  119. friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
  120. friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
  121. #endif
  122. QSizePolicy(int i) : data(i) { }
  123. struct Bits {
  124. quint32 horStretch : 8;
  125. quint32 verStretch : 8;
  126. quint32 horPolicy : 4;
  127. quint32 verPolicy : 4;
  128. quint32 ctype : 5;
  129. quint32 hfw : 1;
  130. quint32 wfh : 1;
  131. quint32 retainSizeWhenHidden : 1;
  132. };
  133. union {
  134. Bits bits;
  135. quint32 data;
  136. };
  137. };
  138. #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
  139. // Can't add in Qt 5, as QList<QSizePolicy> would be BiC:
  140. Q_DECLARE_TYPEINFO(QSizePolicy, Q_PRIMITIVE_TYPE);
  141. #endif
  142. Q_DECLARE_OPERATORS_FOR_FLAGS(QSizePolicy::ControlTypes)
  143. #ifndef QT_NO_DATASTREAM
  144. Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &);
  145. Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &);
  146. #endif
  147. #ifndef QT_NO_DEBUG_STREAM
  148. Q_WIDGETS_EXPORT QDebug operator<<(QDebug dbg, const QSizePolicy &);
  149. #endif
  150. inline void QSizePolicy::transpose() {
  151. Policy hData = horizontalPolicy();
  152. Policy vData = verticalPolicy();
  153. int hStretch = horizontalStretch();
  154. int vStretch = verticalStretch();
  155. setHorizontalPolicy(vData);
  156. setVerticalPolicy(hData);
  157. setHorizontalStretch(vStretch);
  158. setVerticalStretch(hStretch);
  159. }
  160. QT_END_NAMESPACE
  161. #endif // QSIZEPOLICY_H