eliot-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Eliot-dev] eliot/qt Makefile.am board_widget.cpp board_wid...


From: Olivier Teulière
Subject: [Eliot-dev] eliot/qt Makefile.am board_widget.cpp board_wid...
Date: Sun, 17 Oct 2010 21:28:01 +0000

CVSROOT:        /cvsroot/eliot
Module name:    eliot
Changes by:     Olivier Teulière <ipkiss>       10/10/17 21:28:01

Modified files:
        qt             : Makefile.am board_widget.cpp board_widget.h 
Added files:
        qt             : tile_widget.cpp tile_widget.h 

Log message:
        New TileWidget class, to handle a single square

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/Makefile.am?cvsroot=eliot&r1=1.20&r2=1.21
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/board_widget.cpp?cvsroot=eliot&r1=1.16&r2=1.17
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/board_widget.h?cvsroot=eliot&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/tile_widget.cpp?cvsroot=eliot&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/eliot/qt/tile_widget.h?cvsroot=eliot&rev=1.1

Patches:
Index: Makefile.am
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/Makefile.am,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- Makefile.am 17 Oct 2010 21:23:40 -0000      1.20
+++ Makefile.am 17 Oct 2010 21:28:01 -0000      1.21
@@ -62,6 +62,7 @@
     new_game.cpp new_game.h \
     score_widget.cpp score_widget.h \
     dic_wizard.cpp dic_wizard.h \
+    tile_widget.cpp tile_widget.h \
     board_widget.cpp board_widget.h \
     history_widget.cpp history_widget.h \
     play_word_mediator.cpp play_word_mediator.h \
@@ -89,6 +90,7 @@
     bag_widget.moc.cpp \
     score_widget.moc.cpp \
     dic_wizard.moc.cpp \
+    tile_widget.moc.cpp \
     board_widget.moc.cpp \
     history_widget.moc.cpp \
     play_word_mediator.moc.cpp \

