Skip to content

Commit 658b570

Browse files
Massive rework of language changing system
1 parent 46a5de7 commit 658b570

20 files changed

+228
-166
lines changed

src/Convert.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,23 @@ namespace Convert
2828
{
2929
QString lowerLocale = locale.toLower();
3030

31-
for(auto it = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); it != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++it)
32-
if (Unsorted::GetLanguageShortName(it.key()) == lowerLocale)
33-
return it.key();
31+
for(auto elem = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); elem != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++elem)
32+
if (Unsorted::GetLanguageShortName(elem.key()) == lowerLocale)
33+
return elem.key();
3434

3535
return Languages::English;
3636
}
3737

3838
Languages ToLangEnum(const std::string& locale) { return LangEnumFromLocale(QString::fromStdString(locale)); }
39-
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }
39+
Languages ToLangEnum(const QString& locale) { return LangEnumFromLocale(locale); }
40+
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }
41+
42+
QString ToQString(const Languages& locale)
43+
{
44+
for(auto elem = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); elem != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++elem)
45+
if (elem.key() == locale)
46+
return elem.value().first;
47+
48+
return "en";
49+
}
4050
}

src/Convert.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77
namespace Convert
88
{
9-
/// @brief Returns list QStringList of QString as element by converting std::list<std::string>value.
9+
/// @brief Returns list `QStringList` of `QString` as element by converting `std::list<std::string>` value.
1010
QStringList ToQStringList(const std::list<std::string>& srcList);
11-
/// @brief Returns list std::list of std::string as element by converting QStringList<QString> value.
11+
/// @brief Returns list `std::list` of `std::string` as element by converting `QStringList<QString>` value.
1212
inline std::list<std::string> ToStdStringList(const QStringList& srcList);
13-
/// @brief Return enum Languages value that equivalent to the text value.
13+
/// @brief Return enum `Languages` value that equivalent to the text value.
1414
Languages ToLangEnum(const std::string& locale);
15-
/// @brief Converts QChar to Qt::Key enum equivalent.
15+
/// @brief Return enum `Languages` value that equivalent to the text value.
16+
Languages ToLangEnum(const QString& locale);
17+
/// @brief Converts `QChar` to `Qt::Key` enum equivalent.
1618
Qt::Key ToQtKey(const QChar& ch);
19+
/// @brief Converts enum `Language` to `QString`.
20+
QString ToQString(const Languages& locale);
1721
}

src/GUI/ActionHotkeyWidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void ActionHotkeyWidget::keyPressEvent(QKeyEvent* event)
6868
}
6969

7070
int key = event->nativeVirtualKey();
71-
if (PROGRAM_CONSTANTS->GetAllowedKeys().contains(static_cast<Qt::Key>(key)))
71+
if (PROGRAM_CONSTANTS->pSettingsFile->GetAllowedKeys().contains(static_cast<Qt::Key>(key)))
7272
{
7373
// Set new text
7474
hotkey = QKeySequence(key).toString();

src/GUI/GreetingWindow.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ GreetingWindow::GreetingWindow(QWidget* parent) : QWidget(parent)
1818
QHBoxLayout* ltButtons = nullptr;
1919
QVBoxLayout* ltMain = nullptr;
2020
QHBoxLayout* ltSettings = nullptr;
21-
QLabel* lblLanguage = nullptr;
22-
QComboBox* cmbLangList = nullptr;
2321
QVBoxLayout* ltLanguages = nullptr;
2422

2523
// Add "New Project" and "Load Project" buttons to the window
@@ -32,17 +30,6 @@ GreetingWindow::GreetingWindow(QWidget* parent) : QWidget(parent)
3230
btnLoadFromFile->setFixedSize(PROGRAM_CONSTANTS->START_BUTTON_SIZE);
3331
btnLoadFromFile->setObjectName(nameof(btnLoadFromFile));
3432
connect(btnLoadFromFile, &QPushButton::clicked, this, &GreetingWindow::btnLoadFromFileClicked);
35-
36-
lblLanguage = new QLabel(tr("LANGUAGE"));
37-
lblLanguage->setObjectName(nameof(lblLanguage));
38-
39-
cmbLangList = new QComboBox();
40-
cmbLangList->setObjectName(nameof(cmbLangList));
41-
for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
42-
cmbLangList->addItem(Unsorted::GetLanguageFullName(static_cast<Languages>(i)));
43-
cmbLangList->setCurrentIndex(static_cast<int>(WINDOW_MANAGER->GetLanguage()));
44-
cmbLangList->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(WINDOW_MANAGER->GetLanguage()).second);
45-
connect(cmbLangList, QOverload<int>::of(&QComboBox::activated), this, &GreetingWindow::languageChanged);
4633

