Seamly2D
Code documentation
vtoolinternalpath.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 24 11, 2016
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2016 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vtoolinternalpath.h"
53 #include "../../dialogs/tools/piece/dialoginternalpath.h"
54 #include "../vpatterndb/vpiecepath.h"
55 #include "../vpatterndb/vpiecenode.h"
56 #include "../../undocommands/savepieceoptions.h"
57 #include "../vmisc/vcommonsettings.h"
58 #include "../pattern_piece_tool.h"
59 
60 //---------------------------------------------------------------------------------------------------------------------
62  VAbstractPattern *doc, VContainer *data)
63 {
64  SCASSERT(not dialog.isNull());
65  QSharedPointer<DialogInternalPath> dialogTool = dialog.objectCast<DialogInternalPath>();
66  SCASSERT(not dialogTool.isNull())
67  VPiecePath path = dialogTool->GetPiecePath();
68  const quint32 pieceId = dialogTool->GetPieceId();
69  qApp->getUndoStack()->beginMacro("add path");
70  path.SetNodes(PrepareNodes(path, scene, doc, data));
71 
72  VToolInternalPath *pathTool = Create(0, path, pieceId, scene, doc, data, Document::FullParse, Source::FromGui);
73  return pathTool;
74 }
75 
76 //---------------------------------------------------------------------------------------------------------------------
77 VToolInternalPath *VToolInternalPath::Create(quint32 _id, const VPiecePath &path, quint32 pieceId, VMainGraphicsScene *scene,
78  VAbstractPattern *doc, VContainer *data, const Document &parse,
79  const Source &typeCreation, const QString &blockName, const quint32 &idTool)
80 {
81  quint32 id = _id;
82  if (typeCreation == Source::FromGui)
83  {
84  id = data->AddPiecePath(path);
85  }
86  else
87  {
88  data->UpdatePiecePath(id, path);
89  if (parse != Document::FullParse)
90  {
91  doc->UpdateToolData(id, data);
92  }
93  }
94 
95  if (parse == Document::FullParse)
96  {
98  //TODO Need create garbage collector and remove all nodes, that we don't use.
99  //Better check garbage before each saving file. Check only modeling tags.
100  VToolInternalPath *pathTool = new VToolInternalPath(doc, data, id, pieceId, typeCreation, blockName, idTool, doc);
101 
102  VAbstractPattern::AddTool(id, pathTool);
103  if (idTool != NULL_ID)
104  {
105  //Some nodes we don't show on scene. Tool that create this nodes must free memory.
107  SCASSERT(tool != nullptr);
108  pathTool->setParent(tool);// Adopted by a tool
109  }
110  else
111  {
112  if (typeCreation == Source::FromGui && path.GetType() == PiecePathType::InternalPath)
113  { // Seam allowance tool already initializated and can't init the path
114  SCASSERT(pieceId > NULL_ID);
115  PatternPieceTool *saTool = qobject_cast<PatternPieceTool*>(VAbstractPattern::getTool(pieceId));
116  SCASSERT(saTool != nullptr);
117  pathTool->setParentItem(saTool);
118  pathTool->SetParentType(ParentType::Item);
119  }
120  else
121  {
122  // Try to prevent memory leak
123  scene->addItem(pathTool);// First adopted by scene
124  pathTool->hide();// If no one will use node, it will stay hidden
125  pathTool->SetParentType(ParentType::Scene);
126  }
127  }
128  return pathTool;
129  }
130  return nullptr;
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135 {
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
140 void VToolInternalPath::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
141 {
142  qreal lineWeight = ToPixel(qApp->Settings()->getDefaultInternalLineweight(), Unit::Mm);
143 
144  QPen toolPen = pen();
145  toolPen.setWidthF(scaleWidth(lineWeight, sceneScale(scene())));
146  setPen(toolPen);
147 
148  QGraphicsPathItem::paint(painter, option, widget);
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
153 {
155  if (_referens == 1)
156  {
157  if (idTool != NULL_ID)
158  {
160  }
161  else
162  {
164  }
165  ShowNode();
166  QDomElement domElement = doc->elementById(m_id, getTagName());
167  if (domElement.isElement())
168  {
170  }
171  }
172 }
173 
174 //---------------------------------------------------------------------------------------------------------------------
176 {
178  if (_referens == 0)
179  {
180  if (idTool != NULL_ID)
181  {
183  }
184  else
185  {
187  }
188  HideNode();
189  QDomElement domElement = doc->elementById(m_id, getTagName());
190  if (domElement.isElement())
191  {
193  }
194  }
195 }
196 
197 //---------------------------------------------------------------------------------------------------------------------
198 void VToolInternalPath::AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiecePath &path)
199 {
200  doc->SetAttribute(domElement, VDomDocument::AttrId, id);
201  doc->SetAttribute(domElement, AttrName, path.GetName());
202  doc->SetAttribute(domElement, AttrType, static_cast<int>(path.GetType()));
204 
205  if (path.GetType() == PiecePathType::InternalPath)
206  {
207  doc->SetAttribute(domElement, AttrCut, path.IsCutPath());
208  }
209 }
210 
211 //---------------------------------------------------------------------------------------------------------------------
213 {
214  RefreshGeometry();
215 }
216 
217 //---------------------------------------------------------------------------------------------------------------------
219 {
220  Q_UNUSED(enabled)
221  // do nothing
222 }
223 
224 //---------------------------------------------------------------------------------------------------------------------
226 {
227  Q_UNUSED(enabled)
228  // do nothing
229 }
230 
231 //---------------------------------------------------------------------------------------------------------------------
233 {
234  QDomElement domElement = doc->createElement(getTagName());
236 
237  AddAttributes(doc, domElement, m_id, path);
238 
239  if (idTool != NULL_ID)
240  {
241  doc->SetAttribute(domElement, AttrIdTool, idTool);
242  }
243 
244  AddNodes(doc, domElement, path);
245 
246  AddToModeling(domElement);
247 
248  if (m_pieceId > NULL_ID)
249  {
250  const VPiece oldPiece = VAbstractTool::data.GetPiece(m_pieceId);
251  VPiece newPiece = oldPiece;
252 
253  if (path.GetType() == PiecePathType::InternalPath)
254  {
255  newPiece.GetInternalPaths().append(m_id);
256  }
257  else if (path.GetType() == PiecePathType::CustomSeamAllowance)
258  {
259  CustomSARecord record;
260  record.path = m_id;
261 
262  newPiece.GetCustomSARecords().append(record);
263  }
264 
265  SavePieceOptions *saveCommand = new SavePieceOptions(oldPiece, newPiece, doc, m_pieceId);
266  qApp->getUndoStack()->push(saveCommand);// First push then make a connect
267  VAbstractTool::data.UpdatePiece(m_pieceId, newPiece);// Update piece because first save will not call lite update
269  }
270 }
271 
272 //---------------------------------------------------------------------------------------------------------------------
274 {
276  {
277  show();
278  }
279 }
280 
281 //---------------------------------------------------------------------------------------------------------------------
283 {
284  hide();
285 }
286 
287 //---------------------------------------------------------------------------------------------------------------------
288 void VToolInternalPath::ToolCreation(const Source &typeCreation)
289 {
290  if (typeCreation == Source::FromGui || typeCreation == Source::FromTool)
291  {
292  AddToFile();
293  if (typeCreation != Source::FromTool)
294  {
295  qApp->getUndoStack()->endMacro();
296  }
297  }
298  else
299  {
301  }
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
305 VToolInternalPath::VToolInternalPath(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 pieceId,
306  const Source &typeCreation, const QString &blockName, const quint32 &idTool,
307  QObject *qoParent, QGraphicsItem *parent)
308  :VAbstractNode(doc, data, id, 0, blockName, idTool, qoParent),
309  QGraphicsPathItem(parent),
310  m_pieceId(pieceId)
311 {
313  RefreshGeometry();
314  ToolCreation(typeCreation);
315 }
316 
317 //---------------------------------------------------------------------------------------------------------------------
319 {
321  if (path.GetType() == PiecePathType::InternalPath)
322  {
323  QPainterPath p = path.PainterPath(this->getData());
324  p.setFillRule(Qt::OddEvenFill);
325 
326  this->setPath(p);
327  QPen pen = this->pen();
328  pen.setStyle(path.GetPenType());
329  this->setPen(pen);
330  }
331 }
332 
333 //---------------------------------------------------------------------------------------------------------------------
335 {
336  for (int i = 0; i < path.CountNodes(); ++i)
337  {
338  doc->IncrementReferens(VAbstractTool::data.GetGObject(path.at(i).GetId())->getIdTool());
339  }
340 }
341 
342 //---------------------------------------------------------------------------------------------------------------------
344 {
345  for (int i = 0; i < path.CountNodes(); ++i)
346  {
347  doc->DecrementReferens(VAbstractTool::data.GetGObject(path.at(i).GetId())->getIdTool());
348  }
349 }
The VAbstractNode class parent class for all detail node.
Definition: vabstractnode.h:71
ParentType parentType
Definition: vabstractnode.h:91
static const QString AttrIdTool
Definition: vabstractnode.h:77
quint32 idTool
idTool id tool.
Definition: vabstractnode.h:97
void AddToModeling(const QDomElement &domElement)
AddToModeling add tag to modeling tag current pattern peace.
void SetParentType(const ParentType &value)
virtual void LiteParseTree(const Document &parse)=0
virtual void UpdateToolData(const quint32 &id, VContainer *data)=0
virtual void IncrementReferens(quint32 id) const =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 TagPath
virtual void DecrementReferens(quint32 id) const =0
static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiecePath &path)
static const QString AttrInUse
Definition: vabstracttool.h:89
VAbstractPattern * doc
doc dom document container
static QVector< VPieceNode > PrepareNodes(const VPiecePath &path, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
const VContainer * getData() const
getData return pointer to data 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.
virtual void RefreshDataInFile()
RefreshDataInFile refresh attributes in file. If attributes don't exist create them.
The VContainer class container of all variables.
Definition: vcontainer.h:141
void UpdatePiece(quint32 id, const VPiece &piece)
Definition: vcontainer.cpp:510
void UpdatePiecePath(quint32 id, const VPiecePath &path)
Definition: vcontainer.cpp:518
VPiecePath GetPiecePath(quint32 id) const
Definition: vcontainer.cpp:198
quint32 AddPiecePath(const VPiecePath &path)
Definition: vcontainer.cpp:233
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
virtual void decrementReferens()
decrementReferens decrement referens.
Definition: vdatatool.cpp:74
quint32 _referens
_referens keep count tools what use this tool. If value more than 1 you can't delete tool.
Definition: vdatatool.h:87
virtual void incrementReferens()
incrementReferens increment referens.
Definition: vdatatool.h:126
VContainer data
data container with data
Definition: vdatatool.h:84
QDomElement elementById(quint32 id, const QString &tagName=QString())
static void SetParametrUsage(QDomElement &domElement, const QString &name, const NodeUsage &value)
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 VMainGraphicsScene class main scene.
quint32 GetId() const
Definition: vpiecenode.cpp:149
PiecePathType GetType() const
Definition: vpiecepath.cpp:239
qint32 CountNodes() const
Definition: vpiecepath.cpp:209
Qt::PenStyle GetPenType() const
Definition: vpiecepath.cpp:263
const VPieceNode & at(int indx) const
Definition: vpiecepath.cpp:221
bool IsCutPath() const
Definition: vpiecepath.cpp:275
QPainterPath PainterPath(const VContainer *data) const
Definition: vpiecepath.cpp:396
QString GetName() const
Definition: vpiecepath.cpp:251
Definition: vpiece.h:88
QVector< CustomSARecord > GetCustomSARecords() const
Definition: vpiece.cpp:444
QVector< quint32 > GetInternalPaths() const
Definition: vpiece.cpp:426
virtual void decrementReferens() Q_DECL_OVERRIDE
decrementReferens decrement reference for all parent objects.
static VToolInternalPath * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual QString getTagName() const Q_DECL_OVERRIDE
virtual void ToolCreation(const Source &typeCreation) Q_DECL_OVERRIDE
virtual void incrementReferens() Q_DECL_OVERRIDE
incrementReferens increment referens.
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
virtual void HideNode() Q_DECL_OVERRIDE
VToolInternalPath(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 pieceId, const Source &typeCreation, const QString &blockName=QString(), const quint32 &idTool=0, QObject *qoParent=nullptr, QGraphicsItem *parent=nullptr)
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
static void AddAttributes(VAbstractPattern *doc, QDomElement &domElement, quint32 id, const VPiecePath &path)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
void DecrementNodes(const VPiecePath &path) const
virtual void AddToFile() Q_DECL_OVERRIDE
AddToFile add tag with Information about tool into file.
virtual void ShowNode() Q_DECL_OVERRIDE
void IncrementNodes(const VPiecePath &path) const
void NeedLiteParsing(const Document &parse)
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
#define SCASSERT(cond)
Definition: def.h:317
@ InternalPath
Source
Definition: def.h:106
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLineType
Definition: ifcdef.cpp:90
QString PenStyleToLineType(Qt::PenStyle penStyle)
Definition: ifcdef.cpp:204
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrCut
Definition: ifcdef.cpp:93
#define NULL_ID
Definition: ifcdef.h:76
VPiecePath GetPiecePath(int piece, VAbstractPattern *doc, quint32 id)
Definition: union_tool.cpp:129
The CustomSA struct contains record about custom seam allowanse (SA).
Definition: def.h:510
quint32 path
Definition: def.h:520
Document
#define qApp
Definition: vapplication.h:67