Skip to content

Commit

Permalink
Move constants to the ProgramConstants class
Browse files Browse the repository at this point in the history
  • Loading branch information
MahBoiDeveloper committed Feb 12, 2025
1 parent 537a4e1 commit 7e7f45a
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Convert
{
QString lowerLocale = locale.toLower();

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

Expand Down
2 changes: 1 addition & 1 deletion src/GUI/ActionHotkeyWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void ActionHotkeyWidget::keyPressEvent(QKeyEvent* event)
}
else
{
if (!KEYBOARD_KEYS.contains(key))
if (!(PROGRAM_CONSTANTS->KEYBOARD_KEYS.contains(key)))
btnHotkey.setText(tr("It isn't character key!"));
else
btnHotkey.setText(tr("This key isn't allowed!"));
Expand Down
4 changes: 2 additions & 2 deletions src/GUI/Faction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ QMap<Faction::GameObject, GameObjectTypes> Faction::ParseJsonObject(const QJsonO
QMap<GameObject, GameObjectTypes> tmpMap;

// Circle for each element in {"Buildings", "Infantry", "Vehicles", "Aircrafts"} map
for(const QString& qstrObjectsArray : ENTITIES_STRINGS)
for(const QString& qstrObjectsArray : PROGRAM_CONSTANTS->ENTITIES_STRINGS)
{
QJsonArray currArr = obj[qstrObjectsArray].toArray();

Expand All @@ -78,7 +78,7 @@ QMap<Faction::GameObject, GameObjectTypes> Faction::ParseJsonObject(const QJsonO
_layouts.push_back(_layout);
}

tmpMap.insert(Faction::GameObject{_name, _ingameName, _layouts}, ENTITIES_STRINGS.key(qstrObjectsArray));
tmpMap.insert(Faction::GameObject{_name, _ingameName, _layouts}, PROGRAM_CONSTANTS->ENTITIES_STRINGS.key(qstrObjectsArray));
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/GUI/GreetingWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ GreetingWidget::GreetingWidget(QWidget* parent) : QWidget(parent)

// Add "New Project" and "Load Project" buttons to the window
btnNewProject = new QPushButton(tr("NEW") + '\n' + tr("PROJECT"));
btnNewProject->setFixedSize(START_BUTTON_SIZE);
btnNewProject->setFixedSize(PROGRAM_CONSTANTS->START_BUTTON_SIZE);
connect(btnNewProject, &QPushButton::clicked, this, [this](bool)
{
emit pressed(GreetingWidget::StandartButtons::NewProject);
});

btnLoadProject = new QPushButton(tr("LOAD") + '\n' + tr("PROJECT"));
btnLoadProject->setFixedSize(START_BUTTON_SIZE);
btnLoadProject->setFixedSize(PROGRAM_CONSTANTS->START_BUTTON_SIZE);
connect(btnLoadProject, &QPushButton::clicked, this, [this](bool)
{
emit pressed(GreetingWidget::StandartButtons::LoadProject);
Expand All @@ -45,10 +45,9 @@ GreetingWidget::GreetingWidget(QWidget* parent) : QWidget(parent)
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(LANGUAGES_STRINGS.value(WINDOW_MANAGER->GetLanguage()).second);
cmbLangList->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(WINDOW_MANAGER->GetLanguage()).second);
connect(cmbLangList, QOverload<int>::of(&QComboBox::activated), this, &GreetingWidget::languageChanged);


ltLanguages = new QVBoxLayout();
ltLanguages->addStretch(1);
ltLanguages->setSpacing(10);
Expand Down
26 changes: 13 additions & 13 deletions src/GUI/HotkeysMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ HotkeysMainWindow::HotkeysMainWindow(const QVariant& configuration, QWidget* par
// Enable smooth scrolling
pEntitiesTreeWidget->setVerticalScrollMode(QTreeWidget::ScrollMode::ScrollPerPixel);
// Set icon size
pEntitiesTreeWidget->setIconSize(QSize{ICON_MIN_HEIGHT, ICON_MIN_HEIGHT});
pEntitiesTreeWidget->setIconSize(QSize{PROGRAM_CONSTANTS->ICON_MIN_HEIGHT, PROGRAM_CONSTANTS->ICON_MIN_HEIGHT});

connect(pEntitiesTreeWidget, &QTreeWidget::itemSelectionChanged, this, &HotkeysMainWindow::SetHotkeysPanels);

Expand All @@ -56,16 +56,16 @@ HotkeysMainWindow::HotkeysMainWindow(const QVariant& configuration, QWidget* par
for (int i = 0; i < 4; ++i)
{
const Faction currFaction = factionVector.at(sectionIndex + i);

QPushButton* factionButton = new QPushButton{currFaction.GetDisplayName()};

auto shortName = currFaction.GetShortName();
if (vUSAShorNames.contains(shortName)) ;
if (PROGRAM_CONSTANTS->USA_SHORT_NAMES.contains(shortName)) ;

if (vPRCShorNames.contains(shortName))
if (PROGRAM_CONSTANTS->PRC_SHORT_NAMES.contains(shortName))
factionButton->setProperty("faction", "PRC");
if (vGLAShorNames.contains(shortName))

if (PROGRAM_CONSTANTS->GLA_SHORT_NAMES.contains(shortName))
factionButton->setProperty("faction", "GLA");

connect(factionButton, &QPushButton::pressed, this, [=, this]()
Expand All @@ -74,7 +74,7 @@ HotkeysMainWindow::HotkeysMainWindow(const QVariant& configuration, QWidget* par
});

pFactionsButtonsGroup->addButton(factionButton);

if (i == 0) // main faction
ltCurrentFaction->addWidget(factionButton);
else // subfactions
Expand Down Expand Up @@ -117,7 +117,7 @@ HotkeysMainWindow::HotkeysMainWindow(const QVariant& configuration, QWidget* par

QPushButton* btnEmptyButton= new QPushButton();
btnEmptyButton->setProperty("key", "null");
btnEmptyButton->setFixedWidth(EMPTY_KEY_WIDTH);
btnEmptyButton->setFixedWidth(PROGRAM_CONSTANTS->EMPTY_KEY_WIDTH);

pKeyboardFirstLine = CreateKeysOnKeyboard("QWERTYUIOP");
pKeyboardSecondLine = CreateKeysOnKeyboard("ASDFGHJKL");
Expand Down Expand Up @@ -201,15 +201,15 @@ void HotkeysMainWindow::SetGameObjectList(const QString& factionShortName)
if(goMap.isEmpty()) return;

// Create sections for all faction entities types
for(const auto& objectType : ENTITIES_STRINGS.keys())
for(const auto& objectType : PROGRAM_CONSTANTS->ENTITIES_STRINGS.keys())
{
// Create new section of tree list
QTreeWidgetItem* newTopEntityItem = new QTreeWidgetItem();
newTopEntityItem->setText(0, QCoreApplication::translate("QObject", ENTITIES_STRINGS.value(objectType).toUtf8().constData()));
newTopEntityItem->setText(0, QCoreApplication::translate("QObject", PROGRAM_CONSTANTS->ENTITIES_STRINGS.value(objectType).toUtf8().constData()));

// Decorate
newTopEntityItem->setIcon(0, ImageManager::GetGameObjectTypePixmap(objectType)
.scaledToHeight(ICON_SCALING_HEIGHT, Qt::SmoothTransformation));
.scaledToHeight(PROGRAM_CONSTANTS->ICON_SCALING_HEIGHT, Qt::SmoothTransformation));

// If there no objects by type - then skip
if (goMap.keys(objectType).isEmpty()) continue;
Expand Down Expand Up @@ -566,7 +566,7 @@ QHBoxLayout* HotkeysMainWindow::CreateKeysOnKeyboard(const QString& str)
auto tmp = new QPushButton(ch);
tmp->setProperty("key", ch);
tmp->setObjectName(ch);
tmp->setFixedWidth(KEYBOARD_KEY_WIDTH);
tmp->setFixedWidth(PROGRAM_CONSTANTS->KEYBOARD_KEY_WIDTH);
pKeys->addWidget(tmp);
}
return pKeys;
Expand Down
2 changes: 1 addition & 1 deletion src/GUI/HotkeysMainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class HotkeysMainWindow final : public QMainWindow
Q_OBJECT

private: // Data
JSONFile TECH_TREE_SOURCE{TECH_TREE_PATH};
JSONFile TECH_TREE_SOURCE{PROGRAM_CONSTANTS->TECH_TREE_PATH};

QVector<Faction> factionVector;

Expand Down
10 changes: 5 additions & 5 deletions src/GUI/ImageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ QImage ImageManager::DecodeWebpIcon(const QString& iconName)
if (it != ImagesCache.constEnd()) return it.value();

// Find
const QFileInfo targetIconFile = FindIconFile(ICONS_FOLDER, iconName);
const QFileInfo targetIconFile = FindIconFile(PROGRAM_CONSTANTS->ICONS_FOLDER, iconName);

if (targetIconFile.exists())
{
Expand All @@ -30,13 +30,13 @@ QImage ImageManager::DecodeWebpIcon(const QString& iconName)
}
}

QImage ImageManager::DecodeMissingWebpIcon() { return DecodeWebpIconPath(MISSING_ICON_PATH); }
QImage ImageManager::DecodeEditorWebpIcon() { return DecodeWebpIconPath(EDITOR_ICON_PATH); }
QImage ImageManager::DecodeBigEditorWebpIcon() { return DecodeWebpIconPath(EDITOR_BIG_ICON_PATH); }
QImage ImageManager::DecodeMissingWebpIcon() { return DecodeWebpIconPath(PROGRAM_CONSTANTS->MISSING_ICON_PATH); }
QImage ImageManager::DecodeEditorWebpIcon() { return DecodeWebpIconPath(PROGRAM_CONSTANTS->EDITOR_ICON_PATH); }
QImage ImageManager::DecodeBigEditorWebpIcon() { return DecodeWebpIconPath(PROGRAM_CONSTANTS->EDITOR_BIG_ICON_PATH); }

QPixmap ImageManager::GetGameObjectTypePixmap(GameObjectTypes entityType)
{
return QPixmap{QT_ICONS_FOLDER + "/" + ENTITIES_STRINGS.value(entityType) + ".webp"};
return QPixmap{PROGRAM_CONSTANTS->QT_ICONS_FOLDER + "/" + PROGRAM_CONSTANTS->ENTITIES_STRINGS.value(entityType) + ".webp"};
}

QFileInfo ImageManager::FindIconFile(const QString& pathToIconsDir, const QString& fileBaseName)
Expand Down
8 changes: 4 additions & 4 deletions src/GUI/Translations/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,22 @@
<context>
<name>QObject</name>
<message>
<location filename="../../ProgramConstants.hpp" line="50"/>
<location filename="../../ProgramConstants.hpp" line="79"/>
<source>Buildings</source>
<translation>Здания</translation>
</message>
<message>
<location filename="../../ProgramConstants.hpp" line="51"/>
<location filename="../../ProgramConstants.hpp" line="80"/>
<source>Infantry</source>
<translation>Пехота</translation>
</message>
<message>
<location filename="../../ProgramConstants.hpp" line="52"/>
<location filename="../../ProgramConstants.hpp" line="81"/>
<source>Vehicles</source>
<translation>Техника</translation>
</message>
<message>
<location filename="../../ProgramConstants.hpp" line="53"/>
<location filename="../../ProgramConstants.hpp" line="82"/>
<source>Aircrafts</source>
<translation>Авиация</translation>
</message>
Expand Down
13 changes: 9 additions & 4 deletions src/GUI/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

#include "../Logger.hpp"
#include "../Unsorted.hpp"
#include "../Convert.hpp"
#include "../Registry.hpp"

#include "ImageManager.hpp"
#include "WindowManager.hpp"

WindowManager::WindowManager()
{
Language = Convert::ToLangEnum(Registry::GetCurrentUserLanguage());
SetTranslator(Language);

qApp->setWindowIcon(QIcon(QPixmap::fromImage(ImageManager::DecodeEditorWebpIcon())));

LOGMSG("Loading \"" + STYLES_SHEET + "\"...");
QFile css{STYLES_SHEET};
LOGMSG("Loading \"" + PROGRAM_CONSTANTS->STYLES_SHEET + "\"...");
QFile css{PROGRAM_CONSTANTS->STYLES_SHEET};
if (css.open(QIODevice::ReadOnly))
{
qApp->setStyleSheet(css.readAll());
Expand All @@ -33,7 +38,7 @@ WindowManager::WindowManager()
void WindowManager::LaunchWidget_AcceptConfiguration(const QVariant& cfg)
{
// 2nd init protection
if (bEditorInitialized) return;
if (pHotkeysEditor != nullptr) return;

LOGMSG("Loading editor window...");
pHotkeysEditor = std::make_unique<HotkeysMainWindow>(cfg);
Expand All @@ -54,7 +59,7 @@ void WindowManager::SetTranslator(Languages lngType)

Language = lngType;
pAppTranslator = new QTranslator();
pAppTranslator->load(lngShortName, TRANSLATIONS_FOLDER);
pAppTranslator->load(lngShortName, PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER);
qApp->installTranslator(pAppTranslator);
}

Expand Down
4 changes: 1 addition & 3 deletions src/GUI/WindowManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <QTranslator>

#include "../ProgramConstants.hpp"
#include "../Convert.hpp"
#include "../Registry.hpp"

#include "LaunchWidget.hpp"
#include "HotkeysMainWindow.hpp"
Expand All @@ -17,7 +15,7 @@ class WindowManager final
std::unique_ptr<LaunchWidget> pLaunchWidget = nullptr;
std::unique_ptr<HotkeysMainWindow> pHotkeysEditor = nullptr;
inline static QTranslator* pAppTranslator = nullptr;
inline static Languages Language = Languages::English; // Convert::ToLangEnum(Registry::GetCurrentUserLanguage());
inline static Languages Language = Languages::English;
QString strWindowName = "C&C: Generals Zero Hour Hotkey Editor";
bool bEditorInitialized = false;
public:
Expand Down
37 changes: 20 additions & 17 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,47 @@ int main(int argc, const char** argv)
// After this all out text to console MUST be showed via std::wcout and all chars should be converted as wchar_t
// _setmode(_fileno(stdout), _O_U16TEXT);

// Call parsing the Settings.json
PROGRAM_CONSTANTS = make_unique<ProgramConstants>();

// Initialize main Qt application
QApplication HotkeyEditor(argc, const_cast<char**>(argv));

QString workingDirectory = QString::fromStdWString(filesystem::current_path().c_str());

if (workingDirectory.indexOf(BINARIES_FOLDER) != -1)
if (workingDirectory.indexOf(PROGRAM_CONSTANTS->BINARIES_FOLDER) != -1)
{
LOGMSG("Program started from Resources\\Binaries folder. Redirect working directory to the ..\\..\\");
LOGMSG("Before redirect: " + workingDirectory);
LOGMSG("After redirect: " + workingDirectory.replace(BINARIES_FOLDER, "", Qt::CaseSensitive));
LOGMSG("After redirect: " + workingDirectory.replace(PROGRAM_CONSTANTS->BINARIES_FOLDER, "", Qt::CaseSensitive));
filesystem::current_path(workingDirectory.toStdWString());
}

if (!filesystem::exists(SETTINGS_PATH.toStdString().c_str()))
return ShowErrorMessage(SETTINGS_NO_FOUND);
if (!filesystem::exists(PROGRAM_CONSTANTS->SETTINGS_PATH.toStdString().c_str()))
return ShowErrorMessage(PROGRAM_CONSTANTS->SETTINGS_NO_FOUND);

if (!filesystem::exists(TECH_TREE_PATH.toStdString().c_str()))
return ShowErrorMessage(TECH_TREE_NO_FOUND);
if (!filesystem::exists(PROGRAM_CONSTANTS->TECH_TREE_PATH.toStdString().c_str()))
return ShowErrorMessage(PROGRAM_CONSTANTS->TECH_TREE_NO_FOUND);

if (!filesystem::exists(ICONS_FOLDER.toStdString().c_str()))
return ShowErrorMessage(ICONS_FOLDER_NO_FOUND);
if (!filesystem::exists(PROGRAM_CONSTANTS->ICONS_FOLDER.toStdString().c_str()))
return ShowErrorMessage(PROGRAM_CONSTANTS->ICONS_FOLDER_NO_FOUND);

if (!filesystem::exists(THEME_FOLDER.toStdString().c_str()))
return ShowErrorMessage(THEME_FOLDER_NO_FOUND);
if (!filesystem::exists(PROGRAM_CONSTANTS->THEME_FOLDER.toStdString().c_str()))
return ShowErrorMessage(PROGRAM_CONSTANTS->THEME_FOLDER_NO_FOUND);

if (!filesystem::exists(TRANSLATIONS_FOLDER.toStdString().c_str()))
return ShowErrorMessage(TRANSLATIONS_NO_FOUND);
if (!filesystem::exists(PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER.toStdString().c_str()))
return ShowErrorMessage(PROGRAM_CONSTANTS->TRANSLATIONS_NO_FOUND);

// Call parsing the Settings.json
PROGRAM_CONSTANTS = make_unique<ProgramConstants>();
PROGRAM_CONSTANTS->InitializeSettingsJSON();

// Hides console
if (!PROGRAM_CONSTANTS->IsConsoleEnabled())
ShowWindow(GetConsoleWindow(), SW_HIDE);

// Define logger as a singleton class, that could be used anywhere in project
// Define logger as the singleton class, that could be used anywhere in the project
WINDOW_MANAGER = make_unique<WindowManager>();
CSF_PARSER->Parse(RESOURCE_FOLDER + "/DataSamples/generalsRU.csf");
// TODO: Delete later
CSF_PARSER->Parse(PROGRAM_CONSTANTS->RESOURCE_FOLDER + "/DataSamples/generalsRU.csf");

try
{
Expand All @@ -83,7 +86,7 @@ int main(int argc, const char** argv)
catch (const char* msg) { return ShowErrorMessage(msg); }
catch (const string& msg) { return ShowErrorMessage(msg.c_str()); }
catch (const QString& msg) { return ShowErrorMessage(msg.toStdString().c_str()); }
catch (...) { return ShowErrorMessage(UNKNOWN_ERROR); }
catch (...) { return ShowErrorMessage(PROGRAM_CONSTANTS->UNKNOWN_ERROR); }

return 0;
}
9 changes: 6 additions & 3 deletions src/ProgramConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
#include "ProgramConstants.hpp"

ProgramConstants::ProgramConstants()
{
}

const QSet<Qt::Key>& ProgramConstants::GetAllowedKeys() { return keys; }
bool ProgramConstants::IsConsoleEnabled() { return console; }
void ProgramConstants::InitializeSettingsJSON()
{
JSONFile settings(SETTINGS_PATH);
console = settings.Query("$.DebugConsole").toBool();

for (const QJsonValue& ch : settings.Query("$.AllowedHotkeys").toArray())
keys.insert(KEYBOARD_KEYS.value(ch.toString()[0]));
}

const QSet<Qt::Key>& ProgramConstants::GetAllowedKeys() { return keys; }
bool ProgramConstants::IsConsoleEnabled() { return console; }
Loading

0 comments on commit 7e7f45a

Please sign in to comment.