Cute Chess  0.1
graphicsboard.h
1 /*
2  This file is part of Cute Chess.
3  Copyright (C) 2008-2018 Cute Chess authors
4 
5  Cute Chess is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Cute Chess is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef GRAPHICSBOARD_H
20 #define GRAPHICSBOARD_H
21 
22 #include <QGraphicsItem>
23 #include <QColor>
24 #include <QVector>
25 #include <board/square.h>
26 #include <board/piece.h>
27 class GraphicsPiece;
28 class QPropertyAnimation;
29 
30 
39 {
40  public:
42  enum { Type = UserType + 1 };
43 
51  explicit GraphicsBoard(int files,
52  int ranks,
53  qreal squareSize,
54  QGraphicsItem* parent = nullptr);
56  virtual ~GraphicsBoard();
57 
58  // Inherited from QGraphicsItem
59  virtual int type() const;
60  virtual QRectF boundingRect() const;
61  virtual void paint(QPainter* painter,
62  const QStyleOptionGraphicsItem* option,
63  QWidget* widget = nullptr);
64 
71  Chess::Square squareAt(const QPointF& point) const;
78  QPointF squarePos(const Chess::Square& square) const;
85  Chess::Piece pieceTypeAt(const Chess::Square& square) const;
92  GraphicsPiece* pieceAt(const Chess::Square& square) const;
99  GraphicsPiece* takePieceAt(const Chess::Square& square);
100 
102  void clearSquares();
109  void setSquare(const Chess::Square& square, GraphicsPiece* piece);
115  void movePiece(const Chess::Square& source,
116  const Chess::Square& target);
117 
119  void clearHighlights();
126  void setHighlights(const QList<Chess::Square>& squares);
127 
132  bool isFlipped() const;
133 
135  void setFlipped(bool flipped);
136 
137  private:
138  int squareIndex(const Chess::Square& square) const;
139 
140  int m_files;
141  int m_ranks;
142  qreal m_squareSize;
143  qreal m_coordSize;
144  QRectF m_rect;
145  QColor m_lightColor;
146  QColor m_darkColor;
147  QVector<GraphicsPiece*> m_squares;
148  QPropertyAnimation* m_highlightAnim;
149  bool m_flipped;
150 };
151 
152 #endif // GRAPHICSBOARD_H
void setFlipped(bool flipped)
Definition: graphicsboard.cpp:313
Chess::Square squareAt(const QPointF &point) const
Definition: graphicsboard.cpp:152
GraphicsPiece * pieceAt(const Chess::Square &square) const
Definition: graphicsboard.cpp:195
void clearSquares()
Definition: graphicsboard.cpp:222
QPointF squarePos(const Chess::Square &square) const
Definition: graphicsboard.cpp:165
A graphical representation of a chess piece.
Definition: graphicspiece.h:37
A graphical chessboard.
Definition: graphicsboard.h:38
GraphicsPiece * takePieceAt(const Chess::Square &square)
Definition: graphicsboard.cpp:205
void clearHighlights()
Definition: graphicsboard.cpp:264
void setSquare(const Chess::Square &square, GraphicsPiece *piece)
Definition: graphicsboard.cpp:228
void movePiece(const Chess::Square &source, const Chess::Square &target)
Definition: graphicsboard.cpp:246
GraphicsBoard(int files, int ranks, qreal squareSize, QGraphicsItem *parent=nullptr)
Definition: graphicsboard.cpp:51
A chess piece.
Definition: piece.h:40
bool isFlipped() const
Definition: graphicsboard.cpp:308
void setHighlights(const QList< Chess::Square > &squares)
Definition: graphicsboard.cpp:274
A generic chess square type consisting of a file and a rank.
Definition: square.h:33
Chess::Piece pieceTypeAt(const Chess::Square &square) const
Definition: graphicsboard.cpp:187
virtual ~GraphicsBoard()
Definition: graphicsboard.cpp:75