| 263 | } |
| 264 | |
| 265 | void InputField::newInput(const QString& text) |
| 266 | { |
| 267 | Quantity res; |
| 268 | try { |
| 269 | QString input = text; |
| 270 | fixup(input); |
| 271 | |
| 272 | if (isBound()) { |
| 273 | std::shared_ptr<Expression> e( |
| 274 | ExpressionParser::parse(getPath().getDocumentObject(), input.toUtf8()) |
| 275 | ); |
| 276 | |
| 277 | setExpression(e); |
| 278 | |
| 279 | std::unique_ptr<Expression> evalRes(getExpression()->eval()); |
| 280 | |
| 281 | auto* value = freecad_cast<NumberExpression*>(evalRes.get()); |
| 282 | if (value) { |
| 283 | res.setValue(value->getValue()); |
| 284 | res.setUnit(value->getUnit()); |
| 285 | } |
| 286 | } |
| 287 | else { |
| 288 | res = Quantity::parse(input.toStdString()); |
| 289 | } |
| 290 | } |
| 291 | catch (Base::Exception& e) { |
| 292 | QString errorText = QString::fromLatin1(e.what()); |
| 293 | if (iconLabel->isHidden()) { |
| 294 | iconLabel->setVisible(true); |
| 295 | } |
| 296 | Q_EMIT parseError(errorText); |
| 297 | validInput = false; |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | if (res.isDimensionless()) { |
| 302 | res.setUnit(this->actUnit); |
| 303 | } |
| 304 | |
| 305 | // check if unit fits! |
| 306 | if (actUnit != Unit::One && !res.isDimensionless() && actUnit != res.getUnit()) { |
| 307 | if (iconLabel->isHidden()) { |
| 308 | iconLabel->setVisible(true); |
| 309 | } |
| 310 | Q_EMIT parseError(QStringLiteral("Wrong unit")); |
| 311 | validInput = false; |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (iconLabel->isVisible()) { |
| 316 | iconLabel->setVisible(false); |
| 317 | } |
| 318 | validInput = true; |
| 319 | |
| 320 | if (res.getValue() > Maximum) { |
| 321 | res.setValue(Maximum); |
| 322 | } |
nothing calls this directly
no test coverage detected