1 /************************************************************************
2 **
3 ** @file movespoint.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 "movespoint.h"
30
31 #include <QDomElement>
32
33 #include "../ifc/xml/vabstractpattern.h"
34 #include "../ifc/ifcdef.h"
35 #include "../vmisc/vabstractvalapplication.h"
36 #include "../vmisc/def.h"
37 #include "vundocommand.h"
38
39 //---------------------------------------------------------------------------------------------------------------------
MoveSPoint(VAbstractPattern * doc,const double & x,const double & y,const quint32 & id,QGraphicsScene * scene,QUndoCommand * parent)40 MoveSPoint::MoveSPoint(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id,
41 QGraphicsScene *scene, QUndoCommand *parent)
42 : VUndoCommand(QDomElement(), doc, parent), oldX(0.0), oldY(0.0), newX(x), newY(y), scene(scene)
43 {
44 setText(tr("move single point"));
45 nodeId = id;
46 qCDebug(vUndo, "SPoint id %u", nodeId);
47
48 qCDebug(vUndo, "SPoint newX %f", newX);
49 qCDebug(vUndo, "SPoint newY %f", newY);
50
51 SCASSERT(scene != nullptr)
52 QDomElement domElement = doc->elementById(id, VAbstractPattern::TagPoint);
53 if (domElement.isElement())
54 {
55 oldX = VAbstractValApplication::VApp()->toPixel(doc->GetParametrDouble(domElement, AttrX, "0.0"));
56 oldY = VAbstractValApplication::VApp()->toPixel(doc->GetParametrDouble(domElement, AttrY, "0.0"));
57
58 qCDebug(vUndo, "SPoint oldX %f", oldX);
59 qCDebug(vUndo, "SPoint oldY %f", oldY);
60 }
61 else
62 {
63 qCDebug(vUndo, "Can't find spoint with id = %u.", nodeId);
64 }
65 }
66
67 //---------------------------------------------------------------------------------------------------------------------
~MoveSPoint()68 MoveSPoint::~MoveSPoint()
69 {}
70
71 //---------------------------------------------------------------------------------------------------------------------
undo()72 void MoveSPoint::undo()
73 {
74 qCDebug(vUndo, "Undo.");
75
76 Do(oldX, oldY);
77 }
78
79 //---------------------------------------------------------------------------------------------------------------------
redo()80 void MoveSPoint::redo()
81 {
82 qCDebug(vUndo, "Redo.");
83
84 Do(newX, newY);
85 }
86
87 //---------------------------------------------------------------------------------------------------------------------
mergeWith(const QUndoCommand * command)88 bool MoveSPoint::mergeWith(const QUndoCommand *command)
89 {
90 const MoveSPoint *moveCommand = static_cast<const MoveSPoint *>(command);
91 SCASSERT(moveCommand != nullptr)
92 const quint32 id = moveCommand->getSPointId();
93
94 qCDebug(vUndo, "Mergin.");
95 if (id != nodeId)
96 {
97 qCDebug(vUndo, "Merging canceled.");
98 return false;
99 }
100
101 qCDebug(vUndo, "Mergin undo.");
102 newX = moveCommand->getNewX();
103 newY = moveCommand->getNewY();
104 qCDebug(vUndo, "SPoint newX %f", newX);
105 qCDebug(vUndo, "SPoint newY %f", newY);
106 return true;
107 }
108
109 //---------------------------------------------------------------------------------------------------------------------
id() const110 int MoveSPoint::id() const
111 {
112 return static_cast<int>(UndoCommand::MoveSPoint);
113 }
114
115 //---------------------------------------------------------------------------------------------------------------------
Do(double x,double y)116 void MoveSPoint::Do(double x, double y)
117 {
118 qCDebug(vUndo, "Move to x %f", x);
119 qCDebug(vUndo, "Move to y %f", y);
120
121 QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
122 if (domElement.isElement())
123 {
124 doc->SetAttribute(domElement, AttrX, QString().setNum(VAbstractValApplication::VApp()->fromPixel(x)));
125 doc->SetAttribute(domElement, AttrY, QString().setNum(VAbstractValApplication::VApp()->fromPixel(y)));
126
127 emit NeedLiteParsing(Document::LitePPParse);
128 }
129 else
130 {
131 qCDebug(vUndo, "Can't find spoint with id = %u.", nodeId);
132 }
133 }
134