From 083b2035afa9db1fca1211102a32284dcdc9fb49 Mon Sep 17 00:00:00 2001 From: BojanKverh Date: Thu, 29 Sep 2016 01:39:50 +0200 Subject: [PATCH] Hopefully this commit solves the #551 issue --HG-- branch : feature --- src/libs/vlayout/vtextmanager.cpp | 21 +++++++-- src/libs/vlayout/vtextmanager.h | 2 +- src/libs/vtools/tools/vtextgraphicsitem.cpp | 50 ++++++++++++++++----- src/libs/vtools/tools/vtextgraphicsitem.h | 2 +- src/libs/vtools/tools/vtooldetail.cpp | 2 +- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/libs/vlayout/vtextmanager.cpp b/src/libs/vlayout/vtextmanager.cpp index 936a1a95e..b375e3c73 100644 --- a/src/libs/vlayout/vtextmanager.cpp +++ b/src/libs/vlayout/vtextmanager.cpp @@ -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; } diff --git a/src/libs/vlayout/vtextmanager.h b/src/libs/vlayout/vtextmanager.h index 1e3d2bf77..98d41aeb1 100644 --- a/src/libs/vlayout/vtextmanager.h +++ b/src/libs/vlayout/vtextmanager.h @@ -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); diff --git a/src/libs/vtools/tools/vtextgraphicsitem.cpp b/src/libs/vtools/tools/vtextgraphicsitem.cpp index bb73820c2..4fb029650 100644 --- a/src/libs/vtools/tools/vtextgraphicsitem.cpp +++ b/src/libs/vtools/tools/vtextgraphicsitem.cpp @@ -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(); diff --git a/src/libs/vtools/tools/vtextgraphicsitem.h b/src/libs/vtools/tools/vtextgraphicsitem.h index fdd451820..88e0eb460 100644 --- a/src/libs/vtools/tools/vtextgraphicsitem.h +++ b/src/libs/vtools/tools/vtextgraphicsitem.h @@ -98,7 +98,7 @@ protected: void hoverMoveEvent(QGraphicsSceneHoverEvent* pHE); void hoverLeaveEvent(QGraphicsSceneHoverEvent* pHE); void UpdateBox(); - void UpdateFont(); + void CorrectLabel(); double GetAngle(QPointF pt) const; diff --git a/src/libs/vtools/tools/vtooldetail.cpp b/src/libs/vtools/tools/vtooldetail.cpp index 08290fdb0..8c0c03ac6 100644 --- a/src/libs/vtools/tools/vtooldetail.cpp +++ b/src/libs/vtools/tools/vtooldetail.cpp @@ -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)