Speed optimization translating internal variables.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2018-01-21 16:00:53 +02:00
parent 1cfd53c51e
commit 7bdc10419c
9 changed files with 72 additions and 37 deletions

View File

@ -44,7 +44,7 @@ OBJECTS_DIR = obj
include(qmuparser.pri) include(qmuparser.pri)
VERSION = 2.5.0 VERSION = 2.6.0
# Allow MAC OS X to find library inside a bundle # Allow MAC OS X to find library inside a bundle
macx:QMAKE_SONAME_PREFIX = @rpath macx:QMAKE_SONAME_PREFIX = @rpath

View File

@ -32,8 +32,8 @@
@brief This file contains standard definitions used by the parser. @brief This file contains standard definitions used by the parser.
*/ */
#define QMUP_VERSION "2.5.0" #define QMUP_VERSION "2.6.0"
#define QMUP_VERSION_DATE "20170101; GC" #define QMUP_VERSION_DATE "20180121; GC"
#define QMUP_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" #define QMUP_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@ -126,7 +126,7 @@ inline const QmuParserErrorMsg& QmuParserErrorMsg::Instance()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
inline QString QmuParserErrorMsg::operator[] ( int a_iIdx ) const inline QString QmuParserErrorMsg::operator[] ( int a_iIdx ) const
{ {
return m_vErrMsg.value(a_iIdx).translate(); return m_vErrMsg.value(a_iIdx).translate(QString());
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -23,6 +23,7 @@
#include <QByteArray> #include <QByteArray>
#include <QCoreApplication> #include <QCoreApplication>
#include <QLocale>
namespace qmu namespace qmu
{ {
@ -41,12 +42,17 @@ QmuTranslation QmuTranslation::translate(const QString &context, const QString &
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QmuTranslation::QmuTranslation() 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) 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->msourceText = tr.getMsourceText();
this->mdisambiguation = tr.getMdisambiguation(); this->mdisambiguation = tr.getMdisambiguation();
this->mn = tr.getN(); this->mn = tr.getN();
this->localeName.clear();
this->cachedTranslation.clear();
return *this; return *this;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QmuTranslation::QmuTranslation(const QmuTranslation &tr) QmuTranslation::QmuTranslation(const QmuTranslation &tr)
:mcontext(tr.getMcontext()), msourceText(tr.getMsourceText()), mdisambiguation(tr.getMdisambiguation()), : mcontext(tr.mcontext),
mn(tr.getN()) 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(), 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); mdisambiguation.toUtf8().constData(), mn);
} }
return cachedTranslation;
}
} // namespace qmu } // namespace qmu

View File

