6#include "QskLayoutElement.h"
7#include "QskLayoutMetrics.h"
8#include "QskSizePolicy.h"
10static inline bool qskCanGrow(
13 const quint32 growFlags = QskSizePolicy::GrowFlag | QskSizePolicy::ExpandFlag;
14 return element->sizePolicy().policy( orientation ) & growFlags;
17QskLayoutElement::QskLayoutElement()
21QskLayoutElement::~QskLayoutElement()
25QSizeF QskLayoutElement::sizeConstraint(
26 Qt::SizeHint which,
const QSizeF& constraint )
const
28 if ( constraint.isValid() )
31 auto policy = sizePolicy();
33 bool ignoreWidth =
false;
34 bool ignoreHeight =
false;
36 if ( which == Qt::PreferredSize )
42 ignoreWidth = policy.policy( Qt::Horizontal ) & QskSizePolicy::IgnoreFlag;
43 ignoreHeight = policy.policy( Qt::Vertical ) & QskSizePolicy::IgnoreFlag;
45 if ( ( ignoreWidth && ignoreHeight )
46 || ( ignoreWidth && constraint.height() >= 0.0 )
47 || ( ignoreHeight && constraint.width() >= 0.0 ) )
53 const auto whichH = policy.effectiveSizeHintType( which, Qt::Horizontal );
54 const auto whichV = policy.effectiveSizeHintType( which, Qt::Vertical );
58 int constraintType = QskSizePolicy::Unconstrained;
64 if ( constraint.height() >= 0.0 )
66 const auto h = boundedSize( Qt::Vertical, constraint.height() );
67 size = sizeHint( whichV, QSizeF( -1.0, h ) );
69 if ( ( whichH != whichV ) || ( size.height() != h ) )
70 constraintType = QskSizePolicy::WidthForHeight;
72 else if ( constraint.width() >= 0.0 )
74 const auto w = boundedSize( Qt::Horizontal, constraint.width() );
75 size = sizeHint( whichH, QSizeF( w, -1.0 ) );
77 if ( ( whichV != whichH ) || ( size.width() != w ) )
78 constraintType = QskSizePolicy::HeightForWidth;
82 constraintType = policy.constraintType();
84 switch( constraintType )
86 case QskSizePolicy::WidthForHeight:
88 size = sizeHint( whichV );
91 case QskSizePolicy::HeightForWidth:
93 size = sizeHint( whichH );
98 if ( whichV != whichH )
101 size.rwidth() = sizeHint( whichH ).width();
104 size.rheight() = sizeHint( whichV ).height();
108 size = sizeHint( whichH );
114 switch( constraintType )
116 case QskSizePolicy::HeightForWidth:
118 const QSizeF c( size.width(), -1.0 );
119 size.rheight() = sizeHint( whichV, c ).height();
122 case QskSizePolicy::WidthForHeight:
124 const QSizeF c( -1.0, size.height() );
125 size.rwidth() = sizeHint( whichH, c ).width();
130 if ( ignoreWidth || constraint.width() >= 0.0 )
131 size.rwidth() = -1.0;
133 if ( ignoreHeight || constraint.height() >= 0.0 )
134 size.rheight() = -1.0;
139QSizeF QskLayoutElement::constrainedSize(
const QSizeF& size )
const
141 if ( size.width() <= 0.0 && size.height() <= 0.0 )
142 return QSizeF( 0.0, 0.0 );
146 const auto policy = sizePolicy();
148 switch( policy.constraintType() )
150 case QskSizePolicy::WidthForHeight:
152 const qreal h = boundedSize( Qt::Vertical, size.height() );
154 min = sizeConstraint( Qt::MinimumSize, QSizeF( -1.0, h ) );
155 max = sizeConstraint( Qt::MaximumSize, QSizeF( -1.0, h ) );
157 min.rheight() = max.rheight() = h;
160 case QskSizePolicy::HeightForWidth:
162 const qreal w = boundedSize( Qt::Horizontal, size.width() );
164 min = sizeConstraint( Qt::MinimumSize, QSizeF( w, -1.0 ) );
165 max = sizeConstraint( Qt::MaximumSize, QSizeF( w, -1.0 ) );
167 min.rwidth() = max.rwidth() = w;
172 min = sizeConstraint( Qt::MinimumSize, QSizeF() );
173 max = sizeConstraint( Qt::MaximumSize, QSizeF() );
177 qreal width = size.width();
178 qreal height = size.height();
180 if ( max.width() >= 0.0 )
181 width = qMin( width, max.width() );
183 if ( max.height() >= 0.0 )
184 height = qMin( height, max.height() );
186 width = qMax( width, min.width() );
187 height = qMax( height, min.height() );
189 return QSizeF( width, height );
193 Qt::Orientation orientation, qreal constraint )
const
195 const auto policy = sizePolicy().policy( orientation );
197 qreal minimum, preferred, maximum;
199 const auto expandFlags = QskSizePolicy::GrowFlag | QskSizePolicy::ExpandFlag;
201 if ( ( policy & QskSizePolicy::ShrinkFlag ) &&
202 ( policy & expandFlags ) && ( policy & QskSizePolicy::IgnoreFlag ) )
206 minimum = metric( orientation, Qt::MinimumSize, -1.0 );
207 maximum = metric( orientation, Qt::MaximumSize, -1.0 );
212 if ( constraint >= 0.0 )
214 if ( !( policy & QskSizePolicy::ConstrainedFlag ) )
218 preferred = metric( orientation, Qt::PreferredSize, constraint );
220 if ( policy & QskSizePolicy::ShrinkFlag )
221 minimum = metric( orientation, Qt::MinimumSize, -1.0 );
225 if ( policy & expandFlags )
226 maximum = metric( orientation, Qt::MaximumSize, -1.0 );
230 if ( policy & QskSizePolicy::IgnoreFlag )
237qreal QskLayoutElement::metric( Qt::Orientation orientation,
238 Qt::SizeHint which, qreal constraint )
const
242 if ( which == Qt::MinimumSize )
244 const auto hint = sizeHint( Qt::MinimumSize );
246 if ( orientation == Qt::Horizontal )
247 metric = hint.width();
249 metric = hint.height();
254 else if ( which == Qt::MaximumSize )
256 const auto hint = sizeHint( Qt::MaximumSize );
258 if ( orientation == Qt::Horizontal )
259 metric = hint.width();
261 metric = hint.height();
264 metric = QskLayoutMetrics::unlimited;
266 else if ( which == Qt::PreferredSize )
268 const auto constraintType = sizePolicy().constraintType();
270 if ( constraintType == QskSizePolicy::Unconstrained )
272 const auto hint = sizeHint( Qt::PreferredSize );
274 if ( orientation == Qt::Horizontal )
275 metric = hint.width();
277 metric = hint.height();
281 if ( constraint < 0.0 )
283 auto hint = sizeHint( Qt::PreferredSize );
285 if ( orientation == Qt::Horizontal )
287 if ( constraintType == QskSizePolicy::WidthForHeight )
288 hint.setWidth( widthForHeight( hint.height() ) );
290 metric = hint.width();
294 if ( constraintType == QskSizePolicy::HeightForWidth )
295 hint.setHeight( heightForWidth( hint.width() ) );
297 metric = hint.height();
302 if ( orientation == Qt::Horizontal )
304 if ( !qskCanGrow(
this, Qt::Vertical ) )
306 const auto maxH = sizeHint( Qt::PreferredSize ).height();
309 constraint = qMin( constraint, maxH );
312 metric = widthForHeight( constraint );
316 if ( !qskCanGrow(
this, Qt::Horizontal ) )
318 const auto maxW = sizeHint( Qt::PreferredSize ).width();
321 constraint = qMin( constraint, maxW );
324 metric = heightForWidth( constraint );
336qreal QskLayoutElement::boundedSize( Qt::Orientation orientation, qreal size )
const
338 const auto policy = sizePolicy();
340 const auto whichMin = policy.effectiveSizeHintType( Qt::MinimumSize, orientation );
341 const auto whichMax = policy.effectiveSizeHintType( Qt::MaximumSize, orientation );
343 const auto hintMin = sizeHint( whichMin );
344 const auto hintMax = ( whichMax == whichMin ) ? hintMin : sizeHint( whichMax );
346 if ( orientation == Qt::Horizontal )
348 if ( hintMax.width() >= 0.0 )
349 size = qMin( size, hintMax.width() );
351 size = qMax( size, hintMin.width() );
355 if ( hintMax.height() >= 0.0 )
356 size = qMin( size, hintMax.height() );
358 size = qMax( size, hintMin.height() );
366QskItemLayoutElement::QskItemLayoutElement(
const QQuickItem* item )
373 return qskSizePolicy( m_item );
376QSizeF QskItemLayoutElement::sizeHint( Qt::SizeHint which,
377 const QSizeF& constraint )
const
379 return qskEffectiveSizeHint( m_item, which, constraint );
382Qt::Alignment QskItemLayoutElement::alignment()
const
384 return qskLayoutAlignmentHint( m_item );