Base: Add properties to InputField for setting Quantity from Python

These additions are designed to allow easier interaction with the
InputField box from Python. Particularly, the ability to put the
format into scientific notation to prevent truncation of values.
There is also a need for a way to input a new value into the
text box such that it is formatted correctly.
- 'quantityString' property - allows to check, set, format and
display the underlying value. The only way to achieve this
currently from Python is to set the widget text, then hide and
re-show it to format the value.
- 'format' property: set to 'f' (fixed - default),
'e' (scientific) or 'g' (general - recommended)
to set the number format from Python or UI file
- 'precision' property: as above, number of decimals/significant
figures if it is desired to override the global default
This commit is contained in:
Oliver Oxtoby
2017-08-17 10:36:16 +02:00
committed by wmayer
parent 71130d09ed
commit 21fd2743a8
2 changed files with 67 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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