1 /* ColorCode, a free MasterMind clone with built in solver
2 * Copyright (C) 2009 Dirk Laebisch
3 * http://www.laebisch.com/
4 *
5 * ColorCode is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * ColorCode is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with ColorCode. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "settings.h"
20 #include "colorcode.h"
21 #include "ccsolver.h"
22 #include "prevgamesmodel.h"
23 #include "savedgamesmodel.h"
24 #include "highscoresmodel.h"
25
26 using namespace std;
27
GetSettings()28 Settings* GetSettings()
29 {
30 static Settings* s = new Settings();
31 return s;
32 }
33
34 const int Settings::INDICATOR_LETTER = 0;
35 const int Settings::INDICATOR_NUMBER = 1;
36
37 const int Settings::DEF_COLOR_CNT = 8;
38 const int Settings::DEF_PEG_CNT = 4;
39 const int Settings::DEF_DOUBLES = 1;
40 const int Settings::DEF_LIST_MAX_CNT = 15;
41
42 const int Settings::HIGHSCORE_NO = 0;
43 const int Settings::HIGHSCORE_PROMPT = 1;
44 const int Settings::HIGHSCORE_AUTO = 2;
45
46 const int Settings::MAX_LENGTH_USERNAME = 20;
47
48
Settings()49 Settings::Settings()
50 {
51
52 }
53
Init()54 void Settings::Init()
55 {
56 mSettings.setDefaultFormat(QSettings::IniFormat);
57 mSettings.setFallbacksEnabled(false);
58
59 InitSettings();
60 SaveSettingsMap(mDefMap);
61 ReadSettings();
62 SaveSettingsMap(mLastMap);
63
64 emit ReadSettingsGamesDone();
65 }
66
~Settings()67 Settings::~Settings()
68 {
69 }
70
InitSettings()71 void Settings::InitSettings()
72 {
73 mShowToolBar = true;
74 mShowMenuBar = true;
75 mShowStatusBar = false;
76 mShowTimer = true;
77 mShowTenth = true;
78 mShowGameNo = true;
79
80 mShowIndicators = false;
81 mIndicatorType = INDICATOR_LETTER;
82
83 mHideColors = false;
84
85 mPegCnt = DEF_PEG_CNT;
86 mColorCnt = DEF_COLOR_CNT;
87 mDoubles = (bool)DEF_DOUBLES;
88 mGameMode = ColorCode::MODE_HVM;
89 mAutoClose = false;
90 mAutoHints = false;
91 mHintsDelay = 500;
92
93 mSolverStrength = CCSolver::STRENGTH_HIGH;
94
95 mPrevGamesStr = "";
96 mSavedGamesStr = "";
97 mHighGamesStr = "";
98
99 mGamesListMaxCnt = DEF_LIST_MAX_CNT;
100 mCustomRowHColor = false;
101 mRowHColorFg = QApplication::palette().color(QPalette::Active, QPalette::ButtonText).name();
102 mRowHColorBg = QApplication::palette().color(QPalette::Active, QPalette::Button).name();
103 mHighScoreHandling = HIGHSCORE_PROMPT;
104 mHighScoreUserName = "";
105 }
106
GetDefaultsMap() const107 QMap<QString, QVariant> Settings::GetDefaultsMap() const
108 {
109 return mDefMap;
110 }
111
SaveLastSettings()112 void Settings::SaveLastSettings()
113 {
114 SaveSettingsMap(mLastMap);
115 }
116
RestoreLastSettings()117 void Settings::RestoreLastSettings()
118 {
119 RestoreSettingsMap(mLastMap);
120 }
121
RestoreDefSettings()122 void Settings::RestoreDefSettings()
123 {
124 RestoreSettingsMap(mDefMap);
125 }
126
RestoreSettingsMap(QMap<QString,QVariant> & map)127 void Settings::RestoreSettingsMap(QMap<QString, QVariant> &map)
128 {
129 mShowToolBar = map["ShowToolBar"].toBool();
130 mShowMenuBar = map["ShowMenuBar"].toBool();
131 mShowStatusBar = map["ShowStatusBar"].toBool();
132 mShowTimer = map["ShowTimer"].toBool();
133 mShowTenth = map["ShowTenth"].toBool();
134 mShowGameNo = map["ShowGameNo"].toBool();
135
136 mShowIndicators = map["ShowIndicators"].toBool();
137 mIndicatorType = map["IndicatorType"].toInt();
138 mHideColors = map["HideColors"].toBool();
139
140 mPegCnt = map["ColumnCount"].toInt();
141 mColorCnt = map["ColorCount"].toInt();
142 mDoubles = map["Doubles"].toBool();
143
144 mGameMode = map["GameMode"].toInt();
145 mAutoClose = map["AutoClose"].toBool();
146 mAutoHints = map["AutoHints"].toBool();
147 mHintsDelay = map["HintsDelay"].toInt();
148
149 mSolverStrength = map["SolverStrength"].toInt();
150
151 mGamesListMaxCnt = map["GamesListMaxCnt"].toInt();
152 mCustomRowHColor = map["CustomRowHColor"].toBool();
153 mRowHColorFg = map["RowHColorFg"].toString();
154 mRowHColorBg = map["RowHColorBg"].toString();
155 mHighScoreHandling = map["HighScoreHandling"].toInt();
156 mHighScoreUserName = map["HighScoreUserName"].toString();
157
158 Validate();
159 }
160
SaveSettingsMap(QMap<QString,QVariant> & map)161 void Settings::SaveSettingsMap(QMap<QString, QVariant> &map)
162 {
163 map["ShowToolBar"] = mShowToolBar;
164 map["ShowMenuBar"] = mShowMenuBar;
165 map["ShowStatusBar"] = mShowStatusBar;
166 map["ShowTimer"] = mShowTimer;
167 map["ShowTenth"] = mShowTenth;
168 map["ShowGameNo"] = mShowGameNo;
169
170 map["ShowIndicators"] = mShowIndicators;
171 map["IndicatorType"] = mIndicatorType;
172 map["HideColors"] = mHideColors;
173
174 map["ColumnCount"] = mPegCnt;
175 map["ColorCount"] = mColorCnt;
176 map["Doubles"] = mDoubles;
177
178 map["GameMode"] = mGameMode;
179 map["AutoClose"] = mAutoClose;
180 map["AutoHints"] = mAutoHints;
181 map["HintsDelay"] = mHintsDelay;
182
183 map["SolverStrength"] = mSolverStrength;
184
185 map["GamesListMaxCnt"] = mGamesListMaxCnt;
186 map["CustomRowHColor"] = mCustomRowHColor;
187 map["RowHColorFg"] = mRowHColorFg;
188 map["RowHColorBg"] = mRowHColorBg;
189 map["HighScoreHandling"] = mHighScoreHandling;
190 map["HighScoreUserName"] = mHighScoreUserName;
191 }
192
ReadSettings()193 void Settings::ReadSettings()
194 {
195 mSettings.beginGroup("Appearance");
196 mShowToolBar = mSettings.value("ShowToolBar", mShowToolBar).toBool();
197 mShowMenuBar = mSettings.value("ShowMenuBar", mShowMenuBar).toBool();
198 mShowStatusBar = mSettings.value("ShowStatusBar", mShowStatusBar).toBool();
199 mShowTimer = mSettings.value("ShowTimer", mShowTimer).toBool();
200 mShowTenth = mSettings.value("ShowTenth", mShowTenth).toBool();
201 mShowGameNo = mSettings.value("ShowGameNo", mShowGameNo).toBool();
202
203 mShowIndicators = mSettings.value("ShowIndicators", mShowIndicators).toBool();
204 mIndicatorType = mSettings.value("IndicatorType", mIndicatorType).toInt();
205 mHideColors = mSettings.value("HideColors", mHideColors).toBool();
206 mSettings.endGroup();
207
208 mSettings.beginGroup("Game");
209 mPegCnt = mSettings.value("ColumnCount", mPegCnt).toInt();
210 mColorCnt = mSettings.value("ColorCount", mColorCnt).toInt();
211 mDoubles = mSettings.value("Doubles", mDoubles).toBool();
212
213 mGameMode = mSettings.value("GameMode", mGameMode).toInt();
214 mAutoClose = mSettings.value("AutoClose", mAutoClose).toBool();
215 mAutoHints = mSettings.value("AutoHints", mAutoHints).toBool();
216 mHintsDelay = mSettings.value("HintsDelay", mHintsDelay).toInt();
217
218 mSolverStrength = mSettings.value("SolverStrength", mSolverStrength).toInt();
219
220 mSettings.endGroup();
221
222 mSettings.beginGroup("History");
223
224 mPrevGamesStr = mSettings.value("PrevGames", "").toString();
225 mSavedGamesStr = mSettings.value("SavedGames", "").toString();
226 mHighGamesStr = mSettings.value("HighScoreList", "").toString();
227
228 mGamesListMaxCnt = mSettings.value("GamesListMaxCnt", mGamesListMaxCnt).toInt();
229 mCustomRowHColor = mSettings.value("CustomRowHColor", mCustomRowHColor).toBool();
230 mRowHColorFg = mSettings.value("RowHColorFg", mRowHColorFg).toString();
231 mRowHColorBg = mSettings.value("RowHColorBg", mRowHColorBg).toString();
232 mHighScoreHandling = mSettings.value("HighScoreHandling", mHighScoreHandling).toInt();
233 mHighScoreUserName = mSettings.value("HighScoreUserName", mHighScoreUserName).toString();
234
235 mSettings.endGroup();
236
237 Validate();
238 }
239
Validate()240 void Settings::Validate()
241 {
242 if (mIndicatorType != INDICATOR_LETTER && mIndicatorType != INDICATOR_NUMBER)
243 {
244 mIndicatorType = INDICATOR_LETTER;
245 }
246 if (mPegCnt < 2 || mPegCnt > 5)
247 {
248 mPegCnt = 4;
249 }
250 if (mColorCnt < 2 || mColorCnt > 10)
251 {
252 mColorCnt = 8;
253 }
254 if (mGameMode != ColorCode::MODE_HVM && mGameMode != ColorCode::MODE_MVH)
255 {
256 mGameMode = ColorCode::MODE_HVM;
257 }
258 if (mSolverStrength < CCSolver::STRENGTH_LOW || mSolverStrength > CCSolver::STRENGTH_HIGH)
259 {
260 mSolverStrength = CCSolver::STRENGTH_HIGH;
261 }
262 if (mHintsDelay > 10000)
263 {
264 mHintsDelay = 10000;
265 }
266 else if (mHintsDelay < 0)
267 {
268 mHintsDelay = 0;
269 }
270 if (mHighScoreHandling == HIGHSCORE_AUTO && mHighScoreUserName == "")
271 {
272 mHighScoreHandling = HIGHSCORE_PROMPT;
273 }
274 else if (mHighScoreHandling < HIGHSCORE_NO || mHighScoreHandling > HIGHSCORE_AUTO)
275 {
276 mHighScoreHandling = HIGHSCORE_PROMPT;
277 }
278 if (mGamesListMaxCnt < 5 || mGamesListMaxCnt > 50)
279 {
280 mGamesListMaxCnt = DEF_LIST_MAX_CNT;
281 }
282
283 QColor color;
284 color.setNamedColor(mRowHColorFg);
285 if (!color.isValid())
286 {
287 mRowHColorFg = mDefMap["RowHColorFg"].toString();
288 }
289 color.setNamedColor(mRowHColorBg);
290 if (!color.isValid())
291 {
292 mRowHColorBg = mDefMap["RowHColorBg"].toString();
293 }
294 }
295
GetCurSettingsGame() const296 CCGame* Settings::GetCurSettingsGame() const
297 {
298 CCGame* g = new CCGame();
299
300 g->mColorCnt = mColorCnt;
301 g->mPegCnt = mPegCnt;
302 g->mDoubles = int(mDoubles);
303 g->mGameMode = mGameMode;
304 g->mTime = QDateTime::currentDateTime().toTime_t();
305
306 return g;
307 }
308
GetActiveRowHFg() const309 QString Settings::GetActiveRowHFg() const
310 {
311 if (mCustomRowHColor)
312 {
313 return mRowHColorFg;
314 }
315 return mDefMap["RowHColorFg"].toString();
316 }
317
GetActiveRowHBg() const318 QString Settings::GetActiveRowHBg() const
319 {
320 if (mCustomRowHColor)
321 {
322 return mRowHColorBg;
323 }
324 return mDefMap["RowHColorBg"].toString();
325 }
326
WriteSettings()327 void Settings::WriteSettings()
328 {
329 mSettings.beginGroup("Appearance");
330 mSettings.setValue("ShowToolBar", mShowToolBar);
331 mSettings.setValue("ShowMenuBar", mShowMenuBar);
332 mSettings.setValue("ShowStatusBar", mShowStatusBar);
333 mSettings.setValue("ShowTimer", mShowTimer);
334 mSettings.setValue("ShowTenth", mShowTenth);
335 mSettings.setValue("ShowGameNo", mShowGameNo);
336
337 mSettings.setValue("ShowIndicators", mShowIndicators);
338 mSettings.setValue("IndicatorType", mIndicatorType);
339 mSettings.setValue("HideColors", mHideColors);
340 mSettings.endGroup();
341
342 mSettings.beginGroup("Game");
343 mSettings.setValue("ColumnCount", mPegCnt);
344 mSettings.setValue("ColorCount", mColorCnt);
345 mSettings.setValue("Doubles", mDoubles);
346
347 mSettings.setValue("GameMode", mGameMode);
348 mSettings.setValue("AutoClose", mAutoClose);
349 mSettings.setValue("AutoHints", mAutoHints);
350 mSettings.setValue("HintsDelay", mHintsDelay);
351
352 mSettings.setValue("SolverStrength", mSolverStrength);
353 mSettings.endGroup();
354
355 mSettings.beginGroup("History");
356 mSettings.setValue("PrevGames", GetPrevGamesModel()->GetListStr());
357 mSettings.setValue("SavedGames", GetSavedGamesModel()->GetListStr());
358 mSettings.setValue("HighScoreList", GetHighScoresModel()->GetListStr());
359
360 mSettings.setValue("GamesListMaxCnt", mGamesListMaxCnt);
361 mSettings.setValue("CustomRowHColor", mCustomRowHColor);
362 mSettings.setValue("RowHColorFg", mRowHColorFg);
363 mSettings.setValue("RowHColorBg", mRowHColorBg);
364 mSettings.setValue("HighScoreUserName", mHighScoreUserName);
365 mSettings.setValue("HighScoreHandling", mHighScoreHandling);
366
367 mSettings.endGroup();
368
369 mSettings.sync();
370 }
371
372