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 <QSettings>
26 #include <board/square.h>
27 #include <board/genericmove.h>
28 #include <board/boardtransition.h>
29 #include <board/result.h>
30 namespace Chess
31 {
32  class Board;
33  class Move;
34  class Side;
35  class Piece;
36 }
37 class ChessGame;
38 class QSvgRenderer;
39 class QAbstractAnimation;
40 class QPropertyAnimation;
41 class GraphicsBoard;
43 class GraphicsPiece;
44 class PieceChooser;
45 
46 
58 class BoardScene : public QGraphicsScene
59 {
60  Q_OBJECT
61 
62  public:
64  explicit BoardScene(QObject* parent = nullptr);
66  virtual ~BoardScene();
67 
69  Chess::Board* board() const;
77 
78  public slots:
87  void populate();
89  void setFenString(const QString& fenString);
91  void makeMove(const Chess::Move& move);
93  void makeMove(const Chess::GenericMove& move);
95  void undoMove();
97  void flip();
102  void onGameFinished(ChessGame* game, Chess::Result result);
103 
104  signals:
111  void humanMove(const Chess::GenericMove& move,
112  const Chess::Side& side);
113 
114  protected:
115  // Inherited from QGraphicsScene
116  virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* event);
117  virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
118  virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
119 
120  private slots:
121  void onTransitionFinished();
122  void onPromotionChosen(const Chess::Piece& promotion);
123 
124  private:
125  void cancelUserMove();
126 
127  enum MoveDirection
128  {
129  Forward,
130  Backward
131  };
132 
133  QPointF squarePos(const Chess::Square& square) const;
134  GraphicsPiece* pieceAt(const QPointF& pos) const;
135  GraphicsPiece* createPiece(const Chess::Piece& piece);
136  QPropertyAnimation* pieceAnimation(GraphicsPiece* piece,
137  const QPointF& endPoint) const;
138  void stopAnimation();
139  void tryMove(GraphicsPiece* piece, const QPointF& targetPos);
140  void selectPiece(const QList<Chess::Piece>& types,
141  const char* member);
142  void addMoveArrow(const QPointF& sourcePos,
143  const QPointF& targetPos);
144  void applyTransition(const Chess::BoardTransition& transition,
145  MoveDirection direction);
146  void updateMoves();
147 
148  Chess::Board* m_board;
149  MoveDirection m_direction;
150  Chess::BoardTransition m_transition;
152  QPointF m_sourcePos;
153  GraphicsBoard* m_squares;
154  GraphicsPieceReserve* m_reserve;
155  QPointer<PieceChooser> m_chooser;
156  QPointer<QAbstractAnimation> m_anim;
157  QSvgRenderer* m_renderer;
160  Chess::GenericMove m_promotionMove;
161  GraphicsPiece* m_highlightPiece;
162  QGraphicsItemGroup* m_moveArrows;
163  QSettings m_settings;
164 };
165 
166 #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
BoardScene(QObject *parent=nullptr)
Definition: boardscene.cpp:41
Chess::Board * board() const
Definition: boardscene.cpp:61
void undoMove()
Definition: boardscene.cpp:166
void onGameFinished(ChessGame *game, Chess::Result result)
Definition: boardscene.cpp:357
An internal chessboard class.
Definition: board.h:56
A graphical surface for displaying a chessgame.
Definition: boardscene.h:58
void populate()
Definition: boardscene.cpp:83
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:30
The side or color of a chess player.
Definition: side.h:35
void flip()
Definition: boardscene.cpp:193
A chess piece.
Definition: piece.h:40
void humanMove(const Chess::GenericMove &move, const Chess::Side &side)
The result of a chess game.
Definition: result.h:34
Details of a board transition caused by a move.
Definition: boardtransition.h:40
virtual ~BoardScene()
Definition: boardscene.cpp:55
A small and efficient chessmove class.
Definition: move.h:42
void makeMove(const Chess::Move &move)
Definition: boardscene.cpp:145
void setBoard(Chess::Board *board)
Definition: boardscene.cpp:66
A generic chess square type consisting of a file and a rank.
Definition: square.h:33
void setFenString(const QString &fenString)
Definition: boardscene.cpp:135