Seamly2D
Code documentation
movelabel.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 movelabel.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 25 12, 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 "movelabel.h"
53 
54 #include <QDomElement>
55 
56 #include "../ifc/ifcdef.h"
57 #include "../ifc/xml/vabstractpattern.h"
58 #include "../vmisc/def.h"
59 #include "../vmisc/logging.h"
60 #include "../vmisc/vabstractapplication.h"
61 #include "../vtools/tools/vabstracttool.h"
62 #include "../vwidgets/vmaingraphicsview.h"
63 #include "../vundocommand.h"
64 #include "moveabstractlabel.h"
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 MoveLabel::MoveLabel(VAbstractPattern *doc, const QPointF &pos, const quint32 &id, QUndoCommand *parent)
68  : MoveAbstractLabel(doc, id, pos, parent)
69  , m_scene(qApp->getCurrentScene())
70 {
71  setText(tr("move point label"));
72 
73  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
74  if (domElement.isElement())
75  {
76  m_oldPos.rx() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMx, "0.0"));
77  m_oldPos.ry() = qApp->toPixel(doc->GetParametrDouble(domElement, AttrMy, "0.0"));
78  }
79  else
80  {
81  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
82  }
83 }
84 
85 //---------------------------------------------------------------------------------------------------------------------
86 bool MoveLabel::mergeWith(const QUndoCommand *command)
87 {
88  const MoveLabel *moveCommand = static_cast<const MoveLabel *>(command);
89  SCASSERT(moveCommand != nullptr)
90 
91  if (moveCommand->GetPointId() != nodeId)
92  {
93  return false;
94  }
95 
96  m_newPos = moveCommand->GetNewPos();
97 
98  return true;
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
102 int MoveLabel::id() const
103 {
104  return static_cast<int>(UndoCommand::MoveLabel);
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 void MoveLabel::Do(const QPointF &pos)
109 {
110  QDomElement domElement = doc->elementById(nodeId, VAbstractPattern::TagPoint);
111  if (domElement.isElement())
112  {
113  doc->SetAttribute(domElement, AttrMx, QString().setNum(qApp->fromPixel(pos.x())));
114  doc->SetAttribute(domElement, AttrMy, QString().setNum(qApp->fromPixel(pos.y())));
115 
116  if (VAbstractTool *tool = qobject_cast<VAbstractTool *>(VAbstractPattern::getTool(nodeId)))
117  {
118  tool->setPointNamePosition(nodeId, pos);
119  }
120  }
121  else
122  {
123  qCDebug(vUndo, "Can't find point with id = %u.", nodeId);
124  }
125 }
QPointF GetNewPos() const
quint32 GetPointId() const
virtual int id() const Q_DECL_OVERRIDE
Definition: movelabel.cpp:102
virtual void Do(const QPointF &pos) Q_DECL_OVERRIDE
Definition: movelabel.cpp:108
virtual bool mergeWith(const QUndoCommand *command) Q_DECL_OVERRIDE
Definition: movelabel.cpp:86
MoveLabel(VAbstractPattern *doc, const QPointF &pos, const quint32 &id, QUndoCommand *parent=nullptr)
Definition: movelabel.cpp:67
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
static const QString TagPoint
The VAbstractTool abstract class for all tools.
Definition: vabstracttool.h:80
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
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