Seamly2D
Code documentation
vformula.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vformula.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 28 8, 2014
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vformula.h"
53 
54 #include <qnumeric.h>
55 #include <QMessageLogger>
56 #include <QScopedPointer>
57 #include <QtDebug>
58 
59 #include "../qmuparser/qmuparsererror.h"
60 #include "../vgeometry/../ifc/ifcdef.h"
61 #include "../vmisc/def.h"
62 #include "../vmisc/vabstractapplication.h"
63 #include "../vmisc/vcommonsettings.h"
64 #include "calculator.h"
65 #include "vcontainer.h"
66 #include "vtranslatevars.h"
67 
68 //VFormula
69 //---------------------------------------------------------------------------------------------------------------------
71  :formula(QString()), value(tr("Error")), checkZero(true), data(nullptr), toolId(NULL_ID),
72  postfix(QString()), _error(true), dValue(0)
73 {}
74 
75 //---------------------------------------------------------------------------------------------------------------------
76 VFormula::VFormula(const QString &formula, const VContainer *container)
77  : formula(qApp->TrVars()->FormulaToUser(formula, qApp->Settings()->GetOsSeparator())),
78  value(tr("Error")),
79  checkZero(true),
80  data(container),
81  toolId(NULL_ID),
82  postfix(QString()),
83  _error(true),
84  dValue(0)
85 {
86  this->formula.replace("\n", " ");// Replace line return with spaces for calc if exist
87  Eval();
88 }
89 
90 //---------------------------------------------------------------------------------------------------------------------
92 {
93  if ( &formula == this )
94  {
95  return *this;
96  }
97  this->formula = formula.GetFormula();
98  this->value = formula.getStringValue();
99  this->checkZero = formula.getCheckZero();
100  this->data = formula.getData();
101  this->toolId = formula.getToolId();
102  this->postfix = formula.getPostfix();
103  this->_error = formula.error();
104  this->dValue = formula.getDoubleValue();
105  return *this;
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
110  :formula(formula.GetFormula()), value(formula.getStringValue()), checkZero(formula.getCheckZero()),
111  data(formula.getData()), toolId(formula.getToolId()), postfix(formula.getPostfix()), _error(formula.error()),
112  dValue(formula.getDoubleValue())
113 {}
114 
115 //---------------------------------------------------------------------------------------------------------------------
116 bool VFormula::operator==(const VFormula &formula) const
117 {
118  bool isEqual = false;
119  if (this->formula == formula.GetFormula() && this->value == formula.getStringValue() &&
120  this->checkZero == formula.getCheckZero() && this->data == formula.getData() &&
121  this->toolId == formula.getToolId() && this->postfix == formula.getPostfix() &&
122  this->_error == formula.error() && VFuzzyComparePossibleNulls(this->dValue, formula.getDoubleValue()))
123  {
124  isEqual = true;
125  }
126  return isEqual;
127 }
128 
129 bool VFormula::operator!=(const VFormula &formula) const
130 {
131  return !VFormula::operator==(formula);
132 }
133 
134 //---------------------------------------------------------------------------------------------------------------------
136 {
137  if (type == FormulaType::ToUser)
138  {
139  return formula;
140  }
141  else
142  {
143  return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
144  }
145 }
146 
147 //---------------------------------------------------------------------------------------------------------------------
148 void VFormula::SetFormula(const QString &value, FormulaType type)
149 {
150  if (formula != value)
151  {
152  if (type == FormulaType::ToUser)
153  {
154  formula = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
155  }
156  else
157  {
158  formula = value;
159  }
160  formula.replace("\n", " ");// Replace line return with spaces for calc if exist
161  Eval();
162  }
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
167 {
168  return value;
169 }
170 
171 //---------------------------------------------------------------------------------------------------------------------
173 {
174  return dValue;
175 }
176 
177 //---------------------------------------------------------------------------------------------------------------------
179 {
180  return checkZero;
181 }
182 
183 //---------------------------------------------------------------------------------------------------------------------
184 void VFormula::setCheckZero(bool value)
185 {
186  if (checkZero != value)
187  {
188  checkZero = value;
189  Eval();
190  }
191 }
192 
193 //---------------------------------------------------------------------------------------------------------------------
195 {
196  return data;
197 }
198 
199 //---------------------------------------------------------------------------------------------------------------------
200 void VFormula::setData(const VContainer *value)
201 {
202  if (data != value && value != nullptr)
203  {
204  data = value;
205  Eval();
206  }
207 }
208 
209 //---------------------------------------------------------------------------------------------------------------------
210 quint32 VFormula::getToolId() const
211 {
212  return toolId;
213 }
214 
215 //---------------------------------------------------------------------------------------------------------------------
216 void VFormula::setToolId(const quint32 &value)
217 {
218  toolId = value;
219 }
220 
221 //---------------------------------------------------------------------------------------------------------------------
222 QString VFormula::getPostfix() const
223 {
224  return postfix;
225 }
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 void VFormula::setPostfix(const QString &value)
229 {
230  if (postfix != value)
231  {
232  postfix = value;
233  Eval();
234  }
235 }
236 
237 //---------------------------------------------------------------------------------------------------------------------
238 bool VFormula::error() const
239 {
240  return _error;
241 }
242 
243 //---------------------------------------------------------------------------------------------------------------------
245 {
246  return qMetaTypeId<VFormula>();
247 }
248 
249 //---------------------------------------------------------------------------------------------------------------------
251 {
252  if (data == nullptr)
253  {
254  return;
255  }
256  if (formula.isEmpty())
257  {
258  value = tr("Error");
259  _error = true;
260  dValue = 0;
261  }
262  else
263  {
264  try
265  {
266  QScopedPointer<Calculator> cal(new Calculator());
267  QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
268  const qreal result = cal->EvalFormula(data->DataVariables(), expression);
269 
270  if (qIsInf(result) || qIsNaN(result))
271  {
272  value = tr("Error");
273  _error = true;
274  dValue = 0;
275  }
276  else
277  {
278  //if result equal 0
279  if (checkZero && qFuzzyIsNull(result))
280  {
281  value = QString("0");
282  _error = true;
283  dValue = 0;
284  }
285  else
286  {
287  dValue = result;
288  value = QString(qApp->LocaleToString(result) + " " + postfix);
289  _error = false;
290  }
291  }
292  }
293  catch (qmu::QmuParserError &e)
294  {
295  value = tr("Error");
296  _error = true;
297  dValue = 0;
298  qDebug() << "\nMath parser error:\n"
299  << "--------------------------------------\n"
300  << "Message: " << e.GetMsg() << "\n"
301  << "Expression: " << e.GetExpr() << "\n"
302  << "--------------------------------------";
303  }
304  }
305 }
The Calculator class for calculation formula.
Definition: calculator.h:84
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QHash< QString, QSharedPointer< VInternalVariable > > * DataVariables() const
Definition: vcontainer.cpp:718
void Eval()
Definition: vformula.cpp:250
bool _error
Definition: vformula.h:105
qreal dValue
Definition: vformula.h:106
QString postfix
Definition: vformula.h:104
QString formula
Definition: vformula.h:99
void setData(const VContainer *value)
Definition: vformula.cpp:200
bool checkZero
Definition: vformula.h:101
QString getPostfix() const
Definition: vformula.cpp:222
const VContainer * getData() const
Definition: vformula.cpp:194
bool getCheckZero() const
Definition: vformula.cpp:178
bool error() const
Definition: vformula.cpp:238
qreal getDoubleValue() const
Definition: vformula.cpp:172
QString value
Definition: vformula.h:100
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
static int FormulaTypeId()
Definition: vformula.cpp:244
bool operator!=(const VFormula &formula) const
Definition: vformula.cpp:129
VFormula()
Definition: vformula.cpp:70
QString getStringValue() const
Definition: vformula.cpp:166
const VContainer * data
Definition: vformula.h:102
quint32 toolId
Definition: vformula.h:103
bool operator==(const VFormula &formula) const
Definition: vformula.cpp:116
quint32 getToolId() const
Definition: vformula.cpp:210
void setToolId(const quint32 &value)
Definition: vformula.cpp:216
void setCheckZero(bool value)
Definition: vformula.cpp:184
VFormula & operator=(const VFormula &formula)
Definition: vformula.cpp:91
void SetFormula(const QString &value, FormulaType type=FormulaType::ToUser)
Definition: vformula.cpp:148
void setPostfix(const QString &value)
Definition: vformula.cpp:228
Error class of the parser.
const QString & GetMsg() const
Returns the message string for this error.
const QString & GetExpr() const
gets the expression related tp this error.
static Q_REQUIRED_RESULT bool VFuzzyComparePossibleNulls(double p1, double p2)
Definition: def.h:490
#define NULL_ID
Definition: ifcdef.h:76
#define qApp
Definition: vapplication.h:67
FormulaType
Definition: vformula.h:60