From f990a5726c27924b087736a81519ba56f8294a8f Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 27 Apr 2017 14:30:20 +0300 Subject: [PATCH] Refactoring. Join similar parts from methods that update labels. ref #672. Issue #672 that this part better to refactor to eliminate more issues in future. --HG-- branch : release --- src/libs/vtools/tools/vtoolseamallowance.cpp | 138 +++++++++---------- src/libs/vtools/tools/vtoolseamallowance.h | 28 ++-- 2 files changed, 79 insertions(+), 87 deletions(-) diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index f9e23d19f..f79f7469c 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -524,7 +524,7 @@ void VToolSeamAllowance::UpdateAll() //--------------------------------------------------------------------------------------------------------------------- void VToolSeamAllowance::retranslateUi() { - UpdateLabel(); + UpdateDetailLabel(); UpdatePatternInfo(); } @@ -538,7 +538,7 @@ void VToolSeamAllowance::Highlight(quint32 id) /** * @brief UpdateLabel updates the text label, making it just big enough for the text to fit it */ -void VToolSeamAllowance::UpdateLabel() +void VToolSeamAllowance::UpdateDetailLabel() { const VPiece detail = VAbstractTool::data.GetPiece(id); const VPieceLabelData& labelData = detail.GetPatternPieceData(); @@ -546,44 +546,13 @@ void VToolSeamAllowance::UpdateLabel() if (labelData.IsVisible() == true) { QPointF pos; - qreal labelWidth = 0; - qreal labelHeight = 0; qreal labelAngle = 0; - const VTextGraphicsItem::MoveTypes type = FindLabelGeometry(labelData, labelAngle, labelWidth, labelHeight, - pos); - if (type & VGrainlineItem::Error) - { - m_dataLabel->hide(); - return; - } - m_dataLabel->SetMoveType(type); - QFont fnt = QApplication::font(); + if (PrepareLabelData(labelData, m_dataLabel, pos, labelAngle)) { - const int iFS = labelData.GetFontSize(); - iFS < MIN_FONT_SIZE ? fnt.setPixelSize(MIN_FONT_SIZE) : fnt.setPixelSize(iFS); + m_dataLabel->UpdateData(detail.GetName(), labelData); + UpdateLabelItem(m_dataLabel, pos, labelAngle); } - m_dataLabel->SetFont(fnt); - m_dataLabel->SetSize(ToPixel(labelWidth, *VDataTool::data.GetPatternUnit()), - ToPixel(labelHeight, *VDataTool::data.GetPatternUnit())); - m_dataLabel->UpdateData(detail.GetName(), labelData); - - QRectF rectBB; - rectBB.setTopLeft(pos); - rectBB.setWidth(m_dataLabel->boundingRect().width()); - rectBB.setHeight(m_dataLabel->boundingRect().height()); - qreal dX; - qreal dY; - if (m_dataLabel->IsContained(rectBB, labelAngle, dX, dY) == false) - { - pos.setX(pos.x() + dX); - pos.setY(pos.y() + dY); - } - - m_dataLabel->setPos(pos); - m_dataLabel->setRotation(-labelAngle);// expects clockwise direction - m_dataLabel->Update(); - m_dataLabel->show(); } else { @@ -603,45 +572,13 @@ void VToolSeamAllowance::UpdatePatternInfo() if (geom.IsVisible() == true) { QPointF pos; - qreal labelWidth = 0; - qreal labelHeight = 0; qreal labelAngle = 0; - const VTextGraphicsItem::MoveTypes type = FindLabelGeometry(geom, labelAngle, labelWidth, labelHeight, pos); - if (type & VGrainlineItem::Error) - { - m_dataLabel->hide(); - return; - } - m_patternInfo->SetMoveType(type); - QFont fnt = QApplication::font(); - int iFS = geom.GetFontSize(); - if (iFS < MIN_FONT_SIZE) + if (PrepareLabelData(geom, m_patternInfo, pos, labelAngle)) { - iFS = MIN_FONT_SIZE; + m_patternInfo->UpdateData(doc, VContainer::size(), VContainer::height()); + UpdateLabelItem(m_patternInfo, pos, labelAngle); } - fnt.setPixelSize(iFS); - m_patternInfo->SetFont(fnt); - m_patternInfo->SetSize(ToPixel(labelWidth, *VDataTool::data.GetPatternUnit()), - ToPixel(labelHeight, *VDataTool::data.GetPatternUnit())); - m_patternInfo->UpdateData(doc, VContainer::size(), VContainer::height()); - - QRectF rectBB; - rectBB.setTopLeft(pos); - rectBB.setWidth(m_patternInfo->boundingRect().width()); - rectBB.setHeight(m_patternInfo->boundingRect().height()); - qreal dX; - qreal dY; - if (m_patternInfo->IsContained(rectBB, labelAngle, dX, dY) == false) - { - pos.setX(pos.x() + dX); - pos.setY(pos.y() + dY); - } - - m_patternInfo->setPos(pos); - m_patternInfo->setRotation(-labelAngle);// expects clockwise direction - m_patternInfo->Update(); - m_patternInfo->GetTextLines() > 0 ? m_patternInfo->show() : m_patternInfo->hide(); } else { @@ -1190,15 +1127,15 @@ VToolSeamAllowance::VToolSeamAllowance(VAbstractPattern *doc, VContainer *data, connect(m_grainLine, &VGrainlineItem::SignalRotated, this, &VToolSeamAllowance::SaveRotateGrainline); connect(doc, &VAbstractPattern::patternChanged, this, &VToolSeamAllowance::UpdatePatternInfo); - connect(doc, &VAbstractPattern::CheckLayout, this, &VToolSeamAllowance::UpdateLabel); + connect(doc, &VAbstractPattern::CheckLayout, this, &VToolSeamAllowance::UpdateDetailLabel); connect(doc, &VAbstractPattern::CheckLayout, this, &VToolSeamAllowance::UpdatePatternInfo); connect(doc, &VAbstractPattern::CheckLayout, this, &VToolSeamAllowance::UpdateGrainline); - connect(m_sceneDetails, &VMainGraphicsScene::DimensionsChanged, this, &VToolSeamAllowance::UpdateLabel); + connect(m_sceneDetails, &VMainGraphicsScene::DimensionsChanged, this, &VToolSeamAllowance::UpdateDetailLabel); connect(m_sceneDetails, &VMainGraphicsScene::DimensionsChanged, this, &VToolSeamAllowance::UpdatePatternInfo); connect(m_sceneDetails, &VMainGraphicsScene::LanguageChanged, this, &VToolSeamAllowance::retranslateUi); - UpdateLabel(); + UpdateDetailLabel(); UpdatePatternInfo(); UpdateGrainline(); } @@ -1263,7 +1200,7 @@ void VToolSeamAllowance::SaveDialogChange() SavePieceOptions *saveCommand = new SavePieceOptions(oldDet, newDet, doc, id); connect(saveCommand, &SavePieceOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree); qApp->getUndoStack()->push(saveCommand); - UpdateLabel(); + UpdateDetailLabel(); } //--------------------------------------------------------------------------------------------------------------------- @@ -1585,3 +1522,54 @@ void VToolSeamAllowance::ToolCreation(const Source &typeCreation) RefreshDataInFile(); } } + +//--------------------------------------------------------------------------------------------------------------------- +bool VToolSeamAllowance::PrepareLabelData(const VPatternLabelData &labelData, VTextGraphicsItem *labelItem, + QPointF &pos, qreal &labelAngle) +{ + SCASSERT(labelItem != nullptr) + + qreal labelWidth = 0; + qreal labelHeight = 0; + const VTextGraphicsItem::MoveTypes type = FindLabelGeometry(labelData, labelAngle, labelWidth, labelHeight, pos); + if (type & VGrainlineItem::Error) + { + labelItem->hide(); + return false; + } + labelItem->SetMoveType(type); + + QFont fnt = QApplication::font(); + { + const int iFS = labelData.GetFontSize(); + iFS < MIN_FONT_SIZE ? fnt.setPixelSize(MIN_FONT_SIZE) : fnt.setPixelSize(iFS); + } + labelItem->SetFont(fnt); + labelItem->SetSize(ToPixel(labelWidth, *VDataTool::data.GetPatternUnit()), + ToPixel(labelHeight, *VDataTool::data.GetPatternUnit())); + + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VToolSeamAllowance::UpdateLabelItem(VTextGraphicsItem *labelItem, QPointF pos, qreal labelAngle) +{ + SCASSERT(labelItem != nullptr) + + QRectF rectBB; + rectBB.setTopLeft(pos); + rectBB.setWidth(labelItem->boundingRect().width()); + rectBB.setHeight(labelItem->boundingRect().height()); + qreal dX; + qreal dY; + if (labelItem->IsContained(rectBB, labelAngle, dX, dY) == false) + { + pos.setX(pos.x() + dX); + pos.setY(pos.y() + dY); + } + + labelItem->setPos(pos); + labelItem->setRotation(-labelAngle);// expects clockwise direction + labelItem->Update(); + labelItem->GetTextLines() > 0 ? labelItem->show() : labelItem->hide(); +} diff --git a/src/libs/vtools/tools/vtoolseamallowance.h b/src/libs/vtools/tools/vtoolseamallowance.h index 761dc98d9..280244cda 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.h +++ b/src/libs/vtools/tools/vtoolseamallowance.h @@ -103,18 +103,18 @@ public slots: virtual void retranslateUi(); void Highlight(quint32 id); protected slots: - virtual void UpdateLabel(); - virtual void UpdatePatternInfo(); - virtual void UpdateGrainline(); - virtual void SaveMoveDetail(const QPointF &ptPos); - virtual void SaveResizeDetail(qreal dLabelW, int iFontSize); - virtual void SaveRotationDetail(qreal dRot); - virtual void SaveMovePattern(const QPointF& ptPos); - virtual void SaveResizePattern(qreal dLabelW, int iFontSize); - virtual void SaveRotationPattern(qreal dRot); - virtual void SaveMoveGrainline(const QPointF& ptPos); - virtual void SaveResizeGrainline(qreal dLength); - virtual void SaveRotateGrainline(qreal dRot, const QPointF& ptPos); + void UpdateDetailLabel(); + void UpdatePatternInfo(); + void UpdateGrainline(); + void SaveMoveDetail(const QPointF &ptPos); + void SaveResizeDetail(qreal dLabelW, int iFontSize); + void SaveRotationDetail(qreal dRot); + void SaveMovePattern(const QPointF& ptPos); + void SaveResizePattern(qreal dLabelW, int iFontSize); + void SaveRotationPattern(qreal dRot); + void SaveMoveGrainline(const QPointF& ptPos); + void SaveResizeGrainline(qreal dLength); + void SaveRotateGrainline(qreal dRot, const QPointF& ptPos); protected: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE; @@ -163,6 +163,10 @@ private: void InitCSAPaths(const VPiece &detail); void InitInternalPaths(const VPiece &detail); void InitPins(const VPiece &detail); + + bool PrepareLabelData(const VPatternLabelData &labelData, VTextGraphicsItem *labelItem, QPointF &pos, + qreal &labelAngle); + void UpdateLabelItem(VTextGraphicsItem *labelItem, QPointF pos, qreal labelAngle); }; #endif // VTOOLSEAMALLOWANCE_H