Seamly2D
Code documentation
addtocalc.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 addtocalc.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 9 6, 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 "addtocalc.h"
53 
54 #include <QDomNode>
55 
56 #include "../vwidgets/vmaingraphicsview.h"
57 #include "../ifc/xml/vabstractpattern.h"
58 #include "../ifc/ifcdef.h"
59 #include "../vmisc/logging.h"
60 #include "../vmisc/vabstractapplication.h"
61 #include "../vmisc/customevents.h"
62 #include "vundocommand.h"
63 
64 //---------------------------------------------------------------------------------------------------------------------
65 AddToCalc::AddToCalc(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent)
66  : VUndoCommand(xml, doc, parent), activeBlockName(doc->getActiveDraftBlockName()), cursor(doc->getCursor())
67 {
68  setText(tr("add object"));
70 }
71 
72 //---------------------------------------------------------------------------------------------------------------------
74 {
75  qCDebug(vUndo, "Undo.");
76 
77  doc->changeActiveDraftBlock(activeBlockName);//Without this user will not see this change
78 
79  QDomElement calcElement;
81  {
82  QDomElement domElement = doc->elementById(nodeId);
83  if (domElement.isElement())
84  {
85  if (calcElement.removeChild(domElement).isNull())
86  {
87  qCDebug(vUndo, "Can't delete node.");
88  return;
89  }
90  }
91  else
92  {
93  qCDebug(vUndo, "Can't get tool by id = %u.", nodeId);
94  return;
95  }
96  }
97  else
98  {
99  qCDebug(vUndo, "Can't find tag Calculation.");
100  return;
101  }
102  emit NeedFullParsing();
103  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
104  doc->setCurrentDraftBlock(activeBlockName);//Return current pattern piece after undo
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
109 {
110  qCDebug(vUndo, "Redo.");
111 
112  doc->changeActiveDraftBlock(activeBlockName);//Without this user will not see this change
113  doc->setCursor(cursor);
114 
115  QDomElement calcElement;
117  {
118  if (cursor == NULL_ID)
119  {
120  calcElement.appendChild(xml);
121  }
122  else
123  {
124  QDomElement refElement = doc->elementById(cursor);
125  if (refElement.isElement())
126  {
127  calcElement.insertAfter(xml, refElement);
128  }
129  else
130  {
131  qCDebug(vUndo, "Can not find the element after which you want to insert.");
132  return;
133  }
134  }
135  }
136  else
137  {
138  qCDebug(vUndo, "Can't find tag Calculation.");
139  return;
140  }
141  RedoFullParsing();
142  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
147 {
148  if (redoFlag)
149  {
150  emit NeedFullParsing();
151  doc->setCurrentDraftBlock(activeBlockName);//Return current pattern piece after undo
152  }
153  else
154  {
155  QApplication::postEvent(doc, new LiteParseEvent());
156  }
157  redoFlag = true;
158 }
virtual void undo() Q_DECL_OVERRIDE
Definition: addtocalc.cpp:73
quint32 cursor
Definition: addtocalc.h:77
AddToCalc(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent=nullptr)
Definition: addtocalc.cpp:65
virtual void redo() Q_DECL_OVERRIDE
Definition: addtocalc.cpp:108
virtual void RedoFullParsing() Q_DECL_OVERRIDE
Definition: addtocalc.cpp:146
const QString activeBlockName
Definition: addtocalc.h:76
void setCursor(const quint32 &value)
bool getActiveNodeElement(const QString &name, QDomElement &element) const
getActiveNodeElement find element in current draft block by name.
static const QString TagCalculation
void setCurrentDraftBlock(const QString &patterPiece)
void changeActiveDraftBlock(const QString &name, const Document &parse=Document::FullParse)
changeActiveDraftBlock set new active draft block name.
QDomElement elementById(quint32 id, const QString &tagName=QString())
static quint32 getParameterId(const QDomElement &domElement)
getParameterId return value id attribute.
static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item=nullptr)
NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
void NeedFullParsing()
QDomElement xml
Definition: vundocommand.h:105
VAbstractPattern * doc
Definition: vundocommand.h:106
quint32 nodeId
Definition: vundocommand.h:107
#define NULL_ID
Definition: ifcdef.h:76
#define qApp
Definition: vapplication.h:67