Improwing documentation.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-02-08 15:43:30 +02:00
parent 074ca3a4e0
commit 7064dbfaa2
2 changed files with 113 additions and 42 deletions

View File

@ -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 bool VApplication::MeasurementsFromUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
QMap<QString, QmuTranslation>::const_iterator i = measurements.constBegin(); QMap<QString, QmuTranslation>::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 bool VApplication::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
QMap<QString, QmuTranslation>::const_iterator i = variables.constBegin(); QMap<QString, QmuTranslation>::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 bool VApplication::PostfixOperatorsFromUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
QMap<QString, QmuTranslation>::const_iterator i = postfixOperators.constBegin(); QMap<QString, QmuTranslation>::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 bool VApplication::FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
QMap<QString, QmuTranslation>::const_iterator i = functions.constBegin(); QMap<QString, QmuTranslation>::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 bool VApplication::VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
QMap<QString, QmuTranslation>::const_iterator i = variables.constBegin(); QMap<QString, QmuTranslation>::const_iterator i = variables.constBegin();
@ -1594,18 +1634,37 @@ bool VApplication::VariablesToUser(QString &newFormula, int position, const QStr
return false; 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<int, QString> &tokens, void VApplication::CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens,
QMap<int, QString> &numbers) QMap<int, QString> &numbers)
{ {
if (bias == 0) if (bias == 0)
{ {
return; return;// Nothing to correct;
} }
BiasTokens(position, bias, tokens); BiasTokens(position, bias, tokens);
BiasTokens(position, bias, numbers); 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<int, QString> &tokens) const void VApplication::BiasTokens(int position, int bias, QMap<int, QString> &tokens) const
{ {
QMap<int, QString> newTokens; QMap<int, QString> newTokens;
@ -1613,7 +1672,7 @@ void VApplication::BiasTokens(int position, int bias, QMap<int, QString> &tokens
while (i != tokens.constEnd()) while (i != tokens.constEnd())
{ {
if (i.key()<= position) if (i.key()<= position)
{ { // Tokens before position "position" did not change his positions.
newTokens.insert(i.key(), i.value()); newTokens.insert(i.key(), i.value());
} }
else 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 VApplication::FormulaFromUser(const QString &formula)
{ {
QString newFormula = formula; QString newFormula = formula;// Local copy for making changes
Calculator *cal = new Calculator(formula); Calculator *cal = new Calculator(formula);// Eval formula
QMap<int, QString> tokens = cal->GetTokens(); QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements)
QMap<int, QString> numbers = cal->GetNumbers(); QMap<int, QString> numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator
delete cal; delete cal;
QList<int> tKeys = tokens.keys(); QList<int> tKeys = tokens.keys();// Take all tokens positions
QList<QString> tValues = tokens.values(); QList<QString> tValues = tokens.values();
for (int i = 0; i < tKeys.size(); ++i) 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 (MeasurementsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{ {
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); tValues = tokens.values();
@ -1733,7 +1798,7 @@ QString VApplication::FormulaFromUser(const QString &formula)
if (VariablesFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) if (VariablesFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{ {
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); tValues = tokens.values();
@ -1744,7 +1809,7 @@ QString VApplication::FormulaFromUser(const QString &formula)
if (PostfixOperatorsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) if (PostfixOperatorsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{ {
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); tValues = tokens.values();
@ -1755,7 +1820,7 @@ QString VApplication::FormulaFromUser(const QString &formula)
if (FunctionsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) if (FunctionsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{ {
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); 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()) if (loc != QLocale(QLocale::C) && getSettings()->GetOsSeparator())
{ {// User want use Os separator
QList<int> nKeys = numbers.keys(); QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values(); QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i) for (int i = 0; i < nKeys.size(); ++i)
{ {
loc = QLocale::system(); loc = QLocale::system();// From system locale
bool ok = false; bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok); const qreal d = loc.toDouble(nValues.at(i), &ok);
if (ok == false) if (ok == false)
{ {
qDebug()<<"Can't convert to double token"<<nValues.at(i); qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue; continue;//Leave with out translation
} }
loc = QLocale(QLocale::C); loc = QLocale(QLocale::C);// To internal locale
const QString dStr = loc.toString(d); const QString dStr = loc.toString(d);// Internal look for number
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr); newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length(); const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0) if (bias != 0)
{ {// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers); CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys(); nKeys = numbers.keys();
nValues = numbers.values(); nValues = numbers.values();
@ -1797,17 +1862,23 @@ QString VApplication::FormulaFromUser(const QString &formula)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/**
* @brief VApplication::FormulaToUser replace all known tokens in formula to user look. Also change decimal
* separator in numbers.
* @param formula expression that need translate
* @return translated expression
*/
QString VApplication::FormulaToUser(const QString &formula) QString VApplication::FormulaToUser(const QString &formula)
{ {
QString newFormula = formula; QString newFormula = formula;// Local copy for making changes
QMap<int, QString> tokens; QMap<int, QString> tokens;
QMap<int, QString> numbers; QMap<int, QString> numbers;
try try
{ {
Calculator *cal = new Calculator(formula, false); Calculator *cal = new Calculator(formula, false);// Eval formula
tokens = cal->GetTokens(); tokens = cal->GetTokens();// Tokens (variables, measurements)
numbers = cal->GetNumbers(); numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator
delete cal; delete cal;
} }
catch (qmu::QmuParserError &e) 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()); 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(); int bias = tValues.at(i).length() - measurements.value(tValues.at(i)).translate().length();
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); 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()); 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(); int bias = tValues.at(i).length() - functions.value(tValues.at(i)).translate().length();
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); 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()); 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(); int bias = tValues.at(i).length() - postfixOperators.value(tValues.at(i)).translate().length();
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); tValues = tokens.values();
@ -1867,7 +1938,7 @@ QString VApplication::FormulaToUser(const QString &formula)
if (VariablesToUser(newFormula, tKeys.at(i), tValues.at(i), bias)) if (VariablesToUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{ {
if (bias != 0) if (bias != 0)
{ {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys(); tKeys = tokens.keys();
tValues = tokens.values(); 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()) if (loc != QLocale::C && getSettings()->GetOsSeparator())
{ {// User want use Os separator
QList<int> nKeys = numbers.keys(); QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values(); QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i) for (int i = 0; i < nKeys.size(); ++i)
{ {
loc = QLocale(QLocale::C); loc = QLocale(QLocale::C);// From pattern locale
bool ok = false; bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok); const qreal d = loc.toDouble(nValues.at(i), &ok);
if (ok == false) if (ok == false)
{ {
qDebug()<<"Can't convert to double token"<<nValues.at(i); qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue; continue;//Leave with out translation
} }
loc = QLocale::system(); loc = QLocale::system();// To user locale
const QString dStr = loc.toString(d); const QString dStr = loc.toString(d);// Number string in user locale
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr); newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length(); const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0) if (bias != 0)
{ {// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers); CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys(); nKeys = numbers.keys();
nValues = numbers.values(); nValues = numbers.values();

View File

@ -460,8 +460,8 @@ void DialogTool::ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer *tim
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief Eval evaluate formula and show result * @brief Eval evaluate formula and show result
* @param text formula * @param text expresion that we parse
* @param flag flag state of formula * @param flag flag state of eval formula
* @param label label for signal error * @param label label for signal error
* @param postfix unit name * @param postfix unit name
* @param checkZero true - if formula can't be equal zero * @param checkZero true - if formula can't be equal zero
@ -484,10 +484,10 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt
{ {
try try
{ {
// Replace line return with spaces for calc if exist // Replace line return character with spaces for calc if exist
QString formula = text; QString formula = text;
formula.replace("\n", " "); formula.replace("\n", " ");
formula = qApp->FormulaFromUser(formula); formula = qApp->FormulaFromUser(formula);// Translate to internal look.
Calculator *cal = new Calculator(data); Calculator *cal = new Calculator(data);
result = cal->EvalFormula(formula); result = cal->EvalFormula(formula);
delete cal; delete cal;
@ -511,7 +511,7 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt
{ {
loc = QLocale(QLocale::C); loc = QLocale(QLocale::C);
} }
label->setText(loc.toString(result) + postfix); label->setText(loc.toString(result) + " " +postfix);
flag = true; flag = true;
ChangeColor(labelEditFormula, okColor); ChangeColor(labelEditFormula, okColor);
label->setToolTip(tr("Value")); 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; return result;
} }
@ -886,7 +886,7 @@ void DialogTool::EvalFormula()
{ {
SCASSERT(plainTextEditFormula != nullptr); SCASSERT(plainTextEditFormula != nullptr);
SCASSERT(labelResultCalculation != 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); Eval(plainTextEditFormula->toPlainText(), flagFormula, labelResultCalculation, postfix, false);
} }