File indexing completed on 2024-12-15 03:48:05
0001 /* 0002 SPDX-FileCopyrightText: 2008 Sascha Peilicke <sasch.pe@gmx.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "gamewidget.h" 0008 #include "game/game.h" 0009 #include "game/stone.h" 0010 #include "preferences.h" 0011 0012 #include "gui/graphicsview/themerenderer.h" 0013 0014 #include <QIcon> 0015 namespace Kigo { 0016 0017 GameWidget::GameWidget(Game *game, QWidget *parent) 0018 : QWidget(parent), m_game(game) 0019 { 0020 Q_ASSERT(m_game); 0021 setupUi(this); 0022 0023 finishButton->setIcon(QIcon::fromTheme( QStringLiteral( "media-playback-stop" ))); 0024 0025 connect(m_game, &Game::boardInitialized, this, &GameWidget::init); 0026 connect(m_game, &Game::boardChanged, this, &GameWidget::update); 0027 connect(m_game, &Game::consecutivePassMovesPlayed, this, &GameWidget::enableFinishButton); 0028 connect(finishButton, &QPushButton::clicked, this, &GameWidget::finishButtonClicked); 0029 } 0030 0031 void GameWidget::init() 0032 { 0033 if (!m_game->isRunning()) { 0034 //qCDebug(KIGO_LOG) << "Game is not running, no information update"; 0035 return; 0036 } 0037 0038 if (Preferences::whitePlayerHuman()) { 0039 whiteNameLabel->setText(Preferences::whitePlayerName()); 0040 } else { 0041 whiteNameLabel->setText(i18n("Computer (level %1)", Preferences::whitePlayerStrength())); 0042 } 0043 if (Preferences::blackPlayerHuman()) { 0044 blackNameLabel->setText(Preferences::blackPlayerName()); 0045 } else { 0046 blackNameLabel->setText(i18n("Computer (level %1)", Preferences::blackPlayerStrength())); 0047 } 0048 komiLabel->setText(QString::number(m_game->komi())); 0049 handicapLabel->setText(i18np("%1 Stone", "%1 Stones", m_game->fixedHandicap())); 0050 0051 const QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::WhiteStone, QSize(48, 48)); 0052 whiteStoneImageLabel->setPixmap(whiteStone); 0053 const QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::BlackStone, QSize(48, 48)); 0054 blackStoneImageLabel->setPixmap(blackStone); 0055 0056 update(); 0057 } 0058 0059 void GameWidget::update() 0060 { 0061 moveLabel->setText(QString::number(m_game->currentMoveNumber())); 0062 0063 if (!m_game->moves().isEmpty()) { 0064 const Move last = m_game->lastMove(); 0065 if (last.player()->isWhite()) { 0066 lastMoveLabel->setText(i18nc("Indication who played the last move", "%1 (white)", last.stone().toString())); 0067 } else if (last.player()->isBlack()) { 0068 lastMoveLabel->setText(i18nc("Indication who played the last move", "%1 (black)", last.stone().toString())); 0069 } else { 0070 lastMoveLabel->clear(); 0071 } 0072 } 0073 0074 if (m_game->currentPlayer().isWhite()) { 0075 const QPixmap whiteStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::WhiteStone, QSize(64, 64)); 0076 currentPlayerImageLabel->setPixmap(whiteStone); 0077 currentPlayerLabel->setText(i18n("White's turn")); 0078 } else if (m_game->currentPlayer().isBlack()) { 0079 const QPixmap blackStone = ThemeRenderer::self()->renderElement(Kigo::ThemeRenderer::Element::BlackStone, QSize(64, 64)); 0080 currentPlayerImageLabel->setPixmap(blackStone); 0081 currentPlayerLabel->setText(i18n("Black's turn")); 0082 } else { 0083 currentPlayerLabel->clear(); 0084 } 0085 0086 whiteCapturesLabel->setText(i18np("%1 capture", "%1 captures", m_game->captures(m_game->whitePlayer()))); 0087 blackCapturesLabel->setText(i18np("%1 capture", "%1 captures", m_game->captures(m_game->blackPlayer()))); 0088 } 0089 0090 void GameWidget::finishButtonClicked() 0091 { 0092 finishButton->setEnabled(false); 0093 Q_EMIT finishClicked(); 0094 } 0095 0096 void GameWidget::enableFinishButton() 0097 { 0098 finishButton->setEnabled(true); 0099 } 0100 0101 } // End of namespace Kigo 0102 0103 #include "moc_gamewidget.cpp"