Skip to content

Commit

Permalink
Massive rework of language changing system
Browse files Browse the repository at this point in the history
  • Loading branch information
MahBoiDeveloper committed Mar 1, 2025
1 parent 46a5de7 commit 658b570
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 166 deletions.
18 changes: 14 additions & 4 deletions src/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ namespace Convert
{
QString lowerLocale = locale.toLower();

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

return Languages::English;
}

Languages ToLangEnum(const std::string& locale) { return LangEnumFromLocale(QString::fromStdString(locale)); }
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }
Languages ToLangEnum(const QString& locale) { return LangEnumFromLocale(locale); }
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }

QString ToQString(const Languages& locale)
{
for(auto elem = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); elem != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++elem)
if (elem.key() == locale)
return elem.value().first;

return "en";
}
}
12 changes: 8 additions & 4 deletions src/Convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

namespace Convert
{
/// @brief Returns list QStringList of QString as element by converting std::list<std::string>value.
/// @brief Returns list `QStringList` of `QString` as element by converting `std::list<std::string>` value.
QStringList ToQStringList(const std::list<std::string>& srcList);
/// @brief Returns list std::list of std::string as element by converting QStringList<QString> value.
/// @brief Returns list `std::list` of `std::string` as element by converting `QStringList<QString>` value.
inline std::list<std::string> ToStdStringList(const QStringList& srcList);
/// @brief Return enum Languages value that equivalent to the text value.
/// @brief Return enum `Languages` value that equivalent to the text value.
Languages ToLangEnum(const std::string& locale);
/// @brief Converts QChar to Qt::Key enum equivalent.
/// @brief Return enum `Languages` value that equivalent to the text value.
Languages ToLangEnum(const QString& locale);
/// @brief Converts `QChar` to `Qt::Key` enum equivalent.
Qt::Key ToQtKey(const QChar& ch);
/// @brief Converts enum `Language` to `QString`.
QString ToQString(const Languages& locale);
}
2 changes: 1 addition & 1 deletion src/GUI/ActionHotkeyWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void ActionHotkeyWidget::keyPressEvent(QKeyEvent* event)
}

int key = event->nativeVirtualKey();
if (PROGRAM_CONSTANTS->GetAllowedKeys().contains(static_cast<Qt::Key>(key)))
if (PROGRAM_CONSTANTS->pSettingsFile->GetAllowedKeys().contains(static_cast<Qt::Key>(key)))
{
// Set new text
hotkey = QKeySequence(key).toString();
Expand Down
16 changes: 0 additions & 16 deletions src/GUI/GreetingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ GreetingWindow::GreetingWindow(QWidget* parent) : QWidget(parent)
QHBoxLayout* ltButtons = nullptr;
QVBoxLayout* ltMain = nullptr;
QHBoxLayout* ltSettings = nullptr;
QLabel* lblLanguage = nullptr;
QComboBox* cmbLangList = nullptr;
QVBoxLayout* ltLanguages = nullptr;

// Add "New Project" and "Load Project" buttons to the window
Expand All @@ -32,17 +30,6 @@ GreetingWindow::GreetingWindow(QWidget* parent) : QWidget(parent)
btnLoadFromFile->setFixedSize(PROGRAM_CONSTANTS->START_BUTTON_SIZE);
btnLoadFromFile->setObjectName(nameof(btnLoadFromFile));
connect(btnLoadFromFile, &QPushButton::clicked, this, &GreetingWindow::btnLoadFromFileClicked);

lblLanguage = new QLabel(tr("LANGUAGE"));
lblLanguage->setObjectName(nameof(lblLanguage));

cmbLangList = new QComboBox();
cmbLangList->setObjectName(nameof(cmbLangList));
for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
cmbLangList->addItem(Unsorted::GetLanguageFullName(static_cast<Languages>(i)));
cmbLangList->setCurrentIndex(static_cast<int>(WINDOW_MANAGER->GetLanguage()));
cmbLangList->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(WINDOW_MANAGER->GetLanguage()).second);
connect(cmbLangList, QOverload<int>::of(&QComboBox::activated), this, &GreetingWindow::languageChanged);

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

