From dd66a9c9fc23ba4bd20b67479db91e778de55727 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 20 Aug 2017 18:50:13 +0200 Subject: [PATCH] add method toFormat to get number format from character --- src/Base/Quantity.h | 16 ++++++++++++++++ src/Base/QuantityPyImp.cpp | 16 ++++------------ src/Gui/InputField.cpp | 10 ++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index 4b4629b5bf..534f5363b8 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -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; + } + } }; /** diff --git a/src/Base/QuantityPyImp.cpp b/src/Base/QuantityPyImp.cpp index dd4c1c2440..81a7bbc0ec 100644 --- a/src/Base/QuantityPyImp.cpp +++ b/src/Base/QuantityPyImp.cpp @@ -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); } diff --git a/src/Gui/InputField.cpp b/src/Gui/InputField.cpp index b876754fd9..4ea0929070 100644 --- a/src/Gui/InputField.cpp +++ b/src/Gui/InputField.cpp @@ -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); }