QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskSizePolicy.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_SIZE_POLICY_H
7#define QSK_SIZE_POLICY_H
8
9#include "QskGlobal.h"
10#include <qnamespace.h>
11#include <qmetatype.h>
12
13class QSK_EXPORT QskSizePolicy
14{
15 Q_GADGET
16
17 Q_PROPERTY( Policy horizontal READ horizontalPolicy WRITE setHorizontalPolicy )
18 Q_PROPERTY( Policy vertical READ verticalPolicy WRITE setVerticalPolicy )
19
20 public:
21 enum Flag
22 {
23 GrowFlag = 1 << 0,
24 ExpandFlag = 1 << 1,
25 ShrinkFlag = 1 << 2,
26 IgnoreFlag = 1 << 3,
27 ConstrainedFlag = 1 << 4
28 };
29
30 enum Policy
31 {
32 Fixed = 0,
33
34 Minimum = GrowFlag,
35 Maximum = ShrinkFlag,
36 Preferred = GrowFlag | ShrinkFlag,
37 MinimumExpanding = GrowFlag | ExpandFlag,
38 Expanding = GrowFlag | ShrinkFlag | ExpandFlag,
39
40 Ignored = ShrinkFlag | GrowFlag | IgnoreFlag,
41
42 Constrained = ConstrainedFlag,
43
44 ConstrainedMinimum = ConstrainedFlag | Minimum,
45 ConstrainedMaximum = ConstrainedFlag | Maximum,
46 ConstrainedPreferred = ConstrainedFlag | Preferred,
47 ConstrainedMinimumExpanding = ConstrainedFlag | MinimumExpanding,
48 ConstrainedExpanding = ConstrainedFlag | Expanding
49 };
50
51 enum ConstraintType
52 {
53 Unconstrained = 0,
54
55 WidthForHeight = 1 << 0,
56 HeightForWidth = 1 << 1
57 };
58
59 Q_ENUM( Flag )
60 Q_ENUM( Policy )
61 Q_ENUM( ConstraintType )
62
63 constexpr QskSizePolicy() noexcept = default;
64 constexpr QskSizePolicy( Policy horizontalPolicy, Policy verticalPolicy ) noexcept;
65
66 constexpr bool operator==( const QskSizePolicy& ) const noexcept;
67 constexpr bool operator!=( const QskSizePolicy& ) const noexcept;
68
69 void setHorizontalPolicy( Policy ) noexcept;
70 constexpr Policy horizontalPolicy() const noexcept;
71
72 void setVerticalPolicy( Policy ) noexcept;
73 constexpr Policy verticalPolicy() const noexcept;
74
75 void setPolicy( Qt::Orientation, Policy ) noexcept;
76 constexpr Policy policy( Qt::Orientation ) const noexcept;
77
78 ConstraintType constraintType() const noexcept;
79 constexpr bool isConstrained( Qt::Orientation ) const noexcept;
80
81 Qt::SizeHint effectiveSizeHintType(
82 Qt::SizeHint, Qt::Orientation ) const noexcept;
83
84 constexpr QskSizePolicy transposed() const noexcept;
85 void transpose() noexcept;
86
87 private:
88 unsigned char m_horizontalPolicy = Ignored;
89 unsigned char m_verticalPolicy = Ignored;
90};
91
92inline constexpr QskSizePolicy::QskSizePolicy(
93 Policy horizontalPolicy, Policy verticalPolicy ) noexcept
94 : m_horizontalPolicy( horizontalPolicy )
95 , m_verticalPolicy( verticalPolicy )
96{
97}
98
99inline constexpr bool QskSizePolicy::operator==(
100 const QskSizePolicy& other ) const noexcept
101{
102 return ( other.m_horizontalPolicy == m_horizontalPolicy ) &&
103 ( other.m_verticalPolicy == m_verticalPolicy );
104}
105
106inline constexpr bool QskSizePolicy::operator!=(
107 const QskSizePolicy& other ) const noexcept
108{
109 return !( *this == other );
110}
111
112inline void QskSizePolicy::setHorizontalPolicy( Policy policy ) noexcept
113{
114 m_horizontalPolicy = static_cast< unsigned char >( policy );
115}
116
117inline constexpr QskSizePolicy::Policy QskSizePolicy::horizontalPolicy() const noexcept
118{
119 return static_cast< Policy >( m_horizontalPolicy );
120}
121
122inline void QskSizePolicy::setVerticalPolicy( Policy policy ) noexcept
123{
124 m_verticalPolicy = static_cast< unsigned char >( policy );
125}
126
127inline constexpr QskSizePolicy::Policy QskSizePolicy::verticalPolicy() const noexcept
128{
129 return static_cast< Policy >( m_verticalPolicy );
130}
131
132inline constexpr QskSizePolicy::Policy QskSizePolicy::policy(
133 Qt::Orientation orientation ) const noexcept
134{
135 return static_cast< Policy >(
136 ( orientation == Qt::Horizontal ) ? m_horizontalPolicy : m_verticalPolicy );
137}
138
139inline void QskSizePolicy::setPolicy(
140 Qt::Orientation orientation, Policy policy ) noexcept
141{
142 if ( orientation == Qt::Horizontal )
143 m_horizontalPolicy = static_cast< unsigned char >( policy );
144 else
145 m_verticalPolicy = static_cast< unsigned char >( policy );
146}
147
148inline constexpr bool QskSizePolicy::isConstrained(
149 Qt::Orientation orientation ) const noexcept
150{
151 return ( policy( orientation ) & ConstrainedFlag );
152}
153
154inline constexpr QskSizePolicy QskSizePolicy::transposed() const noexcept
155{
156 return QskSizePolicy( verticalPolicy(), horizontalPolicy() );
157}
158
159#ifndef QT_NO_DEBUG_STREAM
160class QDebug;
161QSK_EXPORT QDebug operator<<( QDebug, const QskSizePolicy& );
162#endif
163
164Q_DECLARE_TYPEINFO( QskSizePolicy, Q_MOVABLE_TYPE );
165Q_DECLARE_METATYPE( QskSizePolicy )
166
167#endif