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
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-11-15 13:41:26 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/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"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QStaticStringData>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
|
|
|
#include <QStringList>
|
2017-09-12 06:42:27 +02:00
|
|
|
#include <QSharedPointer>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../qmuparser/qmuparsererror.h"
|
2017-08-04 19:29:43 +02:00
|
|
|
#include "variables/vinternalvariable.h"
|
2013-07-17 13:38:11 +02:00
|
|
|
|
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);
|
2015-10-15 12:07:43 +02:00
|
|
|
* const qreal result = cal->EvalFormula(data->PlainVariables(), formula);
|
2014-05-30 15:04:38 +02:00
|
|
|
* delete cal;
|
|
|
|
*
|
2014-05-07 18:55:57 +02:00
|
|
|
*/
|
2015-10-15 12:07:43 +02:00
|
|
|
Calculator::Calculator()
|
2018-01-24 21:08:53 +01:00
|
|
|
: QmuFormulaBase(),
|
|
|
|
m_varsValues(),
|
|
|
|
m_vars(nullptr)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2017-01-03 10:14:32 +01:00
|
|
|
InitCharSets();
|
2014-09-08 15:17:38 +02:00
|
|
|
|
2018-01-24 21:08:53 +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.
|
|
|
|
SetVarFactory(VarFactory, this);
|
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-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.
|
|
|
|
*/
|
2017-08-04 19:29:43 +02:00
|
|
|
qreal Calculator::EvalFormula(const QHash<QString, QSharedPointer<VInternalVariable>> *vars, const QString &formula)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-01-21 13:31:13 +01:00
|
|
|
// Converting with locale is much faster in case of single numerical value.
|
|
|
|
QLocale c(QLocale::C);
|
|
|
|
bool ok = false;
|
|
|
|
qreal result = c.toDouble(formula, &ok);
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-20 17:00:29 +02:00
|
|
|
SetSepForEval();//Reset separators options
|
2018-01-24 21:08:53 +01:00
|
|
|
m_vars = vars;
|
2014-05-21 10:51:16 +02:00
|
|
|
SetExpr(formula);
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2018-01-24 21:08:53 +01:00
|
|
|
m_pTokenReader->IgnoreUndefVar(true);
|
2014-09-06 10:14:59 +02:00
|
|
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2018-01-24 21:08:53 +01:00
|
|
|
qreal *Calculator::VarFactory(const QString &a_szName, void *a_pUserData)
|
2014-07-29 17:15:18 +02:00
|
|
|
{
|
2018-01-24 21:08:53 +01:00
|
|
|
Q_UNUSED(a_szName)
|
|
|
|
Calculator *calc = static_cast<Calculator *>(a_pUserData);
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2018-01-24 21:08:53 +01:00
|
|
|
if (calc->m_vars != nullptr && calc->m_vars->contains(a_szName))
|
|
|
|
{
|
|
|
|
QSharedPointer<qreal> val(new qreal(*calc->m_vars->value(a_szName)->GetValue()));
|
|
|
|
calc->m_varsValues.append(val);
|
|
|
|
return val.data();
|
|
|
|
}
|
2014-09-06 10:14:59 +02:00
|
|
|
|
2018-01-24 21:08:53 +01:00
|
|
|
if (a_szName.startsWith('#'))
|
|
|
|
{
|
2020-11-09 12:39:32 +01:00
|
|
|
QSharedPointer<qreal> val(new qreal(std::numeric_limits<qreal>::quiet_NaN()));
|
2018-01-24 21:08:53 +01:00
|
|
|
calc->m_varsValues.append(val);
|
|
|
|
return val.data();
|
2013-07-17 13:38:11 +02:00
|
|
|
}
|
2018-01-24 21:08:53 +01:00
|
|
|
|
|
|
|
throw qmu::QmuParserError (qmu::ecUNASSIGNABLE_TOKEN);
|
2013-07-17 13:38:11 +02:00
|
|
|
}
|