Seamly2D
Code documentation
movedoublelabel.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 movedoublelabel.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 24 6, 2015
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) 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 "movedoublelabel.h"
53 
54 #include <QDomElement>
55 
56 #include "../ifc/ifcdef.h"
57 #include "../ifc/xml/vabstractpattern.h"
58 #include "../vmisc/logging.h"
59 #include "../vmisc/vabstractapplication.h"
60 #include "../vmisc/def.h"
61 #include "../vtools/tools/drawTools/vdrawtool.h"
62 #include "../vundocommand.h"
63 #include "moveabstractlabel.h"
64 
65 
66 //---------------------------------------------------------------------------------------------------------------------
68  quint32 toolId, quint32 pointId, QUndoCommand *parent)
69  : MoveAbstractLabel(doc, pointId, pos, parent)
70  , m_type(type)
71  , m_idTool(toolId)
72  , m_scene(qApp->getCurrentScene())
73 {
74  if (type == MoveDoublePoint::FirstPoint)
75  {
76  setText(tr("move the first dart label"));
77  }
78  else
79  {
80  setText(tr("move the second dart label"));
81  }
82 
83  const QDomElement domElement = doc->elementById(m_idTool, VAbstractPattern::TagPoint);
84  if (domElement.isElement())
85  {
86  if (type == MoveDoublePoint::FirstPoint)
87  {
88  m_oldPos.rx() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx1, "0.0"));
89  m_oldPos.ry() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMy1, "0.0"));
90 
91  qCDebug(vUndo, "Label old Mx1 %f", m_oldPos.x());
92  qCDebug(vUndo, "Label old My1 %f", m_oldPos.y());
93  }
94  else
95  {
96  m_oldPos.rx() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx2, "0.0"));
97  m_oldPos.ry() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMy2, "0.0"));
98 
99  qCDebug(vUndo, "Label old Mx2 %f", m_oldPos.x());
100  qCDebug(vUndo, "Label old My2 %f", m_oldPos.y());
101  }
102  }
103  else
104  {
105  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
106  return;
107  }
108 }
109 
110 //---------------------------------------------------------------------------------------------------------------------
111 bool MoveDoubleLabel::mergeWith(const QUndoCommand *command)
112 {
113  const MoveDoubleLabel *moveCommand = static_cast<const MoveDoubleLabel *>(command);
114  SCASSERT(moveCommand != nullptr)
115 
116  if (moveCommand->GetPointId() != nodeId ||
117  moveCommand->GetPointType() != m_type ||
118  moveCommand->GetToolId() != m_idTool)
119  {
120  return false;
121  }
122 
123  m_newPos = moveCommand->GetNewPos();
124 
126  {
127  qCDebug(vUndo, "Label new Mx1 %f", m_newPos.x());
128  qCDebug(vUndo, "Label new My1 %f", m_newPos.y());
129  }
130  else
131  {
132  qCDebug(vUndo, "Label new Mx2 %f", m_newPos.x());
133  qCDebug(vUndo, "Label new My2 %f", m_newPos.y());
134  }
135  return true;
136 }
137 
138 //---------------------------------------------------------------------------------------------------------------------
140 {
141  return static_cast<int>(UndoCommand::MoveDoubleLabel);
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
145 void MoveDoubleLabel::Do(const QPointF &pos)
146 {
148  {
149  qCDebug(vUndo, "New mx1 %f", pos.x());
150  qCDebug(vUndo, "New my1 %f", pos.y());
151  }
152  else
153  {
154  qCDebug(vUndo, "New mx2 %f", pos.x());
155  qCDebug(vUndo, "New my2 %f", pos.y());
156  }
157 
158  QDomElement domElement = doc->elementById(m_idTool, VAbstractPattern::TagPoint);
159  if (domElement.isElement())
160  {
162  {
163  doc->SetAttribute(domElement, AttrMx1, QString().setNum(qApp->fromPixel(pos.x())));
164  doc->SetAttribute(domElement, AttrMy1, QString().setNum(qApp->fromPixel(pos.y())));
165  }
166  else
167  {
168  doc->SetAttribute(domElement, AttrMx2, QString().setNum(qApp->fromPixel(pos.x())));
169  doc->SetAttribute(domElement, AttrMy2, QString().setNum(qApp->fromPixel(pos.y())));
170  }
171 
172  if (VDrawTool *tool = qobject_cast<VDrawTool *>(VAbstractPattern::getTool(m_idTool)))
173  {
174  tool->setPointNamePosition(nodeId, pos);
175  }
176  }
177  else
178  {
179  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
180  }
181 }
QPointF GetNewPos() const
quint32 GetPointId() const
quint32 GetToolId() const
virtual void Do(const QPointF &pos) Q_DECL_OVERRIDE
MoveDoublePoint GetPointType() const
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE
MoveDoubleLabel(VAbstractPattern *doc, const QPointF &pos, MoveDoublePoint type, quint32 toolId, quint32 pointId, QUndoCommand *parent=nullptr)
MoveDoublePoint m_type
virtual int id() const Q_DECL_OVERRIDE
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
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
The VDrawTool abstract class for all draw tool.
Definition: vdrawtool.h:68
VAbstractPattern * doc
Definition: vundocommand.h:106
quint32 nodeId
Definition: vundocommand.h:107
#define SCASSERT(cond)
Definition: def.h:317
const QString AttrMx2
Definition: ifcdef.cpp:80
const QString AttrMx1
Definition: ifcdef.cpp:77
const QString AttrMy1
Definition: ifcdef.cpp:78
const QString AttrMy2
Definition: ifcdef.cpp:81
MoveDoublePoint
#define qApp
Definition: vapplication.h:67