TileLayout: simplify code using the inherited "spacing" property

This commit is contained in:
Olivier Teulière 2012-01-29 21:43:00 +01:00
parent c11de9eed5
commit 3aab437022
4 changed files with 17 additions and 18 deletions

View file

@ -36,7 +36,8 @@ using namespace std;
BagWidget2::BagWidget2(QWidget *parent)
: QWidget(parent), m_game(NULL)
{
TileLayout *layout = new TileLayout(5);
TileLayout *layout = new TileLayout;
layout->setSpacing(5);
setLayout(layout);
}

View file

@ -47,7 +47,8 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
setForegroundRole(QPalette::Window);
setBackgroundRole(QPalette::Window);
TileLayout *layout = new TileLayout(1, BOARD_MAX + 1, BOARD_MAX + 1);
TileLayout *layout = new TileLayout(BOARD_MAX + 1, BOARD_MAX + 1);
layout->setSpacing(1);
// Line full of coordinates
layout->addWidget(new BasicTileWidget(this, ""));
for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
@ -180,7 +181,7 @@ void BoardWidget::paintEvent(QPaintEvent *)
QPainter painter(this);
QRect rect = boardLayout->getBoardRect();
const int size = boardLayout->getSquareSize();
const int spacing = boardLayout->getSpacing();
const int spacing = boardLayout->spacing();
QLine hLine(0, 0, rect.width() + 1, 0);
QLine vLine(0, 0, 0, rect.height() + 1);

View file

@ -29,9 +29,9 @@ using namespace std;
INIT_LOGGER(qt, TileLayout);
TileLayout::TileLayout(int spacing, int nbRows, int nbCols)
TileLayout::TileLayout(int nbRows, int nbCols)
: m_dynamicRow(nbRows == 0), m_dynamicCol(nbCols == 0),
m_nbCols(nbCols), m_nbRows(nbRows), m_space(spacing)
m_nbCols(nbCols), m_nbRows(nbRows)
{
setContentsMargins(0, 0, 0, 0);
}
@ -81,7 +81,7 @@ QLayoutItem *TileLayout::takeAt(int index)
QSize TileLayout::minimumSize() const
{
int size = m_space;
int size = spacing();
if (m_items.empty())
return QSize(size, size);
@ -90,11 +90,11 @@ QSize TileLayout::minimumSize() const
if (m_dynamicCol && m_dynamicRow)
return QSize(size, size);
else if (m_dynamicCol)
return QSize(size * ((m_items.size() - 1) / m_nbRows + 1) - m_space, size * m_nbRows - m_space);
return QSize(size * ((m_items.size() - 1) / m_nbRows + 1) - spacing(), size * m_nbRows - spacing());
else if (m_dynamicRow)
return QSize(size * m_nbCols - m_space, size * ((m_items.size() - 1) / m_nbCols + 1) - m_space);
return QSize(size * m_nbCols - spacing(), size * ((m_items.size() - 1) / m_nbCols + 1) - spacing());
else
return QSize(size * m_nbCols - m_space, size * m_nbRows - m_space);
return QSize(size * m_nbCols - spacing(), size * m_nbRows - spacing());
}
@ -129,8 +129,8 @@ void TileLayout::doLayout(const QRect &rect)
if (m_items.isEmpty())
return;
const int width = rect.width() + m_space;
const int height = rect.height() + m_space;
const int width = rect.width() + spacing();
const int height = rect.height() + spacing();
if (m_dynamicCol && m_dynamicRow)
{
// Dynamic number of columns. The square size is the biggest one
@ -177,7 +177,7 @@ void TileLayout::doLayout(const QRect &rect)
// Now the number of columns and rows are defined.
// Use that to draw the tiles.
const int squareSize = std::min(width / m_nbCols, height / m_nbRows) - m_space;
const int squareSize = std::min(width / m_nbCols, height / m_nbRows) - spacing();
int x = 0;
int y = 0;
int nbInRow = 1;
@ -186,12 +186,12 @@ void TileLayout::doLayout(const QRect &rect)
{
QRect itemRect(QPoint(x, y), QSize(squareSize, squareSize));
item->setGeometry(itemRect);
x += squareSize + m_space;
x += squareSize + spacing();
++nbInRow;
if (nbInRow > m_nbCols)
{
x = 0;
y += squareSize + m_space;
y += squareSize + spacing();
nbInRow = 1;
}
}

View file

@ -32,7 +32,7 @@ class TileLayout : public QLayout
DEFINE_LOGGER();
public:
TileLayout(int spacing, int nbRows = 0, int nbCols = 0);
TileLayout(int nbRows = 0, int nbCols = 0);
virtual ~TileLayout();
void clear();
@ -41,8 +41,6 @@ public:
int getSquareSize() const;
int getSpacing() const { return m_space; }
virtual void addItem(QLayoutItem *item) { m_items.append(item); }
virtual int count() const { return m_items.size(); }
virtual QLayoutItem *itemAt(int index) const { return m_items.value(index); }
@ -57,7 +55,6 @@ private:
bool m_dynamicCol;
int m_nbCols;
int m_nbRows;
int m_space;
void doLayout(const QRect &rect);
};