4734
QPushButton* btnSettings = new QPushButton();
4835
QPixmap pxmSettings = QPixmap{PROGRAM_CONSTANTS->GEARS_ICON_FILE};
@@ -54,9 +41,6 @@ GreetingWindow::GreetingWindow(QWidget* parent) : QWidget(parent)
5441

5542
ltLanguages = new QVBoxLayout();
5643
ltLanguages->addStretch(1);
57-
ltLanguages->setSpacing(10);
58-
ltLanguages->addWidget(lblLanguage);
59-
ltLanguages->addWidget(cmbLangList);
6044

6145
ltSettings = new QHBoxLayout();
6246
ltSettings->addLayout(ltLanguages);

src/GUI/GreetingWindow.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ class GreetingWindow : public QWidget
1313
signals:
1414
void btnLoadFromFileClicked();
1515
void btnLoadFromGameClicked();
16-
void languageChanged(int index);
1716
void btnSettingsClicked();
1817
};

src/GUI/HotkeysMainWindow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ void HotkeysMainWindow::HighlightCurrentKeys()
357357
if (keysCollisions.count(thisHotkey) < 2) hotkeyWidget->HighlightKey(false);
358358
else hotkeyWidget->HighlightKey(true);
359359

360-
if (!PROGRAM_CONSTANTS->GetAllowedKeys().contains(Convert::ToQtKey(thisHotkey[0])))
360+
if (!PROGRAM_CONSTANTS->pSettingsFile->GetAllowedKeys().contains(Convert::ToQtKey(thisHotkey[0])))
361361
hotkeyWidget->HighlightKey(true);
362362
}
363363
}
@@ -391,7 +391,7 @@ void HotkeysMainWindow::KeyboardWindow_Update(int id)
391391
if (accum.count(ch) < 2) key->setProperty("status", "good");
392392
else key->setProperty("status", "bad");
393393

394-
if (!PROGRAM_CONSTANTS->GetAllowedKeys().contains(Convert::ToQtKey(ch)))
394+
if (!PROGRAM_CONSTANTS->pSettingsFile->GetAllowedKeys().contains(Convert::ToQtKey(ch)))
395395
key->setProperty("status", "bad");
396396

397397
key->style()->unpolish(key);
@@ -500,8 +500,8 @@ void HotkeysMainWindow::ActLanguage_Triggered()
500500
return;
501501
}
502502

503-
Languages currLang = WINDOW_MANAGER->GetLanguage();
504-
Languages actLang = Languages::English;
503+
Languages currLang = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
504+
Languages actLang = Languages::English;
505505

506506
pWindowToChangeLanguage = new QDialog{this};
507507
pWindowToChangeLanguage->setWindowTitle(tr("Lanugage"));

