Refactoring. Delete repetitive code.

--HG--
branch : feature
This commit is contained in:
dismine 2014-06-18 14:38:20 +03:00
parent 0659be3375
commit 9fb5a0206a
3 changed files with 10 additions and 70 deletions

View File

@ -310,7 +310,7 @@ void DialogArc::CheckState()
void DialogArc::EvalRadius() void DialogArc::EvalRadius()
{ {
labelEditFormula = ui->labelEditRadius; labelEditFormula = ui->labelEditRadius;
Eval(ui->plainTextEditFormula, flagRadius, timerRadius, ui->labelResultRadius); Eval(ui->plainTextEditFormula->toPlainText(), flagRadius, timerRadius, ui->labelResultRadius);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -320,7 +320,7 @@ void DialogArc::EvalRadius()
void DialogArc::EvalF1() void DialogArc::EvalF1()
{ {
labelEditFormula = ui->labelEditF1; labelEditFormula = ui->labelEditF1;
Eval(ui->lineEditF1, flagF1, timerF1, ui->labelResultF1); Eval(ui->lineEditF1->text(), flagF1, timerF1, ui->labelResultF1);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -330,7 +330,7 @@ void DialogArc::EvalF1()
void DialogArc::EvalF2() void DialogArc::EvalF2()
{ {
labelEditFormula = ui->labelEditF2; labelEditFormula = ui->labelEditF2;
Eval(ui->lineEditF2, flagF2, timerF2, ui->labelResultF2); Eval(ui->lineEditF2->text(), flagF2, timerF2, ui->labelResultF2);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -437,19 +437,18 @@ void DialogTool::ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer *tim
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief Eval evaluate formula and show result * @brief Eval evaluate formula and show result
* @param edit lineEdit of formula * @param text formula
* @param flag flag state of formula * @param flag flag state of formula
* @param timer timer of formula * @param timer timer of formula
* @param label label for signal error * @param label label for signal error
*/ */
void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) void DialogTool::Eval(const QString &text, bool &flag, QTimer *timer, QLabel *label)
{ {
SCASSERT(edit != nullptr);
SCASSERT(timer != nullptr); SCASSERT(timer != nullptr);
SCASSERT(label != nullptr); SCASSERT(label != nullptr);
SCASSERT(labelEditFormula != nullptr); SCASSERT(labelEditFormula != nullptr);
QPalette palette = labelEditFormula->palette(); QPalette palette = labelEditFormula->palette();
if (edit->text().isEmpty()) if (text.isEmpty())
{ {
flag = false; flag = false;
palette.setColor(labelEditFormula->foregroundRole(), Qt::red); palette.setColor(labelEditFormula->foregroundRole(), Qt::red);
@ -458,66 +457,8 @@ void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label)
{ {
try try
{ {
const QString formula = qApp->FormulaFromUser(edit->text()); // Replace line return with spaces for calc if exist
Calculator *cal = new Calculator(data); QString formula = text;
const qreal result = cal->EvalFormula(formula);
delete cal;
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QApplication::organizationName(),
QApplication::applicationName());
bool osSeparatorValue = settings.value("configuration/osSeparator", 1).toBool();
if (osSeparatorValue)
{
QLocale loc = QLocale::system();
label->setText(loc.toString(result) + VDomDocument::UnitsToStr(qApp->patternUnit(), true));
}
else
{
QLocale loc = QLocale(QLocale::C);
label->setText(loc.toString(result) + VDomDocument::UnitsToStr(qApp->patternUnit(), true));
}
flag = true;
palette.setColor(labelEditFormula->foregroundRole(), QColor(76, 76, 76));
emit ToolTip("");
}
catch (qmu::QmuParserError &e)
{
label->setText(tr("Error"));
flag = false;
palette.setColor(labelEditFormula->foregroundRole(), Qt::red);
emit ToolTip("Parser error: "+e.GetMsg());
qDebug() << "\nMath parser error:\n"
<< "--------------------------------------\n"
<< "Message: " << e.GetMsg() << "\n"
<< "Expression: " << e.GetExpr() << "\n"
<< "--------------------------------------";
}
}
CheckState();
timer->stop();
labelEditFormula->setPalette(palette);
}
//---------------------------------------------------------------------------------------------------------------------
void DialogTool::Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label)
{
SCASSERT(edit != nullptr);
SCASSERT(timer != nullptr);
SCASSERT(label != nullptr);
SCASSERT(labelEditFormula != nullptr);
QPalette palette = labelEditFormula->palette();
if (edit->toPlainText().isEmpty())
{
flag = false;
palette.setColor(labelEditFormula->foregroundRole(), Qt::red);
}
else
{
try
{
// Replace line return with spaces for calc
QString formula = edit->toPlainText();
formula.replace("\n", " "); formula.replace("\n", " ");
formula = qApp->FormulaFromUser(formula); formula = qApp->FormulaFromUser(formula);
Calculator *cal = new Calculator(data); Calculator *cal = new Calculator(data);
@ -863,7 +804,7 @@ void DialogTool::EvalFormula()
{ {
SCASSERT(plainTextEditFormula != nullptr); SCASSERT(plainTextEditFormula != nullptr);
SCASSERT(labelResultCalculation != nullptr); SCASSERT(labelResultCalculation != nullptr);
Eval(plainTextEditFormula, flagFormula, timerFormula, labelResultCalculation); Eval(plainTextEditFormula->toPlainText(), flagFormula, timerFormula, labelResultCalculation);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -194,8 +194,7 @@ protected:
void PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget); void PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget);
void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer * timer); void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer * timer);
void ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer * timer); void ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer * timer);
void Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label); void Eval(const QString &text, bool &flag, QTimer *timer, QLabel *label);
void Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label);
void setCurrentPointId(QComboBox *box, quint32 &pointId, const quint32 &value, const quint32 &id) const; void setCurrentPointId(QComboBox *box, quint32 &pointId, const quint32 &value, const quint32 &id) const;
void setCurrentSplineId(QComboBox *box, quint32 &splineId, const quint32 &value, const quint32 &id, void setCurrentSplineId(QComboBox *box, quint32 &splineId, const quint32 &value, const quint32 &id,
ComboBoxCutSpline cut = ComboBoxCutSpline::NoCutSpline) const; ComboBoxCutSpline cut = ComboBoxCutSpline::NoCutSpline) const;