Show language independent formula in dialog.

--HG--
branch : feature
This commit is contained in:
dismine 2014-05-26 16:18:48 +03:00
parent 6170aea8b6
commit 51a420bbdd
5 changed files with 116 additions and 24 deletions

View File

@ -78,6 +78,14 @@ Calculator::Calculator(const QString &formula, bool fromUser)
} }
SetExpr(formula); SetExpr(formula);
try
{
Eval();//Need run for making tokens
}
catch(qmu::QmuParserError &e)
{
return;//Ignore all warnings
}
} }
Calculator::~Calculator() Calculator::~Calculator()

View File

@ -471,6 +471,12 @@
</item> </item>
<item> <item>
<widget class="QRadioButton" name="radioButtonSizeGrowth"> <widget class="QRadioButton" name="radioButtonSizeGrowth">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Size and height</string> <string>Size and height</string>
</property> </property>
@ -481,6 +487,12 @@
</item> </item>
<item> <item>
<widget class="QRadioButton" name="radioButtonStandardTable"> <widget class="QRadioButton" name="radioButtonStandardTable">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Standard table</string> <string>Standard table</string>
</property> </property>
@ -488,6 +500,12 @@
</item> </item>
<item> <item>
<widget class="QRadioButton" name="radioButtonIncrements"> <widget class="QRadioButton" name="radioButtonIncrements">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Increments</string> <string>Increments</string>
</property> </property>
@ -498,6 +516,12 @@
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Length of lines</string> <string>Length of lines</string>
</property> </property>
@ -508,6 +532,12 @@
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Length of arcs</string> <string>Length of arcs</string>
</property> </property>
@ -518,6 +548,12 @@
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text"> <property name="text">
<string>Length of curves</string> <string>Length of curves</string>
</property> </property>

View File

@ -671,8 +671,9 @@ void DialogTool::ValChenged(int row)
} }
if (radioButtonStandardTable->isChecked()) if (radioButtonStandardTable->isChecked())
{ {
VMeasurement stable = data->GetMeasurement(item->text()); QString name = qApp->VarFromUser(item->text());
QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetValueStandardTableRow(item->text())) VMeasurement stable = data->GetMeasurement(name);
QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetValueStandardTableRow(name))
.arg(stable.GetDescription()); .arg(stable.GetDescription());
labelDescription->setText(desc); labelDescription->setText(desc);
return; return;
@ -687,22 +688,22 @@ void DialogTool::ValChenged(int row)
} }
if (radioButtonLengthLine->isChecked()) if (radioButtonLengthLine->isChecked())
{ {
QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetLine(item->text())) QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetLine(qApp->VarFromUser(item->text())))
.arg(tr("Line length")); .arg(tr("Line length"));
labelDescription->setText(desc); labelDescription->setText(desc);
return; return;
} }
if (radioButtonLengthArc->isChecked()) if (radioButtonLengthArc->isChecked())
{ {
QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetLengthArc(item->text())) QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetLengthArc(qApp->VarFromUser(item->text())))
.arg(tr("Arc length")); .arg(tr("Arc length"));
labelDescription->setText(desc); labelDescription->setText(desc);
return; return;
} }
if (radioButtonLengthCurve->isChecked()) if (radioButtonLengthCurve->isChecked())
{ {
QString desc = QString("%1(%2) - %3").arg(item->text()).arg(data->GetLengthSpline(item->text())) QString desc = QString("%1(%2) - %3").arg(item->text())
.arg(tr("Curve length")); .arg(data->GetLengthSpline(qApp->VarFromUser(item->text()))).arg(tr("Curve length"));
labelDescription->setText(desc); labelDescription->setText(desc);
return; return;
} }
@ -757,7 +758,7 @@ void DialogTool::ShowVariable(const QHash<key, val> *var)
while (i.hasNext()) while (i.hasNext())
{ {
i.next(); i.next();
map.insert(i.key(), i.value()); map.insert(qApp->VarToUser(i.key()), i.value());
} }
QMapIterator<key, val> iMap(map); QMapIterator<key, val> iMap(map);

View File

