Skip to content

Commit d15bef9

Browse files
Implement Settings class
1 parent 5531aec commit d15bef9

11 files changed

+89
-29
lines changed

src/GUI/SettingsWindow.cpp

Whitespace-only changes.

src/GUI/SettingsWindow.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma once

src/GUI/Translations/ru.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -402,22 +402,22 @@
402402
<context>
403403
<name>QObject</name>
404404
<message>
405-
<location filename="../../ProgramConstants.hpp" line="98"/>
405+
<location filename="../../ProgramConstants.hpp" line="105"/>
406406
<source>Buildings</source>
407407
<translation>Здания</translation>
408408
</message>
409409
<message>
410-
<location filename="../../ProgramConstants.hpp" line="99"/>
410+
<location filename="../../ProgramConstants.hpp" line="106"/>
411411
<source>Infantry</source>
412412
<translation>Пехота</translation>
413413
</message>
414414
<message>
415-
<location filename="../../ProgramConstants.hpp" line="100"/>
415+
<location filename="../../ProgramConstants.hpp" line="107"/>
416416
<source>Vehicles</source>
417417
<translation>Техника</translation>
418418
</message>
419419
<message>
420-
<location filename="../../ProgramConstants.hpp" line="101"/>
420+
<location filename="../../ProgramConstants.hpp" line="108"/>
421421
<source>Aircrafts</source>
422422
<translation>Авиация</translation>
423423
</message>

src/GUI/WindowManager.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ WindowManager::WindowManager()
3030
}
3131

3232
LOGMSG("Loading launch window...");
33-
pLaunchWidget = std::make_unique<SetUpWindowsWrapper>();
34-
pLaunchWidget->setWindowTitle(strWindowName);
33+
pStartUpWindow = std::make_unique<SetUpWindowsWrapper>();
34+
pStartUpWindow->setWindowTitle(strWindowName);
3535
LOGMSG("Launch window has been loaded");
3636
}
3737

@@ -44,7 +44,7 @@ void WindowManager::LaunchWidget_AcceptConfiguration(const QVariant& cfg)
4444
pHotkeysEditor = std::make_unique<HotkeysMainWindow>(cfg);
4545
pHotkeysEditor->setWindowTitle(strWindowName);
4646
pHotkeysEditor->show();
47-
pLaunchWidget->close();
47+
pStartUpWindow = nullptr;
4848
bEditorInitialized = true;
4949
LOGMSG("Editor window has been loaded");
5050
}
@@ -63,5 +63,5 @@ void WindowManager::SetTranslator(Languages lngType)
6363
qApp->installTranslator(pAppTranslator);
6464
}
6565

66-
void WindowManager::Show() { pLaunchWidget->show(); }
66+
void WindowManager::Show() { pStartUpWindow->show(); }
6767
Languages WindowManager::GetLanguage() { return Language; }

