2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program 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 2 of the License, or
8 (at your option) any later version.
14 #include <boost/shared_ptr.hpp>
15 #include <boost/variant/variant_fwd.hpp>
22 namespace GamePrivate
{
25 typedef std::vector
<Entry
> History
;
27 typedef boost::variant
<class UndoAdd
, class UndoPromote
, class UndoTruncate
,
28 class UndoRemove
, class UndoClear
, class UndoSetComment
> UndoOp
;
29 typedef std::vector
<UndoOp
> UndoHistory
;
35 \class Game game.h <game.h>
36 \brief A game with history and variations.
38 This template class encapsulates an editable game with history and undo editing.
42 GamePrivate::History history
;
45 GamePrivate::UndoHistory undo_history
;
48 GamePrivate::Entry
* fetch(const Index
& ix
);
49 const GamePrivate::Entry
* fetch(const Index
& ix
) const;
50 GamePrivate::History
* fetchRef(const Index
& ix
, int* idx
);
51 const GamePrivate::History
* fetchRef(const Index
& ix
, int* idx
) const;
54 void testMove(const Index
& ix
);
55 void saveUndo(const GamePrivate::UndoOp
& op
);
57 QString
variationPgn(const GamePrivate::History
&, const GamePrivate::Entry
&,
58 int start
, const Index
& _ix
) const;
60 virtual void onAdded(const Index
& i
);
61 virtual void onRemoved(const Index
& i
);
62 virtual void onEntryChanged(const Index
& at
, int propagate
=1);
63 virtual void onPromoteVariation(const Index
& i
, int v
);
64 virtual void onSetComment(const Index
& i
, const QString
& c
);
65 virtual void onSetVComment(const Index
& i
, int v
, const QString
& c
);
66 virtual void onCurrentIndexChanged(const Index
& old
= Index(-1));
67 virtual void onAvailableUndo(bool);
68 virtual void onAvailableRedo(bool);
71 /** Constructor, creates an empty game*/
77 /** \return the index of the current position */
80 /** \return an index opinting to the last position in the main line */
81 Index
lastMainlineIndex() const;
83 /** \return true if the game contains the index */
84 bool containsIndex(const Index
& index
) const;
86 /** \return the current move */
89 /** \return the move at the given index */
90 MovePtr
move(const Index
& index
) const;
92 /** \return the current position */
93 PositionPtr
position() const;
95 /** \return the position at the given index */
96 PositionPtr
position(const Index
& index
) const;
98 /** \return the current comment */
99 QString
comment() const;
101 /** \return the comment at the given index */
102 QString
comment(const Index
& index
) const;
104 /** clears the games, and puts \a pos as root position */
105 void reset(PositionPtr pos
);
113 /** sets the comment in the current index */
114 void setComment(const QString
& c
);
116 /** sets the comment at the given index */
117 void setComment(const Index
& index
, const QString
& c
);
119 /** sets the variation comment at the given index/variation */
120 void setVComment(const Index
& index
, int v
, const QString
& c
);
122 /** promotes the current position in the upper main line */
123 void promoteVariation();
125 /** promotes the given position in the upper main line */
126 void promoteVariation(const Index
& index
);
128 /** promotes the given variation in the upper main line */
129 void promoteVariation(const Index
& index
, int v
);
131 /** removes the given variation in the current index */
132 void removeVariation(int v
);
134 /** removes the given variation in the given index */
135 void removeVariation(const Index
& index
);
137 /** removes the given variation in the given index */
138 void removeVariation(const Index
& index
, int v
);
140 /** removes the given variation in the current index */
141 void clearVariations();
143 /** removes the given variation in the given index */
144 void clearVariations(const Index
& index
);
146 /** removes all the successors of the current position */
149 /** removes all the successors of the given position */
150 void truncate(const Index
& index
);
152 /** adds a new move+position after the current one, on the main
153 line if possible, or else in a new variation */
154 void add(MovePtr move
, PositionPtr pos
);
156 /** forces a move+position at in certain index */
157 bool insert(MovePtr move
, PositionPtr pos
, const Index
& index
);
159 /** \return true if we cannot go forward */
160 bool lastPosition() const;
165 /** go forward (in the current mainline) */
168 /** go to the root position */
171 /** go to the last position (in the current mainline) */
174 /** go to a specified index */
175 bool goTo(const Index
& index
);
177 /** \return a pgn containing the whole game (with variations) */
180 /** loads a pgn in the current game */
181 void load(PositionPtr
, const PGN
& pgn
);
183 /** loads a pgn in the current game */
184 void load(const PGN
& pgn
);