kreversiview.cpp 8.71 KB
Newer Older
1
/*
2 3
    Copyright 2006-2007 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 19

    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/>.
*/

20
#include "kreversiview.h"
21
#include "colorscheme.h"
Denis Kuplyakov's avatar
Denis Kuplyakov committed
22 23

#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
24 25

#include <QStandardPaths>
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
26

Frederik Schwarzer's avatar
Frederik Schwarzer committed
27 28 29 30 31 32 33 34
KReversiView::KReversiView(KReversiGame* game, QWidget *parent, KgThemeProvider *provider)
    : KgDeclarativeView(parent),
    m_provider(provider),
    m_delay(ANIMATION_SPEED_NORMAL),
    m_game(0),
    m_showLastMove(false),
    m_showLegalMoves(false),
    m_showLabels(false)
35
{
36
    m_provider->setDeclarativeEngine(QStringLiteral("themeProvider"), engine());
37

38 39
    qmlRegisterType<ColorScheme>("ColorScheme", 1, 0, "ColorScheme");

40
    QString path = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("qml/Table.qml"));
41
    setSource(QUrl::fromLocalFile(path));
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
42

43
    m_qml_root = (QObject*) rootObject();
44
    rootContext()->setContextProperty(QStringLiteral("container"), this);
45

46 47
    connect(m_qml_root, SIGNAL(cellClicked(int,int)),
            this, SLOT(onPlayerMove(int,int)));
Laurent Montel's avatar
Laurent Montel committed
48

49 50
    setGame(game);
}
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
51

52
void KReversiView::setGame(KReversiGame *game)
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
53
{
54
    // disconnect signals from previous game if they exist,
55 56
    // we are not interested in them anymore
    if (m_game) {
Laurent Montel's avatar
Laurent Montel committed
57 58 59 60 61
        disconnect(m_game, &KReversiGame::boardChanged, this, &KReversiView::updateBoard);
        disconnect(m_game, &KReversiGame::moveFinished, this, &KReversiView::gameMoveFinished);
        disconnect(m_game, &KReversiGame::gameOver, this, &KReversiView::gameOver);
        disconnect(m_game, &KReversiGame::whitePlayerCantMove, this, &KReversiView::whitePlayerCantMove);
        disconnect(m_game, &KReversiGame::blackPlayerCantMove, this, &KReversiView::blackPlayerCantMove);
Denis Kuplyakov's avatar
Denis Kuplyakov committed
62
        delete m_game;
63 64
    }

65 66
    m_game = game;

Denis Kuplyakov's avatar
Denis Kuplyakov committed
67
    if (m_game) {
Laurent Montel's avatar
Laurent Montel committed
68 69 70 71 72
        connect(m_game, &KReversiGame::boardChanged, this, &KReversiView::updateBoard);
        connect(m_game, &KReversiGame::moveFinished, this, &KReversiView::gameMoveFinished);
        connect(m_game, &KReversiGame::gameOver, this, &KReversiView::gameOver);
        connect(m_game, &KReversiGame::whitePlayerCantMove, this, &KReversiView::whitePlayerCantMove);
        connect(m_game, &KReversiGame::blackPlayerCantMove, this, &KReversiView::blackPlayerCantMove);
73

74 75
        m_game->setDelay(m_delay);
    }
76

Denis Kuplyakov's avatar
Denis Kuplyakov committed
77
    m_hint = KReversiMove();
78 79 80 81

    updateBoard();
}

Denis Kuplyakov's avatar
Denis Kuplyakov committed
82
void KReversiView::setChipsPrefix(ChipsPrefix chipsPrefix)
83
{
84
    m_ColouredChips = chipsPrefix;
85 86
    m_qml_root->setProperty("chipsImagePrefix",
                            Utils::chipPrefixToString(chipsPrefix));
87 88 89 90 91 92 93 94
}

void KReversiView::setShowBoardLabels(bool show)
{
    m_showLabels = show;
    updateBoard();
}

95
void KReversiView::setAnimationSpeed(int speed)
96
{
97
    int value = ANIMATION_SPEED_NORMAL;
98 99 100 101 102 103 104 105 106 107 108 109 110
    switch (speed) {
    case 0:
        value = ANIMATION_SPEED_SLOW;
        break;
    default:
    case 1:
        value = ANIMATION_SPEED_NORMAL;
        break;
    case 2:
        value = ANIMATION_SPEED_FAST;
        break;
    }

111
    m_delay = value;
112 113 114 115

    if (m_game)
        m_game->setDelay(value);

116
    m_qml_root->setProperty("chipsAnimationTime", value);
117 118
}

119 120 121 122 123
KReversiView::~KReversiView()
{
    setGame(0);
}

124 125
void KReversiView::updateBoard()
{
Denis Kuplyakov's avatar
Denis Kuplyakov committed
126 127 128 129 130 131 132 133
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            QMetaObject::invokeMethod(m_qml_root, "setPreAnimationTime",
                                      Q_ARG(QVariant, i),
                                      Q_ARG(QVariant, j),
                                      Q_ARG(QVariant, m_game ? m_game->getPreAnimationDelay(KReversiPos(i, j)) : 0));
        }
    }
134

