1/*
2 Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
3 Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
4 Copyright 2013 Denis Kuplyakov <dener.kup@gmail.com>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef KREVERSI_VIEW_H
20#define KREVERSI_VIEW_H
21
22#include <QTimer>
23
24#include <KgDeclarativeView>
25#include <KgThemeProvider>
26
27#include <commondefs.h>
28#include <kreversigame.h>
29
30/**
31 * This class provides graphical representation of KReversiGame
32 * using QML for graphics display.
33 * It displays the reversi board in its current state,
34 * receives a mouse events, translates them with signals,
35 * receives board-changed notifications, nicely animates them.
36 */
37class KReversiView : public KgDeclarativeView
38{
39 Q_OBJECT
40public:
41 explicit KReversiView(KReversiGame* game, QWidget *parent, KgThemeProvider *provider);
42 /**
43 * Destructor used to delete game object owned by class
44 */
45 ~KReversiView();
46
47 /**
48 * Sets the game object which this view will visualize/use
49 *
50 * @param game pointer to game object for visualization. View takes
51 * ownership over game object and will delete it
52 */
53 void setGame(KReversiGame* game);
54
55 /**
56 * Sets the chips prefix to @p chipsPrefix
57 */
58 void setChipsPrefix(ChipsPrefix chipsPrefix);
59
60 /**
61 * Sets whether to show board labels.
62 *
63 * @param show @c true to show labels
64 * @c false to hide labels
65 */
66 void setShowBoardLabels(bool show);
67
68 /**
69 * Sets the animation speed
70 *
71 * @param speed 0 - slow, 1 - normal, 2 - fast
72 *
73 * @return time for animation in milliseconds to pass it to KReversiGame
74 */
75 void setAnimationSpeed(int speed);
76
77public slots:
78 /**
79 * This will make view visually mark the last made move
80 *
81 * @param show @c true to show last move
82 * @c false to don't show last move
83 */
84 void setShowLastMove(bool show);
85
86 /**
87 * This will make view visually mark squares with possible moves
88 *
89 * @param show @c true to show legal moves
90 * @c false to don't show legal moves
91 */
92 void setShowLegalMoves(bool show);
93
94 /**
95 * Shows hint for player
96 */
97 void slotHint();
98
99private slots:
100 /**
101 * Triggered on user click on board, connected to QML signal
102 *
103 * @param row index of the clicked cell row (starting from 0)
104 * @param col index of the clicked cell column (starting from 0)
105 */
106 void onPlayerMove(int row, int col);
107 /**
108 * Synchronizes graphical board with m_game's board
109 */
110 void updateBoard();
111 void gameMoveFinished();
112 void gameOver();
113 void whitePlayerCantMove();
114 void blackPlayerCantMove();
115signals:
116 void userMove(KReversiPos);
117
118private:
119 /**
120 * 40 ms time per frame for animation
121 */
122 static const int ANIMATION_SPEED_SLOW = 40 * 12;
123
124 /**
125 * 25 ms time per frame for animation
126 */
127 static const int ANIMATION_SPEED_NORMAL = 25 * 12;
128
129 /**
130 * 15 ms time per frame for animation
131 */
132 static const int ANIMATION_SPEED_FAST = 15 * 12;
133
134 /**
135 * Used to provide access to QML-implemented board
136 */
137 QObject *m_qml_root;
138
139 /**
140 * Used to access theme engine from QML
141 */
142 KgThemeProvider *m_provider;
143
144 /**
145 * Position of calculated hint. It is not valid if there is no hint
146 */
147 KReversiMove m_hint;
148
149 /**
150 * Current animation time
151 */
152 int m_delay;
153
154 /**
155 * Pointer to game object
156 */
157 KReversiGame *m_game;
158
159 /**
160 * The SVG element prefix for the current chip set
161 */
162 QString m_chipsPrefix;
163
164 /**
165 * If true, then last made turn will be shown to the player
166 */
167 bool m_showLastMove;
168
169 /**
170 * If true, then all possible moves will be shown to the player
171 */
172 bool m_showLegalMoves;
173
174 /**
175 * If true board labels will be rendered
176 */
177 bool m_showLabels;
178
179 /**
180 * Used to handle animation duration due to sequental turning of chips
181 */
182 int m_maxDelay;
183};
184#endif
185