34 #include <KActionCollection>
35 #include <KConfigDialog>
36 #include <KFileDialog>
37 #include <KNS3/DownloadDialog>
38 #include <KStandardDirs>
39 #include <KStandardGameAction>
40 #include <KToggleAction>
42 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
43 #include <libkdegamesprivate/kgamethemeselector.h>
45 #include <QDockWidget>
55 setCentralWidget(m_gameView);
59 setupGUI(QSize(800, 700), KXmlGuiWindow::ToolBar | KXmlGuiWindow::Keys |
60 KXmlGuiWindow::Save | KXmlGuiWindow::Create);
62 connect(m_game, SIGNAL(waiting(
bool)),
this, SLOT(showBusy(
bool)));
63 connect(m_game, SIGNAL(consecutivePassMovesPlayed(
int)),
this, SLOT(showFinishGameAction()));
64 connect(m_game, SIGNAL(resigned(
Player)),
this, SLOT(finishGame()));
65 connect(m_game, SIGNAL(passMovePlayed(
Player)),
this, SLOT(passMovePlayed(
Player)));
67 if (isBackendWorking()) {
68 if (!loadGame(fileName)) {
76 void MainWindow::newGame()
81 m_gameView->setInteractive(
false);
83 m_newGameAction->setEnabled(
true);
84 m_loadGameAction->setEnabled(
true);
85 m_saveAction->setEnabled(
false);
86 m_startGameAction->setEnabled(
true);
87 m_finishGameAction->setEnabled(
false);
89 m_undoMoveAction->setEnabled(
false);
90 m_redoMoveAction->setEnabled(
false);
91 m_passMoveAction->setEnabled(
false);
92 m_hintAction->setEnabled(
false);
94 disconnect(m_game, SIGNAL(canRedoChanged(
bool)), m_redoMoveAction, SLOT(setEnabled(
bool)));
95 disconnect(m_game, SIGNAL(canUndoChanged(
bool)), m_undoMoveAction, SLOT(setEnabled(
bool)));
96 disconnect(m_game, SIGNAL(currentPlayerChanged(
Player)),
this, SLOT(playerChanged()));
98 m_gameDock->setVisible(
false);
99 m_gameDock->toggleViewAction()->setEnabled(
false);
100 m_movesDock->setVisible(
false);
101 m_movesDock->toggleViewAction()->setEnabled(
false);
102 m_setupDock->setVisible(
true);
103 m_errorDock->setVisible(
false);
106 m_gameScene->
showMessage(i18n(
"Set up a new game..."));
109 void MainWindow::loadGame()
111 QString fileName = KFileDialog::getOpenFileName(KUrl(KStandardDirs::locate(
"appdata",
"games/")),
"*.sgf");
112 if (!fileName.isEmpty()) {
117 bool MainWindow::loadGame(
const QString &fileName)
119 if (!fileName.isEmpty() && QFile(fileName).exists()) {
123 m_gameView->setInteractive(
false);
125 m_newGameAction->setEnabled(
true);
126 m_loadGameAction->setEnabled(
true);
127 m_saveAction->setEnabled(
false);
128 m_startGameAction->setEnabled(
true);
129 m_finishGameAction->setEnabled(
false);
131 m_undoMoveAction->setEnabled(
false);
132 m_redoMoveAction->setEnabled(
false);
133 m_passMoveAction->setEnabled(
false);
134 m_hintAction->setEnabled(
false);
136 disconnect(m_game, SIGNAL(canRedoChanged(
bool)), m_redoMoveAction, SLOT(setEnabled(
bool)));
137 disconnect(m_game, SIGNAL(canUndoChanged(
bool)), m_undoMoveAction, SLOT(setEnabled(
bool)));
138 disconnect(m_game, SIGNAL(currentPlayerChanged(Player)),
this, SLOT(playerChanged()));
140 m_gameDock->setVisible(
false);
141 m_gameDock->toggleViewAction()->setEnabled(
false);
142 m_movesDock->setVisible(
false);
143 m_movesDock->toggleViewAction()->setEnabled(
false);
144 m_setupDock->setVisible(
true);
145 m_errorDock->setVisible(
false);
148 m_gameScene->
showMessage(i18n(
"Set up a loaded game..."));
151 m_gameScene->
showMessage(i18n(
"Unable to load game..."));
157 void MainWindow::getMoreGames()
159 KNS3::DownloadDialog dialog(
"kigo-games.knsrc",
this);
169 void MainWindow::backendError()
171 m_gameView->setInteractive(
false);
173 m_newGameAction->setEnabled(
false);
174 m_loadGameAction->setEnabled(
false);
175 m_saveAction->setEnabled(
false);
176 m_startGameAction->setEnabled(
false);
177 m_finishGameAction->setEnabled(
false);
179 m_undoMoveAction->setEnabled(
false);
180 m_redoMoveAction->setEnabled(
false);
181 m_passMoveAction->setEnabled(
false);
182 m_hintAction->setEnabled(
false);
184 disconnect(m_game, SIGNAL(canRedoChanged(
bool)), m_redoMoveAction, SLOT(setEnabled(
bool)));
185 disconnect(m_game, SIGNAL(canUndoChanged(
bool)), m_undoMoveAction, SLOT(setEnabled(
bool)));
186 disconnect(m_game, SIGNAL(currentPlayerChanged(Player)),
this, SLOT(playerChanged()));
187 m_gameDock->setVisible(
false);
188 m_gameDock->toggleViewAction()->setEnabled(
false);
189 m_movesDock->setVisible(
false);
190 m_movesDock->toggleViewAction()->setEnabled(
false);
191 m_setupDock->setVisible(
false);
192 m_errorDock->setVisible(
true);
195 void MainWindow::saveGame()
197 QString fileName = KFileDialog::getSaveFileName(KUrl(QDir::homePath()),
"*.sgf");
199 if (!fileName.isEmpty()) {
200 if (m_game->
save(fileName))
203 m_gameScene->
showMessage(i18n(
"Unable to save game."));
207 void MainWindow::startGame()
209 m_saveAction->setEnabled(
true);
210 m_startGameAction->setEnabled(
false);
211 m_finishGameAction->setEnabled(
false);
217 connect(m_game, SIGNAL(canRedoChanged(
bool)), m_redoMoveAction, SLOT(setEnabled(
bool)));
218 connect(m_game, SIGNAL(canUndoChanged(
bool)), m_undoMoveAction, SLOT(setEnabled(
bool)));
220 m_passMoveAction->setEnabled(
true);
221 m_hintAction->setEnabled(
true);
223 m_gameView->setInteractive(
true);
224 m_undoView->setEnabled(
true);
228 m_passMoveAction->setEnabled(
false);
229 m_hintAction->setEnabled(
false);
231 m_gameView->setInteractive(
false);
232 m_undoView->setEnabled(
false);
237 connect(m_game, SIGNAL(currentPlayerChanged(Player)),
this, SLOT(playerChanged()));
242 m_setupDock->setVisible(
false);
243 m_errorDock->setVisible(
false);
244 m_gameDock->setVisible(
true);
245 m_gameDock->toggleViewAction()->setEnabled(
true);
246 m_movesDock->setVisible(
true);
247 m_movesDock->toggleViewAction()->setEnabled(
true);
252 m_undoView->setEnabled(
false);
255 void MainWindow::finishGame()
257 m_gameView->setInteractive(
false);
261 m_undoMoveAction->setEnabled(
false);
262 m_redoMoveAction->setEnabled(
false);
263 m_passMoveAction->setEnabled(
false);
264 m_hintAction->setEnabled(
false);
265 m_startGameAction->setEnabled(
false);
266 m_finishGameAction->setEnabled(
false);
268 m_undoView->setEnabled(
false);
273 if (score.color() ==
'W') {
280 if (score.upperBound() == score.lowerBound()) {
281 m_gameScene->
showMessage(i18n(
"%1 won with a score of %2.",
282 name, score.score()), 0);
284 m_gameScene->
showMessage(i18n(
"%1 won with a score of %2 (bounds: %3 and %4).",
285 name, score.score(), score.upperBound(), score.lowerBound()), 0);
289 void MainWindow::undo()
297 void MainWindow::redo()
305 void MainWindow::pass()
313 void MainWindow::hint()
319 void MainWindow::showPreferences()
321 if (KConfigDialog::showDialog(
"settings"))
324 KConfigDialog *dialog =
new KConfigDialog(
this,
"settings",
Preferences::self());
325 dialog->addPage(
new GeneralConfig(), i18n(
"General"),
"preferences-other");
326 dialog->addPage(
new KGameThemeSelector(dialog,
Preferences::self(), KGameThemeSelector::NewStuffDisableDownload), i18n(
"Themes"),
"games-config-theme");
327 dialog->setHelp(QString(),
"Kigo");
328 connect(dialog, SIGNAL(settingsChanged(QString)),
this, SLOT(applyPreferences()));
332 void MainWindow::applyPreferences()
339 if (!isBackendWorking()) {
343 m_gameScene->
showMessage(i18n(
"Backend was changed, restart necessary..."));
348 void MainWindow::showBusy(
bool busy)
353 m_undoMoveAction->setDisabled(
true);
354 m_redoMoveAction->setDisabled(
true);
358 m_undoMoveAction->setDisabled(
false);
361 m_redoMoveAction->setDisabled(
false);
364 m_passMoveAction->setDisabled(busy);
365 m_hintAction->setDisabled(busy);
366 m_undoView->setEnabled(
false);
369 m_gameView->setInteractive(!busy);
372 void MainWindow::showFinishGameAction()
374 m_finishGameAction->setEnabled(
true);
377 void MainWindow::playerChanged()
380 QTimer::singleShot(200,
this, SLOT(generateMove()));
384 void MainWindow::generateMove()
389 void MainWindow::passMovePlayed(
const Player &player)
391 if (player.isComputer()) {
392 if (player.isWhite()) {
400 void MainWindow::setupActions()
403 m_newGameAction = KStandardGameAction::gameNew(
this, SLOT(newGame()), actionCollection());
404 m_loadGameAction = KStandardGameAction::load(
this, SLOT(loadGame()), actionCollection());
405 m_getMoreGamesAction =
new KAction(KIcon( QLatin1String(
"get-hot-new-stuff") ), i18nc(
"@action",
"Get More Games..." ),
this);
406 m_getMoreGamesAction->setShortcut(Qt::CTRL + Qt::Key_G);
407 m_getMoreGamesAction->setToolTip(i18nc(
"@action",
"Get More Games..."));
408 connect(m_getMoreGamesAction, SIGNAL(triggered(
bool)),
this, SLOT(getMoreGames()));
409 actionCollection()->addAction( QLatin1String(
"get_more_games" ), m_getMoreGamesAction);
410 m_saveAction = KStandardGameAction::save(
this, SLOT(saveGame()), actionCollection());
411 KStandardGameAction::quit(
this, SLOT(close()), actionCollection());
413 m_startGameAction =
new KAction(KIcon( QLatin1String(
"media-playback-start") ), i18nc(
"@action",
"Start Game" ),
this);
414 m_startGameAction->setShortcut(Qt::Key_S);
415 m_startGameAction->setToolTip(i18nc(
"@action",
"Start Game"));
416 connect(m_startGameAction, SIGNAL(triggered(
bool)),
this, SLOT(startGame()));
417 actionCollection()->addAction( QLatin1String(
"game_start" ), m_startGameAction);
419 m_finishGameAction =
new KAction(KIcon( QLatin1String(
"media-playback-stop") ), i18nc(
"@action",
"Finish Game" ),
this);
420 m_finishGameAction->setShortcut(Qt::Key_F);
421 m_finishGameAction->setToolTip(i18nc(
"@action",
"Finish Game"));
422 connect(m_finishGameAction, SIGNAL(triggered(
bool)),
this, SLOT(finishGame()));
423 actionCollection()->addAction( QLatin1String(
"game_finish" ), m_finishGameAction);
426 m_undoMoveAction = KStandardGameAction::undo(
this, SLOT(undo()), actionCollection());
427 m_redoMoveAction = KStandardGameAction::redo(
this, SLOT(redo()), actionCollection());
428 m_passMoveAction = KStandardGameAction::endTurn(
this, SLOT(pass()), actionCollection());
429 m_passMoveAction->setText(i18nc(
"@action:inmenu Move",
"Pass Move"));
430 m_passMoveAction->setShortcut(Qt::Key_P);
431 m_hintAction = KStandardGameAction::hint(
this, SLOT(hint()), actionCollection());
434 m_moveNumbersAction =
new KToggleAction(KIcon( QLatin1String(
"lastmoves") ), i18nc(
"@action:inmenu View",
"Show Move &Numbers" ),
this);
435 m_moveNumbersAction->setShortcut(Qt::Key_N);
437 connect(m_moveNumbersAction, SIGNAL(toggled(
bool)), m_gameScene, SLOT(showMoveNumbers(
bool)));
438 actionCollection()->addAction( QLatin1String(
"move_numbers" ), m_moveNumbersAction);
441 KStandardAction::preferences(
this, SLOT(showPreferences()), actionCollection());
444 void MainWindow::setupDockWindows()
447 m_setupDock =
new QDockWidget(i18nc(
"@title:window",
"Game Setup"),
this);
448 m_setupDock->setObjectName( QLatin1String(
"setupDock" ));
449 m_setupDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
450 m_setupWidget =
new SetupWidget(m_game,
this);
451 m_setupDock->setWidget(m_setupWidget);
452 connect(m_setupWidget, SIGNAL(startClicked()),
this, SLOT(startGame()));
456 addDockWidget(Qt::RightDockWidgetArea, m_setupDock);
459 m_gameDock =
new QDockWidget(i18nc(
"@title:window",
"Information"),
this);
460 m_gameDock->setObjectName( QLatin1String(
"gameDock" ));
461 m_gameDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
462 GameWidget *gameWidget =
new GameWidget(m_game,
this);
463 connect(gameWidget, SIGNAL(finishClicked()),
this, SLOT(finishGame()));
464 m_gameDock->setWidget(gameWidget);
465 m_gameDock->toggleViewAction()->setText(i18nc(
"@title:window",
"Information"));
466 m_gameDock->toggleViewAction()->setShortcut(Qt::Key_I);
467 actionCollection()->addAction( QLatin1String(
"show_game_panel" ), m_gameDock->toggleViewAction());
468 addDockWidget(Qt::RightDockWidgetArea, m_gameDock);
471 m_movesDock =
new QDockWidget(i18nc(
"@title:window",
"Moves"),
this);
472 m_movesDock->setObjectName( QLatin1String(
"movesDock" ));
473 m_undoView =
new QUndoView(m_game->
undoStack());
474 m_undoView->setEmptyLabel(i18n(
"No move"));
475 m_undoView->setAlternatingRowColors(
true);
476 m_undoView->setFocusPolicy(Qt::NoFocus);
477 m_undoView->setEditTriggers(QAbstractItemView::NoEditTriggers);
478 m_undoView->setSelectionMode(QAbstractItemView::NoSelection);
479 m_movesDock->setWidget(m_undoView);
480 m_movesDock->toggleViewAction()->setText(i18nc(
"@title:window",
"Moves"));
481 m_movesDock->toggleViewAction()->setShortcut(Qt::Key_M);
482 actionCollection()->addAction( QLatin1String(
"show_moves_panel" ), m_movesDock->toggleViewAction());
483 addDockWidget(Qt::RightDockWidgetArea, m_movesDock);
485 m_errorDock =
new QDockWidget(i18nc(
"@title:window",
"Error"),
this);
486 m_errorDock->setObjectName( QLatin1String(
"errorDock" ));
487 m_errorDock->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
488 ErrorWidget *errorWidget =
new ErrorWidget(
this);
489 connect(errorWidget, SIGNAL(configureClicked()),
this, SLOT(showPreferences()));
490 m_errorDock->setWidget(errorWidget);
494 addDockWidget(Qt::BottomDockWidgetArea, m_errorDock);
497 bool MainWindow::isBackendWorking()
505 #include "moc_mainwindow.cpp"
This class provides a graphical representation of the go game using QGraphicsScene.
static bool showBoardLabels()
Get Determines whether board labels are shown.
Score estimatedScore()
Returns an estimate of the final score based on the current game situation.
bool start(const QString &command="gnugo --mode gtp")
Connect to the given Go game game in GTP mode.
QString engineCommand() const
void showTerritory(bool show)
bool loadTheme(const QString &themeName)
Load the game theme specified by 'name'.
The Player class holds all basic attributes of a Go player.
void showPlacementMarker(bool show)
MainWindow(const QString &fileName="", QWidget *parent=0)
This class represents a view on a Go game view.
static ThemeRenderer * self()
Only one ThemeRenderer is needed per application, this method returns the singleton self...
static QString theme()
Get The graphical theme to be used.
static bool showMoveNumbers()
Get Move numbers are drawn onto stones if enabled.
static Preferences * self()
static QString engineCommand()
Get The current game engine command with (optional) parameters.
The Game class implements the Go game and acts as a wrapper around a remote Go Game game implementing...
bool save(const QString &fileName)
Save the current game as a SGF file.
bool playMove(const Move &move, bool undoable=true)
void showLabels(bool show)
void showMessage(const QString &message, int msecs=2000)
bool generateMove(const Player &player, bool undoable=true)