MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / checkExpression

Method checkExpression

src/Gui/Dialogs/DlgExpressionInput.cpp:351–416  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

349}
350
351void DlgExpressionInput::checkExpression(const QString& text)
352{
353 // now handle expression
354 std::shared_ptr<Expression> expr(
355 ExpressionParser::parse(path.getDocumentObject(), text.toUtf8().constData())
356 );
357
358 if (expr) {
359 std::string error = path.getDocumentObject()->ExpressionEngine.validateExpression(path, expr);
360
361 if (!error.empty()) {
362 throw Base::RuntimeError(error.c_str());
363 }
364
365 std::unique_ptr<Expression> result(expr->eval());
366 message = result->toString();
367
368 expression = expr;
369 okBtn->setEnabled(true);
370 ui->msg->clear();
371
372 // set default palette as we may have read text right now
373 ui->msg->setPalette(okBtn->palette());
374
375 auto* n = freecad_cast<NumberExpression*>(result.get());
376 if (n) {
377 Base::Quantity value = n->getQuantity();
378
379 Base::Type typePath = getTypePath();
380 if (typePath == App::PropertyString::getClassTypeId()
381 || typePath == App::PropertyMap::getClassTypeId()) {
382 // For string properties just take the result value without further checks
383 message = App::quote(anyToString(result->getValueAsAny()));
384 }
385 else {
386 if (!value.isValid()) {
387 THROWMT(Base::ValueError, QT_TRANSLATE_NOOP("Exceptions", "Not a Number"));
388 }
389
390 QString msg = QString::fromStdString(value.getUserString());
391 if (impliedUnit != Base::Unit::One) {
392 if (!value.isDimensionless() && value.getUnit() != impliedUnit) {
393 THROWMT(
394 Base::UnitsMismatchError,
395 QT_TRANSLATE_NOOP("Exceptions", "Unit mismatch between result and required unit")
396 );
397 }
398
399 value.setUnit(impliedUnit);
400 }
401 else if (!value.isDimensionless()) {
402 msg += tr(" (Warning: unit discarded)");
403
404 QPalette p(ui->msg->palette());
405 p.setColor(QPalette::WindowText, Qt::red);
406 ui->msg->setPalette(p);
407 }
408

Callers

nothing calls this directly

Calls 15

quoteFunction · 0.85
anyToStringFunction · 0.85
getDocumentObjectMethod · 0.80
constDataMethod · 0.80
evalMethod · 0.80
setPaletteMethod · 0.80
paletteMethod · 0.80
getValueAsAnyMethod · 0.80
isDimensionlessMethod · 0.80
throwIfOutOfRangeMethod · 0.80
toStdStringMethod · 0.80
parseFunction · 0.50

Tested by

no test coverage detected