Seamly2D
Code documentation
remove_groupitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file remove_groupitem.cpp
3  ** @author Douglas S Caskey
4  ** @date Mar 1, 2023
5  **
6  ** @copyright
7  ** Copyright (C) 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 "remove_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 #include "vundocommand.h"
37 
38 //---------------------------------------------------------------------------------------------------------------------
39 
40 RemoveGroupItem::RemoveGroupItem(const QDomElement &xml, VAbstractPattern *doc, quint32 groupId, QUndoCommand *parent)
41  : VUndoCommand(xml, doc, parent)
42  , m_activeDrawName(doc->getActiveDraftBlockName())
43 {
44  setText(tr("Delete group item"));
45  nodeId = groupId;
46 }
47 
48 //---------------------------------------------------------------------------------------------------------------------
50 {
51 }
52 
53 //---------------------------------------------------------------------------------------------------------------------
55 {
56  qCDebug(vUndo, "Undo delete group item");
57  doc->changeActiveDraftBlock(m_activeDrawName);//Without this user will not see this change
58 
59  QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup);
60  if (group.isElement())
61  {
62  if (group.appendChild(xml).isNull())
63  {
64  qCDebug(vUndo, "Can't add the item.");
65  return;
66  }
67 
68  doc->SetModified(false);
69  emit qApp->getCurrentDocument()->patternChanged(true);
70 
71  QDomElement groups = doc->createGroups();
72  if (not groups.isNull())
73  {
74  doc->parseGroups(groups);
75  } else
76  {
77  qCDebug(vUndo, "Can't get tag Groups.");
78  return;
79  }
80 
81  emit updateGroups();
82  }
83  else
84  {
85  qCDebug(vUndo, "Can't get group by id = %u.", nodeId);
86  return;
87  }
88 
89  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
90  emit doc->setCurrentDraftBlock(m_activeDrawName); //Return current draft Block after undo
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
95 {
96  qCDebug(vUndo, "Redo add group item");
97  doc->changeActiveDraftBlock(m_activeDrawName);//Without this user will not see this change
98 
99  QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup);
100  if (group.isElement())
101  {
102  if (group.removeChild(xml).isNull())
103  {
104  qCDebug(vUndo, "Can't delete item.");
105  return;
106  }
107 
108  doc->SetModified(true);
109  emit qApp->getCurrentDocument()->patternChanged(false);
110 
111  // set the item visible. Because if the undo is done when unvisibile and it's not in any group after the
112  // undo, it stays unvisible until the entire drawing is completly rerendered.
113  quint32 objectId = doc->GetParametrUInt(xml,QString("object"),NULL_ID_STR);
114  quint32 toolId = doc->GetParametrUInt(xml,QString("tool"),NULL_ID_STR);
115  VDataTool* tool = doc->getTool(toolId);
116  tool->GroupVisibility(objectId,true);
117 
118  QDomElement groups = doc->createGroups();
119  if (not groups.isNull())
120  {
121  doc->parseGroups(groups);
122  } else
123  {
124  qCDebug(vUndo, "Can't get tag Groups.");
125  return;
126  }
127 
128  emit updateGroups();
129  }
130  else
131  {
132  qCDebug(vUndo, "Can't get group by id = %u.", nodeId);
133  return;
134  }
135 
136  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
137  emit doc->setCurrentDraftBlock(m_activeDrawName); //Return current draft Block after undo
138 }
RemoveGroupItem(const QDomElement &xml, VAbstractPattern *doc, quint32 groupId, QUndoCommand *parent=nullptr)
virtual void redo() Q_DECL_OVERRIDE
virtual ~RemoveGroupItem()
virtual void undo() Q_DECL_OVERRIDE
const QString m_activeDrawName
QDomElement createGroups()
void SetModified(bool modified)
static const QString TagGroup
void parseGroups(const QDomElement &domElement)
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
void setCurrentDraftBlock(const QString &patterPiece)
void changeActiveDraftBlock(const QString &name, const Document &parse=Document::FullParse)
changeActiveDraftBlock set new active draft block name.
The VDataTool class need for getting access to data container of tool.
Definition: vdatatool.h:71
virtual void GroupVisibility(quint32 object, bool visible)=0
QDomElement elementById(quint32 id, const QString &tagName=QString())
static quint32 GetParametrUInt(const QDomElement &domElement, const QString &name, const QString &defValue)
Returns the long long value of the given attribute. RENAME: GetParameterLongLong?
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.
QDomElement xml
Definition: vundocommand.h:105
VAbstractPattern * doc
Definition: vundocommand.h:106
quint32 nodeId
Definition: vundocommand.h:107
#define NULL_ID_STR
Definition: ifcdef.h:77
#define qApp
Definition: vapplication.h:67