From f3c0af7294bf8bd7f81ace947f4fc6602b22be03 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 27 May 2021 19:52:37 +0300 Subject: [PATCH] Improve creating a piece icon. Add more information about contour: seam line, seam allowance, notches, grainline, internal paths. --- src/app/puzzle/vpcarrouselpiece.cpp | 14 +--- src/app/puzzle/vpgraphicspiece.cpp | 4 +- src/app/puzzle/vppiece.cpp | 2 +- src/libs/vdxf/vdxfengine.cpp | 6 +- src/libs/vlayout/vlayoutpaper.cpp | 2 +- src/libs/vlayout/vlayoutpiece.cpp | 103 +++++++++++++++++++++++----- src/libs/vlayout/vlayoutpiece.h | 11 ++- src/libs/vlayout/vposition.cpp | 10 +-- 8 files changed, 108 insertions(+), 44 deletions(-) diff --git a/src/app/puzzle/vpcarrouselpiece.cpp b/src/app/puzzle/vpcarrouselpiece.cpp index 7fc443003..49e973338 100644 --- a/src/app/puzzle/vpcarrouselpiece.cpp +++ b/src/app/puzzle/vpcarrouselpiece.cpp @@ -76,16 +76,7 @@ void VPCarrouselPiece::RefreshSelection() //--------------------------------------------------------------------------------------------------------------------- QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) const { - QVector points = m_piece->GetMappedContourPoints(); // seamline - if(points.isEmpty()) - { - points = m_piece->GetMappedSeamAllowancePoints(); // cutting line - } - - QPolygonF shape(points); - shape << shape.first(); - - QRectF boundingRect = shape.boundingRect(); + QRectF boundingRect = m_piece->DetailBoundingRect(); qreal canvasSize = qMax(boundingRect.height(), boundingRect.width()); QRectF canvas = QRectF(0, 0, canvasSize, canvasSize); @@ -147,7 +138,8 @@ QIcon VPCarrouselPiece::CreatePieceIcon(const QSize &size, bool isDragIcon) cons painter.setBrush(QBrush(Qt::white)); } - painter.drawPolygon(shape); + m_piece->DrawMiniature(painter); + painter.end(); icon.addPixmap(pixmap,iconMode); diff --git a/src/app/puzzle/vpgraphicspiece.cpp b/src/app/puzzle/vpgraphicspiece.cpp index 073fb75b8..f8e40a416 100644 --- a/src/app/puzzle/vpgraphicspiece.cpp +++ b/src/app/puzzle/vpgraphicspiece.cpp @@ -106,7 +106,7 @@ void VPGraphicsPiece::Init() // initialises the grainline if(m_piece->IsGrainlineEnabled()) { - QVector grainLinepoints = m_piece->GetGrainline(); + QVector grainLinepoints = m_piece->GetMappedGrainline(); if(!grainLinepoints.isEmpty()) { m_grainline.moveTo(grainLinepoints.first()); @@ -126,7 +126,7 @@ void VPGraphicsPiece::Init() } // initialises the passmarks - QVector passmarks = m_piece->GetPassmarks(); + QVector passmarks = m_piece->GetMappedPassmarks(); for(auto &passmark : passmarks) { for (auto &line : passmark.lines) diff --git a/src/app/puzzle/vppiece.cpp b/src/app/puzzle/vppiece.cpp index cb46ed570..17d04e5b9 100644 --- a/src/app/puzzle/vppiece.cpp +++ b/src/app/puzzle/vppiece.cpp @@ -54,7 +54,7 @@ VPPiece::VPPiece(VLayoutPiece layoutPiece): VLayoutPiece(layoutPiece) // then translate the piece so that the top left corner of the bouding rect of the piece is at the position // (0,0) in the sheet coordinate system - QRectF boundingRect = DetailBoundingRect(); + QRectF boundingRect = MappedDetailBoundingRect(); QPointF offset = boundingRect.topLeft(); matrix = GetMatrix(); matrix.translate(-offset.x() ,-offset.y()); diff --git a/src/libs/vdxf/vdxfengine.cpp b/src/libs/vdxf/vdxfengine.cpp index ad8887f86..a1860df04 100644 --- a/src/libs/vdxf/vdxfengine.cpp +++ b/src/libs/vdxf/vdxfengine.cpp @@ -771,7 +771,7 @@ void VDxfEngine::ExportAAMANotch(dx_ifaceBlock *detailBlock, const VLayoutPiece { if (detail.IsSeamAllowance()) { - const QVector passmarks = detail.GetPassmarks(); + const QVector passmarks = detail.GetMappedPassmarks(); for(auto &passmark : passmarks) { for (auto &line : passmark.lines) @@ -788,7 +788,7 @@ void VDxfEngine::ExportAAMANotch(dx_ifaceBlock *detailBlock, const VLayoutPiece //--------------------------------------------------------------------------------------------------------------------- void VDxfEngine::ExportAAMAGrainline(dx_ifaceBlock *detailBlock, const VLayoutPiece &detail) { - const QVector grainline = detail.GetGrainline(); + const QVector grainline = detail.GetMappedGrainline(); if (grainline.count() > 1) { if (DRW_Entity *e = AAMALine(QLineF(grainline.first(), grainline.last()), QChar('7'))) @@ -1053,7 +1053,7 @@ void VDxfEngine::ExportASTMNotch(dx_ifaceBlock *detailBlock, const VLayoutPiece { if (detail.IsSeamAllowance()) { - const QVector passmarks = detail.GetPassmarks(); + const QVector passmarks = detail.GetMappedPassmarks(); for(auto &passmark : passmarks) { DRW_ASTMNotch *notch = new DRW_ASTMNotch(); diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index c20b862ac..3253085a1 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -303,7 +303,7 @@ bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece d->globalContour.SetContour(newGContour); VCachedPositions positionChache; - QVector layoutPoints = workDetail.GetLayoutAllowancePoints(); + QVector layoutPoints = workDetail.GetMappedLayoutAllowancePoints(); positionChache.boundingRect = VLayoutPiece::BoundingRect(layoutPoints); positionChache.layoutAllowancePath = VAbstractPiece::PainterPath(layoutPoints); d->positionsCache.append(positionChache); diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index 93610ac51..f6e72ec56 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -530,7 +530,7 @@ VLayoutPiece VLayoutPiece::Create(const VPiece &piece, vidtype id, const VContai det.SetPriority(piece.GetPriority()); // Very important to set main path first! - if (det.ContourPath().isEmpty()) + if (det.MappedContourPath().isEmpty()) { throw VException (tr("Piece %1 doesn't have shape.").arg(piece.GetName())); } @@ -650,11 +650,17 @@ void VLayoutPiece::SetSeamAllowancePoints(const QVector &points, bool s } //--------------------------------------------------------------------------------------------------------------------- -QVector VLayoutPiece::GetLayoutAllowancePoints() const +QVector VLayoutPiece::GetMappedLayoutAllowancePoints() const { return Map(d->layoutAllowance); } +//--------------------------------------------------------------------------------------------------------------------- +QVector VLayoutPiece::GetLayoutAllowancePoints() const +{ + return d->layoutAllowance; +} + //--------------------------------------------------------------------------------------------------------------------- QPointF VLayoutPiece::GetPieceTextPosition() const { @@ -791,11 +797,17 @@ void VLayoutPiece::SetGrainline(const VGrainlineData& geom, const VContainer* pa } //--------------------------------------------------------------------------------------------------------------------- -QVector VLayoutPiece::GetGrainline() const +QVector VLayoutPiece::GetMappedGrainline() const { return Map(d->grainlinePoints); } +//--------------------------------------------------------------------------------------------------------------------- +QVector VLayoutPiece::GetGrainline() const +{ + return d->grainlinePoints; +} + //--------------------------------------------------------------------------------------------------------------------- bool VLayoutPiece::IsGrainlineEnabled() const { @@ -956,22 +968,29 @@ int VLayoutPiece::LayoutEdgeByPoint(const QPointF &p1) const } //--------------------------------------------------------------------------------------------------------------------- -QRectF VLayoutPiece::DetailBoundingRect() const +QRectF VLayoutPiece::MappedDetailBoundingRect() const { return IsSeamAllowance() && not IsSeamAllowanceBuiltIn() ? BoundingRect(GetMappedSeamAllowancePoints()) : BoundingRect(GetMappedContourPoints()); } //--------------------------------------------------------------------------------------------------------------------- -QRectF VLayoutPiece::LayoutBoundingRect() const +QRectF VLayoutPiece::DetailBoundingRect() const { - return BoundingRect(GetLayoutAllowancePoints()); + return IsSeamAllowance() && not IsSeamAllowanceBuiltIn() ? BoundingRect(GetSeamAllowancePoints()) : + BoundingRect(GetContourPoints()); +} + +//--------------------------------------------------------------------------------------------------------------------- +QRectF VLayoutPiece::MappedLayoutBoundingRect() const +{ + return BoundingRect(GetMappedLayoutAllowancePoints()); } //--------------------------------------------------------------------------------------------------------------------- qreal VLayoutPiece::Diagonal() const { - const QRectF rec = LayoutBoundingRect(); + const QRectF rec = MappedLayoutBoundingRect(); return qSqrt(pow(rec.height(), 2) + pow(rec.width(), 2)); } @@ -1037,11 +1056,17 @@ void VLayoutPiece::SetLayoutAllowancePoints() } //--------------------------------------------------------------------------------------------------------------------- -QVector VLayoutPiece::GetPassmarks() const +QVector VLayoutPiece::GetMappedPassmarks() const { return Map(d->passmarks); } +//--------------------------------------------------------------------------------------------------------------------- +QVector VLayoutPiece::GetPassmarks() const +{ + return d->passmarks; +} + //--------------------------------------------------------------------------------------------------------------------- void VLayoutPiece::SetPassmarks(const QVector &passmarks) { @@ -1091,6 +1116,12 @@ void VLayoutPiece::SetInternalPaths(const QVector &internalPat d->m_internalPaths = internalPaths; } +//--------------------------------------------------------------------------------------------------------------------- +QPainterPath VLayoutPiece::MappedContourPath() const +{ + return d->matrix.map(ContourPath()); +} + //--------------------------------------------------------------------------------------------------------------------- QPainterPath VLayoutPiece::ContourPath() const { @@ -1099,7 +1130,7 @@ QPainterPath VLayoutPiece::ContourPath() const // contour if (not IsHideMainPath() || not IsSeamAllowance() || IsSeamAllowanceBuiltIn()) { - path = PainterPath(GetMappedContourPoints()); + path = PainterPath(GetContourPoints()); } // seam allowance @@ -1108,7 +1139,7 @@ QPainterPath VLayoutPiece::ContourPath() const if (not IsSeamAllowanceBuiltIn()) { // Draw seam allowance - QVectorpoints = GetMappedSeamAllowancePoints(); + QVectorpoints = GetSeamAllowancePoints(); if (points.last().toPoint() != points.first().toPoint()) { @@ -1128,9 +1159,9 @@ QPainterPath VLayoutPiece::ContourPath() const // Draw passmarks QPainterPath passmaksPath; const QVector passmarks = GetPassmarks(); - for(auto &passmark : passmarks) + for(const auto &passmark : passmarks) { - for (auto &line : passmark.lines) + for (const auto &line : passmark.lines) { passmaksPath.moveTo(line.p1()); passmaksPath.lineTo(line.p2()); @@ -1145,9 +1176,43 @@ QPainterPath VLayoutPiece::ContourPath() const } //--------------------------------------------------------------------------------------------------------------------- -QPainterPath VLayoutPiece::LayoutAllowancePath() const +QPainterPath VLayoutPiece::MappedLayoutAllowancePath() const { - return PainterPath(GetLayoutAllowancePoints()); + return PainterPath(GetMappedLayoutAllowancePoints()); +} + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutPiece::DrawMiniature(QPainter &painter) +{ + painter.drawPath(ContourPath()); + + for (auto &path : d->m_internalPaths) + { + painter.save(); + + QPen pen = painter.pen(); + pen.setStyle(path.PenStyle()); + painter.setPen(pen); + + painter.drawPath(path.GetPainterPath()); + + painter.restore(); + } + + for (auto &label : d->m_placeLabels) + { + painter.drawPath(VPlaceLabelItem::LabelShapePath(label.shape)); + } + + QPainterPath path; + + QVector gPoints = GetGrainline(); + path.moveTo(gPoints.at(0)); + for (auto p : qAsConst(gPoints)) + { + path.lineTo(p); + } + painter.drawPath(path); } //--------------------------------------------------------------------------------------------------------------------- @@ -1157,7 +1222,7 @@ QGraphicsItem *VLayoutPiece::GetItem(bool textAsPaths) const for (auto &path : d->m_internalPaths) { - QGraphicsPathItem* pathItem = new QGraphicsPathItem(item); + auto* pathItem = new QGraphicsPathItem(item); pathItem->setPath(d->matrix.map(path.GetPainterPath())); QPen pen = pathItem->pen(); @@ -1166,9 +1231,9 @@ QGraphicsItem *VLayoutPiece::GetItem(bool textAsPaths) const pathItem->setPen(pen); } - for (auto &label : d->m_placeLabels) + for (const auto &label : d->m_placeLabels) { - QGraphicsPathItem* pathItem = new QGraphicsPathItem(item); + auto* pathItem = new QGraphicsPathItem(item); QPen pen = pathItem->pen(); pen.setWidthF(VAbstractApplication::VApp()->Settings()->WidthHairLine()); pathItem->setPen(pen); @@ -1341,7 +1406,7 @@ void VLayoutPiece::CreateGrainlineItem(QGraphicsItem *parent) const QPainterPath path; - QVector gPoints = GetGrainline(); + QVector gPoints = GetMappedGrainline(); path.moveTo(gPoints.at(0)); for (auto p : qAsConst(gPoints)) { @@ -1370,7 +1435,7 @@ QGraphicsPathItem *VLayoutPiece::GetMainItem() const QPen pen = item->pen(); pen.setWidthF(VAbstractApplication::VApp()->Settings()->WidthHairLine()); item->setPen(pen); - item->setPath(ContourPath()); + item->setPath(MappedContourPath()); return item; } diff --git a/src/libs/vlayout/vlayoutpiece.h b/src/libs/vlayout/vlayoutpiece.h index 77c5bc132..96eaaddf3 100644 --- a/src/libs/vlayout/vlayoutpiece.h +++ b/src/libs/vlayout/vlayoutpiece.h @@ -82,9 +82,11 @@ public: void SetSeamAllowancePoints(const QVector &points, bool seamAllowance = true, bool seamAllowanceBuiltIn = false); + QVector GetMappedLayoutAllowancePoints() const; QVector GetLayoutAllowancePoints() const; void SetLayoutAllowancePoints(); + QVector GetMappedPassmarks() const; QVector GetPassmarks() const; void SetPassmarks(const QVector &passmarks); @@ -105,6 +107,7 @@ public: const VContainer *pattern); void SetGrainline(const VGrainlineData& geom, const VContainer *pattern); + QVector GetMappedGrainline() const; QVector GetGrainline() const; bool IsGrainlineEnabled() const; qreal GrainlineAngle() const; @@ -137,8 +140,9 @@ public: QLineF LayoutEdge(int i) const; int LayoutEdgeByPoint(const QPointF &p1) const; + QRectF MappedDetailBoundingRect() const; QRectF DetailBoundingRect() const; - QRectF LayoutBoundingRect() const; + QRectF MappedLayoutBoundingRect() const; qreal Diagonal() const; static QRectF BoundingRect(QVector points); @@ -146,8 +150,11 @@ public: bool isNull() const; qint64 Square() const; + QPainterPath MappedContourPath() const; QPainterPath ContourPath() const; - QPainterPath LayoutAllowancePath() const; + QPainterPath MappedLayoutAllowancePath() const; + + void DrawMiniature(QPainter &painter); Q_REQUIRED_RESULT QGraphicsItem *GetItem(bool textAsPaths) const; diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index 6d4eb99be..c13cdb22c 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -299,7 +299,7 @@ void VPosition::SaveCandidate(VBestSquare &bestResult, const VLayoutPiece &detai QVector newGContour = m_data.gContour.UniteWithContour(detail, globalI, detJ, type); newGContour.append(newGContour.first()); const QSizeF size = QPolygonF(newGContour).boundingRect().size(); - const QRectF boundingRect = detail.DetailBoundingRect(); + const QRectF boundingRect = detail.MappedDetailBoundingRect(); const qreal depthPosition = m_data.isOriginPaperOrientationPortrait ? boundingRect.y() : boundingRect.x(); const qreal sidePosition = m_data.isOriginPaperOrientationPortrait ? boundingRect.x() : boundingRect.y(); @@ -332,7 +332,7 @@ bool VPosition::CheckCombineEdges(VLayoutPiece &detail, int j, int &dEdge) #endif CrossingType type = CrossingType::Intersection; - if (not detail.IsForceFlipping() && SheetContains(detail.DetailBoundingRect())) + if (not detail.IsForceFlipping() && SheetContains(detail.MappedDetailBoundingRect())) { if (not m_data.gContour.GetContour().isEmpty()) { @@ -375,7 +375,7 @@ bool VPosition::CheckCombineEdges(VLayoutPiece &detail, int j, int &dEdge) } CrossingType type = CrossingType::Intersection; - if (SheetContains(detail.DetailBoundingRect())) + if (SheetContains(detail.MappedDetailBoundingRect())) { type = Crossing(detail); } @@ -417,7 +417,7 @@ bool VPosition::CheckRotationEdges(VLayoutPiece &detail, int j, int dEdge, qreal #endif CrossingType type = CrossingType::Intersection; - if (SheetContains(detail.DetailBoundingRect())) + if (SheetContains(detail.MappedDetailBoundingRect())) { type = Crossing(detail); } @@ -469,7 +469,7 @@ VPosition::CrossingType VPosition::Crossing(const VLayoutPiece &detail) const return CrossingType::NoIntersection; } - const QVector layoutPoints = detail.GetLayoutAllowancePoints(); + const QVector layoutPoints = detail.GetMappedLayoutAllowancePoints(); const QRectF layoutBoundingRect = VLayoutPiece::BoundingRect(layoutPoints); const QPainterPath layoutAllowancePath = VAbstractPiece::PainterPath(layoutPoints);