HAL
gui_api.cpp
Go to the documentation of this file.
1 #include "gui/gui_api/gui_api.h"
2 
3 
4 #include "gui/gui_globals.h"
19 #include "hal_core/utilities/log.h"
21 
22 #include <algorithm>
23 
24 #include <QSet>
25 
26 namespace hal
27 {
29  {
30  gSelectionRelay->registerSender(this, "GUI API");
31  }
32 
33  std::vector<u32> GuiApi::getSelectedGateIds()
34  {
36  }
37 
38  std::vector<u32> GuiApi::getSelectedNetIds()
39  {
41  }
42 
43  std::vector<u32> GuiApi::getSelectedModuleIds()
44  {
46  }
47 
48  std::tuple<std::vector<u32>, std::vector<u32>, std::vector<u32>> GuiApi::getSelectedItemIds()
49  {
50  return std::make_tuple(getSelectedGateIds(), getSelectedNetIds(), getSelectedModuleIds());
51  }
52 
53  std::vector<Gate*> GuiApi::getSelectedGates()
54  {
55  std::vector<Gate*> gates;
57  gates.push_back(gNetlist->get_gate_by_id(id));
58  return gates;
59  }
60 
61  std::vector<Net*> GuiApi::getSelectedNets()
62  {
63  std::vector<Net*> nets;
65  nets.push_back(gNetlist->get_net_by_id(id));
66  return nets;
67  }
68 
69  std::vector<Module*> GuiApi::getSelectedModules()
70  {
71  std::vector<Module*> modules;
73  modules.push_back(gNetlist->get_module_by_id(id));
74  return modules;
75  }
76 
77  std::tuple<std::vector<Gate*>, std::vector<Net*>, std::vector<Module*>> GuiApi::getSelectedItems()
78  {
79  return std::make_tuple(getSelectedGates(), getSelectedNets(), getSelectedModules());
80  }
81 
82  void GuiApi::selectGate(Gate* gate, bool clear_current_selection, bool navigate_to_selection)
83  {
84  if(!gNetlist->is_gate_in_netlist(gate))
85  return;
86 
87  if(clear_current_selection)
89 
90  gSelectionRelay->addGate(gate->get_id());
93 
94  if(navigate_to_selection)
96  }
97 
98  void GuiApi::selectGate(u32 gate_id, bool clear_current_selection, bool navigate_to_selection)
99  {
100  selectGate(gNetlist->get_gate_by_id(gate_id), clear_current_selection, navigate_to_selection);
101  }
102 
103  void GuiApi::selectGate(const std::vector<Gate*>& gates, bool clear_current_selection, bool navigate_to_selection)
104  {
105  QSet<u32> gate_ids;
106 
107  for(auto gate : gates)
108  {
109  if(!gNetlist->is_gate_in_netlist(gate))
110  return;
111 
112  gate_ids.insert(gate->get_id());
113  }
114 
115  if(clear_current_selection)
117  else
118  gate_ids.unite(gSelectionRelay->selectedGates());
121 
122  if(navigate_to_selection)
124  }
125 
126  void GuiApi::selectGate(const std::vector<u32>& gate_ids, bool clear_current_selection, bool navigate_to_selection)
127  {
128  std::vector<Gate*> gates(gate_ids.size());
129  std::transform(gate_ids.begin(), gate_ids.end(), gates.begin(), [](u32 gate_id){return gNetlist->get_gate_by_id(gate_id);});
130  selectGate(gates, clear_current_selection, navigate_to_selection);
131  }
132 
133  void GuiApi::selectNet(Net* net, bool clear_current_selection, bool navigate_to_selection)
134  {
136  return;
137 
138  if(clear_current_selection)
140 
141  gSelectionRelay->addNet(net->get_id());
144 
145  if(navigate_to_selection)
147  }
148 
149  void GuiApi::selectNet(u32 netId, bool clear_current_selection, bool navigate_to_selection)
150  {
151  selectNet(gNetlist->get_net_by_id(netId), clear_current_selection, navigate_to_selection);
152  }
153 
154  void GuiApi::selectNet(const std::vector<Net*>& nets, bool clear_current_selection, bool navigate_to_selection)
155  {
156  QSet<u32> net_ids;
157 
158  for(auto net : nets)
159  {
161  return;
162 
163  net_ids.insert(net->get_id());
164  }
165 
166  if(clear_current_selection)
168  else
169  net_ids.unite(gSelectionRelay->selectedNets());
172 
173  if(navigate_to_selection)
175  }
176 
177  void GuiApi::selectNet(const std::vector<u32>& net_ids, bool clear_current_selection, bool navigate_to_selection)
178  {
179  std::vector<Net*> nets(net_ids.size());
180  std::transform(net_ids.begin(), net_ids.end(), nets.begin(), [](u32 mNetId){return gNetlist->get_net_by_id(mNetId);});
181  selectNet(nets, clear_current_selection, navigate_to_selection);
182  }
183 
184  void GuiApi::selectModule(Module* module, bool clear_current_selection, bool navigate_to_selection)
185  {
187  return;
188 
189  if(clear_current_selection)
191 
195 
196  if(navigate_to_selection)
198  }
199 
200  void GuiApi::selectModule(u32 module_id, bool clear_current_selection, bool navigate_to_selection)
201  {
202  selectModule(gNetlist->get_module_by_id(module_id), clear_current_selection, navigate_to_selection);
203  }
204 
205  void GuiApi::selectModule(const std::vector<Module*>& modules, bool clear_current_selection, bool navigate_to_selection)
206  {
207  QSet<u32> module_ids;
208 
209  for(auto module : modules)
210  {
212  return;
213 
214  module_ids.insert(module->get_id());
215  }
216 
217  if(clear_current_selection)
219  else
220  module_ids.unite(gSelectionRelay->selectedModules());
221  gSelectionRelay->setSelectedModules(module_ids);
223 
224  if(navigate_to_selection)
226  }
227 
228  void GuiApi::selectModule(const std::vector<u32>& module_ids, bool clear_current_selection, bool navigate_to_selection)
229  {
230  std::vector<Module*> modules(module_ids.size());
231  std::transform(module_ids.begin(), module_ids.end(), modules.begin(), [](u32 g_id){return gNetlist->get_module_by_id(g_id);});
232  selectModule(modules, clear_current_selection, navigate_to_selection);
233  }
234 
235  void GuiApi::select(Gate* gate, bool clear_current_selection, bool navigate_to_selection)
236  {
237  selectGate(gate, clear_current_selection, navigate_to_selection);
238  }
239 
240  void GuiApi::select(Net* net, bool clear_current_selection, bool navigate_to_selection)
241  {
242  selectNet(net, clear_current_selection, navigate_to_selection);
243  }
244  void GuiApi::select(Module* module, bool clear_current_selection, bool navigate_to_selection)
245  {
246  selectModule(module, clear_current_selection, navigate_to_selection);
247  }
248 
249  void GuiApi::select(const std::vector<Gate*>& gates, bool clear_current_selection, bool navigate_to_selection)
250  {
251  selectGate(gates, clear_current_selection, navigate_to_selection);
252  }
253  void GuiApi::select(const std::vector<Net*>& nets, bool clear_current_selection, bool navigate_to_selection)
254  {
255  selectNet(nets, clear_current_selection, navigate_to_selection);
256  }
257 
258  void GuiApi::select(const std::vector<Module*>& modules, bool clear_current_selection, bool navigate_to_selection)
259  {
260  selectModule(modules, clear_current_selection, navigate_to_selection);
261  }
262 
263  void GuiApi::select(const std::vector<u32>& gate_ids, const std::vector<u32>& net_ids, const std::vector<u32>& module_ids, bool clear_current_selection, bool navigate_to_selection)
264  {
265  if(clear_current_selection)
267 
268  selectGate(gate_ids, false, false);
269  selectNet(net_ids, false, false);
270  selectModule(module_ids, false, false);
271 
272  if(navigate_to_selection)
274  }
275 
276  void GuiApi::select(const std::vector<Gate*>& gates, const std::vector<Net*>& nets, const std::vector<Module*>& modules, bool clear_current_selection, bool navigate_to_selection)
277  {
278  if(clear_current_selection)
280 
281  selectGate(gates, false, false);
282  selectNet(nets, false, false);
283  selectModule(modules, false, false);
284 
285  if(navigate_to_selection)
287  }
288 
290  {
291  if(!gNetlist->is_gate_in_netlist(gate))
292  return;
293 
296  }
297 
298  void GuiApi::deselectGate(u32 gate_id)
299  {
301  }
302 
303  void GuiApi::deselectGate(const std::vector<Gate*>& gates)
304  {
306 
307  for(Gate* gate : gates)
308  {
309  if(!gNetlist->is_gate_in_netlist(gate))
310  return;
311 
312  auto it = gate_ids.find(gate->get_id());
313  if (it != gate_ids.end()) gate_ids.erase(it);
314  }
315 
318  }
319 
320  void GuiApi::deselectGate(const std::vector<u32>& gate_ids)
321  {
322  std::vector<Gate*> gates(gate_ids.size());
323  std::transform(gate_ids.begin(), gate_ids.end(), gates.begin(), [](u32 gate_id){return gNetlist->get_gate_by_id(gate_id);});
324  deselectGate(gates);
325  }
326 
328  {
330  return;
331 
332  gSelectionRelay->removeNet(net->get_id());
334  }
335 
337  {
339  }
340 
341  void GuiApi::deselectNet(const std::vector<Net*>& nets)
342  {
344 
345  for(Net* net : nets)
346  {
348  return;
349 
350  auto it = net_ids.find(net->get_id());
351  if (it != net_ids.end()) net_ids.erase(it);
352  }
353 
356  }
357 
358  void GuiApi::deselectNet(const std::vector<u32>& net_ids)
359  {
360  std::vector<Net*> nets(net_ids.size());
361  std::transform(net_ids.begin(), net_ids.end(), nets.begin(), [](u32 mNetId){return gNetlist->get_net_by_id(mNetId);});
362  deselectNet(nets);
363  }
364 
366  {
368  return;
369 
372  }
373 
374  void GuiApi::deselectModule(u32 module_id)
375  {
377  }
378 
379  void GuiApi::deselectModule(const std::vector<Module*>& modules)
380  {
381  QSet<u32> module_ids = gSelectionRelay->selectedModules();
382 
383  for(Module* module : modules)
384  {
386  return;
387  auto it = module_ids.find(module->get_id());
388  if (it != module_ids.end()) module_ids.erase(it);
389  }
390 
391  gSelectionRelay->setSelectedModules(module_ids);
393  }
394 
395  void GuiApi::deselectModule(const std::vector<u32>& module_ids)
396  {
397  std::vector<Module*> modules(module_ids.size());
398  std::transform(module_ids.begin(), module_ids.end(), modules.begin(), [](u32 module_id){return gNetlist->get_module_by_id(module_id);});
399  deselectModule(modules);
400  }
401 
402  void GuiApi::deselect(Gate* gate)
403  {
404  deselectGate(gate);
405  }
406 
408  {
409  deselectNet(net);
410  }
412  {
414  }
415 
416  void GuiApi::deselect(const std::vector<Gate*>& gates)
417  {
418  deselectGate(gates);
419  }
420  void GuiApi::deselect(const std::vector<Net*>& nets)
421  {
422  deselectNet(nets);
423  }
424 
425  void GuiApi::deselect(const std::vector<Module*>& modules)
426  {
427  deselectModule(modules);
428  }
429 
430  void GuiApi::deselect(const std::vector<u32>& gate_ids, const std::vector<u32>& net_ids, const std::vector<u32>& module_ids)
431  {
432  deselectGate(gate_ids);
433  deselectNet(net_ids);
434  deselectModule(module_ids);
435  }
436 
437  void GuiApi::deselect(const std::vector<Gate*>& gates, const std::vector<Net*>& nets, const std::vector<Module*>& modules)
438  {
439  deselectGate(gates);
440  deselectNet(nets);
441  deselectModule(modules);
442  }
443 
445  {
447  }
448 
450  {
451  if (!fromModId || !toModId || fromModId == toModId)
452  {
453  log_warning("gui", "Isolate path from module to module called with faulty parameters");
454  return 0;
455  }
456  Module* fromModule = gNetlist->get_module_by_id(fromModId);
457  if (!fromModule)
458  {
459  log_warning("gui", "Module ID={} to start path from not found.", fromModId);
460  return 0;
461  }
462  Module* toModule = gNetlist->get_module_by_id(toModId);
463  if (!toModule)
464  {
465  log_warning("gui", "Module ID={} to start path from not found.", toModId);
466  return 0;
467  }
468 
469  std::vector<std::vector<Gate*>> pathRows = netlist_utils::get_shortest_path(fromModule,toModule);
470  if (pathRows.empty())
471  {
472  log_warning("gui", "No path found from module ID={} to module ID={}.", fromModId, toModId);
473  return 0;
474  }
475 
476  GridPlacement plc;
477 
478  QSet<u32> moduleIds = {fromModId, toModId};
479  QSet<u32> gateIds;
480  int nrows = pathRows.size();
481  int ncols = pathRows.at(0).size();
482  plc[Node(fromModId,Node::Module)] = QPoint(0,0);
483  plc[Node(toModId,Node::Module)] = QPoint(ncols-1,0);
484 
485  for (int icol = 1; icol < ncols-1; icol++)
486  {
487  int y = 0;
488  for (int irow = 0; irow < nrows; irow++)
489  {
490  u32 gateId = pathRows.at(irow).at(icol)->get_id();
491  if (gateIds.contains(gateId)) continue;
492  gateIds.insert(gateId);
493  plc[Node(gateId,Node::Gate)] = QPoint(icol, y++);
494  }
495  }
496 
498  act->setUseCreatedObject();
499  act->addAction(new ActionCreateObject(UserActionObjectType::ContextView, QString("Path mod_%1 => mod_%2").arg(fromModId).arg(toModId)));
500  ActionAddItemsToObject* actionAddItems = new ActionAddItemsToObject(moduleIds, gateIds);
501  actionAddItems->setPlacementHint(PlacementHint(plc));
502  act->addAction(actionAddItems);
504 
505  return act->object().id();
506  }
507 
508 
509  int GuiApiClasses::View::isolateInNew(std::vector<Module*> modules, std::vector<Gate*> gates)
510  {
511  //check if the inputs are valid
512  for(Module* mod : modules)
513  {
514  if (mod == nullptr)
515  {
516  log_warning("gui","Null values not allowed in module argument");
517  return 0;
518  }
519  }
520 
521  for(Gate* gate : gates)
522  {
523  if (gate == nullptr)
524  {
525  log_warning("gui","Null values not allowed in gate argument");
526  return 0;
527  }
528  }
529  QString name;
530 
531  bool isModuleExclusive = false;
532 
533  //make sure that modules and gates are not empty
534  if(modules.empty() && gates.empty())
535  return 0;
536 
537  //Check if view should be bound exclusively to module
538  if(modules.size() == 1 && gates.empty() && modules[0]){
539  name = QString::fromStdString(modules[0]->get_name()) + QString(" (ID: %1)").arg(modules[0]->get_id());
540  isModuleExclusive = true;
541  //If the view already exists then return existing id
544  if(ctx->name() == name){
545  return ctx->id();
546  }
547  }
548  return 0;
549  }
550  }
551  else
552  {
553  //Get the number which has to be appended to name
554  name = gGraphContextManager->nextViewName("Isolated View");
555  }
557 
558  // Unpack return values and check if not empty
559  QSet<u32> moduleIds = pair.moduleIds;
560  QSet<u32> gateIds = pair.gateIds;
561 
562  if(moduleIds.isEmpty() && gateIds.isEmpty())
563  return 0;
564 
566  act->setUseCreatedObject();
568  act->addAction(new ActionAddItemsToObject(moduleIds, gateIds));
570 
571  if (isModuleExclusive){
573  context->setDirty(false);
574  context->setExclusiveModuleId(modules[0]->get_id());
575  }
576  return act->object().id();
577  }
578 
580  {
581  // check if id is valid
582  if(gGraphContextManager->getContextById(id) == nullptr)
583  {
584  return false;
585  }
586 
590  return true;
591  }
592 
593 
594  bool GuiApiClasses::View::addTo(int id, const std::vector<Module*> modules, const std::vector<Gate*> gates)
595  {
596  //check if the inputs are valid
597  for(Module* mod : modules)
598  {
599  if (mod == nullptr)
600  {
601  log_warning("gui","Null values not allowed in module argument");
602  return 0;
603  }
604  }
605 
606  for(Gate* gate : gates)
607  {
608  if (gate == nullptr)
609  {
610  log_warning("gui","Null values not allowed in gate argument");
611  return 0;
612  }
613  }
614 
615  if (!gGraphContextManager->getContextById(id)) return false; // context does not exist
616 
617 
619  return false; //context shows topmodule so we can not add anything to it
620 
622 
623  // Get ids from modules and gates
624  QSet<u32> moduleIds = pair.moduleIds;
625  QSet<u32> gateIds = pair.gateIds;
626 
627  ActionAddItemsToObject* act = new ActionAddItemsToObject(moduleIds,gateIds);
630  return true;
631  }
632 
633  bool GuiApiClasses::View::removeFrom(int id, const std::vector<Module*> modules, const std::vector<Gate*> gates)
634  {
635  //check if the inputs are valid
636  for(Module* mod : modules)
637  {
638  if (mod == nullptr)
639  {
640  log_warning("gui","Null values not allowed in module argument");
641  return 0;
642  }
643  }
644 
645  for(Gate* gate : gates)
646  {
647  if (gate == nullptr)
648  {
649  log_warning("gui","Null values not allowed in gate argument");
650  return 0;
651  }
652  }
653 
654  if (!gGraphContextManager->getContextById(id)) return false; // context does not exist
655 
656  QSet<u32> moduleIds;
657  QSet<u32> gateIds;
658 
659  for(Module* module : modules)
660  moduleIds.insert(module->get_id());
661  for(Gate* gate : gates)
662  gateIds.insert(gate->get_id());
663 
664  ActionRemoveItemsFromObject* act = new ActionRemoveItemsFromObject(moduleIds,gateIds);
667  return true;
668  }
669 
670  bool GuiApiClasses::View::setName(int id, const std::string& name)
671  {
672  if (!gGraphContextManager->getContextById(id)) return false; // context does not exist
673 
674  //check if name is occupied
676  return false;
677 
678  //check if view is exclusively bound to module
680  return false;
681 
682  //get context matching id and rename it
686  return true;
687  }
688 
689  int GuiApiClasses::View::getId(const std::string& name)
690  {
691  //check if there exists a context with the given name before we iterate over each of them
693  return 0;
694 
695  //find View related to the name
697  if(ctx->name() == QString::fromStdString(name)){
698  return ctx->id();
699  }
700  }
701  return 0;
702  }
703 
704  std::string GuiApiClasses::View::getName(int id)
705  {
707  if(ctx != nullptr){
708  return ctx->name().toStdString();
709  }
710  return {}; //
711  }
712 
713  std::vector<Module*> GuiApiClasses::View::getModules(int id)
714  {
716  if(ctx != nullptr){
717  std::vector<Module*> modules;
718  for(u32 id : ctx->modules()){
719  modules.push_back(gNetlist->get_module_by_id(id));
720  }
721  return modules;
722  }
723  return {};
724  }
725 
726  std::vector<Gate*> GuiApiClasses::View::getGates(int id)
727  {
729  if(ctx != nullptr){
730  std::vector<Gate*> gates;
731  for(u32 id : ctx->gates()){
732  gates.push_back(gNetlist->get_gate_by_id(id));
733  }
734  return gates;
735  }
736  return {};
737  }
738 
739  std::vector<u32> GuiApiClasses::View::getIds(const std::vector<Module*> modules, const std::vector<Gate*> gates)
740  {
741  //check if the inputs are valid
742  for(Module* mod : modules)
743  {
744  if (mod == nullptr)
745  {
746  log_warning("gui","Null values not allowed in module argument");
747  return {};
748  }
749  }
750 
751  for(Gate* gate : gates)
752  {
753  if (gate == nullptr)
754  {
755  log_warning("gui","Null values not allowed in gate argument");
756  return {};
757  }
758  }
759 
760  std::vector<u32> ids;
761 
762  QSet<u32> moduleIds;
763  QSet<u32> gateIds;
764 
765  //Get ids of given modules and gates
766  for(Module* module : modules)
767  {
768  if(module)
769  moduleIds.insert(module->get_id());
770  }
771  for(Gate* gate : gates)
772  {
773  if(gate)
774  gateIds.insert(gate->get_id());
775  }
776 
777  //iterate over each context and look if its showing modules
779  bool isCandidate = true;
780 
781  //Check if modules are in ctx
782  for(u32 moduleId : moduleIds){
783  if(ctx->modules().contains(moduleId))
784  continue;
785  isCandidate = false;
786  break;
787  }
788  //Only check modules if ctx still a valid candidate
789  if(isCandidate){
790  for(u32 gateId : gateIds){
791  if(ctx->gates().contains(gateId))
792  continue;
793  isCandidate = false;
794  break;
795  }
796  }
797 
798  //add it to ids if its a candidate
799  if(isCandidate)
800  ids.push_back(ctx->id());
801 
802  }
803  return ids;
804  }
806  {
807  if(!module)
808  {
809  log_warning("gui","module must not be null");
810  return false;
811  }
812  GraphContext* context = gGraphContextManager->getContextById(view_id);
813 
814  //check if the inputs are valid
815  if(context == nullptr) return false;
816 
817  if(!context->modules().contains(module->get_id())) return false;
818 
821  return true;
822  }
823 
825  {
826  if(!module)
827  {
828  log_warning("gui","module must not be null");
829  return false;
830  }
831  GraphContext* context = gGraphContextManager->getContextById(view_id);
832 
833  //check if the inputs are valid
834  if(context == nullptr) return false;
835 
836  //get gates and submodules that belong to the current module
837  std::vector<Module*> submodules = module->get_submodules();
838  std::vector<Gate*> gates = module->get_gates();
839 
840  bool isValidToFold = false;
841  //check if the view contains gates and submodules of the current module, return false if not
842  for(Gate* gate : gates)
843  if(context->gates().contains(gate->get_id())) {isValidToFold = true; break;}
844  for(Module* submodule : submodules)
845  if(context->modules().contains(submodule->get_id())) {isValidToFold = true; break;}
846  if (isValidToFold)
847  {
850  return true;
851  }
852  return false;
853  }
854 
855  GuiApiClasses::View::ModuleGateIdPair GuiApiClasses::View::getValidObjects(int viewId, const std::vector<Module*> mods, const std::vector<Gate*> gats)
856  {
857  Module* topModule = gNetlist->get_top_module();
858  //copy to prevent inplace operations
859  std::vector<Module*> modules = mods;
860  std::vector<Gate*> gates = gats;
861 
862  //set to store already existing mods and gates
863  QSet<u32> existingModules;
864  QSet<u32> existingGates;
865 
866  QSet<u32> Parents;
867  QSet<u32> modIds;
868  QSet<u32> gatIds;
869  //0) if its not a new view we have to check and remove all parents which have to be placed
870  if (viewId)
871  {
872  //put topmodule into parents because we dont iterate over it here
873  //Parents.insert(topModule->get_id());
874  std::vector<Module*> validMods;
875  //Add parents from the view modules to Parents
876  for (Module* mod : GuiApiClasses::View::getModules(viewId))
877  {
878  existingModules.insert(mod->get_id());
879  validMods.push_back(mod);
880  Module* itr = mod->get_parent_module();
881  while (itr != nullptr)
882  {
883  if (Parents.contains(itr->get_id()))
884  break;
885  Parents.insert(itr->get_id());
886  itr = itr->get_parent_module();
887  }
888 
889  }
890  //Add parents from gate to parents
891  for (Gate* gate : GuiApiClasses::View::getGates(viewId))
892  {
893  existingGates.insert(gate->get_id());
894  Module* itr = gate->get_module();
895  while (itr != nullptr)
896  {
897  if (Parents.contains(itr->get_id()))
898  break;
899  Parents.insert(itr->get_id());
900  itr = itr->get_parent_module();
901  }
902  }
903  //Delete every parent from the list if submodule is in the view
904  for (Module* mod : modules)
905  {
906  if (Parents.contains(mod->get_id()))
907  continue;
908  validMods.push_back(mod);
909  }
910  modules = validMods;
911  }
912  //get ids of modules
913  for (Module* mod : modules)
914  {
915  modIds.insert(mod->get_id());
916  }
917  //1) sort them by priority in DESCENDING order
918  std::sort(modules.begin(), modules.end(), [](const Module* a, const Module* b) -> bool
919  {
920  //sorting in DESCENDING order to check from lowest to highest priority
921  return a->get_submodule_depth() > b->get_submodule_depth();
922  }
923  );
924  //2) remove id if parent is in set
925  for(Module* mod : modules){
926  //check if top module
927  if(mod == topModule){
928  // only add the top module because it has highest priority
929  QSet<u32> temp;
930  temp.insert(topModule->get_id());
931  modIds = temp;
932  break;
933  }
934  //check if parent is in current set and if so remove current mod
935  Module* iterator = mod->get_parent_module();
936  while(iterator != nullptr){
937  if(modIds.contains(iterator->get_id()))
938  {
939  // parent is already in the set so remove mod and stop traversing parent tree
940  modIds.remove(mod->get_id());
941  break;
942  }
943  iterator = iterator->get_parent_module();
944  }
945  }
946  //3) remove all gates which has its ancestor in modIds
947  //check ancestors until topmodule or found in modIds
948  for (Gate* gate : gates)
949  {
950  Module* itr = gate->get_module();
951  bool shouldInsert = true;
952  while (itr != nullptr)
953  {
954  if (modIds.contains(itr->get_id()))
955  {
956  gatIds.remove(gate->get_id());
957  shouldInsert = false;
958  break;
959  }
960  itr = itr->get_parent_module();
961  }
962  if (shouldInsert)
963  gatIds.insert(gate->get_id());
964  }
965 
966  //remove duplicates
967  if (viewId)
968  {
969  modIds -= existingModules;
970  gatIds -= existingGates;
971  }
972  //create struct to return module and gate ID pairs
974  pair.moduleIds = modIds;
975  pair.gateIds = gatIds;
976 
977  return pair;
978 
979  }
980 
982  {
983  const GraphContext* context = gGraphContextManager->getContextById(viewId);
984  if (context == nullptr) return new GridPlacement();
985  return context->getLayouter()->gridPlacementFactory();
986  }
987 
989  {
990  ActionMoveNode* act = new ActionMoveNode(viewId, gp);
992  return true;
993  }
994 
996  {
997  auto currentDirectory = gGraphContextManager->getContextTreeModel()->getCurrentDirectory();
998  if(currentDirectory == nullptr)
999  return 0;
1000  else
1001  {
1002  if(currentDirectory->directory() == nullptr)
1003  return 0;
1004  return currentDirectory->directory()->id();
1005  }
1006  }
1007 
1009  {
1012  }
1013 
1015  {
1018  auto id = act->object().id();
1019  return id;
1020  }
1021 
1023  {
1024  /*ContextDirectory* directory = gGraphContextManager->getDirectoryById(id);
1025  if(directory)
1026  gGraphContextManager->deleteContextDirectory(directory);*/
1030  }
1031 
1032  void GuiApiClasses::View::moveView(u32 viewId, std::optional<u32> destinationDirectoryId, std::optional<int> row)
1033  {
1035  if(!viewItem)
1036  return;
1037 
1038  u32 parentId = 0;
1039  BaseTreeItem* parentItem = viewItem->getParent();
1040  if(parentItem && gGraphContextManager->getContextTreeModel()->getRootItem() != parentItem)
1041  parentId = dynamic_cast<ContextTreeItem*>(parentItem)->getId();
1042 
1044  ActionMoveItem* act;
1045  if(row.has_value())
1046  act = new ActionMoveItem(destinationDirectoryId.value_or(getCurrentDirectory()), parentId, row.value());
1047  else
1048  act = new ActionMoveItem(destinationDirectoryId.value_or(getCurrentDirectory()), parentId);
1049  act->setObject(uao);
1050  act->exec();
1051  }
1052 
1053  void GuiApiClasses::View::moveDirectory(u32 directoryId, std::optional<u32> destinationDirectoryId, std::optional<int> row)
1054  {
1055  BaseTreeItem* directoryItem = gGraphContextManager->getContextTreeModel()->getDirectory(directoryId);
1056  if(!directoryItem)
1057  return;
1058 
1059  u32 parentId = 0;
1060  BaseTreeItem* parentItem = directoryItem->getParent();
1061  if(parentItem && gGraphContextManager->getContextTreeModel()->getRootItem() != parentItem)
1062  parentId = dynamic_cast<ContextTreeItem*>(parentItem)->getId();
1063 
1064  u32 destId = destinationDirectoryId.value_or(getCurrentDirectory());
1066  while (destAnchestor)
1067  {
1068  if (destAnchestor == directoryItem)
1069  {
1070  log_warning("gui", "Invalid attempt to move directory ID={} into dependend directory.", directoryId);
1071  return;
1072  }
1073  destAnchestor = destAnchestor->getParent();
1074  }
1075 
1076 
1078  ActionMoveItem* act;
1079  if(row.has_value())
1080  act = new ActionMoveItem(destId, parentId, row.value());
1081  else
1082  act = new ActionMoveItem(destId, parentId);
1083  act->setObject(uao);
1084  act->exec();
1085  }
1086 
1087  std::optional<std::vector<u32>> GuiApiClasses::View::getChildDirectories(u32 directoryId)
1088  {
1089  ContextTreeItem* directoryItem = dynamic_cast<ContextTreeItem*>(gGraphContextManager->getContextTreeModel()->getDirectory(directoryId));
1090  // Id 0 is the root item. It does not exist as a directory object, but child items can still be retrieved from it.
1091  if(!directoryItem && directoryId != 0)
1092  return std::nullopt;
1094  }
1095 
1096  std::optional<std::vector<u32>> GuiApiClasses::View::getChildViews(u32 directoryId)
1097  {
1098  ContextTreeItem* directoryItem = dynamic_cast<ContextTreeItem*>(gGraphContextManager->getContextTreeModel()->getDirectory(directoryId));
1099  // Id 0 is the root item. It does not exist as a directory object, but child items can still be retrieved from it.
1100  if(!directoryItem && directoryId != 0)
1101  return std::nullopt;
1103  }
1104 }
Adds an item to a module or grouping.
void setPlacementHint(PlacementHint hint)
bool exec() override
Removes an item from a Module or Grouping.
(Future) Base class for all tree models related to the details widget.
virtual BaseTreeItem * getParent() const
BaseTreeItem * getRootItem() const
ContextDirectory * directory() const
std::vector< u32 > getChildDirectoriesOf(u32 directoryId)
void setCurrentDirectory(ContextTreeItem *currentItem)
std::vector< u32 > getChildContextsOf(u32 directoryId)
BaseTreeItem * getDirectory(u32 directoryId) const
ContextTreeItem * getCurrentDirectory()
BaseTreeItem * getContext(u32 contextId) const
Definition: gate.h:58
u32 get_id() const
Definition: gate.cpp:95
Logical container for modules, gates, and nets.
Definition: graph_context.h:55
u32 getExclusiveModuleId() const
bool isShowingModuleExclusively()
void setDirty(bool dty)
void setExclusiveModuleId(u32 id, bool emitSignal=true)
const QSet< u32 > & gates() const
const QSet< u32 > & modules() const
GraphLayouter * getLayouter() const
QString name() const
QString nextViewName(const QString &prefix) const
GraphContext * getContextById(u32 id) const
ContextTreeModel * getContextTreeModel() const
bool contextWithNameExists(const QString &name) const
QVector< GraphContext * > getContexts() const
GridPlacement * gridPlacementFactory() const
static bool foldModule(int view_id, Module *module)
Definition: gui_api.cpp:824
static void deleteDirectory(u32 id)
Definition: gui_api.cpp:1022
static bool unfoldModule(int view_id, Module *module)
Definition: gui_api.cpp:805
static void moveDirectory(u32 directoryId, std::optional< u32 > destinationDirectoryId, std::optional< int > row)
Definition: gui_api.cpp:1053
static int getId(const std::string &name)
Definition: gui_api.cpp:689
static bool removeFrom(int id, const std::vector< Module * > modules, const std::vector< Gate * > gates)
Definition: gui_api.cpp:633
static int isolateInNew(const std::vector< Module * > modules, const std::vector< Gate * > gates)
Definition: gui_api.cpp:509
static bool setName(int id, const std::string &name)
Definition: gui_api.cpp:670
static void moveView(u32 viewId, std::optional< u32 > destinationDirectoryId, std::optional< int > row)
Definition: gui_api.cpp:1032
static int isolateModuleToModulePathInNewView(u32 fromModId, u32 toModId)
Definition: gui_api.cpp:449
static std::string getName(int id)
Definition: gui_api.cpp:704
static GridPlacement * getGridPlacement(int viewId)
Definition: gui_api.cpp:981
static bool setGridPlacement(int viewId, GridPlacement *gp)
Definition: gui_api.cpp:988
static u32 createNewDirectory(const std::string &name)
Definition: gui_api.cpp:1014
static std::vector< Gate * > getGates(int id)
Definition: gui_api.cpp:726
static std::optional< std::vector< u32 > > getChildDirectories(u32 directoryId)
Definition: gui_api.cpp:1087
static bool addTo(int id, const std::vector< Module * > modules, const std::vector< Gate * > gates)
Definition: gui_api.cpp:594
static std::optional< std::vector< u32 > > getChildViews(u32 directoryId)
Definition: gui_api.cpp:1096
static bool deleteView(int id)
Definition: gui_api.cpp:579
static std::vector< Module * > getModules(int id)
Definition: gui_api.cpp:713
static void setCurrentDirectory(u32 id)
Definition: gui_api.cpp:1008
static std::vector< u32 > getIds(const std::vector< Module * > modules, const std::vector< Gate * > gates)
Definition: gui_api.cpp:739
static u32 getCurrentDirectory()
Definition: gui_api.cpp:995
static ModuleGateIdPair getValidObjects(int viewId, const std::vector< Module * >, const std::vector< Gate * >)
Definition: gui_api.cpp:855
void deselectNet(u32 netId)
Definition: gui_api.cpp:336
void deselectModule(u32 module_id)
Definition: gui_api.cpp:374
std::vector< u32 > getSelectedNetIds()
Definition: gui_api.cpp:38
std::tuple< std::vector< Gate * >, std::vector< Net * >, std::vector< Module * > > getSelectedItems()
Definition: gui_api.cpp:77
void deselectAllItems()
Definition: gui_api.cpp:444
std::vector< u32 > getSelectedModuleIds()
Definition: gui_api.cpp:43
std::vector< Gate * > getSelectedGates()
Definition: gui_api.cpp:53
std::vector< Net * > getSelectedNets()
Definition: gui_api.cpp:61
void deselect(Gate *gate)
Definition: gui_api.cpp:402
void selectNet(u32 netId, bool clear_current_selection=true, bool navigate_to_selection=true)
Definition: gui_api.cpp:149
void navigationRequested()
void select(Gate *gate, bool clear_current_selection=true, bool navigate_to_selection=true)
Definition: gui_api.cpp:235
std::vector< u32 > getSelectedGateIds()
Definition: gui_api.cpp:33
std::vector< Module * > getSelectedModules()
Definition: gui_api.cpp:69
std::tuple< std::vector< u32 >, std::vector< u32 >, std::vector< u32 > > getSelectedItemIds()
Definition: gui_api.cpp:48
void selectModule(u32 module_id, bool clear_current_selection=true, bool navigate_to_selection=true)
Definition: gui_api.cpp:200
void selectGate(u32 gate_id, bool clear_current_selection=true, bool navigate_to_selection=true)
Definition: gui_api.cpp:98
void deselectGate(u32 gate_id)
Definition: gui_api.cpp:298
Module * get_parent_module() const
Definition: module.cpp:125
const std::vector< Gate * > & get_gates() const
Definition: module.cpp:393
std::vector< Module * > get_submodules(const std::function< bool(Module *)> &filter=nullptr, bool recursive=false) const
Definition: module.cpp:261
u32 get_id() const
Definition: module.cpp:82
Definition: net.h:58
Module * get_top_module() const
Definition: netlist.cpp:608
bool is_gate_in_netlist(const Gate *gate) const
Definition: netlist.cpp:188
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:193
bool is_module_in_netlist(const Module *module) const
Definition: netlist.cpp:647
bool is_net_in_netlist(const Net *net) const
Definition: netlist.cpp:348
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:613
Net * get_net_by_id(u32 net_id) const
Definition: netlist.cpp:353
The Node class object represents a module or a gate.
Definition: gui_def.h:61
@ Module
Definition: gui_def.h:63
@ Gate
Definition: gui_def.h:63
The PlacementHint class object provides hints for the layouter how new box objects are placed on a vi...
Definition: gui_def.h:211
void setFocus(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
const QSet< u32 > & selectedNets() const
void relaySelectionChanged(void *sender)
QList< u32 > selectedModulesList() const
const QSet< u32 > & selectedGates() const
QList< u32 > selectedNetsList() const
const QSet< u32 > & selectedModules() const
std::vector< u32 > selectedGatesVector() const
std::vector< u32 > selectedNetsVector() const
QList< u32 > selectedGatesList() const
void setSelectedGates(const QSet< u32 > &ids)
std::vector< u32 > selectedModulesVector() const
void setSelectedNets(const QSet< u32 > &ids)
void registerSender(void *sender, QString name)
void setSelectedModules(const QSet< u32 > &ids)
void addAction(UserAction *act)
virtual void setObject(const UserActionObject &obj)
Definition: user_action.cpp:32
virtual UserActionObject object() const
Definition: user_action.h:102
void executeActionBlockThread(UserAction *act)
static UserActionManager * instance()
The UserActionObject class represents a single object used in UserAction.
#define log_warning(channel,...)
Definition: log.h:76
const Module * module(const Gate *g, const NodeBoxes &boxes)
std::vector< Gate * > get_shortest_path(Gate *start_gate, Module *end_module, bool forward_direction)
GraphContextManager * gGraphContextManager
Definition: plugin_gui.cpp:85
SelectionRelay * gSelectionRelay
Definition: plugin_gui.cpp:83
Netlist * gNetlist
Definition: plugin_gui.cpp:80
quint32 u32
Net * net
std::string name
i32 id
Q_EMITQ_EMIT
bool contains(const T &value) 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
bool remove(const T &value)
int size() const const
QSet< T > & unite(const QSet< T > &other)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromStdString(const std::string &str)
std::string toStdString() const const