HAL
action_add_items_to_object.cpp
Go to the documentation of this file.
2 
5 #include "gui/gui_globals.h"
12 
13 namespace hal
14 {
16  {
17  ;
18  }
19 
21 
23  {
24  return new ActionAddItemsToObject;
25  }
26 
28  {
30  }
31 
33  {
34  cryptoHash.addData("mod", 3);
35  cryptoHash.addData(setToText(mModules).toUtf8());
36  cryptoHash.addData("gat", 3);
37  cryptoHash.addData(setToText(mGates).toUtf8());
38  cryptoHash.addData("net", 3);
39  cryptoHash.addData(setToText(mNets).toUtf8());
40  cryptoHash.addData("pin", 3);
41  cryptoHash.addData(setToText(mPins).toUtf8());
42  }
43 
45  {
46  writeParentObjectToXml(xmlOut);
47 
49  switch (mPlacementHint.mode())
50  {
52  break;
55  xmlOut.writeStartElement("placement");
56  xmlOut.writeAttribute("id", QString::number(mPlacementHint.preferredOrigin().id()));
58  xmlOut.writeAttribute("mode", mPlacementHint.mode() == PlacementHint::PreferLeft ? "left" : "right");
59  xmlOut.writeEndElement();
60  break;
62  xmlOut.writeTextElement("gridposition", gridToText(mPlacementHint.gridPosition()));
63  break;
64  }
65  if (!mModules.isEmpty())
66  xmlOut.writeTextElement("modules", setToText(mModules));
67  if (!mGates.isEmpty())
68  xmlOut.writeTextElement("gates", setToText(mGates));
69  if (!mNets.isEmpty())
70  xmlOut.writeTextElement("nets", setToText(mNets));
71  if (!mPins.isEmpty())
72  xmlOut.writeTextElement("pins", setToText(mPins));
73  }
74 
76  {
77  while (xmlIn.readNextStartElement())
78  {
79  if (xmlIn.name() == "placement")
80  {
81  u32 id = xmlIn.attributes().value("id").toInt();
82  UserActionObjectType::ObjectType tp = UserActionObjectType::fromString(xmlIn.attributes().value("type").toString());
84  mPlacementHint = PlacementHint(mode, Node(id, UserActionObjectType::toNodeType(tp)));
85  xmlIn.skipCurrentElement(); // no text body to read
86  }
87  else if (xmlIn.name() == "gridposition")
88  {
90  mPlacementHint = PlacementHint(gp);
91  }
92  else if (xmlIn.name() == "modules")
93  mModules = setFromText(xmlIn.readElementText());
94  else if (xmlIn.name() == "gates")
95  mGates = setFromText(xmlIn.readElementText());
96  else if (xmlIn.name() == "nets")
97  mNets = setFromText(xmlIn.readElementText());
98  else if (xmlIn.name() == "pins")
99  mPins = setFromText(xmlIn.readElementText());
100  }
101  }
102 
104  {
105  Module* m;
106  GraphContext* ctx;
107  Grouping* grp;
108  mUndoAction = nullptr;
109 
110  LayoutLocker llock;
111 
112  switch (mObject.type())
113  {
116  if (m)
117  {
118  class ChildSet
119  {
120  public:
121  QSet<u32> mods;
122  QSet<u32> gats;
123  ActionAddItemsToObject* actionFactory(u32 moduleId) const
124  {
125  ActionAddItemsToObject* retval = new ActionAddItemsToObject(mods, gats);
127  return retval;
128  }
129  };
130  // hash parent module id and collect moved items to generate UNDO action
131  QHash<u32, ChildSet> parentHash;
132  GridPlacement* plc = nullptr;
134  if (ctx) plc = ctx->getLayouter()->gridPlacementFactory();
135 
136  std::vector<Gate*> gates;
137  for (u32 id : mGates)
138  {
139  Gate* g = gNetlist->get_gate_by_id(id);
140  Q_ASSERT(g);
141  parentHash[g->get_module()->get_id()].gats.insert(id);
142  gates.push_back(g);
143  }
144  m->assign_gates(gates);
145  for (u32 id : mModules)
146  {
147  Module* sm = gNetlist->get_module_by_id(id);
148  Q_ASSERT(sm);
149  u32 parentModuleId = sm->get_parent_module() ? sm->get_parent_module()->get_id() : 1;
150  parentHash[parentModuleId].mods.insert(id);
151  sm->set_parent_module(m);
152  }
153 
154  if (!parentHash.isEmpty())
155  {
156  UserActionCompound* compound = new UserActionCompound;
157  for (auto it = parentHash.begin(); it != parentHash.end(); ++it)
158  compound->addAction(it.value().actionFactory(it.key()));
159  if (ctx && plc)
160  {
161  compound->addAction(new ActionMoveNode(ctx->id(),plc));
162  delete plc;
163  }
164  mUndoAction = compound;
165  }
166  }
167  else
168  return false;
169  break;
172  if (ctx)
173  {
174  ctx->beginChange();
175  ctx->add(mModules, mGates, mPlacementHint);
176  ctx->endChange();
177  }
178  else
179  return false;
180  break;
183  if (grp)
184  {
185  for (u32 id : mModules)
186  grp->assign_module_by_id(id, true);
187  for (u32 id : mGates)
188  grp->assign_gate_by_id(id, true);
189  for (u32 id : mNets)
190  grp->assign_net_by_id(id, true);
191  }
192  else
193  return false;
194  break;
195  default:
196  return false;
197  }
198 
199  if (!mUndoAction)
200  {
201  mUndoAction = new ActionRemoveItemsFromObject(mModules, mGates, mNets);
203  }
204 
205  return UserAction::exec();
206  }
207 } // namespace hal
UserActionFactory for ActionAddItemsToObject.
static ActionAddItemsToObjectFactory * sFactory
Adds an item to a module or grouping.
void writeToXml(QXmlStreamWriter &xmlOut) const override
ActionAddItemsToObject(const QSet< u32 > &mods=QSet< u32 >(), const QSet< u32 > &gats=QSet< u32 >(), const QSet< u32 > &nets=QSet< u32 >(), const QSet< u32 > &pins=QSet< u32 >())
void readFromXml(QXmlStreamReader &xmlIn) override
void addToHash(QCryptographicHash &cryptoHash) const override
Removes an item from a Module or Grouping.
ContextManagerWidget * getContextManagerWidget()
Definition: gate.h:58
Logical container for modules, gates, and nets.
Definition: graph_context.h:55
void add(const QSet< u32 > &modules, const QSet< u32 > &gates, PlacementHint placement=PlacementHint())
GraphLayouter * getLayouter() const
GraphContext * getContextById(u32 id) const
GridPlacement * gridPlacementFactory() const
bool assign_module_by_id(const u32 module_id, bool force=false)
Definition: grouping.cpp:248
bool assign_net_by_id(const u32 net_id, bool force=false)
Definition: grouping.cpp:161
bool assign_gate_by_id(const u32 gate_id, bool force=false)
Definition: grouping.cpp:74
Module * get_parent_module() const
Definition: module.cpp:125
bool set_parent_module(Module *new_parent)
Definition: module.cpp:168
bool assign_gates(const std::vector< Gate * > &gates)
Definition: module.cpp:327
u32 get_id() const
Definition: module.cpp:82
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:193
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:613
Grouping * get_grouping_by_id(u32 grouping_id) const
Definition: netlist.cpp:691
The Node class object represents a module or a gate.
Definition: gui_def.h:61
NodeType type() const
type getter for type information
Definition: gui_def.h:71
u32 id() const
id getter for ID information
Definition: gui_def.h:77
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:196
Node preferredOrigin() const
preferredOrigin getter for placement origin if any.
Definition: gui_def.h:231
PlacementModeType mode() const
mode getter for placement mode type
Definition: gui_def.h:223
PlacementModeType
The PlacementModeType enum either most compact arrangement (Standard) or to the left or right of give...
Definition: gui_def.h:202
const QHash< Node, QPoint > & gridPosition() const
Definition: gui_def.h:273
void addAction(UserAction *act)
The UserActionFactory is the abstract base class for registration.
Definition: user_action.h:225
QString tagname() const
Definition: user_action.h:242
The UserAction class is the abstract base class for user interactions.
Definition: user_action.h:57
void writeParentObjectToXml(QXmlStreamWriter &xmlOut) const
static QString gridToText(const QHash< hal::Node, QPoint > &grid)
Definition: user_action.cpp:69
UserAction * mUndoAction
Definition: user_action.h:186
static QString setToText(const QSet< u32 > &set)
Definition: user_action.cpp:50
static QHash< hal::Node, QPoint > gridFromText(const QString &txt)
Definition: user_action.cpp:84
virtual bool exec()
Definition: user_action.cpp:23
static QSet< u32 > setFromText(const QString &s)
Definition: user_action.cpp:61
virtual void setObject(const UserActionObject &obj)
Definition: user_action.cpp:32
UserActionObject mObject
Definition: user_action.h:183
The UserActionObject class represents a single object used in UserAction.
UserActionObjectType::ObjectType type() const
static QString toString(ObjectType t)
static ObjectType fromNodeType(Node::NodeType ntp)
static Node::NodeType toNodeType(ObjectType t)
ContentManager * gContentManager
Definition: plugin_gui.cpp:78
GraphContextManager * gGraphContextManager
Definition: plugin_gui.cpp:85
Netlist * gNetlist
Definition: plugin_gui.cpp:80
quint32 u32
void addData(const char *data, int length)
QHash::iterator begin()
QHash::iterator end()
QHash::iterator insert(const Key &key, const T &value)
bool isEmpty() const const
bool isEmpty() const const
QString number(int n, int base)
int toInt(bool *ok, int base) const const
QString toString() const const
QStringRef value(const QString &namespaceUri, const QString &name) const const
QXmlStreamAttributes attributes() const const
QStringRef name() const const
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
void writeAttribute(const QString &qualifiedName, const QString &value)
void writeEndElement()
void writeStartElement(const QString &qualifiedName)
void writeTextElement(const QString &qualifiedName, const QString &text)