1 /************************************************************************
2  **
3  **  @file
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   31 1, 2017
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2017 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "vtoolpin.h"
30 #include "../../dialogs/tools/piece/dialogpin.h"
31 #include "../../undocommands/savepieceoptions.h"
32 #include "../vtoolseamallowance.h"
33 #include "../vgeometry/vpointf.h"
34 
35 const QString VToolPin::ToolType = QStringLiteral("pin");
36 
37 //---------------------------------------------------------------------------------------------------------------------
Create(const QPointer<DialogTool> & dialog,VAbstractPattern * doc,VContainer * data)38 VToolPin *VToolPin::Create(const QPointer<DialogTool> &dialog, VAbstractPattern *doc, VContainer *data)
39 {
40     SCASSERT(not dialog.isNull());
41     const QPointer<DialogPin> dialogTool = qobject_cast<DialogPin *>(dialog);
42     SCASSERT(not dialogTool.isNull())
43 
44     VToolPinInitData initData;
45     initData.pointId = dialogTool->GetPointId();
46     initData.idObject = dialogTool->GetPieceId();
47     initData.doc = doc;
48     initData.data = data;
49     initData.parse = Document::FullParse;
50     initData.typeCreation = Source::FromGui;
51 
52     return Create(initData);
53 }
54 
55 //---------------------------------------------------------------------------------------------------------------------
Create(VToolPinInitData initData)56 VToolPin *VToolPin::Create(VToolPinInitData initData)
57 {
58     if (initData.typeCreation == Source::FromGui)
59     {
60         initData.id = CreateNode<VPointF>(initData.data, initData.pointId);
61     }
62     else
63     {
64         QSharedPointer<VPointF> point;
65         try
66         {
67             point = initData.data->GeometricObject<VPointF>(initData.pointId);
68         }
69         catch (const VExceptionBadId &e)
70         { // Possible case. Parent was deleted, but the node object is still here.
71             Q_UNUSED(e)
72             initData.data->UpdateId(initData.id);
73             return nullptr;// Just ignore
74         }
75         VPointF *pinPoint = new VPointF(*point);
76         pinPoint->setIdObject(initData.pointId);
77         pinPoint->setMode(Draw::Modeling);
78         initData.data->UpdateGObject(initData.id, pinPoint);
79         if (initData.parse != Document::FullParse)
80         {
81             initData.doc->UpdateToolData(initData.id, initData.data);
82         }
83     }
84     VAbstractTool::AddRecord(initData.id, Tool::Pin, initData.doc);
85     VToolPin *point = nullptr;
86     if (initData.parse == Document::FullParse)
87     {
88         point = new VToolPin(initData);
89 
90         VAbstractPattern::AddTool(initData.id, point);
91         if (initData.idTool != NULL_ID)
92         {
93             //Some nodes we don't show on scene. Tool that create this nodes must free memory.
94             VDataTool *tool = VAbstractPattern::getTool(initData.idTool);
95             SCASSERT(tool != nullptr)
96             point->setParent(tool);// Adopted by a tool
97         }
98         else
99         {
100             // Help to delete the node before each FullParse
101             initData.doc->AddToolOnRemove(point);
102         }
103     }
104     else
105     {
106         initData.doc->UpdateToolData(initData.id, initData.data);
107     }
108     return point;
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
getTagName() const112 QString VToolPin::getTagName() const
113 {
114     return VAbstractPattern::TagPoint;
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
AllowHover(bool enabled)118 void VToolPin::AllowHover(bool enabled)
119 {
120     Q_UNUSED(enabled)
121     // do nothing
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
AllowSelecting(bool enabled)125 void VToolPin::AllowSelecting(bool enabled)
126 {
127     Q_UNUSED(enabled)
128     // do nothing
129 }
130 
131 //---------------------------------------------------------------------------------------------------------------------
AddToFile()132 void VToolPin::AddToFile()
133 {
134     QDomElement domElement = doc->createElement(getTagName());
135 
136     doc->SetAttribute(domElement, VDomDocument::AttrId, m_id);
137     doc->SetAttribute(domElement, AttrType, ToolType);
138     doc->SetAttribute(domElement, AttrIdObject, idNode);
139     if (idTool != NULL_ID)
140     {
141         doc->SetAttribute(domElement, AttrIdTool, idTool);
142     }
143 
144     AddToModeling(domElement);
145 
146     if (m_pieceId > NULL_ID)
147     {
148         const VPiece oldDet = VAbstractTool::data.GetPiece(m_pieceId);
149         VPiece newDet = oldDet;
150 
151         newDet.GetPins().append(m_id);
152         incrementReferens(); // Manually increment reference since in this case a piece tool will not do this for us
153 
154         VAbstractApplication::VApp()->getUndoStack()->push(new SavePieceOptions(oldDet, newDet, doc, m_pieceId));
155     }
156 }
157 
158 //---------------------------------------------------------------------------------------------------------------------
VToolPin(const VToolPinInitData & initData,QObject * qoParent)159 VToolPin::VToolPin(const VToolPinInitData &initData, QObject *qoParent)
160     : VAbstractNode(initData.doc, initData.data, initData.id, initData.pointId, initData.drawName, initData.idTool,
161                     qoParent),
162       m_pieceId(initData.idObject)
163 {
164     ToolCreation(initData.typeCreation);
165 }
166