From 3eadba2b211f80954ade8345f9e25aa4eef5b263 Mon Sep 17 00:00:00 2001 From: 0penBrain <48731257+0penBrain@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:42:39 +0200 Subject: [PATCH] Gui: add decimal point converter to Translator --- src/Gui/Application.cpp | 6 ----- src/Gui/DlgGeneral.ui | 32 +++++++++++----------- src/Gui/DlgGeneralImp.cpp | 5 ++++ src/Gui/DlgGeneralImp.h | 1 + src/Gui/GuiApplication.cpp | 26 ------------------ src/Gui/GuiApplication.h | 9 ------- src/Gui/Language/Translator.cpp | 47 ++++++++++++++++++++++++++++++--- src/Gui/Language/Translator.h | 5 ++++ 8 files changed, 71 insertions(+), 60 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 8e3525f3a3..fbc9083e71 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -2051,12 +2051,6 @@ void Application::runApplication() auto filter = new WheelEventFilter(&mainApp); mainApp.installEventFilter(filter); } - - //filter keyboard events to substitute decimal separator - if (hGrp->GetBool("SubstituteDecimalSeparator", false)) { - auto filter = new KeyboardFilter(&mainApp); - mainApp.installEventFilter(filter); - } // For values different to 1 and 2 use the OS locale settings auto localeFormat = hGrp->GetInt("UseLocaleFormatting", 0); diff --git a/src/Gui/DlgGeneral.ui b/src/Gui/DlgGeneral.ui index e9e06acfab..80c307f26a 100644 --- a/src/Gui/DlgGeneral.ui +++ b/src/Gui/DlgGeneral.ui @@ -103,22 +103,22 @@ - - - If enabled, numerical keypad decimal separator will be substituted with locale separator - - - Substitute decimal separator (needs restart) - - - SubstituteDecimalSeparator - - - General - - - - + + + If enabled, numerical keypad decimal separator will be substituted with locale separator + + + Substitute decimal separator (numpad) + + + SubstituteDecimalSeparator + + + General + + + + diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index ba1b853cb3..0f91b76f8f 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -166,6 +166,10 @@ void DlgGeneralImp::setNumberLocale(bool force/* = false*/) localeIndex = localeFormat; } +void DlgGeneralImp::setDecimalPointConversion(bool on) { + Translator::instance()->enableDecimalPointConversion(on); +} + void DlgGeneralImp::saveSettings() { int index = ui->AutoloadModuleCombo->currentIndex(); @@ -184,6 +188,7 @@ void DlgGeneralImp::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()); diff --git a/src/Gui/DlgGeneralImp.h b/src/Gui/DlgGeneralImp.h index b505ad5947..eb28751155 100644 --- a/src/Gui/DlgGeneralImp.h +++ b/src/Gui/DlgGeneralImp.h @@ -67,6 +67,7 @@ private: void revertToSavedConfig(); bool setLanguage(); //Returns true if language has been changed void setNumberLocale(bool force = false); + void setDecimalPointConversion(bool on); private: int localeIndex; diff --git a/src/Gui/GuiApplication.cpp b/src/Gui/GuiApplication.cpp index 423aed65ef..2ffac946d0 100644 --- a/src/Gui/GuiApplication.cpp +++ b/src/Gui/GuiApplication.cpp @@ -31,7 +31,6 @@ # include # include # include -# include # include # include #endif @@ -322,29 +321,4 @@ bool WheelEventFilter::eventFilter(QObject* obj, QEvent* ev) return false; } -KeyboardFilter::KeyboardFilter(QObject* parent) - : QObject(parent) -{ -} - -bool KeyboardFilter::eventFilter(QObject* obj, QEvent* ev) -{ - if (ev->type() == QEvent::KeyPress || ev->type() == QEvent::KeyRelease) { - auto kev = static_cast(ev); - Qt::KeyboardModifiers mod = kev->modifiers(); - int key = kev->key(); - if ((mod & Qt::KeypadModifier) && (key == Qt::Key_Period || key == Qt::Key_Comma)) - { - QChar dp = QLocale().decimalPoint(); - if (key != dp) { - QKeyEvent modifiedKeyEvent(kev->type(), dp.digitValue(), mod, QString(dp), kev->isAutoRepeat(), kev->count()); - qApp->sendEvent(obj, &modifiedKeyEvent); - return true; - } - } - } - return false; -} - - #include "moc_GuiApplication.cpp" diff --git a/src/Gui/GuiApplication.h b/src/Gui/GuiApplication.h index 4dc32685e8..5fbc265485 100644 --- a/src/Gui/GuiApplication.h +++ b/src/Gui/GuiApplication.h @@ -92,15 +92,6 @@ public: bool eventFilter(QObject* obj, QEvent* ev) override; }; -class KeyboardFilter : public QObject -{ - Q_OBJECT - -public: - explicit KeyboardFilter(QObject* parent); - bool eventFilter(QObject* obj, QEvent* ev) override; -}; - } #endif // GUI_APPLICATION_H diff --git a/src/Gui/Language/Translator.cpp b/src/Gui/Language/Translator.cpp index 028698450d..b7b5ffdc4a 100644 --- a/src/Gui/Language/Translator.cpp +++ b/src/Gui/Language/Translator.cpp @@ -25,6 +25,7 @@ # include # include # include +# include # include # include # include @@ -127,7 +128,8 @@ void Translator::destruct () _pcSingleton=nullptr; } -Translator::Translator() +Translator::Translator(): + decimalPointConversionEnabled(false) { // This is needed for Qt's lupdate d = new TranslatorP; @@ -173,8 +175,8 @@ Translator::Translator() d->mapLanguageTopLevelDomain[QT_TR_NOOP("Valencian" )] = "val-ES"; d->mapLanguageTopLevelDomain[QT_TR_NOOP("Vietnamese" )] = "vi"; - auto entries = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")-> - GetASCII("AdditionalLanguageDomainEntries", ""); + auto hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General"); + auto entries = hGrp->GetASCII("AdditionalLanguageDomainEntries", ""); // The format of the entries is "Language Name 1"="code1";"Language Name 2"="code2";... // Example: "Romanian"="ro";"Polish"="pl"; QRegularExpression matchingRE(QString::fromUtf8("\"(.*[^\\s]+.*)\"\\s*=\\s*\"([^\\s]+)\";?")); @@ -189,6 +191,8 @@ Translator::Translator() d->activatedLanguage = "English"; d->paths = directories(); + + enableDecimalPointConversion(hGrp->GetBool("SubstituteDecimalSeparator", false)); } Translator::~Translator() @@ -358,4 +362,41 @@ void Translator::removeTranslators() d->translators.clear(); } +bool Translator::eventFilter(QObject* obj, QEvent* ev) +{ + if (ev->type() == QEvent::KeyPress || ev->type() == QEvent::KeyRelease) { + QKeyEvent *kev = static_cast(ev); + Qt::KeyboardModifiers mod = kev->modifiers(); + int key = kev->key(); + if ((mod & Qt::KeypadModifier) && (key == Qt::Key_Period || key == Qt::Key_Comma)) + { + QChar dp = QLocale().decimalPoint(); + if (key != dp) { + QKeyEvent modifiedKeyEvent(kev->type(), dp.digitValue(), mod, QString(dp), kev->isAutoRepeat(), kev->count()); + qApp->sendEvent(obj, &modifiedKeyEvent); + return true; + } + } + } + return false; +} + +void Translator::enableDecimalPointConversion(bool on) +{ + if (!on) { + qApp->removeEventFilter(this); + decimalPointConversionEnabled = false; + return; + } + if (on && !decimalPointConversionEnabled) { + qApp->installEventFilter(this); + decimalPointConversionEnabled = true; + } +#if FC_DEBUG + if (on && decimalPointConversionEnabled) { + Base::Console().Instance().Warning("Translator: decimal point converter is already installed\n"); + } +#endif +} + #include "moc_Translator.cpp" diff --git a/src/Gui/Language/Translator.h b/src/Gui/Language/Translator.h index 17aeb91cb2..adb59c58b9 100644 --- a/src/Gui/Language/Translator.h +++ b/src/Gui/Language/Translator.h @@ -74,6 +74,10 @@ public: TStringMap supportedLocales() const; /** Adds a path where localization files can be found */ void addPath(const QString& path); + /** eventFilter used to convert decimal separator **/ + bool eventFilter(QObject* obj, QEvent* ev); + /** Enables/disables decimal separator conversion **/ + void enableDecimalPointConversion(bool on); private: Translator(); @@ -86,6 +90,7 @@ private: private: static Translator* _pcSingleton; TranslatorP* d; + bool decimalPointConversionEnabled; }; } // namespace Gui