From 84c96c35dd74ab398177bcc2ddc5b78915bd1849 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 28 Jan 2019 20:35:10 +0200 Subject: [PATCH] Refactoring layout piece. --HG-- branch : develop --- src/libs/vlayout/vabstractpiece.cpp | 197 ++++++++++++++++++++++++++++ src/libs/vlayout/vabstractpiece.h | 12 +- src/libs/vlayout/vlayoutpiece.cpp | 183 +------------------------- 3 files changed, 214 insertions(+), 178 deletions(-) diff --git a/src/libs/vlayout/vabstractpiece.cpp b/src/libs/vlayout/vabstractpiece.cpp index 3031badd8..f025a0d7e 100644 --- a/src/libs/vlayout/vabstractpiece.cpp +++ b/src/libs/vlayout/vabstractpiece.cpp @@ -31,6 +31,10 @@ #include "../vmisc/vabstractapplication.h" #include "../vgeometry/vpointf.h" #include "../ifc/exception/vexception.h" +#include "../vmisc/vmath.h" +#include "../vpatterndb/floatItemData/vgrainlinedata.h" +#include "../vpatterndb/vcontainer.h" +#include "../vpatterndb/calculator.h" #include #include @@ -1540,3 +1544,196 @@ QVector VAbstractPiece::RollbackSeamAllowance(QVector points, return points; } + + +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractPiece::IsItemContained(const QRectF &parentBoundingRect, const QVector &shape, qreal &dX, + qreal &dY) +{ + dX = 0; + dY = 0; + // single point differences + bool bInside = true; + + for (auto p : shape) + { + qreal dPtX = 0; + qreal dPtY = 0; + if (not parentBoundingRect.contains(p)) + { + if (p.x() < parentBoundingRect.left()) + { + dPtX = parentBoundingRect.left() - p.x(); + } + else if (p.x() > parentBoundingRect.right()) + { + dPtX = parentBoundingRect.right() - p.x(); + } + + if (p.y() < parentBoundingRect.top()) + { + dPtY = parentBoundingRect.top() - p.y(); + } + else if (p.y() > parentBoundingRect.bottom()) + { + dPtY = parentBoundingRect.bottom() - p.y(); + } + + if (qAbs(dPtX) > qAbs(dX)) + { + dX = dPtX; + } + + if (qAbs(dPtY) > qAbs(dY)) + { + dY = dPtY; + } + + bInside = false; + } + } + return bInside; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VAbstractPiece::CorrectPosition(const QRectF &parentBoundingRect, QVector points) +{ + qreal dX = 0; + qreal dY = 0; + if (not IsItemContained(parentBoundingRect, points, dX, dY)) + { + for (int i =0; i < points.size(); ++i) + { + points[i] = QPointF(points.at(i).x() + dX, points.at(i).y() + dY); + } + } + return points; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractPiece::FindGrainlineGeometry(const VGrainlineData& geom, const VContainer *pattern, qreal &length, + qreal &rotationAngle, QPointF &pos) +{ + SCASSERT(pattern != nullptr) + + const quint32 topPin = geom.TopPin(); + const quint32 bottomPin = geom.BottomPin(); + + if (topPin != NULL_ID && bottomPin != NULL_ID) + { + try + { + const auto topPinPoint = pattern->GeometricObject(topPin); + const auto bottomPinPoint = pattern->GeometricObject(bottomPin); + + QLineF grainline(static_cast(*bottomPinPoint), static_cast(*topPinPoint)); + length = grainline.length(); + rotationAngle = grainline.angle(); + + if (not VFuzzyComparePossibleNulls(rotationAngle, 0)) + { + grainline.setAngle(0); + } + + pos = grainline.p1(); + rotationAngle = qDegreesToRadians(rotationAngle); + + return true; + } + catch(const VExceptionBadId &) + { + // do nothing. + } + } + + try + { + Calculator cal1; + rotationAngle = cal1.EvalFormula(pattern->DataVariables(), geom.GetRotation()); + rotationAngle = qDegreesToRadians(rotationAngle); + + Calculator cal2; + length = cal2.EvalFormula(pattern->DataVariables(), geom.GetLength()); + length = ToPixel(length, *pattern->GetPatternUnit()); + } + catch(qmu::QmuParserError &e) + { + Q_UNUSED(e); + return false; + } + + const quint32 centerPin = geom.CenterPin(); + if (centerPin != NULL_ID) + { + try + { + const auto centerPinPoint = pattern->GeometricObject(centerPin); + + QLineF grainline(centerPinPoint->x(), centerPinPoint->y(), + centerPinPoint->x() + length / 2.0, centerPinPoint->y()); + + grainline.setAngle(qRadiansToDegrees(rotationAngle)); + grainline = QLineF(grainline.p2(), grainline.p1()); + grainline.setLength(length); + + pos = grainline.p2(); + } + catch(const VExceptionBadId &) + { + pos = geom.GetPos(); + } + } + else + { + pos = geom.GetPos(); + } + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VAbstractPiece::GrainlinePoints(const VGrainlineData &geom, const VContainer *pattern, + const QRectF &boundingRect, qreal &dAng) +{ + SCASSERT(pattern != nullptr) + + QPointF pt1; + qreal dLen = 0; + if ( not FindGrainlineGeometry(geom, pattern, dLen, dAng, pt1)) + { + return QVector(); + } + + qreal rotation = dAng; + + QPointF pt2(pt1.x() + dLen * qCos(rotation), pt1.y() - dLen * qSin(rotation)); + + const qreal dArrowLen = ToPixel(0.5, *pattern->GetPatternUnit()); + const qreal dArrowAng = M_PI/9; + + QVector v; + v << pt1; + + if (geom.GetArrowType() != ArrowType::atFront) + { + v << QPointF(pt1.x() + dArrowLen * qCos(rotation + dArrowAng), + pt1.y() - dArrowLen * qSin(rotation + dArrowAng)); + v << QPointF(pt1.x() + dArrowLen * qCos(rotation - dArrowAng), + pt1.y() - dArrowLen * qSin(rotation - dArrowAng)); + v << pt1; + } + + v << pt2; + + if (geom.GetArrowType() != ArrowType::atRear) + { + rotation += M_PI; + + v << QPointF(pt2.x() + dArrowLen * qCos(rotation + dArrowAng), + pt2.y() - dArrowLen * qSin(rotation + dArrowAng)); + v << QPointF(pt2.x() + dArrowLen * qCos(rotation - dArrowAng), + pt2.y() - dArrowLen * qSin(rotation - dArrowAng)); + v << pt2; + } + + return CorrectPosition(boundingRect, v); +} diff --git a/src/libs/vlayout/vabstractpiece.h b/src/libs/vlayout/vabstractpiece.h index 9aa4b13d2..c5626fc4f 100644 --- a/src/libs/vlayout/vabstractpiece.h +++ b/src/libs/vlayout/vabstractpiece.h @@ -42,6 +42,8 @@ template class QVector; class VAbstractPieceData; class QPainterPath; +class VGrainlineData; +class VContainer; QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Weffc++") @@ -203,13 +205,21 @@ public: static QVector RollbackSeamAllowance(QVector points, const QLineF &cuttingEdge, bool *success); + static QVector GrainlinePoints(const VGrainlineData &geom, const VContainer *pattern, + const QRectF &boundingRect, qreal &dAng); + protected: template static QVector RemoveDublicates(const QVector &points, bool removeFirstAndLast = true); static bool IsEkvPointOnLine(const QPointF &iPoint, const QPointF &prevPoint, const QPointF &nextPoint); static bool IsEkvPointOnLine(const VSAPoint &iPoint, const VSAPoint &prevPoint, const VSAPoint &nextPoint); - static QPainterPath PlaceLabelImgPath(const PlaceLabelImg &img); + static QPainterPath PlaceLabelImgPath(const PlaceLabelImg &img); + static bool IsItemContained(const QRectF &parentBoundingRect, const QVector &shape, qreal &dX, + qreal &dY); + static QVector CorrectPosition(const QRectF &parentBoundingRect, QVector points); + static bool FindGrainlineGeometry(const VGrainlineData& geom, const VContainer *pattern, qreal &length, + qreal &rotationAngle, QPointF &pos); private: QSharedDataPointer d; }; diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index 5b07ccb26..e971df3ff 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -159,149 +159,6 @@ bool FindLabelGeometry(const VPatternLabelData &labelData, const VContainer *pat return true; } -//--------------------------------------------------------------------------------------------------------------------- -bool FindGrainlineGeometry(const VGrainlineData& geom, const VContainer *pattern, qreal &length, qreal &rotationAngle, - QPointF &pos) -{ - SCASSERT(pattern != nullptr) - - const quint32 topPin = geom.TopPin(); - const quint32 bottomPin = geom.BottomPin(); - - if (topPin != NULL_ID && bottomPin != NULL_ID) - { - try - { - const auto topPinPoint = pattern->GeometricObject(topPin); - const auto bottomPinPoint = pattern->GeometricObject(bottomPin); - - QLineF grainline(static_cast(*bottomPinPoint), static_cast(*topPinPoint)); - length = grainline.length(); - rotationAngle = grainline.angle(); - - if (not VFuzzyComparePossibleNulls(rotationAngle, 0)) - { - grainline.setAngle(0); - } - - pos = grainline.p1(); - rotationAngle = qDegreesToRadians(rotationAngle); - - return true; - } - catch(const VExceptionBadId &) - { - // do nothing. - } - } - - try - { - Calculator cal1; - rotationAngle = cal1.EvalFormula(pattern->DataVariables(), geom.GetRotation()); - rotationAngle = qDegreesToRadians(rotationAngle); - - Calculator cal2; - length = cal2.EvalFormula(pattern->DataVariables(), geom.GetLength()); - length = ToPixel(length, *pattern->GetPatternUnit()); - } - catch(qmu::QmuParserError &e) - { - Q_UNUSED(e); - return false; - } - - const quint32 centerPin = geom.CenterPin(); - if (centerPin != NULL_ID) - { - try - { - const auto centerPinPoint = pattern->GeometricObject(centerPin); - - QLineF grainline(centerPinPoint->x(), centerPinPoint->y(), - centerPinPoint->x() + length / 2.0, centerPinPoint->y()); - - grainline.setAngle(qRadiansToDegrees(rotationAngle)); - grainline = QLineF(grainline.p2(), grainline.p1()); - grainline.setLength(length); - - pos = grainline.p2(); - } - catch(const VExceptionBadId &) - { - pos = geom.GetPos(); - } - } - else - { - pos = geom.GetPos(); - } - return true; -} - -//--------------------------------------------------------------------------------------------------------------------- -bool IsItemContained(const QRectF &parentBoundingRect, const QVector &shape, qreal &dX, qreal &dY) -{ - dX = 0; - dY = 0; - // single point differences - bool bInside = true; - - for (auto p : shape) - { - qreal dPtX = 0; - qreal dPtY = 0; - if (not parentBoundingRect.contains(p)) - { - if (p.x() < parentBoundingRect.left()) - { - dPtX = parentBoundingRect.left() - p.x(); - } - else if (p.x() > parentBoundingRect.right()) - { - dPtX = parentBoundingRect.right() - p.x(); - } - - if (p.y() < parentBoundingRect.top()) - { - dPtY = parentBoundingRect.top() - p.y(); - } - else if (p.y() > parentBoundingRect.bottom()) - { - dPtY = parentBoundingRect.bottom() - p.y(); - } - - if (fabs(dPtX) > fabs(dX)) - { - dX = dPtX; - } - - if (fabs(dPtY) > fabs(dY)) - { - dY = dPtY; - } - - bInside = false; - } - } - return bInside; -} - -//--------------------------------------------------------------------------------------------------------------------- -QVector CorrectPosition(const QRectF &parentBoundingRect, QVector points) -{ - qreal dX = 0; - qreal dY = 0; - if (not IsItemContained(parentBoundingRect, points, dX, dY)) - { - for (int i =0; i < points.size(); ++i) - { - points[i] = QPointF(points.at(i).x() + dX, points.at(i).y() + dY); - } - } - return points; -} - //--------------------------------------------------------------------------------------------------------------------- QVector PrepareAllowance(const QVector &points) { @@ -662,12 +519,12 @@ void VLayoutPiece::SetPatternInfo(VAbstractPattern* pDoc, const VPatternLabelDat //--------------------------------------------------------------------------------------------------------------------- void VLayoutPiece::SetGrainline(const VGrainlineData& geom, const VContainer* pattern) { - SCASSERT(pattern != nullptr) - - QPointF pt1; qreal dAng = 0; - qreal dLen = 0; - if ( not FindGrainlineGeometry(geom, pattern, dLen, dAng, pt1)) + + QScopedPointer item(GetMainPathItem()); + const QVector v = GrainlinePoints(geom, pattern, item->boundingRect(), dAng); + + if (v.isEmpty()) { return; } @@ -675,35 +532,7 @@ void VLayoutPiece::SetGrainline(const VGrainlineData& geom, const VContainer* pa d->grainlineEnabled = true; d->grainlineArrowType = geom.GetArrowType(); d->grainlineAngle = qRadiansToDegrees(dAng); - - QPointF pt2(pt1.x() + dLen * qCos(dAng), pt1.y() - dLen * qSin(dAng)); - - const qreal dArrowLen = ToPixel(0.5, *pattern->GetPatternUnit()); - const qreal dArrowAng = M_PI/9; - - QVector v; - v << pt1; - - if (geom.GetArrowType() != ArrowType::atFront) - { - v << QPointF(pt1.x() + dArrowLen * qCos(dAng + dArrowAng), pt1.y() - dArrowLen * qSin(dAng + dArrowAng)); - v << QPointF(pt1.x() + dArrowLen * qCos(dAng - dArrowAng), pt1.y() - dArrowLen * qSin(dAng - dArrowAng)); - v << pt1; - } - - v << pt2; - - if (geom.GetArrowType() != ArrowType::atRear) - { - dAng += M_PI; - - v << QPointF(pt2.x() + dArrowLen * qCos(dAng + dArrowAng), pt2.y() - dArrowLen * qSin(dAng + dArrowAng)); - v << QPointF(pt2.x() + dArrowLen * qCos(dAng - dArrowAng), pt2.y() - dArrowLen * qSin(dAng - dArrowAng)); - v << pt2; - } - - QScopedPointer item(GetMainPathItem()); - d->grainlinePoints = CorrectPosition(item->boundingRect(), v); + d->grainlinePoints = v; } //---------------------------------------------------------------------------------------------------------------------