Gui: add decimal point converter to Translator

This commit is contained in:
0penBrain
2022-04-08 15:42:39 +02:00
committed by wwmayer
parent d8bd6f1fb4
commit 3eadba2b21
8 changed files with 71 additions and 60 deletions

View File

@@ -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);

View File

@@ -103,22 +103,22 @@
</widget>
</item>
<item row="1" column="2">
<widget class="Gui::PrefCheckBox" name="SubstituteDecimal">
<property name="toolTip">
<string>If enabled, numerical keypad decimal separator will be substituted with locale separator</string>
</property>
<property name="text">
<string>Substitute decimal separator (needs restart)</string>
</property>
<property name="prefEntry" stdset="0">
<cstring>SubstituteDecimalSeparator</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
</widget>
</item>
</layout>
<widget class="Gui::PrefCheckBox" name="SubstituteDecimal">
<property name="toolTip">
<string>If enabled, numerical keypad decimal separator will be substituted with locale separator</string>
</property>
<property name="text">
<string>Substitute decimal separator (numpad)</string>
</property>
<property name="prefEntry" stdset="0">
<cstring>SubstituteDecimalSeparator</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>General</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>

View File

@@ -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());

View File

@@ -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;

View File

@@ -31,7 +31,6 @@
# include <QDataStream>
# include <QFileInfo>
# include <QFileOpenEvent>
# include <QKeyEvent>
# include <QSessionManager>
# include <QTimer>
#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<QKeyEvent *>(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"

View File

@@ -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

View File

@@ -25,6 +25,7 @@
# include <algorithm>
# include <QApplication>
# include <QDir>
# include <QKeyEvent>
# include <QRegularExpression>
# include <QStringList>
# include <QTranslator>
@@ -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: <FCText Name="AdditionalLanguageDomainEntries">"Romanian"="ro";"Polish"="pl";</FCText>
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<QKeyEvent *>(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"

View File

@@ -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