HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
module_widget.cpp
Go to the documentation of this file.
2 
5 #include "gui/gui_globals.h"
7 #include "gui/toolbar/toolbar.h"
10 #include "hal_core/netlist/gate.h"
12 #include "hal_core/netlist/net.h"
17 
18 #include <QApplication>
19 #include <QHeaderView>
20 #include <QItemSelectionModel>
21 #include <QMenu>
22 #include <QModelIndex>
23 #include <QScrollBar>
24 #include <QSet>
25 #include <QShortcut>
26 #include <QTreeView>
27 #include <QVBoxLayout>
28 
29 namespace hal
30 {
32  : ContentWidget("Modules", parent),
33  mTreeView(new ModuleTreeView(this)),
34  mSearchbar(new Searchbar(this)),
35  mToggleNetsAction(new QAction(this)),
36  mToggleGatesAction(new QAction(this)),
37  mRenameAction(new QAction(this)),
38  mDeleteAction(new QAction(this)),
39  mToggleExpandTreeAction(new QAction(this)),
40  mModuleProxyModel(new ModuleProxyModel(this))
41 
42  {
44 
46 
47  mToggleNetsAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mHideNetsIconPath));
48  mToggleGatesAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mHideGatesIconPath));
49  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mSearchIconStyle, mSearchIconPath));
50  mRenameAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mRenameIconPath));
51  mToggleExpandTreeAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mExpandedIconPath));
52 
53  mToggleNetsAction->setToolTip("Toggle net visibility");
54  mToggleGatesAction->setToolTip("Toggle gate visibility");
55  mDeleteAction->setToolTip("Delete module");
56  mSearchAction->setToolTip("Search");
57  mRenameAction->setToolTip("Rename");
58  mToggleExpandTreeAction->setToolTip("Toggle expand all / collapse all");
59 
60  mModuleModel = new ModuleModel(this);
61  mModuleProxyModel->setSourceModel(mModuleModel);
62 
63  mTreeView->setModel(mModuleProxyModel);
64  mTreeView->setDefaultColumnWidth();
65  mTreeView->setSortingEnabled(true);
66  mTreeView->sortByColumn(0, Qt::AscendingOrder);
69  mTreeView->setFrameStyle(QFrame::NoFrame);
70  //mTreeView->header()->close();
71  mTreeView->setExpandsOnDoubleClick(false);
72  // all rows are plain single line text, so the view must not measure each of them separately
73  mTreeView->setUniformRowHeights(true);
76  mContentLayout->addWidget(mTreeView);
77 
78  mSearchbar->setColumnNames(mModuleModel->headerLabels());
79  mContentLayout->addWidget(mSearchbar);
80  mSearchbar->hide();
81 
82  enableDeleteAction(false); // enable upon selected module
83  mIgnoreSelectionChange = false;
84 
86 
87  //connect(mSearchbar, &Searchbar::textEdited, this, &ModuleWidget::filter);
92 
94 
96  connect(mSearchbar, &Searchbar::triggerNewSearch, mModuleProxyModel, &ModuleProxyModel::startSearch);
97 
98  mShortCutDeleteItem = new QShortcut(ContentManager::sSettingDeleteItem->value().toString(), this);
99  mShortCutDeleteItem->setEnabled(false);
100 
102  connect(mShortCutDeleteItem, &QShortcut::activated, this, &ModuleWidget::deleteSelectedItem);
103  connect(mDeleteAction, &QAction::triggered, this, &ModuleWidget::deleteSelectedItem);
104 
105  connect(qApp, &QApplication::focusChanged, this, &ModuleWidget::handleDeleteShortcutOnFocusChanged);
106 
107 
108  connect(mToggleNetsAction, &QAction::triggered, this, &ModuleWidget::handleToggleNetsClicked);
109  connect(mToggleGatesAction, &QAction::triggered, this, &ModuleWidget::handleToggleGatesClicked);
110  connect(mToggleExpandTreeAction, &QAction::triggered, this, &ModuleWidget::handleToggleExpandTreeClicked);
111  connect(mRenameAction, &QAction::triggered, this, &ModuleWidget::handleRenameClicked);
112 
113  mModuleModel->setShowNetsOutsideModules(true); // so that the global inputs and outputs show up as well
114  mModuleModel->populateTree({gNetlist->get_top_module()->get_id()});
115  mTreeView->expandAllModules();
116  }
117 
118  void ModuleWidget::enableDeleteAction(bool enable)
119  {
120  mDeleteAction->setEnabled(enable);
121  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(enable?mActiveIconStyle:mDisabledIconStyle, mDeleteIconPath));
122  }
123 
124  void ModuleWidget::handleToggleNetsClicked()
125  {
126  if(mModuleProxyModel->toggleFilterNets())
127  {
128  mToggleNetsAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mHideNetsIconPath));
129  }
130  else
131  {
132  mToggleNetsAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mShowNetsIconPath));
133  }
134  }
135 
136  void ModuleWidget::handleToggleGatesClicked()
137  {
138  if(mModuleProxyModel->toggleFilterGates())
139  {
140  mToggleGatesAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mHideGatesIconPath));
141  }
142  else
143  {
144  mToggleGatesAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mShowGatesIconPath));
145  }
146  }
147 
148  void ModuleWidget::handleToggleExpandTreeClicked()
149  {
150  if (mTreeView->toggleStateExpanded())
151  {
152  mTreeView->collapseAllModules();
153  mToggleExpandTreeAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mCollapsedIconPath));
154  }
155  else
156  {
157  mTreeView->expandAllModules();
158  mToggleExpandTreeAction->setIcon(gui_utility::getStyledSvgIcon(mActiveIconStyle, mExpandedIconPath));
159  }
160  }
161 
162  void ModuleWidget::handleRenameClicked()
163  {
164  ModuleItem* item = getModuleItemFromIndex(mTreeView->currentIndex());
165  gNetlistRelay->changeElementNameDialog(item->getType(), item->id());
166  }
167 
169  {
170  toolbar->addAction(mToggleNetsAction);
171  toolbar->addAction(mToggleGatesAction);
172  toolbar->addAction(mRenameAction);
173  toolbar->addAction(mDeleteAction);
174  toolbar->addAction(mSearchAction);
175  toolbar->addAction(mToggleExpandTreeAction);
176  }
177 
179  {
182 
183  QList<QShortcut*> list;
184  list.append(mSearchShortcut);
185 
186  return list;
187  }
188 
190  {
191  if (!mSearchAction->isEnabled())
192  return;
193 
194  if (mSearchbar->isHidden())
195  {
196  mSearchbar->show();
197  mSearchbar->setFocus();
198  }
199  else
200  {
201  mSearchbar->hide();
202  setFocus();
203  }
204  }
205 
206  void ModuleWidget::filter(const QString& text)
207  {
208  QRegularExpression* regex = new QRegularExpression(text);
209  if (regex->isValid())
210  {
211  mModuleProxyModel->setFilterRegularExpression(*regex);
212  mTreeView->expandAllModules();
213  QString output = "navigation regular expression '" + text + "' entered.";
214  log_info("user", output.toStdString());
215  }
216  }
217 
219  {
220  QModelIndex index = mTreeView->indexAt(point);
221  if (!index.isValid())
222  return;
223 
224  ModuleItem* item = getModuleItemFromIndex(index);
226  u32 id = item->id();
227 
228  QMenu context_menu;
229 
231  ModuleContextMenu::addModuleSubmenu(&context_menu, id);
233  ModuleContextMenu::addGateSubmenu(&context_menu, id);
235  ModuleContextMenu::addNetSubmenu(&context_menu, id);
236 
238  type==ModuleItem::TreeItemType::Module ? std::vector<u32>({id}) : std::vector<u32>(),
239  type==ModuleItem::TreeItemType::Gate ? std::vector<u32>({id}) : std::vector<u32>(),
240  type==ModuleItem::TreeItemType::Net ? std::vector<u32>({id}) : std::vector<u32>());
241 
242  context_menu.exec(mTreeView->viewport()->mapToGlobal(point));
243  }
244 
245  void ModuleWidget::handleCurrentChanged(const QModelIndex& current, const QModelIndex& previous)
246  {
247  Q_UNUSED(previous);
248  ModuleItem* mi = getModuleItemFromIndex(current);
249  if (!mi)
250  enableDeleteAction(false);
251  else
252  enableDeleteAction(mi->getType() == ModuleItem::TreeItemType::Module);
253  }
254 
256  {
257  Q_UNUSED(selected)
258  Q_UNUSED(deselected)
259 
260  // the model is modifying while it removes or moves the items of (re)moved modules, which deselects them
261  if (mIgnoreSelectionChange || mModuleModel->isModifying())
262  return;
263 
265 
266  QModelIndexList current_selection = mTreeView->selectionModel()->selectedIndexes();
267 
268  QSet<int> selectedMods;
269  QSet<int> selectedGats;
270  for (const auto& index : current_selection)
271  {
272  ModuleItem* mi = getModuleItemFromIndex(index);
273  switch (mi->getType()) {
275  selectedMods.insert(mi->id());
276  gSelectionRelay->addModule(mi->id());
277  break;
279  selectedGats.insert(mi->id());
280  gSelectionRelay->addGate(mi->id());
281  break;
283  gSelectionRelay->addNet(mi->id());
284  break;
285  }
286  }
287 
288  if (selectedMods.size() == 1)
289  {
291  enableDeleteAction(true);
292  }
293  else if (selectedGats.size() == 1)
294  {
296  enableDeleteAction(false);
297  }
298  else
299  {
300  enableDeleteAction(false);
301  }
302 
304  }
305 
307  {
308  ModuleItem* mi = getModuleItemFromIndex(index);
309  switch(mi->getType()){
313  }
314  }
315 
317  {
318  if (sender == this)
319  return;
320 
321  mIgnoreSelectionChange = true;
322 
323  QItemSelection module_selection;
324 
325  for (auto module_id : gSelectionRelay->selectedModulesList())
326  {
327  for(ModuleItem* item : mModuleModel->getItems(module_id))
328  {
329  if(item)
330  {
331  QModelIndex index = mModuleProxyModel->mapFromSource(mModuleModel->getIndexFromItem(item));
332  module_selection.select(index, index);
333  }
334  }
335  }
336 
337  mTreeView->selectionModel()->select(module_selection, QItemSelectionModel::SelectionFlag::ClearAndSelect);
338 
339  mIgnoreSelectionChange = false;
340  }
341 
342  ModuleItem* ModuleWidget::getModuleItemFromIndex(const QModelIndex& index)
343  {
344  return mModuleModel->getItem(mModuleProxyModel->mapToSource(index));
345  }
346 
348  {
349  if (mSearchbar->filterApplied() && mSearchbar->isVisible())
350  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mSearchActiveIconStyle, mSearchIconPath));
351  else
352  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mSearchIconStyle, mSearchIconPath));
353  }
354 
356  {
357  return mModuleModel;
358  }
359 
361  {
362  return mDisabledIconStyle;
363  }
364 
366  {
367  return mActiveIconStyle;
368  }
369 
371  {
372  return mModuleProxyModel;
373  }
374 
376  {
377  return mShowNetsIconPath;
378  }
379 
381  {
382  return mHideNetsIconPath;
383  }
384 
386  {
387  return mShowGatesIconPath;
388  }
389 
391  {
392  return mHideGatesIconPath;
393  }
394 
396  {
397  return mSearchIconPath;
398  }
399 
401  {
402  return mSearchIconStyle;
403  }
404 
406  {
407  return mSearchActiveIconStyle;
408  }
409 
411  {
412  return mDeleteIconPath;
413  }
414 
416  {
417  return mRenameIconPath;
418  }
419 
421  {
422  return mExpandedIconPath;
423  }
424 
426  {
427  return mCollapsedIconPath;
428  }
429 
431  {
432  mDisabledIconStyle = style;
433  }
434 
436  {
437  mActiveIconStyle = style;
438  }
439 
441  {
442  mShowNetsIconPath = path;
443  }
444 
446  {
447  mHideNetsIconPath = path;
448  }
449 
451  {
452  mShowGatesIconPath = path;
453  }
454 
456  {
457  mHideGatesIconPath = path;
458  }
459 
461  {
462  mSearchIconPath = path;
463  }
464 
466  {
467  mSearchIconStyle = style;
468  }
469 
471  {
472  mSearchActiveIconStyle = style;
473  }
474 
476  {
477  mDeleteIconPath = path;
478  }
479 
481  {
482  mRenameIconPath = path;
483  }
484 
486  {
487  mExpandedIconPath = path;
488  }
489 
491  {
492  mCollapsedIconPath = path;
493  }
494 
495  void ModuleWidget::deleteSelectedItem()
496  {
497  if(!mTreeView->currentIndex().isValid())
498  {
499  return;
500  }
501 
502  ModuleItem* selectedItem = getModuleItemFromIndex(mTreeView->currentIndex());
503  if(selectedItem->getParent() != nullptr)
504  {
505  if (getModuleItemFromIndex(mTreeView->currentIndex())->getType() == ModuleItem::TreeItemType::Module)
506  {
507  auto module = gNetlist->get_module_by_id(selectedItem->id());
508  if(!module->is_top_module())
509  gNetlistRelay->deleteModule(getModuleItemFromIndex(mTreeView->currentIndex())->id());
510  }
511  }
512  }
513 
514  void ModuleWidget::handleDeleteShortcutOnFocusChanged(QWidget* oldWidget, QWidget* newWidget)
515  {
516  Q_UNUSED(oldWidget);
517  if(!newWidget) return;
518  if(newWidget->parent() == this)
519  {
520  mShortCutDeleteItem->setEnabled(true);
521  return;
522  }
523  else
524  {
525  mShortCutDeleteItem->setEnabled(false);
526  return;
527  }
528  }
529 }
QModelIndex getIndexFromItem(BaseTreeItem *item) const
QStringList headerLabels() const
static SettingsItemKeybind * sSettingDeleteItem
Abstract class for Widgets within HAL's ContentArea.
QKeySequence mSearchKeysequence
QString name() const
QShortcut * mSearchShortcut
QVBoxLayout * mContentLayout
void openModuleInView(u32 moduleId, bool unfold)
static void addPluginSubmenus(QMenu *contextMenu, Netlist *netlist, const std::vector< u32 > &modules, const std::vector< u32 > &gates, const std::vector< u32 > &nets)
static void addModuleSubmenu(QMenu *contextMenu, u32 id)
static void addGateSubmenu(QMenu *contextMenu, u32 id)
static void addNetSubmenu(QMenu *contextMenu, u32 id)
bool is_top_module() const
Definition: module.cpp:321
An item in the ModuleModel.
Definition: module_item.h:48
u32 id() const
TreeItemType getType() const
A model for displaying multiple netlist elements.
Definition: module_model.h:54
void setShowNetsOutsideModules(bool enable)
QList< ModuleItem * > getItems(const u32 id, ModuleItem::TreeItemType type=ModuleItem::TreeItemType::Module) const
ModuleItem * getItem(const QModelIndex &index) const
void populateTree(const QVector< u32 > &modIds={}, const QVector< u32 > &gatIds={}, const QVector< u32 > &netIds={})
Enables filtering in the ModuleModel.
void startSearch(QString text, int options) override
Wraps the QTreeView used for the ModuleWidget.
bool toggleStateExpanded() const
void handleTreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void setSearchIconPath(const QString &path)
void setSearchIconStyle(const QString &style)
QString activeIconStyle() const
void setExpandedIconPath(const QString &path)
void setHideGatesIconPath(const QString &path)
QString renameIconPath() const
void setDisabledIconStyle(const QString &style)
QString searchIconPath() const
void filter(const QString &text)
QString expandedIconPath() const
void handleSelectionChanged(void *sender)
QString disabledIconStyle() const
void setShowNetsIconPath(const QString &path)
QString hideNetsIconPath() const
void handleTreeViewContextMenuRequested(const QPoint &point)
void setDeleteIconPath(const QString &path)
QString showNetsIconPath() const
virtual QList< QShortcut * > createShortcuts() override
QString searchIconStyle() const
ModuleProxyModel * proxyModel()
void setShowGatesIconPath(const QString &path)
void setCollapsedIconPath(const QString &path)
void handleItemDoubleClicked(const QModelIndex &index)
void handleCurrentChanged(const QModelIndex &current, const QModelIndex &previous=QModelIndex())
ModuleWidget(QWidget *parent=nullptr)
void setRenameIconPath(const QString &path)
void setSearchActiveIconStyle(const QString &style)
QString showGatesIconPath() const
void setHideNetsIconPath(const QString &path)
QString deleteIconPath() const
QString collapsedIconPath() const
void setActiveIconStyle(const QString &style)
QString hideGatesIconPath() const
ModuleModel * getModuleModel() const
QString searchActiveIconStyle() const
virtual void setupToolbar(Toolbar *toolbar) override
Module * get_top_module() const
Definition: netlist.cpp:610
Module * get_module_by_id(u32 module_id) const
Definition: netlist.cpp:615
void deleteModule(const u32 id)
void changeElementNameDialog(ModuleItem::TreeItemType type, u32 id)
A QFrame with a QLineEdit that can be used to input a substring to search for.
Definition: searchbar.h:48
bool filterApplied()
Definition: searchbar.cpp:259
void setColumnNames(QList< QString > list)
Definition: searchbar.cpp:130
void triggerNewSearch(const QString &text, int searchOptions)
void setFocus(ItemType ftype, u32 fid, Subfocus sfoc=Subfocus::None, u32 sfinx=0)
void selectionChanged(void *sender)
void relaySelectionChanged(void *sender)
QList< u32 > selectedModulesList() const
void registerSender(void *sender, QString name)
void keySequenceChanged(QKeySequence value)
Toolbar for all ContentFrames and ContentWidgets.
Definition: toolbar.h:39
uint32_t u32
Definition: defines.h:41
#define log_info(channel,...)
Definition: log.h:70
const Module * module(const Gate *g, const NodeBoxes &boxes)
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
Definition: defines.h:45
GraphContextManager * gGraphContextManager
Definition: plugin_gui.cpp:86
SelectionRelay * gSelectionRelay
Definition: plugin_gui.cpp:84
Netlist * gNetlist
Definition: gui_globals.h:69
NetlistRelay * gNetlistRelay
Definition: gui_globals.h:74
PinType type
QModelIndex currentIndex() const const
void doubleClicked(const QModelIndex &index)
void setEditTriggers(QAbstractItemView::EditTriggers triggers)
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
void setSelectionMode(QAbstractItemView::SelectionMode mode)
QItemSelectionModel * selectionModel() const const
QWidget * viewport() const const
void setEnabled(bool)
void setIcon(const QIcon &icon)
void setToolTip(const QString &tip)
void trigger()
void triggered(bool checked)
void focusChanged(QWidget *old, QWidget *now)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setFrameStyle(int style)
void select(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
virtual void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void append(const T &value)
QAction * exec()
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QObject * sender() const const
bool isValid() const const
QSet::iterator begin()
QSet::iterator insert(const T &value)
int size() const const
void activated()
void setEnabled(bool enable)
void setKey(const QKeySequence &key)
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
void setFilterRegularExpression(const QString &pattern)
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QueuedConnection
CustomContextMenu
AscendingOrder
QAction * addAction(const QString &text)
void sortByColumn(int column)
void setExpandsOnDoubleClick(bool enable)
virtual QModelIndex indexAt(const QPoint &point) const const override
virtual void setModel(QAbstractItemModel *model) override
void setSortingEnabled(bool enable)
void setUniformRowHeights(bool uniform)
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void customContextMenuRequested(const QPoint &pos)
void ensurePolished() const const
void hide()
bool isHidden() const const
QPoint mapToGlobal(const QPoint &pos) const const
void setFocus()
void show()
QStyle * style() const const
bool isVisible() const const