135 136
    for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++) {
137
            QString new_state;
138
            if (m_game) // showing empty board if has no game
Denis Kuplyakov's avatar
Denis Kuplyakov committed
139
                switch (m_game->chipColorAt(KReversiMove(NoColor, i, j))) {
140
                case Black:
141
                    new_state = QStringLiteral("Black");
142 143
                    break;
                case White:
144
                    new_state = QStringLiteral("White");
145 146
                    break;
                case NoColor:
147
                    new_state = QString();
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
                    break;
                }

            QMetaObject::invokeMethod(m_qml_root, "setChipState",
                                      Q_ARG(QVariant, i),
                                      Q_ARG(QVariant, j),
                                      Q_ARG(QVariant, new_state));

            // clearing legal markers, hints and lastmove
            QMetaObject::invokeMethod(m_qml_root, "setLegal",
                                      Q_ARG(QVariant, i),
                                      Q_ARG(QVariant, j),
                                      Q_ARG(QVariant, false));
            QMetaObject::invokeMethod(m_qml_root, "setHint",
                                      Q_ARG(QVariant, i),
                                      Q_ARG(QVariant, j),
                                      Q_ARG(QVariant, false));
            QMetaObject::invokeMethod(m_qml_root, "setLastMove",
                                      Q_ARG(QVariant, i),
                                      Q_ARG(QVariant, j),
                                      Q_ARG(QVariant, false));
        }

    if (m_game && m_showLegalMoves) {
Denis Kuplyakov's avatar
Denis Kuplyakov committed
172
        MoveList possible_moves = m_game->possibleMoves();
173 174 175 176 177 178 179 180
        for (int i = 0; i < possible_moves.size(); i++) {
            QMetaObject::invokeMethod(m_qml_root, "setLegal",
                                      Q_ARG(QVariant, possible_moves.at(i).row),
                                      Q_ARG(QVariant, possible_moves.at(i).col),
                                      Q_ARG(QVariant, true));
        }
    }

181
    m_qml_root->setProperty("isBoardShowingLabels", m_showLabels);
182 183

    if (m_hint.isValid()) {
184 185 186
        QMetaObject::invokeMethod(m_qml_root, "setChipState",
                                  Q_ARG(QVariant, m_hint.row),
                                  Q_ARG(QVariant, m_hint.col),
187
                                  Q_ARG(QVariant, QLatin1Literal("Black")));
188 189 190 191
        QMetaObject::invokeMethod(m_qml_root, "setHint",
                                  Q_ARG(QVariant, m_hint.row),
                                  Q_ARG(QVariant, m_hint.col),
                                  Q_ARG(QVariant, true));
192 193
    }

194
    if (m_game && m_showLastMove) {
Denis Kuplyakov's avatar
Denis Kuplyakov committed
195
        KReversiMove lastmove = m_game->getLastMove();
196 197 198 199 200 201
        if (lastmove.isValid())
            QMetaObject::invokeMethod(m_qml_root, "setLastMove",
                                      Q_ARG(QVariant, lastmove.row),
                                      Q_ARG(QVariant, lastmove.col),
                                      Q_ARG(QVariant, true));
    }
202 203
}

204
void KReversiView::setShowLastMove(bool show)
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
205
{
206 207 208 209 210 211 212 213 214 215 216 217
    m_showLastMove = show;
    updateBoard();
}

void KReversiView::setShowLegalMoves(bool show)
{
    m_showLegalMoves = show;
    updateBoard();
}

void KReversiView::slotHint()
{
Denis Kuplyakov's avatar
Denis Kuplyakov committed
218
    if (!m_game) {
Denis Kuplyakov's avatar
Denis Kuplyakov committed
219
        m_hint = KReversiMove();
220
        return;
221
    }
222

223 224 225 226
    m_hint = m_game->getHint();
    updateBoard();
}

227 228
void KReversiView::onPlayerMove(int row, int col)
{
Denis Kuplyakov's avatar
Denis Kuplyakov committed
229 230 231
    if (!m_game)
        return;

Denis Kuplyakov's avatar
Denis Kuplyakov committed
232
    emit userMove(KReversiPos(row, col));
233 234 235
}


Denis Kuplyakov's avatar
Denis Kuplyakov committed
236
void KReversiView::gameMoveFinished()
237
{
Denis Kuplyakov's avatar
Denis Kuplyakov committed
238
    m_hint = KReversiMove();
239 240 241
    updateBoard();
}

Denis Kuplyakov's avatar
Denis Kuplyakov committed
242
void KReversiView::gameOver()
243 244 245
{
}

Denis Kuplyakov's avatar
Denis Kuplyakov committed
246
void KReversiView::whitePlayerCantMove()
247
{
248
    // TODO: use Computer, You and Opponent instead in message
Denis Kuplyakov's avatar
Denis Kuplyakov committed
249
    QMetaObject::invokeMethod(m_qml_root, "showPopup",
250 251
                              Q_ARG(QVariant, m_ColouredChips ?
                                    i18n("Red can not perform any move. It is blue turn again.") :
Denis Kuplyakov's avatar
Denis Kuplyakov committed
252
                                    i18n("White can not perform any move. It is black turn again.")));
253
    updateBoard();
254 255
}

Denis Kuplyakov's avatar
Denis Kuplyakov committed
256
void KReversiView::blackPlayerCantMove()
257
{
258
    // TODO: use Computer, You and Opponent instead in message
Denis Kuplyakov's avatar
Denis Kuplyakov committed
259
    QMetaObject::invokeMethod(m_qml_root, "showPopup",
260 261
                              Q_ARG(QVariant, m_ColouredChips ?
                                    i18n("Blue can not perform any move. It is red turn again.") :
Denis Kuplyakov's avatar
Denis Kuplyakov committed
262
                                    i18n("Black can not perform any move. It is white turn again.")));
263
    updateBoard();
264
}