From ced8963041618bca7a446d4dd875506c94129d53 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 22 Feb 2018 15:03:11 +0200 Subject: [PATCH] Refactoring VFormula::Eval(). Plus, in case of check If zero set value to Error instead of "0". This confuses users. --HG-- branch : develop --- src/libs/vpatterndb/vformula.cpp | 52 +++++++++++--------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/src/libs/vpatterndb/vformula.cpp b/src/libs/vpatterndb/vformula.cpp index 3ee27015b..3874d2f75 100644 --- a/src/libs/vpatterndb/vformula.cpp +++ b/src/libs/vpatterndb/vformula.cpp @@ -234,53 +234,35 @@ void VFormula::Eval() { return; } - if (formula.isEmpty()) - { - value = tr("Error"); - _error = true; - dValue = 0; - } - else + + value = tr("Error"); + _error = true; + dValue = 0; + qreal result = 0; + + if (not formula.isEmpty()) { try { QScopedPointer cal(new Calculator()); - QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); - const qreal result = cal->EvalFormula(data->DataVariables(), expression); - - if (qIsInf(result) || qIsNaN(result)) - { - value = tr("Error"); - _error = true; - dValue = 0; - } - else - { - //if result equal 0 - if (checkZero && qFuzzyIsNull(result)) - { - value = QString("0"); - _error = true; - dValue = 0; - } - else - { - dValue = result; - value = QString(qApp->LocaleToString(result) + " " + postfix); - _error = false; - } - } + const QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); + result = cal->EvalFormula(data->DataVariables(), expression); } catch (qmu::QmuParserError &e) { - value = tr("Error"); - _error = true; - dValue = 0; qDebug() << "\nMath parser error:\n" << "--------------------------------------\n" << "Message: " << e.GetMsg() << "\n" << "Expression: " << e.GetExpr() << "\n" << "--------------------------------------"; + return; + } + + if (not qIsInf(result) && not qIsNaN(result) && not (checkZero && qFuzzyIsNull(result))) + { + dValue = result; + value = qApp->LocaleToString(result) + QLatin1Char(' ') + postfix; + _error = false; } } }