Seamly2D
Code documentation
movepiece.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file move_pece.cpp
3  ** @author Douglas S Caskey
4  ** @date Dec 27, 2022
5  **
6  ** @copyright
7  ** Copyright (C) 2017 - 2022 Seamly, LLC
8  ** https://github.com/fashionfreedom/seamly2d
9  **
10  ** @brief
11  ** Seamly2D is free software: you can redistribute it and/or modify
12  ** it under the terms of the GNU General Public License as published by
13  ** the Free Software Foundation, either version 3 of the License, or
14  ** (at your option) any later version.
15  **
16  ** Seamly2D is distributed in the hope that it will be useful,
17  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  ** GNU General Public License for more details.
20  **
21  ** You should have received a copy of the GNU General Public License
22  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
23  **************************************************************************/
24 
25 /************************************************************************
26  **
27  ** @file movedetail.cpp
28  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
29  ** @date 13 6, 2014
30  **
31  ** @brief
32  ** @copyright
33  ** This source code is part of the Valentina project, a pattern making
34  ** program, whose allow create and modeling patterns of clothing.
35  ** Copyright (C) 2013-2015 Valentina project
36  ** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
37  **
38  ** Valentina is free software: you can redistribute it and/or modify
39  ** it under the terms of the GNU General Public License as published by
40  ** the Free Software Foundation, either version 3 of the License, or
41  ** (at your option) any later version.
42  **
43  ** Valentina is distributed in the hope that it will be useful,
44  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
45  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46  ** GNU General Public License for more details.
47  **
48  ** You should have received a copy of the GNU General Public License
49  ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
50  **
51  *************************************************************************/
52 
53 #include "movepiece.h"
54 
55 #include <QDomElement>
56 
57 #include "../ifc/xml/vabstractpattern.h"
58 #include "../ifc/ifcdef.h"
59 #include "../vmisc/logging.h"
60 #include "../vmisc/vabstractapplication.h"
61 #include "../vmisc/def.h"
62 #include "../vwidgets/vmaingraphicsview.h"
63 #include "vundocommand.h"
64 
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 MovePiece::MovePiece(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id,
68  QGraphicsScene *scene, QUndoCommand *parent)
69  : VUndoCommand(QDomElement(), doc, parent)
70  , m_oldX(0.0)
71  , m_oldY(0.0)
72  , m_newX(x)
73  , m_newY(y)
74  , m_scene(scene)
75 {
76  setText(QObject::tr("move piece"));
77  nodeId = id;
78 
79  SCASSERT(scene != nullptr)
80  QDomElement domElement = doc->elementById(id, VAbstractPattern::TagPiece);
81  if (domElement.isElement())
82  {
83  m_oldX = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx, "0.0"));
84  m_oldY = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMy, "0.0"));
85  }
86  else
87  {
88  qCDebug(vUndo, "Can't find detail with id = %u.", nodeId);
89  return;
90  }
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
95 {}
96 
97 //---------------------------------------------------------------------------------------------------------------------
99 {
100  qCDebug(vUndo, "Undo.");
101 
102  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPiece);
103  if (domElement.isElement())
104  {
105  SaveCoordinates(domElement, m_oldX, m_oldY);
106 
108  }
109  else
110  {
111  qCDebug(vUndo, "Can't find detail with id = %u.", nodeId);
112  }
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
117 {
118  qCDebug(vUndo, "Redo.");
119 
120  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPiece);
121  if (domElement.isElement())
122  {
123  SaveCoordinates(domElement, m_newX, m_newY);
124 
125  if (redoFlag)
126  {
128  }
129  else
130  {
131  VMainGraphicsView::NewSceneRect(m_scene, qApp->getSceneView());
132  }
133  redoFlag = true;
134  }
135  else
136  {
137  qCDebug(vUndo, "Can't find detail with id = %u.", nodeId);
138  }
139 }
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 // cppcheck-suppress unusedFunction
143 bool MovePiece::mergeWith(const QUndoCommand *command)
144 {
145  const MovePiece *moveCommand = static_cast<const MovePiece *>(command);
146  SCASSERT(moveCommand != nullptr)
147  const quint32 id = moveCommand->getPieceId();
148 
149  if (id != nodeId)
150  {
151  return false;
152  }
153 
154  m_newX = moveCommand->getNewX();
155  m_newY = moveCommand->getNewY();
156  return true;
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
160 int MovePiece::id() const
161 {
162  return static_cast<int>(UndoCommand::MovePiece);
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
166 void MovePiece::SaveCoordinates(QDomElement &domElement, double x, double y)
167 {
168  doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(x)));
169  doc->SetAttribute(domElement, AttrMy, QString().setNum(qApp->fromPixel(y)));
170 }
double m_newX
Definition: movepiece.h:86
double m_oldX
Definition: movepiece.h:84
quint32 getPieceId() const
Definition: movepiece.h:94
double getNewY() const
Definition: movepiece.h:106
virtual void redo() Q_DECL_OVERRIDE
Definition: movepiece.cpp:116
virtual int id() const Q_DECL_OVERRIDE
Definition: movepiece.cpp:160
QGraphicsScene * m_scene
Definition: movepiece.h:88
MovePiece(VAbstractPattern *doc, const double &x, const double &y, const quint32 &id, QGraphicsScene *scene, QUndoCommand *parent=nullptr)
Definition: movepiece.cpp:67
double m_newY
Definition: movepiece.h:87
double getNewX() const
Definition: movepiece.h:100
virtual ~MovePiece()
Definition: movepiece.cpp:94
double m_oldY
Definition: movepiece.h:85
void SaveCoordinates(QDomElement &domElement, double x, double y)
Definition: movepiece.cpp:166
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE
Definition: movepiece.cpp:143
virtual void undo() Q_DECL_OVERRIDE
Definition: movepiece.cpp:98
static const QString TagPiece
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
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.
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 AttrMx
Definition: ifcdef.cpp:74
const QString AttrMy
Definition: ifcdef.cpp:75
#define qApp
Definition: vapplication.h:67