Improving documantation and little refactoring changes in class Calculator.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-02-08 13:48:27 +02:00
parent 17fa00ba75
commit 074ca3a4e0

View File

@ -37,7 +37,7 @@ using namespace qmu;
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator class constructor. Make easy initialization math parser.
* @brief Calculator class wraper for QMuParser. Make easy initialization math parser.
*
* This constructor hide initialization variables, operators, character sets.
* Use this constuctor for evaluation formula. All formulas must be converted to internal look.
@ -99,11 +99,17 @@ Calculator::~Calculator()
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief eval calculate formula.
*
* First we try eval expression without adding variables. If it fail, we take tokens from expression and add variables
* to parser and try again.
*
* @param formula string of formula.
* @return value of formula.
*/
qreal Calculator::EvalFormula(const QString &formula)
{
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
// set value to 0.
SetVarFactory(AddVariable, this);
SetSepForEval();//Reset separators options
@ -119,20 +125,29 @@ qreal Calculator::EvalFormula(const QString &formula)
if (tokens.isEmpty())
{
return result;
return result; // We have found only numbers in expression.
}
// Add variables
// Add variables to parser because we have deal with expression with variables.
InitVariables(data, tokens, formula);
return Eval();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::InitVariables add variables to parser.
*
* For optimization purpose we try don't add variables that we don't need.
*
* @param data pointer to a variable container. Hold all informations about variables.
* @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression.
* @param formula expression, need for throwing better error message.
*/
void Calculator::InitVariables(const VContainer *data, const QMap<int, QString> &tokens, const QString &formula)
{
if (qApp->patternType() == MeasurementsType::Standard)
{
vVarVal = new qreal[2];
vVarVal = new qreal[2]; //stabdard measurements table have two additional variables
}
SCASSERT(data != nullptr)
@ -173,7 +188,7 @@ void Calculator::InitVariables(const VContainer *data, const QMap<int, QString>
}
if (builInFunctions.contains(i.value()))
{// We found built-in function
{// We have found built-in function
found = true;
}
@ -186,6 +201,13 @@ void Calculator::InitVariables(const VContainer *data, const QMap<int, QString>
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::InitCharacterSets init character set for parser.
*
* QMuParser require setting character set for legal characters. Because we try make our expresion language independent
* we set all posible unique characters from all alphabets.
*
*/
void Calculator::InitCharacterSets()
{
//String with all unique symbols for supported alpabets.
@ -218,6 +240,9 @@ qreal* Calculator::AddVariable(const QString &a_szName, void *a_pUserData)
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
*/
void Calculator::SetSepForEval()
{
SetArgSep(',');
@ -225,6 +250,10 @@ void Calculator::SetSepForEval()
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::SetSepForTr set separators for translation expression.
* @param fromUser true if expression come from user (from dialog).
*/
void Calculator::SetSepForTr(bool fromUser)
{
if (fromUser)
@ -233,29 +262,30 @@ void Calculator::SetSepForTr(bool fromUser)
if (osSeparatorValue)
{
QLocale loc = QLocale::system();
const QLocale loc = QLocale::system();
SetDecSep(loc.decimalPoint().toLatin1());
SetThousandsSep(loc.groupSeparator().toLatin1());
SetArgSep(';');
}
else
{
SetArgSep(',');
SetDecSep('.');
return;
}
}
else
{
SetArgSep(',');
SetDecSep('.');
}
SetSepForEval();//Same separators (internal) as for eval.
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::RemoveAll remove token from token list.
*
* Standard Qt class QMap doesn't have method RemoveAll.
* Example: remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
*
* @param map map with tokens
* @param val token that need delete
*/
void Calculator::RemoveAll(QMap<int, QString> &map, const QString &val)
{
QList<int> listKeys = map.keys(val);
const QList<int> listKeys = map.keys(val);//Take all keys that contain token.
if (listKeys.size() > 0)
{
for (int i = 0; i < listKeys.size(); ++i)