Refactoring.

This commit is contained in:
Roman Telezhynskyi 2022-08-02 20:12:23 +03:00
parent 4dd7b93aab
commit 7ae794e0b7
4 changed files with 290 additions and 261 deletions

View File

@ -48,7 +48,6 @@
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VTranslateVars::VTranslateVars() VTranslateVars::VTranslateVars()
:VTranslateMeasurements()
{ {
InitPatternMakingSystems(); InitPatternMakingSystems();
InitVariables(); InitVariables();
@ -545,8 +544,8 @@ void VTranslateVars::InitFunctions()
functions.insert(csrInch_F, translate("VTranslateVars", "csrInch", "cut, split and rotate modeling operation. Takes" functions.insert(csrInch_F, translate("VTranslateVars", "csrInch", "cut, split and rotate modeling operation. Takes"
" inch units.")); " inch units."));
functionsDescriptions.insert(csrInch_F, translate("VTranslateVars", "cut, split and rotate modeling operation. Takes" functionsDescriptions.insert(csrInch_F, translate("VTranslateVars", "cut, split and rotate modeling operation. "
" inch units.", "function csrInch")); "Takes inch units.", "function csrInch"));
functionsArguments.insert(csrInch_F, threeArguments); functionsArguments.insert(csrInch_F, threeArguments);
functions.insert(abs_F, translate("VTranslateVars", "abs", "absolute value")); functions.insert(abs_F, translate("VTranslateVars", "abs", "absolute value"));
@ -622,7 +621,7 @@ void VTranslateVars::InitSystem(const QString &code, const qmu::QmuTranslation &
* @param numbers all numbers * @param numbers all numbers
*/ */
void VTranslateVars::CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens, void VTranslateVars::CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens,
QMap<int, QString> &numbers) const QMap<int, QString> &numbers)
{ {
if (bias == 0) if (bias == 0)
{ {
@ -633,6 +632,188 @@ void VTranslateVars::CorrectionsPositions(int position, int bias, QMap<int, QStr
BiasTokens(position, bias, numbers); BiasTokens(position, bias, numbers);
} }
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::TranslateVarsFromUser(QString &newFormula, QMap<int, QString> &tokens,
QMap<int, QString> &numbers) const
{
QList<int> tKeys = tokens.keys();// Take all tokens positions
QList<QString> tValues = tokens.values();
for (int i = 0; i < tKeys.size(); ++i)
{
int bias = 0;
if (MeasurementsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position of next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (VariablesFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position of next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (FunctionsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position of next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (tValues.at(i) == QLocale().negativeSign())
{// unary minus
newFormula.replace(tKeys.at(i), 1, '-');
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::TranslateNumbersFromUser(QString &newFormula, QMap<int, QString> &tokens,
QMap<int, QString> &numbers, bool osSeparator)
{
QLocale loc = QLocale(); // User locale
if (loc != QLocale::c() && osSeparator)
{// User want use Os separator
QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i)
{
loc = QLocale();// From system locale
bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok);
if (not ok)
{
qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue;//Leave with out translation
}
loc = QLocale::c();// To internal locale
const QString dStr = loc.toString(d);// Internal look for number
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0)
{// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys();
nValues = numbers.values();
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::TranslateVarsToUser(QString &newFormula, QMap<int, QString> &tokens,
QMap<int, QString> &numbers) const
{
QList<int> tKeys = tokens.keys();
QList<QString> tValues = tokens.values();
for (int i = 0; i < tKeys.size(); ++i)
{
if (measurements.contains(tValues.at(i)))
{
newFormula.replace(tKeys.at(i), tValues.at(i).length(),
measurements.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()));
int bias = tValues.at(i).length() -
measurements.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()).length();
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (functions.contains(tValues.at(i)))
{
newFormula.replace(tKeys.at(i), tValues.at(i).length(),
functions.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()));
int bias = tValues.at(i).length() -
functions.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()).length();
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
int bias = 0;
if (VariablesToUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (tValues.at(i) == QChar('-'))
{// unary minus
newFormula.replace(tKeys.at(i), 1, QLocale().negativeSign());
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::TranslateNumbersToUser(QString &newFormula, QMap<int, QString> &tokens,
QMap<int, QString> &numbers, bool osSeparator)
{
QLocale loc = QLocale();// User locale
if (loc != QLocale::C && osSeparator)
{// User want use Os separator
QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i)
{
loc = QLocale::c();// From pattern locale
bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok);
if (not ok)
{
qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue;//Leave with out translation
}
loc = QLocale();// To user locale
QString dStr = loc.toString(d);// Number string in user locale
if (loc.groupSeparator().isSpace())
{
dStr.replace(loc.groupSeparator(), QString());
}
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0)
{// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys();
nValues = numbers.values();
}
}
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief BiasTokens change position for each token that have position more then "position". * @brief BiasTokens change position for each token that have position more then "position".
@ -670,7 +851,7 @@ void VTranslateVars::BiasTokens(int position, int bias, QMap<int, QString> &toke
* @param bias hold change of length between translated and origin token string * @param bias hold change of length between translated and origin token string
* @return true if was found variable with same name. * @return true if was found variable with same name.
*/ */
bool VTranslateVars::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const auto VTranslateVars::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool
{ {
const QString currentLengthTr = const QString currentLengthTr =
variables.value(currentLength).translate(VAbstractApplication::VApp()->Settings()->GetLocale()); variables.value(currentLength).translate(VAbstractApplication::VApp()->Settings()->GetLocale());
@ -711,7 +892,7 @@ bool VTranslateVars::VariablesFromUser(QString &newFormula, int position, const
* @param bias hold change of length between translated and origin token string * @param bias hold change of length between translated and origin token string
* @return true if was found function with same name. * @return true if was found function with same name.
*/ */
bool VTranslateVars::FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const auto VTranslateVars::FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool
{ {
QMap<QString, qmu::QmuTranslation>::const_iterator i = functions.constBegin(); QMap<QString, qmu::QmuTranslation>::const_iterator i = functions.constBegin();
while (i != functions.constEnd()) while (i != functions.constEnd())
@ -736,7 +917,7 @@ bool VTranslateVars::FunctionsFromUser(QString &newFormula, int position, const
* @param bias hold change of length between translated and origin token string * @param bias hold change of length between translated and origin token string
* @return true if was found variable with same name. * @return true if was found variable with same name.
*/ */
bool VTranslateVars::VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const auto VTranslateVars::VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool
{ {
QMap<QString, qmu::QmuTranslation>::const_iterator i = variables.constBegin(); QMap<QString, qmu::QmuTranslation>::const_iterator i = variables.constBegin();
while (i != variables.constEnd()) while (i != variables.constEnd())
@ -764,7 +945,7 @@ bool VTranslateVars::VariablesToUser(QString &newFormula, int position, const QS
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::InternalVarToUser(const QString &var) const auto VTranslateVars::InternalVarToUser(const QString &var) const -> QString
{ {
QString newVar = var; QString newVar = var;
int bias = 0; int bias = 0;
@ -772,14 +953,12 @@ QString VTranslateVars::InternalVarToUser(const QString &var) const
{ {
return newVar; return newVar;
} }
else
{ return var;
return var;
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::VarToUser(const QString &var) const auto VTranslateVars::VarToUser(const QString &var) const -> QString
{ {
if (VAbstractApplication::VApp()->Settings()->GetLocale() == QStringLiteral("zh_CN")) if (VAbstractApplication::VApp()->Settings()->GetLocale() == QStringLiteral("zh_CN"))
{ {
@ -800,7 +979,7 @@ QString VTranslateVars::VarToUser(const QString &var) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::VarFromUser(const QString &var) const auto VTranslateVars::VarFromUser(const QString &var) const -> QString
{ {
if (VAbstractApplication::VApp()->Settings()->GetLocale() == QStringLiteral("zh_CN")) if (VAbstractApplication::VApp()->Settings()->GetLocale() == QStringLiteral("zh_CN"))
{ {
@ -827,19 +1006,19 @@ QString VTranslateVars::VarFromUser(const QString &var) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::PMSystemName(const QString &code) const auto VTranslateVars::PMSystemName(const QString &code) const -> QString
{ {
return PMSystemNames.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale()); return PMSystemNames.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::PMSystemAuthor(const QString &code) const auto VTranslateVars::PMSystemAuthor(const QString &code) const -> QString
{ {
return PMSystemAuthors.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale()); return PMSystemAuthors.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::PMSystemBook(const QString &code) const auto VTranslateVars::PMSystemBook(const QString &code) const -> QString
{ {
return PMSystemBooks.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale()); return PMSystemBooks.value(code).translate(VAbstractApplication::VApp()->Settings()->GetLocale());
} }
@ -852,98 +1031,30 @@ QString VTranslateVars::PMSystemBook(const QString &code) const
* @throw qmu::QmuParserError in case of a wrong expression * @throw qmu::QmuParserError in case of a wrong expression
* @return translated expression * @return translated expression
*/ */
QString VTranslateVars::FormulaFromUser(const QString &formula, bool osSeparator) const auto VTranslateVars::FormulaFromUser(const QString &formula, bool osSeparator) const -> QString
{ {
if (formula.isEmpty()) if (formula.isEmpty())
{ {
return formula; return formula;
} }
QString newFormula = formula;// Local copy for making changes
// Eval formula // Eval formula
QScopedPointer<qmu::QmuTokenParser> cal(new qmu::QmuTokenParser(formula, osSeparator, true, GetTranslatedFunctions())); QScopedPointer<qmu::QmuTokenParser> cal(
new qmu::QmuTokenParser(formula, osSeparator, true, GetTranslatedFunctions()));
QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements) QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements)
QMap<int, QString> numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator QMap<int, QString> numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator
delete cal.take(); delete cal.take();
QList<int> tKeys = tokens.keys();// Take all tokens positions QString newFormula = formula;// Local copy for making changes
QList<QString> tValues = tokens.values();
for (int i = 0; i < tKeys.size(); ++i)
{
int bias = 0;
if (MeasurementsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (VariablesFromUser(newFormula, tKeys.at(i), tValues.at(i), bias)) TranslateVarsFromUser(newFormula, tokens, numbers);
{ TranslateNumbersFromUser(newFormula, tokens, numbers, osSeparator);
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (FunctionsFromUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (tValues.at(i) == QLocale().negativeSign())
{// unary minus
newFormula.replace(tKeys.at(i), 1, '-');
}
}
QLocale loc = QLocale(); // User locale
if (loc != QLocale::c() && osSeparator)
{// User want use Os separator
QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i)
{
loc = QLocale();// From system locale
bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok);
if (ok == false)
{
qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue;//Leave with out translation
}
loc = QLocale::c();// To internal locale
const QString dStr = loc.toString(d);// Internal look for number
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0)
{// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys();
nValues = numbers.values();
}
}
}
return newFormula; return newFormula;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::TryFormulaFromUser(const QString &formula, bool osSeparator) auto VTranslateVars::TryFormulaFromUser(const QString &formula, bool osSeparator) -> QString
{ {
try try
{ {
@ -964,7 +1075,7 @@ QString VTranslateVars::TryFormulaFromUser(const QString &formula, bool osSepara
* @param formula expression that need translate * @param formula expression that need translate
* @return translated expression * @return translated expression
*/ */
QString VTranslateVars::FormulaToUser(const QString &formula, bool osSeparator) const auto VTranslateVars::FormulaToUser(const QString &formula, bool osSeparator) const -> QString
{ {
if (formula.isEmpty()) if (formula.isEmpty())
{ {
@ -991,98 +1102,14 @@ QString VTranslateVars::FormulaToUser(const QString &formula, bool osSeparator)
return newFormula; return newFormula;
} }
QList<int> tKeys = tokens.keys(); TranslateVarsToUser(newFormula, tokens, numbers);
QList<QString> tValues = tokens.values(); TranslateNumbersToUser(newFormula, tokens, numbers, osSeparator);
for (int i = 0; i < tKeys.size(); ++i)
{
if (measurements.contains(tValues.at(i)))
{
newFormula.replace(tKeys.at(i), tValues.at(i).length(),
measurements.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()));
int bias = tValues.at(i).length() - measurements.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()).length();
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (functions.contains(tValues.at(i)))
{
newFormula.replace(tKeys.at(i), tValues.at(i).length(),
functions.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()));
int bias = tValues.at(i).length() - functions.value(tValues.at(i))
.translate(VAbstractApplication::VApp()->Settings()->GetLocale()).length();
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
int bias = 0;
if (VariablesToUser(newFormula, tKeys.at(i), tValues.at(i), bias))
{
if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);
tKeys = tokens.keys();
tValues = tokens.values();
}
continue;
}
if (tValues.at(i) == QChar('-'))
{// unary minus
newFormula.replace(tKeys.at(i), 1, QLocale().negativeSign());
}
}
QLocale loc = QLocale();// User locale
if (loc != QLocale::C && osSeparator)
{// User want use Os separator
QList<int> nKeys = numbers.keys();// Positions for all numbers in expression
QList<QString> nValues = numbers.values();
for (int i = 0; i < nKeys.size(); ++i)
{
loc = QLocale::c();// From pattern locale
bool ok = false;
const qreal d = loc.toDouble(nValues.at(i), &ok);
if (ok == false)
{
qDebug()<<"Can't convert to double token"<<nValues.at(i);
continue;//Leave with out translation
}
loc = QLocale();// To user locale
QString dStr = loc.toString(d);// Number string in user locale
if (loc.groupSeparator().isSpace())
{
dStr.replace(loc.groupSeparator(), QString());
}
newFormula.replace(nKeys.at(i), nValues.at(i).length(), dStr);
const int bias = nValues.at(i).length() - dStr.length();
if (bias != 0)
{// Translated number has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(nKeys.at(i), bias, tokens, numbers);
nKeys = numbers.keys();
nValues = numbers.values();
}
}
}
return newFormula; return newFormula;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::TryFormulaToUser(const QString &formula, bool osSeparator) auto VTranslateVars::TryFormulaToUser(const QString &formula, bool osSeparator) -> QString
{ {
try try
{ {
@ -1116,25 +1143,25 @@ void VTranslateVars::Retranslate()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMap<QString, QString> VTranslateVars::GetTranslatedFunctions() const auto VTranslateVars::GetTranslatedFunctions() const -> QMap<QString, QString>
{ {
return translatedFunctions; return translatedFunctions;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMap<QString, qmu::QmuTranslation> VTranslateVars::GetFunctions() const auto VTranslateVars::GetFunctions() const -> QMap<QString, qmu::QmuTranslation>
{ {
return functions; return functions;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMap<QString, qmu::QmuTranslation> VTranslateVars::GetFunctionsDescriptions() const auto VTranslateVars::GetFunctionsDescriptions() const -> QMap<QString, qmu::QmuTranslation>
{ {
return functionsDescriptions; return functionsDescriptions;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMap<QString, QString> VTranslateVars::GetFunctionsArguments() const auto VTranslateVars::GetFunctionsArguments() const -> QMap<QString, QString>
{ {
return functionsArguments; return functionsArguments;
} }

View File

@ -33,43 +33,44 @@
#include <QtGlobal> #include <QtGlobal>
#include "vtranslatemeasurements.h" #include "vtranslatemeasurements.h"
#include "../vmisc/defglobal.h"
class VTranslateVars : public VTranslateMeasurements class VTranslateVars : public VTranslateMeasurements
{ {
public: public:
explicit VTranslateVars(); explicit VTranslateVars();
virtual ~VTranslateVars() = default; ~VTranslateVars() override = default;
bool VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const; auto VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool;
bool FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const; auto FunctionsFromUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool;
bool VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const; auto VariablesToUser(QString &newFormula, int position, const QString &token, int &bias) const -> bool;
QString InternalVarToUser(const QString &var) const; auto InternalVarToUser(const QString &var) const -> QString;
QString VarToUser(const QString &var) const; auto VarToUser(const QString &var) const -> QString;
QString VarFromUser(const QString &var) const; auto VarFromUser(const QString &var) const -> QString;
QString PMSystemName(const QString &code) const; auto PMSystemName(const QString &code) const -> QString;
QString PMSystemAuthor(const QString &code) const; auto PMSystemAuthor(const QString &code) const -> QString;
QString PMSystemBook(const QString &code) const; auto PMSystemBook(const QString &code) const -> QString;
QString FormulaFromUser(const QString &formula, bool osSeparator) const; auto FormulaFromUser(const QString &formula, bool osSeparator) const -> QString;
static QString TryFormulaFromUser(const QString &formula, bool osSeparator); static auto TryFormulaFromUser(const QString &formula, bool osSeparator) -> QString;
QString FormulaToUser(const QString &formula, bool osSeparator) const; auto FormulaToUser(const QString &formula, bool osSeparator) const -> QString;
static QString TryFormulaToUser(const QString &formula, bool osSeparator); static auto TryFormulaToUser(const QString &formula, bool osSeparator) -> QString;
virtual void Retranslate() override; void Retranslate() override;
QMap<QString, QString> GetTranslatedFunctions() const; auto GetTranslatedFunctions() const -> QMap<QString, QString>;
QMap<QString, qmu::QmuTranslation> GetFunctions() const; auto GetFunctions() const -> QMap<QString, qmu::QmuTranslation>;
QMap<QString, qmu::QmuTranslation> GetFunctionsDescriptions() const; auto GetFunctionsDescriptions() const -> QMap<QString, qmu::QmuTranslation>;
QMap<QString, QString> GetFunctionsArguments() const; auto GetFunctionsArguments() const -> QMap<QString, QString>;
static void BiasTokens(int position, int bias, QMap<int, QString> &tokens); static void BiasTokens(int position, int bias, QMap<int, QString> &tokens);
private: private:
Q_DISABLE_COPY(VTranslateVars) Q_DISABLE_COPY_MOVE(VTranslateVars) //NOLINT
QMap<QString, qmu::QmuTranslation> PMSystemNames{}; QMap<QString, qmu::QmuTranslation> PMSystemNames{};
QMap<QString, qmu::QmuTranslation> PMSystemAuthors{}; QMap<QString, qmu::QmuTranslation> PMSystemAuthors{};
QMap<QString, qmu::QmuTranslation> PMSystemBooks{}; QMap<QString, qmu::QmuTranslation> PMSystemBooks{};
@ -89,8 +90,15 @@ private:
void InitSystem(const QString &code, const qmu::QmuTranslation &name, const qmu::QmuTranslation &author, void InitSystem(const QString &code, const qmu::QmuTranslation &name, const qmu::QmuTranslation &author,
const qmu::QmuTranslation &book); const qmu::QmuTranslation &book);
void CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens, QMap<int, QString> &numbers) const; static void CorrectionsPositions(int position, int bias, QMap<int, QString> &tokens, QMap<int, QString> &numbers);
void TranslateVarsFromUser(QString &newFormula, QMap<int, QString> &tokens, QMap<int, QString> &numbers) const;
static void TranslateNumbersFromUser(QString &newFormula, QMap<int, QString> &tokens, QMap<int, QString> &numbers,
bool osSeparator);
void TranslateVarsToUser(QString &newFormula, QMap<int, QString> &tokens, QMap<int, QString> &numbers) const;
static void TranslateNumbersToUser(QString &newFormula, QMap<int, QString> &tokens, QMap<int, QString> &numbers,
bool osSeparator);
}; };
#endif // VTRANSLATEVARS_H #endif // VTRANSLATEVARS_H

View File

@ -62,7 +62,6 @@
#include "../vpatterndb/variables/vlineangle.h" #include "../vpatterndb/variables/vlineangle.h"
#include "../vpatterndb/variables/vlinelength.h" #include "../vpatterndb/variables/vlinelength.h"
#include "../vpatterndb/variables/vmeasurement.h" #include "../vpatterndb/variables/vmeasurement.h"
#include "../ifc/xml/vdomdocument.h"
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "../vmisc/vabstractapplication.h" #include "../vmisc/vabstractapplication.h"
#include "../vmisc/vcommonsettings.h" #include "../vmisc/vcommonsettings.h"
@ -79,14 +78,7 @@ DialogEditWrongFormula::DialogEditWrongFormula(const VContainer *data, quint32 t
ui(new Ui::DialogEditWrongFormula), ui(new Ui::DialogEditWrongFormula),
m_data(data), m_data(data),
m_toolId(toolId), m_toolId(toolId),
formula(), timerFormula(new QTimer(this))
formulaBaseHeight(0),
checkZero(false),
checkLessThanZero(false),
postfix(),
restoreCursor(false),
timerFormula(new QTimer(this)),
flagFormula(false)
{ {
SCASSERT(data != nullptr) SCASSERT(data != nullptr)
@ -494,10 +486,9 @@ void DialogEditWrongFormula::SetPreviewCalculationsMode()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString DialogEditWrongFormula::GetFormula() const auto DialogEditWrongFormula::GetFormula() const -> QString
{ {
return VAbstractApplication::VApp()->TrVars() return VTranslateVars::TryFormulaFromUser(formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
->TryFormulaFromUser(formula, VAbstractApplication::VApp()->Settings()->GetOsSeparator());
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -554,7 +545,7 @@ void DialogEditWrongFormula::SetDescription(const QString &name, qreal value, bo
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal DialogEditWrongFormula::Eval(const FormulaData &formulaData, bool &flag) auto DialogEditWrongFormula::Eval(const FormulaData &formulaData, bool &flag) -> qreal
{ {
const qreal result = EvalToolFormula(this, formulaData, flag); const qreal result = EvalToolFormula(this, formulaData, flag);
CheckState(); // Disable Ok and Apply buttons if something wrong. CheckState(); // Disable Ok and Apply buttons if something wrong.
@ -586,7 +577,7 @@ void DialogEditWrongFormula::ShowVariable(const QMap<key, val> &var)
if (iMap.value()->Filter(m_toolId) == false) if (iMap.value()->Filter(m_toolId) == false)
{// If we create this variable don't show {// If we create this variable don't show
ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1); ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
QTableWidgetItem *item = new QTableWidgetItem(iMap.key()); auto *item = new QTableWidgetItem(iMap.key());
QFont font = item->font(); QFont font = item->font();
font.setBold(true); font.setBold(true);
item->setFont(font); item->setFont(font);
@ -619,16 +610,16 @@ void DialogEditWrongFormula::ShowMeasurements(const QMap<QString, QSharedPointer
{ {
continue; //skip this measurement continue; //skip this measurement
} }
if (iMap.value()->Filter(m_toolId) == false) if (not iMap.value()->Filter(m_toolId))
{// If we create this variable don't show {// If we create this variable don't show
ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1); ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1);
QTableWidgetItem *itemName = new QTableWidgetItem(iMap.key()); auto *itemName = new QTableWidgetItem(iMap.key());
QFont fontName = itemName->font(); QFont fontName = itemName->font();
fontName.setBold(true); fontName.setBold(true);
itemName->setFont(fontName); itemName->setFont(fontName);
itemName->setToolTip(itemName->text()); itemName->setToolTip(itemName->text());
QTableWidgetItem *itemFullName = new QTableWidgetItem(); auto *itemFullName = new QTableWidgetItem();
QFont fontFullName = itemName->font(); QFont fontFullName = itemName->font();
fontFullName.setBold(true); fontFullName.setBold(true);
itemFullName->setFont(fontFullName); itemFullName->setFont(fontFullName);
@ -735,7 +726,7 @@ void DialogEditWrongFormula::FilterVariablesEdited(const QString &filter)
// show rows with matched filter // show rows with matched filter
const QList<QTableWidgetItem*> items = ui->tableWidget->findItems(filter, Qt::MatchContains); const QList<QTableWidgetItem*> items = ui->tableWidget->findItems(filter, Qt::MatchContains);
for (auto item : items) for (auto *item : items)
{ {
// If filter is empty findItems() for unknown reason returns nullptr items. // If filter is empty findItems() for unknown reason returns nullptr items.
if (item) if (item)

View File

@ -38,6 +38,8 @@
#include <QtGlobal> #include <QtGlobal>
#include <QDialog> #include <QDialog>
#include "../vmisc/defglobal.h"
class VMeasurement; class VMeasurement;
struct FormulaData; struct FormulaData;
class VContainer; class VContainer;
@ -57,37 +59,38 @@ class DialogEditWrongFormula;
*/ */
class DialogEditWrongFormula final : public QDialog class DialogEditWrongFormula final : public QDialog
{ {
Q_OBJECT Q_OBJECT //NOLINT
public: public:
DialogEditWrongFormula(const VContainer *data, quint32 toolId, QWidget *parent = nullptr); DialogEditWrongFormula(const VContainer *data, quint32 toolId, QWidget *parent = nullptr);
virtual ~DialogEditWrongFormula() override; ~DialogEditWrongFormula() override;
QString GetFormula() const; auto GetFormula() const -> QString;
void SetFormula(const QString &value); void SetFormula(const QString &value);
void setCheckZero(bool value); void setCheckZero(bool value);
void setCheckLessThanZero(bool value); void setCheckLessThanZero(bool value);
void setPostfix(const QString &value); void setPostfix(const QString &value);
void SetMeasurementsMode(); void SetMeasurementsMode();
void SetIncrementsMode(); void SetIncrementsMode();
void SetPreviewCalculationsMode(); void SetPreviewCalculationsMode();
public slots: public slots:
virtual void DialogAccepted(); virtual void DialogAccepted();
virtual void DialogRejected(); virtual void DialogRejected();
void EvalFormula();
void ValChanged(int row);
void PutHere();
void PutVal(QTableWidgetItem * item);
void Measurements(); void EvalFormula();
void LengthLines(); void ValChanged(int row);
void RadiusArcs(); void PutHere();
void AnglesCurves(); void PutVal(QTableWidgetItem * item);
void LengthCurves();
void CurvesCLength(); void Measurements();
void AngleLines(); void LengthLines();
void Increments(); void RadiusArcs();
void PreviewCalculations(); void AnglesCurves();
void Functions(); void LengthCurves();
void CurvesCLength();
void AngleLines();
void Increments();
void PreviewCalculations();
void Functions();
signals: signals:
/** /**
* @brief DialogClosed signal dialog closed * @brief DialogClosed signal dialog closed
@ -99,15 +102,15 @@ signals:
*/ */
void DialogApplied(); void DialogApplied();
protected: protected:
virtual bool IsValid() const final; virtual auto IsValid() const -> bool final;
virtual void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
virtual void showEvent( QShowEvent *event ) override; void showEvent( QShowEvent *event ) override;
virtual void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
virtual void CheckState(); virtual void CheckState();
private slots: private slots:
void FilterVariablesEdited(const QString &filter); void FilterVariablesEdited(const QString &filter);
private: private:
Q_DISABLE_COPY(DialogEditWrongFormula) Q_DISABLE_COPY_MOVE(DialogEditWrongFormula) //NOLINT
Ui::DialogEditWrongFormula *ui; Ui::DialogEditWrongFormula *ui;
const VContainer *m_data; const VContainer *m_data;
@ -115,19 +118,19 @@ private:
quint32 m_toolId; quint32 m_toolId;
/** @brief formula string with formula */ /** @brief formula string with formula */
QString formula; QString formula{};
/** @brief formulaBaseHeight base height defined by dialogui */ /** @brief formulaBaseHeight base height defined by dialogui */
int formulaBaseHeight; int formulaBaseHeight{0};
bool checkZero; bool checkZero{false};
bool checkLessThanZero; bool checkLessThanZero{false};
QString postfix; QString postfix{};
bool restoreCursor; bool restoreCursor{false};
QTimer *timerFormula; QTimer *timerFormula;
bool flagFormula; bool flagFormula{false};
/** @brief m_isInitialized true if window is initialized */ /** @brief m_isInitialized true if window is initialized */
bool m_isInitialized{false}; bool m_isInitialized{false};
@ -142,11 +145,11 @@ private:
void SetDescription(const QString &name, qreal value, bool specialUnits, const QString &description); void SetDescription(const QString &name, qreal value, bool specialUnits, const QString &description);
qreal Eval(const FormulaData &formulaData, bool &flag); auto Eval(const FormulaData &formulaData, bool &flag) -> qreal;
}; };
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
inline bool DialogEditWrongFormula::IsValid() const inline auto DialogEditWrongFormula::IsValid() const -> bool
{ {
return flagFormula; return flagFormula;
} }