Layout improvements:

- Handle the minimum size of the layout correctly
 - Draw the lines between tiles, instead of drawing on them
This commit is contained in:
Olivier Teulière 2010-10-22 16:59:43 +00:00
parent 3352337142
commit 6bf27a4509
2 changed files with 52 additions and 40 deletions

View file

@ -20,12 +20,10 @@
#include <algorithm> // For std::transform
#include <cmath>
//#include <QtGui/QPainter>
#include <QtGui/QGridLayout>
#include <QtGui/QMouseEvent>
// XXX
#include <QtGui/QTreeView>
#include <QtGui/QPainter>
// XXX
#include <iostream>
#include "board_widget.h"
@ -44,7 +42,7 @@ class BoardLayout : public QLayout
//Q_OBJECT
public:
BoardLayout(int nbCols): m_nbCols(nbCols), m_space(0)
BoardLayout(int nbCols, int spacing): m_nbCols(nbCols), m_space(spacing)
{
setContentsMargins(0, 0, 0, 0);
}
@ -62,6 +60,18 @@ public:
return m_items.at(m_nbCols + 1)->geometry().united(m_items.back()->geometry());
}
int getSquareSize() const
{
if (m_items.empty())
return 0;
return m_items.at(0)->geometry().width();
}
int getSpacing() const
{
return m_space;
}
virtual void addItem(QLayoutItem *item)
{
m_items.append(item);
@ -79,10 +89,10 @@ public:
}
virtual QSize minimumSize() const
{
QSize size;
QSize size(m_space, m_space);
if (!m_items.empty())
size.expandedTo(m_items.at(0)->minimumSize());
return size * m_nbCols;
size += m_items.at(0)->minimumSize();
return size * m_nbCols + QSize(5, 5);
}
virtual void setGeometry(const QRect &rect)
{
@ -132,7 +142,7 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
setForegroundRole(QPalette::Window);
setBackgroundRole(QPalette::Window);
BoardLayout *layout = new BoardLayout(BOARD_MAX + 1);
BoardLayout *layout = new BoardLayout(BOARD_MAX + 1, 1);
// Line full of coordinates
layout->addWidget(new BasicTileWidget(this, ""));
for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
@ -174,7 +184,6 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
setFrameStyle(QFrame::Panel);
// Use as much space as possible
setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
setMinimumSize(200, 200);
// Listen to changes in the coordinates
QObject::connect(&m_coordModel, SIGNAL(coordChanged(const Coord&, const Coord&)),
@ -237,10 +246,25 @@ QSize BoardWidget::sizeHint() const
void BoardWidget::paintEvent(QPaintEvent *)
{
const BoardLayout *boardLayout = (BoardLayout*)layout();
QPainter painter(this);
QRect rect = ((BoardLayout*)layout())->getBoardRect();
painter.drawRect(rect);
painter.drawRect(rect.adjusted(-1, -1, 1, 1));
QRect rect = boardLayout->getBoardRect();
const int size = boardLayout->getSquareSize();
const int spacing = boardLayout->getSpacing();
QLine hLine(0, 0, rect.width() + 1, 0);
QLine vLine(0, 0, 0, rect.height() + 1);
hLine.translate(size, size);
vLine.translate(size, size);
for (int i = 0; i <= BOARD_MAX; ++i)
{
painter.drawLine(hLine);
painter.drawLine(vLine);
hLine.translate(0, size + spacing);
vLine.translate(size + spacing, 0);
}
//painter.drawRect(rect);
painter.drawRect(rect.adjusted(-2, -2, 1, 1));
}

View file

@ -46,6 +46,7 @@ const QColor TileWidget::ArrowColour(10, 10, 10);
BasicTileWidget::BasicTileWidget(QWidget *parent, QString text)
: QWidget(parent), m_text(text)
{
setMinimumSize(10, 10);
}
@ -85,18 +86,9 @@ TileWidget::TileWidget(QWidget *parent, Multiplier multiplier,
m_row(row), m_col(col), m_isJoker(false),
m_isPreview(false), m_showArrow(false), m_horizontalArrow(true)
{
setMinimumSize(15, 15);
QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
policy.setHeightForWidth(true);
setSizePolicy(policy);
// Try to have a black background... FIXME: not working well!
QPalette pal = palette();
for (int i = 0; i <= 19; ++i)
pal.setColor((QPalette::ColorRole)i, Qt::black);
setPalette(pal);
setForegroundRole(QPalette::Window);
setBackgroundRole(QPalette::Window);
}
@ -131,29 +123,27 @@ void TileWidget::paintEvent(QPaintEvent *)
// XXX: Naive implementation: we repaint everything every time
QPainter painter(this);
//painter.setPen(Qt::NoPen);
const unsigned int xPos = 0;
const unsigned int yPos = 0;
// Set the brush color
// Set the square color
QColor color;
if (!m_tile.isEmpty())
{
if (m_isPreview)
painter.setBrush(PreviewColour);
color = PreviewColour;
else
painter.setBrush(TileColour);
color = TileColour;
}
else if (m_multiplier == WORD_TRIPLE)
painter.setBrush(W3Colour);
color = W3Colour;
else if (m_multiplier == WORD_DOUBLE)
painter.setBrush(W2Colour);
color = W2Colour;
else if (m_multiplier == LETTER_TRIPLE)
painter.setBrush(L3Colour);
color = L3Colour;
else if (m_multiplier == LETTER_DOUBLE)
painter.setBrush(L2Colour);
color = L2Colour;
else
painter.setBrush(EmptyColour);
painter.drawRect(xPos, yPos, squareSize, squareSize);
color = EmptyColour;
painter.fillRect(0, 0, squareSize, squareSize, color);
// Draw the letter
if (!m_tile.isEmpty())
@ -164,7 +154,7 @@ void TileWidget::paintEvent(QPaintEvent *)
if (m_isJoker)
painter.setPen(JokerColour);
painter.setFont(letterFont);
painter.drawText(xPos, yPos + 1, squareSize, squareSize,
painter.drawText(0, 0, squareSize, squareSize,
Qt::AlignCenter, qfw(chr));
painter.setPen(NormalColour);
@ -176,9 +166,9 @@ void TileWidget::paintEvent(QPaintEvent *)
if (showPoints && !m_isJoker)
{
painter.setFont(pointsFont);
painter.drawText(xPos + squareSize * (1 - pointsCoeff),
yPos + squareSize * (1 - pointsCoeff),
squareSize * pointsCoeff, squareSize * pointsCoeff + 3,
painter.drawText(0,
squareSize * (1 - pointsCoeff),
squareSize - 1, squareSize * pointsCoeff + 3,
Qt::AlignRight | Qt::AlignBottom,
QString("%1").arg(m_tile.getPoints()));
}
@ -186,14 +176,12 @@ void TileWidget::paintEvent(QPaintEvent *)
// Draw the arrow
if (m_showArrow)
{
const unsigned int xPos = 1;
const unsigned int yPos = 1;
painter.setPen(QPen(painter.brush().color(), 0));
painter.setBrush(ArrowColour);
const int mid = squareSize / 2;
const int fifth = squareSize / 5;
const int width = squareSize / 16;
painter.translate(xPos + mid, yPos + mid);
painter.translate(mid, mid);
if (m_horizontalArrow)
painter.rotate(90);
const QPoint points[] =