Gui: support of range checks in DlgExpressionInput
This commit is contained in:
@@ -111,6 +111,45 @@ DlgExpressionInput::~DlgExpressionInput()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void NumberRange::setRange(double min, double max)
|
||||
{
|
||||
minimum = min;
|
||||
maximum = max;
|
||||
defined = true;
|
||||
}
|
||||
|
||||
void NumberRange::clearRange()
|
||||
{
|
||||
defined = false;
|
||||
}
|
||||
|
||||
void NumberRange::throwIfOutOfRange(const Base::Quantity& value) const
|
||||
{
|
||||
if (!defined)
|
||||
return;
|
||||
|
||||
if (value.getValue() < minimum || value.getValue() > maximum) {
|
||||
Base::Quantity minVal(minimum, value.getUnit());
|
||||
Base::Quantity maxVal(maximum, value.getUnit());
|
||||
QString valStr = value.getUserString();
|
||||
QString minStr = minVal.getUserString();
|
||||
QString maxStr = maxVal.getUserString();
|
||||
QString error = QString::fromLatin1("Value out of range (%1 out of [%2, %3])").arg(valStr, minStr, maxStr);
|
||||
|
||||
throw Base::ValueError(error.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
void DlgExpressionInput::setRange(double minimum, double maximum)
|
||||
{
|
||||
numberRange.setRange(minimum, maximum);
|
||||
}
|
||||
|
||||
void DlgExpressionInput::clearRange()
|
||||
{
|
||||
numberRange.clearRange();
|
||||
}
|
||||
|
||||
QPoint DlgExpressionInput::expressionPosition() const
|
||||
{
|
||||
return ui->expression->pos();
|
||||
@@ -179,10 +218,13 @@ void DlgExpressionInput::textChanged(const QString &text)
|
||||
ui->msg->setPalette(p);
|
||||
}
|
||||
|
||||
numberRange.throwIfOutOfRange(value);
|
||||
|
||||
ui->msg->setText(msg);
|
||||
}
|
||||
else
|
||||
else {
|
||||
ui->msg->setText(Base::Tools::fromStdString(result->toString()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,19 @@ namespace Gui {
|
||||
|
||||
namespace Dialog {
|
||||
|
||||
class GuiExport NumberRange
|
||||
{
|
||||
public:
|
||||
void setRange(double minimum, double maximum);
|
||||
void clearRange();
|
||||
void throwIfOutOfRange(const Base::Quantity&) const;
|
||||
|
||||
private:
|
||||
double minimum{};
|
||||
double maximum{};
|
||||
bool defined{false};
|
||||
};
|
||||
|
||||
class GuiExport DlgExpressionInput : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -54,6 +67,8 @@ public:
|
||||
explicit DlgExpressionInput(const App::ObjectIdentifier & _path, std::shared_ptr<const App::Expression> _expression, const Base::Unit &_impliedUnit, QWidget *parent = nullptr);
|
||||
~DlgExpressionInput() override;
|
||||
|
||||
void setRange(double minimum, double maximum);
|
||||
void clearRange();
|
||||
std::shared_ptr<App::Expression> getExpression() const { return expression; }
|
||||
|
||||
bool discardedFormula() const { return discarded; }
|
||||
@@ -81,6 +96,7 @@ private:
|
||||
App::ObjectIdentifier path;
|
||||
bool discarded;
|
||||
const Base::Unit impliedUnit;
|
||||
NumberRange numberRange;
|
||||
|
||||
int minimumWidth;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user