Fixed issue #776. Valentina cannot recognize translated functions.

(grafted from 5b89c34d11eea52b32e9fb60b87820ecc5187c31)

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-12-04 11:44:29 +02:00
parent cf727e0365
commit a4df5612d9
8 changed files with 62 additions and 17 deletions

View File

@ -72,6 +72,7 @@
- [#767] Valentina produces wrong main path.
- Fix issue. Curves look too wavy.
- [#773] Tool Point intersection curve and axis cannot find desired intersection point.
- [#776] Valentina cannot recognize translated functions.
# Version 0.5.0 May 9, 2017
- [#581] User can now filter input lists by keyword in function wizard.

View File

@ -65,12 +65,12 @@ QmuParserBase::QmuParserBase()
: m_locale(QLocale::c()),
m_decimalPoint(QLocale::c().decimalPoint()),
m_thousandsSeparator(QLocale::c().groupSeparator()),
m_FunDef(),
m_pParseFormula(&QmuParserBase::ParseString),
m_vRPN(),
m_vStringBuf(),
m_vStringVarBuf(),
m_pTokenReader(),
m_FunDef(),
m_PostOprtDef(),
m_InfixOprtDef(),
m_OprtDef(),
@ -101,12 +101,12 @@ QmuParserBase::QmuParserBase(const QmuParserBase &a_Parser)
: m_locale(a_Parser.getLocale()),
m_decimalPoint(a_Parser.getDecimalPoint()),
m_thousandsSeparator(a_Parser.getThousandsSeparator()),
m_FunDef(),
m_pParseFormula(&QmuParserBase::ParseString),
m_vRPN(),
m_vStringBuf(),
m_vStringVarBuf(),
m_pTokenReader(),
m_FunDef(),
m_PostOprtDef(),
m_InfixOprtDef(),
m_OprtDef(),

View File

@ -138,8 +138,11 @@ protected:
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.
static bool g_DbgDumpCmdCode;
static bool g_DbgDumpStack;
void AddCallback(const QString &a_strName, const QmuParserCallback &a_Callback, funmap_type &a_Storage,
const QString &a_szCharSet );
void Init();
virtual void InitCharSets() = 0;
virtual void InitFun() = 0;
@ -228,7 +231,6 @@ private:
std::unique_ptr<token_reader_type> m_pTokenReader; ///< Managed pointer to the token reader object.
funmap_type m_FunDef; ///< Map of function names and pointers.
funmap_type m_PostOprtDef; ///< Postfix operator callbacks
funmap_type m_InfixOprtDef; ///< unary infix operator.
funmap_type m_OprtDef; ///< Binary operator callbacks
@ -255,8 +257,6 @@ private:
void Assign(const QmuParserBase &a_Parser);
void InitTokenReader();
void ReInit() const;
void AddCallback(const QString &a_strName, const QmuParserCallback &a_Callback,
funmap_type &a_Storage, const QString &a_szCharSet );
void ApplyRemainingOprt(QStack<token_type> &a_stOpt, QStack<token_type> &a_stVal) const;
void ApplyBinOprt(QStack<token_type> &a_stOpt, QStack<token_type> &a_stVal) const;
void ApplyIfElse(QStack<token_type> &a_stOpt, QStack<token_type> &a_stVal) const;

View File

@ -86,7 +86,6 @@ QmuParserCallback::QmuParserCallback ( fun_type3 a_pFun, bool a_bAllowOpti )
m_iCode ( cmFUNC ), m_iType ( tpDBL ), m_bAllowOpti ( a_bAllowOpti )
{}
//---------------------------------------------------------------------------------------------------------------------
#ifdef __GNUC__
__extension__
@ -96,7 +95,6 @@ QmuParserCallback::QmuParserCallback ( fun_type4 a_pFun, bool a_bAllowOpti )
m_iCode ( cmFUNC ), m_iType ( tpDBL ), m_bAllowOpti ( a_bAllowOpti )
{}
//---------------------------------------------------------------------------------------------------------------------
#ifdef __GNUC__
__extension__

View File

@ -53,8 +53,10 @@ QmuTokenParser::QmuTokenParser()
*
* @param formula string with formula.
* @param fromUser true if we parse formula from user
* @param translatedFunctions
*/
QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator, bool fromUser)
QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator,
bool fromUser, const QMap<QString, QString> &translatedFunctions)
:QmuFormulaBase()
{
InitCharSets();
@ -63,16 +65,26 @@ QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator, bool fr
SetSepForTr(osSeparator, fromUser);
// Fix for issue #776. Valentina cannot recognize translated functions.
QMap<QString, QString>::const_iterator i = translatedFunctions.constBegin();
while (i != translatedFunctions.constEnd())
{
if (i.key() != i.value())
{
auto search = m_FunDef.find(i.value());
if(search != m_FunDef.end())
{
AddCallback(i.key(), search->second, m_FunDef, ValidNameChars());
}
}
++i;
}
SetExpr(formula);
//Need run for making tokens. Don't catch exception here, because we want know if formula has error.
Eval();
}
//---------------------------------------------------------------------------------------------------------------------
QmuTokenParser::~QmuTokenParser()
{
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief IsSingle test formula and return true if it contain only one number.

View File

@ -36,8 +36,9 @@ namespace qmu
class QMUPARSERSHARED_EXPORT QmuTokenParser : public QmuFormulaBase
{
public:
QmuTokenParser(const QString &formula, bool osSeparator, bool fromUser = true);
virtual ~QmuTokenParser() Q_DECL_OVERRIDE;
QmuTokenParser(const QString &formula, bool osSeparator, bool fromUser = true,
const QMap<QString, QString> &translatedFunctions = QMap<QString, QString>());
virtual ~QmuTokenParser() = default;
static bool IsSingle(const QString &formula);

View File

@ -55,13 +55,16 @@ VTranslateVars::VTranslateVars()
functions(QMap<QString, qmu::QmuTranslation>()),
postfixOperators(QMap<QString, qmu::QmuTranslation>()),
placeholders(QMap<QString, qmu::QmuTranslation>()),
stDescriptions(QMap<QString, qmu::QmuTranslation>())
stDescriptions(QMap<QString, qmu::QmuTranslation>()),
translatedFunctions(QMap<QString, QString>())
{
InitPatternMakingSystems();
InitVariables();
InitFunctions();
InitPostfixOperators();
InitPlaceholder();
PrepareFunctionTranslations();
}
//---------------------------------------------------------------------------------------------------------------------
@ -493,6 +496,22 @@ void VTranslateVars::InitPlaceholder()
#undef translate
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::PrepareFunctionTranslations()
{
translatedFunctions.clear();
QMap<QString, qmu::QmuTranslation>::const_iterator i = functions.constBegin();
while (i != functions.constEnd())
{
const QString translated = i.value().translate();
if (i.key() != translated)
{
translatedFunctions.insert(translated, i.key());
}
++i;
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTranslateVars::InitSystem(const QString &code, const qmu::QmuTranslation &name, const qmu::QmuTranslation &author,
const qmu::QmuTranslation &book)
@ -837,7 +856,9 @@ QString VTranslateVars::FormulaFromUser(const QString &formula, bool osSeparator
}
QString newFormula = formula;// Local copy for making changes
QScopedPointer<qmu::QmuTokenParser> cal(new qmu::QmuTokenParser(formula, osSeparator));// Eval formula
// Eval formula
QScopedPointer<qmu::QmuTokenParser> cal(new qmu::QmuTokenParser(formula, osSeparator, true,
qApp->TrVars()->GetTranslatedFunctions()));
QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements)
QMap<int, QString> numbers = cal->GetNumbers();// All numbers in expression for changing decimal separator
delete cal.take();
@ -1107,6 +1128,14 @@ void VTranslateVars::Retranslate()
InitFunctions();
InitPostfixOperators();
InitPlaceholder();
PrepareFunctionTranslations();
}
//---------------------------------------------------------------------------------------------------------------------
QMap<QString, QString> VTranslateVars::GetTranslatedFunctions() const
{
return translatedFunctions;
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -68,6 +68,7 @@ public:
virtual void Retranslate() Q_DECL_OVERRIDE;
QMap<QString, QString> GetTranslatedFunctions() const;
QMap<QString, qmu::QmuTranslation> GetFunctions() const;
static void BiasTokens(int position, int bias, QMap<int, QString> &tokens);
@ -82,6 +83,7 @@ private:
QMap<QString, qmu::QmuTranslation> postfixOperators;
QMap<QString, qmu::QmuTranslation> placeholders;
QMap<QString, qmu::QmuTranslation> stDescriptions;
QMap<QString, QString> translatedFunctions;
void InitPatternMakingSystems();
void InitVariables();
@ -89,6 +91,8 @@ private:
void InitPostfixOperators();
void InitPlaceholder();
void PrepareFunctionTranslations();
void InitSystem(const QString &code, const qmu::QmuTranslation &name, const qmu::QmuTranslation &author,
const qmu::QmuTranslation &book);