Skip to content

Commit 537a4e1

Browse files
Refactoring dependencies between classes
1 parent 8a3712e commit 537a4e1

File tree

11 files changed

+91
-71
lines changed

11 files changed

+91
-71
lines changed

src/GUI/BaseConfigurationDialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BaseConfigurationDialog::BaseConfigurationDialog(QWidget* parent) : QDialog(pare
1212
// emit accepted configurations
1313
connect(&dialogButtons, &QDialogButtonBox::accepted, this, [=, this]()
1414
{
15-
emit AcceptedConfiguration(CreateConfigurationData());
15+
emit acceptConfiguration(CreateConfigurationData());
1616
});
1717

1818
connect(&dialogButtons, &QDialogButtonBox::rejected, this, &QDialog::deleteLater);

src/GUI/BaseConfigurationDialog.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ class BaseConfigurationDialog : public QDialog
1818
virtual QVariant CreateConfigurationData() = 0;
1919

2020
signals:
21-
void AcceptedConfiguration(QVariant configuration);
21+
void acceptConfiguration(QVariant configuration);
2222
};

src/GUI/CreationDialog.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class CreationDialog : public BaseConfigurationDialog
66
{
77
Q_OBJECT
8-
98
private: // Data
109
QCheckBox saveToGameBox;
1110

src/GUI/GreetingWidget.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
#include <QVBoxLayout>
55

66
#include "../Logger.hpp"
7+
#include "../ProgramConstants.hpp"
78
#include "../Unsorted.hpp"
9+
#include "WindowManager.hpp"
810
#include "ImageManager.hpp"
911
#include "GreetingWidget.hpp"
1012

11-
GreetingWidget::GreetingWidget(Languages language, QWidget* parent) : QWidget(parent)
13+
GreetingWidget::GreetingWidget(QWidget* parent) : QWidget(parent)
1214
{
1315
QPushButton* btnNewProject = nullptr;
1416
QPushButton* btnLoadProject = nullptr;
@@ -42,9 +44,11 @@ GreetingWidget::GreetingWidget(Languages language, QWidget* parent) : QWidget(pa
4244
cmbLangList = new QComboBox();
4345
for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
4446
cmbLangList->addItem(Unsorted::GetLanguageFullName(static_cast<Languages>(i)));
45-
cmbLangList->setCurrentIndex(static_cast<int>(language));
47+
cmbLangList->setCurrentIndex(static_cast<int>(WINDOW_MANAGER->GetLanguage()));
48+
cmbLangList->setCurrentText(LANGUAGES_STRINGS.value(WINDOW_MANAGER->GetLanguage()).second);
4649
connect(cmbLangList, QOverload<int>::of(&QComboBox::activated), this, &GreetingWidget::languageChanged);
4750

51+
4852
ltLanguages = new QVBoxLayout();
4953
ltLanguages->addStretch(1);
5054
ltLanguages->setSpacing(10);

src/GUI/GreetingWidget.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class GreetingWidget : public QWidget
66
{
77
Q_OBJECT
8-
98
public: // Data
109
enum class StandartButtons
1110
{
@@ -14,7 +13,7 @@ class GreetingWidget : public QWidget
1413
};
1514

1615
public: // Methods
17-
GreetingWidget(Languages language = Languages::English, QWidget* parent = nullptr);
16+
GreetingWidget(QWidget* parent = nullptr);
1817
private:
1918
int GetGreetingTextAverageSize(const QString& text) const;
2019

src/GUI/LaunchWidget.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,40 @@
33
#include "CreationDialog.hpp"
44
#include "LoadDialog.hpp"
55
#include "WindowManager.hpp"
6+
#include "LaunchWidget.hpp"
67

7-
LaunchWidget::LaunchWidget(Languages lngType, QWidget* parent) : QStackedWidget(parent)
8+
LaunchWidget::LaunchWidget(QWidget* parent) : QStackedWidget(parent)
89
{
910
// MainLaunchWidget settings
1011
setFixedSize(795, 440);
1112
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
1213
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint &
1314
~Qt::WindowMinimizeButtonHint);
1415

15-
WINDOW_MANAGER->SetTranslator(lngType);
16-
pStartWidget = new GreetingWidget{lngType};
17-
addWidget(pStartWidget);
18-
UpdateConnectionsToSignals();
16+
pGreetingWidget = new GreetingWidget(this);
17+
addWidget(pGreetingWidget);
18+
AttachConnections();
19+
}
20+
21+
void LaunchWidget::AttachConnections()
22+
{
23+
connect(pGreetingWidget, &GreetingWidget::languageChanged,
24+
this, &LaunchWidget::GreetingWidget_LanguageChanged);
25+
26+
connect(pGreetingWidget, &GreetingWidget::pressed,
27+
this, &LaunchWidget::BtnNewProjectOrBtnLoadProject_Clicked);
1928
}
2029

21-
void LaunchWidget::UpdateConnectionsToSignals()
30+
void LaunchWidget::DetachConnections()
2231
{
23-
connect(pStartWidget, &GreetingWidget::languageChanged,
24-
this, &LaunchWidget::OnChangeLanguage);
25-
connect(pStartWidget, &GreetingWidget::pressed,
26-
this, &LaunchWidget::OnStartButtonClicked);
32+
disconnect(pGreetingWidget, &GreetingWidget::languageChanged,
33+
this, &LaunchWidget::GreetingWidget_LanguageChanged);
34+
35+
disconnect(pGreetingWidget, &GreetingWidget::pressed,
36+
this, &LaunchWidget::BtnNewProjectOrBtnLoadProject_Clicked);
2737
}
2838

29-
void LaunchWidget::OnChangeLanguage(int intLngIndex)
39+
void LaunchWidget::GreetingWidget_LanguageChanged(int intLngIndex)
3040
{
3141
// Find language type by its code.
3242
Languages lngType = static_cast<Languages>(intLngIndex);
@@ -35,13 +45,17 @@ void LaunchWidget::OnChangeLanguage(int intLngIndex)
3545
WINDOW_MANAGER->SetTranslator(lngType);
3646

3747
// Recreate StartWidget and update connections.
38-
pStartWidget->deleteLater();
39-
pStartWidget = new GreetingWidget{lngType};
40-
addWidget(pStartWidget);
41-
UpdateConnectionsToSignals();
48+
DetachConnections();
49+
removeWidget(pGreetingWidget);
50+
pGreetingWidget->deleteLater();
51+
52+
pGreetingWidget = new GreetingWidget(this);
53+
addWidget(pGreetingWidget);
54+
setCurrentWidget(pGreetingWidget);
55+
AttachConnections();
4256
}
4357

44-
void LaunchWidget::OnStartButtonClicked(GreetingWidget::StandartButtons standartButton)
58+
void LaunchWidget::BtnNewProjectOrBtnLoadProject_Clicked(GreetingWidget::StandartButtons standartButton)
4559
{
4660
BaseConfigurationDialog* pConfigurationWidget = nullptr;
4761

@@ -61,5 +75,7 @@ void LaunchWidget::OnStartButtonClicked(GreetingWidget::StandartButtons standart
6175
setCurrentWidget(pConfigurationWidget); // next window (creator)
6276

6377
// if accepted -> send signal with configuration
64-
connect(pConfigurationWidget, &CreationDialog::AcceptedConfiguration, this, &LaunchWidget::AcceptedConfiguration);
78+
connect(pConfigurationWidget, &CreationDialog::acceptConfiguration, this, &LaunchWidget::CreationDialog_AcceptConfiguration);
6579
}
80+
81+
void LaunchWidget::CreationDialog_AcceptConfiguration(const QVariant& cfg) { WINDOW_MANAGER->LaunchWidget_AcceptConfiguration(cfg); }

src/GUI/LaunchWidget.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
#include <QStackedWidget>
33
#include "GreetingWidget.hpp"
44

5-
class LaunchWidget : public QStackedWidget
5+
class LaunchWidget final : public QStackedWidget
66
{
77
Q_OBJECT
88
private: // Data
9-
GreetingWidget* pStartWidget = nullptr;
9+
GreetingWidget* pGreetingWidget = nullptr;
1010

1111
private: // Methods
12-
/// @brief Updates connections with new pointers to the translator and start widget.
13-
void UpdateConnectionsToSignals();
12+
/// @brief Connects slots and singals.
13+
void AttachConnections();
14+
/// @brief Disconnects slots and signals.
15+
void DetachConnections();
1416
public:
15-
LaunchWidget(Languages language, QWidget* parent = nullptr);
17+
LaunchWidget(QWidget* parent = nullptr);
1618

1719
private slots:
1820
/// @brief Change language by its index if language has been changed via select list.
19-
void OnChangeLanguage(int intLngIndex);
21+
void GreetingWidget_LanguageChanged(int intLngIndex);
2022
/// @brief Open create/loader widget if start button has been clicked.
21-
void OnStartButtonClicked(GreetingWidget::StandartButtons standartButton);
22-
signals:
23+
void BtnNewProjectOrBtnLoadProject_Clicked(GreetingWidget::StandartButtons standartButton);
2324
/// @brief Returns checked configuration of user preferences.
24-
void AcceptedConfiguration(const QVariant& configuration);
25+
void CreationDialog_AcceptConfiguration(const QVariant& configuration);
2526
};

src/GUI/Translations/ru.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@
8383
<translation type="vanished">ЗАГРУЗИТЬ ПРОЕКТ</translation>
8484
</message>
8585
<message>
86-
<location filename="../GreetingWidget.cpp" line="26"/>
86+
<location filename="../GreetingWidget.cpp" line="28"/>
8787
<source>NEW</source>
8888
<translation>НОВЫЙ</translation>
8989
</message>
9090
<message>
91-
<location filename="../GreetingWidget.cpp" line="26"/>
92-
<location filename="../GreetingWidget.cpp" line="33"/>
91+
<location filename="../GreetingWidget.cpp" line="28"/>
92+
<location filename="../GreetingWidget.cpp" line="35"/>
9393
<source>PROJECT</source>
9494
<translation>ПРОЕКТ</translation>
9595
</message>
9696
<message>
97-
<location filename="../GreetingWidget.cpp" line="33"/>
97+
<location filename="../GreetingWidget.cpp" line="35"/>
9898
<source>LOAD</source>
9999
<translation>ЗАГРУЗИТЬ</translation>
100100
</message>
101101
<message>
102-
<location filename="../GreetingWidget.cpp" line="40"/>
102+
<location filename="../GreetingWidget.cpp" line="42"/>
103103
<source>LANGUAGE</source>
104104
<translation>ЯЗЫК</translation>
105105
</message>

src/GUI/WindowManager.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#include <QFileInfo>
2-
#include <QTranslator>
32
#include <QApplication>
43

54
#include "../Logger.hpp"
6-
#include "../Convert.hpp"
75
#include "../Unsorted.hpp"
8-
#include "../Registry.hpp"
96

107
#include "ImageManager.hpp"
118
#include "WindowManager.hpp"
129

1310
WindowManager::WindowManager()
1411
{
15-
WindowName = "C&C: Generals Zero Hour Hotkey Editor";
1612
qApp->setWindowIcon(QIcon(QPixmap::fromImage(ImageManager::DecodeEditorWebpIcon())));
1713

1814
LOGMSG("Loading \"" + STYLES_SHEET + "\"...");
@@ -29,37 +25,37 @@ WindowManager::WindowManager()
2925
}
3026

3127
LOGMSG("Loading launch window...");
32-
pLaunchWidget = std::make_unique<LaunchWidget>(Convert::ToLangEnum(Registry::GetCurrentUserLanguage()));
33-
pLaunchWidget->setWindowTitle(WindowName);
28+
pLaunchWidget = std::make_unique<LaunchWidget>();
29+
pLaunchWidget->setWindowTitle(strWindowName);
3430
LOGMSG("Launch window has been loaded");
31+
}
3532

36-
QObject::connect(pLaunchWidget.get(), &LaunchWidget::AcceptedConfiguration, pLaunchWidget.get(), [=, this](const QVariant& cfg)
37-
{
38-
LOGMSG("Loading editor window...");
39-
pHotkeysEditor = std::make_unique<HotkeysMainWindow>(cfg);
40-
pHotkeysEditor->setWindowTitle("C&C: Generals Zero Hour Hotkey Editor");
41-
pHotkeysEditor->show();
42-
pLaunchWidget->hide();
43-
LOGMSG("Editor window has been loaded");
44-
});
33+
void WindowManager::LaunchWidget_AcceptConfiguration(const QVariant& cfg)
34+
{
35+
// 2nd init protection
36+
if (bEditorInitialized) return;
37+
38+
LOGMSG("Loading editor window...");
39+
pHotkeysEditor = std::make_unique<HotkeysMainWindow>(cfg);
40+
pHotkeysEditor->setWindowTitle(strWindowName);
41+
pHotkeysEditor->show();
42+
pLaunchWidget->close();
43+
bEditorInitialized = true;
44+
LOGMSG("Editor window has been loaded");
4545
}
4646

4747
void WindowManager::SetTranslator(Languages lngType)
4848
{
49-
// Delete old translator
50-
if (pAppTranslator != nullptr) qApp->removeTranslator(pAppTranslator);
51-
49+
if (pAppTranslator != nullptr)
50+
qApp->removeTranslator(pAppTranslator);
51+
5252
QString lngShortName = Unsorted::GetLanguageShortName(lngType);
5353
LOGMSG("Set editor language to " + lngShortName.toUpper());
5454

55-
// Create new translator
56-
if (lngType != Languages::English)
57-
{
58-
Language = lngType;
59-
pAppTranslator = new QTranslator();
60-
pAppTranslator->load(lngShortName, TRANSLATIONS_FOLDER);
61-
qApp->installTranslator(pAppTranslator);
62-
}
55+
Language = lngType;
56+
pAppTranslator = new QTranslator();
57+
pAppTranslator->load(lngShortName, TRANSLATIONS_FOLDER);
58+
qApp->installTranslator(pAppTranslator);
6359
}
6460

6561
void WindowManager::Show() { pLaunchWidget->show(); }

src/GUI/WindowManager.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <QTranslator>
44

55
#include "../ProgramConstants.hpp"
6+
#include "../Convert.hpp"
7+
#include "../Registry.hpp"
68

79
#include "LaunchWidget.hpp"
810
#include "HotkeysMainWindow.hpp"
@@ -12,20 +14,23 @@
1214
class WindowManager final
1315
{
1416
private: // Data
15-
std::unique_ptr<LaunchWidget> pLaunchWidget = nullptr;
16-
std::unique_ptr<HotkeysMainWindow> pHotkeysEditor = nullptr;
17-
inline static QTranslator* pAppTranslator = nullptr;
18-
inline static Languages Language = Languages::English;
19-
QString WindowName;
17+
std::unique_ptr<LaunchWidget> pLaunchWidget = nullptr;
18+
std::unique_ptr<HotkeysMainWindow> pHotkeysEditor = nullptr;
19+
inline static QTranslator* pAppTranslator = nullptr;
20+
inline static Languages Language = Languages::English; // Convert::ToLangEnum(Registry::GetCurrentUserLanguage());
21+
QString strWindowName = "C&C: Generals Zero Hour Hotkey Editor";
22+
bool bEditorInitialized = false;
2023
public:
2124
inline static std::unique_ptr<WindowManager> Instance = nullptr;
2225

2326
public: // Methods
2427
WindowManager();
2528
/// @brief Enable start widget rendering.
2629
void Show();
27-
/// @brief Set common l10n translator for the whole project by language type from enum.
30+
/// @brief Set common L10N translator for the whole project app by language type from enum.
2831
void SetTranslator(Languages language);
2932
/// @brief Return current language type for the editor. To set language use `SetTranslator`.
3033
Languages GetLanguage();
34+
/// @brief Due to Qt limitations this slot is a public and should be calls only in `LaunchWidget` to launch editor.
35+
void LaunchWidget_AcceptConfiguration(const QVariant& cfg);
3136
};

0 commit comments

Comments
 (0)