Index: board_widget.cpp
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/board_widget.cpp,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- board_widget.cpp    6 Mar 2010 16:54:20 -0000       1.16
+++ board_widget.cpp    17 Oct 2010 21:28:01 -0000      1.17
@@ -1,6 +1,6 @@
 /*****************************************************************************
  * Eliot
- * Copyright (C) 2008-2009 Olivier Teulière
+ * Copyright (C) 2008-2010 Olivier Teulière
  * Authors: Olivier Teulière <ipkiss @@ gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -20,13 +20,16 @@
 
 #include <algorithm> // For std::transform
 #include <cmath>
-#include <QtGui/QPainter>
-#include <QtGui/QPaintEvent>
+//#include <QtGui/QPainter>
+#include <QtGui/QGridLayout>
 #include <QtGui/QMouseEvent>
-#include <QtCore/QSettings>
+// XXX
+#include <QtGui/QTreeView>
+#include <QtGui/QPainter>
+#include <iostream>
 
 #include "board_widget.h"
-#include "prefs_dialog.h"
+#include "tile_widget.h"
 #include "qtcommon.h"
 #include "public_game.h"
 #include "tile.h"
@@ -36,25 +39,120 @@
 using namespace std;
 
 
-const QColor BoardWidget::EmptyColour(Qt::white);
-const QColor BoardWidget::L2Colour(34, 189, 240);
-const QColor BoardWidget::L3Colour(29, 104, 240);
-const QColor BoardWidget::W2Colour(255, 147, 196);
-const QColor BoardWidget::W3Colour(240, 80, 94);
-const QColor BoardWidget::TileColour(255, 235, 205);
-const QColor BoardWidget::PreviewColour(183, 183, 123);
-const QColor BoardWidget::NormalColour(0, 0, 0);
-const QColor BoardWidget::JokerColour(255, 0, 0);
-const QColor BoardWidget::ArrowColour(10, 10, 10);
+class BoardLayout : public QLayout
+{
+    //Q_OBJECT
+
+public:
+    BoardLayout(int nbCols): m_nbCols(nbCols), m_space(0)
+    {
+        setContentsMargins(0, 0, 0, 0);
+    }
+    ~BoardLayout()
+    {
+        QLayoutItem *item;
+        while ((item = takeAt(0)))
+            delete item;
+    }
+
+    QRect getBoardRect()
+    {
+
+    }
+
+    virtual void addItem(QLayoutItem *item)
+    {
+        m_items.append(item);
+    }
+    virtual bool hasHeightForWidth() const { return true; }
+    virtual int heightForWidth(int width) const { return width; }
+    virtual int count() const { return m_items.size(); }
+    virtual QLayoutItem *itemAt(int index) const { return 
m_items.value(index); }
+    virtual QLayoutItem *takeAt(int index)
+    {
+        if (index >= 0 && index < m_items.size())
+            return m_items.takeAt(index);
+        else
+            return 0;
+    }
+    virtual QSize minimumSize() const
+    {
+        QSize size;
+        if (!m_items.empty())
+            size.expandedTo(m_items.at(0)->minimumSize());
+        return size * m_nbCols;
+    }
+    virtual void setGeometry(const QRect &rect)
+    {
+        QLayout::setGeometry(rect);
+        doLayout(rect);
+    }
+    virtual QSize sizeHint() const { return minimumSize(); }
+
+private:
+    QList<QLayoutItem *> m_items;
+    int m_nbCols;
+    int m_space;
 
+    void doLayout(const QRect &rect)
+    {
+        int size = std::min(rect.width(), rect.height());
+        int squareSize = size / m_nbCols - m_space;
+        QLayoutItem *item;
+        int x = 0;
+        int y = 0;
+        int nbInRow = 1;
+        foreach (item, m_items)
+        {
+            QRect itemRect(QPoint(x, y), QSize(squareSize, squareSize));
+            item->setGeometry(itemRect);
+            x += squareSize + m_space;
+            ++nbInRow;
+            if (nbInRow > m_nbCols)
+            {
+                x = 0;
+                y += squareSize + m_space;
+                nbInRow = 1;
+            }
+        }
+    }
+};
 
 BoardWidget::BoardWidget(CoordModel &iCoordModel, QWidget *parent)
     : QFrame(parent), m_game(NULL), m_coordModel(iCoordModel)
 {
+    // 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);
+
+    BoardLayout *layout = new BoardLayout(15);
+    for (unsigned int row = BOARD_MIN; row <= BOARD_MAX; ++row)
+    {
+        for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
+        {
+            TileWidget::Multiplier mult = TileWidget::NONE;
+            if (Board::GetWordMultiplier(row, col) == 3)
+                mult = TileWidget::WORD_TRIPLE;
+            else if (Board::GetWordMultiplier(row, col) == 2)
+                mult = TileWidget::WORD_DOUBLE;
+            else if (Board::GetLetterMultiplier(row, col) == 3)
+                mult = TileWidget::LETTER_TRIPLE;
+            else if (Board::GetLetterMultiplier(row, col) == 2)
+                mult = TileWidget::LETTER_DOUBLE;
+            TileWidget *t = new TileWidget(this, mult);
+            layout->addWidget(t);
+        }
+    }
+
+    setLayout(layout);
+
     setFrameStyle(QFrame::Panel);
     // Use as much space as possible
-    QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-    setSizePolicy(policy);
+    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
     setMinimumSize(200, 200);
 
     // Listen to changes in the coordinates
@@ -89,8 +187,12 @@
     return QSize(400, 400);
 }
 
+
 void BoardWidget::paintEvent(QPaintEvent *)
 {
+    QPainter painter(this);
+
+#if 0
     const int size = std::min(width(), height());
     const int squareSize = lrint(floor((size - 1) / (BOARD_MAX - BOARD_MIN + 
2)));
 
@@ -102,66 +204,6 @@
     const double pointsCoeff = 8. / 25.;
     pointsFont.setPixelSize(squareSize * pointsCoeff);
 
-    // Should we display the tiles points?
-    QSettings qs(ORGANIZATION, PACKAGE_NAME);
-    bool showPoints = qs.value(PrefsDialog::kINTF_SHOW_TILES_POINTS, 
true).toBool();
-
-    // XXX: Naive implementation: we repaint everything every time
-    QPainter painter(this);
-    //painter.setPen(Qt::NoPen);
-    for (unsigned int row = BOARD_MIN; row <= BOARD_MAX; ++row)
-    {
-        for (unsigned int col = BOARD_MIN; col <= BOARD_MAX; ++col)
-        {
-            const unsigned int xPos = (col - BOARD_MIN + 1) * squareSize;
-            const unsigned int yPos = (row - BOARD_MIN + 1) * squareSize;
-
-            // Set the brush color
-            if (m_game != NULL && !m_game->getBoard().getTile(row, 
col).isEmpty())
-            {
-                if (m_game->getBoard().isTestChar(row, col))
-                    painter.setBrush(PreviewColour);
-                else
-                    painter.setBrush(TileColour);
-            }
-            else if (Board::GetWordMultiplier(row, col) == 3)
-                painter.setBrush(W3Colour);
-            else if (Board::GetWordMultiplier(row, col) == 2)
-                painter.setBrush(W2Colour);
-            else if (Board::GetLetterMultiplier(row, col) == 3)
-                painter.setBrush(L3Colour);
-            else if (Board::GetLetterMultiplier(row, col) == 2)
-                painter.setBrush(L2Colour);
-            else
-                painter.setBrush(EmptyColour);
-            painter.drawRect(xPos, yPos, squareSize, squareSize);
-
-            // Draw the letter
-            if (m_game != NULL && !m_game->getBoard().getTile(row, 
col).isEmpty())
-            {
-                wstring chr = m_game->getBoard().getTile(row, 
col).getDisplayStr();
-                // Make the display char in upper case
-                std::transform(chr.begin(), chr.end(), chr.begin(), towupper);
-                if (m_game->getBoard().isJoker(row, col))
-                    painter.setPen(JokerColour);
-                painter.setFont(letterFont);
-                painter.drawText(xPos, yPos + 1, squareSize, squareSize,
-                                 Qt::AlignCenter, qfw(chr));
-                painter.setPen(NormalColour);
-
-                // Draw the points of the tile
-                if (showPoints && !m_game->getBoard().isJoker(row, col))
-                {
-                    painter.setFont(pointsFont);
-                    painter.drawText(xPos + squareSize * (1 - pointsCoeff),
-                                     yPos + squareSize * (1 - pointsCoeff) + 1,
-                                     squareSize * pointsCoeff, squareSize * 
pointsCoeff + 3,
-                                     Qt::AlignRight | Qt::AlignBottom,
-                                     
QString("%1").arg(m_game->getBoard().getTile(row, col).getPoints()));
-                }
-            }
-        }
-    }
     // Draw the coordinates
     painter.setFont(letterFont);
     for (unsigned x = 1; x <= BOARD_MAX - BOARD_MIN + 1; ++x)
@@ -204,9 +246,11 @@
         painter.setPen(QPen());
         painter.setBrush(NormalColour);
     }
+#endif
 }
 
 
+/*
 void BoardWidget::mousePressEvent(QMouseEvent *iEvent)
 {
     if (m_game == NULL)
@@ -305,4 +349,5 @@
         m_coordModel.clear();
 #endif
 }
+*/
 

Index: board_widget.h
===================================================================
RCS file: /cvsroot/eliot/eliot/qt/board_widget.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- board_widget.h      19 Feb 2009 18:25:18 -0000      1.6
+++ board_widget.h      17 Oct 2010 21:28:01 -0000      1.7
@@ -1,6 +1,6 @@
 /*****************************************************************************
  * Eliot
- * Copyright (C) 2008 Olivier Teulière
+ * Copyright (C) 2008-2010 Olivier Teulière
  * Authors: Olivier Teulière <ipkiss @@ gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
@@ -46,7 +46,7 @@
     /// Paint the board
     virtual void paintEvent(QPaintEvent *iEvent);
     /// Catch mouse clicks on the board
-    virtual void mousePressEvent(QMouseEvent *iEvent);
+    //virtual void mousePressEvent(QMouseEvent *iEvent);
 
 private slots:
     void updateArrow(const Coord &iCoord);
@@ -57,20 +57,6 @@
 
     /// Coordinates of the next word to play
     CoordModel &m_coordModel;
-
-    /// Define a few background colours
-    //@{
-    static const QColor EmptyColour;
-    static const QColor L2Colour;
-    static const QColor L3Colour;
-    static const QColor W2Colour;
-    static const QColor W3Colour;
-    static const QColor TileColour;
-    static const QColor PreviewColour;
-    static const QColor NormalColour;
-    static const QColor JokerColour;
-    static const QColor ArrowColour;
-    //@}
 };
 
 #endif

Index: tile_widget.cpp
===================================================================
RCS file: tile_widget.cpp
diff -N tile_widget.cpp
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tile_widget.cpp     17 Oct 2010 21:28:01 -0000      1.1
@@ -0,0 +1,184 @@
+/*****************************************************************************
+ * Eliot
+ * Copyright (C) 2010 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#include <algorithm> // For std::transform
+#include <QtGui/QPainter>
+#include <QtGui/QPaintEvent>
+#include <QtCore/QSettings>
+
+#include "tile_widget.h"
+#include "prefs_dialog.h"
+#include "qtcommon.h"
+#include "tile.h"
+
+using namespace std;
+
+
+const QColor TileWidget::EmptyColour(Qt::white);
+const QColor TileWidget::L2Colour(34, 189, 240);
+const QColor TileWidget::L3Colour(29, 104, 240);
+const QColor TileWidget::W2Colour(255, 147, 196);
+const QColor TileWidget::W3Colour(240, 80, 94);
+const QColor TileWidget::TileColour(255, 235, 205);
+const QColor TileWidget::PreviewColour(183, 183, 123);
+const QColor TileWidget::NormalColour(0, 0, 0);
+const QColor TileWidget::JokerColour(255, 0, 0);
+const QColor TileWidget::ArrowColour(10, 10, 10);
+
+
+TileWidget::TileWidget(QWidget *parent, Multiplier multiplier)
+    : QWidget(parent), m_multiplier(multiplier), 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);
+}
+
+
+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)
+{
+    m_tile = iTile;
+    m_isJoker = isJoker;
+    m_isPreview = isPreview;
+    m_showArrow = showArrow;
+    m_horizontalArrow = horizontalArrow;
+    update();
+}
+
+
+void TileWidget::paintEvent(QPaintEvent *)
+{
+    const int squareSize = std::min(width(), height());
+
+    // 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);
+
+    // 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
+    if (!m_tile.isEmpty())
+    {
+        if (m_isPreview)
+            painter.setBrush(PreviewColour);
+        else
+            painter.setBrush(TileColour);
+    }
+    else if (m_multiplier == WORD_TRIPLE)
+        painter.setBrush(W3Colour);
+    else if (m_multiplier == WORD_DOUBLE)
+        painter.setBrush(W2Colour);
+    else if (m_multiplier == LETTER_TRIPLE)
+        painter.setBrush(L3Colour);
+    else if (m_multiplier == LETTER_DOUBLE)
+        painter.setBrush(L2Colour);
+    else
+        painter.setBrush(EmptyColour);
+    painter.drawRect(xPos, yPos, squareSize, squareSize);
+
+    // Draw the letter
+    if (!m_tile.isEmpty())
+    {
+        wstring chr = m_tile.getDisplayStr();
+        // Make the display char in upper case
+        std::transform(chr.begin(), chr.end(), chr.begin(), towupper);
+        if (m_isJoker)
+            painter.setPen(JokerColour);
+        painter.setFont(letterFont);
+        painter.drawText(xPos, yPos + 1, squareSize, squareSize,
+                         Qt::AlignCenter, qfw(chr));
+        painter.setPen(NormalColour);
+
+        // Should we display the tiles points?
+        QSettings qs(ORGANIZATION, PACKAGE_NAME);
+        const bool showPoints = qs.value(PrefsDialog::kINTF_SHOW_TILES_POINTS, 
true).toBool();
+
+        // Draw the points of the tile
+        if (showPoints && !m_isJoker)
+        {
+            painter.setFont(pointsFont);
+            painter.drawText(xPos + squareSize * (1 - pointsCoeff),
+                             yPos + squareSize * (1 - pointsCoeff) + 1,
+                             squareSize * pointsCoeff, squareSize * 
pointsCoeff + 3,
+                             Qt::AlignRight | Qt::AlignBottom,
+                             QString("%1").arg(m_tile.getPoints()));
+        }
+    }
+    // Draw the arrow
+    if (m_showArrow)
+    {
+        const unsigned int xPos = 1;
+        const unsigned int yPos = 1;
+        painter.setPen(QPen(ArrowColour, 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);
+        if (m_horizontalArrow)
+            painter.rotate(90);
+        const QPoint points[] =
+        {
+            QPoint(-mid + fifth, -width),
+            QPoint(-mid + 3*fifth, -width),
+            QPoint(-mid + 3*fifth, -fifth),
+            QPoint(-mid + 4*fifth, 0),
+            QPoint(-mid + 3*fifth, fifth),
+            QPoint(-mid + 3*fifth, width),
+            QPoint(-mid + fifth, width)
+        };
+        painter.drawPolygon(points, 7);
+
+        painter.setPen(QPen());
+        painter.setBrush(NormalColour);
+    }
+}
+

Index: tile_widget.h
===================================================================
RCS file: tile_widget.h
diff -N tile_widget.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ tile_widget.h       17 Oct 2010 21:28:01 -0000      1.1
@@ -0,0 +1,91 @@
+/*****************************************************************************
+ * Eliot
+ * Copyright (C) 2010 Olivier Teulière
+ * Authors: Olivier Teulière <ipkiss @@ gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *****************************************************************************/
+
+#ifndef TILE_WIDGET_H_
+#define TILE_WIDGET_H_
+
+#include <QtGui/QWidget>
+#include "tile.h"
+
+
+class TileWidget: public QWidget
+{
+    Q_OBJECT;
+
+public:
+    enum Multiplier
+    {
+        NONE = 0,
+        LETTER_DOUBLE = 1,
+        LETTER_TRIPLE = 2,
+        WORD_DOUBLE = 3,
+        WORD_TRIPLE = 4
+    };
+
+    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
+    virtual void paintEvent(QPaintEvent *iEvent);
+
+private:
+    /// Encapsulated tile
+    Tile m_tile;
+
+    /// Word or letter multipler
+    Multiplier m_multiplier;
+
+    /// Whether the tile is a joker
+    bool m_isJoker;
+
+    /// Whether the tile is used as a preview
+    bool m_isPreview;
+
+    /// Whether we should show the arrow
+    bool m_showArrow;
+
+    /// Whether the arrow is horizontal
+    bool m_horizontalArrow;
+
+    /// Define a few background colours
+    //@{
+    static const QColor EmptyColour;
+    static const QColor L2Colour;
+    static const QColor L3Colour;
+    static const QColor W2Colour;
+    static const QColor W3Colour;
+    static const QColor TileColour;
+    static const QColor PreviewColour;
+    static const QColor NormalColour;
+    static const QColor JokerColour;
+    static const QColor ArrowColour;
+    //@}
+};
+
+#endif
+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]