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)
|
|
|
|
|
:QmuParser(), 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-05-23 20:11:13 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-30 15:04:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* @brief Calculator class constructor. Make easy initialization math parser.
|
|
|
|
|
*
|
|
|
|
|
* This constructor hide initialization variables, operators, character sets.
|
|
|
|
|
* Use this constuctor to get tokens from formula. All formulas must be converted to external look.
|
|
|
|
|
* Example:
|
|
|
|
|
*
|
2015-06-12 10:55:44 +02:00
|
|
|
|
* Calculator *cal = new Calculator(formula, osSeparator, false);
|
2014-05-30 15:04:38 +02:00
|
|
|
|
* tokens = cal->GetTokens();
|
|
|
|
|
* numbers = cal->GetNumbers();
|
|
|
|
|
* delete cal;
|
|
|
|
|
*
|
2014-06-02 09:43:27 +02:00
|
|
|
|
* @param formula string with formula.
|
2014-05-30 15:04:38 +02:00
|
|
|
|
* @param fromUser true if we parse formula from user
|
|
|
|
|
*/
|
2015-06-12 10:55:44 +02:00
|
|
|
|
Calculator::Calculator(const QString &formula, bool osSeparator, bool fromUser)
|
|
|
|
|
:QmuParser(), vVarVal(nullptr), data(nullptr), patternType(MeasurementsType::Standard)
|
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-05-22 14:11:14 +02:00
|
|
|
|
SetVarFactory(AddVariable, this);
|
2014-05-23 20:11:13 +02:00
|
|
|
|
|
2015-06-12 10:55:44 +02:00
|
|
|
|
SetSepForTr(osSeparator, fromUser);
|
2014-05-23 20:11:13 +02:00
|
|
|
|
|
|
|
|
|
SetExpr(formula);
|
2014-08-29 15:42:53 +02:00
|
|
|
|
//Need run for making tokens. Don't catch exception here, because we want know if formula has error.
|
2014-05-30 15:04:38 +02:00
|
|
|
|
Eval();
|
2013-07-17 13:38:11 +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-06-12 10:55:44 +02:00
|
|
|
|
if ((patternType == MeasurementsType::Standard) &&
|
2014-07-29 17:15:18 +02:00
|
|
|
|
(var->GetType() == VarType::Measurement || var->GetType() == VarType::Increment))
|
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-05-30 15:04:38 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
|
/**
|
|
|
|
|
* @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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2014-05-23 20:11:13 +02:00
|
|
|
|
void Calculator::InitCharacterSets()
|
|
|
|
|
{
|
|
|
|
|
//String with all unique symbols for supported alpabets.
|
|
|
|
|
// See script alphabets.py for generation and more information.
|
2014-10-18 13:51:40 +02:00
|
|
|
|
//Note. MSVC doen't support normal string concatenation for long string. Thats why we use QStringList in this place.
|
|
|
|
|
QStringList symbols = QStringList() << "ցЀĆЈVӧĎАғΕĖӅИқΝĞơРңњΥĦШҫ̆جگĮаҳѕεشԶиһνԾрυلՆӝшËՎҔPÓՖXӛӟŞӣզhëծpóӞնxßվāŁЃֆĉЋ"
|
|
|
|
|
<< "CŬđҐГΒęҘЛΚŘġҠУGاհЫدԱҰгβطԹõлκKՁÀуςهՉÈыvیՑÐSOřӘћաőcӐթèkàѓżűðsķչøӥӔĀփїІĈЎ"
|
|
|
|
|
<< "ґĐΗЖҙĘȚΟОҡĠآΧЦتЮұİزηжԸغοоÁՀقχцÉՈيюÑՐђӋіәťӆўáŠĺѐfөըnñŰӤӨӹոľЁրăЉŭċБӸēłΔҖ"
|
|
|
|
|
<< "ЙŤěΜӜDСձģΤӰЩīņحҮбưԳصδHйԻŇμӲӴсՃمτƠщՋєLQŹՓŕÖYśÞaգĽæiŽիӓîqճöyջþĂօЄӦĊЌΑĒДҗј"
|
|
|
|
|
<< "ΙȘĚМΡéĵĢФūӚΩبĪЬүќαذԲдҷιظԺмρՂфÇωوՊьÏՒTŚĻJբdçժlïӪղtպӫAւąЇčŃЏĕӯЗΖEțŮĝПΞأĥ"
|
|
|
|
|
<< "ثĹЧΦÆӳЯIسŲԵзζԽпξكՅÄчφNMՍӌяӢՕÔWÎŝÜџёźեägխoӒյôwĶBžսüЂĄև̈ЊČƏљΓВҕĔӮΛКĜΣТҥĤک"
|
|
|
|
|
<< "ЪƯخγвŅԴŪضλкԼĴσтÅՄنъÍՌRӕՔZÝŜbåդﻩjíլļrӵմzýռپêЅքćچЍďӱҒЕůėژșΘØҚНğńءΠFҢХħΨҪ"
|
|
|
|
|
<< "ЭųįҶرҲеԷňعθҺнԿفπÂхՇψÊэšՏÒUəÚѝŻşҤӑâeէŐımկòuշÕúտŔ";
|
2014-05-23 20:11:13 +02:00
|
|
|
|
|
|
|
|
|
// Defining identifier character sets
|
2014-10-18 13:51:40 +02:00
|
|
|
|
DefineNameChars(QStringLiteral("0123456789_") + symbols.join(""));
|
2014-11-08 14:26:33 +01:00
|
|
|
|
DefineOprtChars(symbols.join("") + QStringLiteral("+-*^/?<>=#!$%&|~_"));
|
2014-05-23 20:11:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 17:00:29 +02:00
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-22 14:11:14 +02:00
|
|
|
|
// Factory function for creating new parser variables
|
|
|
|
|
// This could as well be a function performing database queries.
|
|
|
|
|
qreal* Calculator::AddVariable(const QString &a_szName, void *a_pUserData)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(a_szName)
|
|
|
|
|
Q_UNUSED(a_pUserData)
|
2014-09-08 15:17:38 +02:00
|
|
|
|
|
|
|
|
|
static qreal value = 0;
|
|
|
|
|
return &value;
|
2014-05-22 14:11:14 +02:00
|
|
|
|
}
|
2014-09-20 17:00:29 +02:00
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
|
/**
|
|
|
|
|
* @brief Calculator::SetSepForEval set separators for eval. Each expression eval in internal (C) locale.
|
|
|
|
|
*/
|
2014-09-20 17:00:29 +02:00
|
|
|
|
void Calculator::SetSepForEval()
|
|
|
|
|
{
|
2015-02-10 13:46:11 +01:00
|
|
|
|
SetArgSep(';');
|
|
|
|
|
SetThousandsSep(',');
|
2014-09-20 17:00:29 +02:00
|
|
|
|
SetDecSep('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
|
/**
|
|
|
|
|
* @brief Calculator::SetSepForTr set separators for translation expression.
|
|
|
|
|
* @param fromUser true if expression come from user (from dialog).
|
|
|
|
|
*/
|
2015-06-12 10:55:44 +02:00
|
|
|
|
void Calculator::SetSepForTr(bool osSeparator, bool fromUser)
|
2014-09-20 17:00:29 +02:00
|
|
|
|
{
|
|
|
|
|
if (fromUser)
|
|
|
|
|
{
|
2015-06-12 10:55:44 +02:00
|
|
|
|
if (osSeparator)
|
2014-09-20 17:00:29 +02:00
|
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
|
const QLocale loc = QLocale::system();
|
2014-09-20 17:00:29 +02:00
|
|
|
|
SetDecSep(loc.decimalPoint().toLatin1());
|
|
|
|
|
SetThousandsSep(loc.groupSeparator().toLatin1());
|
|
|
|
|
SetArgSep(';');
|
2015-02-08 12:48:27 +01:00
|
|
|
|
return;
|
2014-09-20 17:00:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-08 12:48:27 +01:00
|
|
|
|
SetSepForEval();//Same separators (internal) as for eval.
|
2014-09-20 17:00:29 +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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|