removed clock pref widget. clock prefs are definitively part of the lua theme.
[kboard.git] / src / kboard.h
blobbf00af503c2998fcd0441f8089dc76c56b301c8c
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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.
9 */
11 #ifndef LOWLEVEL_H
12 #define LOWLEVEL_H
15 class GraphicalAPI;
18 /**
19 * @file kboard.h
20 * @brief Low level abstract classes used by the interface framework.
22 * This file includes definitions for abstract classes used from within
23 * the interface framework to access variant details using dynamic time
24 * polymorphism.
26 * One of the big problems with this approach is what I call 'lack of
27 * functional dependency support' for runtime polymorphism.
28 * What is missing is the ability to override a virtual member function
29 * in a derived class changing its signature. Or better, it is possible
30 * to only change its return type with a covariant type, while it would
31 * often be useful to change any of its argument types with covariant or
32 * even controvariant types.
33 * For controvariant types, I foresee no problems, and all type checking
34 * can be done statically (of course the method has to be dynamically
35 * bound) as they are now.
36 * When using covariant types, the compiler could insert appropriate
37 * dynamic cast's and type checks in the calling code, when there's no
38 * way to know the exact type of an argument at compile time.
40 * The file @ref kboard_wrapped.h tries to address this problem doing part
41 * of the compiler's work. One exception is that type checking code
42 * is inserted in a method wrapper, instead of the calling place, so
43 * even calling a method of a noncasted instance would cause some
44 * upcast / downcast overhead; this is negligible, though, because
45 * that scenario is rather unlikely, since wrapped classes are always
46 * kept in variables of abstract types.
49 #include <map>
50 #include <boost/shared_ptr.hpp>
51 #include <QString>
52 #include <QStringList>
53 #include "point.h"
54 #include "usermove.h"
55 #include "index.h"
56 #include "option.h"
57 #include "decoratedmove.h"
58 #include "interactiontype.h"
59 #include "fwd.h"
62 /**
63 * @brief A superclass for all the piece classes.
65 class AbstractPiece {
66 public:
67 typedef boost::shared_ptr<AbstractPiece> Ptr;
68 virtual ~AbstractPiece() { }
70 /**
71 * Piece equality. Return false if the second pointer
72 * is null.
74 virtual bool equals(AbstractPiece::Ptr other) const = 0;
76 /**
77 * Return a unique key for the piece.
78 * Used to store pixmaps.
80 virtual QString name() const = 0;
82 /**
83 * Create a deep copy of the piece.
85 virtual AbstractPiece::Ptr clone() const = 0;
89 /**
90 * @brief A superclass for all the move classes.
92 * An abstract move is simply something that can serialize
93 * itself either as SAN notation, or as coordinate notation.
95 class AbstractMove {
96 public:
97 typedef boost::shared_ptr<AbstractMove> Ptr;
98 virtual ~AbstractMove() { }
101 * Return a compact SAN representation for the move.
103 virtual QString SAN(boost::shared_ptr<AbstractPosition> ref) const = 0;
106 * Return a decorated representation for the move.
108 virtual DecoratedMove toDecoratedMove(boost::shared_ptr<AbstractPosition> ref) const = 0;
111 * Return the move in coordinate notation.
113 virtual QString toString(boost::shared_ptr<AbstractPosition> ref) const = 0;
116 * Convert the move to a normal user move. Used to
117 * perform move highlighting.
119 virtual NormalUserMove toUserMove() const = 0;
122 * Checks if the two moves are equal.
124 virtual bool equals(Ptr other) const = 0;
129 * @brief Superclass for pools
131 * A general interface for pools.
133 class AbstractPool {
134 public:
135 typedef boost::shared_ptr<AbstractPool> Ptr;
136 virtual ~AbstractPool() {}
139 * \return the number of items in the pool
141 virtual int size() = 0;
144 * Inserts a piece in the pool, preferably at the position \a pref_index.
145 * But the pool can be unpredictable and the piece can be placed at an arbitrary position.
146 * \return the position at which the item was placed.
148 virtual int insert(int pref_index, AbstractPiece::Ptr piece) = 0;
151 * Gets the piece at the position \a index in the pool.
153 virtual AbstractPiece::Ptr get(int index) = 0;
156 * Removes the piece at the position \a index in the pool.
157 * \return the removed piece.
159 virtual AbstractPiece::Ptr take(int index) = 0;
165 * @brief A superclass for all the position classes.
167 * A general interface for positions. It is used from within
168 * the graphical framework to validate external input wrt
169 * the played variant.
171 class AbstractPosition {
172 public:
173 typedef boost::shared_ptr<AbstractPosition> Ptr;
174 typedef boost::shared_ptr<AbstractPool> PoolPtr;
175 virtual ~AbstractPosition() { }
178 * Return board size in logical units. This could be
179 * static if C++ permitted virtual static functions.
181 virtual Point size() const = 0;
184 * Returns the text to be used for the border surrounding this position in a board
186 virtual QStringList borderCoords() const = 0;
189 * Create the starting piece setup.
191 virtual void setup() = 0;
194 * Retrieve the piece on square @a p.
195 * Return a null pointer if that square is empty.
197 virtual AbstractPiece::Ptr get(const Point& p) const = 0;
200 * Set a piece on the board.
202 virtual void set(const Point& p, AbstractPiece::Ptr piece) = 0;
205 * \return an interface to modify the pool of the board relative to \a player
207 virtual AbstractPool::Ptr pool(int player) = 0;
210 * \return 1 if the piece can be moved, -1 if could be moved in the future (premove), or else 0.
212 virtual InteractionType movable(const Point& p) const = 0;
215 * \return 1 if this pool can be dropped, -1 if could be dropped in the future (premove), or else 0.
217 virtual InteractionType droppable(int) const = 0;
220 * \return an id corresponding to the player
221 * who is in turn.
223 virtual int turn() const = 0;
226 * Sets the player on move
228 virtual void setTurn(int) = 0;
231 * \return the player that just moved
233 virtual int previousTurn() const = 0;
236 * Switch to the next player.
238 virtual void switchTurn() = 0;
241 * Check move legality. Set move fields as needed.
242 * Return whether the move is legal.
243 * This function should return immediately if \a m
244 * has already been tested.
246 virtual bool testMove(AbstractMove::Ptr m) const = 0;
249 * Execute move \a m. Assume that \a m is legal and tested.
251 virtual void move(AbstractMove::Ptr m) = 0;
254 * Create a deep copy of the position.
256 virtual AbstractPosition::Ptr clone() const = 0;
259 * Make the position equal to the given one.
261 virtual void copyFrom(const AbstractPosition::Ptr&) = 0;
264 * Tests if two positions are equal.
266 virtual bool equals(AbstractPosition::Ptr p) const = 0;
269 * Return a move from an algebraic notation, or a null pointer.
271 virtual AbstractMove::Ptr getMove(const class AlgebraicNotation&) const = 0;
274 * Return a move from an algebraic notation, or a null pointer.
276 virtual AbstractMove::Ptr getMove(const QString&) const = 0;
279 * Return a string representing the current state of the position.
281 virtual QString state() const = 0;
284 * Return a FEN representation for the position, assuming
285 * @a halfmove as halfmove clock and @a fullmove as full move
286 * number.
288 virtual QString fen(int halfmove, int fullmove) const = 0;
291 * A piece somehow representing or related to the move
292 * which has to be drawn by the interface.
293 * Examples:
294 * * for one click moves, the piece that would
295 * appear on the square.
296 * * for promotions in chesslike variants, the promoted
297 * piece.
298 * @note Return a null pointer if the move has no valid
299 * piece hint defined.
301 virtual AbstractPiece::Ptr moveHint(AbstractMove::Ptr) const = 0;
304 * Variant introspection
306 virtual QString variant() const = 0;
309 * For debugging
311 virtual void dump() const = 0;
314 class AnimationGroup;
317 * @brief An abstract superclass for all animator classes.
319 * An animator is a class which is given a difference between
320 * a graphical and a logical position, and schedules an animation
321 * which graphically does an update.
322 * If the difference is due to a move, the animator tries to
323 * create a smooth animation which could possibly work for many
324 * chess-like variants, and then fills in the gaps with a direct
325 * update.
327 class AbstractAnimator {
328 public:
329 typedef boost::shared_ptr<AbstractAnimator> Ptr;
330 virtual ~AbstractAnimator() { }
333 * Sync the graphical position with the given position.
335 virtual AnimationPtr warp(AbstractPosition::Ptr) = 0;
338 * Animate forward syncing to the given position.
340 virtual AnimationPtr forward(AbstractPosition::Ptr, AbstractMove::Ptr) = 0;
343 * Animate back syncing to the given position.
345 virtual AnimationPtr back(AbstractPosition::Ptr, AbstractMove::Ptr) = 0;
349 class VariantInfo {
350 public:
351 virtual ~VariantInfo() { }
352 virtual AbstractPosition::Ptr createPosition() = 0;
353 virtual AbstractPosition::Ptr createCustomPosition(const OptList& l) = 0;
354 virtual AbstractPosition::Ptr createPositionFromFEN(const QString& fen) = 0;
355 virtual AbstractPosition::Ptr createChessboard(int turn, bool, bool, bool, bool, const Point&) = 0;
356 virtual void forallPieces(class PieceFunction&) = 0;
357 virtual int moveListLayout() const = 0;
358 virtual AbstractAnimator::Ptr createAnimator(GraphicalAPI* graphical_api) = 0;
359 virtual AbstractMove::Ptr createNormalMove(const NormalUserMove&) = 0;
360 virtual AbstractMove::Ptr createDropMove(const DropUserMove&) = 0;
361 virtual AbstractMove::Ptr getVerboseMove(int turn, const class VerboseNotation&) const = 0;
364 * Create a new piece using the given description string.
365 * \param description A string representing the piece to be created. Its
366 * meaning is defined by the variant.
367 * \return A newly created piece.
369 virtual AbstractPiece::Ptr createPiece(const QString& description) = 0;
372 * \return if moves are done by just clicking
374 virtual bool simpleMoves() = 0;
377 * \return the name of the variant
379 virtual QString name() const = 0;
382 * \return the name of the theme proxy variant, ie the variant whose theme can be used
383 * for the current one (for instance crazyhouse can use chess themes).
385 virtual QString themeProxy() const = 0;
388 * \return the (subvariant) options that can be specified for position creation, such as board size, etc
390 virtual OptList positionOptions() const = 0;
393 #endif // LOWLEVEL_H