src/GUI/SetUpWindowsWrapper.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ SetUpWindowsWrapper::SetUpWindowsWrapper(QWidget* parent) : QStackedWidget(paren
2424

2525
void SetUpWindowsWrapper::AttachConnections()
2626
{
27-
connect(pGreetingWidget, &GreetingWindow::languageChanged,
28-
this, &SetUpWindowsWrapper::GreetingWidget_LanguageChanged);
27+
connect(pSettingsWindow, &SettingsWindow::languageChanged,
28+
this, &SetUpWindowsWrapper::SettingsWindow_LanguageChanged);
2929

3030
connect(pGreetingWidget, &GreetingWindow::btnLoadFromFileClicked,
3131
this, &SetUpWindowsWrapper::BtnLoadFromFile_Clicked);
@@ -54,8 +54,8 @@ void SetUpWindowsWrapper::AttachConnections()
5454

5555
void SetUpWindowsWrapper::DetachConnections()
5656
{
57-
disconnect(pGreetingWidget, &GreetingWindow::languageChanged,
58-
this, &SetUpWindowsWrapper::GreetingWidget_LanguageChanged);
57+
disconnect(pSettingsWindow, &SettingsWindow::languageChanged,
58+
this, &SetUpWindowsWrapper::SettingsWindow_LanguageChanged);
5959

6060
disconnect(pGreetingWidget, &GreetingWindow::btnLoadFromFileClicked,
6161
this, &SetUpWindowsWrapper::BtnLoadFromFile_Clicked);
@@ -84,10 +84,10 @@ void SetUpWindowsWrapper::DetachConnections()
8484

8585
void SetUpWindowsWrapper::AddWidgets()
8686
{
87-
pGreetingWidget = new GreetingWindow(this);
88-
pLoadFromTheGameWindow = new LoadFromTheGameWindow(pGreetingWidget);
89-
pLoadFromTheFileWindow = new LoadFromTheFileWindow(pGreetingWidget);
90-
pSettingsWindow = new SettingsWindow(pGreetingWidget);
87+
pGreetingWidget = new GreetingWindow();
88+
pLoadFromTheGameWindow = new LoadFromTheGameWindow();
89+
pLoadFromTheFileWindow = new LoadFromTheFileWindow();
90+
pSettingsWindow = new SettingsWindow();
9191

9292
pGreetingWidget->setFixedSize(size());
9393
pLoadFromTheGameWindow->setFixedSize(size());
@@ -100,11 +100,9 @@ void SetUpWindowsWrapper::AddWidgets()
100100
addWidget(pSettingsWindow);
101101
}
102102

103-
void SetUpWindowsWrapper::GreetingWidget_LanguageChanged(int intLngIndex)
103+
void SetUpWindowsWrapper::SettingsWindow_LanguageChanged()
104104
{
105-
Languages lngType = static_cast<Languages>(intLngIndex);
106-
107-
WINDOW_MANAGER->SetTranslator(lngType);
105+
WINDOW_MANAGER->SetTranslator();
108106

109107
DetachConnections();
110108
pGreetingWidget->deleteLater();
@@ -114,7 +112,7 @@ void SetUpWindowsWrapper::GreetingWidget_LanguageChanged(int intLngIndex)
114112

115113
AddWidgets();
116114
AttachConnections();
117-
setCurrentWidget(pGreetingWidget);
115+
setCurrentWidget(pSettingsWindow);
118116
}
119117

120118
void SetUpWindowsWrapper::BtnLoadFromFile_Clicked() { setCurrentWidget(pLoadFromTheFileWindow); }
@@ -127,7 +125,6 @@ void SetUpWindowsWrapper::LoadFromTheGameWindow_AcceptConfiguration()
127125
// TODO: Make it load vanila Generals
128126
// Also as work with non-ascii paths
129127
// Also as search in big-archives (see more at GZH source code)
130-
// Also as if-protection when `Game\English` doesnt have `generals.csf`
131128
QString path = QString::fromStdString(Registry::GetPathToGame(Registry::Games::GeneralsZeroHour)) + "\\Data\\English\\generals.csf";
132129

133130
if (!QFile::exists(path))
@@ -138,11 +135,11 @@ void SetUpWindowsWrapper::LoadFromTheGameWindow_AcceptConfiguration()
138135
}
139136

140137
WINDOW_MANAGER->SetCSFFilePath(path);
141-
WINDOW_MANAGER->LaunchWidget_AcceptConfiguration();
138+
WINDOW_MANAGER->StartUpWindow_AcceptConfiguration();
142139
}
143140

144141
void SetUpWindowsWrapper::LoadFromTheFileWindow_AcceptConfiguration()
145142
{
146143
WINDOW_MANAGER->SetCSFFilePath(pLoadFromTheFileWindow->findChild<QLineEdit*>("lneFilePath", Qt::FindChildrenRecursively)->text());
147-
WINDOW_MANAGER->LaunchWidget_AcceptConfiguration();
144+
WINDOW_MANAGER->StartUpWindow_AcceptConfiguration();
148145
}

src/GUI/SetUpWindowsWrapper.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class SetUpWindowsWrapper final : public QStackedWidget
2929
private slots:
3030
/// @brief Show `GreetingWindow`, if button `Back` has been clicked.
3131
void BtnBack_Clicked();
32-
/// @brief Change language by its index if language has been changed via select list.
33-
void GreetingWidget_LanguageChanged(int intLngIndex);
32+
/// @brief Change app language.
33+
void SettingsWindow_LanguageChanged();
3434
/// @brief Show `SettingsWindow` if button `btnSettings` has been clicked.
3535
void BtnSettings_Clicked();
3636
/// @brief Show window to load hotkeys information from .csf file.

src/GUI/SettingsWindow.cpp

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,61 @@
22

33
#include "../Logger.hpp"
44
#include "../NameOfExt.hpp"
5+
#include "../ProgramConstants.hpp"
6+
#include "../Unsorted.hpp"
7+
#include "WindowManager.hpp"
58
#include "SettingsWindow.hpp"
69

710
SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)
811
{
9-
ltMain = new QVBoxLayout(this);
12+
ltMain = new QVBoxLayout();
1013
ltButtons = new QHBoxLayout();
11-
ltSettings = new QVBoxLayout();
12-
btnBack = new QPushButton(this);
13-
btnSave = new QPushButton(this);
14-
btnResetAll = new QPushButton(this);
15-
chkEnableDebugConsole = new QCheckBox(this);
16-
chkEnableDiscordRPC = new QCheckBox(this);
17-
chkForceSystemLanguage = new QCheckBox(this);
14+
ltSettings = new QHBoxLayout();
15+
ltLeftColumn = new QVBoxLayout();
16+
ltRightColumn = new QVBoxLayout();
17+
ltLanguage = new QHBoxLayout();
18+
btnBack = new QPushButton();
19+
btnSave = new QPushButton();
20+
btnResetAll = new QPushButton();
21+
chkEnableDebugConsole = new QCheckBox();
22+
chkEnableDiscordRPC = new QCheckBox();
23+
chkForceSystemLanguage = new QCheckBox();
24+
lblLanguage = new QLabel();
25+
cmbLanguage = new QComboBox();
1826

1927
chkEnableDebugConsole->setText(tr("Enable debug console"));
2028
chkEnableDebugConsole->setObjectName(nameof(chkEnableDebugConsole));
21-
if (settings.IsConsoleEnabled())
29+
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())
2230
chkEnableDebugConsole->setCheckState(Qt::CheckState::Checked);
2331
else
2432
chkEnableDebugConsole->setCheckState(Qt::CheckState::Unchecked);
2533

