From 7bdc10419cbd18dedb56024dbca163994c736474 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 21 Jan 2018 16:00:53 +0200 Subject: [PATCH] Speed optimization translating internal variables. --HG-- branch : develop --- src/libs/qmuparser/qmuparser.pro | 2 +- src/libs/qmuparser/qmuparserdef.h | 4 +- src/libs/qmuparser/qmuparsererror.h | 2 +- src/libs/qmuparser/qmutranslation.cpp | 35 ++++++++++++--- src/libs/qmuparser/qmutranslation.h | 4 +- src/libs/vmisc/vcommonsettings.cpp | 8 +++- .../vpatterndb/vtranslatemeasurements.cpp | 9 ++-- src/libs/vpatterndb/vtranslatevars.cpp | 43 +++++++++++-------- .../support/dialogeditwrongformula.cpp | 2 +- 9 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/libs/qmuparser/qmuparser.pro b/src/libs/qmuparser/qmuparser.pro index 785d12831..0ca5f1ea7 100644 --- a/src/libs/qmuparser/qmuparser.pro +++ b/src/libs/qmuparser/qmuparser.pro @@ -44,7 +44,7 @@ OBJECTS_DIR = obj include(qmuparser.pri) -VERSION = 2.5.0 +VERSION = 2.6.0 # Allow MAC OS X to find library inside a bundle macx:QMAKE_SONAME_PREFIX = @rpath diff --git a/src/libs/qmuparser/qmuparserdef.h b/src/libs/qmuparser/qmuparserdef.h index d5d5126f6..f4c462f88 100644 --- a/src/libs/qmuparser/qmuparserdef.h +++ b/src/libs/qmuparser/qmuparserdef.h @@ -32,8 +32,8 @@ @brief This file contains standard definitions used by the parser. */ -#define QMUP_VERSION "2.5.0" -#define QMUP_VERSION_DATE "20170101; GC" +#define QMUP_VERSION "2.6.0" +#define QMUP_VERSION_DATE "20180121; GC" #define QMUP_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/src/libs/qmuparser/qmuparsererror.h b/src/libs/qmuparser/qmuparsererror.h index b93ca0ad0..8453b1a19 100644 --- a/src/libs/qmuparser/qmuparsererror.h +++ b/src/libs/qmuparser/qmuparsererror.h @@ -126,7 +126,7 @@ inline const QmuParserErrorMsg& QmuParserErrorMsg::Instance() //--------------------------------------------------------------------------------------------------------------------- inline QString QmuParserErrorMsg::operator[] ( int a_iIdx ) const { - return m_vErrMsg.value(a_iIdx).translate(); + return m_vErrMsg.value(a_iIdx).translate(QString()); } //--------------------------------------------------------------------------- diff --git a/src/libs/qmuparser/qmutranslation.cpp b/src/libs/qmuparser/qmutranslation.cpp index 7efa1fc83..5f9adaeec 100644 --- a/src/libs/qmuparser/qmutranslation.cpp +++ b/src/libs/qmuparser/qmutranslation.cpp @@ -23,6 +23,7 @@ #include #include +#include namespace qmu { @@ -41,12 +42,17 @@ QmuTranslation QmuTranslation::translate(const QString &context, const QString & //--------------------------------------------------------------------------------------------------------------------- QmuTranslation::QmuTranslation() - :mcontext(QString()), msourceText(QString()), mdisambiguation(QString()), mn(-1) + : mcontext(), msourceText(), mdisambiguation(), mn(-1), localeName(), cachedTranslation() {} //--------------------------------------------------------------------------------------------------------------------- QmuTranslation::QmuTranslation(const QString &context, const QString &sourceText, const QString &disambiguation, int n) - :mcontext(context), msourceText(sourceText), mdisambiguation(disambiguation), mn(n) + : mcontext(context), + msourceText(sourceText), + mdisambiguation(disambiguation), + mn(n), + localeName(), + cachedTranslation() {} //--------------------------------------------------------------------------------------------------------------------- @@ -60,20 +66,35 @@ QmuTranslation &QmuTranslation::operator=(const QmuTranslation &tr) this->msourceText = tr.getMsourceText(); this->mdisambiguation = tr.getMdisambiguation(); this->mn = tr.getN(); + this->localeName.clear(); + this->cachedTranslation.clear(); return *this; } //--------------------------------------------------------------------------------------------------------------------- QmuTranslation::QmuTranslation(const QmuTranslation &tr) - :mcontext(tr.getMcontext()), msourceText(tr.getMsourceText()), mdisambiguation(tr.getMdisambiguation()), - mn(tr.getN()) + : mcontext(tr.mcontext), + msourceText(tr.msourceText), + mdisambiguation(tr.mdisambiguation), + mn(tr.mn), + localeName(), + cachedTranslation() {} //--------------------------------------------------------------------------------------------------------------------- -QString QmuTranslation::translate() const +QString QmuTranslation::translate(const QString &locale) const { - return QCoreApplication::translate(mcontext.toUtf8().constData(), msourceText.toUtf8().constData(), - mdisambiguation.toUtf8().constData(), mn); + if (cachedTranslation.isEmpty() || locale.isEmpty() || localeName != locale) + { + if (not locale.isEmpty()) + { + localeName = locale; + } + + cachedTranslation = QCoreApplication::translate(mcontext.toUtf8().constData(), msourceText.toUtf8().constData(), + mdisambiguation.toUtf8().constData(), mn); + } + return cachedTranslation; } } // namespace qmu diff --git a/src/libs/qmuparser/qmutranslation.h b/src/libs/qmuparser/qmutranslation.h index 9cb727bba..a3d7e259b 100644 --- a/src/libs/qmuparser/qmutranslation.h +++ b/src/libs/qmuparser/qmutranslation.h @@ -58,7 +58,7 @@ public: int n = -1); QmuTranslation &operator=(const QmuTranslation &tr); QmuTranslation(const QmuTranslation &tr); - QString translate() const; + QString translate(const QString &locale) const; static QmuTranslation translate(const QString &context, const QString &sourceText, const QString &disambiguation = nullptr, int n = -1); QString getMcontext() const; @@ -70,6 +70,8 @@ private: QString msourceText; QString mdisambiguation; int mn; + mutable QString localeName; + mutable QString cachedTranslation; }; //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vmisc/vcommonsettings.cpp b/src/libs/vmisc/vcommonsettings.cpp index b5c568aa3..ac59b2e9b 100644 --- a/src/libs/vmisc/vcommonsettings.cpp +++ b/src/libs/vmisc/vcommonsettings.cpp @@ -97,6 +97,7 @@ const QString settingLabelUserTimeFormats = QStringLiteral("label/userTimeFormat // Reading settings file is very expensive, cache curve approximation to speed up getting value qreal curveApproximationCached = -1; +QString localeCached = QString(); //--------------------------------------------------------------------------------------------------------------------- QStringList ClearFormats(const QStringList &predefinedFormats, QStringList formats) @@ -431,13 +432,18 @@ void VCommonSettings::SetAutosaveTime(const int &value) //--------------------------------------------------------------------------------------------------------------------- QString VCommonSettings::GetLocale() const { - return value(settingConfigurationLocale, QLocale().name()).toString(); + if (localeCached.isEmpty()) + { + localeCached = value(settingConfigurationLocale, QLocale().name()).toString(); + } + return localeCached; } //--------------------------------------------------------------------------------------------------------------------- void VCommonSettings::SetLocale(const QString &value) { setValue(settingConfigurationLocale, value); + localeCached = value; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vpatterndb/vtranslatemeasurements.cpp b/src/libs/vpatterndb/vtranslatemeasurements.cpp index 5e788a828..5959477f7 100644 --- a/src/libs/vpatterndb/vtranslatemeasurements.cpp +++ b/src/libs/vpatterndb/vtranslatemeasurements.cpp @@ -30,6 +30,7 @@ #include "../qmuparser/qmutranslation.h" #include "measurements.h" +#include "../vmisc/vabstractapplication.h" //--------------------------------------------------------------------------------------------------------------------- VTranslateMeasurements::VTranslateMeasurements() @@ -61,7 +62,7 @@ bool VTranslateMeasurements::MeasurementsFromUser(QString &newFormula, int posit QMap::const_iterator i = measurements.constBegin(); while (i != measurements.constEnd()) { - if (token == i.value().translate()) + if (token == i.value().translate(qApp->Settings()->GetLocale())) { newFormula.replace(position, token.length(), i.key()); bias = token.length() - i.key().length(); @@ -77,7 +78,7 @@ QString VTranslateMeasurements::MToUser(const QString &measurement) const { if (measurements.contains(measurement)) { - return measurements.value(measurement).translate(); + return measurements.value(measurement).translate(qApp->Settings()->GetLocale()); } else { @@ -109,7 +110,7 @@ QString VTranslateMeasurements::GuiText(const QString &measurement) const { if (guiTexts.contains(measurement)) { - return guiTexts.value(measurement).translate(); + return guiTexts.value(measurement).translate(qApp->Settings()->GetLocale()); } else { @@ -122,7 +123,7 @@ QString VTranslateMeasurements::Description(const QString &measurement) const { if (descriptions.contains(measurement)) { - return descriptions.value(measurement).translate(); + return descriptions.value(measurement).translate(qApp->Settings()->GetLocale()); } else { diff --git a/src/libs/vpatterndb/vtranslatevars.cpp b/src/libs/vpatterndb/vtranslatevars.cpp index 1c459c9a0..e80a63be9 100644 --- a/src/libs/vpatterndb/vtranslatevars.cpp +++ b/src/libs/vpatterndb/vtranslatevars.cpp @@ -490,7 +490,7 @@ void VTranslateVars::PrepareFunctionTranslations() QMap::const_iterator i = functions.constBegin(); while (i != functions.constEnd()) { - const QString translated = i.value().translate(); + const QString translated = i.value().translate(qApp->Settings()->GetLocale()); if (i.key() != translated) { translatedFunctions.insert(translated, i.key()); @@ -571,14 +571,15 @@ void VTranslateVars::BiasTokens(int position, int bias, QMap &toke */ bool VTranslateVars::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const { - const QString currentLengthTr = variables.value(currentLength).translate(); - const QString currentSeamAllowanceTr = variables.value(currentSeamAllowance).translate(); + const QString currentLengthTr = variables.value(currentLength).translate(qApp->Settings()->GetLocale()); + const QString currentSeamAllowanceTr = variables.value(currentSeamAllowance) + .translate(qApp->Settings()->GetLocale()); QMap::const_iterator i = variables.constBegin(); while (i != variables.constEnd()) { const qmu::QmuTranslation &var = i.value(); - const QString varTr = var.translate(); + const QString varTr = var.translate(qApp->Settings()->GetLocale()); if (token.indexOf(varTr) == 0) { @@ -613,7 +614,7 @@ bool VTranslateVars::FunctionsFromUser(QString &newFormula, int position, const QMap::const_iterator i = functions.constBegin(); while (i != functions.constEnd()) { - if (token == i.value().translate()) + if (token == i.value().translate(qApp->Settings()->GetLocale())) { newFormula.replace(position, token.length(), i.key()); bias = token.length() - i.key().length(); @@ -646,10 +647,10 @@ bool VTranslateVars::VariablesToUser(QString &newFormula, int position, const QS continue; } - newFormula.replace(position, i.key().length(), i.value().translate()); + newFormula.replace(position, i.key().length(), i.value().translate(qApp->Settings()->GetLocale())); QString newToken = token; - newToken.replace(0, i.key().length(), i.value().translate()); + newToken.replace(0, i.key().length(), i.value().translate(qApp->Settings()->GetLocale())); bias = token.length() - newToken.length(); return true; } @@ -685,7 +686,7 @@ QString VTranslateVars::PlaceholderToUser(QString var) const if (placeholders.contains(var)) { - return placeholders.value(var).translate() + number; + return placeholders.value(var).translate(qApp->Settings()->GetLocale()) + number; } return var; @@ -698,7 +699,7 @@ QString VTranslateVars::PlaceholderToUserText(QString text) const auto i = placeholders.constBegin(); while (i != placeholders.constEnd()) { - const QString translated = per + i.value().translate() + per; + const QString translated = per + i.value().translate(qApp->Settings()->GetLocale()) + per; const QString original = per + i.key() + per; if (translated != original) @@ -717,7 +718,7 @@ QString VTranslateVars::PlaceholderFromUserText(QString text) const auto i = placeholders.constBegin(); while (i != placeholders.constEnd()) { - const QString translated = per + i.value().translate() + per; + const QString translated = per + i.value().translate(qApp->Settings()->GetLocale()) + per; const QString original = per + i.key() + per; if (translated != original) @@ -734,12 +735,12 @@ QString VTranslateVars::VarToUser(const QString &var) const { if (measurements.contains(var)) { - return measurements.value(var).translate(); + return measurements.value(var).translate(qApp->Settings()->GetLocale()); } if (functions.contains(var)) { - return functions.value(var).translate(); + return functions.value(var).translate(qApp->Settings()->GetLocale()); } return InternalVarToUser(var); @@ -770,19 +771,19 @@ QString VTranslateVars::VarFromUser(const QString &var) const //--------------------------------------------------------------------------------------------------------------------- QString VTranslateVars::PMSystemName(const QString &code) const { - return PMSystemNames.value(code).translate(); + return PMSystemNames.value(code).translate(qApp->Settings()->GetLocale()); } //--------------------------------------------------------------------------------------------------------------------- QString VTranslateVars::PMSystemAuthor(const QString &code) const { - return PMSystemAuthors.value(code).translate(); + return PMSystemAuthors.value(code).translate(qApp->Settings()->GetLocale()); } //--------------------------------------------------------------------------------------------------------------------- QString VTranslateVars::PMSystemBook(const QString &code) const { - return PMSystemBooks.value(code).translate(); + return PMSystemBooks.value(code).translate(qApp->Settings()->GetLocale()); } //--------------------------------------------------------------------------------------------------------------------- @@ -938,8 +939,10 @@ QString VTranslateVars::FormulaToUser(const QString &formula, bool osSeparator) { if (measurements.contains(tValues.at(i))) { - newFormula.replace(tKeys.at(i), tValues.at(i).length(), measurements.value(tValues.at(i)).translate()); - int bias = tValues.at(i).length() - measurements.value(tValues.at(i)).translate().length(); + newFormula.replace(tKeys.at(i), tValues.at(i).length(), + measurements.value(tValues.at(i)).translate(qApp->Settings()->GetLocale())); + int bias = tValues.at(i).length() - measurements.value(tValues.at(i)) + .translate(qApp->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); @@ -951,8 +954,10 @@ QString VTranslateVars::FormulaToUser(const QString &formula, bool osSeparator) if (functions.contains(tValues.at(i))) { - newFormula.replace(tKeys.at(i), tValues.at(i).length(), functions.value(tValues.at(i)).translate()); - int bias = tValues.at(i).length() - functions.value(tValues.at(i)).translate().length(); + newFormula.replace(tKeys.at(i), tValues.at(i).length(), + functions.value(tValues.at(i)).translate(qApp->Settings()->GetLocale())); + int bias = tValues.at(i).length() - functions.value(tValues.at(i)) + .translate(qApp->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); diff --git a/src/libs/vtools/dialogs/support/dialogeditwrongformula.cpp b/src/libs/vtools/dialogs/support/dialogeditwrongformula.cpp index 300160bd5..40903534b 100644 --- a/src/libs/vtools/dialogs/support/dialogeditwrongformula.cpp +++ b/src/libs/vtools/dialogs/support/dialogeditwrongformula.cpp @@ -574,7 +574,7 @@ void DialogEditWrongFormula::ShowFunctions() while (i != qApp->TrVars()->GetFunctions().constEnd()) { ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1); - QTableWidgetItem *item = new QTableWidgetItem(i.value().translate()); + QTableWidgetItem *item = new QTableWidgetItem(i.value().translate(qApp->Settings()->GetLocale())); item->setFont(QFont("Times", 12, QFont::Bold)); ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, ColumnName, item); item->setToolTip(i.value().getMdisambiguation());