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.
|
|
|
|
|
** Copyright (C) 2013 Valentina project
|
|
|
|
|
** <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-09-25 17:44:06 +02:00
|
|
|
|
#include "../core/vapplication.h"
|
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
|
|
|
|
/**
|
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 for evaluation formula. All formulas must be converted to internal look.
|
|
|
|
|
* Example:
|
|
|
|
|
*
|
|
|
|
|
* const QString formula = qApp->FormulaFromUser(edit->text());
|
|
|
|
|
* Calculator *cal = new Calculator(data);
|
|
|
|
|
* const qreal result = cal->EvalFormula(formula);
|
|
|
|
|
* delete cal;
|
|
|
|
|
*
|
2014-05-07 18:55:57 +02:00
|
|
|
|
* @param data pointer to a variable container.
|
|
|
|
|
*/
|
|
|
|
|
Calculator::Calculator(const VContainer *data)
|
2014-07-29 17:15:18 +02:00
|
|
|
|
:QmuParser(), vVarVal(nullptr), data(data)
|
2013-11-04 21:35:15 +01:00
|
|
|
|
{
|
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:
|
|
|
|
|
*
|
|
|
|
|
* Calculator *cal = new Calculator(formula, false);
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2014-05-23 20:11:13 +02:00
|
|
|
|
Calculator::Calculator(const QString &formula, bool fromUser)
|
2014-07-29 17:15:18 +02:00
|
|
|
|
:QmuParser(), vVarVal(nullptr), 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-05-22 14:11:14 +02:00
|
|
|
|
SetVarFactory(AddVariable, this);
|
2014-05-23 20:11:13 +02:00
|
|
|
|
|
2014-09-20 17:00:29 +02:00
|
|
|
|
SetSepForTr(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-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.
|
|
|
|
|
* @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
|
|
|
|
{
|
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();
|
|
|
|
|
if (tokens.isEmpty())
|
2013-11-04 21:35:15 +01:00
|
|
|
|
{
|
2014-07-29 17:15:18 +02:00
|
|
|
|
return result;
|
2014-05-21 15:51:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
|
// Add 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
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
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
|
|
|
|
{
|
2014-06-12 09:22:29 +02:00
|
|
|
|
if (qApp->patternType() == MeasurementsType::Standard)
|
2014-05-21 15:51:47 +02:00
|
|
|
|
{
|
2014-07-29 17:15:18 +02:00
|
|
|
|
vVarVal = new qreal[2];
|
2013-07-17 13:38:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
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());
|
2014-07-29 17:15:18 +02:00
|
|
|
|
if ((qApp->patternType() == MeasurementsType::Standard) &&
|
|
|
|
|
(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());
|
2014-07-29 13:28:18 +02:00
|
|
|
|
m->SetValue(data->size(), data->height());
|
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
|
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
|
if (qApp->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
|
|
|
|
|
2014-10-02 11:46:24 +02:00
|
|
|
|
if (builInFunctions.contains(i.value()))
|
|
|
|
|
{// We found built-in function
|
|
|
|
|
found = true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
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(""));
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void Calculator::SetSepForEval()
|
|
|
|
|
{
|
|
|
|
|
SetArgSep(',');
|
|
|
|
|
SetDecSep('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
void Calculator::SetSepForTr(bool fromUser)
|
|
|
|
|
{
|
|
|
|
|
if (fromUser)
|
|
|
|
|
{
|
|
|
|
|
bool osSeparatorValue = qApp->getSettings()->value("configuration/osSeparator", 1).toBool();
|
|
|
|
|
|
|
|
|
|
if (osSeparatorValue)
|
|
|
|
|
{
|
|
|
|
|
QLocale loc = QLocale::system();
|
|
|
|
|
SetDecSep(loc.decimalPoint().toLatin1());
|
|
|
|
|
SetThousandsSep(loc.groupSeparator().toLatin1());
|
|
|
|
|
SetArgSep(';');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetArgSep(',');
|
|
|
|
|
SetDecSep('.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SetArgSep(',');
|
|
|
|
|
SetDecSep('.');
|
|
|
|
|
}
|
|
|
|
|
}
|