Cute Chess  0.1
boardscene.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 BOARDSCENE_H
20 #define BOARDSCENE_H
21 
22 #include <QGraphicsScene>
23 #include <QMultiMap>
24 #include <QPointer>
25 #include <board/square.h>
26 #include <board/genericmove.h>
27 #include <board/boardtransition.h>
28 #include <board/result.h>
29 namespace Chess
30 {
31  class Board;
32  class Move;
33  class Side;
34  class Piece;
35 }
36 class ChessGame;
37 class QSvgRenderer;
38 class QAbstractAnimation;
39 class QPropertyAnimation;
40 class GraphicsBoard;
42 class GraphicsPiece;
43 class PieceChooser;
44 
45 
57 class BoardScene : public QGraphicsScene
58 {
59  Q_OBJECT
60 
61  public:
63  explicit BoardScene(QObject* parent = nullptr);
65  virtual ~BoardScene();
66 
68  Chess::Board* board() const;
75  void setBoard(Chess::Board* board);
76 
77  public slots:
86  void populate();
88  void setFenString(const QString& fenString);
90  void makeMove(const Chess::Move& move);
92  void makeMove(const Chess::GenericMove& move);
94  void undoMove();
96  void flip();
101  void onGameFinished(ChessGame* game, Chess::Result result);
102 
103  signals:
110  void humanMove(const Chess::GenericMove& move,
111  const Chess::Side& side);
112 
113  protected:
114  // Inherited from QGraphicsScene
115  virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
116  virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
117  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
118 
119  private slots:
120  void onTransitionFinished();
121  void onPromotionChosen(const Chess::Piece& promotion);
122 
123  private:
124  void cancelUserMove();
125 
126  enum MoveDirection
127  {
128  Forward,
129  Backward
130  };
131 
132  QPointF squarePos(const Chess::Square& square) const;
133  GraphicsPiece* pieceAt(const QPointF& pos) const;
134  GraphicsPiece* createPiece(const Chess::Piece& piece);
135  QPropertyAnimation* pieceAnimation(GraphicsPiece* piece,
136  const QPointF& endPoint) const;
137  void stopAnimation();
138  void tryMove(GraphicsPiece* piece, const QPointF& targetPos);
139  void selectPiece(const QList<Chess::Piece>& types,
140  const char* member);
141  void addMoveArrow(const QPointF& sourcePos,
142  const QPointF& targetPos);
143  void applyTransition(const Chess::BoardTransition& transition,
144  MoveDirection direction);
145  void updateMoves();
146 
147  Chess::Board* m_board;
148  MoveDirection m_direction;
149  Chess::BoardTransition m_transition;
151  QPointF m_sourcePos;
152  GraphicsBoard* m_squares;
153  GraphicsPieceReserve* m_reserve;
154  QPointer<PieceChooser> m_chooser;
155  QPointer<QAbstractAnimation> m_anim;
156  QSvgRenderer* m_renderer;
159  Chess::GenericMove m_promotionMove;
160  GraphicsPiece* m_highlightPiece;
161  QGraphicsItemGroup* m_moveArrows;
162 };
163 
164 #endif // BOARDSCENE_H
A light-weight QGraphicsObject dialog for selecting a chess piece.
Definition: piecechooser.h:35
A graphical reserve for captured chess pieces.
Definition: graphicspiecereserve.h:35
An internal chessboard class.
Definition: board.h:57
A graphical surface for displaying a chessgame.
Definition: boardscene.h:57
A graphical representation of a chess piece.
Definition: graphicspiece.h:37
A graphical chessboard.
Definition: graphicsboard.h:38
Definition: chessgame.h:38
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:34
Definition: boardscene.h:29
The side or color of a chess player.
Definition: side.h:35
A chess piece.
Definition: piece.h:40
The result of a chess game.
Definition: result.h:34
Details of a board transition caused by a move.
Definition: boardtransition.h:40
A small and efficient chessmove class.
Definition: move.h:42
A generic chess square type consisting of a file and a rank.
Definition: square.h:33