HAL
selection_relay.cpp
Go to the documentation of this file.
2 
3 #include "gui/gui_globals.h"
6 #include "hal_core/netlist/net.h"
9 
10 namespace hal
11 {
12  // SET VIA SETTINGS OR TOOLBUTTON
13  bool SelectionRelay::sNavigationSkipsEnabled = false;
14 
16  mAction(nullptr), mDisableExecution(false),
17  mFocusType(ItemType::None), mSubfocus(Subfocus::None)
18  {
19  clear();
20  }
21 
23  {
24  initializeAction();
25  mAction->mModules.clear();
26  mAction->mGates.clear();
27  mAction->mNets.clear();
28  mAction->mObject = UserActionObject();
29  mAction->mSubfocus = Subfocus::None;
30  mAction->mSubfocusIndex = 0;
31  mModulesSuppressedByFilter.clear();
32  mGatesSuppressedByFilter.clear();
33  mNetsSuppressedByFilter.clear();
34  }
35 
36  void SelectionRelay::initializeAction()
37  {
38  if (!mAction)
39  {
40  mAction = new ActionSetSelectionFocus;
41  mAction->mModules = mSelectedModules;
42  mAction->mGates = mSelectedGates;
43  mAction->mNets = mSelectedNets;
44  mAction->mObject = UserActionObject(
45  mFocusId,
47  mAction->mSubfocus = mSubfocus;
48  mAction->mSubfocusIndex = mSubfocusIndex;
49  }
50  }
51 
52  void SelectionRelay::executeAction()
53  {
54  if (!mAction || mDisableExecution) return;
55 
56  mDisableExecution = true;
57  if (mAction->hasModifications())
58  mAction->exec();
59  else
60  delete mAction;
61  mAction = nullptr;
62  mDisableExecution = false;
63  }
64 
66  {
67  initializeAction();
68  mAction->mGates.insert(id);
69  }
70 
72  {
73  initializeAction();
74  mAction->mNets.insert(id);
75  }
76 
78  {
79  initializeAction();
80  mAction->mModules.insert(id);
81  }
82 
84  {
86  for (u32 id : mSelectedModules)
88  for (u32 id : mSelectedGates)
90  for (u32 id : mSelectedNets)
92  return retval;
93  }
94 
96  {
97  initializeAction();
98  mAction->mGates = ids;
99  }
100 
102  {
103  initializeAction();
104  mAction->mNets = ids;
105  }
106 
108  {
109  initializeAction();
110  mAction->mModules = ids;
111  }
112 
113  void SelectionRelay::actionSetSelected(const QSet<u32>& mods, const QSet<u32>& gats, const QSet<u32>& nets)
114  {
115  mSelectedModules = mods;
116  mSelectedGates = gats;
117  mSelectedNets = nets;
118  Q_EMIT selectionChanged(nullptr);
119  }
120 
122  {
123  initializeAction();
124  mAction->mGates.remove(id);
125  }
126 
128  {
129  initializeAction();
130  mAction->mNets.remove(id);
131  }
132 
134  {
135  initializeAction();
136  mAction->mModules.remove(id);
137  }
138 
139  void SelectionRelay::setFocus(ItemType ftype, u32 fid, Subfocus sfoc, u32 sfinx)
140  {
141  initializeAction();
143  mAction->mSubfocus = sfoc;
144  mAction->mSubfocusIndex = sfinx;
145  }
146 
148  {
149  mFocusType = ftype;
150  mFocusId = fid;
151  mSubfocus = sfoc;
152  mSubfocusIndex = sfinx;
153  }
154 
156  {
157  clear();
158  executeAction();
159  }
160 
162  {
163  mSenderRegister.append(QPair<void*, QString>(sender, name));
164  }
165 
166  void SelectionRelay::removeSender(void* sender)
167  {
168  for (QPair<void*, QString> pair : mSenderRegister)
169  {
170  if (pair.first == sender)
171  mSenderRegister.removeOne(pair);
172  }
173  }
174 
176  {
177 #ifdef HAL_STUDY
178  evaluateSelectionChanged(sender);
179 #else
180  Q_UNUSED(sender);
181 #endif
182  executeAction();
183  }
184 
186  {
188  executeAction();
189  }
190 
191  // TODO deduplicate navigateUp and navigateDown
193  {
194  u32 size = 0;
195 
196  switch (mFocusType)
197  {
198  case ItemType::None: {
199  return;
200  }
201  case ItemType::Gate: {
202  Gate* g = gNetlist->get_gate_by_id(mFocusId);
203 
204  if (!g)
205  return;
206 
207  if (mSubfocus == Subfocus::Left)
208  {
209  size = g->get_type()->get_input_pins().size();
210 
211  if (!size) // CHECK NECESSARY ???
212  return; // INVALID STATE, FIX OR IGNORE ???
213 
214  break;
215  }
216 
217  if (mSubfocus == Subfocus::Right)
218  {
219  size = g->get_type()->get_output_pins().size();
220 
221  if (!size) // CHECK NECESSARY ???
222  return; // INVALID STATE, FIX OR IGNORE ???
223 
224  break;
225  }
226 
227  return;
228  }
229  case ItemType::Net: {
230  Net* n = gNetlist->get_net_by_id(mFocusId);
231 
232  if (!n)
233  return;
234 
235  if (mSubfocus == Subfocus::Right)
236  {
237  size = n->get_destinations().size();
238 
239  if (!size) // CHECK NECESSARY ???
240  return; // INVALID STATE, FIX OR IGNORE ???
241 
242  break;
243  }
244 
245  return;
246  }
247  case ItemType::Module: {
248  Module* m = gNetlist->get_module_by_id(mFocusId);
249 
250  if (!m)
251  return;
252 
253  if (mSubfocus == Subfocus::Left)
254  {
255  size = m->get_input_nets().size();
256 
257  if (!size) // CHECK NECESSARY ???
258  return; // INVALID STATE, FIX OR IGNORE ???
259 
260  break;
261  }
262 
263  if (mSubfocus == Subfocus::Right)
264  {
265  size = m->get_output_nets().size();
266 
267  if (!size) // CHECK NECESSARY ???
268  return; // INVALID STATE, FIX OR IGNORE ???
269 
270  break;
271  }
272 
273  return;
274  }
275  }
276 
277  initializeAction();
278  if (mSubfocusIndex == 0)
279  mAction->mSubfocusIndex = size - 1;
280  else
281  --mAction->mSubfocusIndex;
282  relaySubfocusChanged(nullptr);
283  }
284 
286  {
287  u32 size = 0;
288 
289  switch (mFocusType)
290  {
291  case ItemType::None: {
292  return;
293  }
294  case ItemType::Gate: {
295  Gate* g = gNetlist->get_gate_by_id(mFocusId);
296 
297  if (!g)
298  return;
299 
300  if (mSubfocus == Subfocus::Left)
301  {
302  size = g->get_type()->get_input_pins().size();
303 
304  if (!size) // CHECK NECESSARY ???
305  return; // INVALID STATE, FIX OR IGNORE ???
306 
307  break;
308  }
309 
310  if (mSubfocus == Subfocus::Right)
311  {
312  size = g->get_type()->get_output_pins().size();
313 
314  if (!size) // CHECK NECESSARY ???
315  return; // INVALID STATE, FIX OR IGNORE ???
316 
317  break;
318  }
319 
320  return;
321  }
322  case ItemType::Net: {
323  Net* n = gNetlist->get_net_by_id(mFocusId);
324 
325  if (!n)
326  return;
327 
328  if (mSubfocus == Subfocus::Right)
329  {
330  size = n->get_destinations().size();
331 
332  if (!size) // CHECK NECESSARY ???
333  return; // INVALID STATE, FIX OR IGNORE ???
334 
335  break;
336  }
337 
338  return;
339  }
340  case ItemType::Module: {
341  Module* m = gNetlist->get_module_by_id(mFocusId);
342 
343  if (!m)
344  return;
345 
346  if (mSubfocus == Subfocus::Left)
347  {
348  size = m->get_input_nets().size();
349 
350  if (!size) // CHECK NECESSARY ???
351  return; // INVALID STATE, FIX OR IGNORE ???
352 
353  break;
354  }
355 
356  if (mSubfocus == Subfocus::Right)
357  {
358  size = m->get_output_nets().size();
359 
360  if (!size) // CHECK NECESSARY ???
361  return; // INVALID STATE, FIX OR IGNORE ???
362 
363  break;
364  }
365 
366  return;
367  }
368  }
369 
370  initializeAction();
371  if (mSubfocusIndex == size - 1)
372  mAction->mSubfocusIndex = 0;
373  else
374  ++mAction->mSubfocusIndex;
375  relaySubfocusChanged(nullptr);
376  }
377 
378  /*void SelectionRelay::suppressedByFilter(const QList<u32>& modIds, const QList<u32>& gatIds, const QList<u32>& netIds)
379  {
380  initializeAction();
381  mModulesSuppressedByFilter = modIds.toSet();
382  mGatesSuppressedByFilter = gatIds.toSet();
383  mNetsSuppressedByFilter = netIds.toSet();
384  executeAction();
385  }*/
386 
388  {
389  return mSelectedModules.contains(id) && !mModulesSuppressedByFilter.contains(id);
390  }
391 
393  {
394  return mSelectedGates.contains(id) && !mGatesSuppressedByFilter.contains(id);
395  }
396 
398  {
399  return mSelectedNets.contains(id) && !mNetsSuppressedByFilter.contains(id);
400  }
401 
403  {
404  auto it = mSelectedModules.find(id);
405  if (it != mSelectedModules.end())
406  {
407  initializeAction();
408  mAction->mModules.remove(id);
409  executeAction();
410  }
411  }
412 
414  {
415  auto it = mSelectedGates.find(id);
416  if (it != mSelectedGates.end())
417  {
418  initializeAction();
419  mAction->mGates.remove(id);
420  executeAction();
421  }
422  }
423 
425  {
426  auto it = mSelectedNets.find(id);
427  if (it != mSelectedNets.end())
428  {
429  initializeAction();
430  mAction->mGates.remove(id);
431  executeAction();
432  }
433  }
434 
435  void SelectionRelay::followModuleInputPin(Module* m, u32 input_pin_index)
436  {
437  Q_UNUSED(m)
438  Q_UNUSED(input_pin_index)
439  // TODO implement
440  }
441 
442  void SelectionRelay::followModuleOutputPin(Module* m, u32 output_pin_index)
443  {
444  Q_UNUSED(m)
445  Q_UNUSED(output_pin_index)
446  // TODO implement
447  }
448 
449  void SelectionRelay::subfocusNone()
450  {
451  initializeAction();
452  mAction->mSubfocus = Subfocus::None;
453  mAction->mSubfocusIndex = 0;
454  relaySubfocusChanged(nullptr);
455  }
456 
457  void SelectionRelay::subfocusLeft()
458  {
459  initializeAction();
460  mAction->mSubfocus = Subfocus::Left;
461  mAction->mSubfocusIndex = 0;
462  relaySubfocusChanged(nullptr);
463  }
464 
465  void SelectionRelay::subfocusRight()
466  {
467  mSubfocus = Subfocus::Right;
468  mSubfocusIndex = 0;
469 
470  Q_EMIT subfocusChanged(nullptr);
471  }
472 
474  {
475  QList<Node> retval;
476  for (u32 mid : selectedModulesList())
477  retval.append(Node(mid,Node::Module));
478  for (u32 gid : selectedGatesList())
479  retval.append(Node(gid,Node::Gate));
480  return retval;
481  }
482 
483 #ifdef HAL_STUDY
484  void SelectionRelay::evaluateSelectionChanged(void *sender)
485  {
486  QString method = "unknown";
487  for(const auto pair : mSenderRegister)
488  {
489  if(pair.first == sender)
490  {
491  method = pair.second;
492  break;
493  }
494  }
495 
496  auto createSubstring = [](std::string first_part, QSet<u32> ids){
497 
498  std::string final_string = first_part;
499  for(const auto &i : ids)
500  final_string += std::to_string(i) + ", ";
501 
502  if(!ids.isEmpty())
503  final_string.resize(final_string.size()-2);
504 
505  return final_string + "}";
506  };
507 
508  std::string gateIdsSubstring = createSubstring("Gate-Ids: {", mSelectedGates);
509  std::string netIdsSubstring = createSubstring("Net-Ids: {", mSelectedNets);
510  std::string moduleIdsSubstring = createSubstring("Module-Ids: {", mSelectedModules);
511  log_info("UserStudy", "Selection changed, Method: {}, New Sel.: {}, {}, {}", method.toStdString(), gateIdsSubstring, netIdsSubstring, moduleIdsSubstring);
512 
513 
514  }
515 #endif
516 } // namespace hal
Set the selection and focus.
void setObject(const UserActionObject &obj) override
Definition: gate.h:58
const std::unordered_set< Net * > & get_input_nets() const
Definition: module.cpp:540
const std::unordered_set< Net * > & get_output_nets() const
Definition: module.cpp:545
Definition: net.h:58
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
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
QList< UserActionObject > toUserActionObject() const
void setFocus(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
void selectionChanged(void *sender)
bool isNetSelected(u32 id) const
void relaySelectionChanged(void *sender)
void removeSender(void *sender)
void relaySubfocusChanged(void *sender)
QList< Node > selectedNodesList() const
QList< u32 > selectedModulesList() const
void setFocusDirect(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
void handleNetRemoved(const u32 id)
void actionSetSelected(const QSet< u32 > &mods, const QSet< u32 > &gats, const QSet< u32 > &nets)
QList< u32 > selectedGatesList() const
void handleGateRemoved(const u32 id)
void handleModuleRemoved(const u32 id)
void setSelectedGates(const QSet< u32 > &ids)
SelectionRelay(QObject *parent=nullptr)
bool isModuleSelected(u32 id) const
void setSelectedNets(const QSet< u32 > &ids)
void subfocusChanged(void *sender)
void registerSender(void *sender, QString name)
bool isGateSelected(u32 id) const
void setSelectedModules(const QSet< u32 > &ids)
UserActionObject mObject
Definition: user_action.h:183
The UserActionObject class represents a single object used in UserAction.
static ObjectType fromSelectionType(SelectionRelay::ItemType itp)
#define log_info(channel,...)
Definition: log.h:70
Netlist * gNetlist
Definition: plugin_gui.cpp:80
n
Definition: test.py:6
quint32 u32
std::string name
void append(const T &value)
Q_EMITQ_EMIT
QObject * sender() const const
void clear()
bool contains(const T &value) const const
QSet::iterator end()
QSet::iterator find(const T &value)
QSet::iterator insert(const T &value)
bool remove(const T &value)
void resize(int size)
std::string toStdString() const const
void append(const T &value)