Add uniformCellWidths and uniformCellHeights to QGridLayoutEngine

Some users wish to make layouts with uniform column widths or
uniform row heights, ignoring the sizeHints of individual items.
This patch enables this mode in the QGridLayoutEngine, used
by the QuickLayout.

The sizeHints are aggregated and their average preferred size
and extreme minimum and maximum sizes are respected in the
layout.

Change-Id: Ibb9409eb0a11a1ce8bb305f44a4cb0071c871ec9
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Matthias Rauter 2023-05-04 10:45:19 +02:00 committed by Jan Arve Sæther
parent 38c8eb8564
commit b8b34544d0
2 changed files with 59 additions and 0 deletions

View File

@ -758,6 +758,8 @@ QGridLayoutEngine::QGridLayoutEngine(Qt::Alignment defaultAlignment, bool snapTo
m_visualDirection = Qt::LeftToRight;
m_defaultAlignment = defaultAlignment;
m_snapToPixelGrid = snapToPixelGrid;
m_uniformCellWidths = false;
m_uniformCellHeights = false;
invalidate();
}
@ -875,6 +877,34 @@ qreal QGridLayoutEngine::rowSizeHint(Qt::SizeHint which, int row, Qt::Orientatio
return q_infos[orientation].boxes.value(row).q_sizes(which);
}
bool QGridLayoutEngine::uniformCellWidths() const
{
return m_uniformCellWidths;
}
void QGridLayoutEngine::setUniformCellWidths(bool uniformCellWidths)
{
if (m_uniformCellWidths == uniformCellWidths)
return;
m_uniformCellWidths = uniformCellWidths;
invalidate();
}
bool QGridLayoutEngine::uniformCellHeights() const
{
return m_uniformCellHeights;
}
void QGridLayoutEngine::setUniformCellHeights(bool uniformCellHeights)
{
if (m_uniformCellHeights == uniformCellHeights)
return;
m_uniformCellHeights = uniformCellHeights;
invalidate();
}
void QGridLayoutEngine::setRowAlignment(int row, Qt::Alignment alignment,
Qt::Orientation orientation)
{
@ -1499,6 +1529,27 @@ void QGridLayoutEngine::fillRowData(QGridLayoutRowData *rowData,
rowSpacing = qMax(windowMargin, rowSpacing);
}
}
if (rowData->boxes.size() > 1 &&
((orientation == Qt::Horizontal && m_uniformCellWidths) ||
(orientation == Qt::Vertical && m_uniformCellHeights))) {
qreal averagePreferredSize = 0.;
qreal minimumMaximumSize = std::numeric_limits<qreal>::max();
qreal maximumMinimumSize = 0.;
for (const auto &box : rowData->boxes) {
averagePreferredSize += box.q_preferredSize;
minimumMaximumSize = qMin(minimumMaximumSize, box.q_maximumSize);
maximumMinimumSize = qMax(maximumMinimumSize, box.q_minimumSize);
}
averagePreferredSize /= rowData->boxes.size();
minimumMaximumSize = qMax(minimumMaximumSize, maximumMinimumSize);
averagePreferredSize = qBound(maximumMinimumSize, averagePreferredSize, minimumMaximumSize);
for (auto &box : rowData->boxes) {
box.q_preferredSize = averagePreferredSize;
box.q_minimumSize = maximumMinimumSize;
box.q_maximumSize = minimumMaximumSize;
}
}
}
void QGridLayoutEngine::ensureEffectiveFirstAndLastRows() const

View File

@ -334,6 +334,12 @@ public:
qreal rowSizeHint(Qt::SizeHint which, int row,
Qt::Orientation orientation = Qt::Vertical) const;
bool uniformCellWidths() const;
void setUniformCellWidths(bool uniformCellWidths);
bool uniformCellHeights() const;
void setUniformCellHeights(bool uniformCellHeights);
void setRowAlignment(int row, Qt::Alignment alignment, Qt::Orientation orientation);
Qt::Alignment rowAlignment(int row, Qt::Orientation orientation) const;
@ -415,6 +421,8 @@ private:
// Configuration
Qt::Alignment m_defaultAlignment;
unsigned m_snapToPixelGrid : 1;
unsigned m_uniformCellWidths : 1;
unsigned m_uniformCellHeights : 1;
// Lazily computed from the above user input
mutable QHVContainer<int> q_cachedEffectiveFirstRows;