From 89ce8afe776316bd384fa0a928af899d70723125 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 23 Aug 2016 21:24:14 +0300 Subject: [PATCH] Localize numeric keyboard's dot key according to locale and user preferences. --HG-- branch : develop --- src/app/tape/tmainwindow.cpp | 57 +++++++++++++++++++ src/app/tape/tmainwindow.h | 1 + src/app/tape/tmainwindow.ui | 2 +- src/app/valentina/core/vformulaproperty.cpp | 3 + .../valentina/dialogs/dialogincrements.cpp | 35 +++++++++++- src/app/valentina/dialogs/dialogincrements.h | 5 +- .../plugins/vstringproperty.cpp | 40 ++++++++++++- .../plugins/vstringproperty.h | 4 ++ src/libs/vtools/dialogs/tools/dialogtool.cpp | 17 +++++- 9 files changed, 155 insertions(+), 9 deletions(-) diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index ce83093da..ea4479341 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -110,6 +110,9 @@ TMainWindow::TMainWindow(QWidget *parent) ui->lineEditEmail->setClearButtonEnabled(true); #endif + ui->lineEditFind->installEventFilter(this); + ui->plainTextEditFormula->installEventFilter(this); + search = QSharedPointer(new VTableSearch(ui->tableWidget)); ui->tabWidget->setVisible(false); @@ -556,6 +559,60 @@ void TMainWindow::showEvent(QShowEvent *event) isInitialized = true;//first show windows are held } +//--------------------------------------------------------------------------------------------------------------------- +bool TMainWindow::eventFilter(QObject *object, QEvent *event) +{ + if (QPlainTextEdit *plainTextEdit = qobject_cast(object)) + { + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return)) + { + // Ignore Enter key + return true; + } + else if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier)) + { + if (qApp->Settings()->GetOsSeparator()) + { + plainTextEdit->insertPlainText(QLocale::system().decimalPoint()); + } + else + { + plainTextEdit->insertPlainText(QLocale::c().decimalPoint()); + } + return true; + } + } + } + else if (QLineEdit *textEdit = qobject_cast(object)) + { + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier)) + { + if (qApp->Settings()->GetOsSeparator()) + { + textEdit->insert(QLocale::system().decimalPoint()); + } + else + { + textEdit->insert(QLocale::c().decimalPoint()); + } + return true; + } + } + } + else + { + // pass the event on to the parent class + return QMainWindow::eventFilter(object, event); + } + return false;// pass the event to the widget +} + //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::FileSave() { diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index c9a5eb8ae..4b1dc532a 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -68,6 +68,7 @@ protected: virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; virtual void changeEvent(QEvent* event) Q_DECL_OVERRIDE; virtual void showEvent(QShowEvent *event) Q_DECL_OVERRIDE; + virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; private slots: void FileNew(); diff --git a/src/app/tape/tmainwindow.ui b/src/app/tape/tmainwindow.ui index c31f1bae9..e63c42a86 100644 --- a/src/app/tape/tmainwindow.ui +++ b/src/app/tape/tmainwindow.ui @@ -47,7 +47,7 @@ - 1 + 0 diff --git a/src/app/valentina/core/vformulaproperty.cpp b/src/app/valentina/core/vformulaproperty.cpp index ad5b8dd48..1686dc6d8 100644 --- a/src/app/valentina/core/vformulaproperty.cpp +++ b/src/app/valentina/core/vformulaproperty.cpp @@ -33,6 +33,7 @@ #include "vformulapropertyeditor.h" #include "../vpropertyexplorer/vproperties.h" #include "../vpatterndb/vformula.h" +#include "../vmisc/vabstractapplication.h" enum class ChildType : char {Invalid = 0, Value = 1, Formula = 2}; @@ -48,12 +49,14 @@ VFormulaProperty::VFormulaProperty(const QString &name) addChild(tmpValue); tmpValue->setUpdateBehaviour(true, false); tmpValue->setReadOnly(true); + tmpValue->setOsSeparator(qApp->Settings()->GetOsSeparator()); tmpValue->setTypeForParent(static_cast(ChildType::Value)); VStringProperty* tmpFormula = new VStringProperty(tr("Formula")); addChild(tmpFormula); tmpFormula->setClearButtonEnable(true); tmpFormula->setUpdateBehaviour(true, false); + tmpFormula->setOsSeparator(qApp->Settings()->GetOsSeparator()); tmpFormula->setTypeForParent(static_cast(ChildType::Formula)); setValue(0); diff --git a/src/app/valentina/dialogs/dialogincrements.cpp b/src/app/valentina/dialogs/dialogincrements.cpp index cc38efc40..7b07dc8b4 100644 --- a/src/app/valentina/dialogs/dialogincrements.cpp +++ b/src/app/valentina/dialogs/dialogincrements.cpp @@ -68,9 +68,12 @@ DialogIncrements::DialogIncrements(VContainer *data, VPattern *doc, QWidget *par ui->lineEditFind->setClearButtonEnabled(true); #endif + ui->lineEditFind->installEventFilter(this); + search = QSharedPointer(new VTableSearch(ui->tableWidgetIncrement)); formulaBaseHeight = ui->plainTextEditFormula->height(); + ui->plainTextEditFormula->installEventFilter(this); qApp->Settings()->GetOsSeparator() ? setLocale(QLocale::system()) : setLocale(QLocale(QLocale::C)); @@ -726,7 +729,37 @@ void DialogIncrements::changeEvent(QEvent *event) FullUpdateFromFile(); } // remember to call base class implementation - QWidget::changeEvent(event); + QWidget::changeEvent(event); +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogIncrements::eventFilter(QObject *object, QEvent *event) +{ + if (QLineEdit *textEdit = qobject_cast(object)) + { + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier)) + { + if (qApp->Settings()->GetOsSeparator()) + { + textEdit->insert(QLocale::system().decimalPoint()); + } + else + { + textEdit->insert(QLocale::c().decimalPoint()); + } + return true; + } + } + } + else + { + // pass the event on to the parent class + return DialogTool::eventFilter(object, event); + } + return false;// pass the event to the widget } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/valentina/dialogs/dialogincrements.h b/src/app/valentina/dialogs/dialogincrements.h index d54ef48b3..af30e4b45 100644 --- a/src/app/valentina/dialogs/dialogincrements.h +++ b/src/app/valentina/dialogs/dialogincrements.h @@ -59,8 +59,9 @@ signals: void FullUpdateTree(const Document &parse); protected: - virtual void closeEvent ( QCloseEvent * event ) Q_DECL_OVERRIDE; - virtual void changeEvent ( QEvent * event) Q_DECL_OVERRIDE; + virtual void closeEvent ( QCloseEvent * event ) Q_DECL_OVERRIDE; + virtual void changeEvent ( QEvent * event) Q_DECL_OVERRIDE; + virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; private slots: void ShowIncrementDetails(); void AddIncrement(); diff --git a/src/libs/vpropertyexplorer/plugins/vstringproperty.cpp b/src/libs/vpropertyexplorer/plugins/vstringproperty.cpp index 5aaf3e94e..692297516 100644 --- a/src/libs/vpropertyexplorer/plugins/vstringproperty.cpp +++ b/src/libs/vpropertyexplorer/plugins/vstringproperty.cpp @@ -20,6 +20,7 @@ #include "vstringproperty.h" +#include #include #include #include @@ -38,7 +39,7 @@ using namespace VPE; VPE::VStringProperty::VStringProperty(const QString &name, const QMap &settings) - : VProperty(name, QVariant::String), readOnly(false), typeForParent(0), clearButton(false) + : VProperty(name, QVariant::String), readOnly(false), typeForParent(0), clearButton(false), m_osSeparator(false) { VProperty::setSettings(settings); d_ptr->VariantValue.setValue(QString()); @@ -46,7 +47,7 @@ VPE::VStringProperty::VStringProperty(const QString &name, const QMapVariantValue.setValue(QString()); d_ptr->VariantValue.convert(QVariant::String); @@ -61,6 +62,7 @@ QWidget *VPE::VStringProperty::createEditor(QWidget *parent, const QStyleOptionV QLineEdit* tmpEditor = new QLineEdit(parent); tmpEditor->setLocale(parent->locale()); tmpEditor->setReadOnly(readOnly); + tmpEditor->installEventFilter(this); #if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0) tmpEditor->setClearButtonEnabled(clearButton); #endif @@ -87,6 +89,11 @@ void VPE::VStringProperty::setReadOnly(bool readOnly) this->readOnly = readOnly; } +void VStringProperty::setOsSeparator(bool separator) +{ + m_osSeparator = separator; +} + void VStringProperty::setClearButtonEnable(bool value) { this->clearButton = value; @@ -150,3 +157,32 @@ void VStringProperty::setTypeForParent(int value) { typeForParent = value; } + +bool VStringProperty::eventFilter(QObject *object, QEvent *event) +{ + if (QLineEdit *textEdit = qobject_cast(object)) + { + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier)) + { + if (m_osSeparator) + { + textEdit->insert(QLocale::system().decimalPoint()); + } + else + { + textEdit->insert(QLocale::c().decimalPoint()); + } + return true; + } + } + } + else + { + // pass the event on to the parent class + return VProperty::eventFilter(object, event); + } + return false;// pass the event to the widget +} diff --git a/src/libs/vpropertyexplorer/plugins/vstringproperty.h b/src/libs/vpropertyexplorer/plugins/vstringproperty.h index 0fe83ac43..3c5039357 100644 --- a/src/libs/vpropertyexplorer/plugins/vstringproperty.h +++ b/src/libs/vpropertyexplorer/plugins/vstringproperty.h @@ -63,6 +63,7 @@ public: virtual QVariant getEditorData(const QWidget* editor) const Q_DECL_OVERRIDE; void setReadOnly(bool readOnly); + void setOsSeparator(bool separator); void setClearButtonEnable(bool value); //! Sets the settings. @@ -94,6 +95,9 @@ protected: bool readOnly; int typeForParent; bool clearButton; + bool m_osSeparator; + + virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE; private: Q_DISABLE_COPY(VStringProperty) diff --git a/src/libs/vtools/dialogs/tools/dialogtool.cpp b/src/libs/vtools/dialogs/tools/dialogtool.cpp index c4090d21f..48a492a0c 100644 --- a/src/libs/vtools/dialogs/tools/dialogtool.cpp +++ b/src/libs/vtools/dialogs/tools/dialogtool.cpp @@ -346,8 +346,7 @@ void DialogTool::MoveCursorToEnd(QPlainTextEdit *plainTextEdit) //--------------------------------------------------------------------------------------------------------------------- bool DialogTool::eventFilter(QObject *object, QEvent *event) { - QPlainTextEdit *plainTextEdit = qobject_cast(object); - if (plainTextEdit != nullptr) + if (QPlainTextEdit *plainTextEdit = qobject_cast(object)) { if (event->type() == QEvent::KeyPress) { @@ -357,6 +356,18 @@ bool DialogTool::eventFilter(QObject *object, QEvent *event) // Ignore Enter key return true; } + else if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier)) + { + if (qApp->Settings()->GetOsSeparator()) + { + plainTextEdit->insertPlainText(QLocale::system().decimalPoint()); + } + else + { + plainTextEdit->insertPlainText(QLocale::c().decimalPoint()); + } + return true; + } } } else @@ -364,7 +375,7 @@ bool DialogTool::eventFilter(QObject *object, QEvent *event) // pass the event on to the parent class return QDialog::eventFilter(object, event); } - return false; + return false;// pass the event to the widget } //---------------------------------------------------------------------------------------------------------------------