Seamly2D
Code documentation
move_groupitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file move_groupitem.cpp
3  ** @author Douglas S Caskey
4  ** @date Mar 2, 2023
5  **
6  ** @copyright
7  ** Copyright (C) 2017 - 2023 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 #include "move_groupitem.h"
26 
27 #include <QDomNode>
28 #include <QDomNodeList>
29 
30 #include "../vmisc/logging.h"
31 #include "../vmisc/vabstractapplication.h"
32 #include "../vmisc/def.h"
33 #include "../vwidgets/vmaingraphicsview.h"
34 #include "../ifc/xml/vabstractpattern.h"
35 #include "../vtools/tools/vdatatool.h"
36 
37 //---------------------------------------------------------------------------------------------------------------------
38 
39 MoveGroupItem::MoveGroupItem(const QDomElement &source, const QDomElement &dest, VAbstractPattern *doc,
40  quint32 sourceId, quint32 destinationId, QUndoCommand *parent)
41  : VUndoCommand(source, doc, parent)
42  , m_source(source)
43  , m_dest(dest)
44  , m_activeDraftblockName(doc->getActiveDraftBlockName())
45  , m_sourceGroupId(sourceId)
46  , m_destinationGroupId(destinationId)
47 {
48  setText(tr("Move group item"));
49 }
50 
51 //---------------------------------------------------------------------------------------------------------------------
53 {
54 }
55 
56 //---------------------------------------------------------------------------------------------------------------------
58 {
59  qCDebug(vUndo, "Undo move group item");
60  doc->changeActiveDraftBlock(m_activeDraftblockName);//Without this user will not see this change
61 
62  QDomElement sourceGroup = doc->elementById(m_sourceGroupId, VAbstractPattern::TagGroup);
63  QDomElement destintationGroup = doc->elementById(m_destinationGroupId, VAbstractPattern::TagGroup);
64  if (sourceGroup.isElement() && destintationGroup.isElement())
65  {
66  if (sourceGroup.appendChild(m_source).isNull())
67  {
68  qCDebug(vUndo, "Can't move the item.");
69  return;
70  }
71 
72  if (destintationGroup.removeChild(m_dest).isNull())
73  {
74  qCDebug(vUndo, "Can't move item.");
75  //sourceGroup.removeChild(m_source);
76  return;
77  }
78 
79  doc->SetModified(false);
80  emit qApp->getCurrentDocument()->patternChanged(true);
81 
82  QDomElement groups = doc->createGroups();
83  if (not groups.isNull())
84  {
85  doc->parseGroups(groups);
86  } else
87  {
88  qCDebug(vUndo, "Can't get tag Groups.");
89  return;
90  }
91 
92  emit updateGroups();
93  }
94  else
95  {
96  qCDebug(vUndo, "Can't get group by id = %u.", m_sourceGroupId);
97  return;
98  }
99 
100  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
101  emit doc->setCurrentDraftBlock(m_activeDraftblockName);//Return current draft Block after undo
102 
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
107 {
108  qCDebug(vUndo, "Redo move group item");
109  doc->changeActiveDraftBlock(m_activeDraftblockName);//Without this user will not see this change
110 
111  QDomElement sourceGroup = doc->elementById(m_sourceGroupId, VAbstractPattern::TagGroup);
112  QDomElement destintationGroup = doc->elementById(m_destinationGroupId, VAbstractPattern::TagGroup);
113  if (sourceGroup.isElement() && destintationGroup.isElement())
114  {
115  if (destintationGroup.appendChild(m_dest).isNull())
116  {
117  qCDebug(vUndo, "Can't move the item.");
118  return;
119  }
120 
121  if (sourceGroup.removeChild(m_source).isNull())
122  {
123  qCDebug(vUndo, "Can't move item.");
124  //destintationGroup.removeChild(m_dest);
125  return;
126  }
127 
128  doc->SetModified(false);
129  emit qApp->getCurrentDocument()->patternChanged(true);
130 
131  QDomElement groups = doc->createGroups();
132  if (not groups.isNull())
133  {
134  doc->parseGroups(groups);
135  } else
136  {
137  qCDebug(vUndo, "Can't get tag Groups.");
138  return;
139  }
140 
141  emit updateGroups();
142  }
143  else
144  {
145  qCDebug(vUndo, "Can't get group by id = %u.", m_sourceGroupId);
146  return;
147  }
148 
149  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
150  emit doc->setCurrentDraftBlock(m_activeDraftblockName); //Return current draft Block after undo
151 }
virtual void redo() Q_DECL_OVERRIDE
virtual void undo() Q_DECL_OVERRIDE
QDomElement m_dest
QDomElement m_source
MoveGroupItem(const QDomElement &source, const QDomElement &dest, VAbstractPattern *doc, quint32 sourceId, quint32 destinationId, QUndoCommand *parent=nullptr)
virtual ~MoveGroupItem()
quint32 m_destinationGroupId
const QString m_activeDraftblockName
quint32 m_sourceGroupId
void updateGroups()
QDomElement createGroups()
void SetModified(bool modified)
static const QString TagGroup
void parseGroups(const QDomElement &domElement)
void setCurrentDraftBlock(const QString &patterPiece)
void changeActiveDraftBlock(const QString &name, const Document &parse=Document::FullParse)
changeActiveDraftBlock set new active draft block name.
QDomElement elementById(quint32 id, const QString &tagName=QString())
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
#define qApp
Definition: vapplication.h:67