2634
chkEnableDiscordRPC->setText(tr("Enable Discord RPC (WIP)"));
2735
chkEnableDiscordRPC->setObjectName(nameof(chkEnableDiscordRPC));
28-
if (settings.IsDiscordRPCEnabled())
36+
if (PROGRAM_CONSTANTS->pSettingsFile->IsDiscordRPCEnabled())
2937
chkEnableDiscordRPC->setCheckState(Qt::CheckState::Checked);
3038
else
3139
chkEnableDiscordRPC->setCheckState(Qt::CheckState::Unchecked);
3240

33-
// TODO: Make it work later.
34-
chkForceSystemLanguage->setVisible(false);
41+
chkForceSystemLanguage->setText(tr("Force editor use system language"));
42+
chkForceSystemLanguage->setObjectName(nameof(chkForceSystemLanguage));
3543

36-
ltSettings->addWidget(chkEnableDebugConsole);
37-
ltSettings->addWidget(chkEnableDiscordRPC);
44+
lblLanguage->setText(tr("Language:"));
45+
lblLanguage->setObjectName(nameof(lblLanguage));
46+
47+
for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
48+
cmbLanguage->addItem(Unsorted::GetLanguageFullName(static_cast<Languages>(i)));
49+
cmbLanguage->setCurrentIndex(static_cast<int>(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()));
50+
cmbLanguage->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()).second);
51+
52+
ltLanguage->setAlignment(Qt::AlignLeading);
53+
ltLanguage->addWidget(lblLanguage);
54+
ltLanguage->addWidget(cmbLanguage);
55+
56+
ltLeftColumn->addWidget(chkEnableDebugConsole);
57+
ltLeftColumn->addWidget(chkEnableDiscordRPC);
58+
ltLeftColumn->addWidget(chkForceSystemLanguage);
59+
ltLeftColumn->addLayout(ltLanguage);
3860

