From 0b7f1218f998441b16e26678b8b64018d5047bad Mon Sep 17 00:00:00 2001 From: Chris Esposito Date: Mon, 17 Sep 2018 12:01:17 +1000 Subject: [PATCH] EspoSpinBox has better input algorithm --- Desktop_Interface/ui_elements/espospinbox.cpp | 52 +++++++++++++++++++ Desktop_Interface/ui_elements/espospinbox.h | 6 ++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Desktop_Interface/ui_elements/espospinbox.cpp b/Desktop_Interface/ui_elements/espospinbox.cpp index 4cc73ea9..e26a561c 100644 --- a/Desktop_Interface/ui_elements/espospinbox.cpp +++ b/Desktop_Interface/ui_elements/espospinbox.cpp @@ -9,26 +9,32 @@ QString espoSpinBox::textFromValue(double value) const{ QString windowText; if (value == 0){ QTextStream(&windowText) << value; + lastValidValue = value; return windowText; } if (value >= 1000000){ QTextStream(&windowText) << value/1000000 << "M"; + lastValidValue = value; return windowText; } if (value >= 1000){ QTextStream(&windowText) << value/1000 << "k"; + lastValidValue = value; return windowText; } if (value >= 1){ QTextStream(&windowText) << value; + lastValidValue = value; return windowText; } if (value >= 1/1000){ QTextStream(&windowText) << value * 1000 << "m"; + lastValidValue = value; return windowText; } if (value >= 1/1000000){ QTextStream(&windowText) << value * 1000000 << "u"; + lastValidValue = value; return windowText; } return "invalid"; @@ -51,3 +57,49 @@ void espoSpinBox::changeStepping(double value){ roundval = (roundval == 0) ? 0.1 : roundval/10; setSingleStep(roundval); } + +QValidator::State espoSpinBox::validate(QString& text, int& pos) const +{ + prefixLength = pos; + return QValidator::State::Acceptable; +} + +double espoSpinBox::valueFromText(const QString &text) const +{ + double ret; + bool isValid; + + qDebug() << text.mid(0, prefixLength - 1) << text.at(prefixLength - 1).toLatin1(); + + switch (text.at(prefixLength - 1).toLatin1()) + { + case 'M': + ret = text.mid(0, prefixLength - 1).toDouble(&isValid) * 1000000; + break; + + case 'k': + ret = text.mid(0, prefixLength - 1).toDouble(&isValid) * 1000; + break; + + case 'm': + ret = text.mid(0, prefixLength - 1).toDouble(&isValid) / 1000; + break; + + case 'u': + ret = text.mid(0, prefixLength - 1).toDouble(&isValid) / 1000000; + break; + + default: + ret = text.mid(0, prefixLength).toDouble(&isValid); + } + + if (isValid) + { + return ret; + } + else + { + qDebug() << "espoSpinBox: warning: invalid text input." << "Defaulting to last known good value of" << lastValidValue; + return lastValidValue; + } +} diff --git a/Desktop_Interface/ui_elements/espospinbox.h b/Desktop_Interface/ui_elements/espospinbox.h index 25375df8..8007d1d5 100644 --- a/Desktop_Interface/ui_elements/espospinbox.h +++ b/Desktop_Interface/ui_elements/espospinbox.h @@ -15,8 +15,12 @@ class espoSpinBox : public QDoubleSpinBox Q_OBJECT public: explicit espoSpinBox(QWidget *parent = 0); + QValidator::State validate(QString& text, int& pos) const override; private: - QString textFromValue(double value) const; + QString textFromValue(double value) const override; + double valueFromText(const QString &text) const override; + mutable int prefixLength = -1; + mutable double lastValidValue = -1; signals: public slots: