Improve parsing formulas.

Enable subexpressions. Allow user to define local variables.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2018-01-24 22:08:53 +02:00
parent 9b5b8dde04
commit 54d3ae7561
42 changed files with 90 additions and 220 deletions

View File

@ -615,12 +615,7 @@ bool TMainWindow::eventFilter(QObject *object, QEvent *event)
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
{
// Ignore Enter key
return true;
}
else if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
{
if (qApp->Settings()->GetOsSeparator())
{
@ -1225,8 +1220,7 @@ void TMainWindow::Fx()
DialogEditWrongFormula *dialog = new DialogEditWrongFormula(meash->GetData(), NULL_ID, this);
dialog->setWindowTitle(tr("Edit measurement"));
dialog->SetMeasurementsMode();
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormula->toPlainText().replace("\n", " "),
true));
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormula->toPlainText(), true));
const QString postfix = UnitsToStr(mUnit, true);//Show unit in dialog lable (cm, mm or inch)
dialog->setPostfix(postfix);
@ -1650,9 +1644,7 @@ void TMainWindow::SaveMValue()
const QTableWidgetItem *nameField = ui->tableWidget->item(row, ColumnName);
// Replace line return character with spaces for calc if exist
QString text = ui->plainTextEditFormula->toPlainText();
text.replace("\n", " ");
QTableWidgetItem *formulaField = ui->tableWidget->item(row, ColumnFormula);
if (formulaField->text() == text)
@ -2639,7 +2631,6 @@ bool TMainWindow::EvalFormula(const QString &formula, bool fromUser, VContainer
{
f = formula;
}
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
qreal result = cal->EvalFormula(data->DataVariables(), f);

View File

@ -392,7 +392,6 @@ void DialogFinalMeasurements::SaveFormula()
// Replace line return character with spaces for calc if exist
QString text = ui->plainTextEditFormula->toPlainText();
text.replace("\n", " ");
QTableWidgetItem *formulaField = ui->tableWidget->item(row, 2);
if (formulaField->text() == text)
@ -479,7 +478,7 @@ void DialogFinalMeasurements::Fx()
QScopedPointer<DialogEditWrongFormula> dialog(new DialogEditWrongFormula(&m_data, NULL_ID, this));
dialog->setWindowTitle(tr("Edit measurement"));
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormula->toPlainText().replace("\n", " "),
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormula->toPlainText(),
qApp->Settings()->GetOsSeparator()));
const QString postfix = UnitsToStr(qApp->patternUnit(), true);
dialog->setPostfix(postfix);//Show unit in dialog lable (cm, mm or inch)
@ -600,7 +599,6 @@ bool DialogFinalMeasurements::EvalUserFormula(const QString &formula, bool fromU
{
f = formula;
}
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(m_data.DataVariables(), f);
@ -719,11 +717,9 @@ qreal DialogFinalMeasurements::EvalFormula(const QString &formula, bool &ok)
{
try
{
QString f = formula;
// Replace line return character with spaces for calc if exist
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
result = cal->EvalFormula(m_data.DataVariables(), f);
result = cal->EvalFormula(m_data.DataVariables(), formula);
if (qIsInf(result) || qIsNaN(result))
{

View File

@ -357,7 +357,6 @@ bool DialogIncrements::EvalIncrementFormula(const QString &formula, bool fromUse
{
f = formula;
}
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->DataVariables(), f);
@ -1138,9 +1137,7 @@ void DialogIncrements::SaveIncrFormula()
const QTableWidgetItem *nameField = table->item(row, 0);
// Replace line return character with spaces for calc if exist
QString text = textEdit->toPlainText();
text.replace("\n", " ");
QTableWidgetItem *formulaField = table->item(row, 2);
if (formulaField->text() == text)
@ -1286,7 +1283,7 @@ void DialogIncrements::Fx()
dialog->setWindowTitle(tr("Edit increment"));
incrementMode ? dialog->SetIncrementsMode() : dialog->SetPreviewCalculationsMode();
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(plainTextEditFormula->toPlainText().replace("\n", " "),
dialog->SetFormula(qApp->TrVars()->TryFormulaFromUser(plainTextEditFormula->toPlainText(),
qApp->Settings()->GetOsSeparator()));
const QString postfix = UnitsToStr(qApp->patternUnit(), true);
dialog->setPostfix(postfix);//Show unit in dialog lable (cm, mm or inch)

View File

@ -1654,11 +1654,8 @@ bool MainWindowsNoGUI::ExportFMeasurementsToCSVData(const QString &fileName, boo
{
try
{
QString f = m.formula;
// Replace line return character with spaces for calc if exist
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(completeData.DataVariables(), f);
const qreal result = cal->EvalFormula(completeData.DataVariables(), m.formula);
csv.setText(i, 1, qApp->LocaleToString(result)); // value

View File

@ -3239,11 +3239,8 @@ qreal VPattern::EvalFormula(VContainer *data, const QString &formula, bool *ok)
{
try
{
// Replace line return character with spaces for calc if exist
QString f = formula;
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->DataVariables(), f);
const qreal result = cal->EvalFormula(data->DataVariables(), formula);
(qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true;
return result;

View File

@ -2036,6 +2036,11 @@ bool VAbstractPattern::IsVariable(const QString &token) const
}
}
if (token.startsWith('#'))
{
return true;
}
return false;
}

View File

@ -66,11 +66,11 @@ QmuParserBase::QmuParserBase()
m_decimalPoint(QLocale::c().decimalPoint()),
m_thousandsSeparator(QLocale::c().groupSeparator()),
m_FunDef(),
m_pTokenReader(),
m_pParseFormula(&QmuParserBase::ParseString),
m_vRPN(),
m_vStringBuf(),
m_vStringVarBuf(),
m_pTokenReader(),
m_PostOprtDef(),
m_InfixOprtDef(),
m_OprtDef(),
@ -102,11 +102,11 @@ QmuParserBase::QmuParserBase(const QmuParserBase &a_Parser)
m_decimalPoint(a_Parser.getDecimalPoint()),
m_thousandsSeparator(a_Parser.getThousandsSeparator()),
m_FunDef(),
m_pTokenReader(),
m_pParseFormula(&QmuParserBase::ParseString),
m_vRPN(),
m_vStringBuf(),
m_vStringVarBuf(),
m_pTokenReader(),
m_PostOprtDef(),
m_InfixOprtDef(),
m_OprtDef(),

View File

@ -134,11 +134,17 @@ public:
void setThousandsSeparator(const QChar &c);
protected:
/**
* @brief Typedef for the token reader.
*/
typedef QmuParserTokenReader token_reader_type;
static const QStringList c_DefaultOprt;
QLocale m_locale;///< The locale used by the parser
QChar m_decimalPoint;
QChar m_thousandsSeparator;
funmap_type m_FunDef; ///< Map of function names and pointers.
std::unique_ptr<token_reader_type> m_pTokenReader; ///< Managed pointer to the token reader object.
static bool g_DbgDumpCmdCode;
static bool g_DbgDumpStack;
void AddCallback(const QString &a_strName, const QmuParserCallback &a_Callback, funmap_type &a_Storage,
@ -204,11 +210,6 @@ private:
*/
typedef QVector<QString> stringbuf_type;
/**
* @brief Typedef for the token reader.
*/
typedef QmuParserTokenReader token_reader_type;
/**
* @brief Type used for parser tokens.
*/
@ -229,8 +230,6 @@ private:
mutable stringbuf_type m_vStringBuf; ///< String buffer, used for storing string function arguments
stringbuf_type m_vStringVarBuf;
std::unique_ptr<token_reader_type> m_pTokenReader; ///< Managed pointer to the token reader object.
funmap_type m_PostOprtDef; ///< Postfix operator callbacks
funmap_type m_InfixOprtDef; ///< unary infix operator.
funmap_type m_OprtDef; ///< Binary operator callbacks

View File

@ -931,6 +931,8 @@ bool QmuParserTokenReader::IsUndefVarTok ( token_type &a_Tok )
// If a factory is available implicitely create new variables
if ( m_pFactory )
{
try
{
qreal *fVar = m_pFactory ( strTok, m_pFactoryData );
a_Tok.SetVar ( fVar, strTok );
@ -944,6 +946,12 @@ bool QmuParserTokenReader::IsUndefVarTok ( token_type &a_Tok )
( *m_pVarDef ) [strTok] = fVar;
m_UsedVar[strTok] = fVar; // Add variable to used-var-list
}
catch (const qmu::QmuParserError &e)
{
Q_UNUSED(e)
return false;
}
}
else
{
a_Tok.SetVar ( &m_fZero, strTok );

View File

@ -83,6 +83,7 @@ QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator,
SetExpr(formula);
//Need run for making tokens. Don't catch exception here, because we want know if formula has error.
m_pTokenReader->IgnoreUndefVar(true);
Eval();
}

View File

@ -881,11 +881,8 @@ qreal VMeasurements::EvalFormula(VContainer *data, const QString &formula, bool
{
try
{
// Replace line return character with spaces for calc if exist
QString f = formula;
f.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->DataVariables(), f);
const qreal result = cal->EvalFormula(data->DataVariables(), formula);
(qIsInf(result) || qIsNaN(result)) ? *ok = false : *ok = true;
return result;

View File

@ -53,11 +53,15 @@
*
*/
Calculator::Calculator()
:QmuFormulaBase()
: QmuFormulaBase(),
m_varsValues(),
m_vars(nullptr)
{
InitCharSets();
setAllowSubexpressions(false);//Only one expression per time
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
// set value to 0.
SetVarFactory(VarFactory, this);
SetSepForEval();
}
@ -82,71 +86,33 @@ qreal Calculator::EvalFormula(const QHash<QString, QSharedPointer<VInternalVaria
return result;
}
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
// set value to 0.
SetVarFactory(AddVariable, this);
SetSepForEval();//Reset separators options
m_vars = vars;
SetExpr(formula);
result = Eval();
QMap<int, QString> tokens = this->GetTokens();
// Remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
RemoveAll(tokens, QStringLiteral("-"));
for (int i = 0; i < builInFunctions.size(); ++i)
{
if (tokens.isEmpty())
{
break;
}
RemoveAll(tokens, builInFunctions.at(i));
}
if (tokens.isEmpty())
{
return result; // We have found only numbers in expression.
}
// Add variables to parser because we have deal with expression with variables.
InitVariables(vars, tokens, formula);
m_pTokenReader->IgnoreUndefVar(true);
return Eval();
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Calculator::InitVariables add variables to parser.
*
* For optimization purpose we try don't add variables that we don't need.
*
* @param vars list of variables.
* @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression.
* @param formula expression, need for throwing better error message.
*/
void Calculator::InitVariables(const QHash<QString, QSharedPointer<VInternalVariable> > *vars,
const QMap<int, QString> &tokens, const QString &formula)
qreal *Calculator::VarFactory(const QString &a_szName, void *a_pUserData)
{
QMap<int, QString>::const_iterator i = tokens.constBegin();
while (i != tokens.constEnd())
Q_UNUSED(a_szName)
Calculator *calc = static_cast<Calculator *>(a_pUserData);
if (calc->m_vars != nullptr && calc->m_vars->contains(a_szName))
{
bool found = false;
if (vars->contains(i.value()))
{
DefineVar(i.value(), vars->value(i.value())->GetValue());
found = true;
QSharedPointer<qreal> val(new qreal(*calc->m_vars->value(a_szName)->GetValue()));
calc->m_varsValues.append(val);
return val.data();
}
if (found == false && builInFunctions.contains(i.value()))
{// We have found built-in function
found = true;
if (a_szName.startsWith('#'))
{
QSharedPointer<qreal> val(new qreal(0));
calc->m_varsValues.append(val);
return val.data();
}
if (found == false)
{
throw qmu::QmuParserError (qmu::ecUNASSIGNABLE_TOKEN, i.value(), formula, i.key());
}
++i;
}
throw qmu::QmuParserError (qmu::ecUNASSIGNABLE_TOKEN);
}

View File

@ -64,11 +64,12 @@ public:
virtual ~Calculator() Q_DECL_EQ_DEFAULT;
qreal EvalFormula(const QHash<QString, QSharedPointer<VInternalVariable> > *vars, const QString &formula);
protected:
static qreal* VarFactory(const QString &a_szName, void *a_pUserData);
private:
Q_DISABLE_COPY(Calculator)
void InitVariables(const QHash<QString, QSharedPointer<VInternalVariable> > *vars, const QMap<int, QString> &tokens,
const QString &formula);
QVector<QSharedPointer<qreal>> m_varsValues;
const QHash<QString, QSharedPointer<VInternalVariable> > *m_vars;
};
#endif // CALCULATOR_H

View File

@ -65,9 +65,7 @@ VFormula::VFormula(const QString &formula, const VContainer *container)
postfix(QString()),
_error(true),
dValue(0)
{
this->formula.replace("\n", " ");// Replace line return with spaces for calc if exist
}
{}
//---------------------------------------------------------------------------------------------------------------------
VFormula &VFormula::operator=(const VFormula &formula)
@ -145,7 +143,6 @@ void VFormula::SetFormula(const QString &value, FormulaType type)
{
formula = value;
}
formula.replace("\n", " ");// Replace line return with spaces for calc if exist
}
}

View File

@ -47,8 +47,6 @@ qreal EvalFormula(const VContainer *data, QString formula)
{
try
{
// Replace line return character with spaces for calc if exist
formula.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(data->DataVariables(), formula);

View File

@ -800,11 +800,8 @@ bool VPiecePath::IsVisible(const QHash<QString, QSharedPointer<VInternalVariable
bool visible = true;
try
{
QString formula = GetVisibilityTrigger();
// Replace line return character with spaces for calc if exist
formula.replace("\n", " ");
QScopedPointer<Calculator> cal(new Calculator());
const qreal result = cal->EvalFormula(vars, formula);
const qreal result = cal->EvalFormula(vars, GetVisibilityTrigger());
if (qIsInf(result) || qIsNaN(result))
{

View File

@ -129,7 +129,6 @@ DialogEditWrongFormula::~DialogEditWrongFormula()
void DialogEditWrongFormula::DialogAccepted()
{
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
emit DialogClosed(QDialog::Accepted);
accepted();
}

View File

@ -217,7 +217,6 @@ void DialogAlongLine::SaveData()
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
SCASSERT(line != nullptr)

View File

@ -277,11 +277,8 @@ void DialogArc::ShowVisualization()
void DialogArc::SaveData()
{
radius = ui->plainTextEditFormula->toPlainText();
radius.replace("\n", " ");
f1 = ui->plainTextEditF1->toPlainText();
f1.replace("\n", " ");
f2 = ui->plainTextEditF2->toPlainText();
f2.replace("\n", " ");
VisToolArc *path = qobject_cast<VisToolArc *>(vis);
SCASSERT(path != nullptr)

View File

@ -354,13 +354,8 @@ void DialogArcWithLength::ShowVisualization()
void DialogArcWithLength::SaveData()
{
radius = ui->plainTextEditRadius->toPlainText();
radius.replace("\n", " ");
f1 = ui->plainTextEditF1->toPlainText();
f1.replace("\n", " ");
length = ui->plainTextEditLength->toPlainText();
length.replace("\n", " ");
VisToolArcWithLength *path = qobject_cast<VisToolArcWithLength *>(vis);
SCASSERT(path != nullptr)

View File

@ -319,9 +319,7 @@ void DialogBisector::SetLineColor(const QString &value)
void DialogBisector::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
SCASSERT(line != nullptr)

View File

@ -308,9 +308,7 @@ void DialogCurveIntersectAxis::ShowVisualization()
void DialogCurveIntersectAxis::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formulaAngle = ui->plainTextEditFormula->toPlainText();
formulaAngle.replace("\n", " ");
VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
SCASSERT(line != nullptr)

View File

@ -144,7 +144,6 @@ void DialogCutArc::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolCutArc *path = qobject_cast<VisToolCutArc *>(vis);
SCASSERT(path != nullptr)

View File

@ -160,7 +160,6 @@ void DialogCutSpline::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolCutSpline *path = qobject_cast<VisToolCutSpline *>(vis);
SCASSERT(path != nullptr)

View File

@ -160,7 +160,6 @@ void DialogCutSplinePath::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolCutSplinePath *path = qobject_cast<VisToolCutSplinePath *>(vis);
SCASSERT(path != nullptr)

View File

@ -612,15 +612,10 @@ void DialogEllipticalArc::ShowVisualization()
void DialogEllipticalArc::SaveData()
{
radius1 = ui->plainTextEditRadius1->toPlainText();
radius1.replace("\n", " ");
radius2 = ui->plainTextEditRadius2->toPlainText();
radius2.replace("\n", " ");
f1 = ui->plainTextEditF1->toPlainText();
f1.replace("\n", " ");
f2 = ui->plainTextEditF2->toPlainText();
f2.replace("\n", " ");
rotationAngle = ui->plainTextEditRotationAngle->toPlainText();
rotationAngle.replace("\n", " ");
VisToolEllipticalArc *path = qobject_cast<VisToolEllipticalArc *>(vis);
SCASSERT(path != nullptr)

View File

@ -344,12 +344,8 @@ void DialogEndLine::ShowVisualization()
void DialogEndLine::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formulaLength = ui->plainTextEditFormula->toPlainText();
formulaLength.replace("\n", " ");
formulaAngle = ui->plainTextEditAngle->toPlainText();
formulaAngle.replace("\n", " ");
VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
SCASSERT(line != nullptr)

View File

@ -375,9 +375,7 @@ void DialogLineIntersectAxis::ShowVisualization()
void DialogLineIntersectAxis::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formulaAngle = ui->plainTextEditFormula->toPlainText();
formulaAngle.replace("\n", " ");
VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
SCASSERT(line != nullptr)

View File

@ -513,15 +513,9 @@ void DialogMove::ShowVisualization()
void DialogMove::SaveData()
{
m_suffix = ui->lineEditSuffix->text();
formulaAngle = ui->plainTextEditAngle->toPlainText();
formulaAngle.replace("\n", " ");
formulaRotationAngle = ui->plainTextEditRotationAngle->toPlainText();
formulaRotationAngle.replace("\n", " ");
formulaLength = ui->plainTextEditLength->toPlainText();
formulaLength.replace("\n", " ");
VisToolMove *operation = qobject_cast<VisToolMove *>(vis);
SCASSERT(operation != nullptr)

View File

@ -198,7 +198,6 @@ void DialogNormal::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
angle = ui->doubleSpinBoxAngle->value();
VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);

View File

@ -302,15 +302,12 @@ void DialogPointFromCircleAndTangent::SaveData()
{
pointName = ui->lineEditNamePoint->text();
QString radius = ui->plainTextEditRadius->toPlainText();
radius.replace("\n", " ");
VisToolPointFromCircleAndTangent *point = qobject_cast<VisToolPointFromCircleAndTangent *>(vis);
SCASSERT(point != nullptr)
point->setObject1Id(GetTangentPointId());
point->setObject2Id(GetCircleCenterId());
point->setCRadius(radius);
point->setCRadius(ui->plainTextEditRadius->toPlainText());
point->setCrossPoint(GetCrossCirclesPoint());
point->RefreshGeometry();
}

View File

@ -220,7 +220,6 @@ void DialogPointOfContact::SaveData()
{
pointName = ui->lineEditNamePoint->text();
radius = ui->plainTextEditFormula->toPlainText();
radius.replace("\n", " ");
VisToolPointOfContact *line = qobject_cast<VisToolPointOfContact *>(vis);
SCASSERT(line != nullptr)

View File

@ -389,19 +389,13 @@ void DialogPointOfIntersectionCircles::SaveData()
{
pointName = ui->lineEditNamePoint->text();
QString c1Radius = ui->plainTextEditCircle2Radius->toPlainText();
c1Radius.replace("\n", " ");
QString c2Radius = ui->plainTextEditCircle2Radius->toPlainText();
c2Radius.replace("\n", " ");
VisToolPointOfIntersectionCircles *point = qobject_cast<VisToolPointOfIntersectionCircles *>(vis);
SCASSERT(point != nullptr)
point->setObject1Id(GetFirstCircleCenterId());
point->setObject2Id(GetSecondCircleCenterId());
point->setC1Radius(c1Radius);
point->setC2Radius(c2Radius);
point->setC1Radius(ui->plainTextEditCircle2Radius->toPlainText());
point->setC2Radius(ui->plainTextEditCircle2Radius->toPlainText());
point->setCrossPoint(GetCrossCirclesPoint());
point->RefreshGeometry();
}

View File

@ -372,9 +372,7 @@ void DialogRotation::ShowVisualization()
void DialogRotation::SaveData()
{
m_suffix = ui->lineEditSuffix->text();
formulaAngle = ui->plainTextEditFormula->toPlainText();
formulaAngle.replace("\n", " ");
VisToolRotation *operation = qobject_cast<VisToolRotation *>(vis);
SCASSERT(operation != nullptr)

View File

@ -224,7 +224,6 @@ void DialogShoulderPoint::SaveData()
{
pointName = ui->lineEditNamePoint->text();
formula = ui->plainTextEditFormula->toPlainText();
formula.replace("\n", " ");
VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
SCASSERT(line != nullptr)

View File

@ -444,16 +444,9 @@ void DialogSpline::EvalLength2()
VSpline DialogSpline::CurrentSpline() const
{
QString angle1F = ui->plainTextEditAngle1F->toPlainText();
angle1F.replace("\n", " ");
QString angle2F = ui->plainTextEditAngle2F->toPlainText();
angle2F.replace("\n", " ");
QString length1F = ui->plainTextEditLength1F->toPlainText();
length1F.replace("\n", " ");
QString length2F = ui->plainTextEditLength2F->toPlainText();
length2F.replace("\n", " ");
const QHash<QString, QSharedPointer<VInternalVariable> > *vars = data->DataVariables();

View File

@ -306,7 +306,7 @@ void DialogSplinePath::Angle1Changed()
SCASSERT(item != nullptr)
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
const QString angle1F = ui->plainTextEditAngle1F->toPlainText().replace("\n", " ");
const QString angle1F = ui->plainTextEditAngle1F->toPlainText();
const qreal angle1 = Visualization::FindValFromUser(angle1F, data->DataVariables());
p.SetAngle1(angle1, VTranslateVars::TryFormulaFromUser(angle1F, qApp->Settings()->GetOsSeparator()));
@ -340,7 +340,7 @@ void DialogSplinePath::Angle2Changed()
SCASSERT(item != nullptr)
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
const QString angle2F = ui->plainTextEditAngle2F->toPlainText().replace("\n", " ");
const QString angle2F = ui->plainTextEditAngle2F->toPlainText();
const qreal angle2 = Visualization::FindValFromUser(angle2F, data->DataVariables());
p.SetAngle2(angle2, VTranslateVars::TryFormulaFromUser(angle2F, qApp->Settings()->GetOsSeparator()));
@ -374,7 +374,7 @@ void DialogSplinePath::Length1Changed()
SCASSERT(item != nullptr)
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
const QString length1F = ui->plainTextEditLength1F->toPlainText().replace("\n", " ");
const QString length1F = ui->plainTextEditLength1F->toPlainText();
const qreal length1 = Visualization::FindLengthFromUser(length1F, data->DataVariables());
p.SetLength1(length1, VTranslateVars::TryFormulaFromUser(length1F, qApp->Settings()->GetOsSeparator()));
@ -399,7 +399,7 @@ void DialogSplinePath::Length2Changed()
SCASSERT(item != nullptr)
VSplinePoint p = qvariant_cast<VSplinePoint>(item->data(Qt::UserRole));
const QString length2F = ui->plainTextEditLength2F->toPlainText().replace("\n", " ");
const QString length2F = ui->plainTextEditLength2F->toPlainText();
const qreal length2 = Visualization::FindLengthFromUser(length2F, data->DataVariables());
p.SetLength2(length2, VTranslateVars::TryFormulaFromUser(length2F, qApp->Settings()->GetOsSeparator()));
@ -415,7 +415,7 @@ void DialogSplinePath::FXAngle1()
auto dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit first control point angle"));
QString angle1F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditAngle1F->toPlainText().replace("\n", " "),
QString angle1F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditAngle1F->toPlainText(),
qApp->Settings()->GetOsSeparator());
dialog->SetFormula(angle1F);
@ -440,7 +440,7 @@ void DialogSplinePath::FXAngle2()
auto dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit second control point angle"));
QString angle2F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditAngle2F->toPlainText().replace("\n", " "),
QString angle2F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditAngle2F->toPlainText(),
qApp->Settings()->GetOsSeparator());
dialog->SetFormula(angle2F);
@ -465,7 +465,7 @@ void DialogSplinePath::FXLength1()
auto dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit first control point length"));
QString length1F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditLength1F->toPlainText().replace("\n", " "),
QString length1F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditLength1F->toPlainText(),
qApp->Settings()->GetOsSeparator());
dialog->SetFormula(length1F);
@ -490,7 +490,7 @@ void DialogSplinePath::FXLength2()
auto dialog = new DialogEditWrongFormula(data, toolId, this);
dialog->setWindowTitle(tr("Edit second control point length"));
QString length2F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditLength2F->toPlainText().replace("\n", " "),
QString length2F = VTranslateVars::TryFormulaFromUser(ui->plainTextEditLength2F->toPlainText(),
qApp->Settings()->GetOsSeparator());
dialog->SetFormula(length2F);

View File

@ -77,7 +77,7 @@ template <class T> class QSharedPointer;
Q_LOGGING_CATEGORY(vDialog, "v.dialog")
#define DIALOG_MAX_FORMULA_HEIGHT 64
#define DIALOG_MAX_FORMULA_HEIGHT 80
namespace
{
@ -401,12 +401,7 @@ bool DialogTool::eventFilter(QObject *object, QEvent *event)
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
{
// Ignore Enter key
return true;
}
else if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
{
if (qApp->Settings()->GetOsSeparator())
{
@ -799,11 +794,8 @@ qreal DialogTool::Eval(const QString &text, bool &flag, QLabel *label, const QSt
{
try
{
// Replace line return character with spaces for calc if exist
QString formula = text;
formula.replace("\n", " ");
// Translate to internal look.
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
QString formula = qApp->TrVars()->FormulaFromUser(text, qApp->Settings()->GetOsSeparator());
QScopedPointer<Calculator> cal(new Calculator());
result = cal->EvalFormula(data->DataVariables(), formula);

View File

@ -1201,7 +1201,6 @@ void DialogPiecePath::SetPieceId(quint32 id)
QString DialogPiecePath::GetFormulaSAWidth() const
{
QString width = ui->plainTextEditFormulaWidth->toPlainText();
width.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(width, qApp->Settings()->GetOsSeparator());
}
@ -1305,7 +1304,6 @@ void DialogPiecePath::NewItem(const VPieceNode &node)
QString DialogPiecePath::GetFormulaSAWidthBefore() const
{
QString width = ui->plainTextEditFormulaWidthBefore->toPlainText();
width.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(width, qApp->Settings()->GetOsSeparator());
}
@ -1313,7 +1311,6 @@ QString DialogPiecePath::GetFormulaSAWidthBefore() const
QString DialogPiecePath::GetFormulaSAWidthAfter() const
{
QString width = ui->plainTextEditFormulaWidthAfter->toPlainText();
width.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(width, qApp->Settings()->GetOsSeparator());
}
@ -1321,7 +1318,6 @@ QString DialogPiecePath::GetFormulaSAWidthAfter() const
QString DialogPiecePath::GetFormulaVisible() const
{
QString formula = ui->plainTextEditFormulaVisible->toPlainText();
formula.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
}

View File

@ -151,7 +151,7 @@ void DialogPlaceLabel::SetLabelType(PlaceLabelType type)
//---------------------------------------------------------------------------------------------------------------------
QString DialogPlaceLabel::GetWidth() const
{
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaWidth->toPlainText().replace("\n", " "),
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaWidth->toPlainText(),
qApp->Settings()->GetOsSeparator());
}
@ -177,7 +177,7 @@ void DialogPlaceLabel::SetWidth(const QString &value)
//---------------------------------------------------------------------------------------------------------------------
QString DialogPlaceLabel::GetHeight() const
{
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaHeight->toPlainText().replace("\n", " "),
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaHeight->toPlainText(),
qApp->Settings()->GetOsSeparator());
}
@ -203,7 +203,7 @@ void DialogPlaceLabel::SetHeight(const QString &value)
//---------------------------------------------------------------------------------------------------------------------
QString DialogPlaceLabel::GetAngle() const
{
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaAngle->toPlainText().replace("\n", " "),
return qApp->TrVars()->TryFormulaFromUser(ui->plainTextEditFormulaAngle->toPlainText(),
qApp->Settings()->GetOsSeparator());
}

View File

@ -72,10 +72,7 @@ void EnableDefButton(QPushButton *defButton, const QString &formula)
QString GetFormulaFromUser(QPlainTextEdit *textEdit)
{
SCASSERT(textEdit != nullptr)
QString formula = textEdit->toPlainText();
formula.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
return qApp->TrVars()->TryFormulaFromUser(textEdit->toPlainText(), qApp->Settings()->GetOsSeparator());
}
}
@ -1515,7 +1512,6 @@ void DialogSeamAllowance::UpdateGrainlineValues()
QString qsVal;
try
{
qsFormula.replace("\n", " ");
qsFormula = qApp->TrVars()->FormulaFromUser(qsFormula, qApp->Settings()->GetOsSeparator());
Calculator cal;
qreal dVal = cal.EvalFormula(data->DataVariables(), qsFormula);
@ -1597,7 +1593,7 @@ void DialogSeamAllowance::UpdateDetailLabelValues()
QString qsVal;
try
{
qsFormula.replace("\n", " ");
qsFormula = qApp->TrVars()->FormulaFromUser(qsFormula, qApp->Settings()->GetOsSeparator());
Calculator cal;
qreal dVal = cal.EvalFormula(data->DataVariables(), qsFormula);
@ -1683,7 +1679,6 @@ void DialogSeamAllowance::UpdatePatternLabelValues()
QString qsVal;
try
{
qsFormula.replace("\n", " ");
qsFormula = qApp->TrVars()->FormulaFromUser(qsFormula, qApp->Settings()->GetOsSeparator());
Calculator cal;
qreal dVal = cal.EvalFormula(data->DataVariables(), qsFormula);
@ -1814,7 +1809,6 @@ void DialogSeamAllowance::EditGrainlineFormula()
if (dlg.exec() == QDialog::Accepted)
{
QString qsFormula = dlg.GetFormula();
qsFormula.replace("\n", " ");
if (sender() == uiTabGrainline->pushButtonLen)
{
@ -1871,7 +1865,6 @@ void DialogSeamAllowance::EditDLFormula()
if (dlg.exec() == QDialog::Accepted)
{
QString qsFormula = dlg.GetFormula();
qsFormula.replace("\n", " ");
if (sender() == uiTabLabels->pushButtonDLHeight)
{
SetDLHeight(qsFormula);
@ -1931,7 +1924,6 @@ void DialogSeamAllowance::EditPLFormula()
if (dlg.exec() == QDialog::Accepted)
{
QString qsFormula = dlg.GetFormula();
qsFormula.replace("\n", " ");
if (sender() == uiTabLabels->pushButtonPLHeight)
{
SetPLHeight(qsFormula);
@ -2965,7 +2957,6 @@ void DialogSeamAllowance::InitAllPinComboboxes()
QString DialogSeamAllowance::GetFormulaSAWidth() const
{
QString width = uiTabPaths->plainTextEditFormulaWidth->toPlainText();
width.replace("\n", " ");
return qApp->TrVars()->TryFormulaFromUser(width, qApp->Settings()->GetOsSeparator());
}

View File

@ -164,7 +164,6 @@ qreal Visualization::FindValFromUser(const QString &expression,
{
// Replace line return with spaces for calc if exist
QString formula = expression;
formula.replace("\n", " ");
if (fromUser)
{
formula = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());