diff --git a/src/app/tools/drawTools/vdrawtool.cpp b/src/app/tools/drawTools/vdrawtool.cpp index 3626c5982..457de4522 100644 --- a/src/app/tools/drawTools/vdrawtool.cpp +++ b/src/app/tools/drawTools/vdrawtool.cpp @@ -28,6 +28,12 @@ #include "vdrawtool.h" +#include + +#include + +#include + qreal VDrawTool::factor = 1; //--------------------------------------------------------------------------------------------------------------------- @@ -135,6 +141,51 @@ void VDrawTool::SetFactor(qreal factor) } } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief CheckFormula check formula. + * + * Try calculate formula. If find error show dialog that allow user try fix formula. If user can't throw exception. In + * successes case return result calculation and fixed formula string. If formula ok don't touch formula. + * @param formula [in|out] string with formula. + * @param data [in] container with variables. Need for math parser. + * @throw QmuParserError. + * @return result of calculation formula. + */ +qreal VDrawTool::CheckFormula(QString &formula, VContainer *data) +{ + qreal result = 0; + Calculator *cal = nullptr; + try + { + cal = new Calculator(data); + result = cal->EvalFormula(formula); + delete cal; + } + catch(qmu::QmuParserError &e) + { + delete cal; + DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data); + dialog->setFormula(formula); + if (dialog->exec() == QDialog::Accepted) + { + formula = dialog->getFormula(); + //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here. + //Don't know why. + delete dialog; + Calculator *cal = new Calculator(data); + result = cal->EvalFormula(formula); + delete cal;//Here can be memory leak, but dialog already check this formula and probability very low. + } + else + { + delete dialog; + throw; + } + } + return result; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief AddToCalculation add tool to calculation tag in pattern file. diff --git a/src/app/tools/drawTools/vdrawtool.h b/src/app/tools/drawTools/vdrawtool.h index 437798af4..e665ba67f 100644 --- a/src/app/tools/drawTools/vdrawtool.h +++ b/src/app/tools/drawTools/vdrawtool.h @@ -50,6 +50,7 @@ public: virtual void setDialog() {} void ignoreContextMenu(bool enable); + static qreal CheckFormula(QString &formula, VContainer *data); public slots: virtual void ShowTool(quint32 id, Qt::GlobalColor color, bool enable); virtual void ChangedActivDraw(const QString &newName); diff --git a/src/app/tools/drawTools/vtoolendline.cpp b/src/app/tools/drawTools/vtoolendline.cpp index f210ed6a3..dbfece704 100644 --- a/src/app/tools/drawTools/vtoolendline.cpp +++ b/src/app/tools/drawTools/vtoolendline.cpp @@ -90,32 +90,7 @@ void VToolEndLine::Create(const quint32 _id, const QString &pointName, const QSt const VPointF *basePoint = data->GeometricObject(basePointId); QLineF line = QLineF(basePoint->toQPointF(), QPointF(basePoint->x()+100, basePoint->y())); - qreal result = 0; - - try - { - Calculator *cal = new Calculator(data); - result = cal->EvalFormula(formula); - delete cal; - } - catch(qmu::QmuParserError &e) - { - DialogEditWrongFormula *dialog = new DialogEditWrongFormula(data); - dialog->setFormula(formula); - if (dialog->exec() == QDialog::Accepted) - { - formula = dialog->getFormula(); - delete dialog; - Calculator *cal = new Calculator(data); - result = cal->EvalFormula(formula); - delete cal; - } - else - { - delete dialog; - throw; - } - } + qreal result = CheckFormula(formula, data); line.setLength(qApp->toPixel(result)); line.setAngle(angle);