Seamly2D
Code documentation
add_groupitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ** @file add_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 "add_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 AddGroupItem::AddGroupItem(const QDomElement &xml, VAbstractPattern *doc, quint32 groupId, QUndoCommand *parent)
40  : VUndoCommand(xml, doc, parent)
41  , m_activeDraftblockName(doc->getActiveDraftBlockName())
42 {
43  setText(tr("Add item to group"));
44  nodeId = groupId;
45 }
46 
47 //---------------------------------------------------------------------------------------------------------------------
49 {
50 }
51 
52 //---------------------------------------------------------------------------------------------------------------------
54 {
55  qCDebug(vUndo, "Undo add group item");
56 
57  doc->changeActiveDraftBlock(m_activeDraftblockName);//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.removeChild(xml).isNull())
63  {
64  qCDebug(vUndo, "Can't delete item.");
65  return;
66  }
67 
68  doc->SetModified(true);
69  emit qApp->getCurrentDocument()->patternChanged(false);
70 
71  // set the item visible. Because if the undo is done when invisibile and it's not in any group after the
72  // undo, it stays invisible until the entire drawing is completly rerendered.
73  quint32 objectId = doc->GetParametrUInt(xml,QString("object"),NULL_ID_STR);
74  quint32 toolId = doc->GetParametrUInt(xml,QString("tool"),NULL_ID_STR);
75  VDataTool* tool = doc->getTool(toolId);
76  tool->GroupVisibility(objectId,true);
77 
78  QDomElement groups = doc->createGroups();
79  if (not groups.isNull())
80  {
81  doc->parseGroups(groups);
82  } else
83  {
84  qCDebug(vUndo, "Can't get tag Groups.");
85  return;
86  }
87 
88  emit updateGroups();
89  }
90  else
91  {
92  qCDebug(vUndo, "Can't get group by id = %u.", nodeId);
93  return;
94  }
95 
96  VMainGraphicsView::NewSceneRect(qApp->getCurrentScene(), qApp->getSceneView());
97  emit doc->setCurrentDraftBlock(m_activeDraftblockName);//Return current draft Block after undo
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------------
102 {
103  qCDebug(vUndo, "Redo add group item");
104  doc->changeActiveDraftBlock(m_activeDraftblockName);//Without this user will not see this change
105 
106  QDomElement group = doc->elementById(nodeId, VAbstractPattern::TagGroup);
107  if (group.isElement())
108  {
109  if (group.appendChild(xml).isNull())
110  {
111  qCDebug(vUndo, "Can't add item.");
112  return;
113  }
114 
115  doc->SetModified(true);
116  emit qApp->getCurrentDocument()->patternChanged(false);
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_activeDraftblockName);//Return current draft Block after undo
138 }
virtual void redo() Q_DECL_OVERRIDE
virtual void undo() Q_DECL_OVERRIDE
void updateGroups()
const QString m_activeDraftblockName
Definition: add_groupitem.h:52
AddGroupItem(const QDomElement &xml, VAbstractPattern *doc, quint32 nodeId, QUndoCommand *parent=nullptr)
virtual ~AddGroupItem()
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