Parse template and prepare pattern label.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-08-15 15:22:33 +03:00
parent b80bd85dec
commit b212d04d11
9 changed files with 149 additions and 159 deletions

View File

@ -195,6 +195,7 @@ const QString VAbstractPattern::NodeSplinePath = QStringLiteral("NodeSplinePath"
QHash<quint32, VDataTool*> VAbstractPattern::tools = QHash<quint32, VDataTool*>();
QVector<VLabelTemplateLine> VAbstractPattern::patternLabelLines = QVector<VLabelTemplateLine>();
bool VAbstractPattern::patternLabelWasChanged = false;
namespace
{
@ -1383,6 +1384,7 @@ void VAbstractPattern::SetPatternLabelTemplate(const QVector<VLabelTemplateLine>
RemoveAllChildren(tag);
SetLabelTemplate(tag, lines);
patternLabelLines = lines;
patternLabelWasChanged = true;
}
//---------------------------------------------------------------------------------------------------------------------
@ -1402,6 +1404,18 @@ QVector<VLabelTemplateLine> VAbstractPattern::GetPatternLabelTemplate() const
return patternLabelLines;
}
//---------------------------------------------------------------------------------------------------------------------
void VAbstractPattern::SetPatternWasChanged(bool changed)
{
patternLabelWasChanged = changed;
}
//---------------------------------------------------------------------------------------------------------------------
bool VAbstractPattern::GetPatternWasChanged() const
{
return patternLabelWasChanged;
}
//---------------------------------------------------------------------------------------------------------------------
QString VAbstractPattern::GetImage() const
{

View File

@ -157,6 +157,9 @@ public:
void SetPatternLabelTemplate(const QVector<VLabelTemplateLine> &lines);
QVector<VLabelTemplateLine> 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<quint32, VDataTool*> tools;
/** @brief patternLabelLines list to speed up reading a template by many pieces. */
static QVector<VLabelTemplateLine> patternLabelLines;
static bool patternLabelWasChanged;
static void ToolExists(const quint32 &id);
static VPiecePath ParsePathNodes(const QDomElement &domElement);

View File

@ -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<QPoin
const TextLine& tl = tm.GetSourceLine(i);
QFont fnt = tm.GetFont();
fnt.setPixelSize(tm.GetFont().pixelSize() + tl.m_iFontSize);
fnt.setWeight(tl.m_eFontWeight);
fnt.setStyle(tl.m_eStyle);
fnt.setBold(tl.bold);
fnt.setItalic(tl.italic);
QFontMetrics fm(fnt);

View File

@ -96,7 +96,7 @@ public:
QPointF GetPatternTextPosition() const;
QStringList GetPatternText() const;
void SetPatternInfo(const VAbstractPattern* pDoc, const VPatternLabelData& geom, const QFont& font,
qreal dSize, qreal dHeight, const VContainer *pattern);
const VContainer *pattern);
void SetGrainline(const VGrainlineData& geom, const VContainer *pattern);
QVector<QPointF> GetGrainline() const;

View File

@ -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<TextLine> VTextManager::m_patternLabelLines = QList<TextLine>();
namespace
{
//---------------------------------------------------------------------------------------------------------------------
QMap<QString, QString> PreparePlaceholders(const VAbstractPattern *doc)
{
SCASSERT(doc != nullptr)
QMap<QString, QString> 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<QString, QString> &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<TextLine> PrepareLines(const QVector<VLabelTemplateLine> &lines)
{
QList<TextLine> 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<Qt::Alignment>(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<TextLine> 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)
QVector<VLabelTemplateLine> lines = pDoc->GetPatternLabelTemplate();
if (lines.isEmpty() && m_patternLabelLines.isEmpty())
{
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));
placeholder = QLatin1String("%") + qApp->TrVars()->PlaceholderToUser(pl_height) + QLatin1String("%");
tl.m_qsText.replace(placeholder, QString::number(dHeight));
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);
}
return; // Nothing to parse
}
const QMap<QString, QString> placeholders = PreparePlaceholders(pDoc);
for (int i=0; i<lines.size(); ++i)
{
lines[i].line = ReplacePlaceholders(placeholders, lines.at(i).line);
}
m_patternLabelLines = PrepareLines(lines);
}
m_liLines = m_patternLabelLines;
}

View File

@ -51,8 +51,8 @@ struct TextLine
{
QString m_qsText;
int m_iFontSize; // 0 means default
QFont::Weight m_eFontWeight;
QFont::Style m_eStyle;
bool bold;
bool italic;
Qt::Alignment m_eAlign;
TextLine();
@ -67,7 +67,7 @@ class VTextManager
Q_DECLARE_TR_FUNCTIONS(VTextManager)
public:
VTextManager();
virtual ~VTextManager();
virtual ~VTextManager() = default;
VTextManager(const VTextManager &text);
VTextManager &operator=(const VTextManager &text);
@ -80,17 +80,17 @@ public:
void FitFontSize(qreal fW, qreal fH);
QList<TextLine> 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<TextLine> m_liLines;
static QList<TextLine> m_patternLabelLines;
};
#endif // VTEXTMANAGER_H

View File

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

View File

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

View File

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