diff --git a/qt/bag_widget2.cpp b/qt/bag_widget2.cpp index 35b7638..0194119 100644 --- a/qt/bag_widget2.cpp +++ b/qt/bag_widget2.cpp @@ -56,10 +56,10 @@ void BagWidget2::setGame(const PublicGame *iGame) { for (unsigned i = 0; i < tile.maxNumber(); ++i) { - TileWidget *tileWidget = new TileWidget(this, TileWidget::NONE, 0, 0); - TileWidgetDecorator *decoWidget = new TileWidgetDecorator(this, *tileWidget); - decoWidget->tileChanged(tile, false, TileWidget::NORMAL); - layout->addWidget(decoWidget); + TileWidget *tileWidget = new TileWidget(NULL, TileWidget::NONE, 0, 0); + tileWidget->setBorder(); + tileWidget->tileChanged(tile, false, TileWidget::NORMAL); + layout->addWidget(tileWidget); } } } diff --git a/qt/board_widget.cpp b/qt/board_widget.cpp index 7dbde8e..d5409e7 100644 --- a/qt/board_widget.cpp +++ b/qt/board_widget.cpp @@ -50,19 +50,21 @@ BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent) TileLayout *layout = new TileLayout(BOARD_MAX + 1, BOARD_MAX + 1); layout->setSpacing(1); // Line full of coordinates - layout->addWidget(new BasicTileWidget(this, "")); + TileWidget *cornerTile = new TileWidget; + cornerTile->setCoordText(""); + layout->addWidget(cornerTile); for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col) { - BasicTileWidget *coordTile = - new BasicTileWidget(this, QString("%1").arg(col)); + TileWidget *coordTile = new TileWidget; + coordTile->setCoordText(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))); + TileWidget *coordTile = new TileWidget; + coordTile->setCoordText(QString(QChar('A' + row - BOARD_MIN))); layout->addWidget(coordTile); // Add the squares for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col) diff --git a/qt/tile_widget.cpp b/qt/tile_widget.cpp index d111e4f..e14bcc1 100644 --- a/qt/tile_widget.cpp +++ b/qt/tile_widget.cpp @@ -1,6 +1,6 @@ /***************************************************************************** * Eliot - * Copyright (C) 2010 Olivier Teulière + * Copyright (C) 2010-2012 Olivier Teulière * Authors: Olivier Teulière * * This program is free software; you can redistribute it and/or modify @@ -30,9 +30,7 @@ using namespace std; -INIT_LOGGER(qt, BasicTileWidget); INIT_LOGGER(qt, TileWidget); -INIT_LOGGER(qt, TileWidgetDecorator); const QColor TileWidget::EmptyColour(Qt::white); @@ -48,53 +46,63 @@ 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) -{ - setMinimumSize(10, 10); -} - - -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(1, 1, squareSize, squareSize, Qt::AlignCenter, m_text); -} - -// -------------- - TileWidget::TileWidget(QWidget *parent, Multiplier multiplier, int row, int col) - : BasicTileWidget(parent), m_multiplier(multiplier), + : QFrame(parent), m_multiplier(multiplier), m_row(row), m_col(col), m_isJoker(false), m_state(NORMAL), m_showArrow(false), m_horizontalArrow(true) { QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding); policy.setHeightForWidth(true); setSizePolicy(policy); + + setMidLineWidth(0); + setBorder(0); +} + + +void TileWidget::setCoordText(QString iText) +{ + m_state = COORDS; + m_text = iText; + setBorder(0); +} + + +void TileWidget::setBorder(int width) +{ + if (frameWidth() == width) + return; + + if (width <= 0) + { + setFrameStyle(QFrame::NoFrame | QFrame::Plain); + } + else + { + setFrameStyle(QFrame::Box | QFrame::Plain); + setLineWidth(width); + } + setMinimumSize(QSize(15, 15) + 2 * QSize(width, width)); +} + + +int TileWidget::heightForWidth(int width) const +{ + return width; +} + + +QSize TileWidget::sizeHint() const +{ + const int width = frameWidth(); + return QSize(30, 30) + 2 * QSize(width, width); +} + + +int TileWidget::getSquareSize() const +{ + return std::min(contentsRect().width(), contentsRect().height()); } @@ -115,20 +123,32 @@ void TileWidget::arrowChanged(bool showArrow, bool horizontalArrow) } -void TileWidget::paintEvent(QPaintEvent *) +void TileWidget::paintEvent(QPaintEvent *iEvent) { + QFrame::paintEvent(iEvent); + const int squareSize = getSquareSize(); + QPainter painter(this); + painter.translate(QPoint(contentsMargins().left(), contentsMargins().top())); + // The font must grow with the square size QFont letterFont = font(); letterFont.setPixelSize(squareSize * 2 / 3); + if (m_state == COORDS) + { + painter.setFont(letterFont); + painter.drawText(1, 1, squareSize, squareSize, Qt::AlignCenter, m_text); + + return; + } + QFont pointsFont = font(); const double pointsCoeff = 8. / 25.; pointsFont.setPixelSize(squareSize * pointsCoeff); // XXX: Naive implementation: we repaint everything every time - QPainter painter(this); // Set the square color QColor color; @@ -223,35 +243,3 @@ void TileWidget::mousePressEvent(QMouseEvent *iEvent) emit mousePressed(m_row, m_col, iEvent); } -// -------------- - -TileWidgetDecorator::TileWidgetDecorator(QWidget *parent, TileWidget &wrapped) - : TileWidget(parent), m_wrapped(wrapped) -{ - QVBoxLayout *layout = new QVBoxLayout; - layout->setContentsMargins(1, 1, 1, 1); - layout->addWidget(&m_wrapped); - setLayout(layout); - - QObject::connect(&m_wrapped, SIGNAL(mousePressed(int, int, QMouseEvent*)), - this, SIGNAL(mousePressed(int, int, QMouseEvent*))); -} - - -void TileWidgetDecorator::tileChanged(const Tile &iTile, bool isJoker, State state) -{ - m_wrapped.tileChanged(iTile, isJoker, state); -} - - -void TileWidgetDecorator::arrowChanged(bool showArrow, bool horizontalArrow) -{ - m_wrapped.arrowChanged(showArrow, horizontalArrow); -} - -void TileWidgetDecorator::paintEvent(QPaintEvent *) -{ - QPainter painter(this); - painter.drawRect(0, 0, width() - 1, height() - 1); -} - diff --git a/qt/tile_widget.h b/qt/tile_widget.h index c8534e7..69a4bcd 100644 --- a/qt/tile_widget.h +++ b/qt/tile_widget.h @@ -1,6 +1,6 @@ /***************************************************************************** * Eliot - * Copyright (C) 2010 Olivier Teulière + * Copyright (C) 2010-2012 Olivier Teulière * Authors: Olivier Teulière * * This program is free software; you can redistribute it and/or modify @@ -21,40 +21,17 @@ #ifndef TILE_WIDGET_H_ #define TILE_WIDGET_H_ -#include +#include #include "tile.h" #include "logging.h" -/** - * Simplified tile, only used to draw the coordinates of the board - */ -class BasicTileWidget: public QWidget -{ - DEFINE_LOGGER(); - -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. + * It can also be used to display coordinates, if the setCoordText() method + * has been called. */ -class TileWidget: public BasicTileWidget +class TileWidget: public QFrame { Q_OBJECT; DEFINE_LOGGER(); @@ -71,6 +48,7 @@ public: enum State { + COORDS, NORMAL, PREVIEW, PLAYED, @@ -80,6 +58,17 @@ public: explicit TileWidget(QWidget *parent = 0, Multiplier multiplier = NONE, int row = 0, int col = 0); + void setCoordText(QString iText); + + void setBorder(int width = 2); + + int getSquareSize() const; + + virtual int heightForWidth(int w) const; + + /// Define a default size + virtual QSize sizeHint() const; + public slots: virtual void tileChanged(const Tile &iTile, bool isJoker, State state); virtual void arrowChanged(bool showArrow, bool horizontalArrow); @@ -117,6 +106,9 @@ private: /// Whether the arrow is horizontal bool m_horizontalArrow; + /// Text used for coordinates + QString m_text; + /// Define a few background colours //@{ static const QColor EmptyColour; @@ -133,28 +125,5 @@ private: //@} }; -class TileWidgetDecorator : public TileWidget -{ - Q_OBJECT; - DEFINE_LOGGER(); - -public: - TileWidgetDecorator(QWidget *parent, TileWidget &wrapped); - -public slots: - virtual void tileChanged(const Tile &iTile, bool isJoker, State state); - virtual void arrowChanged(bool showArrow, bool horizontalArrow); - -signals: - void mousePressed(int row, int col, QMouseEvent *iEvent); - -protected: - /// Paint the square - virtual void paintEvent(QPaintEvent *iEvent); - -private: - TileWidget & m_wrapped; -}; - #endif