2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file calculator.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-07-17 13:38:11 +02:00
|
|
|
#include "calculator.h"
|
|
|
|
#include <QDebug>
|
2014-05-29 10:46:28 +02:00
|
|
|
#include <QSettings>
|
2014-06-08 20:10:57 +02:00
|
|
|
#include "vcontainer.h"
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2014-06-02 09:43:27 +02:00
|
|
|
using namespace qmu;
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-07 18:55:57 +02:00
|
|
|
/**
|
2015-02-08 12:48:27 +01:00
|
|
|
* @brief Calculator class wraper for QMuParser. Make easy initialization math parser.
|
2014-05-30 15:04:38 +02:00
|
|
|
*
|
|
|
|
* This constructor hide initialization variables, operators, character sets.
|
|
|
|
* Use this constuctor for evaluation formula. All formulas must be converted to internal look.
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* const QString formula = qApp->FormulaFromUser(edit->text());
|
2015-06-12 10:55:44 +02:00
|
|
|
* Calculator *cal = new Calculator(data, patternType);
|
2014-05-30 15:04:38 +02:00
|
|
|
* const qreal result = cal->EvalFormula(formula);
|
|
|
|
* delete cal;
|
|
|
|
*
|
2014-05-07 18:55:57 +02:00
|
|
|
* @param data pointer to a variable container.
|
|
|
|
*/
|
2015-06-12 10:55:44 +02:00
|
|
|
Calculator::Calculator(const VContainer *data, MeasurementsType patternType)
|
2015-08-09 19:46:21 +02:00
|
|
|
:QmuFormulaBase(), vVarVal(nullptr), data(data), patternType(patternType)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-11-22 13:49:09 +01:00
|
|
|
SCASSERT(data != nullptr)
|
2014-05-23 20:11:13 +02:00
|
|
|
InitCharacterSets();
|
2014-08-29 15:42:53 +02:00
|
|
|
setAllowSubexpressions(false);//Only one expression per time
|
2014-09-08 15:17:38 +02:00
|
|
|
|
2014-09-20 17:00:29 +02:00
|
|
|
SetSepForEval();
|
2014-05-23 20:11:13 +02:00
|
|
|
}
|
2014-05-22 14:11:14 +02:00
|
|
|
|
2014-11-22 13:49:09 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-21 15:51:47 +02:00
|
|
|
Calculator::~Calculator()
|
|
|
|
{
|
|
|
|
delete [] vVarVal;
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-07 18:55:57 +02:00
|
|
|
/**
|
2014-05-21 10:51:16 +02:00
|
|
|
* @brief eval calculate formula.
|
2015-02-08 12:48:27 +01:00
|
|
|
*
|
|
|
|
* First we try eval expression without adding variables. If it fail, we take tokens from expression and add variables
|
|
|
|
* to parser and try again.
|
|
|
|
*
|
2014-05-21 10:51:16 +02:00
|
|
|
* @param formula string of formula.
|
2014-05-07 18:55:57 +02:00
|
|
|
* @return value of formula.
|
|
|
|
*/
|
2014-05-21 10:51:16 +02:00
|
|
|
qreal Calculator::EvalFormula(const QString &formula)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
|
|
|
|
// set value to 0.
|
2014-07-29 17:15:18 +02:00
|
|
|
SetVarFactory(AddVariable, this);
|
2014-09-20 17:00:29 +02:00
|
|
|
SetSepForEval();//Reset separators options
|
2014-07-29 17:15:18 +02:00
|
|
|
|
2014-05-21 10:51:16 +02:00
|
|
|
SetExpr(formula);
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
qreal result = 0;
|
|
|
|
result = Eval();
|
|
|
|
|
|
|
|
QMap<int, QString> tokens = this->GetTokens();
|
2014-11-09 15:01:25 +01:00
|
|
|
|
|
|
|
// Remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
|
2014-11-22 13:49:09 +01:00
|
|
|
RemoveAll(tokens, QStringLiteral("-"));
|
2014-11-09 15:01:25 +01:00
|
|
|
|
2015-05-14 18:20:34 +02:00
|
|
|
for (int i = 0; i < builInFunctions.size(); ++i)
|
|
|
|
{
|
|
|
|
if (tokens.isEmpty())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
RemoveAll(tokens, builInFunctions.at(i));
|
|
|
|
}
|
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
if (tokens.isEmpty())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
return result; // We have found only numbers in expression.
|
2014-05-21 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:48:27 +01:00
|
|
|
// Add variables to parser because we have deal with expression with variables.
|
2014-09-06 10:14:59 +02:00
|
|
|
InitVariables(data, tokens, formula);
|
|
|
|
return Eval();
|
2014-07-29 17:15:18 +02:00
|
|
|
}
|
2014-05-21 15:51:47 +02:00
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2014-09-06 10:14:59 +02:00
|
|
|
void Calculator::InitVariables(const VContainer *data, const QMap<int, QString> &tokens, const QString &formula)
|
2014-07-29 17:15:18 +02:00
|
|
|
{
|
2015-06-12 10:55:44 +02:00
|
|
|
if (patternType == MeasurementsType::Standard)
|
2014-05-21 15:51:47 +02:00
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
vVarVal = new qreal[2]; //stabdard measurements table have two additional variables
|
2013-07-17 13:38:11 +02:00
|
|
|
}
|
|
|
|
|
2014-11-22 13:49:09 +01:00
|
|
|
SCASSERT(data != nullptr)
|
2014-08-21 14:44:40 +02:00
|
|
|
const QHash<QString, QSharedPointer<VInternalVariable> > *vars = data->DataVariables();
|
2014-05-21 10:51:16 +02:00
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
QMap<int, QString>::const_iterator i = tokens.constBegin();
|
|
|
|
while (i != tokens.constEnd())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-09-11 19:15:07 +02:00
|
|
|
bool found = false;
|
2014-07-29 17:15:18 +02:00
|
|
|
if (vars->contains(i.value()))
|
2014-05-21 10:51:16 +02:00
|
|
|
{
|
2014-08-21 14:44:40 +02:00
|
|
|
QSharedPointer<VInternalVariable> var = vars->value(i.value());
|
2015-08-25 12:35:50 +02:00
|
|
|
if (patternType == MeasurementsType::Standard && var->GetType() == VarType::Measurement)
|
2014-05-21 10:51:16 +02:00
|
|
|
{
|
2014-08-21 14:44:40 +02:00
|
|
|
QSharedPointer<VVariable> m = data->GetVariable<VVariable>(i.value());
|
2015-06-12 10:55:44 +02:00
|
|
|
m->SetValue(data->size(), data->height(), *data->GetPatternUnit());
|
2014-05-21 10:51:16 +02:00
|
|
|
}
|
2014-07-29 17:15:18 +02:00
|
|
|
DefineVar(i.value(), var->GetValue());
|
2014-09-06 10:14:59 +02:00
|
|
|
found = true;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2015-06-12 10:55:44 +02:00
|
|
|
if (patternType == MeasurementsType::Standard)
|
2014-05-21 10:51:16 +02:00
|
|
|
{
|
2014-07-29 17:15:18 +02:00
|
|
|
if (i.value() == data->SizeName())
|
|
|
|
{
|
|
|
|
vVarVal[0] = data->size();
|
|
|
|
DefineVar(data->SizeName(), &vVarVal[0]);
|
2014-09-06 10:14:59 +02:00
|
|
|
found = true;
|
2014-07-29 17:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i.value() == data->HeightName())
|
2014-05-21 10:51:16 +02:00
|
|
|
{
|
2014-07-29 17:15:18 +02:00
|
|
|
vVarVal[1] = data->height();
|
|
|
|
DefineVar(data->HeightName(), &vVarVal[1]);
|
2014-09-06 10:14:59 +02:00
|
|
|
found = true;
|
2014-05-21 10:51:16 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 10:14:59 +02:00
|
|
|
|
|
|
|
if (found == false)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
throw qmu::QmuParserError (ecUNASSIGNABLE_TOKEN, i.value(), formula, i.key());
|
2014-09-06 10:14:59 +02:00
|
|
|
}
|
2014-07-29 17:15:18 +02:00
|
|
|
++i;
|
2013-07-17 13:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 15:01:25 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2014-11-09 15:01:25 +01:00
|
|
|
void Calculator::RemoveAll(QMap<int, QString> &map, const QString &val)
|
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
const QList<int> listKeys = map.keys(val);//Take all keys that contain token.
|
2014-11-09 15:01:25 +01:00
|
|
|
if (listKeys.size() > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < listKeys.size(); ++i)
|
|
|
|
{
|
|
|
|
map.remove(listKeys.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|