From 9fb5a0206a7bf769cb0c7c3116691d235d7d1b5a Mon Sep 17 00:00:00 2001 From: dismine Date: Wed, 18 Jun 2014 14:38:20 +0300 Subject: [PATCH] Refactoring. Delete repetitive code. --HG-- branch : feature --- src/app/dialogs/tools/dialogarc.cpp | 6 +-- src/app/dialogs/tools/dialogtool.cpp | 71 +++------------------------- src/app/dialogs/tools/dialogtool.h | 3 +- 3 files changed, 10 insertions(+), 70 deletions(-) diff --git a/src/app/dialogs/tools/dialogarc.cpp b/src/app/dialogs/tools/dialogarc.cpp index 92cae1561..c32e5e514 100644 --- a/src/app/dialogs/tools/dialogarc.cpp +++ b/src/app/dialogs/tools/dialogarc.cpp @@ -310,7 +310,7 @@ void DialogArc::CheckState() void DialogArc::EvalRadius() { 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() { 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() { labelEditFormula = ui->labelEditF2; - Eval(ui->lineEditF2, flagF2, timerF2, ui->labelResultF2); + Eval(ui->lineEditF2->text(), flagF2, timerF2, ui->labelResultF2); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/dialogs/tools/dialogtool.cpp b/src/app/dialogs/tools/dialogtool.cpp index 570d2ef8e..d7a35b19c 100644 --- a/src/app/dialogs/tools/dialogtool.cpp +++ b/src/app/dialogs/tools/dialogtool.cpp @@ -437,19 +437,18 @@ void DialogTool::ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer *tim //--------------------------------------------------------------------------------------------------------------------- /** * @brief Eval evaluate formula and show result - * @param edit lineEdit of formula + * @param text formula * @param flag flag state of formula * @param timer timer of formula * @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(label != nullptr); SCASSERT(labelEditFormula != nullptr); QPalette palette = labelEditFormula->palette(); - if (edit->text().isEmpty()) + if (text.isEmpty()) { flag = false; palette.setColor(labelEditFormula->foregroundRole(), Qt::red); @@ -458,66 +457,8 @@ void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) { try { - const QString formula = qApp->FormulaFromUser(edit->text()); - Calculator *cal = new Calculator(data); - 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(); + // Replace line return with spaces for calc if exist + QString formula = text; formula.replace("\n", " "); formula = qApp->FormulaFromUser(formula); Calculator *cal = new Calculator(data); @@ -863,7 +804,7 @@ void DialogTool::EvalFormula() { SCASSERT(plainTextEditFormula != nullptr); SCASSERT(labelResultCalculation != nullptr); - Eval(plainTextEditFormula, flagFormula, timerFormula, labelResultCalculation); + Eval(plainTextEditFormula->toPlainText(), flagFormula, timerFormula, labelResultCalculation); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/dialogs/tools/dialogtool.h b/src/app/dialogs/tools/dialogtool.h index 0ddbabe69..5497c2f58 100644 --- a/src/app/dialogs/tools/dialogtool.h +++ b/src/app/dialogs/tools/dialogtool.h @@ -194,8 +194,7 @@ protected: void PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget); void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer * timer); void ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer * timer); - void Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label); - void Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label); + void Eval(const QString &text, bool &flag, QTimer *timer, QLabel *label); 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, ComboBoxCutSpline cut = ComboBoxCutSpline::NoCutSpline) const;