void DoubleSpinBox::onChange() { if (getExpression()) { std::unique_ptr<Expression> result(getExpression()->eval()); NumberExpression * value = freecad_dynamic_cast<NumberExpression>(result.get()); if (value) { setValue(boost::math::round(value->getValue())); setReadOnly(true); iconLabel->setPixmap(getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight))); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Text, Qt::lightGray); lineEdit()->setPalette(p); } setToolTip(Base::Tools::fromStdString(getExpression()->toString())); } else { setReadOnly(false); iconLabel->setPixmap(getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight))); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text)); lineEdit()->setPalette(p); } iconLabel->setToolTip(QString()); }
void Gui::QuantitySpinBox::onChange() { Q_ASSERT(isBound()); if (getExpression()) { std::unique_ptr<Expression> result(getExpression()->eval()); NumberExpression * value = freecad_dynamic_cast<NumberExpression>(result.get()); if (value) { std::stringstream s; s << value->getValue(); lineEdit()->setText(value->getQuantity().getUserString()); setReadOnly(true); QPixmap pixmap = getIcon(":/icons/bound-expression.svg", QSize(iconHeight, iconHeight)); iconLabel->setPixmap(pixmap); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Text, Qt::lightGray); lineEdit()->setPalette(p); } iconLabel->setToolTip(QString()); setToolTip(Base::Tools::fromStdString(getExpression()->toString())); } else { setReadOnly(false); QPixmap pixmap = getIcon(":/icons/bound-expression-unset.svg", QSize(iconHeight, iconHeight)); iconLabel->setPixmap(pixmap); QPalette p(lineEdit()->palette()); p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text)); lineEdit()->setPalette(p); iconLabel->setToolTip(QString()); } iconLabel->setToolTip(QString()); }
void Sheet::updateProperty(CellAddress key) { const Property * prop; Cell * cell = getCell(key); if (cell != 0) { Expression * output; const Expression * input = cell->getExpression(); if (input) { output = input->eval(); } else { std::string s; if (cell->getStringContent(s)) output = new StringExpression(this, s); else output = new StringExpression(this, ""); } /* Eval returns either NumberExpression or StringExpression objects */ if (freecad_dynamic_cast<NumberExpression>(output)) { NumberExpression * number = static_cast<NumberExpression*>(output); if (number->getUnit().isEmpty()) prop = setFloatProperty(key, number->getValue()); else prop = setQuantityProperty(key, number->getValue(), number->getUnit()); } else prop = setStringProperty(key, freecad_dynamic_cast<StringExpression>(output)->getText().c_str()); delete output; } else clear(key); cellUpdated(key); }
Expression *ConditionalExpression::eval() const { std::auto_ptr<Expression> e(condition->eval()); NumberExpression * v = freecad_dynamic_cast<NumberExpression>(e.get()); if (v == 0) throw ExpressionError("Invalid expression"); if (fabs(v->getValue()) > 0.5) return trueExpr->eval(); else return falseExpr->eval(); }
Expression *ConditionalExpression::simplify() const { std::auto_ptr<Expression> e(condition->simplify()); NumberExpression * v = freecad_dynamic_cast<NumberExpression>(e.get()); if (v == 0) return new ConditionalExpression(owner, condition->simplify(), trueExpr->simplify(), falseExpr->simplify()); else { if (fabs(v->getValue()) > 0.5) return trueExpr->simplify(); else return falseExpr->simplify(); } }