1 /************************************************************************
2  **
3  **  @file   addtocalc.cpp
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   9 6, 2014
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2013-2015 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "addtocalc.h"
30 
31 #include <QDomNode>
32 
33 #include "../vwidgets/vmaingraphicsview.h"
34 #include "../ifc/xml/vabstractpattern.h"
35 #include "../ifc/ifcdef.h"
36 #include "../vmisc/vabstractvalapplication.h"
37 #include "../vmisc/customevents.h"
38 #include "vundocommand.h"
39 
40 //---------------------------------------------------------------------------------------------------------------------
AddToCalc(const QDomElement & xml,VAbstractPattern * doc,QUndoCommand * parent)41 AddToCalc::AddToCalc(const QDomElement &xml, VAbstractPattern *doc, QUndoCommand *parent)
42     : VUndoCommand(xml, doc, parent), nameActivDraw(doc->GetNameActivPP()), cursor(doc->getCursor())
43 {
44     setText(tr("add object"));
45     nodeId = doc->GetParametrId(xml);
46 }
47 
48 //---------------------------------------------------------------------------------------------------------------------
undo()49 void AddToCalc::undo()
50 {
51     qCDebug(vUndo, "Undo.");
52 
53     doc->ChangeActivPP(nameActivDraw);//Without this user will not see this change
54 
55     QDomElement calcElement;
56     if (doc->GetActivNodeElement(VAbstractPattern::TagCalculation, calcElement))
57     {
58         QDomElement domElement = doc->elementById(nodeId);
59         if (domElement.isElement())
60         {
61             if (calcElement.removeChild(domElement).isNull())
62             {
63                 qCDebug(vUndo, "Can't delete node.");
64                 return;
65             }
66         }
67         else
68         {
69             qCDebug(vUndo, "Can't get tool by id = %u.", nodeId);
70             return;
71         }
72     }
73     else
74     {
75         qCDebug(vUndo, "Can't find tag Calculation.");
76         return;
77     }
78     emit NeedFullParsing();
79     VMainGraphicsView::NewSceneRect(VAbstractValApplication::VApp()->getCurrentScene(),
80                                     VAbstractValApplication::VApp()->getSceneView());
81     if (VAbstractValApplication::VApp()->GetDrawMode() == Draw::Calculation)
82     {
83         emit doc->SetCurrentPP(nameActivDraw);//Return current pattern piece after undo
84     }
85 }
86 
87 //---------------------------------------------------------------------------------------------------------------------
redo()88 void AddToCalc::redo()
89 {
90     qCDebug(vUndo, "Redo.");
91 
92     doc->ChangeActivPP(nameActivDraw);//Without this user will not see this change
93     doc->setCursor(cursor);
94 
95     QDomElement calcElement;
96     if (doc->GetActivNodeElement(VAbstractPattern::TagCalculation, calcElement))
97     {
98         if (cursor == NULL_ID)
99         {
100             calcElement.appendChild(xml);
101         }
102         else
103         {
104             QDomElement refElement = doc->elementById(cursor);
105             if (refElement.isElement())
106             {
107                 calcElement.insertAfter(xml, refElement);
108             }
109             else
110             {
111                 qCDebug(vUndo, "Can not find the element after which you want to insert.");
112                 return;
113             }
114         }
115     }
116     else
117     {
118         qCDebug(vUndo, "Can't find tag Calculation.");
119         return;
120     }
121     RedoFullParsing();
122     VMainGraphicsView::NewSceneRect(VAbstractValApplication::VApp()->getCurrentScene(),
123                                     VAbstractValApplication::VApp()->getSceneView());
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
RedoFullParsing()127 void AddToCalc::RedoFullParsing()
128 {
129     if (redoFlag)
130     {
131         emit NeedFullParsing();
132         if (VAbstractValApplication::VApp()->GetDrawMode() == Draw::Calculation)
133         {
134             emit doc->SetCurrentPP(nameActivDraw);//Return current pattern piece after undo
135         }
136     }
137     else
138     {
139         QApplication::postEvent(doc, new LiteParseEvent());
140     }
141     redoFlag = true;
142 }
143