Making class Calculator independent from class VContainer.
--HG-- branch : develop
This commit is contained in:
parent
4fbae45406
commit
5cd9a88761
|
@ -2171,8 +2171,8 @@ bool TMainWindow::EvalFormula(const QString &formula, bool fromUser, VContainer
|
|||
f = formula;
|
||||
}
|
||||
f.replace("\n", " ");
|
||||
Calculator *cal = new Calculator(data, mType);
|
||||
const qreal result = UnitConvertor(cal->EvalFormula(f), mUnit, pUnit);
|
||||
Calculator *cal = new Calculator();
|
||||
const qreal result = UnitConvertor(cal->EvalFormula(data->PlainVariables(), f), mUnit, pUnit);
|
||||
delete cal;
|
||||
|
||||
label->setText(qApp->LocaleToString(result) + " " +postfix);
|
||||
|
|
|
@ -323,8 +323,8 @@ bool DialogIncrements::EvalIncrementFormula(const QString &formula, bool fromUse
|
|||
f = formula;
|
||||
}
|
||||
f.replace("\n", " ");
|
||||
Calculator *cal = new Calculator(data, qApp->patternType());
|
||||
const qreal result = cal->EvalFormula(f);
|
||||
Calculator *cal = new Calculator();
|
||||
const qreal result = cal->EvalFormula(data->PlainVariables(), f);
|
||||
delete cal;
|
||||
|
||||
label->setText(qApp->LocaleToString(result) + " " + postfix);
|
||||
|
|
|
@ -2043,8 +2043,8 @@ qreal VPattern::EvalFormula(VContainer *data, const QString &formula, bool *ok)
|
|||
// Replace line return character with spaces for calc if exist
|
||||
QString f = formula;
|
||||
f.replace("\n", " ");
|
||||
Calculator *cal = new Calculator(data, qApp->patternType());
|
||||
const qreal result = cal->EvalFormula(f);
|
||||
Calculator *cal = new Calculator();
|
||||
const qreal result = cal->EvalFormula(data->PlainVariables(), f);
|
||||
delete cal;
|
||||
|
||||
*ok = true;
|
||||
|
|
|
@ -829,8 +829,8 @@ qreal VMeasurements::EvalFormula(VContainer *data, const QString &formula, bool
|
|||
// Replace line return character with spaces for calc if exist
|
||||
QString f = formula;
|
||||
f.replace("\n", " ");
|
||||
Calculator *cal = new Calculator(data, type);
|
||||
const qreal result = cal->EvalFormula(f);
|
||||
Calculator *cal = new Calculator();
|
||||
const qreal result = cal->EvalFormula(data->PlainVariables(), f);
|
||||
delete cal;
|
||||
|
||||
*ok = true;
|
||||
|
|
|
@ -43,15 +43,14 @@ using namespace qmu;
|
|||
*
|
||||
* const QString formula = qApp->FormulaFromUser(edit->text());
|
||||
* Calculator *cal = new Calculator(data, patternType);
|
||||
* const qreal result = cal->EvalFormula(formula);
|
||||
* const qreal result = cal->EvalFormula(data->PlainVariables(), formula);
|
||||
* delete cal;
|
||||
*
|
||||
* @param data pointer to a variable container.
|
||||
*/
|
||||
Calculator::Calculator(const VContainer *data, MeasurementsType patternType)
|
||||
:QmuFormulaBase(), vVarVal(nullptr), data(data), patternType(patternType)
|
||||
Calculator::Calculator()
|
||||
:QmuFormulaBase()
|
||||
{
|
||||
SCASSERT(data != nullptr)
|
||||
InitCharacterSets();
|
||||
setAllowSubexpressions(false);//Only one expression per time
|
||||
|
||||
|
@ -61,7 +60,6 @@ Calculator::Calculator(const VContainer *data, MeasurementsType patternType)
|
|||
//---------------------------------------------------------------------------------------------------------------------
|
||||
Calculator::~Calculator()
|
||||
{
|
||||
delete [] vVarVal;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
|
@ -74,7 +72,7 @@ Calculator::~Calculator()
|
|||
* @param formula string of formula.
|
||||
* @return value of formula.
|
||||
*/
|
||||
qreal Calculator::EvalFormula(const QString &formula)
|
||||
qreal Calculator::EvalFormula(const QHash<QString, qreal *> &vars, 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.
|
||||
|
@ -106,7 +104,7 @@ qreal Calculator::EvalFormula(const QString &formula)
|
|||
}
|
||||
|
||||
// Add variables to parser because we have deal with expression with variables.
|
||||
InitVariables(data, tokens, formula);
|
||||
InitVariables(vars, tokens, formula);
|
||||
return Eval();
|
||||
}
|
||||
|
||||
|
@ -116,53 +114,28 @@ qreal Calculator::EvalFormula(const QString &formula)
|
|||
*
|
||||
* 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 vars list of 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)
|
||||
void Calculator::InitVariables(const QHash<QString, qreal *> &vars, const QMap<int, QString> &tokens,
|
||||
const QString &formula)
|
||||
{
|
||||
if (patternType == MeasurementsType::Standard)
|
||||
{
|
||||
vVarVal = new qreal[2]; //stabdard measurements table have two additional variables
|
||||
}
|
||||
|
||||
SCASSERT(data != nullptr)
|
||||
const QHash<QString, QSharedPointer<VInternalVariable> > *vars = data->DataVariables();
|
||||
|
||||
QMap<int, QString>::const_iterator i = tokens.constBegin();
|
||||
while (i != tokens.constEnd())
|
||||
{
|
||||
bool found = false;
|
||||
if (vars->contains(i.value()))
|
||||
if (vars.contains(i.value()))
|
||||
{
|
||||
QSharedPointer<VInternalVariable> var = vars->value(i.value());
|
||||
if (patternType == MeasurementsType::Standard && var->GetType() == VarType::Measurement)
|
||||
{
|
||||
QSharedPointer<VVariable> m = data->GetVariable<VVariable>(i.value());
|
||||
m->SetValue(data->size(), data->height(), *data->GetPatternUnit());
|
||||
}
|
||||
DefineVar(i.value(), var->GetValue());
|
||||
DefineVar(i.value(), vars.value(i.value()));
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (patternType == MeasurementsType::Standard)
|
||||
{
|
||||
if (i.value() == data->SizeName())
|
||||
{
|
||||
vVarVal[0] = data->size();
|
||||
DefineVar(data->SizeName(), &vVarVal[0]);
|
||||
if (found == false && builInFunctions.contains(i.value()))
|
||||
{// We have found built-in function
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (i.value() == data->HeightName())
|
||||
{
|
||||
vVarVal[1] = data->height();
|
||||
DefineVar(data->HeightName(), &vVarVal[1]);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false)
|
||||
{
|
||||
throw qmu::QmuParserError (ecUNASSIGNABLE_TOKEN, i.value(), formula, i.key());
|
||||
|
|
|
@ -48,23 +48,21 @@ class VContainer;
|
|||
* //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here.
|
||||
* //Don't know why.
|
||||
* delete dialog;
|
||||
* Calculator *cal = new Calculator(data);
|
||||
* result = cal->EvalFormula(formula);
|
||||
* Calculator *cal = new Calculator();
|
||||
* result = cal->EvalFormula(data->PlainVariables(), formula);
|
||||
* delete cal;//Here can be memory leak, but dialog already check this formula and probability very low.
|
||||
* }
|
||||
*/
|
||||
class Calculator:public qmu::QmuFormulaBase
|
||||
{
|
||||
public:
|
||||
Calculator(const VContainer *data, MeasurementsType patternType);
|
||||
Calculator();
|
||||
virtual ~Calculator();
|
||||
qreal EvalFormula(const QString &formula);
|
||||
qreal EvalFormula(const QHash<QString, qreal *> &vars, const QString &formula);
|
||||
private:
|
||||
Q_DISABLE_COPY(Calculator)
|
||||
qreal *vVarVal;
|
||||
const VContainer *data;
|
||||
MeasurementsType patternType;
|
||||
void InitVariables(const VContainer *data, const QMap<int, QString> &tokens, const QString &formula);
|
||||
void InitVariables(const QHash<QString, qreal *> &vars, const QMap<int, QString> &tokens,
|
||||
const QString &formula);
|
||||
static void RemoveAll(QMap<int, QString> &map, const QString &val);
|
||||
};
|
||||
|
||||
|
|
|
@ -489,9 +489,9 @@ const QMap<QString, QSharedPointer<VSplineAngle> > VContainer::DataAnglesCurves(
|
|||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
const QHash<QString, const qreal *> VContainer::PlainVariables() const
|
||||
const QHash<QString, qreal *> VContainer::PlainVariables() const
|
||||
{
|
||||
QHash<QString, const qreal *> vars;
|
||||
QHash<QString, qreal *> vars;
|
||||
|
||||
QHash<QString, QSharedPointer<VInternalVariable>>::const_iterator i = d->variables.constBegin();
|
||||
while (i != d->variables.constEnd())
|
||||
|
@ -504,6 +504,8 @@ const QHash<QString, const qreal *> VContainer::PlainVariables() const
|
|||
m->SetValue(size(), height(), qApp->patternUnit());
|
||||
}
|
||||
vars.insert(i.key(), var->GetValue());
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
if (qApp->patternType() == MeasurementsType::Standard)
|
||||
|
@ -619,7 +621,7 @@ qreal VContainer::size()
|
|||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
const qreal *VContainer::rsize()
|
||||
qreal *VContainer::rsize()
|
||||
{
|
||||
return &_size;
|
||||
}
|
||||
|
@ -641,7 +643,7 @@ qreal VContainer::height()
|
|||
}
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------
|
||||
const qreal *VContainer::rheight()
|
||||
qreal *VContainer::rheight()
|
||||
{
|
||||
return &_height;
|
||||
}
|
||||
|
|
|
@ -138,10 +138,10 @@ public:
|
|||
static void SetHeight(qreal height);
|
||||
void SetHeightName(const QString &name);
|
||||
static qreal size();
|
||||
static const qreal *rsize();
|
||||
static qreal *rsize();
|
||||
QString SizeName() const;
|
||||
static qreal height();
|
||||
static const qreal *rheight();
|
||||
static qreal *rheight();
|
||||
QString HeightName()const;
|
||||
|
||||
bool VariableExist(const QString& name);
|
||||
|
@ -162,7 +162,7 @@ public:
|
|||
const QMap<QString, QSharedPointer<VArcAngle> > DataAnglesArcs() const;
|
||||
const QMap<QString, QSharedPointer<VSplineAngle> > DataAnglesCurves() const;
|
||||
|
||||
const QHash<QString, const qreal *> PlainVariables() const;
|
||||
const QHash<QString, qreal *> PlainVariables() const;
|
||||
|
||||
static bool IsUnique(const QString &name);
|
||||
|
||||
|
|
|
@ -226,9 +226,9 @@ void VFormula::Eval()
|
|||
{
|
||||
try
|
||||
{
|
||||
Calculator *cal = new Calculator(data, qApp->patternType());
|
||||
Calculator *cal = new Calculator();
|
||||
QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
|
||||
const qreal result = cal->EvalFormula(expression);
|
||||
const qreal result = cal->EvalFormula(data->PlainVariables(), expression);
|
||||
delete cal;
|
||||
|
||||
//if result equal 0
|
||||
|
|
|
@ -364,8 +364,8 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt
|
|||
formula.replace("\n", " ");
|
||||
// Translate to internal look.
|
||||
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
|
||||
Calculator *cal = new Calculator(data, qApp->patternType());
|
||||
result = cal->EvalFormula(formula);
|
||||
Calculator *cal = new Calculator();
|
||||
result = cal->EvalFormula(data->PlainVariables(), formula);
|
||||
delete cal;
|
||||
|
||||
//if result equal 0
|
||||
|
|
|
@ -285,8 +285,8 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
|
|||
Calculator *cal = nullptr;
|
||||
try
|
||||
{
|
||||
cal = new Calculator(data, qApp->patternType());
|
||||
result = cal->EvalFormula(formula);
|
||||
cal = new Calculator();
|
||||
result = cal->EvalFormula(data->PlainVariables(), formula);
|
||||
delete cal;
|
||||
}
|
||||
catch (qmu::QmuParserError &e)
|
||||
|
@ -316,8 +316,8 @@ qreal VDrawTool::CheckFormula(const quint32 &toolId, QString &formula, VContaine
|
|||
/* Need delete dialog here because parser in dialog don't allow use correct separator for
|
||||
* parsing here. */
|
||||
delete dialog;
|
||||
Calculator *cal1 = new Calculator(data, qApp->patternType());
|
||||
result = cal1->EvalFormula(formula);
|
||||
Calculator *cal1 = new Calculator();
|
||||
result = cal1->EvalFormula(data->PlainVariables(), formula);
|
||||
delete cal1; /* Here can be memory leak, but dialog already check this formula and probability
|
||||
* very low. */
|
||||
break;
|
||||
|
|
|
@ -147,8 +147,8 @@ qreal Visualization::FindVal(const QString &expression)
|
|||
QString formula = expression;
|
||||
formula.replace("\n", " ");
|
||||
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
|
||||
Calculator *cal = new Calculator(Visualization::data, qApp->patternType());
|
||||
val = cal->EvalFormula(formula);
|
||||
Calculator *cal = new Calculator();
|
||||
val = cal->EvalFormula(data->PlainVariables(), formula);
|
||||
delete cal;
|
||||
}
|
||||
catch (qmu::QmuParserError &e)
|
||||
|
|
Loading…
Reference in New Issue
Block a user