Gui: override QuantitySpinBox::setExpression and validate expression to update internal representation

This commit is contained in:
wmayer
2022-01-05 20:15:03 +01:00
parent 00d6f4f2df
commit 6aec424aa5
2 changed files with 31 additions and 11 deletions

View File

@@ -455,6 +455,21 @@ void QuantitySpinBox::updateText(const Quantity &quant)
handlePendingEmit();
}
void QuantitySpinBox::validateInput()
{
Q_D(QuantitySpinBox);
int pos = 0;
QValidator::State state;
QString text = lineEdit()->text();
d->validateAndInterpret(text, pos, state);
if (state != QValidator::Acceptable) {
lineEdit()->setText(d->validStr);
}
handlePendingEmit();
}
Base::Quantity QuantitySpinBox::value() const
{
Q_D(const QuantitySpinBox);
@@ -700,6 +715,19 @@ QString QuantitySpinBox::getUserString(const Base::Quantity& val) const
}
}
void QuantitySpinBox::setExpression(std::shared_ptr<Expression> expr)
{
Q_ASSERT(isBound());
try {
ExpressionBinding::setExpression(expr);
validateInput();
}
catch (const Base::Exception & e) {
showInvalidExpression(QString::fromLatin1(e.what()));
}
}
QAbstractSpinBox::StepEnabled QuantitySpinBox::stepEnabled() const
{
Q_D(const QuantitySpinBox);
@@ -869,17 +897,7 @@ void QuantitySpinBox::focusInEvent(QFocusEvent * event)
void QuantitySpinBox::focusOutEvent(QFocusEvent * event)
{
Q_D(QuantitySpinBox);
int pos = 0;
QString text = lineEdit()->text();
QValidator::State state;
d->validateAndInterpret(text, pos, state);
if (state != QValidator::Acceptable) {
lineEdit()->setText(d->validStr);
}
handlePendingEmit();
validateInput();
QToolTip::hideText();
QAbstractSpinBox::focusOutEvent(event);

View File

@@ -141,6 +141,7 @@ protected Q_SLOTS:
void handlePendingEmit();
protected:
virtual void setExpression(std::shared_ptr<App::Expression> expr);
virtual void openFormulaDialog();
virtual StepEnabled stepEnabled() const;
virtual void showEvent(QShowEvent * event);
@@ -153,6 +154,7 @@ protected:
virtual void paintEvent(QPaintEvent *event);
private:
void validateInput();
void updateText(const Base::Quantity&);
void updateFromCache(bool);
QString getUserString(const Base::Quantity& val, double& factor, QString& unitString) const;