add method toFormat to get number format from character

This commit is contained in:
wmayer
2017-08-20 18:50:13 +02:00
parent 59cc63faea
commit dd66a9c9fc
3 changed files with 24 additions and 18 deletions

View File

@@ -63,6 +63,22 @@ struct QuantityFormat {
return 'g';
}
}
static inline NumberFormat toFormat(char c, bool* ok = 0) {
if (ok)
*ok = true;
switch (c) {
case 'f':
return Fixed;
case 'e':
return Scientific;
case 'g':
return Default;
default:
if (ok)
*ok = false;
return Default;
}
}
};
/**

View File

@@ -631,19 +631,11 @@ void QuantityPy::setFormat(Py::Tuple arg)
#endif
if (fmtstr.size() != 1)
throw Py::ValueError("Invalid format character");
switch (fmtstr.front()) {
case 'f':
fmt.format = QuantityFormat::Fixed;
break;
case 'e':
fmt.format = QuantityFormat::Scientific;
break;
case 'g':
fmt.format = QuantityFormat::Default;
break;
default:
bool ok;
fmt.format = Base::QuantityFormat::toFormat(fmtstr.front(), &ok);
if (!ok)
throw Py::ValueError("Invalid format character");
}
getQuantityPtr()->setFormat(fmt);
}

View File

@@ -535,13 +535,11 @@ QString InputField::getFormat() const
void InputField::setFormat(const QString& format)
{
if (format.isEmpty())
return;
QChar c = format[0];
Base::QuantityFormat f = this->actQuantity.getFormat();
if (format == QString::fromLatin1("f"))
f.format = Base::QuantityFormat::NumberFormat::Fixed;
else if (format == QString::fromLatin1("e"))
f.format = Base::QuantityFormat::NumberFormat::Scientific;
else
f.format = Base::QuantityFormat::NumberFormat::Default;
f.format = Base::QuantityFormat::toFormat(c.toLatin1());
actQuantity.setFormat(f);
updateText(actQuantity);
}