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 fH rectangle height
* @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)
*/
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();
QFont fnt = m_font;
int iY = 0;
fMinW = fW;
fMinH = fH;
for (int i = 0; i < m_liLines.count(); ++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
if (fm.width(qslLines[iL]) + iHorSp > fW)
{
fMinW = fm.width(qslLines[iL]) + iHorSp;
return false;
}
tlOut.m_qsText = qslLines[iL];
@ -187,7 +193,11 @@ bool VTextManager::IsBigEnough(qreal fW, qreal fH, int iFontSize)
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)
{
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;
}
while (IsBigEnough(fW, fH, iFontSize) == false && iFontSize >= MIN_FONT_SIZE)
while (IsBigEnough(fW, fH, iFontSize, fMinW, fMinH) == false && iFontSize >= MIN_FONT_SIZE)
{
--iFontSize;
}

View file

@ -82,7 +82,7 @@ public:
int GetCount() const;
int GetSourceLineCount() 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 Update(const QString& qsName, const VPatternPieceData& data);
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)
{
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()
{
UpdateFont();
CorrectLabel();
UpdateBox();
}
@ -577,19 +585,41 @@ void VTextGraphicsItem::UpdateBox()
* @brief VTextGraphicsItem::UpdateFont sets the text font size, so that the entire text will
* just fit into the label bounding box
*/
void VTextGraphicsItem::UpdateFont()
void VTextGraphicsItem::CorrectLabel()
{
int iFS = m_tm.GetFont().pixelSize();
qreal fMinW;
qreal fMinH;
// 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;
}
// 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)
{
--iFS;
if (iFS > MIN_FONT_SIZE)
{
--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);
UpdateBox();

View file

@ -98,7 +98,7 @@ protected:
void hoverMoveEvent(QGraphicsSceneHoverEvent* pHE);
void hoverLeaveEvent(QGraphicsSceneHoverEvent* pHE);
void UpdateBox();
void UpdateFont();
void CorrectLabel();
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);
qreal dScale = qSqrt(QPoint::dotProduct(pt - pt0, pt - pt0));
grainLine->SetScale(100/dScale);
qDebug() << "SCALE" << dScale << 10/dScale;
//qDebug() << "SCALE" << dScale << 10/dScale;
}
if (dataLabel->IsIdle() == false || patternInfo->IsIdle() == false || grainLine->IsIdle() == false)