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)
VERSION = 2.5.0
VERSION = 2.6.0
# Allow MAC OS X to find library inside a bundle
macx:QMAKE_SONAME_PREFIX = @rpath

View file

@ -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"

View file

@ -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());
}
//---------------------------------------------------------------------------

View file

@ -23,6 +23,7 @@
#include <QByteArray>
#include <QCoreApplication>
#include <QLocale>
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

View file

@ -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;
};
//---------------------------------------------------------------------------------------------------------------------

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
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;
}
//---------------------------------------------------------------------------------------------------------------------

View file

@ -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<QString, qmu::QmuTranslation>::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
{

View file

@ -490,7 +490,7 @@ void VTranslateVars::PrepareFunctionTranslations()
QMap<QString, qmu::QmuTranslation>::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<int, QString> &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<QString, qmu::QmuTranslation>::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<QString, qmu::QmuTranslation>::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);

View file

@ -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());