mirror of https://github.com/EspoTek/Labrador.git
EspoSpinBox has better input algorithm
This commit is contained in:
parent
bd0e618a45
commit
0b7f1218f9
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue