diff --git a/src/Gui/InputField.cpp b/src/Gui/InputField.cpp index 0fee1e4cc4..b876754fd9 100644 --- a/src/Gui/InputField.cpp +++ b/src/Gui/InputField.cpp @@ -288,6 +288,8 @@ void InputField::newInput(const QString & text) double dFactor; res.getUserString(dFactor,actUnitStr); actUnitValue = res.getValue()/dFactor; + // Preserve previous format + res.setFormat(this->actQuantity.getFormat()); actQuantity = res; // signaling @@ -439,6 +441,20 @@ const Base::Unit& InputField::getUnit() const return actUnit; } +/// get stored, valid quantity as a string +QString InputField::getQuantityString(void) const +{ + return actQuantity.getUserString(); +} + +/// set, validate and display quantity from a string. Must match existing units. +void InputField::setQuantityString(const QString& text) +{ + // Input and then format the quantity + newInput(text); + updateText(actQuantity); +} + /// get the value of the singleStep property double InputField::singleStep(void)const { @@ -499,6 +515,37 @@ QString InputField::getUnitText(void) return actUnitStr; } +int InputField::getPrecision() const +{ + return this->actQuantity.getFormat().precision; +} + +void InputField::setPrecision(const int precision) +{ + Base::QuantityFormat format = actQuantity.getFormat(); + format.precision = precision; + actQuantity.setFormat(format); + updateText(actQuantity); +} + +QString InputField::getFormat() const +{ + return QString(QChar::fromLatin1(actQuantity.getFormat().toFormat())); +} + +void InputField::setFormat(const QString& format) +{ + 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; + actQuantity.setFormat(f); + updateText(actQuantity); +} + // get the value of the minimum property int InputField::historySize(void)const { @@ -522,6 +569,7 @@ void InputField::selectNumber(void) QChar d = locale().decimalPoint(); QChar g = locale().groupSeparator(); QChar n = locale().negativeSign(); + QChar e = locale().exponential(); for (QString::iterator it = str.begin(); it != str.end(); ++it) { if (it->isDigit()) @@ -532,6 +580,8 @@ void InputField::selectNumber(void) i++; else if (*it == n) i++; + else if (*it == e && actQuantity.getFormat().format != Base::QuantityFormat::Fixed) + i++; else // any non-number character break; } diff --git a/src/Gui/InputField.h b/src/Gui/InputField.h index a2842c6162..3cf9ea17f0 100644 --- a/src/Gui/InputField.h +++ b/src/Gui/InputField.h @@ -68,7 +68,10 @@ class GuiExport InputField : public ExpressionLineEdit, public ExpressionBinding Q_PROPERTY(double minimum READ minimum WRITE setMinimum ) Q_PROPERTY(int historySize READ historySize WRITE setHistorySize ) Q_PROPERTY(QString unit READ getUnitText WRITE setUnitText ) + Q_PROPERTY(int precision READ getPrecision WRITE setPrecision ) + Q_PROPERTY(QString format READ getFormat WRITE setFormat ) Q_PROPERTY(Base::Quantity quantity READ getQuantity WRITE setValue ) + Q_PROPERTY(QString quantityString READ getQuantityString WRITE setQuantityString ) public: @@ -83,6 +86,12 @@ public: /// get the current value Base::Quantity getQuantity(void)const{return this->actQuantity;} + /// get stored, valid quantity as a string (user string - avoid storing) + QString getQuantityString(void) const; + + /// set, validate and display quantity from a string. Must match existing units. + void setQuantityString(const QString& text); + /// gives the current state of the user input, gives true if it is a valid input with correct quantity /// (shown by the green pixmap), returns false if the input is a unparsable string or has a wrong unit /// (shown by the red pixmap in the gui) @@ -119,6 +128,14 @@ public: void setUnitText(const QString&); /// get the unit as a string (can be used in the *.ui file) QString getUnitText(void); + /// get the value of the precision property + int getPrecision(void) const; + /// set the value of the precision property (can be used in the *.ui file) + void setPrecision(const int); + /// get the value of the format property: "f" (fixed), "e" (scientific), "g" (general) + QString getFormat(void) const; + /// set the value of the format property (can be used in the *.ui file) + void setFormat(const QString&); /// set the number portion selected (use after setValue()) void selectNumber(void); /// input validation