2016-07-18 22:30:14 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QFontMetrics>
|
2016-07-24 19:07:38 +02:00
|
|
|
#include <QDir>
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
#include "vtextmanager.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief TextLine::TextLine default constructor
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
TextLine::TextLine()
|
|
|
|
:m_qsText(), m_iFontSize(MIN_FONT_SIZE), m_eFontWeight(QFont::Normal), m_eStyle(QFont::StyleNormal),
|
|
|
|
m_eAlign(Qt::AlignCenter), m_iHeight(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::VTextManager constructor
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
VTextManager::VTextManager()
|
2016-08-02 14:12:13 +02:00
|
|
|
:m_font(), m_liLines(), m_liOutput()
|
2016-07-18 22:30:14 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::~VTextManager destructor
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
VTextManager::~VTextManager()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::GetSpacing returns the vertical spacing between the lines
|
|
|
|
* @return
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
int VTextManager::GetSpacing() const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::SetFont set the text base font
|
|
|
|
* @param font text base font
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::GetFont returns the text base font
|
|
|
|
* @return text base 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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::SetFontSize sets the font size
|
|
|
|
* @param iFS font size in pixels
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::AddLine add new text line to the list
|
|
|
|
* @param tl text line object to be added
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::Clear deletes the list of texts
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::GetCount returns the number of output text lines
|
|
|
|
* @return number of output text lines
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
int VTextManager::GetCount() const
|
2016-07-19 02:26:50 +02:00
|
|
|
{
|
|
|
|
return m_liOutput.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::GetSourceLineCount returns the number of input text lines
|
|
|
|
* @return number of text lines that were added to the list by calling AddLine
|
|
|
|
*/
|
2016-07-19 02:26:50 +02:00
|
|
|
int VTextManager::GetSourceLineCount() const
|
|
|
|
{
|
|
|
|
return m_liLines.count();
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::GetLine returns the i-th output text line
|
|
|
|
* @param i index of the output text line
|
|
|
|
* @return i-th output text line
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::IsBigEnough Checks if rectangle of size (fW, fH) is big enough to hold the text with base font
|
|
|
|
* size iFontSize
|
|
|
|
* @param fW rectangle width
|
|
|
|
* @param fH rectangle height
|
|
|
|
* @param iFontSize base font size
|
|
|
|
* @return true, if rectangle of size (fW, fH)
|
|
|
|
*/
|
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);
|
2016-07-29 21:59:48 +02:00
|
|
|
int iHorSp = fm.width(" ");
|
2016-07-18 22:30:14 +02:00
|
|
|
tlOut.m_iHeight = fm.height();
|
|
|
|
QStringList qslLines = SplitString(tlOut.m_qsText, fW, fm);
|
|
|
|
for (int iL = 0; iL < qslLines.count(); ++iL)
|
|
|
|
{
|
2016-07-24 12:30:10 +02:00
|
|
|
// check if every line fits within the label width
|
2016-07-29 21:59:48 +02:00
|
|
|
if (fm.width(qslLines[iL]) + iHorSp > fW)
|
2016-07-24 12:30:10 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-18 22:30:14 +02:00
|
|
|
tlOut.m_qsText = qslLines[iL];
|
|
|
|
m_liOutput << tlOut;
|
|
|
|
iY += tlOut.m_iHeight + GetSpacing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iY < fH;
|
|
|
|
}
|
|
|
|
|
2016-07-19 13:47:21 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::FitFontSize sets the font size just big enough, so that the text fits into rectangle of
|
|
|
|
* size (fW, fH)
|
|
|
|
* @param fW rectangle width
|
|
|
|
* @param fH rectangle height
|
|
|
|
*/
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::Update updates the text lines with detail data
|
|
|
|
* @param qsName detail name
|
|
|
|
* @param data reference to the detail data
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::Update updates the text lines with pattern info
|
|
|
|
* @param pDoc pointer to the abstract pattern object
|
|
|
|
*/
|
2016-07-22 11:56:45 +02:00
|
|
|
void VTextManager::Update(const VAbstractPattern *pDoc)
|
2016-07-21 21:46:49 +02:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
TextLine tl;
|
|
|
|
// all information must be centered
|
|
|
|
tl.m_eAlign = Qt::AlignCenter;
|
|
|
|
|
|
|
|
// Company name
|
2016-07-22 11:56:45 +02:00
|
|
|
tl.m_qsText = pDoc->GetCompanyName();
|
2016-07-21 21:46:49 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::DemiBold;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 4;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Pattern name
|
2016-07-22 11:56:45 +02:00
|
|
|
tl.m_qsText = pDoc->GetPatternName();
|
2016-07-21 21:46:49 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 2;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Pattern number
|
2016-07-22 11:56:45 +02:00
|
|
|
tl.m_qsText = pDoc->GetPatternNumber();
|
2016-07-21 21:46:49 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Customer name
|
2016-07-22 11:56:45 +02:00
|
|
|
tl.m_qsText = pDoc->GetCustomerName();
|
2016-07-21 21:46:49 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleItalic;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
|
|
|
// Size
|
2016-07-22 11:56:45 +02:00
|
|
|
tl.m_qsText = pDoc->GetPatternSize();
|
2016-07-21 21:46:49 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
2016-07-24 12:30:10 +02:00
|
|
|
// Measurements
|
2016-07-25 19:10:13 +02:00
|
|
|
tl.m_qsText = QFileInfo(pDoc->MPath()).fileName();
|
2016-07-24 12:30:10 +02:00
|
|
|
if (tl.m_qsText.isEmpty() == false && pDoc->IsMeasurementsVisible() == true)
|
|
|
|
{
|
|
|
|
tl.m_eFontWeight = QFont::Normal;
|
|
|
|
tl.m_eStyle = QFont::StyleNormal;
|
|
|
|
tl.m_iFontSize = 0;
|
|
|
|
AddLine(tl);
|
|
|
|
}
|
2016-07-21 21:46:49 +02:00
|
|
|
// Date
|
2016-07-22 11:56:45 +02:00
|
|
|
QDate date;
|
|
|
|
if (pDoc->IsDateVisible() == true)
|
|
|
|
{
|
|
|
|
date = QDate::currentDate();
|
|
|
|
}
|
2016-07-21 21:46:49 +02:00
|
|
|
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-22 11:56:45 +02:00
|
|
|
|
2016-07-21 21:46:49 +02:00
|
|
|
}
|
|
|
|
|
2016-07-18 22:30:14 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextManager::SplitString splits the string into several lines, which all fit into width fW
|
|
|
|
* @param qs string to split
|
|
|
|
* @param fW required width of every output string
|
|
|
|
* @param fm font metrics of the font used
|
|
|
|
* @return list of strings, each of which is not wider than fW using the font metrics fm
|
|
|
|
*/
|
2016-07-18 22:30:14 +02:00
|
|
|
QStringList VTextManager::SplitString(const QString &qs, qreal fW, const QFontMetrics &fm)
|
|
|
|
{
|
|
|
|
QRegularExpression reg("\\s+");
|
2016-07-23 15:26:15 +02:00
|
|
|
// split the string into words
|
2016-07-18 22:30:14 +02:00
|
|
|
QStringList qslWords = qs.split(reg);
|
|
|
|
QStringList qslLines;
|
|
|
|
QString qsCurrent;
|
|
|
|
for (int i = 0; i < qslWords.count(); ++i)
|
|
|
|
{
|
|
|
|
if (qsCurrent.length() > 0)
|
|
|
|
{
|
2016-08-02 14:12:13 +02:00
|
|
|
qsCurrent += QLatin1String(" ");
|
2016-07-18 22:30:14 +02:00
|
|
|
}
|
2016-07-23 15:26:15 +02:00
|
|
|
// check if another word can be added into current line
|
2016-07-18 22:30:14 +02:00
|
|
|
if (fm.width(qsCurrent + qslWords[i]) > fW)
|
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// if not, add the current line into the list of text lines
|
2016-07-24 12:30:10 +02:00
|
|
|
if (qsCurrent.isEmpty() == false)
|
|
|
|
{
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
}
|
2016-07-23 15:26:15 +02:00
|
|
|
// and set the current line to contain the current word
|
2016-07-18 22:30:14 +02:00
|
|
|
qsCurrent = qslWords[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qsCurrent += qslWords[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
return qslLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|