Seamly2D
Code documentation
moveoperationlabel.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 moveoperationlabel.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 13 5, 2016
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) 2016 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 "moveoperationlabel.h"
53 
54 #include <QDomNode>
55 #include <QDomNodeList>
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 "../vtools/tools/drawTools/vdrawtool.h"
63 #include "../vundocommand.h"
64 #include "moveabstractlabel.h"
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 MoveOperationLabel::MoveOperationLabel(quint32 idTool, VAbstractPattern *doc, const QPointF &pos, quint32 idPoint,
68  QUndoCommand *parent)
69  : MoveAbstractLabel(doc, idPoint, pos, parent)
70  , m_idTool(idTool)
71  , m_scene(qApp->getCurrentScene())
72 {
73  setText(tr("move point label"));
74 
75  qCDebug(vUndo, "Tool id %u", m_idTool);
76 
77  const QDomElement element = getDestinationObject(m_idTool, nodeId);
78  if (element.isElement())
79  {
80  m_oldPos.rx() = qApp->toPixel(doc->GetParametrDouble(element, AttrMx, "0.0"));
81  m_oldPos.ry() = qApp->toPixel(doc->GetParametrDouble(element, AttrMy, "0.0"));
82 
83  qCDebug(vUndo, "Label old Mx %f", m_oldPos.x());
84  qCDebug(vUndo, "Label old My %f", m_oldPos.y());
85  }
86  else
87  {
88  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
89  return;
90  }
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
94 bool MoveOperationLabel::mergeWith(const QUndoCommand *command)
95 {
96  const MoveOperationLabel *moveCommand = static_cast<const MoveOperationLabel *>(command);
97  SCASSERT(moveCommand != nullptr)
98 
99  if (moveCommand->GetToolId() != m_idTool && moveCommand->GetPointId() != nodeId)
100  {
101  return false;
102  }
103 
104  qCDebug(vUndo, "Merge with undo.");
105  m_newPos = moveCommand->GetNewPos();
106  qCDebug(vUndo, "Label new Mx %f", m_newPos.x());
107  qCDebug(vUndo, "Label new My %f", m_newPos.y());
108  return true;
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
113 {
114  return static_cast<int>(UndoCommand::RotationMoveLabel);
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
118 void MoveOperationLabel::Do(const QPointF &pos)
119 {
120  qCDebug(vUndo, "New mx %f", pos.x());
121  qCDebug(vUndo, "New my %f", pos.y());
122 
123  QDomElement domElement = getDestinationObject(m_idTool, nodeId);
124  if (not domElement.isNull() && domElement.isElement())
125  {
126  doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(pos.x())));
127  doc->SetAttribute(domElement, AttrMy, QString().setNum(qApp->fromPixel(pos.y())));
128 
129  if (VDrawTool *tool = qobject_cast<VDrawTool *>(VAbstractPattern::getTool(m_idTool)))
130  {
131  tool->setPointNamePosition(nodeId, pos);
132  }
133  }
134  else
135  {
136  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
137  }
138 }
QPointF GetNewPos() const
quint32 GetPointId() const
virtual void Do(const QPointF &pos) Q_DECL_OVERRIDE
quint32 GetToolId() const
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE
virtual int id() const Q_DECL_OVERRIDE
MoveOperationLabel(quint32 idTool, VAbstractPattern *doc, const QPointF &pos, quint32 idPoint, QUndoCommand *parent=nullptr)
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
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
QDomElement getDestinationObject(quint32 idTool, quint32 idPoint) const
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