Hopefully this commit solves the #551 issue

--HG--
branch : feature
This commit is contained in:
BojanKverh 2016-09-29 01:39:50 +02:00
parent 85b8cb2bd7
commit 083b2035af
5 changed files with 60 additions and 17 deletions

View File

@ -159,13 +159,18 @@ const TextLine& VTextManager::GetLine(int i) const
* @param fW rectangle width * @param fW rectangle width
* @param fH rectangle height * @param fH rectangle height
* @param iFontSize base font size * @param iFontSize base font size
* @param fMinW minimal required rectangle width to fit the text
* @param fMinH minimal required rectangle height to fit the text
* @return true, if rectangle of size (fW, fH) * @return true, if rectangle of size (fW, fH)
*/ */
bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize) bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize, qreal& fMinW, qreal& fMinH)
{ {
m_liOutput.clear(); m_liOutput.clear();
QFont fnt = m_font; QFont fnt = m_font;
int iY = 0; int iY = 0;
fMinW = fW;
fMinH = fH;
for (int i = 0; i < m_liLines.count(); ++i) for (int i = 0; i < m_liLines.count(); ++i)
{ {
const TextLine& tl = m_liLines.at(i); const TextLine& tl = m_liLines.at(i);
@ -180,6 +185,7 @@ bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize)
// check if every line fits within the label width // check if every line fits within the label width
if (fm.width(qslLines[iL]) + iHorSp > fW) if (fm.width(qslLines[iL]) + iHorSp > fW)
{ {
fMinW = fm.width(qslLines[iL]) + iHorSp;
return false; return false;
} }
tlOut.m_qsText = qslLines[iL]; tlOut.m_qsText = qslLines[iL];
@ -187,7 +193,11 @@ bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize)
iY += tlOut.m_iHeight + GetSpacing(); iY += tlOut.m_iHeight + GetSpacing();
} }
} }
return iY < fH; if (iY > fH)
{
fMinH = iY;
}
return iY <= fH;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -200,11 +210,14 @@ bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize)
void VTextManager::FitFontSize(qreal fW, qreal fH) void VTextManager::FitFontSize(qreal fW, qreal fH)
{ {
int iFontSize = GetFont().pixelSize(); int iFontSize = GetFont().pixelSize();
while (IsBigEnough(fW, fH, iFontSize) == true && iFontSize <= MAX_FONT_SIZE) qreal fMinW;
qreal fMinH;
while (IsBigEnough(fW, fH, iFontSize, fMinW, fMinH) == true && iFontSize <= MAX_FONT_SIZE)
{ {
++iFontSize; ++iFontSize;
} }
while (IsBigEnough(fW, fH, iFontSize) == false && iFontSize >= MIN_FONT_SIZE) while (IsBigEnough(fW, fH, iFontSize, fMinW, fMinH) == false && iFontSize >= MIN_FONT_SIZE)
{ {
--iFontSize; --iFontSize;
} }

View File

@ -82,7 +82,7 @@ public:
int GetCount() const; int GetCount() const;
int GetSourceLineCount() const; int GetSourceLineCount() const;
const TextLine& GetLine(int i) const; const TextLine& GetLine(int i) const;
bool IsBigEnough(qreal fW, qreal fH, int iFontSize); bool IsBigEnough(qreal fW, qreal fH, int iFontSize, qreal& fMinW, qreal& fMinH);
void FitFontSize(qreal fW, qreal fH); void FitFontSize(qreal fW, qreal fH);
void Update(const QString& qsName, const VPatternPieceData& data); void Update(const QString& qsName, const VPatternPieceData& data);
void Update(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight); void Update(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight);

View File

@ -196,13 +196,21 @@ bool VTextGraphicsItem::IsIdle() const
void VTextGraphicsItem::AddLine(const TextLine& tl) void VTextGraphicsItem::AddLine(const TextLine& tl)
{ {
m_tm.AddLine(tl); m_tm.AddLine(tl);
while (m_tm.IsBigEnough(MIN_W, m_iMinH, MIN_FONT_SIZE) == false) qreal fW = MIN_W;
qreal fH = m_iMinH;
qreal fMinW;
qreal fMinH;
while (m_tm.IsBigEnough(fW, fH, MIN_FONT_SIZE, fMinW, fMinH) == false)
{ {
m_iMinH += 5; SetSize(fMinW, fMinH);
fW = m_rectBoundingBox.width();
fH = m_rectBoundingBox.height();
} }
if (m_rectBoundingBox.height() < m_iMinH) qreal dX;
qreal dY;
if (IsContained(m_rectBoundingBox, rotation(), dX, dY) == false)
{ {
SetSize(m_rectBoundingBox.width(), m_iMinH); setPos(m_rectBoundingBox.left() + dX, m_rectBoundingBox.top() + dY);
} }
} }
@ -259,11 +267,11 @@ void VTextGraphicsItem::SetSize(qreal fW, qreal fH)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief VTextGraphicsItem::Update sets the correct font size and redraws the label * @brief VTextGraphicsItem::Update sets the correct size and font size and redraws the label
*/ */
void VTextGraphicsItem::Update() void VTextGraphicsItem::Update()
{ {
UpdateFont(); CorrectLabel();
UpdateBox(); UpdateBox();
} }
@ -577,20 +585,42 @@ void VTextGraphicsItem::UpdateBox()
* @brief VTextGraphicsItem::UpdateFont sets the text font size, so that the entire text will * @brief VTextGraphicsItem::UpdateFont sets the text font size, so that the entire text will
* just fit into the label bounding box * just fit into the label bounding box
*/ */
void VTextGraphicsItem::UpdateFont() void VTextGraphicsItem::CorrectLabel()
{ {
int iFS = m_tm.GetFont().pixelSize(); int iFS = m_tm.GetFont().pixelSize();
qreal fMinW;
qreal fMinH;
// increase the font size until the bounding rect is not big enough // increase the font size until the bounding rect is not big enough
while (iFS < MAX_FONT_SIZE && m_tm.IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == true) while (
iFS < MAX_FONT_SIZE &&
m_tm.IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS, fMinW, fMinH) == true
)
{ {
++iFS; ++iFS;
} }
// decrease the font size until the bounding rect is big enough // decrease the font size until the bounding rect is big enough
while (iFS >= MIN_FONT_SIZE && m_tm.IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == false) while (m_tm.IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS, fMinW, fMinH) == false)
{
if (iFS > MIN_FONT_SIZE)
{ {
--iFS; --iFS;
} }
else
{
SetSize(fMinW, fMinH);
}
}
qreal dX;
qreal dY;
QRectF rectBB;
rectBB.setTopLeft(pos());
rectBB.setSize(m_rectBoundingBox.size());
if (IsContained(rectBB, rotation(), dX, dY) == false)
{
// put the label inside the pattern
setPos(pos().x() + dX, pos().y() + dY);
}
m_tm.SetFontSize(iFS); m_tm.SetFontSize(iFS);
UpdateBox(); UpdateBox();
} }

View File

@ -98,7 +98,7 @@ protected:
void hoverMoveEvent(QGraphicsSceneHoverEvent* pHE); void hoverMoveEvent(QGraphicsSceneHoverEvent* pHE);
void hoverLeaveEvent(QGraphicsSceneHoverEvent* pHE); void hoverLeaveEvent(QGraphicsSceneHoverEvent* pHE);
void UpdateBox(); void UpdateBox();
void UpdateFont(); void CorrectLabel();
double GetAngle(QPointF pt) const; double GetAngle(QPointF pt) const;

View File

@ -420,7 +420,7 @@ void VToolDetail::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
QPoint pt = scene()->views().at(0)->mapFromScene(0, 100); QPoint pt = scene()->views().at(0)->mapFromScene(0, 100);
qreal dScale = qSqrt(QPoint::dotProduct(pt - pt0, pt - pt0)); qreal dScale = qSqrt(QPoint::dotProduct(pt - pt0, pt - pt0));
grainLine->SetScale(100/dScale); grainLine->SetScale(100/dScale);
qDebug() << "SCALE" << dScale << 10/dScale; //qDebug() << "SCALE" << dScale << 10/dScale;
} }
if (dataLabel->IsIdle() == false || patternInfo->IsIdle() == false || grainLine->IsIdle() == false) if (dataLabel->IsIdle() == false || patternInfo->IsIdle() == false || grainLine->IsIdle() == false)