HAL
gate_info_table.cpp
Go to the documentation of this file.
2 
4 
5 #include "gui/gui_globals.h"
12 
13 #include <QMenu>
14 #include <QInputDialog>
15 
16 namespace hal
17 {
18  const QString GateInfoTable::nameRowKey = "Name";
19  const QString GateInfoTable::idRowKey = "ID";
20  const QString GateInfoTable::typeRowKey = "Type";
21  const QString GateInfoTable::gateTypePropertiesRowKey = "Properties";
22  const QString GateInfoTable::locationRowKey = "Location";
23  const QString GateInfoTable::moduleRowKey = "Module";
24 
25  GateInfoTable::GateInfoTable(QWidget* parent) : GeneralTableWidget(parent), mGate(nullptr)
26  {
27  mNameEntryContextMenu = new QMenu();
28  mNameEntryContextMenu->addAction("Name to clipboard", std::bind(&GateInfoTable::copyName, this));
29  mNameEntryContextMenu->addSection("Misc");
30  mNameEntryContextMenu->addAction("Change name", std::bind(&GateInfoTable::changeName, this));
31  mNameEntryContextMenu->addSection("Python");
32  mNameEntryContextMenu->addAction(QIcon(":/icons/python"), "Get name", std::bind(&GateInfoTable::pyCopyName, this));
33 
34  mIdEntryContextMenu = new QMenu();
35  mIdEntryContextMenu->addAction("ID to clipboard", std::bind(&GateInfoTable::copyId, this));
36  mIdEntryContextMenu->addSection("Misc");
37  mIdEntryContextMenu->addAction(QIcon(":/icons/python"), "Get ID", std::bind(&GateInfoTable::pyCopyId, this));
38 
39  mTypeEntryContextMenu = new QMenu();
40  mTypeEntryContextMenu->addAction("Type name to clipboard", std::bind(&GateInfoTable::copyType, this));
41  mTypeEntryContextMenu->addSection("Python");
42  mTypeEntryContextMenu->addAction(QIcon(":/icons/python"), "Get type", std::bind(&GateInfoTable::pyCopyType, this));
43 
44  mPropertiesEntryContextMenu = new QMenu();
45  mPropertiesEntryContextMenu->addAction("Properties to clipboard", std::bind(&GateInfoTable::copyproperties, this));
46  mPropertiesEntryContextMenu->addSection("Python");
47  mPropertiesEntryContextMenu->addAction(QIcon(":/icons/python"), "Get properties", std::bind(&GateInfoTable::pyCopyproperties, this));
48 
49  mLocationEntryContextMenu = new QMenu();
50  mLocationEntryContextMenu->addAction("Location to clipboard", std::bind(&GateInfoTable::copyLocation, this));
51  mLocationEntryContextMenu->addSection("Phyton");
52  mLocationEntryContextMenu->addAction(QIcon(":/icons/python"), "Get location", std::bind(&GateInfoTable::pyCopyLocation, this));
53 
54  mModuleEntryContextMenu = new QMenu();
55  mModuleEntryContextMenu->addAction("Module name to clipboard", std::bind(&GateInfoTable::copyModuleName, this));
56  mModuleEntryContextMenu->addAction("Module ID to clipboard", std::bind(&GateInfoTable::copyModuleID, this));
57  mModuleEntryContextMenu->addAction("Set as current selection", std::bind(&GateInfoTable::setModuleAsSelection, this));
58  mModuleEntryContextMenu->addAction("Add to current selection", std::bind(&GateInfoTable::addModuleToSelection, this));
59  mModuleEntryContextMenu->addAction("Change module", std::bind(&GateInfoTable::moveToModuleAction, this));
60  mModuleEntryContextMenu->addSection("Python");
61  mModuleEntryContextMenu->addAction(QIcon(":/icons/python"), "Get module", std::bind(&GateInfoTable::pyCopyModule, this));
62 
63  mModuleDoubleClickedAction = std::bind(&GateInfoTable::setModuleAsSelection, this);
64 
65  connect(gNetlistRelay, &NetlistRelay::gateRemoved, this, &GateInfoTable::handleGateRemoved);
66  connect(gNetlistRelay, &NetlistRelay::gateNameChanged, this, &GateInfoTable::handleGateNameChanged);
67  connect(gNetlistRelay, &NetlistRelay::gateLocationChanged, this, &GateInfoTable::handleGateLocationChanged);
68  connect(gNetlistRelay, &NetlistRelay::moduleNameChanged, this, &GateInfoTable::handleModuleNameChanged);
69  connect(gNetlistRelay, &NetlistRelay::moduleGateAssigned, this, &GateInfoTable::handleModuleGateAssigned);
70  }
71 
73  {
74  if(gNetlist->is_gate_in_netlist(gate))
75  {
76  mGate = gate;
77 
78  setRow(nameRowKey , name(), mNameEntryContextMenu);
79  setRow(idRowKey, id(), mIdEntryContextMenu);
80  setRow(typeRowKey, type(), mTypeEntryContextMenu);
81  setRow(gateTypePropertiesRowKey, properties(), mPropertiesEntryContextMenu);
82  setRow(locationRowKey, location(), mLocationEntryContextMenu);
83  setRow(moduleRowKey, parentModule(), mModuleEntryContextMenu, mModuleDoubleClickedAction);
84 
85  adjustSize();
86  }
87  }
88 
89  QString GateInfoTable::name() const
90  {
91  return QString::fromStdString(mGate->get_name());
92  }
93 
94  QString GateInfoTable::id() const
95  {
96  return QString::number(mGate->get_id());
97  }
98 
99  QString GateInfoTable::type() const
100  {
101  QString gateType = "None";
102 
103  GateType* type = mGate->get_type();
104 
105  if(type)
106  gateType = QString::fromStdString(type->get_name());
107 
108  return gateType;
109  }
110 
111  QString GateInfoTable::properties() const
112  {
113  QString properties = "None";
114 
115  GateType* type = mGate->get_type();
116 
117  if(type)
118  {
119  properties.clear();
120 
121  for(hal::GateTypeProperty gtp : type->get_properties())
122  properties.append(QString::fromStdString(enum_to_string(gtp)) + ", ");
123 
124  properties.chop(2);
125  }
126 
127  return properties;
128  }
129 
130  QString GateInfoTable::location() const
131  {
132  QString location = "N/A";
133 
134  if (mGate->has_location())
135  {
136  location =
137  "X:" + QString::number(mGate->get_location_x()) +
138  " Y:" + QString::number(mGate->get_location_y());
139  }
140 
141  return location;
142  }
143 
144  QString GateInfoTable::parentModule() const
145  {
146  if(!mGate)
147  return "";
148 
149  Module* module = mGate->get_module();
150 
151  return QString::fromStdString(module->get_name()) + "[ID:" + QString::number(module->get_id()) + "]";
152  }
153 
154  void GateInfoTable::changeName()
155  {
156  QString oldName = QString::fromStdString(mGate->get_name());
157  QString prompt = "Change gate name";
158 
159  bool confirm;
160  QString newName = QInputDialog::getText(this, prompt, "New name:", QLineEdit::Normal, oldName, &confirm);
161 
162  if (confirm)
163  {
164  ActionRenameObject* act = new ActionRenameObject(newName);
165  act->setObject(UserActionObject(mGate->get_id(), UserActionObjectType::ObjectType::Gate));
166  act->exec();
167  }
168  }
169 
170  void GateInfoTable::copyName() const
171  {
172  copyToClipboard(name());
173  }
174 
175  void GateInfoTable::pyCopyName() const
176  {
178  }
179 
180  void GateInfoTable::copyId() const
181  {
182  copyToClipboard(id());
183  }
184 
185  void GateInfoTable::pyCopyId() const
186  {
188  }
189 
190  void GateInfoTable::copyType() const
191  {
193  }
194 
195  void GateInfoTable::pyCopyType() const
196  {
198  }
199 
200  void GateInfoTable::copyproperties() const
201  {
202  copyToClipboard(properties());
203  }
204 
205  void GateInfoTable::pyCopyproperties() const
206  {
208  }
209 
210  void GateInfoTable::copyLocation() const
211  {
212  copyToClipboard(location());
213  }
214 
215  void GateInfoTable::pyCopyLocation() const
216  {
218  }
219 
220  void GateInfoTable::copyModuleName() const
221  {
222  //copyToClipboard(parentModule().replace(QRegularExpression("\\[Id:\\d*]"), ""));
224  }
225 
226  void GateInfoTable::pyCopyModule() const
227  {
229  }
230 
231  void GateInfoTable::copyModuleID() const
232  {
234  }
235 
236  void GateInfoTable::addModuleToSelection()
237  {
240  }
241 
242  void GateInfoTable::setModuleAsSelection()
243  {
247  }
248 
249  void GateInfoTable::moveToModuleAction()
250  {
251  QSet<u32> excludeMods;
252  if (mGate) excludeMods.insert(mGate->get_module()->get_id());
253  ModuleDialog md(excludeMods, "Move to module", false, nullptr, this);
254  if (md.exec() != QDialog::Accepted) return;
255  if (md.isNewModule())
256  {
257  QString currModName = QString::fromStdString(mGate->get_module()->get_name());
258  bool ok;
259  QString name = QInputDialog::getText(nullptr, "", "New module will be created under \"" + currModName + "\"\nModule Name:", QLineEdit::Normal, "", &ok);
260  if (!ok || name.isEmpty()) return;
261 
262  ActionCreateObject* actNewModule = new ActionCreateObject(UserActionObjectType::Module, name);
263  actNewModule->setParentId(mGate->get_module()->get_id());
264  UserActionCompound* compound = new UserActionCompound;
265  compound->setUseCreatedObject();
266  compound->addAction(actNewModule);
267  compound->addAction(new ActionAddItemsToObject(QSet<u32>(), QSet<u32>() << mGate->get_id()));
268  compound->exec();
269  return;
270  }
271  ActionAddItemsToObject* addAct = new ActionAddItemsToObject(QSet<u32>(), QSet<u32>() << mGate->get_id());
272  addAct->setObject(UserActionObject(md.selectedId(), UserActionObjectType::Module));
273  addAct->exec();
274  }
275 
276  void GateInfoTable::handleGateRemoved(Gate* gate)
277  {
278  if(mGate == gate)
279  {
280  mGate = nullptr;
281 
282  const QString notification("Displayed gate has been removed.");
283 
284  setRow(nameRowKey, notification, nullptr);
285  setRow(idRowKey, notification, nullptr);
286  setRow(typeRowKey, notification, nullptr);
287  setRow(gateTypePropertiesRowKey, notification, nullptr);
288  setRow(locationRowKey, notification, nullptr);
289  setRow(moduleRowKey, notification, nullptr, nullptr);
290 
291  adjustSize();
292  }
293  }
294 
295  void GateInfoTable::handleGateNameChanged(Gate* gate)
296  {
297  if(mGate == gate)
298  refresh();
299  }
300 
301  void GateInfoTable::handleGateLocationChanged(Gate* gate)
302  {
303  if(mGate == gate)
304  refresh();
305  }
306 
307  void GateInfoTable::handleModuleNameChanged(Module* module)
308  {
309  if(!mGate)
310  return;
311 
312  if(mGate->get_module() == module)
313  refresh();
314  }
315 
316  void GateInfoTable::handleModuleGateAssigned(Module* module, const u32 gateId)
317  {
318  Q_UNUSED(module)
319 
320  if(!mGate)
321  return;
322 
323  if(mGate->get_id() == gateId)
324  refresh();
325  }
326 
327  void GateInfoTable::refresh()
328  {
329  setGate(mGate);
330  }
331 }
Definition: gate.h:58
i32 get_location_x() const
Definition: gate.cpp:130
i32 get_location_y() const
Definition: gate.cpp:135
GateType * get_type() const
Definition: gate.cpp:125
const std::string & get_name() const
Definition: gate.cpp:105
bool has_location() const
Definition: gate.cpp:145
Module * get_module() const
Definition: gate.cpp:174
u32 get_id() const
Definition: gate.cpp:95
void setGate(Gate *gate)
GateInfoTable(QWidget *parent=nullptr)
void setRow(const QString &key, const QString &val, QMenu *contextMenu=nullptr, std::function< void()> doubleClickAction=nullptr)
void copyToClipboard(const QString &text) const
std::string get_name() const
Definition: module.cpp:87
u32 get_id() const
Definition: module.cpp:82
bool is_gate_in_netlist(const Gate *gate) const
Definition: netlist.cpp:188
void moduleGateAssigned(Module *m, const u32 assigned_gate) const
void moduleNameChanged(Module *m) const
void gateRemoved(Gate *g) const
void gateNameChanged(Gate *g) const
void gateLocationChanged(Gate *g) const
static QString pyCodeGateLocation(u32 gateId)
static QString pyCodeGateType(u32 gateId)
static QString pyCodeProperties(u32 gateId)
static QString pyCodeGateModule(u32 gateId)
static QString pyCodeGateName(u32 gateId)
static QString pyCodeGateId(u32 gateId)
void relaySelectionChanged(void *sender)
const Module * module(const Gate *g, const NodeBoxes &boxes)
SelectionRelay * gSelectionRelay
Definition: plugin_gui.cpp:83
Netlist * gNetlist
Definition: plugin_gui.cpp:80
NetlistRelay * gNetlistRelay
Definition: plugin_gui.cpp:81
std::string enum_to_string(T e)
Definition: enums.h:52
quint32 u32
PinType type
std::string name
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
QAction * addAction(const QString &text)
QAction * addSection(const QString &text)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QSet::iterator insert(const T &value)
QString & append(QChar ch)
void chop(int n)
void clear()
QString fromStdString(const std::string &str)
QString number(int n, int base)