kreversiview.h 4.65 KB
Newer Older
1
/*
2 3
    Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
    Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
4
    Copyright 2013 Denis Kuplyakov <dener.kup@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
19 20 21
#ifndef KREVERSI_VIEW_H
#define KREVERSI_VIEW_H

Denis Kuplyakov's avatar
Denis Kuplyakov committed
22 23
#include <QTimer>

24 25
#include <KgDeclarativeView>
#include <KgThemeProvider>
26

27 28
#include "commondefs.h"
#include "kreversigame.h"
29

30 31 32 33
/**
 *  This class provides graphical representation of KReversiGame
 *  using QML for graphics display.
 *  It displays the reversi board in its current state,
34
 *  receives a mouse events, translates them with signals,
35 36
 *  receives board-changed notifications, nicely animates them.
 */
37
class KReversiView : public KgDeclarativeView
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
38
{
39
    Q_OBJECT
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
40
public:
41
    explicit KReversiView(KReversiGame* game, QWidget *parent, KgThemeProvider *provider);
42 43 44 45
    /**
     *  Destructor used to delete game object owned by class
     */
    ~KReversiView();
46

47
    /**
48 49 50 51
     *  Sets the game object which this view will visualize/use
     *
     *  @param game pointer to game object for visualization. View takes
     *              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
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
58
    void setChipsPrefix(ChipsPrefix chipsPrefix);
59

60 61
    /**
     *  Sets whether to show board labels.
62 63 64
     *
     *  @param show @c true to show labels
     *              @c false to hide labels
65 66
     */
    void setShowBoardLabels(bool show);
67

68
    /**
69 70 71
     *  Sets the animation speed
     *
     *  @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

77 78
public slots:
    /**
79 80 81 82
    *   This will make view visually mark the last made move
    *
    *   @param show @c true to show last move
    *               @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 90
     *
     *  @param show @c true to show legal moves
     *              @c false to don't show legal moves
91 92
     */
    void setShowLegalMoves(bool show);
93

94 95 96 97
    /**
     *  Shows hint for player
     */
    void slotHint();
98

Denis Kuplyakov's avatar
Denis Kuplyakov committed
99
private slots:
100
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
101
     *  Triggered on user click on board, connected to QML signal
102
     *
103 104
     *  @param row index of the clicked cell row (starting from 0)
     *  @param col index of the clicked cell column (starting from 0)
105
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
106
    void onPlayerMove(int row, int col);
107
    /**
Denis Kuplyakov's avatar
Denis Kuplyakov committed
108
     *  Synchronizes graphical board with m_game's board
109
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
110 111 112 113 114
    void updateBoard();
    void gameMoveFinished();
    void gameOver();
    void whitePlayerCantMove();
    void blackPlayerCantMove();
115
signals:
Denis Kuplyakov's avatar
Denis Kuplyakov committed
116
    void userMove(KReversiPos);
117

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
118
private:
119 120 121
    /**
     *  40 ms time per frame for animation
     */
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 131

    /**
     *  15 ms time per frame for animation
     */
132
    static const int ANIMATION_SPEED_FAST = 15 * 12;
133 134 135 136

    /**
     *  Used to provide access to QML-implemented board
     */
137
    QObject *m_qml_root;
138 139 140 141

    /**
     *  Used to access theme engine from QML
     */
142
    KgThemeProvider *m_provider;
143 144 145 146

    /**
     *  Position of calculated hint. It is not valid if there is no hint
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
147
    KReversiMove m_hint;
148 149 150 151

    /**
     *  Current animation time
     */
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
    ChipsPrefix m_ColouredChips;
163

164 165 166 167
    /**
     *  If true, then last made turn will be shown to the player
     */
    bool m_showLastMove;
168

169 170 171 172
    /**
     *  If true, then all possible moves will be shown to the player
     */
    bool m_showLegalMoves;
173

174 175 176 177
    /**
     *  If true board labels will be rendered
     */
    bool m_showLabels;
178 179 180 181 182
    
    /**
     *  Used to handle animation duration due to sequental turning of chips
     */
    int m_maxDelay;
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
183 184
};
#endif