@ -58,7 +58,7 @@ public:
int n = -1); int n = -1);
QmuTranslation &operator=(const QmuTranslation &tr); QmuTranslation &operator=(const QmuTranslation &tr);
QmuTranslation(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, static QmuTranslation translate(const QString &context, const QString &sourceText,
const QString &disambiguation = nullptr, int n = -1); const QString &disambiguation = nullptr, int n = -1);
QString getMcontext() const; QString getMcontext() const;
@ -70,6 +70,8 @@ private:
QString msourceText; QString msourceText;
QString mdisambiguation; QString mdisambiguation;
int mn; int mn;
mutable QString localeName;
mutable QString cachedTranslation;
}; };
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -97,6 +97,7 @@ const QString settingLabelUserTimeFormats = QStringLiteral("label/userTimeFormat
// Reading settings file is very expensive, cache curve approximation to speed up getting value // Reading settings file is very expensive, cache curve approximation to speed up getting value
qreal curveApproximationCached = -1; qreal curveApproximationCached = -1;
QString localeCached = QString();
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QStringList ClearFormats(const QStringList &predefinedFormats, QStringList formats) QStringList ClearFormats(const QStringList &predefinedFormats, QStringList formats)
@ -431,13 +432,18 @@ void VCommonSettings::SetAutosaveTime(const int &value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VCommonSettings::GetLocale() const 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) void VCommonSettings::SetLocale(const QString &value)
{ {
setValue(settingConfigurationLocale, value); setValue(settingConfigurationLocale, value);
localeCached = value;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -30,6 +30,7 @@
#include "../qmuparser/qmutranslation.h" #include "../qmuparser/qmutranslation.h"
#include "measurements.h" #include "measurements.h"
#include "../vmisc/vabstractapplication.h"
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VTranslateMeasurements::VTranslateMeasurements() VTranslateMeasurements::VTranslateMeasurements()
@ -61,7 +62,7 @@ bool VTranslateMeasurements::MeasurementsFromUser(QString &newFormula, int posit
QMap<QString, qmu::QmuTranslation>::const_iterator i = measurements.constBegin(); QMap<QString, qmu::QmuTranslation>::const_iterator i = measurements.constBegin();
while (i != measurements.constEnd()) while (i != measurements.constEnd())
{ {
if (token == i.value().translate()) if (token == i.value().translate(qApp->Settings()->GetLocale()))
{ {
newFormula.replace(position, token.length(), i.key()); newFormula.replace(position, token.length(), i.key());
bias = token.length() - i.key().length(); bias = token.length() - i.key().length();
@ -77,7 +78,7 @@ QString VTranslateMeasurements::MToUser(const QString &measurement) const
{ {
if (measurements.contains(measurement)) if (measurements.contains(measurement))
{ {
return measurements.value(measurement).translate(); return measurements.value(measurement).translate(qApp->Settings()->GetLocale());
} }
else else
{ {
@ -109,7 +110,7 @@ QString VTranslateMeasurements::GuiText(const QString &measurement) const
{ {
if (guiTexts.contains(measurement)) if (guiTexts.contains(measurement))
{ {
return guiTexts.value(measurement).translate(); return guiTexts.value(measurement).translate(qApp->Settings()->GetLocale());
} }
else else
{ {
@ -122,7 +123,7 @@ QString VTranslateMeasurements::Description(const QString &measurement) const
{ {
if (descriptions.contains(measurement)) if (descriptions.contains(measurement))
{ {
return descriptions.value(measurement).translate(); return descriptions.value(measurement).translate(qApp->Settings()->GetLocale());
} }
else else
{ {

View File

@ -490,7 +490,7 @@ void VTranslateVars::PrepareFunctionTranslations()
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())
{ {
const QString translated = i.value().translate(); const QString translated = i.value().translate(qApp->Settings()->GetLocale());
if (i.key() != translated) if (i.key() != translated)
{ {
translatedFunctions.insert(translated, i.key()); translatedFunctions.insert(translated, i.key());
@ -571,14 +571,15 @@ void VTranslateVars::BiasTokens(int position, int bias, QMap<int, QString> &toke
*/ */
bool VTranslateVars::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const bool VTranslateVars::VariablesFromUser(QString &newFormula, int position, const QString &token, int &bias) const
{ {
const QString currentLengthTr = variables.value(currentLength).translate(); const QString currentLengthTr = variables.value(currentLength).translate(qApp->Settings()->GetLocale());
const QString currentSeamAllowanceTr = variables.value(currentSeamAllowance).translate(); const QString currentSeamAllowanceTr = variables.value(currentSeamAllowance)
.translate(qApp->Settings()->GetLocale());
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())
{ {
const qmu::QmuTranslation &var = i.value(); const qmu::QmuTranslation &var = i.value();
const QString varTr = var.translate(); const QString varTr = var.translate(qApp->Settings()->GetLocale());
if (token.indexOf(varTr) == 0) if (token.indexOf(varTr) == 0)
{ {
@ -613,7 +614,7 @@ bool VTranslateVars::FunctionsFromUser(QString &newFormula, int position, const
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())
{ {
if (token == i.value().translate()) if (token == i.value().translate(qApp->Settings()->GetLocale()))
{ {
newFormula.replace(position, token.length(), i.key()); newFormula.replace(position, token.length(), i.key());
bias = token.length() - i.key().length(); bias = token.length() - i.key().length();
@ -646,10 +647,10 @@ bool VTranslateVars::VariablesToUser(QString &newFormula, int position, const QS
continue; 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; 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(); bias = token.length() - newToken.length();
return true; return true;
} }
@ -685,7 +686,7 @@ QString VTranslateVars::PlaceholderToUser(QString var) const
if (placeholders.contains(var)) if (placeholders.contains(var))
{ {
return placeholders.value(var).translate() + number; return placeholders.value(var).translate(qApp->Settings()->GetLocale()) + number;
} }
return var; return var;
@ -698,7 +699,7 @@ QString VTranslateVars::PlaceholderToUserText(QString text) const
auto i = placeholders.constBegin(); auto i = placeholders.constBegin();
while (i != placeholders.constEnd()) 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; const QString original = per + i.key() + per;
if (translated != original) if (translated != original)
@ -717,7 +718,7 @@ QString VTranslateVars::PlaceholderFromUserText(QString text) const
auto i = placeholders.constBegin(); auto i = placeholders.constBegin();
while (i != placeholders.constEnd()) 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; const QString original = per + i.key() + per;
if (translated != original) if (translated != original)
@ -734,12 +735,12 @@ QString VTranslateVars::VarToUser(const QString &var) const
{ {
if (measurements.contains(var)) if (measurements.contains(var))
{ {
return measurements.value(var).translate(); return measurements.value(var).translate(qApp->Settings()->GetLocale());
} }
if (functions.contains(var)) if (functions.contains(var))
{ {
return functions.value(var).translate(); return functions.value(var).translate(qApp->Settings()->GetLocale());
} }
return InternalVarToUser(var); return InternalVarToUser(var);
@ -770,19 +771,19 @@ QString VTranslateVars::VarFromUser(const QString &var) const
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QString VTranslateVars::PMSystemName(const QString &code) 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 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 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))) if (measurements.contains(tValues.at(i)))
{ {
newFormula.replace(tKeys.at(i), tValues.at(i).length(), measurements.value(tValues.at(i)).translate()); newFormula.replace(tKeys.at(i), tValues.at(i).length(),
int bias = tValues.at(i).length() - measurements.value(tValues.at(i)).translate().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) if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected. {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); 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))) if (functions.contains(tValues.at(i)))
{ {
newFormula.replace(tKeys.at(i), tValues.at(i).length(), functions.value(tValues.at(i)).translate()); newFormula.replace(tKeys.at(i), tValues.at(i).length(),
int bias = tValues.at(i).length() - functions.value(tValues.at(i)).translate().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) if (bias != 0)
{// Translated token has different length than original. Position next tokens need to be corrected. {// Translated token has different length than original. Position next tokens need to be corrected.
CorrectionsPositions(tKeys.at(i), bias, tokens, numbers); CorrectionsPositions(tKeys.at(i), bias, tokens, numbers);

View File

@ -574,7 +574,7 @@ void DialogEditWrongFormula::ShowFunctions()
while (i != qApp->TrVars()->GetFunctions().constEnd()) while (i != qApp->TrVars()->GetFunctions().constEnd())
{ {
ui->tableWidget->setRowCount(ui->tableWidget->rowCount() + 1); 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)); item->setFont(QFont("Times", 12, QFont::Bold));
ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, ColumnName, item); ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, ColumnName, item);
item->setToolTip(i.value().getMdisambiguation()); item->setToolTip(i.value().getMdisambiguation());