3961
btnSave->setText(tr("SAVE"));
4062
btnSave->setObjectName(nameof(btnSave));
@@ -48,6 +70,9 @@ SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)
4870
btnBack->setObjectName(nameof(btnBack));
4971
connect(btnBack, &QPushButton::clicked, this, &SettingsWindow::btnBackClicked);
5072

73+
ltSettings->addLayout(ltLeftColumn);
74+
ltSettings->addLayout(ltRightColumn);
75+
5176
ltButtons->addWidget(btnSave);
5277
ltButtons->addWidget(btnResetAll);
5378
ltButtons->addWidget(btnBack);
@@ -62,25 +87,30 @@ SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)
6287

6388
void SettingsWindow::BtnSave_Clicked()
6489
{
65-
settings.SetConsoleStatus(chkEnableDebugConsole->checkState());
90+
PROGRAM_CONSTANTS->pSettingsFile->SetConsoleStatus(chkEnableDebugConsole->checkState());
6691
ConsoleWindowStateUpdate(chkEnableDebugConsole->checkState());
6792

68-
settings.SetDiscordRPCStatus(chkEnableDiscordRPC->checkState());
93+
PROGRAM_CONSTANTS->pSettingsFile->SetDiscordRPCStatus(chkEnableDiscordRPC->checkState());
6994
DiscordRPCStateUpdate(chkEnableDiscordRPC->checkState());
7095

71-
settings.Save();
96+
PROGRAM_CONSTANTS->pSettingsFile->SetLanguage(static_cast<Languages>(cmbLanguage->currentIndex()));
97+
emit languageChanged();
98+
99+
PROGRAM_CONSTANTS->pSettingsFile->Save();
72100
}
73101

74102
void SettingsWindow::BtnResetAll_Clicked()
75103
{
76-
settings.SetToDefault();
104+
PROGRAM_CONSTANTS->pSettingsFile->SetToDefault();
77105

78-
if (settings.IsConsoleEnabled())
106+
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())
79107
chkEnableDebugConsole->setCheckState(Qt::CheckState::Checked);
80108
else
81109
chkEnableDebugConsole->setCheckState(Qt::CheckState::Unchecked);
82110

83111
chkEnableDebugConsole->update();
112+
chkEnableDiscordRPC->update();
113+
chkForceSystemLanguage->update();
84114
}
85115

86116
void SettingsWindow::ConsoleWindowStateUpdate(const Qt::CheckState& state)

src/GUI/SettingsWindow.hpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
#include <QWidget>
33
#include <QPushButton>
44
#include <QCheckBox>
5+
#include <QLabel>
6+
#include <QComboBox>
57
#include <QVBoxLayout>
68
#include "../Settings.hpp"
79

810
class SettingsWindow final : public QWidget
911
{
1012
Q_OBJECT
1113
private: // Data
12-
Settings settings;
13-
14-
QVBoxLayout* ltMain = nullptr;
15-
QHBoxLayout* ltButtons = nullptr;
16-
QVBoxLayout* ltSettings = nullptr;
1714
QPushButton* btnBack = nullptr;
1815
QPushButton* btnSave = nullptr;
1916
QPushButton* btnResetAll = nullptr;
2017
QCheckBox* chkEnableDebugConsole = nullptr;
2118
QCheckBox* chkEnableDiscordRPC = nullptr;
2219
QCheckBox* chkForceSystemLanguage = nullptr;
23-
public:
20+
QLabel* lblLanguage = nullptr;
21+
QComboBox* cmbLanguage = nullptr;
22+
QVBoxLayout* ltMain = nullptr;
23+
QHBoxLayout* ltButtons = nullptr;
24+
QHBoxLayout* ltSettings = nullptr;
25+
QVBoxLayout* ltLeftColumn = nullptr;
26+
QVBoxLayout* ltRightColumn = nullptr;
27+
QHBoxLayout* ltLanguage = nullptr;
28+
2429
private: // Methods
2530
/// @brief Enable/disable debug console.
2631
void ConsoleWindowStateUpdate(const Qt::CheckState& state);
@@ -34,4 +39,5 @@ private slots:
3439
SettingsWindow(QWidget* parent = nullptr);
3540
signals:
3641
void btnBackClicked();
42+
void languageChanged();
3743
};

0 commit comments

Comments
 (0)