ltLanguages = new QVBoxLayout();
ltLanguages->addStretch(1);
ltLanguages->setSpacing(10);
ltLanguages->addWidget(lblLanguage);
ltLanguages->addWidget(cmbLangList);

ltSettings = new QHBoxLayout();
ltSettings->addLayout(ltLanguages);
Expand Down
1 change: 0 additions & 1 deletion src/GUI/GreetingWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ class GreetingWindow : public QWidget
signals:
void btnLoadFromFileClicked();
void btnLoadFromGameClicked();
void languageChanged(int index);
void btnSettingsClicked();
};
8 changes: 4 additions & 4 deletions src/GUI/HotkeysMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void HotkeysMainWindow::HighlightCurrentKeys()
if (keysCollisions.count(thisHotkey) < 2) hotkeyWidget->HighlightKey(false);
else hotkeyWidget->HighlightKey(true);

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

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

key->style()->unpolish(key);
Expand Down Expand Up @@ -500,8 +500,8 @@ void HotkeysMainWindow::ActLanguage_Triggered()
return;
}

Languages currLang = WINDOW_MANAGER->GetLanguage();
Languages actLang = Languages::English;
Languages currLang = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
Languages actLang = Languages::English;

pWindowToChangeLanguage = new QDialog{this};
pWindowToChangeLanguage->setWindowTitle(tr("Lanugage"));
Expand Down
29 changes: 13 additions & 16 deletions src/GUI/SetUpWindowsWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ SetUpWindowsWrapper::SetUpWindowsWrapper(QWidget* parent) : QStackedWidget(paren

void SetUpWindowsWrapper::AttachConnections()
{
connect(pGreetingWidget, &GreetingWindow::languageChanged,
this, &SetUpWindowsWrapper::GreetingWidget_LanguageChanged);
connect(pSettingsWindow, &SettingsWindow::languageChanged,
this, &SetUpWindowsWrapper::SettingsWindow_LanguageChanged);

connect(pGreetingWidget, &GreetingWindow::btnLoadFromFileClicked,
this, &SetUpWindowsWrapper::BtnLoadFromFile_Clicked);
Expand Down Expand Up @@ -54,8 +54,8 @@ void SetUpWindowsWrapper::AttachConnections()

void SetUpWindowsWrapper::DetachConnections()
{
disconnect(pGreetingWidget, &GreetingWindow::languageChanged,
this, &SetUpWindowsWrapper::GreetingWidget_LanguageChanged);
disconnect(pSettingsWindow, &SettingsWindow::languageChanged,
this, &SetUpWindowsWrapper::SettingsWindow_LanguageChanged);

disconnect(pGreetingWidget, &GreetingWindow::btnLoadFromFileClicked,
this, &SetUpWindowsWrapper::BtnLoadFromFile_Clicked);
Expand Down Expand Up @@ -84,10 +84,10 @@ void SetUpWindowsWrapper::DetachConnections()

void SetUpWindowsWrapper::AddWidgets()
{
pGreetingWidget = new GreetingWindow(this);
pLoadFromTheGameWindow = new LoadFromTheGameWindow(pGreetingWidget);
pLoadFromTheFileWindow = new LoadFromTheFileWindow(pGreetingWidget);
pSettingsWindow = new SettingsWindow(pGreetingWidget);
pGreetingWidget = new GreetingWindow();
pLoadFromTheGameWindow = new LoadFromTheGameWindow();
pLoadFromTheFileWindow = new LoadFromTheFileWindow();
pSettingsWindow = new SettingsWindow();

pGreetingWidget->setFixedSize(size());
pLoadFromTheGameWindow->setFixedSize(size());
Expand All @@ -100,11 +100,9 @@ void SetUpWindowsWrapper::AddWidgets()
addWidget(pSettingsWindow);
}

void SetUpWindowsWrapper::GreetingWidget_LanguageChanged(int intLngIndex)
void SetUpWindowsWrapper::SettingsWindow_LanguageChanged()
{
Languages lngType = static_cast<Languages>(intLngIndex);

WINDOW_MANAGER->SetTranslator(lngType);
WINDOW_MANAGER->SetTranslator();

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

AddWidgets();
AttachConnections();
setCurrentWidget(pGreetingWidget);
setCurrentWidget(pSettingsWindow);
}

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

if (!QFile::exists(path))
Expand All @@ -138,11 +135,11 @@ void SetUpWindowsWrapper::LoadFromTheGameWindow_AcceptConfiguration()
}

WINDOW_MANAGER->SetCSFFilePath(path);
WINDOW_MANAGER->LaunchWidget_AcceptConfiguration();
WINDOW_MANAGER->StartUpWindow_AcceptConfiguration();
}

void SetUpWindowsWrapper::LoadFromTheFileWindow_AcceptConfiguration()
{
WINDOW_MANAGER->SetCSFFilePath(pLoadFromTheFileWindow->findChild<QLineEdit*>("lneFilePath", Qt::FindChildrenRecursively)->text());
WINDOW_MANAGER->LaunchWidget_AcceptConfiguration();
WINDOW_MANAGER->StartUpWindow_AcceptConfiguration();
}
4 changes: 2 additions & 2 deletions src/GUI/SetUpWindowsWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class SetUpWindowsWrapper final : public QStackedWidget
private slots:
/// @brief Show `GreetingWindow`, if button `Back` has been clicked.
void BtnBack_Clicked();
/// @brief Change language by its index if language has been changed via select list.
void GreetingWidget_LanguageChanged(int intLngIndex);
/// @brief Change app language.
void SettingsWindow_LanguageChanged();
/// @brief Show `SettingsWindow` if button `btnSettings` has been clicked.
void BtnSettings_Clicked();
/// @brief Show window to load hotkeys information from .csf file.
Expand Down
68 changes: 49 additions & 19 deletions src/GUI/SettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,61 @@

#include "../Logger.hpp"
#include "../NameOfExt.hpp"
#include "../ProgramConstants.hpp"
#include "../Unsorted.hpp"
#include "WindowManager.hpp"
#include "SettingsWindow.hpp"

SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)
{
ltMain = new QVBoxLayout(this);
ltMain = new QVBoxLayout();
ltButtons = new QHBoxLayout();
ltSettings = new QVBoxLayout();
btnBack = new QPushButton(this);
btnSave = new QPushButton(this);
btnResetAll = new QPushButton(this);
chkEnableDebugConsole = new QCheckBox(this);
chkEnableDiscordRPC = new QCheckBox(this);
chkForceSystemLanguage = new QCheckBox(this);
ltSettings = new QHBoxLayout();
ltLeftColumn = new QVBoxLayout();
ltRightColumn = new QVBoxLayout();
ltLanguage = new QHBoxLayout();
btnBack = new QPushButton();
btnSave = new QPushButton();
btnResetAll = new QPushButton();
chkEnableDebugConsole = new QCheckBox();
chkEnableDiscordRPC = new QCheckBox();
chkForceSystemLanguage = new QCheckBox();
lblLanguage = new QLabel();
cmbLanguage = new QComboBox();

chkEnableDebugConsole->setText(tr("Enable debug console"));
chkEnableDebugConsole->setObjectName(nameof(chkEnableDebugConsole));
if (settings.IsConsoleEnabled())
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())
chkEnableDebugConsole->setCheckState(Qt::CheckState::Checked);
else
chkEnableDebugConsole->setCheckState(Qt::CheckState::Unchecked);

