2016-07-18 22:30:14 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
|
|
|
|
#include "vtextmanager.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
TextLine::TextLine()
|
|
|
|
:m_qsText(), m_iFontSize(MIN_FONT_SIZE), m_eFontWeight(QFont::Normal), m_eStyle(QFont::StyleNormal),
|
|
|
|
m_eAlign(Qt::AlignCenter), m_iHeight(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VTextManager::VTextManager()
|
2016-07-19 19:02:20 +02:00
|
|
|
:m_font(), m_liLines(), m_liOutput()
|
2016-07-18 22:30:14 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VTextManager::~VTextManager()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VTextManager::GetSpacing() const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::SetFont(const QFont& font)
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
m_font = font;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
const QFont& VTextManager::GetFont() const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return m_font;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::SetFontSize(int iFS)
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
m_font.setPixelSize(iFS);
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::AddLine(const TextLine& tl)
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
m_liLines << tl;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::Clear()
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
m_liLines.clear();
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VTextManager::GetCount() const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return m_liOutput.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VTextManager::GetSourceLineCount() const
|
|
|
|
{
|
|
|
|
return m_liLines.count();
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
const TextLine& VTextManager::GetLine(int i) const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return m_liOutput[i];
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize)
|
|
|
|
{
|
|
|
|
m_liOutput.clear();
|
|
|
|
QFont fnt = m_font;
|
|
|
|
int iY = 0;
|
|
|
|
for (int i = 0; i < m_liLines.count(); ++i)
|
|
|
|
{
|
|
|
|
const TextLine& tl = m_liLines.at(i);
|
|
|
|
TextLine tlOut = tl;
|
|
|
|
fnt.setPixelSize(iFontSize + tl.m_iFontSize);
|
|
|
|
QFontMetrics fm(fnt);
|
|
|
|
tlOut.m_iHeight = fm.height();
|
|
|
|
QStringList qslLines = SplitString(tlOut.m_qsText, fW, fm);
|
|
|
|
for (int iL = 0; iL < qslLines.count(); ++iL)
|
|
|
|
{
|
|
|
|
tlOut.m_qsText = qslLines[iL];
|
|
|
|
m_liOutput << tlOut;
|
|
|
|
iY += tlOut.m_iHeight + GetSpacing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iY < fH;
|
|
|
|
}
|
|
|
|
|
2016-07-19 13:47:21 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::FitFontSize(qreal fW, qreal fH)
|
|
|
|
{
|
|
|
|
int iFontSize = GetFont().pixelSize();
|
|
|
|
while (IsBigEnough(fW, fH, iFontSize) == true && iFontSize <= MAX_FONT_SIZE)
|
|
|
|
{
|
|
|
|
++iFontSize;
|
|
|
|
}
|
|
|
|
while (IsBigEnough(fW, fH, iFontSize) == false && iFontSize >= MIN_FONT_SIZE)
|
|
|
|
{
|
|
|
|
--iFontSize;
|
|
|
|
}
|
|
|
|
SetFontSize(iFontSize);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:46:49 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::Update(const QString& qsName, const VPatternPieceData& data)
|
|
|
|
{
|
|
|
|
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.m_iFontSize = 6;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// name
|
|
|
|
tl.m_qsText = qsName;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::DemiBold;
|
|
|
|
tl.m_iFontSize = 2;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// MCP
|
|
|
|
QString qsText = "Cut %1 on %2%3";
|
|
|
|
QStringList qslPlace;
|
|
|
|
qslPlace << "" << " on Fold";
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
for (int i = 0; i < data.GetMCPCount(); ++i)
|
|
|
|
{
|
|
|
|
MaterialCutPlacement mcp = data.GetMCP(i);
|
|
|
|
if (mcp.m_iCutNumber > 0)
|
|
|
|
{
|
|
|
|
tl.m_qsText = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).
|
|
|
|
arg(qslPlace[int(mcp.m_ePlacement)]);
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextManager::Update(const QString &qsPattern, const QString &qsNumber, const QString &qsSize,
|
|
|
|
const QString &qsCompany, const QString &qsCustomer, const QDate& date)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
TextLine tl;
|
|
|
|
// all information must be centered
|
|
|
|
tl.m_eAlign = Qt::AlignCenter;
|
|
|
|
|
|
|
|
// Company name
|
|
|
|
tl.m_qsText = qsCompany;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::DemiBold;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 4;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Pattern name
|
|
|
|
tl.m_qsText = qsPattern;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 2;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Pattern number
|
|
|
|
tl.m_qsText = qsNumber;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Customer name
|
|
|
|
tl.m_qsText = qsCustomer;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleItalic;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Size
|
|
|
|
tl.m_qsText = qsSize;
|
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Date
|
|
|
|
if (date.isValid() == true)
|
|
|
|
{
|
|
|
|
tl.m_qsText = date.toString("dd MMMM yyyy");
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 22:30:14 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QStringList VTextManager::SplitString(const QString &qs, qreal fW, const QFontMetrics &fm)
|
|
|
|
{
|
|
|
|
QRegularExpression reg("\\s+");
|
|
|
|
QStringList qslWords = qs.split(reg);
|
|
|
|
QStringList qslLines;
|
|
|
|
QString qsCurrent;
|
|
|
|
for (int i = 0; i < qslWords.count(); ++i)
|
|
|
|
{
|
|
|
|
if (qsCurrent.length() > 0)
|
|
|
|
{
|
|
|
|
qsCurrent += QLatin1Literal(" ");
|
|
|
|
}
|
|
|
|
if (fm.width(qsCurrent + qslWords[i]) > fW)
|
|
|
|
{
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
qsCurrent = qslWords[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qsCurrent += qslWords[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
return qslLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|