From b212d04d1175a5401320876267d915592ca6cfb2 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 15 Aug 2017 15:22:33 +0300 Subject: [PATCH] Parse template and prepare pattern label. --HG-- branch : feature --- src/libs/ifc/xml/vabstractpattern.cpp | 14 ++ src/libs/ifc/xml/vabstractpattern.h | 4 + src/libs/vlayout/vlayoutpiece.cpp | 11 +- src/libs/vlayout/vlayoutpiece.h | 2 +- src/libs/vlayout/vtextmanager.cpp | 232 +++++++++---------- src/libs/vlayout/vtextmanager.h | 12 +- src/libs/vtools/tools/vtoolseamallowance.cpp | 2 +- src/libs/vwidgets/vtextgraphicsitem.cpp | 27 +-- src/libs/vwidgets/vtextgraphicsitem.h | 4 +- 9 files changed, 149 insertions(+), 159 deletions(-) diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index a987db4eb..d2d9480cf 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -195,6 +195,7 @@ const QString VAbstractPattern::NodeSplinePath = QStringLiteral("NodeSplinePath" QHash VAbstractPattern::tools = QHash(); QVector VAbstractPattern::patternLabelLines = QVector(); +bool VAbstractPattern::patternLabelWasChanged = false; namespace { @@ -1383,6 +1384,7 @@ void VAbstractPattern::SetPatternLabelTemplate(const QVector RemoveAllChildren(tag); SetLabelTemplate(tag, lines); patternLabelLines = lines; + patternLabelWasChanged = true; } //--------------------------------------------------------------------------------------------------------------------- @@ -1402,6 +1404,18 @@ QVector VAbstractPattern::GetPatternLabelTemplate() const return patternLabelLines; } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPattern::SetPatternWasChanged(bool changed) +{ + patternLabelWasChanged = changed; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractPattern::GetPatternWasChanged() const +{ + return patternLabelWasChanged; +} + //--------------------------------------------------------------------------------------------------------------------- QString VAbstractPattern::GetImage() const { diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index 33a4e9647..65cbf2da9 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -157,6 +157,9 @@ public: void SetPatternLabelTemplate(const QVector &lines); QVector GetPatternLabelTemplate() const; + void SetPatternWasChanged(bool changed); + bool GetPatternWasChanged() const; + QString GetImage() const; QString GetImageExtension() const; void SetImage(const QString &text, const QString &extension); @@ -389,6 +392,7 @@ protected: static QHash tools; /** @brief patternLabelLines list to speed up reading a template by many pieces. */ static QVector patternLabelLines; + static bool patternLabelWasChanged; static void ToolExists(const quint32 &id); static VPiecePath ParsePathNodes(const QDomElement &domElement); diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index 65b6c713f..2655f9068 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -400,8 +400,7 @@ VLayoutPiece VLayoutPiece::Create(const VPiece &piece, const VContainer *pattern if (geom.IsVisible() == true) { VAbstractPattern* pDoc = qApp->getCurrentDocument(); - det.SetPatternInfo(pDoc, geom, qApp->Settings()->GetLabelFont(), VContainer::size(), VContainer::height(), - pattern); + det.SetPatternInfo(pDoc, geom, qApp->Settings()->GetLabelFont(), pattern); } const VGrainlineData& grainlineGeom = piece.GetGrainlineGeometry(); @@ -545,7 +544,7 @@ QStringList VLayoutPiece::GetPatternText() const //--------------------------------------------------------------------------------------------------------------------- void VLayoutPiece::SetPatternInfo(const VAbstractPattern* pDoc, const VPatternLabelData& geom, const QFont &font, - qreal dSize, qreal dHeight, const VContainer *pattern) + const VContainer *pattern) { QPointF ptPos; qreal labelWidth = 0; @@ -578,7 +577,7 @@ void VLayoutPiece::SetPatternInfo(const VAbstractPattern* pDoc, const VPatternLa d->m_tmPattern.SetFont(font); d->m_tmPattern.SetFontSize(geom.GetFontSize()); - d->m_tmPattern.Update(pDoc, dSize, dHeight); + d->m_tmPattern.Update(pDoc); // generate lines of text d->m_tmPattern.SetFontSize(geom.GetFontSize()); @@ -1026,8 +1025,8 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector GetGrainline() const; diff --git a/src/libs/vlayout/vtextmanager.cpp b/src/libs/vlayout/vtextmanager.cpp index a6e219e8b..accd56963 100644 --- a/src/libs/vlayout/vtextmanager.cpp +++ b/src/libs/vlayout/vtextmanager.cpp @@ -38,6 +38,7 @@ #include "../vpatterndb/floatItemData/vpiecelabeldata.h" #include "../vmisc/vabstractapplication.h" #include "../vmisc/vmath.h" +#include "../vpatterndb/vcontainer.h" #include "vtextmanager.h" //--------------------------------------------------------------------------------------------------------------------- @@ -47,11 +48,92 @@ TextLine::TextLine() : m_qsText(), m_iFontSize(MIN_FONT_SIZE), - m_eFontWeight(QFont::Normal), - m_eStyle(QFont::StyleNormal), + bold(false), + italic(false), m_eAlign(Qt::AlignCenter) {} +QList VTextManager::m_patternLabelLines = QList(); + +namespace +{ + +//--------------------------------------------------------------------------------------------------------------------- +QMap PreparePlaceholders(const VAbstractPattern *doc) +{ + SCASSERT(doc != nullptr) + + QMap placeholders; + + QLocale locale(qApp->Settings()->GetLocale()); + placeholders.insert(pl_date, locale.toString(QDate::currentDate())); + placeholders.insert(pl_time, locale.toString(QTime::currentTime())); + placeholders.insert(pl_patternName, doc->GetPatternName()); + placeholders.insert(pl_patternNumber, doc->GetPatternNumber()); + placeholders.insert(pl_author, doc->GetCompanyName()); + placeholders.insert(pl_customer, doc->GetCustomerName()); + placeholders.insert(pl_pExt, QString("val")); + placeholders.insert(pl_pFileName, QFileInfo(qApp->GetPPath()).fileName()); + placeholders.insert(pl_mFileName, QFileInfo(doc->MPath()).fileName()); + + QString curSize; + QString curHeight; + QString mExt; + if (qApp->patternType() == MeasurementsType::Multisize) + { + curSize = QString::number(VContainer::size()); + curHeight = QString::number(VContainer::height()); + mExt = "vst"; + } + else if (qApp->patternType() == MeasurementsType::Individual) + { + mExt = "vit"; + } + + placeholders.insert(pl_size, curSize); + placeholders.insert(pl_height, curHeight); + placeholders.insert(pl_mExt, mExt); + + return placeholders; +} + +//--------------------------------------------------------------------------------------------------------------------- +QString ReplacePlaceholders(const QMap &placeholders, QString line) +{ + QChar per('%'); + auto i = placeholders.constBegin(); + while (i != placeholders.constEnd()) + { + line.replace(per+i.key()+per, i.value()); + ++i; + } + return line; +} + +//--------------------------------------------------------------------------------------------------------------------- +QList PrepareLines(const QVector &lines) +{ + QList textLines; + + for (int i=0; i < lines.size(); ++i) + { + if (not lines.at(i).line.isEmpty()) + { + TextLine tl; + tl.m_qsText = lines.at(i).line; + tl.m_eAlign = static_cast(lines.at(i).alignment); + tl.m_iFontSize = 0; + tl.bold = lines.at(i).bold; + tl.italic = lines.at(i).italic; + + textLines << tl; + } + } + + return textLines; +} +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VTextManager::VTextManager constructor @@ -60,13 +142,6 @@ VTextManager::VTextManager() : m_font(), m_liLines() {} -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief VTextManager::~VTextManager destructor - */ -VTextManager::~VTextManager() -{} - //--------------------------------------------------------------------------------------------------------------------- VTextManager::VTextManager(const VTextManager &text) : m_font(text.GetFont()), m_liLines(text.GetAllSourceLines()) @@ -130,25 +205,6 @@ QList VTextManager::GetAllSourceLines() const return m_liLines; } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief AddSourceLine add new text line to the list - * @param tl text line object to be added - */ -void VTextManager::AddSourceLine(const TextLine& tl) -{ - m_liLines << tl; -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief ClearSourceLines deletes the list of texts - */ -void VTextManager::ClearSourceLines() -{ - m_liLines.clear(); -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief VTextManager::GetSourceLinesCount returns the number of input text lines @@ -202,8 +258,8 @@ void VTextManager::FitFontSize(qreal fW, qreal fH) const TextLine& tl = GetSourceLine(i); fnt = m_font; fnt.setPixelSize(iFS + tl.m_iFontSize); - fnt.setWeight(tl.m_eFontWeight); - fnt.setStyle(tl.m_eStyle); + fnt.setBold(tl.bold); + fnt.setItalic(tl.italic); QFontMetrics fm(fnt); const int iTW = fm.width(tl.m_qsText); if (iTW > iMaxLen) @@ -215,8 +271,8 @@ void VTextManager::FitFontSize(qreal fW, qreal fH) if (iMaxLen > fW) { QFont fnt = m_font; - fnt.setWeight(maxLine.m_eFontWeight); - fnt.setStyle(maxLine.m_eStyle); + fnt.setBold(maxLine.bold); + fnt.setItalic(maxLine.italic); int lineLength = 0; do @@ -240,27 +296,27 @@ void VTextManager::FitFontSize(qreal fW, qreal fH) */ void VTextManager::Update(const QString& qsName, const VPieceLabelData& data) { - ClearSourceLines(); + m_liLines.clear(); + TextLine tl; // all text must be centered and normal style! tl.m_eAlign = Qt::AlignCenter; - tl.m_eStyle = QFont::StyleNormal; // letter tl.m_qsText = data.GetLetter(); if (tl.m_qsText.isEmpty() == false) { - tl.m_eFontWeight = QFont::Bold; + tl.bold = true; tl.m_iFontSize = 6; - AddSourceLine(tl); + m_liLines << tl; } // name tl.m_qsText = qsName; if (tl.m_qsText.isEmpty() == false) { - tl.m_eFontWeight = QFont::DemiBold; + tl.bold = true; tl.m_iFontSize = 2; - AddSourceLine(tl); + m_liLines << tl; } // MCP QStringList qslMaterials; @@ -271,7 +327,7 @@ void VTextManager::Update(const QString& qsName, const VPieceLabelData& data) QString qsText = QLatin1String("%1, ") + tr("cut") + QLatin1String(" %2%3"); QStringList qslPlace; qslPlace << "" << QLatin1String(" ") + tr("on fold"); - tl.m_eFontWeight = QFont::Normal; + tl.bold = false; tl.m_iFontSize = 0; for (int i = 0; i < data.GetMCPCount(); ++i) { @@ -289,7 +345,7 @@ void VTextManager::Update(const QString& qsName, const VPieceLabelData& data) } tl.m_qsText = qsText.arg(qsMat).arg(mcp.m_iCutNumber). arg(qslPlace[int(mcp.m_ePlacement)]); - AddSourceLine(tl); + m_liLines << tl; } } } @@ -299,89 +355,27 @@ void VTextManager::Update(const QString& qsName, const VPieceLabelData& data) * @brief VTextManager::Update updates the text lines with pattern info * @param pDoc pointer to the abstract pattern object */ -void VTextManager::Update(const VAbstractPattern *pDoc, qreal dSize, qreal dHeight) +void VTextManager::Update(const VAbstractPattern *pDoc) { - ClearSourceLines(); - TextLine tl; - // all information must be centered - tl.m_eAlign = Qt::AlignCenter; + m_liLines.clear(); - // Company name - tl.m_qsText = pDoc->GetCompanyName(); - if (tl.m_qsText.isEmpty() == false) + if (m_patternLabelLines.isEmpty() || pDoc->GetPatternWasChanged()) { - tl.m_eFontWeight = QFont::DemiBold; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 4; - AddSourceLine(tl); - } - // Pattern name - tl.m_qsText = pDoc->GetPatternName(); - if (tl.m_qsText.isEmpty() == false) - { - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 2; - AddSourceLine(tl); - } - // Pattern number - tl.m_qsText = pDoc->GetPatternNumber(); - if (tl.m_qsText.isEmpty() == false) - { - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 0; - AddSourceLine(tl); - } - // Customer name - tl.m_qsText = pDoc->GetCustomerName(); - if (tl.m_qsText.isEmpty() == false) - { - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleItalic; - tl.m_iFontSize = 0; - AddSourceLine(tl); - } - // Size - tl.m_qsText = pDoc->GetPatternSize(); - if (tl.m_qsText.isEmpty() == false) - { - // Such describing placeholders will help avoid mistake of localization. - // Translators very often remove '%'. - QString placeholder = QLatin1String("%") + qApp->TrVars()->PlaceholderToUser(pl_size) + QLatin1String("%"); - tl.m_qsText.replace(placeholder, QString::number(dSize)); + QVector lines = pDoc->GetPatternLabelTemplate(); + if (lines.isEmpty() && m_patternLabelLines.isEmpty()) + { + return; // Nothing to parse + } - placeholder = QLatin1String("%") + qApp->TrVars()->PlaceholderToUser(pl_height) + QLatin1String("%"); - tl.m_qsText.replace(placeholder, QString::number(dHeight)); + const QMap placeholders = PreparePlaceholders(pDoc); - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 0; - AddSourceLine(tl); - } - // Measurements - tl.m_qsText = QFileInfo(pDoc->MPath()).fileName(); - if (tl.m_qsText.isEmpty() == false && pDoc->IsMeasurementsVisible() == true) - { - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 0; - AddSourceLine(tl); - } - // Date - QDate date; - if (pDoc->IsDateVisible() == true) - { - date = QDate::currentDate(); - } - if (date.isValid() == true) - { - QLocale locale(qApp->Settings()->GetLocale()); - tl.m_qsText = locale.toString(date, "dd MMMM yyyy"); - tl.m_eFontWeight = QFont::Normal; - tl.m_eStyle = QFont::StyleNormal; - tl.m_iFontSize = 0; - AddSourceLine(tl); + for (int i=0; i GetAllSourceLines() const; - void AddSourceLine(const TextLine& tl); - void ClearSourceLines(); int GetSourceLinesCount() const; const TextLine& GetSourceLine(int i) const; void Update(const QString& qsName, const VPieceLabelData& data); - void Update(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight); + void Update(const VAbstractPattern* pDoc); private: QFont m_font; QList m_liLines; + + static QList m_patternLabelLines; }; #endif // VTEXTMANAGER_H diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index 6ea8ba99e..4f753e06e 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -578,7 +578,7 @@ void VToolSeamAllowance::UpdatePatternInfo() if (PrepareLabelData(geom, m_patternInfo, pos, labelAngle)) { - m_patternInfo->UpdateData(doc, VContainer::size(), VContainer::height()); + m_patternInfo->UpdateData(doc); UpdateLabelItem(m_patternInfo, pos, labelAngle); } } diff --git a/src/libs/vwidgets/vtextgraphicsitem.cpp b/src/libs/vwidgets/vtextgraphicsitem.cpp index cd916d53f..9cc1121a5 100644 --- a/src/libs/vwidgets/vtextgraphicsitem.cpp +++ b/src/libs/vwidgets/vtextgraphicsitem.cpp @@ -164,8 +164,8 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem const TextLine& tl = m_tm.GetSourceLine(i); fnt.setPixelSize(m_tm.GetFont().pixelSize() + tl.m_iFontSize); - fnt.setWeight(tl.m_eFontWeight); - fnt.setStyle(tl.m_eStyle); + fnt.setBold(tl.bold); + fnt.setItalic(tl.italic); QString qsText = tl.m_qsText; QFontMetrics fm(fnt); @@ -235,25 +235,6 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem } } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief VTextGraphicsItem::AddLine adds a line of text to the label list. - * @param tl line of text to add - */ -void VTextGraphicsItem::AddLine(const TextLine& tl) -{ - m_tm.AddSourceLine(tl); -} - -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief VTextGraphicsItem::Clear deletes all the label texts - */ -void VTextGraphicsItem::Clear() -{ - m_tm.ClearSourceLines(); -} - //--------------------------------------------------------------------------------------------------------------------- /** * @brief VTextGraphicsItem::SetSize Tries to set the label size to (fW, fH). If any of those is too small, the label @@ -362,9 +343,9 @@ void VTextGraphicsItem::UpdateData(const QString &qsName, const VPieceLabelData * @brief VTextGraphicsItem::UpdateData Updates the pattern label * @param pDoc pointer to the pattern object */ -void VTextGraphicsItem::UpdateData(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight) +void VTextGraphicsItem::UpdateData(const VAbstractPattern* pDoc) { - m_tm.Update(pDoc, dSize, dHeight); + m_tm.Update(pDoc); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vwidgets/vtextgraphicsitem.h b/src/libs/vwidgets/vtextgraphicsitem.h index f59ce5388..be29a12f1 100644 --- a/src/libs/vwidgets/vtextgraphicsitem.h +++ b/src/libs/vwidgets/vtextgraphicsitem.h @@ -63,12 +63,10 @@ public: void SetFont(const QFont& fnt); int GetFontSize() const; - void AddLine(const TextLine& tl); - void Clear(); void SetSize(qreal fW, qreal fH); bool IsContained(QRectF rectBB, qreal dRot, qreal& dX, qreal& dY) const; void UpdateData(const QString& qsName, const VPieceLabelData& data); - void UpdateData(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight); + void UpdateData(const VAbstractPattern* pDoc); int GetTextLines() const; protected: