1 /************************************************************************
2  **
3  **  @file   calculator.h
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   November 15, 2013
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2013-2015 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #ifndef CALCULATOR_H
30 #define CALCULATOR_H
31 
32 #include <qcompilerdetection.h>
33 #include <QHash>
34 #include <QMap>
35 #include <QString>
36 #include <QtGlobal>
37 
38 #include "../qmuparser/qmuformulabase.h"
39 
40 class VInternalVariable;
41 
42 /**
43  * @brief The Calculator class for calculation formula.
44  *
45  * Main purpose make easy evaluate value of formula and get tokens.
46  * Note. If created to many parser for different purpes in the same time parser can work wrong.
47  * Example:
48  * DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data);
49  * dialog->SetFormula(formula);
50  * if (dialog->exec() == QDialog::Accepted)
51  * {
52  *     formula = dialog->GetFormula();
53  *     //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here.
54  *     //Don't know why.
55  *     delete dialog;
56  *     QScopedPointer<Calculator> cal(new Calculator());
57  *     result = cal->EvalFormula(data->PlainVariables(), formula);
58  * }
59  */
60 class Calculator:public qmu::QmuFormulaBase
61 {
62 public:
63     Calculator();
64     virtual ~Calculator() Q_DECL_EQ_DEFAULT;
65 
66     qreal EvalFormula(const QHash<QString, QSharedPointer<VInternalVariable> > *vars, const QString &formula);
67 protected:
68     static qreal* VarFactory(const QString &a_szName, void *a_pUserData);
69     static qreal Warning(const QString &warningMsg, qreal value);
70 private:
71     Q_DISABLE_COPY(Calculator)
72     QVector<QSharedPointer<qreal>> m_varsValues;
73     const QHash<QString, QSharedPointer<VInternalVariable> > *m_vars;
74 };
75 
76 #endif // CALCULATOR_H
77