chkEnableDiscordRPC->setText(tr("Enable Discord RPC (WIP)"));
chkEnableDiscordRPC->setObjectName(nameof(chkEnableDiscordRPC));
if (settings.IsDiscordRPCEnabled())
if (PROGRAM_CONSTANTS->pSettingsFile->IsDiscordRPCEnabled())
chkEnableDiscordRPC->setCheckState(Qt::CheckState::Checked);
else
chkEnableDiscordRPC->setCheckState(Qt::CheckState::Unchecked);

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

ltSettings->addWidget(chkEnableDebugConsole);
ltSettings->addWidget(chkEnableDiscordRPC);
lblLanguage->setText(tr("Language:"));
lblLanguage->setObjectName(nameof(lblLanguage));

for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
cmbLanguage->addItem(Unsorted::GetLanguageFullName(static_cast<Languages>(i)));
cmbLanguage->setCurrentIndex(static_cast<int>(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()));
cmbLanguage->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()).second);

ltLanguage->setAlignment(Qt::AlignLeading);
ltLanguage->addWidget(lblLanguage);
ltLanguage->addWidget(cmbLanguage);

ltLeftColumn->addWidget(chkEnableDebugConsole);
ltLeftColumn->addWidget(chkEnableDiscordRPC);
ltLeftColumn->addWidget(chkForceSystemLanguage);
ltLeftColumn->addLayout(ltLanguage);

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

