Refactoring.

Reduce complexity of the function.
This commit is contained in:
Roman Telezhynskyi 2023-10-10 11:25:13 +03:00
parent 6d471c5a55
commit 11b5efc514

View File

@ -286,39 +286,24 @@ QT_WARNING_POP
const qint64 placeholdersExpirationTimeout = 15; // seconds
//---------------------------------------------------------------------------------------------------------------------
auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bool pieceLabel = false,
const QString &pieceAreaShortName = QString()) -> QMap<QString, QString>
void PrepareMeasurementsPlaceholders(const VContainer *data, QMap<QString, QString> &placeholders)
{
SCASSERT(doc != nullptr)
SCASSERT(data != nullptr)
if (placeholdersExpirationTime->isValid() && QDateTime::currentDateTime() <= *placeholdersExpirationTime)
const QMap<QString, QSharedPointer<VMeasurement>> measurements = data->DataMeasurements();
auto i = measurements.constBegin();
while (i != measurements.constEnd())
{
*placeholdersExpirationTime = QDateTime::currentDateTime().addSecs(placeholdersExpirationTimeout);
return *placeholdersCache;
placeholders.insert(pl_measurement + i.key(), QString::number(*i.value()->GetValue()));
++i;
}
}
QMap<QString, QString> placeholders;
// Pattern tags
//---------------------------------------------------------------------------------------------------------------------
void PrepareCustomerPlaceholders(const VAbstractPattern *doc, QMap<QString, QString> &placeholders)
{
QLocale locale(VAbstractApplication::VApp()->Settings()->GetLocale());
const QString date = locale.toString(QDate::currentDate(), doc->GetLabelDateFormat());
placeholders.insert(pl_date, date);
const QString time = locale.toString(QTime::currentTime(), doc->GetLabelTimeFormat());
placeholders.insert(pl_time, time);
placeholders.insert(pl_patternName, doc->GetPatternName());
placeholders.insert(pl_patternNumber, doc->GetPatternNumber());
placeholders.insert(pl_author, doc->GetCompanyName());
placeholders.insert(pl_mUnits, UnitsToStr(VAbstractValApplication::VApp()->MeasurementsUnits(), true));
const QString pUnits = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true);
placeholders.insert(pl_pUnits, pUnits);
placeholders.insert(pl_mSizeUnits, UnitsToStr(VAbstractValApplication::VApp()->DimensionSizeUnits(), true));
placeholders.insert(pl_areaUnits, pUnits + QStringLiteral("²"));
if (VAbstractValApplication::VApp()->GetMeasurementsType() == MeasurementsType::Individual)
{
placeholders.insert(pl_customer, VAbstractValApplication::VApp()->GetCustomerName());
@ -338,11 +323,11 @@ auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bo
placeholders.insert(pl_email, doc->GetCustomerEmail());
}
}
placeholders.insert(pl_pExt, QStringLiteral("val"));
placeholders.insert(pl_pFileName, QFileInfo(VAbstractValApplication::VApp()->GetPatternPath()).baseName());
placeholders.insert(pl_mFileName, QFileInfo(doc->MPath()).baseName());
//---------------------------------------------------------------------------------------------------------------------
void PrepareDimensionPlaceholders(QMap<QString, QString> &placeholders)
{
QString heightValue = QString::number(VAbstractValApplication::VApp()->GetDimensionHeight());
placeholders.insert(pl_height, heightValue);
placeholders.insert(pl_dimensionX, heightValue);
@ -372,11 +357,11 @@ auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bo
label = VAbstractValApplication::VApp()->GetDimensionWaistLabel();
placeholders.insert(pl_waistLabel, not label.isEmpty() ? label : waistValue);
}
}
placeholders.insert(pl_mExt, VAbstractValApplication::VApp()->GetMeasurementsType() == MeasurementsType::Multisize
? QStringLiteral("vst")
: QStringLiteral("vit"));
//---------------------------------------------------------------------------------------------------------------------
void PrepareUserMaterialsPlaceholders(const VAbstractPattern *doc, QMap<QString, QString> &placeholders)
{
const QMap<int, QString> materials = doc->GetPatternMaterials();
for (int i = 0; i < userMaterialPlaceholdersQuantity; ++i)
{
@ -390,18 +375,12 @@ auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bo
placeholders.insert(pl_userMaterial + number, value);
}
}
{
const QMap<QString, QSharedPointer<VMeasurement>> measurements = data->DataMeasurements();
auto i = measurements.constBegin();
while (i != measurements.constEnd())
{
placeholders.insert(pl_measurement + i.key(), QString::number(*i.value()->GetValue()));
++i;
}
}
{
//---------------------------------------------------------------------------------------------------------------------
void PrepareFinalMeasurementsPlaceholders(const VAbstractPattern *doc, bool pieceLabel,
const QString &pieceAreaShortName, QMap<QString, QString> &placeholders)
{
VContainer completeData = doc->GetCompleteData();
completeData.FillPiecesAreas(VAbstractValApplication::VApp()->patternUnits());
@ -417,8 +396,7 @@ auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bo
}
catch (qmu::QmuParserError &e)
{
const QString errorMsg =
QObject::tr("Failed to prepare full piece area placeholder. %1.").arg(e.GetMsg());
const QString errorMsg = QObject::tr("Failed to prepare full piece area placeholder. %1.").arg(e.GetMsg());
VAbstractApplication::VApp()->IsPedantic()
? throw VException(errorMsg)
: qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg;
@ -468,8 +446,58 @@ auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bo
: qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg;
}
}
}
//---------------------------------------------------------------------------------------------------------------------
auto PreparePlaceholders(const VAbstractPattern *doc, const VContainer *data, bool pieceLabel = false,
const QString &pieceAreaShortName = QString()) -> QMap<QString, QString>
{
SCASSERT(doc != nullptr)
SCASSERT(data != nullptr)
if (placeholdersExpirationTime->isValid() && QDateTime::currentDateTime() <= *placeholdersExpirationTime)
{
*placeholdersExpirationTime = QDateTime::currentDateTime().addSecs(placeholdersExpirationTimeout);
return *placeholdersCache;
}
QMap<QString, QString> placeholders;
// Pattern tags
QLocale locale(VAbstractApplication::VApp()->Settings()->GetLocale());
const QString date = locale.toString(QDate::currentDate(), doc->GetLabelDateFormat());
placeholders.insert(pl_date, date);
const QString time = locale.toString(QTime::currentTime(), doc->GetLabelTimeFormat());
placeholders.insert(pl_time, time);
placeholders.insert(pl_patternName, doc->GetPatternName());
placeholders.insert(pl_patternNumber, doc->GetPatternNumber());
placeholders.insert(pl_author, doc->GetCompanyName());
placeholders.insert(pl_mUnits, UnitsToStr(VAbstractValApplication::VApp()->MeasurementsUnits(), true));
const QString pUnits = UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true);
placeholders.insert(pl_pUnits, pUnits);
placeholders.insert(pl_mSizeUnits, UnitsToStr(VAbstractValApplication::VApp()->DimensionSizeUnits(), true));
placeholders.insert(pl_areaUnits, pUnits + QStringLiteral("²"));
PrepareCustomerPlaceholders(doc, placeholders);
placeholders.insert(pl_pExt, QStringLiteral("val"));
placeholders.insert(pl_pFileName, QFileInfo(VAbstractValApplication::VApp()->GetPatternPath()).baseName());
placeholders.insert(pl_mFileName, QFileInfo(doc->MPath()).baseName());
PrepareDimensionPlaceholders(placeholders);
placeholders.insert(pl_mExt, VAbstractValApplication::VApp()->GetMeasurementsType() == MeasurementsType::Multisize
? QStringLiteral("vst")
: QStringLiteral("vit"));
PrepareUserMaterialsPlaceholders(doc, placeholders);
PrepareMeasurementsPlaceholders(data, placeholders);
PrepareFinalMeasurementsPlaceholders(doc, pieceLabel, pieceAreaShortName, placeholders);
// Piece tags
placeholders.insert(pl_pLetter, QString());
placeholders.insert(pl_pAnnotation, QString());
@ -828,8 +856,8 @@ void VTextManager::Update(const QString &qsName, const VPieceLabelData &data, co
{
m_liLines.clear();
QMap<QString, QString> placeholders =
PreparePlaceholders(VAbstractValApplication::VApp()->getCurrentDocument(), pattern, true, data.GetAreaShortName());
QMap<QString, QString> placeholders = PreparePlaceholders(VAbstractValApplication::VApp()->getCurrentDocument(),
pattern, true, data.GetAreaShortName());
InitPiecePlaceholders(placeholders, qsName, data);
QVector<VLabelTemplateLine> lines = data.GetLabelTemplate();