Skip to content

Commit d79a400

Browse files
Fix corrupted text in TabLabels when translation occurs after the constructor
1 parent f741385 commit d79a400

3 files changed

Lines changed: 46 additions & 27 deletions

File tree

qt/OpenRGBDialog/OpenRGBDialog.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ void OpenRGBDialog::AddPluginsPage()
711711
/*-----------------------------------------------------*\
712712
| Create the tab label |
713713
\*-----------------------------------------------------*/
714-
TabLabel* PluginTabLabel = new TabLabel(OpenRGBFont::extension, tr("Plugins"), (char *)"Plugins", (char *)context);
714+
TabLabel* PluginTabLabel = new TabLabel(OpenRGBFont::extension, (char *)"Plugins", (char *)context, true);
715715

716716
ui->SettingsTabBar->tabBar()->setTabButton(ui->SettingsTabBar->tabBar()->count() - 1, QTabBar::LeftSide, PluginTabLabel);
717717
}
@@ -728,7 +728,7 @@ void OpenRGBDialog::AddSoftwareInfoPage()
728728
/*-----------------------------------------------------*\
729729
| Create the tab label |
730730
\*-----------------------------------------------------*/
731-
TabLabel* SoftwareTabLabel = new TabLabel(OpenRGBFont::info, tr("About OpenRGB"), (char *)"About OpenRGB", (char *)context);
731+
TabLabel* SoftwareTabLabel = new TabLabel(OpenRGBFont::info, (char *)"About OpenRGB", (char *)context, true);
732732

733733
ui->InformationTabBar->tabBar()->setTabButton(ui->InformationTabBar->tabBar()->count() - 1, QTabBar::LeftSide, SoftwareTabLabel);
734734
}
@@ -745,7 +745,7 @@ void OpenRGBDialog::AddSupportedDevicesPage()
745745
/*-----------------------------------------------------*\
746746
| Create the tab label |
747747
\*-----------------------------------------------------*/
748-
TabLabel* SupportedTabLabel = new TabLabel(OpenRGBFont::controller, tr("Supported Devices"), (char *)"Supported Devices", (char *)context);
748+
TabLabel* SupportedTabLabel = new TabLabel(OpenRGBFont::controller, (char *)"Supported Devices", (char *)context, true);
749749

750750
ui->SettingsTabBar->tabBar()->setTabButton(ui->SettingsTabBar->tabBar()->count() - 1, QTabBar::LeftSide, SupportedTabLabel);
751751
}
@@ -762,7 +762,7 @@ void OpenRGBDialog::AddSettingsPage()
762762
/*-----------------------------------------------------*\
763763
| Create the tab label |
764764
\*-----------------------------------------------------*/
765-
TabLabel* SettingsTabLabel = new TabLabel(OpenRGBFont::options, tr("General Settings"), (char *)"General Settings", (char *)context);
765+
TabLabel* SettingsTabLabel = new TabLabel(OpenRGBFont::options, (char *)"General Settings", (char *)context, true);
766766

767767
ui->SettingsTabBar->tabBar()->setTabButton(ui->SettingsTabBar->tabBar()->count() - 1, QTabBar::LeftSide, SettingsTabLabel);
768768

@@ -785,7 +785,7 @@ void OpenRGBDialog::AddManualDevicesSettingsPage()
785785
/*-----------------------------------------------------*\
786786
| Create the tab label |
787787
\*-----------------------------------------------------*/
788-
TabLabel* SettingsTabLabel = new TabLabel(OpenRGBFont::bulb, tr("Manually Added Devices"), (char *)"Manually Added Devices", (char *)context);
788+
TabLabel* SettingsTabLabel = new TabLabel(OpenRGBFont::bulb, (char *)"Manually Added Devices", (char *)context, true);
789789

790790
ui->SettingsTabBar->tabBar()->setTabButton(ui->SettingsTabBar->tabBar()->count() - 1, QTabBar::LeftSide, SettingsTabLabel);
791791
}
@@ -795,7 +795,7 @@ void OpenRGBDialog::AddPlugin(OpenRGBPluginEntry* plugin)
795795
/*-----------------------------------------------------*\
796796
| Create the tab label |
797797
\*-----------------------------------------------------*/
798-
TabLabel* PluginTabLabel = new TabLabel(OpenRGBFont::extension, QString::fromStdString(plugin->info.Label), (char *)plugin->info.Label.c_str(), (char *)context);
798+
TabLabel* PluginTabLabel = new TabLabel(OpenRGBFont::extension, (char *)plugin->info.Label.c_str(), (char *)context, false);
799799

800800
/*-----------------------------------------------------*\
801801
| Place plugin as its own top level tab |
@@ -948,7 +948,7 @@ void OpenRGBDialog::AddI2CToolsPage()
948948
/*-----------------------------------------------------*\
949949
| Create the tab label |
950950
\*-----------------------------------------------------*/
951-
TabLabel* SMBusToolsTabLabel = new TabLabel(OpenRGBFont::toolbox, tr("SMBus Tools"), (char *)"SMBus Tools", (char *)context);
951+
TabLabel* SMBusToolsTabLabel = new TabLabel(OpenRGBFont::toolbox, (char *)"SMBus Tools", (char *)context, true);
952952

