10#include "qwt_dyngrid_layout.h"
15class QwtDynGridLayout::PrivateData
23 void updateLayoutCache();
31 Qt::Orientations expanding;
37void QwtDynGridLayout::PrivateData::updateLayoutCache()
39 itemSizeHints.resize( itemList.count() );
44 it != itemList.constEnd(); ++it, index++ )
46 itemSizeHints[ index ] = ( *it )->sizeHint();
63 setSpacing( spacing );
64 setContentsMargins( margin, margin, margin, margin );
74 setSpacing( spacing );
80void QwtDynGridLayout::init()
82 m_data =
new QwtDynGridLayout::PrivateData;
83 m_data->maxColumns = m_data->numRows = m_data->numColumns = 0;
90 qDeleteAll( m_data->itemList );
97 m_data->isDirty =
true;
98 QLayout::invalidate();
121 return m_data->maxColumns;
130 m_data->itemList.append( item );
139 return m_data->itemList.isEmpty();
147 return m_data->itemList.count();
159 if ( index < 0 || index >= m_data->itemList.count() )
162 return m_data->itemList.at( index );
174 if ( index < 0 || index >= m_data->itemList.count() )
177 m_data->isDirty =
true;
178 return m_data->itemList.takeAt( index );
184 return m_data->itemList.count();
198 m_data->expanding = expanding;
213 return m_data->expanding;
224 QLayout::setGeometry( rect );
230 m_data->numRows =
itemCount() / m_data->numColumns;
238 it != m_data->itemList.constEnd(); ++it )
240 ( *it )->setGeometry( itemGeometries[index] );
262 if ( m_data->maxColumns > 0 )
270 const int rowWidth = maxRowWidth(
numColumns );
271 if ( rowWidth > width )
285int QwtDynGridLayout::maxRowWidth(
int numColumns )
const
293 if ( m_data->isDirty )
294 m_data->updateLayoutCache();
297 index < m_data->itemSizeHints.count(); index++ )
300 colWidth[col] = qMax( colWidth[col],
301 m_data->itemSizeHints[index].width() );
304 const QMargins m = contentsMargins();
306 int rowWidth = m.left() + m.right() + (
numColumns - 1 ) * spacing();
308 rowWidth += colWidth[col];
321 if ( m_data->isDirty )
322 m_data->updateLayoutCache();
325 for (
int i = 0; i < m_data->itemSizeHints.count(); i++ )
327 const int itemW = m_data->itemSizeHints[i].width();
345 uint numColumns )
const
349 return itemGeometries;
356 return itemGeometries;
363 bool expandH, expandV;
367 if ( expandH || expandV )
372 const QRect alignedRect = alignmentRect( rect );
375 const int xOffset = expandH ? 0 : alignedRect.x();
376 const int yOffset = expandV ? 0 : alignedRect.y();
381 const int xySpace = spacing();
383 const QMargins m = contentsMargins();
385 rowY[0] = yOffset + m.top();
386 for ( uint r = 1; r <
numRows; r++ )
387 rowY[r] = rowY[r - 1] + rowHeight[r - 1] + xySpace;
389 colX[0] = xOffset + m.left();
391 colX[c] = colX[c - 1] + colWidth[c - 1] + xySpace;
393 const int itemCount = m_data->itemList.size();
401 const QRect itemGeometry( colX[col], rowY[row],
402 colWidth[col], rowHeight[row] );
403 itemGeometries.append( itemGeometry );
406 return itemGeometries;
425 if ( m_data->isDirty )
426 m_data->updateLayoutCache();
428 for (
int index = 0; index < m_data->itemSizeHints.count(); index++ )
433 const QSize& size = m_data->itemSizeHints[index];
435 rowHeight[row] = ( col == 0 )
436 ? size.height() : qMax( rowHeight[row], size.height() );
437 colWidth[col] = ( row == 0 )
438 ? size.width() : qMax( colWidth[col], size.width() );
470 const QMargins m = contentsMargins();
472 int h = m.top() + m.bottom() + (
numRows - 1 ) * spacing();
473 for ( uint row = 0; row <
numRows; row++ )
497 bool expandH, expandV;
501 const QMargins m = contentsMargins();
505 int xDelta = rect.width() - m.left() - m.right() - (
numColumns - 1 ) * spacing();
507 xDelta -= colWidth[col];
513 const int space = xDelta / (
numColumns - col );
514 colWidth[col] += space;
526 int yDelta = rect.height() - m.top() - m.bottom() - (
numRows - 1 ) * spacing();
527 for ( uint row = 0; row <
numRows; row++ )
528 yDelta -= rowHeight[row];
532 for ( uint row = 0; row <
numRows; row++ )
534 const int space = yDelta / (
numRows - row );
535 rowHeight[row] += space;
556 if ( m_data->maxColumns > 0 )
568 const QMargins m = contentsMargins();
570 int h = m.top() + m.bottom() + (
numRows - 1 ) * spacing();
571 for ( uint row = 0; row <
numRows; row++ )
574 int w = m.left() + m.right() + (
numColumns - 1 ) * spacing();
578 return QSize( w, h );
588 return m_data->numRows;
598 return m_data->numColumns;
601#include "moc_qwt_dyngrid_layout.cpp"
void layoutGrid(uint numColumns, QVector< int > &rowHeight, QVector< int > &colWidth) const
virtual int heightForWidth(int) const override
virtual QLayoutItem * itemAt(int index) const override
virtual uint columnsForWidth(int width) const
Calculate the number of columns for a given width.
virtual bool isEmpty() const override
virtual QLayoutItem * takeAt(int index) override
virtual int maxItemWidth() const
void setExpandingDirections(Qt::Orientations)
virtual QSize sizeHint() const override
virtual Qt::Orientations expandingDirections() const override
Returns whether this layout can make use of more space than sizeHint().
uint maxColumns() const
Return the upper limit for the number of columns.
void stretchGrid(const QRect &rect, uint numColumns, QVector< int > &rowHeight, QVector< int > &colWidth) const
void setMaxColumns(uint maxColumns)
virtual void addItem(QLayoutItem *) override
Add an item to the next free position.
QList< QRect > layoutItems(const QRect &, uint numColumns) const
virtual void setGeometry(const QRect &) override
virtual ~QwtDynGridLayout()
Destructor.
QwtDynGridLayout(QWidget *, int margin=0, int spacing=-1)
virtual bool hasHeightForWidth() const override
virtual int count() const override
virtual void invalidate() override
Invalidate all internal caches.