TileLayout: respect the margins and alignment

This commit is contained in:
Olivier Teulière 2012-07-17 23:32:39 +02:00
parent aa557f8121
commit d693a53910
2 changed files with 36 additions and 28 deletions

View file

@ -81,56 +81,53 @@ QLayoutItem *TileLayout::takeAt(int index)
QSize TileLayout::minimumSize() const QSize TileLayout::minimumSize() const
{ {
int size = spacing(); const QSize marginsSize = geometry().size() - contentsRect().size();
if (m_items.empty()) if (m_items.empty())
return QSize(size, size); return marginsSize;
size += m_items.at(0)->minimumSize().width();
const QSize spacingSize(spacing(), spacing());
const int size = spacing() + m_items.at(0)->minimumSize().width();
if (m_dynamicCol && m_dynamicRow) if (m_dynamicCol && m_dynamicRow)
return QSize(size, size); return marginsSize - spacingSize + QSize(size, size);
else if (m_dynamicCol) else if (m_dynamicCol)
return QSize(size * ((m_items.size() - 1) / m_nbRows + 1) - spacing(), size * m_nbRows - spacing()); return marginsSize - spacingSize + QSize(size * ((m_items.size() - 1) / m_nbRows + 1), size * m_nbRows);
else if (m_dynamicRow) else if (m_dynamicRow)
return QSize(size * m_nbCols - spacing(), size * ((m_items.size() - 1) / m_nbCols + 1) - spacing()); return marginsSize - spacingSize + QSize(size * m_nbCols, size * ((m_items.size() - 1) / m_nbCols + 1));
else else
return QSize(size * m_nbCols - spacing(), size * m_nbRows - spacing()); return marginsSize - spacingSize + QSize(size * m_nbCols, size * m_nbRows);
} }
QSize TileLayout::sizeHint() const QSize TileLayout::sizeHint() const
{ {
int size = spacing(); const QSize marginsSize = geometry().size() - contentsRect().size();
if (m_items.empty()) if (m_items.empty())
return QSize(size, size); return marginsSize;
size += m_items.at(0)->sizeHint().width();
const QSize spacingSize(spacing(), spacing());
const int size = spacing() + m_items.at(0)->sizeHint().width();
if (m_dynamicCol && m_dynamicRow) if (m_dynamicCol && m_dynamicRow)
return QSize(size, size); return marginsSize - spacingSize + QSize(size, size);
else if (m_dynamicCol) else if (m_dynamicCol)
return QSize(size * ((m_items.size() - 1) / m_nbRows + 1) - spacing(), size * m_nbRows - spacing()); return marginsSize - spacingSize + QSize(size * ((m_items.size() - 1) / m_nbRows + 1), size * m_nbRows);
else if (m_dynamicRow) else if (m_dynamicRow)
return QSize(size * m_nbCols - spacing(), size * ((m_items.size() - 1) / m_nbCols + 1) - spacing()); return marginsSize - spacingSize + QSize(size * m_nbCols, size * ((m_items.size() - 1) / m_nbCols + 1));
else else
return QSize(size * m_nbCols - spacing(), size * m_nbRows - spacing()); return marginsSize - spacingSize + QSize(size * m_nbCols, size * m_nbRows);
} }
void TileLayout::setGeometry(const QRect &rect) void TileLayout::setGeometry(const QRect &rect)
{ {
QLayout::setGeometry(rect); QLayout::setGeometry(rect);
doLayout(rect);
}
void TileLayout::doLayout(const QRect &rect)
{
if (m_items.isEmpty()) if (m_items.isEmpty())
return; return;
const int width = rect.width() + spacing(); // We use contentsRect() to take margins into account
const int height = rect.height() + spacing(); const int width = contentsRect().width() + spacing();
const int height = contentsRect().height() + spacing();
if (m_dynamicCol && m_dynamicRow) if (m_dynamicCol && m_dynamicRow)
{ {
// Dynamic number of columns. The square size is the biggest one // Dynamic number of columns. The square size is the biggest one
@ -177,9 +174,22 @@ void TileLayout::doLayout(const QRect &rect)
// Now the number of columns and rows are defined. // Now the number of columns and rows are defined.
// Use that to draw the tiles. // Use that to draw the tiles.
const int squareSize = std::min(width / m_nbCols, height / m_nbRows) - spacing(); const int squareSizeWithSpacing = std::min(width / m_nbCols, height / m_nbRows);
int x = 0; const int squareSize = squareSizeWithSpacing - spacing();
int y = 0;
// Handle margins and alignment
int x = contentsMargins().left();
if (alignment() & Qt::AlignRight)
x += width - m_nbCols * squareSizeWithSpacing;
else if (alignment() & Qt::AlignHCenter)
x += (width - m_nbCols * squareSizeWithSpacing) / 2;
int y = contentsMargins().top();
if (alignment() & Qt::AlignBottom)
y += height - m_nbRows * squareSizeWithSpacing;
else if (alignment() & Qt::AlignVCenter)
y += (height - m_nbRows * squareSizeWithSpacing) / 2;
// Resize items
int nbInRow = 1; int nbInRow = 1;
QLayoutItem *item; QLayoutItem *item;
foreach (item, m_items) foreach (item, m_items)

View file

@ -55,8 +55,6 @@ private:
bool m_dynamicCol; bool m_dynamicCol;
int m_nbCols; int m_nbCols;
int m_nbRows; int m_nbRows;
void doLayout(const QRect &rect);
}; };
#endif #endif