From 51e670bd844bf92ecd9699346c95eee4b9f96a7e Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 26 Oct 2017 16:57:52 +0300 Subject: [PATCH] Duplicate from GUI. --HG-- branch : feature --- src/app/valentina/mainwindow.cpp | 3 +- src/libs/ifc/xml/vabstractpattern.cpp | 18 ++ src/libs/ifc/xml/vabstractpattern.h | 2 + .../vtools/tools/nodeDetails/vnodearc.cpp | 2 +- src/libs/vtools/tools/nodeDetails/vnodearc.h | 2 +- .../tools/nodeDetails/vnodeellipticalarc.cpp | 2 +- .../tools/nodeDetails/vnodeellipticalarc.h | 2 +- .../vtools/tools/nodeDetails/vnodepoint.cpp | 2 +- .../vtools/tools/nodeDetails/vnodepoint.h | 2 +- .../vtools/tools/nodeDetails/vnodespline.cpp | 2 +- .../vtools/tools/nodeDetails/vnodespline.h | 2 +- .../tools/nodeDetails/vnodesplinepath.cpp | 2 +- .../tools/nodeDetails/vnodesplinepath.h | 2 +- src/libs/vtools/tools/vabstracttool.h | 7 +- src/libs/vtools/tools/vtoolseamallowance.cpp | 211 +++++++++++++++++- src/libs/vtools/tools/vtoolseamallowance.h | 18 +- 16 files changed, 252 insertions(+), 27 deletions(-) diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 4298a9ddc..cae1e9574 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -1127,8 +1127,7 @@ void MainWindow::ClosedDialogDuplicateDetail(int result) VMainGraphicsScene *scene = qobject_cast(currentScene); SCASSERT(scene != nullptr) - QGraphicsItem *tool = dynamic_cast(VToolSeamAllowance::Duplicate(dialogTool, scene, doc, - pattern)); + QGraphicsItem *tool = dynamic_cast(VToolSeamAllowance::Duplicate(dialogTool, scene, doc)); // Do not check for nullptr! See issue #719. ui->view->itemClicked(tool); } diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index c17775469..86d190725 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -2420,3 +2420,21 @@ void VAbstractPattern::SetGroupVisivility(quint32 id, bool visible) qDebug("Can't get group by id = %u.", id); } } + +//--------------------------------------------------------------------------------------------------------------------- +QString VAbstractPattern::PieceDrawName(quint32 id) +{ + const QDomElement detail = elementById(id, VAbstractPattern::TagDetail); + if (detail.isNull()) + { + return QString(); + } + + const QDomElement draw = detail.parentNode().parentNode().toElement(); + if (draw.isNull() || not draw.hasAttribute(VAbstractPattern::AttrName)) + { + return QString(); + } + + return draw.attribute(VAbstractPattern::AttrName); +} diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index 920837731..d2ccf0d16 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -198,6 +198,8 @@ public: bool GetGroupVisivility(quint32 id); void SetGroupVisivility(quint32 id, bool visible); + QString PieceDrawName(quint32 id); + static const QString TagPattern; static const QString TagCalculation; static const QString TagModeling; diff --git a/src/libs/vtools/tools/nodeDetails/vnodearc.cpp b/src/libs/vtools/tools/nodeDetails/vnodearc.cpp index 13ae8beb6..6832ef4e8 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodearc.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodearc.cpp @@ -59,7 +59,7 @@ VNodeArc::VNodeArc(const VAbstractNodeInitData &initData, QObject *qoParent) * @brief Create help create tool. * @param initData init data. */ -void VNodeArc::Create(VAbstractNodeInitData initData) +void VNodeArc::Create(const VAbstractNodeInitData &initData) { if (initData.parse == Document::FullParse) { diff --git a/src/libs/vtools/tools/nodeDetails/vnodearc.h b/src/libs/vtools/tools/nodeDetails/vnodearc.h index 1be22d12a..b12f7c9ef 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodearc.h +++ b/src/libs/vtools/tools/nodeDetails/vnodearc.h @@ -47,7 +47,7 @@ class VNodeArc :public VAbstractNode { Q_OBJECT public: - static void Create(VAbstractNodeInitData initData); + static void Create(const VAbstractNodeInitData &initData); static const QString ToolType; virtual QString getTagName() const Q_DECL_OVERRIDE; diff --git a/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.cpp b/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.cpp index f79d6a24b..9aa9d43a6 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.cpp @@ -42,7 +42,7 @@ const QString VNodeEllipticalArc::ToolType = QStringLiteral("modeling"); //--------------------------------------------------------------------------------------------------------------------- -void VNodeEllipticalArc::Create(VAbstractNodeInitData initData) +void VNodeEllipticalArc::Create(const VAbstractNodeInitData &initData) { if (initData.parse == Document::FullParse) { diff --git a/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.h b/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.h index e392f6192..31af86c22 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.h +++ b/src/libs/vtools/tools/nodeDetails/vnodeellipticalarc.h @@ -44,7 +44,7 @@ class VNodeEllipticalArc :public VAbstractNode { Q_OBJECT public: - static void Create(VAbstractNodeInitData initData); + static void Create(const VAbstractNodeInitData &initData); static const QString ToolType; virtual QString getTagName() const Q_DECL_OVERRIDE; diff --git a/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp b/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp index 721907088..864e89adf 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodepoint.cpp @@ -85,7 +85,7 @@ VNodePoint::VNodePoint(const VAbstractNodeInitData &initData, QObject *qoParent, * @brief Create help create tool. * @param initData init data. */ -void VNodePoint::Create(VAbstractNodeInitData initData) +void VNodePoint::Create(const VAbstractNodeInitData &initData) { if (initData.parse == Document::FullParse) { diff --git a/src/libs/vtools/tools/nodeDetails/vnodepoint.h b/src/libs/vtools/tools/nodeDetails/vnodepoint.h index 30ee4380d..ee550ab94 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodepoint.h +++ b/src/libs/vtools/tools/nodeDetails/vnodepoint.h @@ -50,7 +50,7 @@ class VNodePoint: public VAbstractNode, public VScenePoint { Q_OBJECT public: - static void Create(VAbstractNodeInitData initData); + static void Create(const VAbstractNodeInitData &initData); static const QString ToolType; virtual int type() const Q_DECL_OVERRIDE {return Type;} diff --git a/src/libs/vtools/tools/nodeDetails/vnodespline.cpp b/src/libs/vtools/tools/nodeDetails/vnodespline.cpp index ee75c99c7..6fa3c46f8 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodespline.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodespline.cpp @@ -60,7 +60,7 @@ VNodeSpline::VNodeSpline(const VAbstractNodeInitData &initData, QObject *qoParen * @param initData init data. * @return pointer to node. */ -VNodeSpline *VNodeSpline::Create(VAbstractNodeInitData initData) +VNodeSpline *VNodeSpline::Create(const VAbstractNodeInitData &initData) { VNodeSpline *spl = nullptr; if (initData.parse == Document::FullParse) diff --git a/src/libs/vtools/tools/nodeDetails/vnodespline.h b/src/libs/vtools/tools/nodeDetails/vnodespline.h index 5f2c82690..e7336936a 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodespline.h +++ b/src/libs/vtools/tools/nodeDetails/vnodespline.h @@ -47,7 +47,7 @@ class VNodeSpline:public VAbstractNode { Q_OBJECT public: - static VNodeSpline *Create(VAbstractNodeInitData initData); + static VNodeSpline *Create(const VAbstractNodeInitData &initData); static const QString ToolType; virtual QString getTagName() const Q_DECL_OVERRIDE; diff --git a/src/libs/vtools/tools/nodeDetails/vnodesplinepath.cpp b/src/libs/vtools/tools/nodeDetails/vnodesplinepath.cpp index 201f6eed5..d9b050d83 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodesplinepath.cpp +++ b/src/libs/vtools/tools/nodeDetails/vnodesplinepath.cpp @@ -59,7 +59,7 @@ VNodeSplinePath::VNodeSplinePath(const VAbstractNodeInitData &initData, QObject * @brief Create help create tool. * @param initData init data. */ -void VNodeSplinePath::Create(VAbstractNodeInitData initData) +void VNodeSplinePath::Create(const VAbstractNodeInitData &initData) { if (initData.parse == Document::FullParse) { diff --git a/src/libs/vtools/tools/nodeDetails/vnodesplinepath.h b/src/libs/vtools/tools/nodeDetails/vnodesplinepath.h index 2bbc0bc40..5b71f26fb 100644 --- a/src/libs/vtools/tools/nodeDetails/vnodesplinepath.h +++ b/src/libs/vtools/tools/nodeDetails/vnodesplinepath.h @@ -47,7 +47,7 @@ class VNodeSplinePath : public VAbstractNode { Q_OBJECT public: - static void Create(VAbstractNodeInitData initData); + static void Create(const VAbstractNodeInitData &initData); static const QString ToolType; virtual QString getTagName() const Q_DECL_OVERRIDE; diff --git a/src/libs/vtools/tools/vabstracttool.h b/src/libs/vtools/tools/vabstracttool.h index 585de3903..bb2c0fce1 100644 --- a/src/libs/vtools/tools/vabstracttool.h +++ b/src/libs/vtools/tools/vabstracttool.h @@ -110,8 +110,6 @@ public: virtual void ShowVisualization(bool show) =0; virtual void ChangeLabelPosition(quint32 id, const QPointF &pos); - template - static quint32 CreateNode(VContainer *data, quint32 id); public slots: /** * @brief FullUpdateFromFile update tool data form file. @@ -162,6 +160,11 @@ protected: virtual void DeleteToolWithConfirm(bool ask = true); static int ConfirmDeletion(); + template + static quint32 CreateNode(VContainer *data, quint32 id); + static quint32 CreateNodeSpline(VContainer *data, quint32 id); + static quint32 CreateNodeSplinePath(VContainer *data, quint32 id); + template void AddVisualization(); diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index c9009fecf..7188f5a61 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -34,12 +34,7 @@ #include "../vpatterndb/calculator.h" #include "../vpatterndb/floatItemData/vpatternlabeldata.h" #include "../vpatterndb/floatItemData/vpiecelabeldata.h" -#include "nodeDetails/vnodearc.h" -#include "nodeDetails/vnodeellipticalarc.h" -#include "nodeDetails/vnodepoint.h" -#include "nodeDetails/vnodespline.h" -#include "nodeDetails/vnodesplinepath.h" -#include "nodeDetails/vtoolpiecepath.h" +#include "nodeDetails/nodedetails.h" #include "../vgeometry/varc.h" #include "../vgeometry/vellipticalarc.h" #include "../vgeometry/vcubicbezier.h" @@ -47,6 +42,7 @@ #include "../vgeometry/vpointf.h" #include "../vgeometry/vspline.h" #include "../vgeometry/vsplinepath.h" +#include "../vgeometry/vplacelabelitem.h" #include "../ifc/xml/vpatternconverter.h" #include "../undocommands/addpiece.h" #include "../undocommands/deletepiece.h" @@ -154,7 +150,7 @@ VToolSeamAllowance *VToolSeamAllowance::Create(VToolSeamAllowanceInitData &initD //--------------------------------------------------------------------------------------------------------------------- VToolSeamAllowance *VToolSeamAllowance::Duplicate(QSharedPointer dialog, VMainGraphicsScene *scene, - VAbstractPattern *doc, VContainer *data) + VAbstractPattern *doc) { SCASSERT(not dialog.isNull()); QSharedPointer dialogTool = dialog.objectCast(); @@ -163,22 +159,37 @@ VToolSeamAllowance *VToolSeamAllowance::Duplicate(QSharedPointer dia VToolSeamAllowanceInitData initData; initData.scene = scene; initData.doc = doc; - initData.data = data; initData.parse = Document::FullParse; initData.typeCreation = Source::FromGui; + initData.drawName = doc->PieceDrawName(dialogTool->Duplicate()); -// initData.detail = dialogTool->GetPiece(); + VContainer toolData = VAbstractPattern::getTool(dialogTool->Duplicate())->getData(); + initData.data = &toolData; + + VPiece detail = initData.data->GetPiece(dialogTool->Duplicate()); + detail.SetMx(dialogTool->MoveDuplicateX()); + detail.SetMy(dialogTool->MoveDuplicateY()); + initData.detail = detail; initData.width = initData.detail.GetFormulaSAWidth(); -// initData.detail.GetPath().SetNodes(PrepareNodes(initData.detail.GetPath(), scene, doc, data)); - return Duplicate(initData); } //--------------------------------------------------------------------------------------------------------------------- VToolSeamAllowance *VToolSeamAllowance::Duplicate(VToolSeamAllowanceInitData &initData) { - return nullptr; + VPiece dupDetail = initData.detail; + + QMap replacements; + dupDetail.GetPath().SetNodes(DuplicateNodes(initData.detail.GetPath(), initData, replacements)); + dupDetail.SetCustomSARecords(DuplicateCustomSARecords(initData.detail.GetCustomSARecords(), initData, + replacements)); + dupDetail.SetInternalPaths(DuplicateInternalPaths(initData.detail.GetInternalPaths(), initData)); + dupDetail.SetPins(DuplicatePins(initData.detail.GetPins(), initData)); + dupDetail.SetPlaceLabels(DuplicatePlaceLabels(initData.detail.GetPlaceLabels(), initData)); + + initData.detail = dupDetail; + return VToolSeamAllowance::Create(initData); } //--------------------------------------------------------------------------------------------------------------------- @@ -1766,3 +1777,179 @@ void VToolSeamAllowance::AddPointRecords(VAbstractPattern *doc, QDomElement &dom domElement.appendChild(pinsElement); } } + +//--------------------------------------------------------------------------------------------------------------------- +quint32 VToolSeamAllowance::DuplicateNode(const VPieceNode &node, const VToolSeamAllowanceInitData &initData) +{ + SCASSERT(initData.scene != nullptr) + SCASSERT(initData.doc != nullptr) + SCASSERT(initData.data != nullptr) + + const QSharedPointer gobj = initData.data->GetGObject(node.GetId()); + + VAbstractNodeInitData initNodeData; + initNodeData.idObject = gobj->getIdObject(); + initNodeData.doc = initData.doc; + initNodeData.data = initData.data; + initNodeData.parse = Document::FullParse; + initNodeData.typeCreation = Source::FromGui; + initNodeData.scene = initData.scene; + initNodeData.drawName = initData.drawName; + + switch (node.GetTypeTool()) + { + case (Tool::NodePoint): + initNodeData.id = VAbstractTool::CreateNode(initData.data, gobj->getIdObject()); + VNodePoint::Create(initNodeData); + break; + case (Tool::NodeArc): + initNodeData.id = VAbstractTool::CreateNode(initData.data, gobj->getIdObject()); + VNodeArc::Create(initNodeData); + break; + case (Tool::NodeElArc): + initNodeData.id = VAbstractTool::CreateNode(initData.data, gobj->getIdObject()); + VNodeEllipticalArc::Create(initNodeData); + break; + case (Tool::NodeSpline): + initNodeData.id = VAbstractTool::CreateNodeSpline(initData.data, gobj->getIdObject()); + VNodeSpline::Create(initNodeData); + break; + case (Tool::NodeSplinePath): + initNodeData.id = VAbstractTool::CreateNodeSplinePath(initData.data, gobj->getIdObject()); + VNodeSplinePath::Create(initNodeData); + break; + default: + qDebug()<<"May be wrong tool type!!! Ignoring."<GetPiecePath(id); + VPiecePath newPath = path; + QMap recordReplacements; // Not used + newPath.SetNodes(DuplicateNodes(path, initData, recordReplacements)); + + const quint32 idPath = initData.data->AddPiecePath(newPath); + + VToolPiecePathInitData initNodeData; + initNodeData.id = idPath; + initNodeData.idObject = NULL_ID; // piece id + initNodeData.scene = initData.scene; + initNodeData.doc = initData.doc; + initNodeData.data = initData.data; + initNodeData.parse = Document::FullParse; + initNodeData.typeCreation = Source::FromTool; + initNodeData.drawName = initData.drawName; + initNodeData.path = newPath; + + VToolPiecePath::Create(initNodeData); + return idPath; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VToolSeamAllowance::DuplicateCustomSARecords(const QVector &records, + const VToolSeamAllowanceInitData &initData, + const QMap &replacements) +{ + QVector newRecords; + for(int i=0; i < records.size(); ++i) + { + CustomSARecord record = records.at(i); + record.path = DuplicatePiecePath(record.path, initData); + record.startPoint = replacements.value(record.startPoint, NULL_ID); + record.endPoint = replacements.value(record.endPoint, NULL_ID); + newRecords.append(record); + } + return newRecords; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VToolSeamAllowance::DuplicateInternalPaths(const QVector &iPaths, + const VToolSeamAllowanceInitData &initData) +{ + QVector newPaths; + for(int i=0; i < iPaths.size(); ++i) + { + newPaths.append(DuplicatePiecePath(iPaths.at(i), initData)); + } + return newPaths; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VToolSeamAllowance::DuplicatePins(const QVector &pins, + const VToolSeamAllowanceInitData &initData) +{ + QVector newPins; + for(int i=0; i < pins.size(); ++i) + { + QSharedPointer pin = initData.data->GeometricObject(pins.at(i)); + + VToolPinInitData initNodeData; + initNodeData.id = initData.data->AddGObject(new VPointF(*pin)); + initNodeData.pointId = pin->getIdObject(); + initNodeData.idObject = NULL_ID; // piece id + initNodeData.doc = initData.doc; + initNodeData.data = initData.data; + initNodeData.parse = Document::FullParse; + initNodeData.typeCreation = Source::FromTool; + initNodeData.drawName = initData.drawName; + + VToolPin::Create(initNodeData); + newPins.append(initNodeData.id); + } + return newPins; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VToolSeamAllowance::DuplicatePlaceLabels(const QVector &placeLabels, + const VToolSeamAllowanceInitData &initData) +{ + QVector newPlaceLabels; + for(int i=0; i < placeLabels.size(); ++i) + { + QSharedPointer label = initData.data->GeometricObject(placeLabels.at(i)); + + VToolPlaceLabelInitData initNodeData; + initNodeData.idObject = NULL_ID; // piece id + initNodeData.doc = initData.doc; + initNodeData.data = initData.data; + initNodeData.parse = Document::FullParse; + initNodeData.typeCreation = Source::FromTool; + initNodeData.drawName = initData.drawName; + initNodeData.width = label->GetWidthFormula(); + initNodeData.height = label->GetHeightFormula(); + initNodeData.angle = label->GetAngleFormula(); + initNodeData.type = label->GetLabelType(); + initNodeData.centerPoint = label->GetCenterPoint(); + initNodeData.id = initNodeData.data->AddGObject(new VPlaceLabelItem(*label)); + + VToolPlaceLabel::Create(initNodeData); + newPlaceLabels.append(initNodeData.id); + } + return newPlaceLabels; +} + +//--------------------------------------------------------------------------------------------------------------------- +QVector VToolSeamAllowance::DuplicateNodes(const VPiecePath &path, + const VToolSeamAllowanceInitData &initData, + QMap &replacements) +{ + QVector nodes; + for (int i = 0; i< path.CountNodes(); ++i) + { + VPieceNode nodeD = path.at(i); + const quint32 oldId = nodeD.GetId(); + const quint32 id = DuplicateNode(nodeD, initData); + if (id > NULL_ID) + { + nodeD.SetId(id); + nodes.append(nodeD); + replacements.insert(oldId, id); + } + } + return nodes; +} diff --git a/src/libs/vtools/tools/vtoolseamallowance.h b/src/libs/vtools/tools/vtoolseamallowance.h index c13742e52..55aea4aae 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.h +++ b/src/libs/vtools/tools/vtoolseamallowance.h @@ -65,7 +65,7 @@ public: VAbstractPattern *doc, VContainer *data); static VToolSeamAllowance* Create(VToolSeamAllowanceInitData &initData); static VToolSeamAllowance* Duplicate(QSharedPointer dialog, VMainGraphicsScene *scene, - VAbstractPattern *doc, VContainer *data); + VAbstractPattern *doc); static VToolSeamAllowance* Duplicate(VToolSeamAllowanceInitData &initData); static const quint8 pieceVersion; @@ -200,6 +200,22 @@ private: static void AddPointRecords(VAbstractPattern *doc, QDomElement &domElement, const QVector &records, const QString &tag); + + static QVector DuplicateNodes(const VPiecePath &path, const VToolSeamAllowanceInitData &initData, + QMap &replacements); + static quint32 DuplicateNode(const VPieceNode &node, const VToolSeamAllowanceInitData &initData); + + static quint32 DuplicatePiecePath(quint32 id, const VToolSeamAllowanceInitData &initData); + + static QVector DuplicateCustomSARecords(const QVector &records, + const VToolSeamAllowanceInitData &initData, + const QMap &replacements); + + static QVector DuplicateInternalPaths(const QVector &iPaths, + const VToolSeamAllowanceInitData &initData); + static QVector DuplicatePins(const QVector &pins, const VToolSeamAllowanceInitData &initData); + static QVector DuplicatePlaceLabels(const QVector &placeLabels, + const VToolSeamAllowanceInitData &initData); }; #endif // VTOOLSEAMALLOWANCE_H