Gui: fixes #6663: [Bug] Decimal separator not chosen by locale system settings anymore

This commit is contained in:
wmayer
2022-03-28 15:38:51 +02:00
parent 8cf6bf6909
commit b50ec016bd
6 changed files with 92 additions and 30 deletions

View File

@@ -2072,10 +2072,12 @@ void Application::runApplication(void)
mainApp.installEventFilter(filter);
}
if (hGrp->GetBool("UseLocaleFormatting", false)) {
Translator::instance()->setLocale(hGrp->GetASCII(("Language"), Translator::instance()->activeLanguage().c_str()));
// For values different to 1 and 2 use the OS locale settings
auto localeFormat = hGrp->GetInt("UseLocaleFormatting", 0);
if (localeFormat == 1) {
Translator::instance()->setLocale(hGrp->GetASCII("Language", Translator::instance()->activeLanguage().c_str()));
}
else {
else if (localeFormat == 2) {
Translator::instance()->setLocale("C");
}

View File

@@ -106,20 +106,28 @@
</widget>
</item>
<item row="1" column="0">
<widget class="Gui::PrefCheckBox" name="UseLocaleFormatting">
<property name="toolTip">
<string>If enabled, number formatting will be set according to selected language
If not, C/POSIX default formatting will be used (English-like)</string>
</property>
<property name="text">
<string>Use selected language number format</string>
</property>
<widget class="Gui::PrefComboBox" name="UseLocaleFormatting">
<property name="prefEntry" stdset="0">
<cstring>UseLocaleFormatting</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
<item>
<property name="text">
<string>Number format of operating system</string>
</property>
</item>
<item>
<property name="text">
<string>Use selected language number format</string>
</property>
</item>
<item>
<property name="text">
<string>C/POSIX number format</string>
</property>
</item>
</widget>
</item>
</layout>
@@ -174,24 +182,24 @@ If not, C/POSIX default formatting will be used (English-like)</string>
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>30</number>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>100</number>
</attribute>
<attribute name="horizontalHeaderMinimumSectionSize">
<number>30</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>16</number>
</attribute>
<attribute name="verticalHeaderDefaultSectionSize">
<number>24</number>
</attribute>
<attribute name="verticalHeaderMinimumSectionSize">
<number>16</number>
</attribute>
<column>
<property name="text">
<string>Name</string>
@@ -601,6 +609,11 @@ after FreeCAD launches</string>
<extends>QCheckBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
<customwidget>
<class>Gui::PrefComboBox</class>
<extends>QComboBox</extends>
<header>Gui/PrefWidgets.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>Languages</tabstop>

View File

@@ -53,6 +53,7 @@ using namespace Gui::Dialog;
*/
DlgGeneralImp::DlgGeneralImp( QWidget* parent )
: PreferencePage(parent)
, localeIndex(0)
, ui(new Ui_DlgGeneral)
{
ui->setupUi(this);
@@ -119,6 +120,38 @@ void DlgGeneralImp::setRecentFileSize()
}
}
void DlgGeneralImp::setLanguage()
{
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString lang = QLocale::languageToString(QLocale().language());
QByteArray language = hGrp->GetASCII("Language", (const char*)lang.toLatin1()).c_str();
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
if (current != language) {
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
}
void DlgGeneralImp::setNumberLocale()
{
int localeFormat = ui->UseLocaleFormatting->currentIndex();
// Only make the change if locale setting has changed
if (localeIndex == localeFormat)
return;
if (localeFormat == 0) {
Translator::instance()->setSystemLocale();
}
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");
}
}
void DlgGeneralImp::saveSettings()
{
int index = ui->AutoloadModuleCombo->currentIndex();
@@ -134,17 +167,10 @@ void DlgGeneralImp::saveSettings()
ui->SplashScreen->onSave();
setRecentFileSize();
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString lang = QLocale::languageToString(QLocale().language());
QByteArray language = hGrp->GetASCII("Language", (const char*)lang.toLatin1()).c_str();
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
if (current != language) {
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
if (ui->UseLocaleFormatting->isChecked())
Translator::instance()->setLocale(current.constData());
setLanguage();
setNumberLocale();
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QVariant size = ui->toolbarIconSize->itemData(ui->toolbarIconSize->currentIndex());
int pixel = size.toInt();
hGrp->SetInt("ToolbarIconSize", pixel);
@@ -196,6 +222,8 @@ void DlgGeneralImp::loadSettings()
auto langToStr = Translator::instance()->activeLanguage();
QByteArray language = hGrp->GetASCII("Language", langToStr.c_str()).c_str();
localeIndex = ui->UseLocaleFormatting->currentIndex();
int index = 1;
TStringMap list = Translator::instance()->supportedLocales();
ui->Languages->clear();
@@ -303,7 +331,9 @@ void DlgGeneralImp::loadSettings()
void DlgGeneralImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
int index = ui->UseLocaleFormatting->currentIndex();
ui->retranslateUi(this);
ui->UseLocaleFormatting->setCurrentIndex(index);
}
else {
QWidget::changeEvent(e);

View File

@@ -64,8 +64,11 @@ private:
void setRecentFileSize();
void saveAsNewPreferencePack();
void revertToSavedConfig();
void setLanguage();
void setNumberLocale();
private:
int localeIndex;
std::unique_ptr<Ui_DlgGeneral> ui;
std::unique_ptr<DlgCreateNewPreferencePackImp> newPreferencePackDialog;
std::unique_ptr<DlgPreferencePackManagementImp> preferencePackManagementDialog;

View File

@@ -258,16 +258,28 @@ bool Translator::setLocale(const std::string& language) const
if (!bcp47.empty())
loc = QLocale(QString::fromStdString(bcp47));
QLocale::setDefault(loc);
// Need to manually send the event so locale change is fully took into account on widgets
auto ev = QEvent(QEvent::LocaleChange);
qApp->sendEvent(qApp, &ev);
updateLocaleChange();
#ifdef FC_DEBUG
Base::Console().Log("Locale changed to %s => %s\n", qPrintable(loc.bcp47Name()), qPrintable(loc.name()));
#endif
return (loc.language() != loc.C);
}
void Translator::setSystemLocale() const
{
QLocale::setDefault(QLocale::system());
updateLocaleChange();
}
void Translator::updateLocaleChange() const
{
// Need to manually send the event so locale change is fully took into account on widgets
auto ev = QEvent(QEvent::LocaleChange);
qApp->sendEvent(qApp, &ev);
}
QStringList Translator::directories() const
{
QStringList list;

View File

@@ -68,6 +68,7 @@ public:
std::string locale(const std::string&) const;
/** Sets default Qt locale based on given language name. Returns true if matching QLocale found**/
bool setLocale(const std::string&) const;
void setSystemLocale() const;
/** Returns a list of supported languages. */
TStringList supportedLanguages() const;
/** Returns a map of supported languages/locales. */
@@ -81,6 +82,7 @@ private:
void removeTranslators();
QStringList directories() const;
void installQMFiles(const QDir& dir, const char* locale);
void updateLocaleChange() const;
private:
static Translator* _pcSingleton;