HAL  v4.5.0-311-gd7fdf7b20
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
graph_context.cpp
Go to the documentation of this file.
3 
12 #include "gui/gui_globals.h"
13 #include "gui/gui_def.h"
17 #include <QVector>
18 #include <QJsonArray>
21 
22 namespace hal
23 {
24  static const bool sLazyUpdates = false;
25 
27  : QObject(parent),
28  mId(id_),
29  mName(name),
30  mParentWidget(nullptr),
31  mDirty(false),
32  mLayouter(nullptr),
33  mShader(nullptr),
34  mUserUpdateCount(0),
35  mUnappliedChanges(false),
36  mSceneUpdateRequired(false),
37  mSceneUpdateInProgress(false),
38  mSpecialUpdate(false),
39  mExclusiveModuleId(0)
40  {
41  mTimestamp = QDateTime::currentDateTime();
42  connect(MainWindow::sSettingStyle,&SettingsItemDropdown::intChanged,this,&GraphContext::handleStyleChanged);
43  connect(gNetlistRelay, &NetlistRelay::moduleNameChanged, this, &GraphContext::handleModuleNameChanged);
44  connect(this, &GraphContext::exclusiveModuleLost, this, &GraphContext::handleExclusiveModuleLost);
45  }
46 
48  {
50  if (mParentWidget) mParentWidget->handleContextAboutToBeDeleted();
51  delete mLayouter;
52  delete mShader;
53  }
54 
56  {
57  assert(layouter);
58 
59  if (mLayouter)
60  delete mLayouter;
61 
62  mLayouter = layouter;
63  }
64 
66  {
67  assert(shader);
68 
69  if (mShader)
70  delete mShader;
71 
72  mShader = shader;
73  }
74 
76  {
77  ++mUserUpdateCount;
78  }
79 
81  {
82  --mUserUpdateCount;
83  if (mUserUpdateCount == 0)
84  {
85  evaluateChanges();
86  update();
87  }
88  }
89 
90  void GraphContext::add(const QSet<u32>& modules, const QSet<u32>& gates, PlacementHint placement)
91  {
92  QSet<u32> new_modules = modules - mModules;
93  QSet<u32> new_gates = gates - mGates;
94 
95  QSet<u32> old_modules = mRemovedModules & modules;
96  QSet<u32> old_gates = mRemovedGates & gates;
97 
98  // if we have a placement hint for the added nodes, we ignore it and leave
99  // the nodes where they are (i.e. we just deschedule their removal and not
100  // re-run the placement algo on them)
101  mRemovedModules -= old_modules;
102  mRemovedGates -= old_gates;
103 
104  mAddedModules += new_modules;
105  mAddedGates += new_gates;
106 
107  mPlacementList[placement.mode()].append(
108  PlacementEntry(placement,new_modules,new_gates));
109 
110  if (mUserUpdateCount == 0)
111  {
112  evaluateChanges();
113  update();
114  }
115  if (!new_modules.isEmpty() || !new_gates.isEmpty()) setDirty(true);
116  }
117 
118  void GraphContext::remove(const QSet<u32>& modules, const QSet<u32>& gates)
119  {
120  QSet<u32> old_modules = modules & mModules;
121  QSet<u32> old_gates = gates & mGates;
122 
123  mRemovedModules += old_modules;
124  mRemovedGates += old_gates;
125 
126  mAddedModules -= modules;
127  mAddedGates -= gates;
128 
129  // We don't update mPlacementList here. Keeping elements
130  // in memory is less of an issue than finding the modules/gates _by value_
131  // in the maps and removing them. At the end of applyChanges() the lists are
132  // cleared anyway.
133 
134  if (mUserUpdateCount == 0)
135  {
136  evaluateChanges();
137  update();
138  }
139  if (!old_modules.isEmpty() || !old_gates.isEmpty()) setDirty(true);
140  }
141 
143  {
144  mRemovedModules = mModules;
145  mRemovedGates = mGates;
146 
147  mAddedModules.clear();
148  mAddedGates.clear();
149 
150  for (int i=0; i<4; i++)
151  mPlacementList[i].clear();
152 
153  if (mUserUpdateCount == 0)
154  {
155  evaluateChanges();
156  update();
157  }
158  }
159 
161  {
162  QSet<u32> containedGates = mGates + mAddedGates - mRemovedGates;
163  return containedGates.contains(gateId);
164  }
165 
166  bool GraphContext::isModuleUnfolded(const u32 moduleId) const
167  {
168  QSet<u32> containedGates = mGates + mAddedGates - mRemovedGates;
169  QSet<u32> containedModules = mModules + mAddedModules - mRemovedModules;
170 
171  auto m = gNetlist->get_module_by_id(moduleId);
172 
173  for (const Gate* g : m->get_gates())
174  {
175  if (!containedGates.contains(g->get_id())) return false;
176  }
177  for (const Module* sm : m->get_submodules())
178  {
179  if (!isModuleUnfolded(sm->get_id()) && !containedModules.contains(sm->get_id()))
180  return false;
181  }
182  return true;
183  }
184 
185  void GraphContext::unfoldModule(const u32 id, const PlacementHint& plc)
186  {
187  auto contained_modules = mModules + mAddedModules - mRemovedModules;
188 
189  if (contained_modules.find(id) != contained_modules.end())
190  {
191  auto m = gNetlist->get_module_by_id(id);
194 
195  Node singleContentNode;
196  PlacementHint childPlc;
197 
198  for (const Gate* g : m->get_gates())
199  {
200  singleContentNode = Node(g->get_id(),Node::Gate);
201  gates.insert(g->get_id());
202  }
203  for (const Module* sm : m->get_submodules())
204  {
205  singleContentNode = Node(sm->get_id(),Node::Module);
206  modules.insert(sm->get_id());
207  }
208 
209  if (plc.mode() == PlacementHint::GridPosition)
210  {
211  // placement determined by caller
212  childPlc = plc;
213  }
214  else if (gates.size() + modules.size() == 1)
215  {
216  // There is only a single child in this module, keep the grid position
218  NetLayoutPoint childPos = mLayouter->positonForNode(Node(id,Node::Module));
219  if (!childPos.isUndefined())
220  childPlc.addGridPosition(singleContentNode,childPos);
221  }
222 
223  // That would unfold the empty module into nothing, meaning there would
224  // be no way back
225  assert(!gates.empty() || !modules.empty());
226 
227  beginChange();
228  mLayouter->prepareRollback();
229  remove({id}, {});
230  add(modules, gates, childPlc);
231  endChange();
232  }
233  }
234 
235  bool GraphContext::empty() const
236  {
237  return mGates.empty() && mModules.empty();
238  }
239 
241  {
242  QSet<u32> tempMod = mModules + mAddedModules - mRemovedModules;
243  QSet<u32> tempGat = mGates + mAddedGates - mRemovedGates;
244  return tempMod.isEmpty() && tempGat.isEmpty();
245  }
246 
247  void GraphContext::testIfAffected(const u32 id, const u32* moduleId, const u32* gateId)
248  {
249  if (testIfAffectedInternal(id, moduleId, gateId))
251  }
252 
253  bool GraphContext::testIfAffectedInternal(const u32 id, const u32* moduleId, const u32* gateId)
254  {
255  Node nd(id,Node::Module);
256  if (getLayouter()->boxes().boxForNode(nd))
257  return true;
258 
259  std::vector<Gate*> modifiedGates;
260  if (moduleId)
261  {
262  Module* m = gNetlist->get_module_by_id(*moduleId);
263  if (m) modifiedGates = m->get_gates(nullptr,true);
264  }
265  if (gateId)
266  {
267  Gate* g = gNetlist->get_gate_by_id(*gateId);
268  if (g) modifiedGates.push_back(g);
269  }
270  for (Gate* mg : modifiedGates)
271  if (getLayouter()->boxes().boxForGate(mg))
272  return true;
273 
274  return false;
275  }
276 
277  void GraphContext::removeModuleContents(const u32 moduleId)
278  {
279  QSet<u32> childGates;
280  QSet<u32> childModules;
281 
282  for (const Gate* g : gNetlist->get_module_by_id(moduleId)->get_gates(nullptr, true))
283  {
284  childGates.insert(g->get_id());
285  }
286  for (const Module* sm : gNetlist->get_module_by_id(moduleId)->get_submodules(nullptr, true))
287  {
288  childModules.insert(sm->get_id());
289  }
290  remove(childModules, childGates);
291  }
292 
293  bool GraphContext::isShowingModule(const u32 id) const
294  {
295  return isShowingModule(id, {}, {}, {}, {});
296  }
297 
298  bool GraphContext::isShowingModule(const u32 id, const QSet<u32>& minus_modules, const QSet<u32>& minus_gates, const QSet<u32>& plus_modules, const QSet<u32>& plus_gates) const
299  {
300  // There are all sorts of problems when we allow this, since now any empty
301  // module thinks that it is every other empty module. Blocking this,
302  // however, essentially means that we must destroy all empty contexts:
303  // With the block in place, a context won't recognize it's module anymore
304  // once the last gate has removed, so adding a new gate won't update the
305  // context.
306  if (empty())
307  {
308  return false;
309  }
310 
311  // TODO deduplicate
314 
316 
317  auto contextGates = (mGates - mRemovedGates) + mAddedGates;
318  auto contextModules = (mModules - mRemovedModules) + mAddedModules;
319  auto moduleGates = (gates - minus_gates) + plus_gates;
320  auto moduleModules = (modules - minus_modules) + plus_modules;
321 
322  return contextGates == moduleGates && contextModules == moduleModules;
323  }
324 
325 
327  {
328  auto contextGates = (mGates - mRemovedGates) + mAddedGates;
329  if (!contextGates.isEmpty()) return false;
330  auto contextModules = (mModules - mRemovedModules) + mAddedModules;
331  if (contextModules.size() != 1) return false;
332  // the module has to be taken from what the context shows once its pending changes are applied, as mModules
333  // may still be empty or hold a module that is about to be removed
334  return (*contextModules.constBegin() == 1); // top_module has ID=1
335  }
336 
337  void GraphContext::getModuleChildrenRecursively(const u32 id, QSet<u32>* gates, QSet<u32>* modules) const
338  {
339 
340  auto containedModules = mModules + mAddedModules - mRemovedModules;
341 
342  Module* m = gNetlist->get_module_by_id(id);
343  if (!m) return;
344 
345  for (const Gate* g : m->get_gates())
346  {
347  gates->insert(g->get_id());
348  }
349  for (const Module* sm : m->get_submodules())
350  {
351  if (!containedModules.contains(sm->get_id()) && isModuleUnfolded(sm->get_id()))
353  else
354  modules->insert(sm->get_id());
355  }
356  }
357 
359  {
360  // Module Context connection is lost when a unfolded submodule (visible in view) is deleted
361  // The connection is lost, because the deletion of an unfolded module does not add its submodules/gates
362  // to the view
363 
364  if (!mExclusiveModuleId) return false;
365 
366  auto containedModules = mModules + mAddedModules - mRemovedModules;
367  auto containedGates = mGates + mAddedGates - mRemovedGates;
368 
369  // folded module
370  if (containedGates.empty() && containedModules.size() == 1 && *containedModules.begin() == mExclusiveModuleId)
371  return true;
372  // unfolded module
373  if (isShowingModule(mExclusiveModuleId, {}, {}, {}, {}))
374  return true;
375  return false;
376  }
377 
378  bool GraphContext::isShowingNetSource(const u32 mNetId) const
379  {
380  auto net = gNetlist->get_net_by_id(mNetId);
381  auto src_pins = net->get_sources();
382 
383  for(auto pin : src_pins)
384  {
385  if(pin->get_gate() != nullptr)
386  {
387  if(mGates.contains(pin->get_gate()->get_id()))
388  return true;
389  }
390  }
391 
392  return false;
393  }
394 
396  {
397  auto net = gNetlist->get_net_by_id(mNetId);
398  auto dst_pins = net->get_destinations();
399 
400  for(auto pin : dst_pins)
401  {
402  if(pin->get_gate() != nullptr)
403  {
404  if(mGates.contains(pin->get_gate()->get_id()))
405  return true;
406  }
407  }
408 
409  return false;
410  }
411 
413  {
414  for (Endpoint* ep : n->get_sources())
415  {
416  Gate* g = ep->get_gate();
417  NodeBox* box = mLayouter->boxes().boxForGate(g);
418  if (box) return box->getNode();
419  }
420  return Node();
421  }
422 
424  {
425  for (Endpoint* ep : n->get_destinations())
426  {
427  Gate* g = ep->get_gate();
428  NodeBox* box = mLayouter->boxes().boxForGate(g);
429  if (box) return box->getNode();
430  }
431  return Node();
432  }
433 
435  {
436  return mModules;
437  }
438 
440  {
441  return mGates;
442  }
443 
445  {
446  return mNets;
447  }
448 
450  {
451  return mLayouter->scene();
452  }
453 
455  {
456  return mId;
457  }
458 
460  {
461  return mName;
462  }
463 
465  {
466  if (mDirty) return mName + "*";
467  return mName;
468  }
469 
471  {
472  return mSceneUpdateInProgress;
473  }
474 
476  {
477  mSceneUpdateRequired = true;
478 
479  if (sLazyUpdates)
480  if (!mParentWidget)
481  return;
482 
483  if(mUserUpdateCount == 0)
484  update();
485  }
486 
487  void GraphContext::refreshModule(const u32 moduleId)
488  {
489  NodeBox* box = getLayouter()->boxes().boxForNode(Node(moduleId,Node::Module));
490  if (!box) return;
491  Module* m = gNetlist->get_module_by_id(moduleId);
492  if (!m) return;
493  GraphicsModule* gm = dynamic_cast<GraphicsModule*>(box->item());
494  if (!gm) return;
495  gm->setModuleLabel(m);
496  gm->update();
497  }
498 
500  {
501  if (mGates.contains(id))
502  return Node(id, Node::Gate);
503 
504  Gate* g = gNetlist->get_gate_by_id(id);
505 
506  if (!g)
507  return Node();
508 
509  Module* m = g->get_module();
510 
511  while (m)
512  {
513  if (mModules.contains(m->get_id()))
514  return Node(m->get_id(), Node::Module);
515  m = m->get_parent_module();
516  }
517 
518  return Node();
519  }
520 
521  /*GraphLayouter* GraphContext::debugGetLayouter() const
522  {
523  return mLayouter;
524  }*/
525 
526  void GraphContext::layoutProgress(int percent) const
527  {
528  QString text;
529  if (!percent) text = QString("Layout %1[%2]").arg(mName).arg(mId);
530  if (mParentWidget) mParentWidget->showProgress(percent,text);
531  }
532 
534  {
535  if (mParentWidget) mParentWidget->storeViewport();
536  }
537 
539  {
540  mLayouter->updatePlacement(plc);
542  }
543 
544  void GraphContext::handleLayouterFinished()
545  {
546  if (mUnappliedChanges)
547  applyChanges();
548 
549  if (mSceneUpdateRequired)
550  {
551  requireSceneUpdate();
552  }
553  else
554  {
555  mShader->update();
556  mLayouter->scene()->updateVisuals(mShader->getShading());
557 
558  mSceneUpdateInProgress = false;
559 
560  mLayouter->scene()->connectAll();
561 
562  if (mParentWidget) mParentWidget->handleSceneAvailable();
563  }
564  }
565 
566  void GraphContext::evaluateChanges()
567  {
568  if (!mAddedGates.isEmpty() || !mRemovedGates.isEmpty() || !mAddedModules.isEmpty() || !mRemovedModules.isEmpty())
569  mUnappliedChanges = true;
570  }
571 
572  void GraphContext::update()
573  {
574  if (mSceneUpdateInProgress)
575  return;
576 
577  storeViewport();
578 
579  if (mUnappliedChanges)
580  applyChanges();
581 
582  if (mSceneUpdateRequired)
583  requireSceneUpdate();
584  }
585 
586  void GraphContext::applyChanges()
587  {
588  // since changes are only applied once in a while added module might not exist any more
589  auto it = mAddedModules.begin();
590  while (it != mAddedModules.end())
591  {
592  if (gNetlist->get_module_by_id(*it))
593  ++it;
594  else
595  it = mAddedModules.erase(it);
596  }
597 
598  mModules -= mRemovedModules;
599  mGates -= mRemovedGates;
600 
601  mModules += mAddedModules;
602  mGates += mAddedGates;
603 
604  mLayouter->remove(mRemovedModules, mRemovedGates, mNets);
605  mShader->remove(mRemovedModules, mRemovedGates, mNets);
606 
607  updateNets();
608 
609  int placementOrder[4] = { PlacementHint::GridPosition,
613 
614  for (int iplc = 0; iplc<4; iplc++)
615  {
616  for (const PlacementEntry& plcEntry : mPlacementList[placementOrder[iplc]])
617  {
618  QSet<u32> modsForHint = plcEntry.mModules;
619  modsForHint &= mAddedModules; // may contain obsolete entries that we must filter out
620  QSet<u32> gatsForHint = plcEntry.mGates;
621  gatsForHint &= mAddedGates;
622  mLayouter->add(modsForHint, gatsForHint, mNets, plcEntry.mPlacementHint);
623  }
624  }
625 
626  mShader->add(mAddedModules, mAddedGates, mNets);
627 
628  mAddedModules.clear();
629  mAddedGates.clear();
630 
631  mRemovedModules.clear();
632  mRemovedGates.clear();
633 
634  for (int i=0; i<4; i++)
635  mPlacementList[i].clear();
636 
637  mUnappliedChanges = false;
638  mSceneUpdateRequired = true;
639  }
640 
642  {
643  mNets.clear();
644  for (const auto& id : mGates)
645  {
646  if (mRemovedGates.contains(id)) continue;
647  auto g = gNetlist->get_gate_by_id(id);
648  if (!g) continue;
649  for (auto net : g->get_fan_in_nets())
650  {
651  //if(!net->is_unrouted() || net->is_global_input_net() || net->is_global_output_net())
652  mNets.insert(net->get_id());
653  }
654  for (auto net : g->get_fan_out_nets())
655  {
656  //if(!net->is_unrouted() || net->is_global_input_net() || net->is_global_output_net())
657  mNets.insert(net->get_id());
658  }
659  }
660  for (const auto& id : mModules)
661  {
662  if (mRemovedModules.contains(id)) continue;
663  auto m = gNetlist->get_module_by_id(id);
664  if (!m) continue;
665  for (auto net : m->get_input_nets())
666  {
667  mNets.insert(net->get_id());
668  }
669  for (auto net : m->get_output_nets())
670  {
671  mNets.insert(net->get_id());
672  }
673  }
674  }
675 
676  void GraphContext::requireSceneUpdate()
677  {
678  if (LayoutLockerManager::instance()->canUpdate(this))
679  startSceneUpdate();
680  }
681 
682  void GraphContext::startSceneUpdate()
683  {
684  mSceneUpdateRequired = false;
685  mSceneUpdateInProgress = true;
686 
687  if (mParentWidget) mParentWidget->handleSceneUnavailable();
688  mLayouter->scene()->disconnectAll();
689 
690  // LayouterTask* task = new LayouterTask(mLayouter);
691  // connect(task, &LayouterTask::finished, this, &GraphContext::handleLayouterFinished, Qt::ConnectionType::QueuedConnection);
692  // gThreadPool->queueTask(task);
693 
695 
696  mLayouter->layout();
697  handleLayouterFinished();
698  }
699 
701  {
702  if (!mSceneUpdateInProgress) return;
703  if (!mLayouter->rollback()) return;
704  mGates.clear();
705  mModules.clear();
706  for (const Node& nd : mLayouter->nodeToPositionMap().keys())
707  {
708  switch (nd.type())
709  {
710  case Node::Module: mModules.insert(nd.id()); break;
711  case Node::Gate: mGates.insert(nd.id()); break;
712  default: break;
713  }
714  }
715  mLayouter->layout();
716  handleLayouterFinished();
717  }
718 
719  void GraphContext::handleStyleChanged(int istyle)
720  {
721  Q_UNUSED(istyle);
722  handleLayouterFinished();
723  }
724 
726  {
729  }
730 
731  void GraphContext::handleModuleNameChanged(Module* m)
732  {
733  if (mExclusiveModuleId == m->get_id())
734  {
735  QString name = QString::fromStdString(m->get_name()) + " (ID: " + QString::number(m->get_id()) + ")";
738  act->exec();
739  }
740  Q_EMIT(dataChanged());
741  }
742 
744  {
745  return mTimestamp;
746  }
747 
749  {
750  if (json.contains("timestamp") && json["timestamp"].isString())
751  mTimestamp = QDateTime::fromString(json["timestamp"].toString());
752 
753  QList<QPair<Node,QPoint>> nodesToPlace;
754 
755  if (json.contains("modules") && json["modules"].isArray())
756  {
757  QJsonArray jsonMods = json["modules"].toArray();
758  int nmods = jsonMods.size();
759  for (int imod=0; imod<nmods; imod++)
760  {
761  QJsonObject jsonMod = jsonMods.at(imod).toObject();
762  if (!jsonMod.contains("id") || !jsonMod["id"].isDouble()) continue;
763  u32 id = jsonMod["id"].toInt();
764  if (!gNetlist->get_module_by_id(id))
765  {
766  log_warning("gui", "Module id={} not found in netlist, view id={} not restored.", id, mId);
767  return false;
768  }
769  if (!jsonMod.contains("x") || !jsonMod["x"].isDouble()) continue;
770  int x = jsonMod["x"].toInt();
771  if (!jsonMod.contains("y") || !jsonMod["y"].isDouble()) continue;
772  int y = jsonMod["y"].toInt();
773  Node nd(id,Node::Module);
774  nodesToPlace.append(QPair<Node,QPoint>(nd,QPoint(x,y)));
775  }
776  }
777 
778  if (json.contains("gates") && json["gates"].isArray())
779  {
780  QJsonArray jsonGats = json["gates"].toArray();
781  int ngats = jsonGats.size();
782  for (int igat=0; igat<ngats; igat++)
783  {
784  QJsonObject jsonGat = jsonGats.at(igat).toObject();
785  if (!jsonGat.contains("id") || !jsonGat["id"].isDouble()) continue;
786  u32 id = jsonGat["id"].toInt();
787  if (!gNetlist->get_gate_by_id(id))
788  {
789  log_warning("gui", "Gate id={} not found in netlist, view id={} not restored.", id, mId);
790  return false;
791  }
792  if (!jsonGat.contains("x") || !jsonGat["x"].isDouble()) continue;
793  int x = jsonGat["x"].toInt();
794  if (!jsonGat.contains("y") || !jsonGat["y"].isDouble()) continue;
795  int y = jsonGat["y"].toInt();
796  Node nd(id,Node::Gate);
797  nodesToPlace.append(QPair<Node,QPoint>(nd,QPoint(x,y)));
798  }
799  }
800 
801  if (nodesToPlace.isEmpty())
802  {
803  log_warning("gui", "Cannot restore view id={}, there are no nodes to place.", mId);
804  return false;
805  }
806 
807  mModules.clear();
808  mGates.clear();
809  for (const QPair<Node,QPoint>& box : nodesToPlace)
810  {
811  if (box.first.type() == Node::Module)
812  mModules.insert(box.first.id());
813  else
814  mGates.insert(box.first.id());
815  mLayouter->setNodePosition(box.first,box.second);
816  }
817  if (mModules.size()==1 && mGates.isEmpty())
818  setExclusiveModuleId(*(mModules.begin()),false);
819 
820  if (json.contains("nets") && json["nets"].isArray())
821  {
822  QJsonArray jsonNets = json["nets"].toArray();
823  int nnets = jsonNets.size();
824  for (int inet=0; inet<nnets; inet++)
825  {
826  QJsonObject jsonNet = jsonNets.at(inet).toObject();
827  if (!jsonNet.contains("id") || !jsonNet["id"].isDouble()) continue;
828  u32 id = jsonNet["id"].toInt();
829  if (!gNetlist->get_net_by_id(id)) return false;
830  mNets.insert(id);
831  }
832  }
833 
834  //scheduleSceneUpdate();
835  setDirty(false);
836  return true;
837  }
838 
839  void GraphContext::writeToFile(QJsonObject& json, int parentId)
840  {
841  json["id"] = (int) mId;
842  json["name"] = mName;
843  json["timestamp"] = mTimestamp.toString();
844  json["exclusiveModuleId"] = (int) mExclusiveModuleId;
845  json["visible"] = gContentManager->getGraphTabWidget()->visibleStatus(this);
846  json["parentId"] = (int) parentId;
848  QJsonArray jsonMods;
849  for (u32 id : mModules)
850  {
852  Q_ASSERT(!pos.isUndefined());
853  QJsonObject jsonMod;
854  jsonMod["id"] = (int) id;
855  jsonMod["x"] = (int) pos.x();
856  jsonMod["y"] = (int) pos.y();
857  jsonMods.append(jsonMod);
858  }
859  json["modules"] = jsonMods;
860 
862  QJsonArray jsonGats;
863  for (u32 id : mGates)
864  {
866  Q_ASSERT(!pos.isUndefined());
867  QJsonObject jsonGat;
868  jsonGat["id"] = (int) id;
869  jsonGat["x"] = (int) pos.x();
870  jsonGat["y"] = (int) pos.y();
871  jsonGats.append(jsonGat);
872  }
873  json["gates"] = jsonGats;
874 
876  QJsonArray jsonNets;
877  for (u32 id : mNets)
878  {
879  QJsonObject jsonNet;
880  jsonNet["id"] = (int) id;
881  jsonNets.append(jsonNet);
882  }
883  json["nets"] = jsonNets;
884  setDirty(false);
885  }
886 
887  void GraphContext::setDirty(bool dty)
888  {
889  if (mDirty==dty) return;
890  mDirty = dty;
891  Q_EMIT(dataChanged());
892  }
893 
894  void GraphContext::setScheduleRemove(const QSet<u32>& mods, const QSet<u32>& gats)
895  {
896  mScheduleRemoveModules = mods;
897  mScheduleRemoveGates = gats;
898  }
899 
901  {
902  switch (nd.type()) {
903  case Node::Module:
904  {
905  auto it = mScheduleRemoveModules.find(nd.id());
906  if (it != mScheduleRemoveModules.end())
907  {
908  mScheduleRemoveModules.erase(it);
909  return true;
910  }
911  break;
912  }
913  case Node::Gate:
914  {
915  auto it = mScheduleRemoveGates.find(nd.id());
916  if (it != mScheduleRemoveGates.end())
917  {
918  mScheduleRemoveGates.erase(it);
919  return true;
920  }
921  break;
922  }
923  default:
924  break;
925  }
926  return false;
927  }
928 
930  {
931  mSpecialUpdate = state;
932  }
933 
935  {
936  if (mParentWidget)
937  mParentWidget->showComments(nd);
938  }
939 
940  void GraphContext::setExclusiveModuleId(u32 id, bool emitSignal)
941  {
942  u32 old_id = mExclusiveModuleId;
943  mExclusiveModuleId = id;
944 
945  // Emit signal if context is not showing an exclusive module anymore
946  if ((id == 0 && old_id != 0) && emitSignal)
947  Q_EMIT(exclusiveModuleLost(old_id));
948  }
949 
950  void GraphContext::handleExclusiveModuleLost(u32 old_id)
951  {
952  Module* m = gNetlist->get_module_by_id(old_id);
953  assert(m);
954 
955  u32 cnt = 0;
956  while (true)
957  {
958  ++cnt;
959  QString new_name = QString::fromStdString(m->get_name()) + " modified";
960  if (cnt > 1)
961  {
962  new_name += " (" + QString::number(cnt) + ")";
963  }
964  bool found = false;
965  for (const auto& ctx : gGraphContextManager->getContexts())
966  {
967  if (ctx->name() == new_name)
968  {
969  found = true;
970  break;
971  }
972  }
973  if (!found)
974  {
975  ActionRenameObject* act = new ActionRenameObject(new_name);
976  act->setObject(UserActionObject(this->id(),UserActionObjectType::ContextView));
977  act->exec();
978  break;
979  }
980  }
981  Q_EMIT(dataChanged());
982  }
983 }
GraphTabWidget * getGraphTabWidget()
Get hal's graph tab widget.
Definition: gate.h:59
Module * get_module() const
Definition: gate.cpp:211
bool isModuleUnfolded(const u32 moduleId) const
bool isGateUnfolded(u32 gateId) const
void remove(const QSet< u32 > &modules, const QSet< u32 > &gates)
void setSpecialUpdate(bool state)
void writeToFile(QJsonObject &json, int parentId)
void unfoldModule(const u32 id, const PlacementHint &plc)
void refreshModule(const u32 moduleId)
void showComments(const Node &nd)
GraphicsScene * scene()
bool readFromFile(const QJsonObject &json)
Node getNetSource(const Net *n) const
bool isShowingModuleExclusively()
bool isShowingFoldedTopModule() const
void setDirty(bool dty)
bool isShowingModule(const u32 id) const
void setExclusiveModuleId(u32 id, bool emitSignal=true)
void add(const QSet< u32 > &modules, const QSet< u32 > &gates, PlacementHint placement=PlacementHint())
bool empty() const
void updatePlacement(const GridPlacement &plc)
void layoutProgress(int percent) const
Node getNetDestination(const Net *n) const
const QSet< u32 > & gates() const
const QSet< u32 > & modules() const
void testIfAffected(const u32 id, const u32 *moduleId, const u32 *gateId)
Node nodeForGate(const u32 id) const
void exclusiveModuleLost(u32 old_id)
void setScheduleRemove(const QSet< u32 > &mods, const QSet< u32 > &gats)
void setShader(GraphShader *shader)
bool isScheduledRemove(const Node &nd)
QDateTime getTimestamp() const
GraphLayouter * getLayouter() const
GraphContext(u32 id_, const QString &name, QObject *parent=nullptr)
bool willBeEmptied() const
QString getNameWithDirtyState() const
void setLayouter(GraphLayouter *layouter)
QString name() const
const QSet< u32 > & nets() const
bool isShowingNetSource(const u32 mNetId) const
bool sceneUpdateInProgress() const
bool isShowingNetDestination(const u32 mNetId) const
void getModuleChildrenRecursively(const u32 moduleId, QSet< u32 > *gates, QSet< u32 > *modules) const
QVector< GraphContext * > getContexts() const
Base class for all specific layouters.
virtual void remove(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets)=0
void setNodePosition(const Node &n, const QPoint &p)
virtual void add(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets, PlacementHint placement=PlacementHint())=0
NetLayoutPoint positonForNode(const Node &nd) const
const QMap< Node, QPoint > nodeToPositionMap() const
GraphicsScene * scene() const
const NodeBoxes & boxes() const
void updatePlacement(const GridPlacement &plc)
Base class to store and update visual information about the graph.
Definition: graph_shader.h:47
virtual void add(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets)=0
virtual void update()=0
virtual void remove(const QSet< u32 > modules, const QSet< u32 > gates, const QSet< u32 > nets)=0
const Shading & getShading()
int visibleStatus(const GraphContext *ctx) const
void handleSceneUnavailable()
void showProgress(int percent, const QString &text=QString())
void handleSceneAvailable()
void showComments(const Node &nd)
void handleContextAboutToBeDeleted()
Abstract base class for modules.
void setModuleLabel(const Module *m, bool init=false)
Container for a GraphGraphicsView containing gates, nets, and modules.
void updateVisuals(const GraphShader::Shading &s)
static LayoutLockerManager * instance()
void removeWaitingContext(GraphContext *ctx)
static SettingsItemDropdown * sSettingStyle
Definition: main_window.h:302
Module * get_parent_module() const
Definition: module.cpp:126
const std::vector< Gate * > & get_gates() const
Definition: module.cpp:400
std::string get_name() const
Definition: module.cpp:88
const std::unordered_set< Net * > & get_input_nets() const
Definition: module.cpp:549
std::vector< Module * > get_submodules(const std::function< bool(Module *)> &filter=nullptr, bool recursive=false) const
Definition: module.cpp:268
const std::unordered_set< Net * > & get_output_nets() const
Definition: module.cpp:554
u32 get_id() const
Definition: module.cpp:83
Definition: net.h:58
std::vector< Endpoint * > get_destinations(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Definition: net.cpp:450
std::vector< Endpoint * > get_sources(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Definition: net.cpp:276
bool isUndefined() const
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:195
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:615
Net * get_net_by_id(u32 net_id) const
Definition: netlist.cpp:355
void moduleNameChanged(Module *m) const
The NodeBox class represents a node placed at a grid position within a hal view.
Definition: node_box.h:50
GraphicsNode * item() const
item getter for graphics item object
Definition: node_box.h:124
Node getNode() const
getNode getter for node information
Definition: node_box.h:75
NodeBox * boxForNode(const Node &n) const
boxForNode find NodeBox by node
Definition: node_box.h:200
NodeBox * boxForGate(const Gate *g) const
boxForGate find NodeBox by Gate pointer.
Definition: node_box.h:184
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
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
u32 id() const
id getter for ID information
Definition: gui_def.h:77
Container class to store a PlacementHint togerther with a set of modules and gates.
Definition: gui_def.h:301
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:214
void addGridPosition(const Node &nd, const QPoint &p)
Definition: gui_def.h:281
PlacementModeType mode() const
mode getter for placement mode type
Definition: gui_def.h:241
void intChanged(int value)
virtual void setObject(const UserActionObject &obj)
Definition: user_action.cpp:32
The UserActionObject class represents a single object used in UserAction.
uint32_t u32
Definition: defines.h:41
#define log_warning(channel,...)
Definition: log.h:76
Definition: defines.h:45
ContentManager * gContentManager
Definition: plugin_gui.cpp:79
GraphContextManager * gGraphContextManager
Definition: plugin_gui.cpp:86
Netlist * gNetlist
Definition: gui_globals.h:69
NetlistRelay * gNetlistRelay
Definition: gui_globals.h:74
Net * net
std::string name
QDateTime currentDateTime()
QDateTime fromString(const QString &string, Qt::DateFormat format)
QString toString(Qt::DateFormat format) const const
void update(const QRectF &rect)
void append(const QJsonValue &value)
QJsonValue at(int i) const const
int size() const const
bool contains(const QString &key) const const
QJsonObject toObject() const const
void append(const T &value)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int x() const const
int y() const const
QSet::iterator begin()
void clear()
bool contains(const T &value) const const
bool empty() const const
QSet::iterator end()
QSet::iterator erase(QSet::iterator pos)
QSet::iterator find(const T &value)
QSet::iterator insert(const T &value)
bool isEmpty() const const
int size() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromStdString(const std::string &str)
QString number(int n, int base)