grid layout code improved
This commit is contained in:
parent
9e1e37cf6f
commit
66041bca22
|
@ -670,6 +670,7 @@ void QskGridLayoutEngine::setupChain( Qt::Orientation orientation,
|
|||
before adding those that occupy more than one cell
|
||||
*/
|
||||
QVarLengthArray< const Element* > postponed;
|
||||
postponed.reserve( m_data->elements.size() );
|
||||
|
||||
for ( const auto& element : m_data->elements )
|
||||
{
|
||||
|
@ -697,7 +698,7 @@ void QskGridLayoutEngine::setupChain( Qt::Orientation orientation,
|
|||
const auto& settings = m_data->settings( orientation );
|
||||
|
||||
for ( const auto& setting : settings.settings() )
|
||||
chain.narrowCell( setting.position, setting.cell() );
|
||||
chain.shrinkCell( setting.position, setting.cell() );
|
||||
|
||||
for ( const auto element : postponed )
|
||||
{
|
||||
|
|
|
@ -37,7 +37,7 @@ void QskLayoutChain::reset( int count, qreal constraint )
|
|||
m_validCells = 0;
|
||||
}
|
||||
|
||||
void QskLayoutChain::narrowCell( int index, const CellData& newCell )
|
||||
void QskLayoutChain::shrinkCell( int index, const CellData& newCell )
|
||||
{
|
||||
if ( !newCell.isValid )
|
||||
return;
|
||||
|
@ -58,11 +58,17 @@ void QskLayoutChain::narrowCell( int index, const CellData& newCell )
|
|||
|
||||
if ( !newCell.hint.isDefault() )
|
||||
{
|
||||
cell.hint.setSizes(
|
||||
qMax( cell.hint.minimum(), newCell.hint.minimum() ),
|
||||
qMax( cell.hint.preferred(), newCell.hint.preferred() ),
|
||||
qMin( cell.hint.maximum(), newCell.hint.maximum() )
|
||||
);
|
||||
auto& hint = cell.hint;
|
||||
auto& newHint = newCell.hint;
|
||||
|
||||
hint.setMinimum( qMax( hint.minimum(), newHint.minimum() ) );
|
||||
hint.setPreferred( qMax( hint.preferred(), newHint.preferred() ) );
|
||||
|
||||
if ( newHint.maximum() < hint.maximum() )
|
||||
{
|
||||
hint.setMaximum( newHint.maximum() );
|
||||
cell.isShrunk = true;
|
||||
}
|
||||
|
||||
cell.hint.normalize();
|
||||
}
|
||||
|
@ -244,7 +250,7 @@ QskLayoutChain::Segments QskLayoutChain::segments( qreal size ) const
|
|||
qreal offset = 0.0;
|
||||
qreal extra = 0.0;;
|
||||
|
||||
switch( m_extraSpacingAt )
|
||||
switch( m_fillMode )
|
||||
{
|
||||
case Leading:
|
||||
offset = padding;
|
||||
|
@ -290,11 +296,19 @@ QskLayoutChain::Segments QskLayoutChain::distributed(
|
|||
fillSpacing = m_spacing;
|
||||
|
||||
segment.start = offset;
|
||||
segment.length = cell.hint.size( which ) + extra;
|
||||
|
||||
if ( which == Qt::MaximumSize && cell.isShrunk )
|
||||
{
|
||||
segment.length = cell.hint.size( which );
|
||||
offset += segment.length + extra;
|
||||
}
|
||||
else
|
||||
{
|
||||
segment.length = cell.hint.size( which ) + extra;
|
||||
offset += segment.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
|
|
@ -29,19 +29,6 @@ class QskLayoutChain
|
|||
class CellData
|
||||
{
|
||||
public:
|
||||
inline bool operator==( const CellData& other ) const
|
||||
{
|
||||
return ( isValid == other.isValid )
|
||||
&& ( canGrow == other.canGrow )
|
||||
&& ( stretch == other.stretch )
|
||||
&& ( hint == other.hint );
|
||||
}
|
||||
|
||||
inline bool operator!=( const CellData& other ) const
|
||||
{
|
||||
return !( *this == other );
|
||||
}
|
||||
|
||||
inline qreal size( int which ) const
|
||||
{
|
||||
return hint.size( which );
|
||||
|
@ -56,10 +43,11 @@ class QskLayoutChain
|
|||
|
||||
int stretch = 0;
|
||||
bool canGrow = false;
|
||||
bool isShrunk = false;
|
||||
bool isValid = false;
|
||||
};
|
||||
|
||||
enum ExtraSpacing
|
||||
enum FillMode
|
||||
{
|
||||
Leading = 1 << 0,
|
||||
Trailing = 1 << 1
|
||||
|
@ -73,7 +61,7 @@ class QskLayoutChain
|
|||
void reset( int count, qreal constraint );
|
||||
void expandCell( int index, const CellData& );
|
||||
void expandCells( int start, int end, const CellData& );
|
||||
void narrowCell( int index, const CellData& );
|
||||
void shrinkCell( int index, const CellData& );
|
||||
void finish();
|
||||
|
||||
const CellData& cell( int index ) const { return m_cells[ index ]; }
|
||||
|
@ -81,7 +69,8 @@ class QskLayoutChain
|
|||
bool setSpacing( qreal spacing );
|
||||
qreal spacing() const { return m_spacing; }
|
||||
|
||||
void setExtraSpacingAt( int extraSpacingAt ) { m_extraSpacingAt = extraSpacingAt; }
|
||||
void setFillMode( int mode ) { m_fillMode = mode; }
|
||||
int fillMode() const { return m_fillMode; }
|
||||
|
||||
Segments segments( qreal size ) const;
|
||||
QskLayoutHint boundingHint() const { return m_boundingHint; }
|
||||
|
@ -98,7 +87,7 @@ class QskLayoutChain
|
|||
qreal m_constraint = -2.0;
|
||||
|
||||
qreal m_spacing = 0;
|
||||
int m_extraSpacingAt;
|
||||
int m_fillMode = 0;
|
||||
|
||||
int m_sumStretches = 0;
|
||||
int m_validCells = 0;
|
||||
|
|
|
@ -130,7 +130,6 @@ Qt::Alignment QskLayoutEngine2D::defaultAlignment() const
|
|||
return static_cast< Qt::Alignment >( m_data->defaultAlignment );
|
||||
}
|
||||
|
||||
|
||||
qreal QskLayoutEngine2D::defaultSpacing( Qt::Orientation ) const
|
||||
{
|
||||
return 5.0; // should be from the skin
|
||||
|
@ -168,27 +167,33 @@ bool QskLayoutEngine2D::setExtraSpacingAt( Qt::Edges edges )
|
|||
|
||||
m_data->extraSpacingAt = edges;
|
||||
|
||||
int value = 0;
|
||||
{
|
||||
int fillMode = 0;
|
||||
|
||||
if ( edges & Qt::LeftEdge )
|
||||
value |= QskLayoutChain::Leading;
|
||||
fillMode |= QskLayoutChain::Leading;
|
||||
|
||||
if ( edges & Qt::RightEdge )
|
||||
value |= QskLayoutChain::Trailing;
|
||||
fillMode |= QskLayoutChain::Trailing;
|
||||
|
||||
m_data->columnChain.setExtraSpacingAt( value );
|
||||
m_data->columnChain.setFillMode( fillMode );
|
||||
}
|
||||
|
||||
value = 0;
|
||||
{
|
||||
int fillMode = 0;
|
||||
|
||||
if ( edges & Qt::TopEdge )
|
||||
value |= QskLayoutChain::Leading;
|
||||
fillMode |= QskLayoutChain::Leading;
|
||||
|
||||
if ( edges & Qt::BottomEdge )
|
||||
value |= QskLayoutChain::Trailing;
|
||||
fillMode |= QskLayoutChain::Trailing;
|
||||
|
||||
m_data->rowChain.setExtraSpacingAt( value );
|
||||
m_data->rowChain.setFillMode( fillMode );
|
||||
}
|
||||
|
||||
invalidate();
|
||||
m_data->layoutSize = QSize();
|
||||
m_data->rows.clear();
|
||||
m_data->columns.clear();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -357,7 +362,7 @@ void QskLayoutEngine2D::setupChain( Qt::Orientation orientation,
|
|||
void QskLayoutEngine2D::updateSegments( const QSizeF& size ) const
|
||||
{
|
||||
auto& rowChain = m_data->rowChain;
|
||||
auto& colLine = m_data->columnChain;
|
||||
auto& columnChain = m_data->columnChain;
|
||||
|
||||
auto& rows = m_data->rows;
|
||||
auto& columns = m_data->columns;
|
||||
|
@ -372,14 +377,14 @@ void QskLayoutEngine2D::updateSegments( const QSizeF& size ) const
|
|||
rows = rowChain.segments( size.height() );
|
||||
|
||||
setupChain( Qt::Horizontal, rows );
|
||||
columns = colLine.segments( size.width() );
|
||||
columns = columnChain.segments( size.width() );
|
||||
|
||||
break;
|
||||
}
|
||||
case QskLayoutConstraint::HeightForWidth:
|
||||
{
|
||||
setupChain( Qt::Horizontal );
|
||||
columns = colLine.segments( size.width() );
|
||||
columns = columnChain.segments( size.width() );
|
||||
|
||||
setupChain( Qt::Vertical, m_data->columns );
|
||||
rows = rowChain.segments( size.height() );
|
||||
|
@ -389,7 +394,7 @@ void QskLayoutEngine2D::updateSegments( const QSizeF& size ) const
|
|||
default:
|
||||
{
|
||||
setupChain( Qt::Horizontal );
|
||||
columns = colLine.segments( size.width() );
|
||||
columns = columnChain.segments( size.width() );
|
||||
|
||||
setupChain( Qt::Vertical );
|
||||
rows = rowChain.segments( size.height() );
|
||||
|
|
Loading…
Reference in New Issue