From caa6b425b978aede08e2e870a4bd120bf13279bf Mon Sep 17 00:00:00 2001 From: BojanKverh Date: Fri, 24 Jun 2016 17:57:08 +0200 Subject: [PATCH] Tried QGraphicsTextItem::boundingBox approach --HG-- branch : feature --- src/app/share/collection/Keiko_skirt.val | 18 ++-- src/libs/vpatterndb/vpatternpiecedata.h | 2 +- src/libs/vtools/tools/vtextgraphicsitem.cpp | 101 +++++++++++++------- src/libs/vtools/tools/vtextgraphicsitem.h | 13 ++- src/libs/vtools/tools/vtooldetail.cpp | 75 +++++++-------- src/libs/vtools/tools/vtooldetail.h | 1 + 6 files changed, 118 insertions(+), 92 deletions(-) diff --git a/src/app/share/collection/Keiko_skirt.val b/src/app/share/collection/Keiko_skirt.val index 32ac3722d..e097391eb 100644 --- a/src/app/share/collection/Keiko_skirt.val +++ b/src/app/share/collection/Keiko_skirt.val @@ -1,7 +1,7 @@ - 0.3.1 + 0.2.4 cm @@ -26,14 +26,14 @@ - - - - - - - - + + + + + + + + diff --git a/src/libs/vpatterndb/vpatternpiecedata.h b/src/libs/vpatterndb/vpatternpiecedata.h index 0591a95ad..aee873fbb 100644 --- a/src/libs/vpatterndb/vpatternpiecedata.h +++ b/src/libs/vpatterndb/vpatternpiecedata.h @@ -29,7 +29,7 @@ #ifndef VPATTERNPIECEDATA_H #define VPATTERNPIECEDATA_H -#define MIN_FONT_SIZE 24 +#define MIN_FONT_SIZE 12 #include #include diff --git a/src/libs/vtools/tools/vtextgraphicsitem.cpp b/src/libs/vtools/tools/vtextgraphicsitem.cpp index f1c5a45c9..9f97f2702 100644 --- a/src/libs/vtools/tools/vtextgraphicsitem.cpp +++ b/src/libs/vtools/tools/vtextgraphicsitem.cpp @@ -29,11 +29,16 @@ #include #include #include +#include #include #include "vtextgraphicsitem.h" #define RESIZE_SQUARE 30 +#define MIN_W 120 +#define MIN_H 60 +#define MIN_FONT_SIZE 12 +#define MAX_FONT_SIZE 36 //--------------------------------------------------------------------------------------------------------------------- VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent) @@ -42,7 +47,12 @@ VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent) setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); m_eMode = mNormal; - m_iMinW = 100; + m_rectBoundingBox.setTopLeft(QPointF(0, 0)); + setTextWidth(MIN_W); + m_rectBoundingBox.setWidth(MIN_W); + m_iMinH = MIN_H; + m_rectBoundingBox.setHeight(m_iMinH); + setZValue(2); } //--------------------------------------------------------------------------------------------------------------------- @@ -67,13 +77,6 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem } } -//--------------------------------------------------------------------------------------------------------------------- -void VTextGraphicsItem::SetMinimalWidth(int iW) -{ - m_iMinW = iW; - setTextWidth(m_iMinW); -} - //--------------------------------------------------------------------------------------------------------------------- void VTextGraphicsItem::Reset() { @@ -82,11 +85,21 @@ void VTextGraphicsItem::Reset() } //--------------------------------------------------------------------------------------------------------------------- -void VTextGraphicsItem::SetHTML(const QString& qsHTML) +QRectF VTextGraphicsItem::boundingRect() const +{ return m_rectBoundingBox; } + +//--------------------------------------------------------------------------------------------------------------------- +void VTextGraphicsItem::SetHTML(const QString& qsHtml) { - QGraphicsTextItem::setHtml(qsHTML); - m_iMinFontSize = font().pixelSize(); - m_iMinH = boundingRect().height(); + QGraphicsTextItem::setHtml(qsHtml); + m_rectBoundingBox.setHeight(document()->size().height()); + + // calculate the minimal height + QScopedPointer pDoc(document()->clone()); + QFont fnt = font(); + fnt.setPixelSize(MIN_FONT_SIZE); + pDoc->setDefaultFont(fnt); + m_iMinH = pDoc->size().height(); } //--------------------------------------------------------------------------------------------------------------------- @@ -98,13 +111,11 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME) if (m_rectResize.contains(pME->pos()) == true) { m_eMode = mResize; - m_iCurrentH = boundingRect().height(); } else { m_eMode = mMove; } - qDebug() << "MOUSEPRESS" << m_rectResize << pME->pos() << int(m_eMode); Update(); m_ptStart = pME->pos(); } @@ -122,29 +133,21 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *pME) else if (m_eMode == mResize) { prepareGeometryChange(); - int iNewW = textWidth() + pME->pos().x() - m_ptStart.x(); - m_iCurrentH += pME->pos().y() - m_ptStart.y(); - if (iNewW >= m_iMinW) - { - setTextWidth(iNewW); - m_ptStart.setX(pME->pos().x()); - Update(); - } + qreal fW = m_rectBoundingBox.width() + pME->pos().x() - m_ptStart.x(); + qreal fH = m_rectBoundingBox.height() + pME->pos().y() - m_ptStart.y(); - if (m_iCurrentH >= m_iMinH) + if (fW >= MIN_W) { - int iFontSize = m_iMinFontSize*m_iCurrentH/m_iMinH; - if (iFontSize > m_iMinFontSize*2) - { - // prevent too big letters - return; - } - QFont fnt = font(); - fnt.setPixelSize(iFontSize); - setFont(fnt); - m_ptStart.setY(pME->pos().y()); - Update(); + m_rectBoundingBox.setWidth(fW); + setTextWidth(fW); } + if (fH >= m_iMinH) + { + m_rectBoundingBox.setHeight(fH); + } + m_ptStart = pME->pos(); + + UpdateFont(); } } @@ -183,10 +186,34 @@ void VTextGraphicsItem::SetResizeArea() } //--------------------------------------------------------------------------------------------------------------------- -void VTextGraphicsItem::setHtml(const QString &qsHtml) +void VTextGraphicsItem::UpdateFont() { - Q_UNUSED(qsHtml); - // prevent using this method! Use SetHTML instead! + QScopedPointer pDoc(document()->clone()); + pDoc->setTextWidth(m_rectBoundingBox.width()); + + QFont fnt = font(); + int iFS = fnt.pixelSize(); + QSizeF sz = pDoc->size(); + while (sz.width() < m_rectBoundingBox.width() && sz.height() < m_rectBoundingBox.height() && iFS < MAX_FONT_SIZE) + { + ++iFS; + fnt.setPixelSize(iFS); + pDoc->setDefaultFont(fnt); + sz = pDoc->size(); + } + + while ((sz.width() >= m_rectBoundingBox.width() || sz.height() >= m_rectBoundingBox.height()) && iFS > MIN_FONT_SIZE) + { + --iFS; + fnt.setPixelSize(iFS); + pDoc->setDefaultFont(fnt); + sz = pDoc->size(); + + } + + fnt.setPixelSize(iFS); + setFont(fnt); + Update(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/tools/vtextgraphicsitem.h b/src/libs/vtools/tools/vtextgraphicsitem.h index f9a0b21e9..9a840f526 100644 --- a/src/libs/vtools/tools/vtextgraphicsitem.h +++ b/src/libs/vtools/tools/vtextgraphicsitem.h @@ -48,10 +48,10 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - void SetMinimalWidth(int iW); - void Reset(); - void SetHTML(const QString& qsHTML); + + QRectF boundingRect() const; + void SetHTML(const QString& qsHtml); signals: void SignalMoved(QPointF ptPos); @@ -64,16 +64,15 @@ protected: void Update(); void SetResizeArea(); - void setHtml(const QString& qsHtml); + + void UpdateFont(); private: - int m_iMinW; Mode m_eMode; QPointF m_ptStart; QRectF m_rectResize; - int m_iMinFontSize; int m_iMinH; - int m_iCurrentH; + QRectF m_rectBoundingBox; }; #endif // VTEXTGRAPHICSITEM_H diff --git a/src/libs/vtools/tools/vtooldetail.cpp b/src/libs/vtools/tools/vtooldetail.cpp index 7008923ad..c4a0f588c 100644 --- a/src/libs/vtools/tools/vtooldetail.cpp +++ b/src/libs/vtools/tools/vtooldetail.cpp @@ -130,6 +130,7 @@ VToolDetail::VToolDetail(VAbstractPattern *doc, VContainer *data, const quint32 connect(dataLabel, &VTextGraphicsItem::SignalMoved, this, &VToolDetail::SaveMove); connect(dataLabel, &VTextGraphicsItem::SignalResized, this, &VToolDetail::SaveResize); + UpdateLabel(); } //--------------------------------------------------------------------------------------------------------------------- @@ -302,7 +303,6 @@ void VToolDetail::FullUpdateFromGuiOk(int result) { if (result == QDialog::Accepted) { - qDebug() << "FullUpdate" << qApp->getUndoStack()->count(); SCASSERT(dialog != nullptr); DialogDetail *dialogTool = qobject_cast(dialog); SCASSERT(dialogTool != nullptr); @@ -313,7 +313,7 @@ void VToolDetail::FullUpdateFromGuiOk(int result) SaveDetailOptions *saveCommand = new SaveDetailOptions(oldDet, newDet, doc, id, this->scene()); connect(saveCommand, &SaveDetailOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); qApp->getUndoStack()->push(saveCommand); - qDebug() << "FullUpdate finished" << qApp->getUndoStack()->count(); + UpdateLabel(); } delete dialog; dialog = nullptr; @@ -629,6 +629,41 @@ void VToolDetail::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) } } +//--------------------------------------------------------------------------------------------------------------------- +/** + * @brief UpdateLabel updates the text label, making it just big enough for the text to fit it + */ +void VToolDetail::UpdateLabel() +{ + const VDetail detail = VAbstractTool::data.GetDetail(id); + + const VPatternPieceData& data = detail.GetPatternPieceData(); + if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0) + { + QString qsText = "Cut %1 on %2%3"; + QStringList qslPlace; + qslPlace << "" << " on Fold"; + QFont fnt = qApp->font(); + fnt.setPixelSize(data.GetFontSize()); + dataLabel->setFont(fnt); + QString qsHTML = "
" + data.GetLetter() + "