ltSettings->addLayout(ltLeftColumn);
ltSettings->addLayout(ltRightColumn);

ltButtons->addWidget(btnSave);
ltButtons->addWidget(btnResetAll);
ltButtons->addWidget(btnBack);
Expand All @@ -62,25 +87,30 @@ SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)

void SettingsWindow::BtnSave_Clicked()
{
settings.SetConsoleStatus(chkEnableDebugConsole->checkState());
PROGRAM_CONSTANTS->pSettingsFile->SetConsoleStatus(chkEnableDebugConsole->checkState());
ConsoleWindowStateUpdate(chkEnableDebugConsole->checkState());

settings.SetDiscordRPCStatus(chkEnableDiscordRPC->checkState());
PROGRAM_CONSTANTS->pSettingsFile->SetDiscordRPCStatus(chkEnableDiscordRPC->checkState());
DiscordRPCStateUpdate(chkEnableDiscordRPC->checkState());

settings.Save();
PROGRAM_CONSTANTS->pSettingsFile->SetLanguage(static_cast<Languages>(cmbLanguage->currentIndex()));
emit languageChanged();

PROGRAM_CONSTANTS->pSettingsFile->Save();
}

void SettingsWindow::BtnResetAll_Clicked()
{
settings.SetToDefault();
PROGRAM_CONSTANTS->pSettingsFile->SetToDefault();

if (settings.IsConsoleEnabled())
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())
chkEnableDebugConsole->setCheckState(Qt::CheckState::Checked);
else
chkEnableDebugConsole->setCheckState(Qt::CheckState::Unchecked);

chkEnableDebugConsole->update();
chkEnableDiscordRPC->update();
chkForceSystemLanguage->update();
}

void SettingsWindow::ConsoleWindowStateUpdate(const Qt::CheckState& state)
Expand Down
18 changes: 12 additions & 6 deletions src/GUI/SettingsWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@
#include <QWidget>
#include <QPushButton>
#include <QCheckBox>
#include <QLabel>
#include <QComboBox>
#include <QVBoxLayout>
#include "../Settings.hpp"

class SettingsWindow final : public QWidget
{
Q_OBJECT
private: // Data
Settings settings;

QVBoxLayout* ltMain = nullptr;
QHBoxLayout* ltButtons = nullptr;
QVBoxLayout* ltSettings = nullptr;
QPushButton* btnBack = nullptr;
QPushButton* btnSave = nullptr;
QPushButton* btnResetAll = nullptr;
QCheckBox* chkEnableDebugConsole = nullptr;
QCheckBox* chkEnableDiscordRPC = nullptr;
QCheckBox* chkForceSystemLanguage = nullptr;
public:
QLabel* lblLanguage = nullptr;
QComboBox* cmbLanguage = nullptr;
QVBoxLayout* ltMain = nullptr;
QHBoxLayout* ltButtons = nullptr;
QHBoxLayout* ltSettings = nullptr;
QVBoxLayout* ltLeftColumn = nullptr;
QVBoxLayout* ltRightColumn = nullptr;
QHBoxLayout* ltLanguage = nullptr;

private: // Methods
/// @brief Enable/disable debug console.
void ConsoleWindowStateUpdate(const Qt::CheckState& state);
Expand All @@ -34,4 +39,5 @@ private slots:
SettingsWindow(QWidget* parent = nullptr);
signals:
void btnBackClicked();
void languageChanged();
};
Loading

0 comments on commit 658b570

Please sign in to comment.