Seamly2D
Code documentation
movespoint.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 movespoint.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 "movespoint.h"
53 
54 #include <QDomElement>
55 
56 #include "../ifc/xml/vabstractpattern.h"
57 #include "../ifc/ifcdef.h"
58 #include "../vmisc/logging.h"
59 #include "../vmisc/vabstractapplication.h"
60 #include "../vmisc/def.h"
61 #include "vundocommand.h"
62 
63 //---------------------------------------------------------------------------------------------------------------------
64 MoveSPoint::MoveSPoint(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id,
65  QGraphicsScene *scene, QUndoCommand *parent)
66  : VUndoCommand(QDomElement(), doc, parent), oldX(0.0), oldY(0.0), newX(x), newY(y), scene(scene)
67 {
68  setText(tr("move single point"));
69  nodeId = id;
70  qCDebug(vUndo, "SPoint id %u", nodeId);
71 
72  qCDebug(vUndo, "SPoint newX %f", newX);
73  qCDebug(vUndo, "SPoint newY %f", newY);
74 
75  SCASSERT(scene != nullptr)
76  QDomElement domElement = doc->elementById(id, VAbstractPattern::TagPoint);
77  if (domElement.isElement())
78  {
79  oldX = qApp->toPixel(doc->GetParametrDouble(domElement, AttrX, "0.0"));
80  oldY = qApp->toPixel(doc->GetParametrDouble(domElement, AttrY, "0.0"));
81 
82  qCDebug(vUndo, "SPoint oldX %f", oldX);
83  qCDebug(vUndo, "SPoint oldY %f", oldY);
84  }
85  else
86  {
87  qCDebug(vUndo, "Can't find spoint with id = %u.", nodeId);
88  return;
89  }
90 }
91 
92 //---------------------------------------------------------------------------------------------------------------------
94 {}
95 
96 //---------------------------------------------------------------------------------------------------------------------
98 {
99  qCDebug(vUndo, "Undo.");
100 
101  Do(oldX, oldY);
102 }
103 
104 //---------------------------------------------------------------------------------------------------------------------
106 {
107  qCDebug(vUndo, "Redo.");
108 
109  Do(newX, newY);
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
113 bool MoveSPoint::mergeWith(const QUndoCommand *command)
114 {
115  const MoveSPoint *moveCommand = static_cast<const MoveSPoint *>(command);
116  SCASSERT(moveCommand != nullptr)
117  const quint32 id = moveCommand->getSPointId();
118 
119  qCDebug(vUndo, "Mergin.");
120  if (id != nodeId)
121  {
122  qCDebug(vUndo, "Merging canceled.");
123  return false;
124  }
125 
126  qCDebug(vUndo, "Mergin undo.");
127  newX = moveCommand->getNewX();
128  newY = moveCommand->getNewY();
129  qCDebug(vUndo, "SPoint newX %f", newX);
130  qCDebug(vUndo, "SPoint newY %f", newY);
131  return true;
132 }
133 
134 //---------------------------------------------------------------------------------------------------------------------
135 int MoveSPoint::id() const
136 {
137  return static_cast<int>(UndoCommand::MoveSPoint);
138 }
139 
140 //---------------------------------------------------------------------------------------------------------------------
141 void MoveSPoint::Do(double x, double y)
142 {
143  qCDebug(vUndo, "Move to x %f", x);
144  qCDebug(vUndo, "Move to y %f", y);
145 
146  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
147  if (domElement.isElement())
148  {
149  doc->SetAttribute(domElement, AttrX, QString().setNum(qApp->fromPixel(x)));
150  doc->SetAttribute(domElement, AttrY, QString().setNum(qApp->fromPixel(y)));
151 
153  }
154  else
155  {
156  qCDebug(vUndo, "Can't find spoint with id = %u.", nodeId);
157  return;
158  }
159 }
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE
Definition: movespoint.cpp:113
double oldY
Definition: movespoint.h:83
double oldX
Definition: movespoint.h:82
double getNewX() const
Definition: movespoint.h:96
QGraphicsScene * scene
Definition: movespoint.h:86
virtual int id() const Q_DECL_OVERRIDE
Definition: movespoint.cpp:135
virtual void undo() Q_DECL_OVERRIDE
Definition: movespoint.cpp:97
MoveSPoint(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent=nullptr)
Definition: movespoint.cpp:64
virtual ~MoveSPoint() Q_DECL_OVERRIDE
Definition: movespoint.cpp:93
quint32 getSPointId() const
Definition: movespoint.h:90
void Do(double x, double y)
Definition: movespoint.cpp:141
double getNewY() const
Definition: movespoint.h:102
double newY
Definition: movespoint.h:85
double newX
Definition: movespoint.h:84
virtual void redo() Q_DECL_OVERRIDE
Definition: movespoint.cpp:105
static const QString TagPoint
QDomElement elementById(quint32 id, const QString &tagName=QString())
static qreal GetParametrDouble(const QDomElement &domElement, const QString &name, const QString &defValue)
Returns the double value of the given attribute.
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
SetAttribute set attribute in pattern file. Replace "," by ".".
Definition: vdomdocument.h:185
VAbstractPattern * doc
Definition: vundocommand.h:106
void NeedLiteParsing(const Document &parse)
quint32 nodeId
Definition: vundocommand.h:107
#define SCASSERT(cond)
Definition: def.h:317
const QString AttrY
Definition: ifcdef.cpp:89
const QString AttrX
Definition: ifcdef.cpp:88
@ LiteBlockParse
#define qApp
Definition: vapplication.h:67