Seamly2D
Code documentation
deletepiece.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file deletepiece.cpp
3  ** @author Douglas S Caskey
4  ** @date Dec 11, 2022
5  **
6  ** @copyright
7  ** Copyright (C) 2017 - 2022 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
28  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
29  ** @date 9 11, 2016
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) 2016 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 "deletepiece.h"
54 
55 #include <QDomElement>
56 #include <QHash>
57 
58 #include "../ifc/ifcdef.h"
59 #include "../ifc/xml/vabstractpattern.h"
60 #include "../ifc/xml/vdomdocument.h"
61 #include "../vmisc/logging.h"
62 #include "../vmisc/def.h"
63 #include "../tools/vdatatool.h"
64 #include "vundocommand.h"
65 #include "../vpatterndb/vpiecenode.h"
66 #include "../vpatterndb/vpiecepath.h"
67 
68 //---------------------------------------------------------------------------------------------------------------------
69 DeletePiece::DeletePiece(VAbstractPattern *doc, quint32 id, const VPiece &piece, QUndoCommand *parent)
70  : VUndoCommand(QDomElement(), doc, parent)
71  , m_parentNode()
72  , m_siblingId(NULL_ID)
73  , m_piece (piece)
74 {
75  setText(tr("delete tool"));
76  nodeId = id;
77  QDomElement domElement = doc->elementById(id, VAbstractPattern::TagPiece);
78  if (domElement.isElement())
79  {
80  xml = domElement.cloneNode().toElement();
81  m_parentNode = domElement.parentNode();
82  QDomNode previousPiece = domElement.previousSibling();
83  if (previousPiece.isNull())
84  {
86  }
87  else
88  {
89  // Better save id of previous piece instead of reference to node.
90  m_siblingId = doc->GetParametrUInt(previousPiece.toElement(), VAbstractPattern::AttrId, NULL_ID_STR);
91  }
92  }
93  else
94  {
95  qCDebug(vUndo, "Can't get piece by id = %u.", nodeId);
96  return;
97  }
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------------
102 {}
103 
104 //---------------------------------------------------------------------------------------------------------------------
106 {
107  qCDebug(vUndo, "Undo.");
108 
110  emit NeedFullParsing();
111 }
112 
113 //---------------------------------------------------------------------------------------------------------------------
115 {
116  qCDebug(vUndo, "Redo.");
117 
118  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPiece);
119  if (domElement.isElement())
120  {
121  m_parentNode.removeChild(domElement);
122 
123  // Union delete two old pieces and create one new.
124  // So when UnionDetail delete piece we can't use FullParsing. So we hide piece on scene directly.
125  PatternPieceTool *tool = qobject_cast<PatternPieceTool*>(VAbstractPattern::getTool(nodeId));
126  SCASSERT(tool != nullptr);
127  tool->hide();
128 
133  emit NeedFullParsing(); // Doesn't work when UnionDetail delete piece.
134  }
135  else
136  {
137  qCDebug(vUndo, "Can't get piece by id = %u.", nodeId);
138  return;
139  }
140 }
quint32 m_siblingId
Definition: deletepiece.h:75
QDomNode m_parentNode
Definition: deletepiece.h:74
VPiece m_piece
Definition: deletepiece.h:76
virtual ~DeletePiece()
virtual void redo() Q_DECL_OVERRIDE
DeletePiece(VAbstractPattern *doc, quint32 id, const VPiece &piece, QUndoCommand *parent=nullptr)
Definition: deletepiece.cpp:69
virtual void undo() Q_DECL_OVERRIDE
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
static const QString TagPiece
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?
static const QString AttrId
Definition: vdomdocument.h:108
QVector< VPieceNode > GetNodes() const
Definition: vpiecepath.cpp:227
Definition: vpiece.h:88
QVector< CustomSARecord > GetCustomSARecords() const
Definition: vpiece.cpp:444
QVector< quint32 > GetInternalPaths() const
Definition: vpiece.cpp:426
QVector< quint32 > getAnchors() const
Definition: vpiece.cpp:462
VPiecePath GetPath() const
Definition: vpiece.cpp:156
void NeedFullParsing()
void DecrementReferences(const QVector< quint32 > &nodes) const
QDomElement xml
Definition: vundocommand.h:105
VAbstractPattern * doc
Definition: vundocommand.h:106
void UndoDeleteAfterSibling(QDomNode &parentNode, const quint32 &siblingId) const
quint32 nodeId
Definition: vundocommand.h:107
#define SCASSERT(cond)
Definition: def.h:317
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77