HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
gatelibrary_content_widget.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
6 #include "gui/toolbar/toolbar.h"
10 
11 #include <QHeaderView>
12 #include <QVBoxLayout>
13 #include <QMenu>
14 
15 namespace hal
16 {
17 
19  {
20  QVBoxLayout* layout = new QVBoxLayout(this);
22  setLineWidth(2);
24  mSearchbar = new Searchbar(this);
25 
26  mTableView = new QTableView(this);
27  mPinProxyModel = new PinProxyModel(this);
28  mPinProxyModel->setSourceModel(model);
29  mTableView->setModel(mPinProxyModel);
34  // mTableView->horizontalHeader()->setStretchLastSection(true);
35  mTableView->verticalHeader()->hide();
36 
39 
40  mTableView->setSortingEnabled(true);
41  mTableView->sortByColumn(0, Qt::AscendingOrder);
42 
43 
44  mToolbar = new Toolbar;
46  mToolbar->setIconSize(QSize(18, 18));
47 
48  mAddAction = new QAction("Add gate type", this);
49  mEditAction = new QAction("Edit gate type", this);
50  mDeleteAction = new QAction("Delete gate type", this);
51  mSearchAction = new QAction("Search", this);
52 
53  //connections
55  connect(mTableView, &QTableView::doubleClicked, this, &GatelibraryContentWidget::handleDoubleClicked);
56  connect(mTableView->selectionModel(), &QItemSelectionModel::currentChanged, this, &GatelibraryContentWidget::handleCurrentSelectionChanged);
57  connect(mSearchAction, &QAction::triggered, this, &GatelibraryContentWidget::toggleSearchbar);
58  connect(mEditAction, &QAction::triggered, this, &GatelibraryContentWidget::handleEditAction);
59  connect(mDeleteAction, &QAction::triggered, this, &GatelibraryContentWidget::handleDeleteAction);
60 
61  connect(mDeleteAction, &QAction::triggered, this, &GatelibraryContentWidget::handleUnsavedChanges);
62 
64 
65  mToolbar->addAction(mAddAction);
66  mToolbar->addAction(mEditAction);
67  mToolbar->addAction(mDeleteAction);
68  mToolbar->addAction(mSearchAction);
69 
70 
71  layout->addWidget(mToolbar);
72  layout->addWidget(mTableView);
73  layout->addWidget(mSearchbar);
74 
75  mSearchbar->hide();
76  mSearchbar->setColumnNames({"Name", "ID"});
77  }
78 
79  bool GatelibraryContentWidget::initialize(GateLibrary* gateLibrary, bool readOnly)
80  {
81  if(!gateLibrary)
82  {
84  // readonly flag makes sure mGateLibrary does not get modified, const attribute not needed
85  mGateLibrary = const_cast<GateLibrary*>(gNetlist->get_gate_library());
86  mReadOnly = true;
87  }
88  else
89  {
90  QString title = "Load gate library";
91  QString text = "HAL Gate Library (*.hgl *.lib)";
92  QString path = QDir::currentPath();
93  QFile gldpath(":/path/gate_library_definitions");
94  if (gldpath.open(QIODevice::ReadOnly))
95  path = QString::fromUtf8(gldpath.readAll());
96 
97  //TODO fileDialog throws bad window error
98  QString fileName = QFileDialog::getOpenFileName(nullptr, title, path, text, nullptr);
99  if (fileName == nullptr)
100  return false;
101 
104 
105  auto gateLibrary = FileManager::loadGateLibrary(fileName);
106 
107  if(gateLibrary)
108  mGateLibrary = gateLibrary;
109  else
110  {
111  //extract gatelibrary name from path
112  mGateLibrary = new GateLibrary(std::filesystem::path(fileName.toStdString()), QFileInfo(fileName).baseName().toStdString());
113  }
114 
115  mReadOnly = false;
116  }
117 
118  }
119  else
120  {
121  mReadOnly = readOnly;
122  mGateLibrary = gateLibrary;
123  }
124  QDir dir(QDir::home());
126  QString("GateLibrary %1").arg(QDir::home().relativeFilePath(QString::fromStdString(mGateLibrary->get_path().string()))));
127  mPath = mGateLibrary->get_path().generic_string();
128  return true;
129  }
130 
132  {
133  QModelIndex idx = mTableView->indexAt(pos);
134 
135  if(!idx.isValid())
136  return;
137 
138  QMenu menu;
139 
140  menu.addAction(mEditAction);
141  menu.addAction(mDeleteAction);
142 
143  menu.move(mapToGlobal(pos));
144  menu.exec();
145  }
146 
148  {
149  mGateLibrary = gl;
150  }
151 
152  void GatelibraryContentWidget::setGateLibraryPath(std::filesystem::path p)
153  {
154  mPath = p;
155  }
156 
157  void GatelibraryContentWidget::handleEditAction()
158  {
159  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
160  if (inx.isValid())
161  Q_EMIT triggerEditType(inx);
162  }
163 
164  void GatelibraryContentWidget::handleDeleteAction()
165  {
166  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
167  if (inx.isValid())
169  }
170 
171  void GatelibraryContentWidget::handleCurrentSelectionChanged(QModelIndex prevIndex){
172  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
173  if (inx.isValid())
174  Q_EMIT triggerCurrentSelectionChanged(inx, prevIndex);
175  }
176 
177  void GatelibraryContentWidget::handleDoubleClicked(QModelIndex index)
178  {
179  QModelIndex inx = mPinProxyModel->mapToSource(index);
180  if (inx.isValid())
182  }
183 
185  {
186  if(mCreationMode)
188  else
189  {
192  if(gate_library_manager::save(mPath,mGateLibrary,true))
193  {
195  window()->setWindowTitle(mTitle);
196  }
197  }
198  mCreationMode = false;
199  }
200 
202  {
203  std::filesystem::path oldPath = mGateLibrary->get_path();
204  QString path = QDir::currentPath();
205  QFile gldpath(":/path/gate_library_definitions");
206  if (gldpath.open(QIODevice::ReadOnly))
207  path = QString::fromUtf8(gldpath.readAll());
208 
211 
212  QString filename = "";
213  QFileDialog fd(this, "Save as", path, "HGL *.hgl");
214  fd.setDefaultSuffix(".hgl");
216  if (fd.exec()) {
217  filename = fd.selectedFiles().front();
218  }
219 
220  // order of conditions important:
221  // 1. Filename for save as provided
222  // 2. Copy was saved under new filename
223  // 3. We ar *not* in readOnly Mode
224  // => continue to work with copy
225  if (!filename.isEmpty() && gate_library_manager::save(filename.toStdString(),mGateLibrary,true) && !mReadOnly)
226  {
227  mPath = filename.toStdString();
229  window()->setWindowTitle(filename);
230  mGateLibrary->set_path(std::filesystem::path(mPath));
231  QString glName = filename.mid(filename.lastIndexOf('/')+1); //truncate path until first '/'
232  std::string gatelibName = glName.left(glName.length()-4).toStdString(); //remove file extension to get the name
233  if(mPath != oldPath)
234  mGateLibrary->set_name(gatelibName);
235  }
236  std::string p = mGateLibrary->get_path().generic_string();
237  mCreationMode = false;
238  }
239 
240  void GatelibraryContentWidget::handleUnsavedChanges()
241  {
242  if(!(window()->windowTitle().right(1) == "*"))
243  {
244  mTitle = window()->windowTitle();
245  window()->setWindowTitle(mTitle + " *");
246  }
247  }
248 
249  void GatelibraryContentWidget::toggleSearchbar()
250  {
251  if (!mSearchAction->isEnabled())
252  return;
253 
254  if (mSearchbar->isHidden())
255  {
256  mSearchbar->show();
257  mSearchbar->setFocus();
258  }
259  else
260  {
261  mSearchbar->hide();
262  setFocus();
263  }
264  }
265 
267  {
268  QStyle* s = style();
269 
270  s->unpolish(this);
271  s->polish(this);
272 
273  mReadOnly = readOnly;
274  toggleReadOnlyMode(readOnly);
275 
276  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mEnabledIconStyle,mSearchIconPath));
277 
278  }
279 
281  {
282  mReadOnly = readOnly;
283  mDeleteAction->setEnabled(!readOnly);
284  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle, mDeleteIconPath));
285  mEditAction->setEnabled(!readOnly);
286  mEditAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mEditTypeIconPath));
287 
288  mAddAction->setEnabled(!readOnly);
289  mAddAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mAddTypeIconPath));
290 
291  }
292 
294  {
295  mDeleteAction->setEnabled(selected);
296  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle, mDeleteIconPath));
297  mEditAction->setEnabled(selected);
298  mEditAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle,mEditTypeIconPath));
299 
300  }
301 
303  {
304  return mDisabledIconStyle;
305  }
306 
308  {
309  return mEnabledIconStyle;
310  }
311 
313  {
314  return mAddTypeIconPath;
315  }
316 
318  {
319  return mEditTypeIconPath;
320  }
321 
323  {
324  return mSearchIconPath;
325  }
326 
328  {
329  return mSearchIconStyle;
330  }
331 
333  {
334  return mSearchActiveIconStyle;
335  }
336 
338  {
339  return mDeleteIconPath;
340  }
341 
343  {
344  return mDeleteIconStyle;
345  }
346 
348  {
349  mDisabledIconStyle = s;
350  }
351 
353  {
354  mEnabledIconStyle = s;
355  }
356 
358  {
359  mAddTypeIconPath = s;
360  }
361 
363  {
364  mEditTypeIconPath = s;
365  }
366 
368  {
369  mSearchIconPath = s;
370  }
371 
373  {
374  mSearchIconStyle = s;
375  }
376 
378  {
379  mSearchActiveIconStyle = s;
380  }
381 
383  {
384  mDeleteIconPath = s;
385  }
386 
388  {
389  mDeleteIconStyle = s;
390  }
391 }
static GateLibrary * loadGateLibrary(const QString &path)
std::filesystem::path get_path() const
void set_path(const std::filesystem::path &modified_path)
void set_name(const std::string &modified_name)
void setGateLibraryPath(std::filesystem::path p)
void triggerEditType(QModelIndex index)
void handleContextMenuRequested(const QPoint &pos)
void triggerCurrentSelectionChanged(QModelIndex index, QModelIndex prevIndex)
void triggerDoubleClicked(QModelIndex index)
GatelibraryContentWidget(GatelibraryTableModel *model, QWidget *parent=nullptr)
void triggerDeleteType(QModelIndex index)
A model to display loaded gatelibraries.
void loadFeature(FacExtensionInterface::Feature ft, const QString &extension=QString())
const GateLibrary * get_gate_library() const
Definition: netlist.cpp:134
A proxy model to filter the ContextTableModel by a given string.
GuiPluginTable * mGuiPluginTable
Definition: plugin_relay.h:68
virtual void startSearch(QString text, int options)=0
A QFrame with a QLineEdit that can be used to input a substring to search for.
Definition: searchbar.h:48
void setColumnNames(QList< QString > list)
Definition: searchbar.cpp:130
void triggerNewSearch(const QString &text, int searchOptions)
Toolbar for all ContentFrames and ContentWidgets.
Definition: toolbar.h:39
bool save(std::filesystem::path file_path, GateLibrary *gate_lib, bool overwrite=false)
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
PluginRelay * gPluginRelay
Definition: plugin_gui.cpp:83
FileStatusManager * gFileStatusManager
Definition: plugin_gui.cpp:85
Netlist * gNetlist
Definition: gui_globals.h:69
QModelIndex currentIndex() const const
void doubleClicked(const QModelIndex &index)
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
void setSelectionMode(QAbstractItemView::SelectionMode mode)
QItemSelectionModel * selectionModel() const const
bool isEnabled() const const
void setIcon(const QIcon &icon)
void triggered(bool checked)
virtual int exec()
QString currentPath()
QDir home()
virtual bool open(QIODevice::OpenMode mode) override
void setAcceptMode(QFileDialog::AcceptMode mode)
void setDefaultSuffix(const QString &suffix)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QStringList selectedFiles() const const
void setLineWidth(int)
void setFrameStyle(int style)
void setSectionResizeMode(QHeaderView::ResizeMode mode)
QByteArray readAll()
void currentChanged(const QModelIndex &current, const QModelIndex &previous)
void addWidget(QWidget *w)
T & front()
QAction * addAction(const QString &text)
QAction * exec()
bool isValid() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QString fromStdString(const std::string &str)
QString fromUtf8(const char *str, int size)
bool isEmpty() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString left(int n) const const
int length() const const
QString mid(int position, int n) const const
std::string toStdString() const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
CustomContextMenu
AscendingOrder
void sortByColumn(int column)
QHeaderView * horizontalHeader() const const
virtual QModelIndex indexAt(const QPoint &pos) const const override
virtual void setModel(QAbstractItemModel *model) override
void setSortingEnabled(bool enable)
QHeaderView * verticalHeader() const const
QAction * addAction(const QString &text)
void setIconSize(const QSize &iconSize)
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void customContextMenuRequested(const QPoint &pos)
void hide()
bool isHidden() const const
QLayout * layout() const const
QPoint mapToGlobal(const QPoint &pos) const const
void move(int x, int y)
void setFocus()
void show()
void setSizePolicy(QSizePolicy)
QStyle * style() const const
QWidget * window() const const
void setWindowTitle(const QString &)