@ -1505,7 +1505,7 @@ void VApplication::InitPostfixOperators()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VApplication::Measurements(QString &newFormula, int position, const QString &token) bool VApplication::MeasurementsFromUser(QString &newFormula, int position, const QString &token) const
{ {
QMap<QString, VTranslation>::const_iterator i = measurements.constBegin(); QMap<QString, VTranslation>::const_iterator i = measurements.constBegin();
while (i != measurements.constEnd()) while (i != measurements.constEnd())
@ -1521,7 +1521,7 @@ bool VApplication::Measurements(QString &newFormula, int position, const QString
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VApplication::VariablesFromUser(QString &newFormula, int position, const QString &token) bool VApplication::VariablesFromUser(QString &newFormula, int position, const QString &token) const
{ {
QMap<QString, VTranslation>::const_iterator i = variables.constBegin(); QMap<QString, VTranslation>::const_iterator i = variables.constBegin();
while (i != variables.constEnd()) while (i != variables.constEnd())
@ -1537,7 +1537,7 @@ bool VApplication::VariablesFromUser(QString &newFormula, int position, const QS
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VApplication::PostfixOperators(QString &newFormula, int position, const QString &token) bool VApplication::PostfixOperatorsFromUser(QString &newFormula, int position, const QString &token) const
{ {
QMap<QString, VTranslation>::const_iterator i = postfixOperators.constBegin(); QMap<QString, VTranslation>::const_iterator i = postfixOperators.constBegin();
while (i != postfixOperators.constEnd()) while (i != postfixOperators.constEnd())
@ -1553,7 +1553,7 @@ bool VApplication::PostfixOperators(QString &newFormula, int position, const QSt
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VApplication::Functions(QString &newFormula, int position, const QString &token) bool VApplication::FunctionsFromUser(QString &newFormula, int position, const QString &token) const
{ {
QMap<QString, VTranslation>::const_iterator i = functions.constBegin(); QMap<QString, VTranslation>::const_iterator i = functions.constBegin();
while (i != functions.constEnd()) while (i != functions.constEnd())
@ -1569,14 +1569,14 @@ bool VApplication::Functions(QString &newFormula, int position, const QString &t
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VApplication::VariablesToUser(QString &newFormula, int position, const QString &token) bool VApplication::VariablesToUser(QString &newFormula, int position, const QString &token) const
{ {
QMap<QString, VTranslation>::const_iterator i = variables.constBegin(); QMap<QString, VTranslation>::const_iterator i = variables.constBegin();
while (i != variables.constEnd()) while (i != variables.constEnd())
{ {
if(token.indexOf( i.key() ) == 0) if(token.indexOf( i.key() ) == 0)
{ {
newFormula.replace(position, variables.value(i.key()).translate().length(), i.value().translate()); newFormula.replace(position, i.key().length(), i.value().translate());
return true; return true;
} }
++i; ++i;
@ -1592,9 +1592,55 @@ void VApplication::setPatternUnit(const Valentina::Units &patternUnit)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VApplication::Measurement(const QString &measurement) const QString VApplication::VarToUser(const QString &var) const
{ {
return measurements.value(measurement).translate(); if (measurements.contains(var))
{
return measurements.value(var).translate();
}
if (functions.contains(var))
{
return functions.value(var).translate();
}
if (postfixOperators.contains(var))
{
return postfixOperators.value(var).translate();
}
QString newVar = var;
if(VariablesToUser(newVar, 0, var))
{
return newVar;
}
return newVar;
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::VarFromUser(const QString &var) const
{
QString newVar = var;
if(MeasurementsFromUser(newVar, 0, var))
{
return newVar;
}
if(VariablesFromUser(newVar, 0, var))
{
return newVar;
}
if(PostfixOperatorsFromUser(newVar, 0, var))
{
return newVar;
}
if(FunctionsFromUser(newVar, 0, var))
{
return newVar;
}
return newVar;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -1653,7 +1699,7 @@ QString VApplication::FormulaFromUser(const QString &formula)
QMap<int, QString>::const_iterator i = tokens.constBegin(); QMap<int, QString>::const_iterator i = tokens.constBegin();
while (i != tokens.constEnd()) while (i != tokens.constEnd())
{ {
if(Measurements(newFormula, i.key(), i.value())) if(MeasurementsFromUser(newFormula, i.key(), i.value()))
{ {
++i; ++i;
continue; continue;
@ -1665,13 +1711,13 @@ QString VApplication::FormulaFromUser(const QString &formula)
continue; continue;
} }
if(PostfixOperators(newFormula, i.key(), i.value())) if(PostfixOperatorsFromUser(newFormula, i.key(), i.value()))
{ {
++i; ++i;
continue; continue;
} }
if(Functions(newFormula, i.key(), i.value())) if(FunctionsFromUser(newFormula, i.key(), i.value()))
{ {
++i; ++i;
continue; continue;

View File

@ -72,7 +72,8 @@ public:
QString pathToTables() const; QString pathToTables() const;
qreal widthMainLine() const; qreal widthMainLine() const;
qreal widthHairLine() const; qreal widthHairLine() const;
QString Measurement(const QString &measurement) const; QString VarToUser(const QString &var) const;
QString VarFromUser(const QString &var) const;
QString GuiText(const QString &measurement) const; QString GuiText(const QString &measurement) const;
QString Description(const QString &measurement) const; QString Description(const QString &measurement) const;
QString Variable(const QString &name) const; QString Variable(const QString &name) const;
@ -96,11 +97,11 @@ private:
void InitVariables(); void InitVariables();
void InitFunctions(); void InitFunctions();
void InitPostfixOperators(); void InitPostfixOperators();
bool Measurements(QString &newFormula, int position, const QString &token); bool MeasurementsFromUser(QString &newFormula, int position, const QString &token) const;
bool VariablesFromUser(QString &newFormula, int position, const QString &token); bool VariablesFromUser(QString &newFormula, int position, const QString &token) const;
bool PostfixOperators(QString &newFormula, int position, const QString &token); bool PostfixOperatorsFromUser(QString &newFormula, int position, const QString &token) const;
bool Functions(QString &newFormula, int position, const QString &token); bool FunctionsFromUser(QString &newFormula, int position, const QString &token) const;
bool VariablesToUser(QString &newFormula, int position, const QString &token); bool VariablesToUser(QString &newFormula, int position, const QString &token) const;
}; };
inline Valentina::Units VApplication::patternUnit() const inline Valentina::Units VApplication::patternUnit() const