Seamly2D
Code documentation
vundocommand.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 vundocommand.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 16 7, 2014
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) 2013-2015 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 "vundocommand.h"
53 
54 #include <QDomNode>
55 #include <QApplication>
56 
57 #include "../ifc/ifcdef.h"
58 #include "../vmisc/def.h"
59 #include "../vmisc/customevents.h"
60 #include "../vpatterndb/vnodedetail.h"
61 #include "../vpatterndb/vpiecenode.h"
62 #include "../tools/drawTools/operation/vabstractoperation.h"
63 
64 Q_LOGGING_CATEGORY(vUndo, "v.undo")
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 VUndoCommand::VUndoCommand(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent)
68  : QObject()
69  , QUndoCommand(parent)
70  , xml(xml), doc(doc)
71  , nodeId(NULL_ID)
72  , redoFlag(false)
73 {
74  SCASSERT(doc != nullptr)
75 }
76 
77 //---------------------------------------------------------------------------------------------------------------------
79 {
80  if (redoFlag)
81  {
82  emit NeedFullParsing();
83  }
84  else
85  {
86  QApplication::postEvent(doc, new LiteParseEvent());
87  }
88  redoFlag = true;
89 }
90 
91 //---------------------------------------------------------------------------------------------------------------------
92 void VUndoCommand::UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const
93 {
94  if (siblingId == NULL_ID)
95  {
96  parentNode.appendChild(xml);
97  }
98  else
99  {
100  const QDomElement refElement = doc->NodeById(siblingId);
101  parentNode.insertAfter(xml, refElement);
102  }
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
107 {
108  for (qint32 i = 0; i < nodes.size(); ++i)
109  {
110  doc->IncrementReferens(nodes.at(i));
111  }
112 }
113 
114 //---------------------------------------------------------------------------------------------------------------------
116 {
117  for (qint32 i = 0; i < nodes.size(); ++i)
118  {
119  doc->DecrementReferens(nodes.at(i));
120  }
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
125 {
127 
128  for (qint32 i = 0; i < nodes.size(); ++i)
129  {
130  n.append(nodes.at(i).path);
131  }
132 
134 }
135 
136 //---------------------------------------------------------------------------------------------------------------------
138 {
140 
141  for (qint32 i = 0; i < nodes.size(); ++i)
142  {
143  n.append(nodes.at(i).path);
144  }
145 
147 }
148 
149 //---------------------------------------------------------------------------------------------------------------------
151 {
153 
154  for (qint32 i = 0; i < nodes.size(); ++i)
155  {
156  n.append(nodes.at(i).GetId());
157  }
158 
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
164 {
166 
167  for (qint32 i = 0; i < nodes.size(); ++i)
168  {
169  n.append(nodes.at(i).GetId());
170  }
171 
173 }
174 
175 //---------------------------------------------------------------------------------------------------------------------
176 QDomElement VUndoCommand::getDestinationObject(quint32 idTool, quint32 idPoint) const
177 {
178  const QDomElement tool = doc->elementById(idTool, VAbstractPattern::TagOperation);
179  if (tool.isElement())
180  {
181  QDomElement correctDest;
182  const QDomNodeList nodeList = tool.childNodes();
183  for (qint32 i = 0; i < nodeList.size(); ++i)
184  {
185  const QDomElement dest = nodeList.at(i).toElement();
186  if (not dest.isNull() && dest.isElement() && dest.tagName() == VAbstractOperation::TagDestination)
187  {
188  correctDest = dest;
189  break;
190  }
191  }
192 
193  if (not correctDest.isNull())
194  {
195  const QDomNodeList destObjects = correctDest.childNodes();
196  for (qint32 i = 0; i < destObjects.size(); ++i)
197  {
198  const QDomElement obj = destObjects.at(i).toElement();
199  if (not obj.isNull() && obj.isElement())
200  {
201  const quint32 id = doc->GetParametrUInt(obj, AttrIdObject, NULL_ID_STR);
202  if (idPoint == id)
203  {
204  return obj;
205  }
206  }
207  }
208  }
209  }
210 
211  return QDomElement();
212 }
static const QString TagDestination
virtual void IncrementReferens(quint32 id) const =0
static const QString TagOperation
virtual void DecrementReferens(quint32 id) const =0
QDomElement elementById(quint32 id, const QString &tagName=QString())
static quint32 GetParametrUInt(const QDomElement &domElement, const QString &name, const QString &defValue)
Returns the long long value of the given attribute. RENAME: GetParameterLongLong?
QDomElement NodeById(const quint32 &nodeId)
void NeedFullParsing()
virtual void RedoFullParsing()
void DecrementReferences(const QVector< quint32 > &nodes) const
QDomElement xml
Definition: vundocommand.h:105
VAbstractPattern * doc
Definition: vundocommand.h:106
void IncrementReferences(const QVector< quint32 > &nodes) const
void UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const
QDomElement getDestinationObject(quint32 idTool, quint32 idPoint) const
#define SCASSERT(cond)
Definition: def.h:317
const QString AttrIdObject
Definition: ifcdef.cpp:148
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77