From 90027c32107a44f5d0d7995181d8ef876a3cd55b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 17 Oct 2010 21:45:10 +0000 Subject: [PATCH] Display the coordinates of the board, and draw a square around it. It now looks exactly like the old board (with 1 pixel difference for the coordinates, making it better). --- qt/board_widget.cpp | 45 ++++++++++++++++++--------------------- qt/tile_widget.cpp | 52 +++++++++++++++++++++++++++++++++------------ qt/tile_widget.h | 34 +++++++++++++++++++++++------ 3 files changed, 87 insertions(+), 44 deletions(-) diff --git a/qt/board_widget.cpp b/qt/board_widget.cpp index 1d2f326..d697dda 100644 --- a/qt/board_widget.cpp +++ b/qt/board_widget.cpp @@ -55,9 +55,11 @@ public: delete item; } - QRect getBoardRect() + QRect getBoardRect() const { - + if (m_items.size() < m_nbCols + 2) + return QRect(); + return m_items.at(m_nbCols + 1)->geometry().united(m_items.back()->geometry()); } virtual void addItem(QLayoutItem *item) @@ -129,9 +131,23 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent) setForegroundRole(QPalette::Window); setBackgroundRole(QPalette::Window); - BoardLayout *layout = new BoardLayout(15); + BoardLayout *layout = new BoardLayout(16); + // Line full of coordinates + layout->addWidget(new BasicTileWidget(this, "")); + for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col) + { + BasicTileWidget *coordTile = + new BasicTileWidget(this, QString("%1").arg(col)); + layout->addWidget(coordTile); + } + // Rest of the board for (unsigned int row = BOARD_MIN; row <= BOARD_MAX; ++row) { + // Add the coordinate + BasicTileWidget *coordTile = + new BasicTileWidget(this, QString(QChar('A' + row - BOARD_MIN))); + layout->addWidget(coordTile); + // Add the squares for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col) { TileWidget::Multiplier mult = TileWidget::NONE; @@ -191,32 +207,13 @@ QSize BoardWidget::sizeHint() const void BoardWidget::paintEvent(QPaintEvent *) { QPainter painter(this); + QRect rect = ((BoardLayout*)layout())->getBoardRect(); + painter.drawRect(rect); #if 0 const int size = std::min(width(), height()); const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 2))); - // The font must grow with the square size - QFont letterFont = font(); - letterFont.setPixelSize(squareSize * 2 / 3); - - QFont pointsFont = font(); - const double pointsCoeff = 8. / 25.; - pointsFont.setPixelSize(squareSize * pointsCoeff); - - // Draw the coordinates - painter.setFont(letterFont); - for (unsigned x = 1; x <= BOARD_MAX - BOARD_MIN + 1; ++x) - { - painter.drawText(x * squareSize, 1, - squareSize, squareSize, - Qt::AlignCenter, - QString::number(x)); - painter.drawText(0, x * squareSize, - squareSize, squareSize, - Qt::AlignCenter, - QString(1, 'A' + x - 1)); - } // Draw the arrow const Coord &markCoord = m_coordModel.getCoord(); if (m_game != NULL && markCoord.isValid()) diff --git a/qt/tile_widget.cpp b/qt/tile_widget.cpp index 3be4ec0..2856cc6 100644 --- a/qt/tile_widget.cpp +++ b/qt/tile_widget.cpp @@ -43,8 +43,44 @@ const QColor TileWidget::JokerColour(255, 0, 0); const QColor TileWidget::ArrowColour(10, 10, 10); +BasicTileWidget::BasicTileWidget(QWidget *parent, QString text) + : QWidget(parent), m_text(text) +{ +} + + +int BasicTileWidget::heightForWidth(int width) const +{ + return width; +} + + +QSize BasicTileWidget::sizeHint() const +{ + return QSize(30, 30); +} + + +int BasicTileWidget::getSquareSize() const +{ + return std::min(width(), height()); +} + + +void BasicTileWidget::paintEvent(QPaintEvent *) +{ + const int squareSize = getSquareSize(); + QFont letterFont = font(); + letterFont.setPixelSize(squareSize * 2 / 3); + + QPainter painter(this); + painter.setFont(letterFont); + painter.drawText(0, 1, squareSize, squareSize, Qt::AlignCenter, m_text); +} + + TileWidget::TileWidget(QWidget *parent, Multiplier multiplier) - : QWidget(parent), m_multiplier(multiplier), m_isJoker(false), + : BasicTileWidget(parent), m_multiplier(multiplier), m_isJoker(false), m_isPreview(false), m_showArrow(false), m_horizontalArrow(true) { setMinimumSize(15, 15); @@ -62,18 +98,6 @@ TileWidget::TileWidget(QWidget *parent, Multiplier multiplier) } -int TileWidget::heightForWidth(int width) const -{ - return width; -} - - -QSize TileWidget::sizeHint() const -{ - return QSize(30, 30); -} - - void TileWidget::tileChanged(const Tile &iTile, bool isJoker, bool isPreview, bool showArrow, bool horizontalArrow) { @@ -88,7 +112,7 @@ void TileWidget::tileChanged(const Tile &iTile, bool isJoker, bool isPreview, void TileWidget::paintEvent(QPaintEvent *) { - const int squareSize = std::min(width(), height()); + const int squareSize = getSquareSize(); // The font must grow with the square size QFont letterFont = font(); diff --git a/qt/tile_widget.h b/qt/tile_widget.h index c7ce95d..b097929 100644 --- a/qt/tile_widget.h +++ b/qt/tile_widget.h @@ -25,7 +25,33 @@ #include "tile.h" -class TileWidget: public QWidget +/** + * Simplified tile, only used to draw the coordinates of the board + */ +class BasicTileWidget: public QWidget +{ +public: + BasicTileWidget(QWidget *parent = 0, QString text = ""); + + int getSquareSize() const; + + virtual int heightForWidth(int w) const; + +protected: + /// Define a default size + virtual QSize sizeHint() const; + /// Paint the square + virtual void paintEvent(QPaintEvent *iEvent); + +private: + QString m_text; +}; + + +/** + * Widget used to display a square on the board, with or without letter. + */ +class TileWidget: public BasicTileWidget { Q_OBJECT; @@ -41,16 +67,12 @@ public: explicit TileWidget(QWidget *parent = 0, Multiplier multiplier = NONE); - virtual int heightForWidth(int w) const; - public slots: void tileChanged(const Tile &iTile, bool isJoker, bool isPreview, bool showArrow, bool horizontalArrow); protected: - /// Define a default size - virtual QSize sizeHint() const; - /// Paint the board + /// Paint the square virtual void paintEvent(QPaintEvent *iEvent); private: