Cute Chess  0.1
graphicsboard.h
1 /*
2  This file is part of Cute Chess.
3 
4  Cute Chess is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  Cute Chess is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef GRAPHICSBOARD_H
19 #define GRAPHICSBOARD_H
20 
21 #include <QGraphicsItem>
22 #include <QColor>
23 #include <QVector>
24 #include <board/square.h>
25 #include <board/piece.h>
26 class GraphicsPiece;
27 class QPropertyAnimation;
28 
29 
38 {
39  public:
41  enum { Type = UserType + 1 };
42 
50  explicit GraphicsBoard(int files,
51  int ranks,
52  qreal squareSize,
53  QGraphicsItem* parent = nullptr);
55  virtual ~GraphicsBoard();
56 
57  // Inherited from QGraphicsItem
58  virtual int type() const;
59  virtual QRectF boundingRect() const;
60  virtual void paint(QPainter* painter,
61  const QStyleOptionGraphicsItem* option,
62  QWidget* widget = nullptr);
63 
70  Chess::Square squareAt(const QPointF& point) const;
77  QPointF squarePos(const Chess::Square& square) const;
84  Chess::Piece pieceTypeAt(const Chess::Square& square) const;
91  GraphicsPiece* pieceAt(const Chess::Square& square) const;
98  GraphicsPiece* takePieceAt(const Chess::Square& square);
99 
101  void clearSquares();
108  void setSquare(const Chess::Square& square, GraphicsPiece* piece);
114  void movePiece(const Chess::Square& source,
115  const Chess::Square& target);
116 
118  void clearHighlights();
125  void setHighlights(const QList<Chess::Square>& squares);
126 
131  bool isFlipped() const;
132 
134  void setFlipped(bool flipped);
135 
136  private:
137  int squareIndex(const Chess::Square& square) const;
138 
139  int m_files;
140  int m_ranks;
141  qreal m_squareSize;
142  QRectF m_rect;
143  QColor m_lightColor;
144  QColor m_darkColor;
145  QVector<GraphicsPiece*> m_squares;
146  QPropertyAnimation* m_highlightAnim;
147  bool m_flipped;
148 };
149 
150 #endif // GRAPHICSBOARD_H
void setFlipped(bool flipped)
Definition: graphicsboard.cpp:274
Chess::Piece pieceTypeAt(const Chess::Square &square) const
Definition: graphicsboard.cpp:148
void clearSquares()
Definition: graphicsboard.cpp:183
A graphical representation of a chess piece.
Definition: graphicspiece.h:36
A graphical chessboard.
Definition: graphicsboard.h:37
bool isFlipped() const
Definition: graphicsboard.cpp:269
GraphicsPiece * takePieceAt(const Chess::Square &square)
Definition: graphicsboard.cpp:166
void clearHighlights()
Definition: graphicsboard.cpp:225
void setSquare(const Chess::Square &square, GraphicsPiece *piece)
Definition: graphicsboard.cpp:189
void movePiece(const Chess::Square &source, const Chess::Square &target)
Definition: graphicsboard.cpp:207
GraphicsBoard(int files, int ranks, qreal squareSize, QGraphicsItem *parent=nullptr)
Definition: graphicsboard.cpp:50
A chess piece.
Definition: piece.h:39
Chess::Square squareAt(const QPointF &point) const
Definition: graphicsboard.cpp:113
QPointF squarePos(const Chess::Square &square) const
Definition: graphicsboard.cpp:126
GraphicsPiece * pieceAt(const Chess::Square &square) const
Definition: graphicsboard.cpp:156
void setHighlights(const QList< Chess::Square > &squares)
Definition: graphicsboard.cpp:235
A generic chess square type consisting of a file and a rank.
Definition: square.h:32
virtual ~GraphicsBoard()
Definition: graphicsboard.cpp:73