Gui: use parameter observer for locale parameter

Parameter change should be independent of the use of the preference window.
This commit is contained in:
Benjamin Bræstrup Sayoc
2025-05-13 16:49:52 +02:00
committed by Kacper Donat
parent 9714e2f87a
commit 117796bf08
4 changed files with 55 additions and 37 deletions

View File

@@ -115,6 +115,48 @@ public:
};
}
class Translator::ParameterObserver : public ParameterGrp::ObserverType
{
public:
ParameterObserver(Translator* client) : client(client)
{
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General");
hGrp->Attach(this);
}
void OnChange(Base::Subject<const char*>& caller, const char* creason) override
{
(void) caller;
std::string_view reason = creason;
if (reason == "UseLocaleFormatting") {
int format = hGrp->GetInt("UseLocaleFormatting");
if (format == 0) {
client->setLocale(); // Defaults to system locale
}
else if (format == 1) {
// Language must need to be set before locale. How do we ensure this?
std::string language = hGrp->GetASCII("Language");
client->setLocale(language);
}
else if (format == 2) {
client->setLocale("C");
}
else {
throw Base::ValueError("Parameter \"UseLocaleFormatting\" value out of bounds for Translator::formattingOptions");
}
}
else if (reason == "SubstituteDecimalSeparator") {
bool value = hGrp->GetBool("SubstituteDecimal");
client->enableDecimalPointConversion(value);
}
}
Translator* client;
static ParameterGrp::handle hGrp;
};
ParameterGrp::handle Translator::ParameterObserver::hGrp; // definition for export
Translator* Translator::instance()
{
if (!_pcSingleton)
@@ -131,6 +173,8 @@ void Translator::destruct ()
Translator::Translator()
{
observer = std::make_unique<Translator::ParameterObserver>(this);
// This is needed for Qt's lupdate
// clang-format off
d = new TranslatorP;

View File

@@ -52,6 +52,13 @@ class GuiExport Translator : public QObject
Q_OBJECT
public:
class ParameterObserver;
static constexpr std::initializer_list<const char*> formattingOptions{
QT_TR_NOOP("Operating system"),
QT_TR_NOOP("Selected language"),
QT_TR_NOOP("C/POSIX")
};
/** @name singleton stuff */
//@{
/// Creates an instance
@@ -92,6 +99,7 @@ private:
void updateLocaleChange() const;
private:
std::unique_ptr<ParameterObserver> observer;
static Translator* _pcSingleton;
TranslatorP* d;
std::unique_ptr<Translator, std::function<void(Translator*)>> decimalPointConverter;

View File

@@ -85,6 +85,9 @@ DlgSettingsGeneral::DlgSettingsGeneral( QWidget* parent )
recreatePreferencePackMenu();
for(const char* option : Translator::formattingOptions) {
ui->UseLocaleFormatting->addItem(tr(option));
}
ui->themesCombobox->setEnabled(true);
Gui::Document* doc = Gui::Application::Instance->activeDocument();
@@ -172,30 +175,9 @@ void DlgSettingsGeneral::setNumberLocale(bool force/* = false*/)
if (localeIndex == localeFormat && (!force || localeFormat == 0)) {
return;
}
if (localeFormat == 0) {
Translator::instance()->setLocale(); // Defaults to system locale
}
else if (localeFormat == 1) {
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
Translator::instance()->setLocale(current.constData());
}
else if (localeFormat == 2) {
Translator::instance()->setLocale("C");
}
else {
return; // Prevent localeIndex updating if localeFormat is out of range
}
localeIndex = localeFormat;
}
void DlgSettingsGeneral::setDecimalPointConversion(bool on)
{
if (Translator::instance()->isEnabledDecimalPointConversion() != on) {
Translator::instance()->enableDecimalPointConversion(on);
}
}
void DlgSettingsGeneral::saveUnitSystemSettings()
{
ParameterGrp::handle hGrpu = App::GetApplication().GetParameterGroupByPath
@@ -256,7 +238,6 @@ void DlgSettingsGeneral::saveSettings()
bool force = setLanguage();
// In case type is "Selected language", we need to force locale change
setNumberLocale(force);
setDecimalPointConversion(ui->SubstituteDecimal->isChecked());
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QVariant size = ui->toolbarIconSize->itemData(ui->toolbarIconSize->currentIndex());

View File

@@ -161,21 +161,6 @@
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
<item>
<property name="text">
<string>Operating system</string>
</property>
</item>
<item>
<property name="text">
<string>Selected language</string>
</property>
</item>
<item>
<property name="text">
<string>C/POSIX</string>
</property>
</item>
</widget>
</item>
<item row="4" column="2">