New method CheckFormula.

--HG--
branch : feature
This commit is contained in:
dismine 2014-05-30 12:33:30 +03:00
parent 1b877ca978
commit 18307e0a14
3 changed files with 53 additions and 26 deletions

View File

@ -28,6 +28,12 @@
#include "vdrawtool.h" #include "vdrawtool.h"
#include <qmuparsererror.h>
#include <dialogs/tools/dialogeditwrongformula.h>
#include <container/calculator.h>
qreal VDrawTool::factor = 1; 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. * @brief AddToCalculation add tool to calculation tag in pattern file.

View File

@ -50,6 +50,7 @@ public:
virtual void setDialog() {} virtual void setDialog() {}
void ignoreContextMenu(bool enable); void ignoreContextMenu(bool enable);
static qreal CheckFormula(QString &formula, VContainer *data);
public slots: public slots:
virtual void ShowTool(quint32 id, Qt::GlobalColor color, bool enable); virtual void ShowTool(quint32 id, Qt::GlobalColor color, bool enable);
virtual void ChangedActivDraw(const QString &newName); virtual void ChangedActivDraw(const QString &newName);

View File

@ -90,32 +90,7 @@ void VToolEndLine::Create(const quint32 _id, const QString &pointName, const QSt
const VPointF *basePoint = data->GeometricObject<const VPointF *>(basePointId); const VPointF *basePoint = data->GeometricObject<const VPointF *>(basePointId);
QLineF line = QLineF(basePoint->toQPointF(), QPointF(basePoint->x()+100, basePoint->y())); QLineF line = QLineF(basePoint->toQPointF(), QPointF(basePoint->x()+100, basePoint->y()));
qreal result = 0; qreal result = CheckFormula(formula, data);
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;
}
}
line.setLength(qApp->toPixel(result)); line.setLength(qApp->toPixel(result));
line.setAngle(angle); line.setAngle(angle);