Seamly2D
Code documentation
anchorpoint_tool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file anchorpoint_tool.cpp
3  ** @author Douglas S Caskey
4  ** @date Jan 3, 2023
5  **
6  ** @copyright
7  ** Copyright (C) 2017 - 2023 Seamly, LLC
8  ** https://github.com/fashionfreedom/seamly2d
9  **
10  ** @brief
11  ** Seamly2D is free software: you can redistribute it and/or modify
12  ** it under the terms of the GNU General Public License as published by
13  ** the Free Software Foundation, either version 3 of the License, or
14  ** (at your option) any later version.
15  **
16  ** Seamly2D is distributed in the hope that it will be useful,
17  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  ** GNU General Public License for more details.
20  **
21  ** You should have received a copy of the GNU General Public License
22  ** along with Seamly2D. if not, see <http://www.gnu.org/licenses/>.
23  **************************************************************************/
24 
25 /************************************************************************
26  **
27  ** @file vtoolpin.cpp
28  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
29  ** @date 31 1, 2017
30  **
31  ** @brief
32  ** @copyright
33  ** This source code is part of the Valentina project, a pattern making
34  ** program, whose allow create and modeling patterns of clothing.
35  ** Copyright (C) 2017 Valentina project
36  ** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
37  **
38  ** Valentina is free software: you can redistribute it and/or modify
39  ** it under the terms of the GNU General Public License as published by
40  ** the Free Software Foundation, either version 3 of the License, or
41  ** (at your option) any later version.
42  **
43  ** Valentina is distributed in the hope that it will be useful,
44  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
45  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46  ** GNU General Public License for more details.
47  **
48  ** You should have received a copy of the GNU General Public License
49  ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
50  **
51  *************************************************************************/
52 
53 #include "anchorpoint_tool.h"
54 #include "../../dialogs/tools/anchorpoint_dialog.h"
55 #include "../../undocommands/savepieceoptions.h"
56 #include "../pattern_piece_tool.h"
57 #include "../vgeometry/vpointf.h"
58 
59 const QString AnchorPointTool::ToolType = QStringLiteral("anchor");
60 
61 //---------------------------------------------------------------------------------------------------------------------
63 {
64  SCASSERT(not dialog.isNull());
65  QSharedPointer<AnchorPointDialog> dialogTool = dialog.objectCast<AnchorPointDialog>();
66  SCASSERT(not dialogTool.isNull())
67  const quint32 pointId = dialogTool->GetPointId();
68  const quint32 pieceId = dialogTool->GetPieceId();
69 
70  return Create(0, pointId, pieceId, doc, data, Document::FullParse, Source::FromGui);
71 }
72 
73 //---------------------------------------------------------------------------------------------------------------------
74 AnchorPointTool *AnchorPointTool::Create(quint32 _id, quint32 pointId, quint32 pieceId, VAbstractPattern *doc, VContainer *data,
75  const Document &parse, const Source &typeCreation, const QString &blockName,
76  const quint32 &idTool)
77 {
78  quint32 id = _id;
79  if (typeCreation == Source::FromGui)
80  {
81  id = CreateNode<VPointF>(data, pointId);
82  }
83  else
84  {
86  try
87  {
88  point = data->GeometricObject<VPointF>(pointId);
89  }
90  catch (const VExceptionBadId &e)
91  { // Possible case. Parent was deleted, but the node object is still here.
92  Q_UNUSED(e)
93  data->UpdateId(id);
94  return nullptr;// Just ignore
95  }
96  VPointF *anchorPoint = new VPointF(*point);
97  anchorPoint->setMode(Draw::Modeling);
98  data->UpdateGObject(id, anchorPoint);
99  if (parse != Document::FullParse)
100  {
101  doc->UpdateToolData(id, data);
102  }
103  }
105  AnchorPointTool *point = nullptr;
106  if (parse == Document::FullParse)
107  {
108  point = new AnchorPointTool(doc, data, id, pointId, pieceId, typeCreation, blockName, idTool, doc);
109 
110  VAbstractPattern::AddTool(id, point);
111  if (idTool != NULL_ID)
112  {
113  //Some nodes we don't show on scene. Tool that create this nodes must free memory.
115  SCASSERT(tool != nullptr)
116  point->setParent(tool);// Adopted by a tool
117  }
118  else
119  {
120  // Help to delete the node before each FullParse
121  doc->AddToolOnRemove(point);
122  }
123  }
124  else
125  {
126  doc->UpdateToolData(id, data);
127  }
128  return point;
129 }
130 
131 //---------------------------------------------------------------------------------------------------------------------
133 {
135 }
136 
137 //---------------------------------------------------------------------------------------------------------------------
138 void AnchorPointTool::AllowHover(bool enabled)
139 {
140  Q_UNUSED(enabled)
141  // do nothing
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
146 {
147  Q_UNUSED(enabled)
148  // do nothing
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
153 {
154  QDomElement domElement = doc->createElement(getTagName());
155 
156  doc->SetAttribute(domElement, VDomDocument::AttrId, m_id);
157  doc->SetAttribute(domElement, AttrType, ToolType);
158  doc->SetAttribute(domElement, AttrIdObject, idNode);
159  if (idTool != NULL_ID)
160  {
161  doc->SetAttribute(domElement, AttrIdTool, idTool);
162  }
163 
164  AddToModeling(domElement);
165 
166  if (m_pieceId > NULL_ID)
167  {
168  const VPiece oldPiece = VAbstractTool::data.GetPiece(m_pieceId);
169  VPiece newPiece = oldPiece;
170 
171  newPiece.getAnchors().append(m_id);
172 
173  SavePieceOptions *saveCommand = new SavePieceOptions(oldPiece, newPiece, doc, m_pieceId);
174  qApp->getUndoStack()->push(saveCommand);// First push then make a connect
175  VAbstractTool::data.UpdatePiece(m_pieceId, newPiece);// Update piece because first save will not call lite update
177  }
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
181 AnchorPointTool::AnchorPointTool(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 pointId, quint32 pieceId,
182  const Source &typeCreation, const QString &blockName, const quint32 &idTool, QObject *qoParent)
183  : VAbstractNode(doc, data, id, pointId, blockName, idTool, qoParent)
184  , m_pieceId(pieceId)
185 {
186  ToolCreation(typeCreation);
187 }
virtual QString getTagName() const Q_DECL_OVERRIDE
static AnchorPointTool * Create(QSharedPointer< DialogTool > dialog, VAbstractPattern *doc, VContainer *data)
static const QString ToolType
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
virtual void AddToFile() Q_DECL_OVERRIDE
AddToFile add tag with Information about tool into file.
AnchorPointTool(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 pointId, quint32 pieceId, const Source &typeCreation, const QString &blockName=QString(), const quint32 &idTool=0, QObject *qoParent=nullptr)
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
The VAbstractNode class parent class for all detail node.
Definition: vabstractnode.h:71
static const QString AttrIdTool
Definition: vabstractnode.h:77
quint32 idTool
idTool id tool.
Definition: vabstractnode.h:97
quint32 idNode
idNodenode id.
Definition: vabstractnode.h:94
virtual void ToolCreation(const Source &typeCreation) Q_DECL_OVERRIDE
void AddToModeling(const QDomElement &domElement)
AddToModeling add tag to modeling tag current pattern peace.
virtual void LiteParseTree(const Document &parse)=0
void AddToolOnRemove(VDataTool *tool)
virtual void UpdateToolData(const quint32 &id, VContainer *data)=0
static void AddTool(quint32 id, VDataTool *tool)
AddTool add tool to list tools.
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
static const QString TagPoint
VAbstractPattern * doc
doc dom document container
static void AddRecord(const quint32 id, const Tool &toolType, VAbstractPattern *doc)
AddRecord add record about tool in history.
const quint32 m_id
id object id.
The VContainer class container of all variables.
Definition: vcontainer.h:141
void UpdatePiece(quint32 id, const VPiece &piece)
Definition: vcontainer.cpp:510
static void UpdateId(quint32 newId)
UpdateId update id. If new id bigger when current save new like current.
Definition: vcontainer.cpp:269
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
void UpdateGObject(quint32 id, T *obj)
UpdateGObject update GObject by id.
Definition: vcontainer.h:374
VPiece GetPiece(quint32 id) const
Definition: vcontainer.cpp:185
The VDataTool class need for getting access to data container of tool.
Definition: vdatatool.h:71
VContainer data
data container with data
Definition: vdatatool.h:84
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
SetAttribute set attribute in pattern file. Replace "," by ".".
Definition: vdomdocument.h:185
static const QString AttrId
Definition: vdomdocument.h:108
The VExceptionBadId class for exception bad id.
void setMode(const Draw &value)
setMode set mode creation.
Definition: vgobject.cpp:178
Definition: vpiece.h:88
QVector< quint32 > getAnchors() const
Definition: vpiece.cpp:462
The VPointF class keep data of point.
Definition: vpointf.h:75
void NeedLiteParsing(const Document &parse)
#define SCASSERT(cond)
Definition: def.h:317
@ AnchorPoint
Source
Definition: def.h:106
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrIdObject
Definition: ifcdef.cpp:148
#define NULL_ID
Definition: ifcdef.h:76
Document
#define qApp
Definition: vapplication.h:67
@ Modeling