From 8ae49592c7a140907691e7ec88742e64cccf32ad Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sat, 23 Jan 2021 22:51:04 +0800 Subject: [PATCH] qt: Save settings to settings.ini Do not resetAiPlayers when autoRestart. --- .gitignore | 3 +++ src/option.h | 5 ++++ src/ui/qt/game.cpp | 50 +++++++++++++++++++++++++++++++++++++++- src/ui/qt/game.h | 15 ++++++++++++ src/ui/qt/gamewindow.cpp | 20 ++++++++++++---- 5 files changed, 87 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index af122188..ab0b545b 100644 --- a/.gitignore +++ b/.gitignore @@ -342,6 +342,9 @@ plyInfo.dat preCalculatedVars.dat shortKnotValue.dat +# Settings +settings.ini + # Executable files src/fishmill diff --git a/src/option.h b/src/option.h index 5140ef79..63a7377d 100644 --- a/src/option.h +++ b/src/option.h @@ -70,6 +70,11 @@ public: shufflingEnabled = enabled; } + bool getLearnEndgameEnabled() const noexcept + { + return learnEndgame; + } + void setLearnEndgameEnabled(bool enabled) noexcept { #ifdef ENDGAME_LEARNING_FORCE diff --git a/src/ui/qt/game.cpp b/src/ui/qt/game.cpp index ab12f6c7..83254925 100644 --- a/src/ui/qt/game.cpp +++ b/src/ui/qt/game.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -109,6 +110,35 @@ Game::Game( #endif moveHistory.reserve(256); + + loadSettings(); +} + +void Game::loadSettings() +{ + bool empty = false; + + QFileInfo file(SETTINGS_FILE); + if (!file.exists()) { + cout << SETTINGS_FILE.toStdString() << " is not exists, create it." << endl; + empty = true; + } + + settings = new QSettings(SETTINGS_FILE, QSettings::IniFormat); + + setEngineBlack(empty? false : settings->value("Options/blackIsAiPlayer").toBool()); + setEngineWhite(empty ? true : settings->value("Options/whiteIsAiPlayer").toBool()); + setSound(empty ? true : settings->value("Options/Sound").toBool()); + setShuffling(empty ? true : settings->value("Options/Shuffling").toBool()); + setRule(empty ? DEFAULT_RULE_NUMBER : settings->value("Options/ruleNo").toInt()); + setResignIfMostLose(empty ? false : settings->value("Options/ResignIfMostLose").toBool()); + setOpeningBook(empty ? false : settings->value("Options/OpeningBook").toBool()); + setLearnEndgame(empty ? false : settings->value("Options/LearnEndgameEnabled").toBool()); + setIDS(empty ? false : settings->value("Options/IDS").toBool()); + setDepthExtension(empty ? true : settings->value("Options/DepthExtension").toBool()); + setAutoRestart(empty ? false : settings->value("Options/AutoRestart").toBool()); + setAutoChangeFirstMove(empty ? false : settings->value("Options/AutoChangeFirstMove").toBool()); + setAnimation(empty ? true : settings->value("Options/Animation").toBool()); } Game::~Game() @@ -126,6 +156,9 @@ Game::~Game() #endif /* ENDGAME_LEARNING */ moveHistory.clear(); + + delete settings; + settings = nullptr; } const map Game::getActions() @@ -205,7 +238,7 @@ void Game::gameReset() // Stop threads if (!gameOptions.getAutoRestart()) { pauseThreads(); - resetAiPlayers(); + //resetAiPlayers(); } // Clear pieces @@ -353,6 +386,8 @@ void Game::setRule(int ruleNo, int stepLimited /*= -1*/, int timeLimited /*= -1* // Reset game gameReset(); + + settings->setValue("Options/ruleNo", ruleNo); } void Game::setEngine(Color color, bool enabled) @@ -371,11 +406,13 @@ void Game::setEngine(Color color, bool enabled) void Game::setEngineBlack(bool enabled) { setEngine(BLACK, enabled); + settings->setValue("Options/blackIsAiPlayer", enabled); } void Game::setEngineWhite(bool enabled) { setEngine(WHITE, enabled); + settings->setValue("Options/whiteIsAiPlayer", enabled); } void Game::setAiDepthTime(int time1, int time2) @@ -403,11 +440,14 @@ void Game::setAnimation(bool arg) noexcept durationTime = 500; else durationTime = 0; + + settings->setValue("Options/Animation", arg); } void Game::setSound(bool arg) noexcept { hasSound = arg; + settings->setValue("Options/Sound", arg); } void Game::playSound(GameSound soundType, Color c) @@ -515,26 +555,31 @@ void Game::playSound(GameSound soundType, Color c) void Game::setResignIfMostLose(bool enabled) { gameOptions.setResignIfMostLose(enabled); + settings->setValue("Options/ResignIfMostLose", enabled); } void Game::setAutoRestart(bool enabled) { gameOptions.setAutoRestart(enabled); + settings->setValue("Options/AutoRestart", enabled); } void Game::setAutoChangeFirstMove(bool enabled) { gameOptions.setAutoChangeFirstMove(enabled); + settings->setValue("Options/AutoChangeFirstMove", enabled); } void Game::setShuffling(bool enabled) { gameOptions.setShufflingEnabled(enabled); + settings->setValue("Options/Shuffling", enabled); } void Game::setLearnEndgame(bool enabled) { gameOptions.setLearnEndgameEnabled(enabled); + settings->setValue("Options/LearnEndgameEnabled", enabled); #ifdef ENDGAME_LEARNING if (gameOptions.isEndgameLearningEnabled()) { @@ -546,18 +591,21 @@ void Game::setLearnEndgame(bool enabled) void Game::setIDS(bool enabled) { gameOptions.setIDSEnabled(enabled); + settings->setValue("Options/IDS", enabled); } // DepthExtension void Game::setDepthExtension(bool enabled) { gameOptions.setDepthExtension(enabled); + settings->setValue("Options/DepthExtension", enabled); } // OpeningBook void Game::setOpeningBook(bool enabled) { gameOptions.setOpeningBook(enabled); + settings->setValue("Options/OpeningBook", enabled); } void Game::flip() diff --git a/src/ui/qt/game.h b/src/ui/qt/game.h index 437cc546..28522074 100644 --- a/src/ui/qt/game.h +++ b/src/ui/qt/game.h @@ -35,6 +35,7 @@ #include #include #include +#include #include "position.h" #include "gamescene.h" @@ -411,6 +412,20 @@ private: bool isInverted; public: + const QString SETTINGS_FILE = "settings.ini"; + QSettings *settings {nullptr}; + + void loadSettings(); + + bool soundEnabled() + { + return hasSound; + } + + bool animationEnabled() + { + return hasAnimation; + } // True when the computer takes the lead bool isAiPlayer[COLOR_NB]; diff --git a/src/ui/qt/gamewindow.cpp b/src/ui/qt/gamewindow.cpp index bb5eea43..db116679 100644 --- a/src/ui/qt/gamewindow.cpp +++ b/src/ui/qt/gamewindow.cpp @@ -306,10 +306,8 @@ void MillGameWindow::initialize() connect(game, SIGNAL(statusBarChanged(QString)), statusBarlabel, SLOT(setText(QString))); - ruleNo = DEFAULT_RULE_NUMBER; - ruleActionList[ruleNo]->setChecked(true); - - game->setRule(ruleNo); + ruleActionList[game->getRuleIndex()]->setChecked(true); + game->setRule(game->getRuleIndex()); // Update rule display ruleInfo(); @@ -391,7 +389,19 @@ void MillGameWindow::initialize() connect(ui.pushButton_option, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &))); #endif /* MOBILE_APP_UI */ - ui.actionEngine2_R->setChecked(true); + ui.actionEngine1_T->setChecked(game->isAiPlayer[BLACK]); + ui.actionEngine2_R->setChecked(game->isAiPlayer[WHITE]); + + ui.actionSound_S->setChecked(game->soundEnabled()); + ui.actionAnimation_A->setChecked(game->animationEnabled()); + + ui.actionShuffling_R->setChecked(gameOptions.getShufflingEnabled()); + ui.actionIDS_I->setChecked(gameOptions.getIDSEnabled()); + ui.actionDepthExtension_D->setChecked(gameOptions.getDepthExtension()); + ui.actionResignIfMostLose_G->setChecked(gameOptions.getResignIfMostLose()); + ui.actionAutoRestart_A->setChecked(gameOptions.getAutoRestart()); + ui.actionOpeningBook_O->setChecked(gameOptions.getOpeningBook()); + ui.actionLearnEndgame_E->setChecked(gameOptions.getLearnEndgameEnabled()); } #ifdef MOBILE_APP_UI