Seamly2D
Code documentation
delete_draftblock.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file delete_draftblock.h
3  ** @author Douglas S Caskey
4  ** @date Dec 27, 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 deletepatternpiece.cpp
28  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
29  ** @date 14 6, 2014
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) 2013-2015 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 "delete_draftblock.h"
54 
55 #include <QDomNode>
56 #include <QDomNodeList>
57 
58 #include "vundocommand.h"
59 #include "../ifc/xml/vabstractpattern.h"
60 #include "../vmisc/logging.h"
61 
62 //---------------------------------------------------------------------------------------------------------------------
63 DeleteDraftBlock::DeleteDraftBlock(VAbstractPattern *doc, const QString &draftBlockName, QUndoCommand *parent)
64  : VUndoCommand(QDomElement(), doc, parent)
65  , draftBlockName(draftBlockName)
66  , draftBlock(QDomElement())
67  , previousDraftBlockName(QString())
68 {
69  setText(tr("delete draft block %1").arg(draftBlockName));
70 
71  const QDomElement block = doc->getDraftBlockElement(draftBlockName);
72  draftBlock = block.cloneNode().toElement();
73  const QDomElement previousDraftBlock = block.previousSibling().toElement();//find previous pattern piece
74  if (not previousDraftBlock.isNull() && previousDraftBlock.tagName() == VAbstractPattern::TagDraftBlock)
75  {
77  }
78 }
79 
80 //---------------------------------------------------------------------------------------------------------------------
82 {}
83 
84 //---------------------------------------------------------------------------------------------------------------------
86 {
87  qCDebug(vUndo, "Undo.");
88 
89  QDomElement rootElement = doc->documentElement();
90 
91  if (not previousDraftBlockName.isEmpty())
92  { // not first in the list, add after tag draft block
93  const QDomNode previousDraftBlock = doc->getDraftBlockElement(previousDraftBlockName);
94  rootElement.insertAfter(draftBlock, previousDraftBlock);
95  }
96  else
97  { // first in the list, add before tag draftBlock
98  const QDomNodeList list = rootElement.elementsByTagName(VAbstractPattern::TagDraftBlock);
99  QDomElement block;
100 
101  if (not list.isEmpty())
102  {
103  block = list.at(0).toElement();
104  }
105 
106  Q_ASSERT_X(not block.isNull(), Q_FUNC_INFO, "Couldn't' find tag draft block");
107  rootElement.insertBefore(draftBlock, block);
108  }
109 
110  emit NeedFullParsing();
112 }
113 
114 //---------------------------------------------------------------------------------------------------------------------
116 {
117  qCDebug(vUndo, "Redo.");
118 
119  QDomElement rootElement = doc->documentElement();
120  const QDomElement draftBlock = doc->getDraftBlockElement(draftBlockName);
121  rootElement.removeChild(draftBlock);
122  emit NeedFullParsing();
123 }
QDomElement draftBlock
DeleteDraftBlock(VAbstractPattern *doc, const QString &draftBlockName, QUndoCommand *parent=nullptr)
QString previousDraftBlockName
virtual ~DeleteDraftBlock() Q_DECL_OVERRIDE
virtual void undo() Q_DECL_OVERRIDE
virtual void redo() Q_DECL_OVERRIDE
QDomElement getDraftBlockElement(const QString &name)
static const QString TagDraftBlock
static const QString AttrName
void changeActiveDraftBlock(const QString &name, const Document &parse=Document::FullParse)
changeActiveDraftBlock set new active draft block name.
static QString GetParametrString(const QDomElement &domElement, const QString &name, const QString &defValue=QString())
Returns the string value of the given attribute. RENAME: see above.
void NeedFullParsing()
VAbstractPattern * doc
Definition: vundocommand.h:106