1 /************************************************************************
2  **
3  **  @file   movedetail.cpp
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   13 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 "movepiece.h"
30 
31 #include <QDomElement>
32 
33 #include "../ifc/xml/vabstractpattern.h"
34 #include "../ifc/ifcdef.h"
35 #include "../vmisc/vabstractapplication.h"
36 #include "../vmisc/def.h"
37 #include "../vwidgets/vmaingraphicsview.h"
38 #include "vundocommand.h"
39 #include "../tools/vtoolseamallowance.h"
40 
41 //---------------------------------------------------------------------------------------------------------------------
MovePiece(VAbstractPattern * doc,const double & x,const double & y,const quint32 & id,QGraphicsScene * scene,QUndoCommand * parent)42 MovePiece::MovePiece(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id,
43                      QGraphicsScene *scene, QUndoCommand *parent)
44     : VUndoCommand(QDomElement(), doc, parent),
45       m_oldX(0.0),
46       m_oldY(0.0),
47       m_newX(x),
48       m_newY(y),
49       m_scene(scene)
50 {
51     setText(QObject::tr("move detail"));
52     nodeId = id;
53 
54     SCASSERT(scene != nullptr)
55     QDomElement domElement = doc->elementById(id, VAbstractPattern::TagDetail);
56     if (domElement.isElement())
57     {
58         m_oldX = VAbstractValApplication::VApp()->toPixel(doc->GetParametrDouble(domElement, AttrMx, "0.0"));
59         m_oldY = VAbstractValApplication::VApp()->toPixel(doc->GetParametrDouble(domElement, AttrMy, "0.0"));
60     }
61     else
62     {
63         qCDebug(vUndo, "Can't find detail with id = %u.", nodeId);
64     }
65 }
66 
67 //---------------------------------------------------------------------------------------------------------------------
undo()68 void MovePiece::undo()
69 {
70     qCDebug(vUndo, "Undo.");
71     Do(m_oldX, m_oldY);
72 }
73 
74 //---------------------------------------------------------------------------------------------------------------------
redo()75 void MovePiece::redo()
76 {
77     qCDebug(vUndo, "Redo.");
78     Do(m_newX, m_newY);
79 }
80 
81 //---------------------------------------------------------------------------------------------------------------------
82 // cppcheck-suppress unusedFunction
mergeWith(const QUndoCommand * command)83 bool MovePiece::mergeWith(const QUndoCommand *command)
84 {
85     const MovePiece *moveCommand = static_cast<const MovePiece *>(command);
86     SCASSERT(moveCommand != nullptr)
87     const quint32 id = moveCommand->getDetId();
88 
89     if (id != nodeId)
90     {
91         return false;
92     }
93 
94     m_newX = moveCommand->getNewX();
95     m_newY = moveCommand->getNewY();
96     return true;
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
id() const100 int MovePiece::id() const
101 {
102     return static_cast<int>(UndoCommand::MovePiece);
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
Do(qreal x,qreal y)106 void MovePiece::Do(qreal x, qreal y)
107 {
108     qCDebug(vUndo, "Do.");
109 
110     QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagDetail);
111     if (domElement.isElement())
112     {
113         SaveCoordinates(domElement, x, y);
114 
115         VToolSeamAllowance *tool = qobject_cast<VToolSeamAllowance *>(VAbstractPattern::getTool(nodeId));
116         if (tool)
117         {
118             tool->Move(x, y);
119         }
120         VMainGraphicsView::NewSceneRect(m_scene, VAbstractValApplication::VApp()->getSceneView(), tool);
121     }
122     else
123     {
124         qCDebug(vUndo, "Can't find detail with id = %u.", nodeId);
125     }
126 }
127 
128 //---------------------------------------------------------------------------------------------------------------------
SaveCoordinates(QDomElement & domElement,double x,double y)129 void MovePiece::SaveCoordinates(QDomElement &domElement, double x, double y)
130 {
131     doc->SetAttribute(domElement, AttrMx, QString().setNum(VAbstractValApplication::VApp()->fromPixel(x)));
132     doc->SetAttribute(domElement, AttrMy, QString().setNum(VAbstractValApplication::VApp()->fromPixel(y)));
133 }
134