src/GUI/WindowManager.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class WindowManager final
1313
{
1414
private: // Data
15-
std::unique_ptr<SetUpWindowsWrapper> pLaunchWidget = nullptr;
15+
std::unique_ptr<SetUpWindowsWrapper> pStartUpWindow = nullptr;
1616
std::unique_ptr<HotkeysMainWindow> pHotkeysEditor = nullptr;
1717
inline static QTranslator* pAppTranslator = nullptr;
1818
inline static Languages Language = Languages::English;

src/Main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ int main(int argc, const char** argv)
6666
if (!filesystem::exists(PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER.toStdString().c_str()))
6767
return ShowErrorMessage(PROGRAM_CONSTANTS->TRANSLATIONS_NO_FOUND);
6868

69-
PROGRAM_CONSTANTS->InitializeSettingsJSON();
69+
PROGRAM_CONSTANTS->InitializeFileSettings();
7070

7171
// Hides console
7272
if (!PROGRAM_CONSTANTS->IsConsoleEnabled())

src/Parsers/JSONFile.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ using namespace std;
6464
splitList.removeFirst();
6565
LOGSTM << "Splited and updated query has length : " << splitList.length() << endl;
6666

67-
QJsonObject currObj = jsonObject;
67+
QJsonObject currObj = jsonObject;
6868
QJsonValue currVal;
6969

7070
for (int iter = 0; iter < splitList.length(); iter++)
@@ -75,7 +75,7 @@ using namespace std;
7575
// Current value actually is array
7676
if (currSplit.contains('[') && currSplit.contains(']'))
7777
{
78-
static QRegularExpression regexp{"\\[\\d+\\]"};
78+
const QRegularExpression regexp{"\\[\\d+\\]"};
7979

8080
// Find [xxxx] number of index in array and clear string from [] bracket
8181
int arrayIndex = regexp.match(currSplit).captured(0).remove('[').remove(']').toInt();

src/ProgramConstants.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ ProgramConstants::ProgramConstants()
88
{
99
}
1010

11-
const QSet<Qt::Key>& ProgramConstants::GetAllowedKeys() { return keys; }
12-
bool ProgramConstants::IsConsoleEnabled() { return console; }
13-
void ProgramConstants::InitializeSettingsJSON()
14-
{
15-
JSONFile settings(SETTINGS_FILE);
16-
console = settings.Query("$.DebugConsole").toBool();
17-
18-
for (const QJsonValue& ch : settings.Query("$.AllowedHotkeys").toArray())
19-
keys.insert(KEYBOARD_KEYS.value(ch.toString()[0]));
20-
}
11+
const QSet<Qt::Key> ProgramConstants::GetAllowedKeys() { return SettingsFile->GetAllowedKeys(); }
12+
const bool ProgramConstants::IsConsoleEnabled() { return SettingsFile->IsConsoleEnabled(); }
13+
void ProgramConstants::InitializeFileSettings() { SettingsFile = std::make_unique<Settings>(); }

src/ProgramConstants.hpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <QSize>
77
#include <QObject>
88

9+
#include "Settings.hpp"
10+
911
#define PROGRAM_CONSTANTS ProgramConstants::Instance
1012

1113
enum class GameObjectTypes
@@ -26,10 +28,9 @@ enum class Languages
2628
class ProgramConstants
2729
{
2830
private: // Data
29-
QSet<Qt::Key> keys = {};
30-
bool console = false;
31+
std::unique_ptr<Settings> SettingsFile = nullptr;
3132
public:
32-
inline static std::unique_ptr<ProgramConstants> Instance;
33+
inline static std::unique_ptr<ProgramConstants> Instance = nullptr;
3334

3435
// Folders
3536
const QString RESOURCE_FOLDER = "Resources";
@@ -79,6 +80,13 @@ class ProgramConstants
7980
const QVector<QString> USA_SHORT_NAMES = {"USA", "SWG", "AIR", "LSR"};
8081
const QVector<QString> PRC_SHORT_NAMES = {"PRC", "TNK", "INF", "NUK"};
8182

83+
const QSet<Qt::Key> DEFAULT_ALLOWED_KEYS =
84+
{
85+
Qt::Key_R, Qt::Key_T, Qt::Key_Y, Qt::Key_U, Qt::Key_O, Qt::Key_I, Qt::Key_P,
86+
Qt::Key_A, Qt::Key_S, Qt::Key_D, Qt::Key_F, Qt::Key_G, Qt::Key_H, Qt::Key_J, Qt::Key_K, Qt::Key_L,
87+
Qt::Key_Z, Qt::Key_X, Qt::Key_C, Qt::Key_V, Qt::Key_B, Qt::Key_N, Qt::Key_M
88+
};
89+
8290
const QMap<QChar, Qt::Key> KEYBOARD_KEYS =
8391
{
8492
{'Q', Qt::Key_Q}, {'W', Qt::Key_W}, {'E', Qt::Key_E}, {'R', Qt::Key_R}, {'T', Qt::Key_T}, {'Y', Qt::Key_Y}, {'U', Qt::Key_U}, {'I', Qt::Key_I}, {'O', Qt::Key_O}, {'P', Qt::Key_P},
@@ -92,7 +100,6 @@ class ProgramConstants
92100
{Languages::Russian, {"ru", "Русский"}}
93101
};
94102

95-
96103
const QMap<GameObjectTypes, QString> INGAME_ENTITIES_STRINGS =
97104
{
98105
{GameObjectTypes::Buildings, QObject::tr("Buildings")},
@@ -104,9 +111,9 @@ class ProgramConstants
104111
public: // Methods
105112
ProgramConstants();
106113
/// @brief Parse `Resource\Settings.json`.
107-
void InitializeSettingsJSON();
114+
void InitializeFileSettings();
108115
/// @brief Returns `QSet` of available keys from `QWEWRTY` keyboard to choice by user.
109-
const QSet<Qt::Key>& GetAllowedKeys();
116+
const QSet<Qt::Key> GetAllowedKeys();
110117
/// @brief Returns field status for console from settings file.
111-
bool IsConsoleEnabled();
118+
const bool IsConsoleEnabled();
112119
};

src/Settings.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <QJsonValue>
2+
#include <QJsonArray>
3+
#include <QJsonDocument>
4+
5+
#include "Parsers/JSONFile.hpp"
6+
#include "ProgramConstants.hpp"
7+
#include "Settings.hpp"
8+
9+
Settings::Settings()
10+
{
11+
SetToDefault();
12+
ParseSettings();
13+
}
14+
15+
Settings::~Settings()
16+
{
17+
}
18+
19+
void Settings::SetToDefault()
20+
{
21+
allowedKeys = PROGRAM_CONSTANTS->DEFAULT_ALLOWED_KEYS;
22+
enabledConsole = false;
23+
}
24+
25+
void Settings::ParseSettings()
26+
{
27+
JSONFile json(PROGRAM_CONSTANTS->SETTINGS_FILE);
28+
29+
enabledConsole = json.Query("$.DebugConsole").toBool();
30+
31+
for (const QJsonValue& ch : json.Query("$.AllowedHotkeys").toArray())
32+
allowedKeys.insert(PROGRAM_CONSTANTS->KEYBOARD_KEYS.value(ch.toString()[0]));
33+
}
34+
35+
const bool Settings::IsConsoleEnabled() { return enabledConsole; }
36+
const QSet<Qt::Key> Settings::GetAllowedKeys() { return allowedKeys; }

src/Settings.hpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include <QSet>
3+
4+
class Settings final
5+
{
6+
private: // Data
7+
QSet<Qt::Key> allowedKeys = {};
8+
bool enabledConsole = false;
9+
10+
public: // Method
11+
Settings();
12+
~Settings();
13+
14+
/// @brief Set all `Settings` class variables to default values.
15+
void SetToDefault();
16+
/// @brief Parse `Resources\Settings.json`.
17+
void ParseSettings();
18+
19+
/// @brief Returns field status for console from settings file.
20+
const bool IsConsoleEnabled();
21+
/// @brief Returns `QSet` of available keys from `QWEWRTY` keyboard to choice by user.
22+
const QSet<Qt::Key> GetAllowedKeys();
23+
};

0 commit comments

Comments
 (0)