953953
ui->InformationTabBar->tabBar()->setTabButton(ui->InformationTabBar->tabBar()->count() - 1, QTabBar::LeftSide, SMBusToolsTabLabel);
954954
}
@@ -1062,7 +1062,7 @@ void OpenRGBDialog::UpdateDevicesList()
10621062
/*---------------------------------------------*\
10631063
| Create the tab label |
10641064
\*---------------------------------------------*/
1065-
TabLabel* NewTabLabel = new TabLabel(GetIcon(controllers[controller_idx]->type), QString::fromStdString(controllers[controller_idx]->GetName()), (char *)controllers[controller_idx]->GetName().c_str(), (char *)context);
1065+
TabLabel* NewTabLabel = new TabLabel(GetIcon(controllers[controller_idx]->type), (char *)controllers[controller_idx]->GetName().c_str(), (char *)context, false);
10661066

10671067
ui->DevicesTabBar->tabBar()->setTabButton(ui->DevicesTabBar->count() - 1, QTabBar::LeftSide, NewTabLabel);
10681068
ui->DevicesTabBar->tabBar()->setTabToolTip(ui->DevicesTabBar->count() - 1, QString::fromStdString(controllers[controller_idx]->GetName()));
@@ -1115,7 +1115,7 @@ void OpenRGBDialog::UpdateDevicesList()
11151115
/*---------------------------------------------*\
11161116
| Create the tab label |
11171117
\*---------------------------------------------*/
1118-
TabLabel* NewTabLabel = new TabLabel(GetIcon(controllers[controller_idx]->type), QString::fromStdString(controllers[controller_idx]->GetName()), (char *)controllers[controller_idx]->GetName().c_str(), (char *)context);
1118+
TabLabel* NewTabLabel = new TabLabel(GetIcon(controllers[controller_idx]->type), (char *)controllers[controller_idx]->GetName().c_str(), (char *)context, false);
11191119

11201120
ui->InformationTabBar->tabBar()->setTabButton(ui->InformationTabBar->count() - 1, QTabBar::LeftSide, NewTabLabel);
11211121
ui->InformationTabBar->tabBar()->setTabToolTip(ui->InformationTabBar->count() - 1, QString::fromStdString(controllers[controller_idx]->GetName()));
@@ -1815,7 +1815,7 @@ void OpenRGBDialog::AddConsolePage()
18151815
/*-----------------------------------------------------*\
18161816
| Create the tab label |
18171817
\*-----------------------------------------------------*/
1818-
TabLabel* ConsoleTabLabel = new TabLabel(OpenRGBFont::terminal, tr("Log Console"), (char *)"Log Console", (char *)context);
1818+
TabLabel* ConsoleTabLabel = new TabLabel(OpenRGBFont::terminal, (char *)"Log Console", (char *)context, true);
18191819

18201820
ui->InformationTabBar->tabBar()->setTabButton(ui->InformationTabBar->tabBar()->count() - 1, QTabBar::LeftSide, ConsoleTabLabel);
18211821
}

qt/TabLabel.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "OpenRGBFont.h"
1313
#include "ui_TabLabel.h"
1414

15-
TabLabel::TabLabel(int icon, QString name, char* original, char* context) :
15+
TabLabel::TabLabel(int icon, char* label, char* context, bool translatable) :
1616
QWidget(nullptr),
1717
ui(new Ui::TabLabel)
1818
{
@@ -24,10 +24,11 @@ TabLabel::TabLabel(int icon, QString name, char* original, char* context) :
2424
ui->icon->setFont(font);
2525
ui->icon->setText(OpenRGBFont::icon(icon));
2626

27-
ui->name->setText(name);
27+
this->translatable = translatable;
28+
this->label = label;
29+
this->context = context;
2830

29-
label = original;
30-
ctxt = context;
31+
UpdateLabel(true);
3132
}
3233

3334
TabLabel::~TabLabel()
@@ -39,14 +40,29 @@ void TabLabel::changeEvent(QEvent *event)
3940
{
4041
if(event->type() == QEvent::LanguageChange)
4142
{
42-
/*-----------------------------------------------------*\
43-
| Storing the base string in label |
44-
| enables switching between multiple languages |
45-
| The context needs to be stored as the translation |
46-
| file requires the originating context |
47-
\*-----------------------------------------------------*/
48-
QApplication* app = static_cast<QApplication *>(QApplication::instance());
49-
50-
ui->name->setText(app->translate(ctxt, label));
43+
UpdateLabel(false);
44+
}
45+
}
46+
47+
void TabLabel::UpdateLabel(bool in_constructor)
48+
{
49+
if(translatable)
50+
{
51+
/*-------------------------------------------------*\
52+
| Storing the base string in label |
53+
| enables switching between multiple languages |
54+
| The context needs to be stored as the translation |
55+
| file requires the originating context |
56+
\*-------------------------------------------------*/
57+
QApplication* app = static_cast<QApplication *>(QApplication::instance());
58+
ui->name->setText(app->translate(context, label));
59+
}
60+
else if(in_constructor)
61+
{
62+
/*-------------------------------------------------*\
63+
| Must only be called during constructor if not |
64+
| translatable as label buffer may not exist |
65+
\*-------------------------------------------------*/
66+
ui->name->setText(label);
5167
}
5268
}

qt/TabLabel.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ class TabLabel : public QWidget
2121
Q_OBJECT
2222

2323
public:
24-
TabLabel(int icon, QString name, char* original, char* context);
24+
TabLabel(int icon, char* label, char* context, bool translatable);
2525
~TabLabel();
2626

2727
private:
28-
Ui::TabLabel *ui;
29-
char* label;
30-
char* ctxt;
28+
Ui::TabLabel* ui;
29+
char* label;
30+
char* context;
31+
bool translatable;
32+
33+
void UpdateLabel(bool in_constructor);
3134

3235
private slots:
3336
void changeEvent(QEvent *event);

0 commit comments

Comments
 (0)