"; + qsHTML += "" + data.GetName() + "
"; + for (int i = 0; i < data.GetMCPCount(); ++i) + { + MaterialCutPlacement mcp = data.GetMCP(i); + QString qsLine = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]); + qsHTML += qsLine + "
"; + } + dataLabel->SetHTML(qsHTML); + dataLabel->setPos(data.GetPos()); + dataLabel->show(); + } + else + { + dataLabel->hide(); + } +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief SaveMove saves the move operation to the undo stack @@ -752,42 +787,6 @@ void VToolDetail::RefreshGeometry() } this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); - - const VPatternPieceData& data = detail.GetPatternPieceData(); - if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0) - { - QString qsText = "Cut %1 on %2%3"; - QStringList qslPlace; - qslPlace << "" << " on Fold"; - QFont fnt = qApp->font(); - fnt.setPixelSize(data.GetFontSize()); - QFontMetrics fm(fnt); - dataLabel->setFont(fnt); - int iMinW = 200; - QString qsHTML = "" + data.GetLetter() + "
"; - qsHTML += "" + data.GetName() + "
"; - for (int i = 0; i < data.GetMCPCount(); ++i) - { - MaterialCutPlacement mcp = data.GetMCP(i); - QString qsLine = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]); - if (fm.width(qsLine) > iMinW) - iMinW = fm.width(qsLine); - qsHTML += qsLine + "
"; - } - // also add some offset - dataLabel->SetMinimalWidth(iMinW + 10); - if (data.GetLabelWidth() > iMinW) - { - dataLabel->setTextWidth(data.GetLabelWidth()); - } - dataLabel->SetHTML(qsHTML); - dataLabel->setPos(data.GetPos()); - dataLabel->show(); - } - else - { - dataLabel->hide(); - } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vtools/tools/vtooldetail.h b/src/libs/vtools/tools/vtooldetail.h index a71c72b01..322129eac 100644 --- a/src/libs/vtools/tools/vtooldetail.h +++ b/src/libs/vtools/tools/vtooldetail.h @@ -104,6 +104,7 @@ protected: virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ) Q_DECL_OVERRIDE; virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE; virtual void SetVisualization() Q_DECL_OVERRIDE {} + virtual void UpdateLabel(); protected slots: virtual void SaveMove(QPointF ptPos);