HAL
gatelibrary_content_widget.cpp
Go to the documentation of this file.
2 #include "gui/gui_globals.h"
5 #include "gui/toolbar/toolbar.h"
9 
10 #include <QHeaderView>
11 #include <QVBoxLayout>
12 #include <QMenu>
13 
14 namespace hal
15 {
16 
18  {
19  QVBoxLayout* layout = new QVBoxLayout(this);
21  setLineWidth(2);
23  mSearchbar = new Searchbar(this);
24 
25  mTableView = new QTableView(this);
26  mPinProxyModel = new PinProxyModel(this);
27  mPinProxyModel->setSourceModel(model);
28  mTableView->setModel(mPinProxyModel);
33  // mTableView->horizontalHeader()->setStretchLastSection(true);
34  mTableView->verticalHeader()->hide();
35 
38 
39  mTableView->setSortingEnabled(true);
40  mTableView->sortByColumn(0, Qt::AscendingOrder);
41 
42 
43  mToolbar = new Toolbar;
45  mToolbar->setIconSize(QSize(18, 18));
46 
47  mAddAction = new QAction("Add gate type", this);
48  mEditAction = new QAction("Edit gate type", this);
49  mDeleteAction = new QAction("Delete gate type", this);
50  mSearchAction = new QAction("Search", this);
51 
52  //connections
54  connect(mTableView, &QTableView::doubleClicked, this, &GatelibraryContentWidget::handleDoubleClicked);
55  connect(mTableView->selectionModel(), &QItemSelectionModel::currentChanged, this, &GatelibraryContentWidget::handleCurrentSelectionChanged);
56  connect(mSearchAction, &QAction::triggered, this, &GatelibraryContentWidget::toggleSearchbar);
57  connect(mEditAction, &QAction::triggered, this, &GatelibraryContentWidget::handleEditAction);
58  connect(mDeleteAction, &QAction::triggered, this, &GatelibraryContentWidget::handleDeleteAction);
59 
60  connect(mDeleteAction, &QAction::triggered, this, &GatelibraryContentWidget::handleUnsavedChanges);
61 
63 
64  mToolbar->addAction(mAddAction);
65  mToolbar->addAction(mEditAction);
66  mToolbar->addAction(mDeleteAction);
67  mToolbar->addAction(mSearchAction);
68 
69 
70  layout->addWidget(mToolbar);
71  layout->addWidget(mTableView);
72  layout->addWidget(mSearchbar);
73 
74  mSearchbar->hide();
75  mSearchbar->setColumnNames({"Name", "ID"});
76  }
77 
78  bool GatelibraryContentWidget::initialize(GateLibrary* gateLibrary, bool readOnly)
79  {
80  if(!gateLibrary)
81  {
83  // readonly flag makes sure mGateLibrary does not get modified, const attribute not needed
84  mGateLibrary = const_cast<GateLibrary*>(gNetlist->get_gate_library());
85  mReadOnly = true;
86  }
87  else
88  {
89  QString title = "Load gate library";
90  QString text = "HAL Gate Library (*.hgl *.lib)";
91  QString path = QDir::currentPath();
92  QFile gldpath(":/path/gate_library_definitions");
93  if (gldpath.open(QIODevice::ReadOnly))
94  path = QString::fromUtf8(gldpath.readAll());
95 
96  //TODO fileDialog throws bad window error
97  QString fileName = QFileDialog::getOpenFileName(nullptr, title, path, text, nullptr);
98  if (fileName == nullptr)
99  return false;
100 
103 
104  auto gateLibrary = gate_library_manager::load(std::filesystem::path(fileName.toStdString()));
105 
106  if(gateLibrary)
107  mGateLibrary = gateLibrary;
108  else
109  {
110  //extract gatelibrary name from path
111  mGateLibrary = new GateLibrary(std::filesystem::path(fileName.toStdString()), QFileInfo(fileName).baseName().toStdString());
112  }
113 
114  mReadOnly = false;
115  }
116 
117  }
118  else
119  {
120  mReadOnly = readOnly;
121  mGateLibrary = gateLibrary;
122  }
123  QDir dir(QDir::home());
125  QString("GateLibrary %1").arg(QDir::home().relativeFilePath(QString::fromStdString(mGateLibrary->get_path().string()))));
126  mPath = mGateLibrary->get_path().generic_string();
127  return true;
128  }
129 
131  {
132  QModelIndex idx = mTableView->indexAt(pos);
133 
134  if(!idx.isValid())
135  return;
136 
137  QMenu menu;
138 
139  menu.addAction(mEditAction);
140  menu.addAction(mDeleteAction);
141 
142  menu.move(mapToGlobal(pos));
143  menu.exec();
144  }
145 
147  {
148  mGateLibrary = gl;
149  }
150 
151  void GatelibraryContentWidget::setGateLibraryPath(std::filesystem::path p)
152  {
153  mPath = p;
154  }
155 
156  void GatelibraryContentWidget::handleEditAction()
157  {
158  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
159  if (inx.isValid())
160  Q_EMIT triggerEditType(inx);
161  }
162 
163  void GatelibraryContentWidget::handleDeleteAction()
164  {
165  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
166  if (inx.isValid())
168  }
169 
170  void GatelibraryContentWidget::handleCurrentSelectionChanged(QModelIndex prevIndex){
171  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
172  if (inx.isValid())
173  Q_EMIT triggerCurrentSelectionChanged(inx, prevIndex);
174  }
175 
176  void GatelibraryContentWidget::handleDoubleClicked(QModelIndex index)
177  {
178  QModelIndex inx = mPinProxyModel->mapToSource(index);
179  if (inx.isValid())
181  }
182 
184  {
185  if(mCreationMode)
187  else
188  {
191  if(gate_library_manager::save(mPath,mGateLibrary,true))
192  {
194  window()->setWindowTitle(mTitle);
195  }
196  }
197  mCreationMode = false;
198  }
199 
201  {
202  std::filesystem::path oldPath = mGateLibrary->get_path();
203  QString path = QDir::currentPath();
204  QFile gldpath(":/path/gate_library_definitions");
205  if (gldpath.open(QIODevice::ReadOnly))
206  path = QString::fromUtf8(gldpath.readAll());
207 
210 
211  QString filename = "";
212  QFileDialog fd(this, "Save as", path, "HGL *.hgl");
213  fd.setDefaultSuffix(".hgl");
215  if (fd.exec()) {
216  filename = fd.selectedFiles().front();
217  }
218 
219  // order of conditions important:
220  // 1. Filename for save as provided
221  // 2. Copy was saved under new filename
222  // 3. We ar *not* in readOnly Mode
223  // => continue to work with copy
224  if (!filename.isEmpty() && gate_library_manager::save(filename.toStdString(),mGateLibrary,true) && !mReadOnly)
225  {
226  mPath = filename.toStdString();
228  window()->setWindowTitle(filename);
229  mGateLibrary->set_path(std::filesystem::path(mPath));
230  QString glName = filename.mid(filename.lastIndexOf('/')+1); //truncate path until first '/'
231  std::string gatelibName = glName.left(glName.length()-4).toStdString(); //remove file extension to get the name
232  if(mPath != oldPath)
233  mGateLibrary->set_name(gatelibName);
234  }
235  std::string p = mGateLibrary->get_path().generic_string();
236  mCreationMode = false;
237  }
238 
239  void GatelibraryContentWidget::handleUnsavedChanges()
240  {
241  if(!(window()->windowTitle().right(1) == "*"))
242  {
243  mTitle = window()->windowTitle();
244  window()->setWindowTitle(mTitle + " *");
245  }
246  }
247 
248  void GatelibraryContentWidget::toggleSearchbar()
249  {
250  if (!mSearchAction->isEnabled())
251  return;
252 
253  if (mSearchbar->isHidden())
254  {
255  mSearchbar->show();
256  mSearchbar->setFocus();
257  }
258  else
259  {
260  mSearchbar->hide();
261  setFocus();
262  }
263  }
264 
266  {
267  QStyle* s = style();
268 
269  s->unpolish(this);
270  s->polish(this);
271 
272  mReadOnly = readOnly;
273  toggleReadOnlyMode(readOnly);
274 
275  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mEnabledIconStyle,mSearchIconPath));
276 
277  }
278 
280  {
281  mReadOnly = readOnly;
282  mDeleteAction->setEnabled(!readOnly);
283  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle, mDeleteIconPath));
284  mEditAction->setEnabled(!readOnly);
285  mEditAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mEditTypeIconPath));
286 
287  mAddAction->setEnabled(!readOnly);
288  mAddAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mAddTypeIconPath));
289 
290  }
291 
293  {
294  mDeleteAction->setEnabled(selected);
295  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle, mDeleteIconPath));
296  mEditAction->setEnabled(selected);
297  mEditAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle,mEditTypeIconPath));
298 
299  }
300 
302  {
303  return mDisabledIconStyle;
304  }
305 
307  {
308  return mEnabledIconStyle;
309  }
310 
312  {
313  return mAddTypeIconPath;
314  }
315 
317  {
318  return mEditTypeIconPath;
319  }
320 
322  {
323  return mSearchIconPath;
324  }
325 
327  {
328  return mSearchIconStyle;
329  }
330 
332  {
333  return mSearchActiveIconStyle;
334  }
335 
337  {
338  return mDeleteIconPath;
339  }
340 
342  {
343  return mDeleteIconStyle;
344  }
345 
347  {
348  mDisabledIconStyle = s;
349  }
350 
352  {
353  mEnabledIconStyle = s;
354  }
355 
357  {
358  mAddTypeIconPath = s;
359  }
360 
362  {
363  mEditTypeIconPath = s;
364  }
365 
367  {
368  mSearchIconPath = s;
369  }
370 
372  {
373  mSearchIconStyle = s;
374  }
375 
377  {
378  mSearchActiveIconStyle = s;
379  }
380 
382  {
383  mDeleteIconPath = s;
384  }
385 
387  {
388  mDeleteIconStyle = s;
389  }
390 }
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:132
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
GateLibrary * load(std::filesystem::path file_path, bool reload)
bool save(std::filesystem::path file_path, GateLibrary *gate_lib, bool overwrite)
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
PluginRelay * gPluginRelay
Definition: plugin_gui.cpp:82
FileStatusManager * gFileStatusManager
Definition: plugin_gui.cpp:84
Netlist * gNetlist
Definition: plugin_gui.cpp:80
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 &)