diff --git a/src/app/core/vapplication.cpp b/src/app/core/vapplication.cpp index c23c427c4..34b5f0ad7 100644 --- a/src/app/core/vapplication.cpp +++ b/src/app/core/vapplication.cpp @@ -1505,6 +1505,14 @@ void VApplication::InitSTDescriptions() } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::MeasurementsFromUser translate measurement to internal look. + * @param newFormula [in|out] expression to translate + * @param position token position + * @param token token to translate + * @param bias hold change of length between translated and origin token string + * @return true if was found measurement with same name. + */ bool VApplication::MeasurementsFromUser(QString &newFormula, int position, const QString &token, int &bias) const { QMap::const_iterator i = measurements.constBegin(); @@ -1522,6 +1530,14 @@ bool VApplication::MeasurementsFromUser(QString &newFormula, int position, const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::VariablesFromUser translate variable to internal look. + * @param newFormula [in|out] expression to translate + * @param position token position + * @param token token to translate + * @param bias hold change of length between translated and origin token string + * @return true if was found variable with same name. + */ bool VApplication::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const { QMap::const_iterator i = variables.constBegin(); @@ -1541,6 +1557,14 @@ bool VApplication::VariablesFromUser(QString &newFormula, int position, const QS } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::PostfixOperatorsFromUser translate postfix operator to internal look. + * @param newFormula [in|out] expression to translate + * @param position token position + * @param token token to translate + * @param bias hold change of length between translated and origin token string + * @return true if was found postfix operator with same name. + */ bool VApplication::PostfixOperatorsFromUser(QString &newFormula, int position, const QString &token, int &bias) const { QMap::const_iterator i = postfixOperators.constBegin(); @@ -1558,6 +1582,14 @@ bool VApplication::PostfixOperatorsFromUser(QString &newFormula, int position, c } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::FunctionsFromUser translate function name to internal look. + * @param newFormula [in|out] expression to translate + * @param position token position + * @param token token to translate + * @param bias hold change of length between translated and origin token string + * @return true if was found function with same name. + */ bool VApplication::FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const { QMap::const_iterator i = functions.constBegin(); @@ -1575,6 +1607,14 @@ bool VApplication::FunctionsFromUser(QString &newFormula, int position, const QS } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::VariablesToUser translate variable name to user. + * @param newFormula [in|out] expression to translate + * @param position token position + * @param token token to translate + * @param bias hold change of length between translated and origin token string + * @return true if was found variable with same name. + */ bool VApplication::VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const { QMap::const_iterator i = variables.constBegin(); @@ -1594,18 +1634,37 @@ bool VApplication::VariablesToUser(QString &newFormula, int position, const QStr return false; } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::CorrectionsPositions correct position tokens in expression after token translation. + * + * Because translated string can have different length compare to original need make correction after each translation. + * If bias = 0 correction will not happens. + * + * @param position position currecnt token in expression + * @param bias difference between original token length and translated + * @param tokens all tokens + * @param numbers all numbers + */ void VApplication::CorrectionsPositions(int position, int bias, QMap &tokens, QMap &numbers) { if (bias == 0) { - return; + return;// Nothing to correct; } BiasTokens(position, bias, tokens); BiasTokens(position, bias, numbers); } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::BiasTokens change position for each token that have position more then "position". + * @param position token position + * @param bias difference between original token length and translated + * @param tokens all tokens + */ void VApplication::BiasTokens(int position, int bias, QMap &tokens) const { QMap newTokens; @@ -1613,7 +1672,7 @@ void VApplication::BiasTokens(int position, int bias, QMap &tokens while (i != tokens.constEnd()) { if (i.key()<= position) - { + { // Tokens before position "position" did not change his positions. newTokens.insert(i.key(), i.value()); } else @@ -1705,16 +1764,22 @@ QString VApplication::PostfixOperator(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- +/** + * @brief VApplication::FormulaFromUser replace all known tokens in formula to internal look. Also change decimal + * separator in numbers. + * @param formula expression that need translate + * @return translated expression + */ QString VApplication::FormulaFromUser(const QString &formula) { - QString newFormula = formula; + QString newFormula = formula;// Local copy for making changes - Calculator *cal = new Calculator(formula); - QMap tokens = cal->GetTokens(); - QMap numbers = cal->GetNumbers(); + Calculator *cal = new Calculator(formula);// Eval formula + QMap tokens = cal->GetTokens();// Tokens (variables, measurements) + QMap numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator delete cal; - QList tKeys = tokens.keys(); + QList tKeys = tokens.keys();// Take all tokens positions QList tValues = tokens.values(); for (int i = 0; i < tKeys.size(); ++i) { @@ -1722,7 +1787,7 @@ QString VApplication::FormulaFromUser(const QString &formula) if (MeasurementsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) { if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1733,7 +1798,7 @@ QString VApplication::FormulaFromUser(const QString &formula) if (VariablesFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) { if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1744,7 +1809,7 @@ QString VApplication::FormulaFromUser(const QString &formula) if (PostfixOperatorsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) { if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1755,7 +1820,7 @@ QString VApplication::FormulaFromUser(const QString &formula) if (FunctionsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) { if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1764,28 +1829,28 @@ QString VApplication::FormulaFromUser(const QString &formula) } } - QLocale loc = QLocale::system(); + QLocale loc = QLocale::system(); // User locale if (loc != QLocale(QLocale::C) && getSettings()->GetOsSeparator()) - { - QList nKeys = numbers.keys(); + {// User want use Os separator + QList nKeys = numbers.keys();// Positions for all numbers in expression QList nValues = numbers.values(); for (int i = 0; i < nKeys.size(); ++i) { - loc = QLocale::system(); + loc = QLocale::system();// From system locale bool ok = false; const qreal d = loc.toDouble(nValues.at(i), &ok); if (ok == false) { qDebug()<<"Can't convert to double token"< tokens; QMap numbers; try { - Calculator *cal = new Calculator(formula, false); - tokens = cal->GetTokens(); - numbers = cal->GetNumbers(); + Calculator *cal = new Calculator(formula, false);// Eval formula + tokens = cal->GetTokens();// Tokens (variables, measurements) + numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator delete cal; } catch (qmu::QmuParserError &e) @@ -1829,7 +1900,7 @@ QString VApplication::FormulaToUser(const QString &formula) newFormula.replace(tKeys.at(i), tValues.at(i).length(), measurements.value(tValues.at(i)).translate()); int bias = tValues.at(i).length() - measurements.value(tValues.at(i)).translate().length(); if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1842,7 +1913,7 @@ QString VApplication::FormulaToUser(const QString &formula) newFormula.replace(tKeys.at(i), tValues.at(i).length(), functions.value(tValues.at(i)).translate()); int bias = tValues.at(i).length() - functions.value(tValues.at(i)).translate().length(); if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1855,7 +1926,7 @@ QString VApplication::FormulaToUser(const QString &formula) newFormula.replace(tKeys.at(i), tValues.at(i).length(), postfixOperators.value(tValues.at(i)).translate()); int bias = tValues.at(i).length() - postfixOperators.value(tValues.at(i)).translate().length(); if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1867,7 +1938,7 @@ QString VApplication::FormulaToUser(const QString &formula) if (VariablesToUser(newFormula, tKeys.at(i), tValues.at(i), bias)) { if (bias != 0) - { + {// Translated token has different length than original. Position next tokens need to be corrected. CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); tKeys = tokens.keys(); tValues = tokens.values(); @@ -1876,28 +1947,28 @@ QString VApplication::FormulaToUser(const QString &formula) } } - QLocale loc = QLocale::system(); + QLocale loc = QLocale::system();// User locale if (loc != QLocale::C && getSettings()->GetOsSeparator()) - { - QList nKeys = numbers.keys(); + {// User want use Os separator + QList nKeys = numbers.keys();// Positions for all numbers in expression QList nValues = numbers.values(); for (int i = 0; i < nKeys.size(); ++i) { - loc = QLocale(QLocale::C); + loc = QLocale(QLocale::C);// From pattern locale bool ok = false; const qreal d = loc.toDouble(nValues.at(i), &ok); if (ok == false) { qDebug()<<"Can't convert to double token"<FormulaFromUser(formula); + formula = qApp->FormulaFromUser(formula);// Translate to internal look. Calculator *cal = new Calculator(data); result = cal->EvalFormula(formula); delete cal; @@ -511,7 +511,7 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt { loc = QLocale(QLocale::C); } - label->setText(loc.toString(result) + postfix); + label->setText(loc.toString(result) + " " +postfix); flag = true; ChangeColor(labelEditFormula, okColor); label->setToolTip(tr("Value")); @@ -532,7 +532,7 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt << "--------------------------------------"; } } - CheckState(); + CheckState(); // Disable Ok and Apply buttons if something wrong. return result; } @@ -886,7 +886,7 @@ void DialogTool::EvalFormula() { SCASSERT(plainTextEditFormula != nullptr); SCASSERT(labelResultCalculation != nullptr); - const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit()); + const QString postfix = VDomDocument::UnitsToStr(qApp->patternUnit());//Show unit in dialog lable (cm, mm or inch) Eval(plainTextEditFormula->toPlainText(), flagFormula, labelResultCalculation, postfix, false); }