kreversiview.h 4.64 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 24
#include <KgDeclarativeView>
#include <KgThemeProvider>
25

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

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

46
    /**
47 48 49 50
     *  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
51
     */
52
    void setGame(KReversiGame* game);
53

54
    /**
55
     *  Sets the chips prefix to @p chipsPrefix
56
     */
Denis Kuplyakov's avatar
Denis Kuplyakov committed
57
    void setChipsPrefix(ChipsPrefix chipsPrefix);
58

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

67
    /**
68 69 70
     *  Sets the animation speed
     *
     *  @param speed 0 - slow, 1 - normal, 2 - fast
71 72
     *
     *  @return time for animation in milliseconds to pass it to KReversiGame
73
     */
74
    void setAnimationSpeed(int speed);
75

76
public Q_SLOTS:
77
    /**
78 79 80 81
    *   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
82 83
     */
    void setShowLastMove(bool show);
84

85 86
    /**
     *  This will make view visually mark squares with possible moves
87 88 89
     *
     *  @param show @c true to show legal moves
     *              @c false to don't show legal moves
90 91
     */
    void setShowLegalMoves(bool show);
92

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

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

Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
117
private:
118 119 120
    /**
     *  40 ms time per frame for animation
     */
121
    static const int ANIMATION_SPEED_SLOW = 40 * 12;
122 123

    /**
124
     *  25 ms time per frame for animation
125
     */
126
    static const int ANIMATION_SPEED_NORMAL = 25 * 12;
127 128 129 130

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

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

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

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

    /**
     *  Current animation time
     */
151
    int m_delay;
152

153
    /**
154
     *  Pointer to game object
155 156
     */
    KReversiGame *m_game;
157

158
    /**
159
     *  The SVG element prefix for the current chip set
160
     */
161
    ChipsPrefix m_ColouredChips;
162

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

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

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