HAL
gatelibrary_manager.cpp
Go to the documentation of this file.
2 
9 
10 #include <QGridLayout>
11 #include <QVBoxLayout>
12 #include <QHBoxLayout>
13 #include <QGraphicsView>
14 #include <QTabWidget>
15 #include <QTableView>
16 
17 #include <QDebug>
18 #include <QDir>
19 #include <QFileDialog>
20 #include <QHeaderView>
21 #include <QLabel>
22 #include <QPushButton>
23 #include <QTableWidget>
24 #include <QSplitter>
25 #include <QDialogButtonBox>
26 #include <gui/gui_globals.h>
27 
28 namespace hal
29 {
31  : QFrame(parent), mFrameWidth(0), mLayout(new QGridLayout())
32  {
33  mSplitter = new QSplitter(this);
34  QWidget* rightWindow = new QWidget(mSplitter);
35  QGridLayout* rlay = new QGridLayout(rightWindow);
37 
38  mTableModel = new GatelibraryTableModel(this);
39  mContentWidget = new GatelibraryContentWidget(mTableModel,mSplitter);
40 
41  //pages for the tab widget
42  mGeneralTab = new GateLibraryTabGeneral(this);
43  mPinTab = new GateLibraryTabPin(false,this);
44  mBooleanFunctionTab = new GateLibraryTabTruthTable(this);
45 
46  //buttons
47  mOkBtn = bbox->button(QDialogButtonBox::Ok);
48  mCancelBtn = bbox->button(QDialogButtonBox::Cancel);
49  mCancelBtn->setEnabled(true);
50 
51  //adding pages to the tab widget
52  mTabWidget = new QTabWidget(this);
53  mTabWidget->addTab(mGeneralTab, "Gate Type");
54  mTabWidget->addTab(mPinTab, "Pins");
55  mTabWidget->addTab(mBooleanFunctionTab, "Truth Table"); // TODO : implement truth table
56 
57  mGraphicsView = new GatelibraryGraphicsView(this);
58  QGraphicsScene* sc = new QGraphicsScene(mGraphicsView);
59  sc->setSceneRect(0,0,300,1200);
60  mGraphicsView->setScene(sc);
61 
62  rlay->addWidget(mTabWidget,0,0);
63  rlay->addWidget(mGraphicsView,0,1);
64  rlay->addWidget(bbox,1,0,1,2);
65 
66  // Add widgets to the layout
67  mSplitter->addWidget(mContentWidget);
68  mSplitter->addWidget(rightWindow);
69 
70  mLayout->addWidget(mSplitter);
71 
75  connect(mContentWidget->mAddAction, &QAction::triggered, this, &GateLibraryManager::handleAddWizard);
79 
80  setLayout(mLayout);
81  repolish(); // CALL FROM PARENT
82  }
83 
85  {
86  QStyle* s = style();
87 
88  s->unpolish(this);
89  s->polish(this);
90  }
91 
93  {
94  mCreationMode = true; //needed for callUnsavedChangesWindow
95  mContentWidget->mCreationMode = mCreationMode;
96  std::filesystem::path path;
97  QFile gldpath(":/path/gate_library_definitions");
98  if (gldpath.open(QIODevice::ReadOnly))
99  {
100  path = QString::fromUtf8(gldpath.readAll()).toStdString();
101  QStringList allNames = QDir(QString::fromStdString(path.string())).entryList();
102  if(allNames.contains("unnamed_gate_library.hgl"))
103  {
104  u32 cnt = 1;
105  while(allNames.contains(QString("unnamed_gate_library(%1).hgl").arg(cnt)))
106  cnt++;
107  path += std::filesystem::path(QString("/unnamed_gate_library(%1).hgl").arg(cnt).toStdString());
108  }
109  else path += "/unnamed_gate_library.hgl";
110  }
111 
113  {
114  if(!callUnsavedChangesWindow()) return;
115  else window()->setWindowTitle(QString("GateLibrary %1").arg(QDir::home().relativeFilePath(QString::fromStdString(path.string()))));
116  }
117  mContentWidget->mGateLibrary = new GateLibrary(path, path.string());
118  initialize(mContentWidget->mGateLibrary);
119  mCreationMode = false;
120  }
121 
123  {
124  mContentWidget->handleSaveAction();
125  }
126 
128  {
129  mContentWidget->handleSaveAsAction();
130  }
131 
132  bool GateLibraryManager::initialize(GateLibrary* gateLibrary, bool readOnly)
133  {
134  if (!mContentWidget->initialize(gateLibrary,readOnly))
135  return false;
136 
137  if (gateLibrary) mProjectName = window()->windowTitle();
138 
139  mDemoNetlist = netlist_factory::create_netlist(mContentWidget->mGateLibrary);
140  mGraphicsView->showGate(nullptr);
141  updateTabs(nullptr);
142  mTableModel->loadFile(mContentWidget->mGateLibrary);
143  mContentWidget->activate(mContentWidget->mReadOnly);
144 
145  mContentWidget->toggleSelection(false);
146  if (!mContentWidget->mReadOnly) gFileStatusManager->gatelibOpened();
147  return true;
148  }
149 
151  {
152  if(mContentWidget->mReadOnly)
153  return;
154  GateType* gtEditable = mTableModel->getGateTypeAtIndex(index.row());
155  mWizard = new GateLibraryWizard(mContentWidget->mGateLibrary, gtEditable);
156  connect(mWizard, &GateLibraryWizard::triggerUnsavedChanges, mContentWidget, &GatelibraryContentWidget::handleUnsavedChanges);
157 
158  mWizard->exec();
159  initialize(mContentWidget->mGateLibrary);
160 
161  for (int r=0; r<mTableModel->rowCount(); r++) {
162  if(mTableModel->getGateTypeAtIndex(r) == gtEditable)
163  mContentWidget->mTableView->selectRow(r);
164  }
165  }
166 
168  {
169  mWizard = new GateLibraryWizard(mContentWidget->mGateLibrary);
170  connect(mWizard, &GateLibraryWizard::triggerUnsavedChanges, mContentWidget, &GatelibraryContentWidget::handleUnsavedChanges);
171 
172  mWizard->exec();
173  initialize(mContentWidget->mGateLibrary);
174 
175  for (int r=0; r<mTableModel->rowCount(); r++) {
176  if(mTableModel->getGateTypeAtIndex(r) == mWizard->getRecentCreatedGate())
177  mContentWidget->mTableView->selectRow(r);
178  }
179  }
180 
182  {
183  GateType* gate = mTableModel->getGateTypeAtIndex(index.row());
184  mContentWidget->mGateLibrary->remove_gate_type(gate->get_name());
185  initialize(mContentWidget->mGateLibrary);
187  //qInfo() << "handleDeleteType " << QString::fromStdString(gate->get_name()) << ":" << gate->get_id();
188  }
189 
191  {
192  QSet<u32>* occupiedIds = new QSet<u32>;
193  u32 freeId = 1;
194  for (auto gt : mContentWidget->mGateLibrary->get_gate_types()) {
195  occupiedIds->insert(gt.second->get_id());
196  }
197  while(occupiedIds->contains(freeId))
198  {
199  freeId++;
200  }
201  return freeId;
202  }
203 
205  {
206  Q_UNUSED(prevIndex);
207  GateType* gateType;
208  //get selected gate
209  gateType = mTableModel->getGateTypeAtIndex(index.row());
210  //qInfo() << "selected " << QString::fromStdString(gateType->get_name());
211 
212  if(!mContentWidget->mReadOnly) mContentWidget->toggleSelection(true);
213  //update tabs
214  updateTabs(gateType);
215  if (mDemoNetlist)
216  {
217  Gate* g = mDemoNetlist->get_gate_by_id(1);
218  if (g) mDemoNetlist->delete_gate(g);
219  g = mDemoNetlist.get()->create_gate(1,gateType,"Instance of");
220  mGraphicsView->showGate(g);
221  }
222  }
223 
224  void GateLibraryManager::emitClose()
225  {
227  Q_EMIT close();
228  }
229 
230 
232  {
234  {
235  if(!mContentWidget->mReadOnly) window()->setWindowTitle("HAL");
236  else window()->setWindowTitle(mProjectName);
237  emitClose();
238  }
239  else
240  {
242  }
243  }
244 
246  {
248  mContentWidget->handleSaveAction();
249  emitClose();
250  }
251 
252  GateType* GateLibraryManager::getSelectedGate()
253  {
254  PinProxyModel* proxyModel = mContentWidget->mPinProxyModel;
255  QModelIndex index = mContentWidget->mTableView->currentIndex();
256  QModelIndex sourceIndex = proxyModel->mapToSource(index);
257 
258  return mTableModel->getGateTypeAtIndex(sourceIndex.row());
259  }
260 
262  {
263  mGeneralTab->update(gateType);
264  mBooleanFunctionTab->update(gateType);
265  mPinTab->update(gateType);
266  }
267 
269  {
270  return mContentWidget->mReadOnly;
271  }
272 
274  {
275  QMessageBox* msgBox = new QMessageBox(this);
276  msgBox->setWindowTitle("Unsaved changes");
277  msgBox->setInformativeText("The current gate library has been modified. Do you want to save your changes or discard them?");
279 
280  int r = msgBox->exec();
281  switch(r)
282  {
283  case QMessageBox::Save:
284  mContentWidget->handleSaveAsAction();
285  emitClose();
286  break;
288  if(!mCreationMode)
289  {
290  gate_library_manager::remove(std::filesystem::path(mContentWidget->mGateLibrary->get_path()));
291  mDemoNetlist.reset(); //delete unique pointer
293  if(!mContentWidget->mReadOnly) window()->setWindowTitle("HAL");
294  else window()->setWindowTitle(mProjectName);
295  emitClose();
296  }
297  else
298  {
299  gate_library_manager::remove(std::filesystem::path(mContentWidget->mGateLibrary->get_path()));
300  mDemoNetlist.reset(); //delete unique pointer
302  }
303  break;
304  case QMessageBox::Cancel:
305  msgBox->reject();
306  return false;
307  }
308  return true;
309  }
310 
312  {
313  QFrame::resizeEvent(evt);
314  if (evt->size().width() != mFrameWidth)
315  {
316  mFrameWidth = evt->size().width();
317  QList<int> splitSizes;
318  // divide available size in percent
319  splitSizes << mFrameWidth * 27 / 100 << mFrameWidth * 73 / 100;
320  mSplitter->setSizes(splitSizes);
321  }
322  }
323 }
Definition: gate.h:58
std::filesystem::path get_path() const
void remove_gate_type(const std::string &name)
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
bool initialize(GateLibrary *gateLibrary=nullptr, bool readOnly=false)
void updateTabs(GateType *gateType)
void handleEditWizard(const QModelIndex &index)
void handleDeleteType(QModelIndex index)
void handleSelectionChanged(const QModelIndex &index, const QModelIndex &prevIndex)
GateLibraryManager(MainWindow *parent)
void resizeEvent(QResizeEvent *evt) override
void update(GateType *gt) override
void update(GateType *gate) override
const std::string & get_name() const
Definition: gate_type.cpp:64
void triggerEditType(QModelIndex index)
void triggerCurrentSelectionChanged(QModelIndex index, QModelIndex prevIndex)
void triggerDoubleClicked(QModelIndex index)
void triggerDeleteType(QModelIndex index)
A model to display loaded gatelibraries.
void loadFile(const GateLibrary *g)
GateType * getGateTypeAtIndex(int index)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
The top level widget.
Definition: main_window.h:65
A proxy model to filter the ContextTableModel by a given string.
void remove(std::filesystem::path file_path)
std::unique_ptr< Netlist > create_netlist(const GateLibrary *gate_library)
Create a new empty netlist using the specified gate library.
FileStatusManager * gFileStatusManager
Definition: plugin_gui.cpp:84
quint32 u32
This file contains various functions to create and load netlists.
void clicked(bool checked)
QModelIndex currentIndex() const const
void triggered(bool checked)
virtual int exec()
virtual void reject()
QPushButton * button(QDialogButtonBox::StandardButton which) const const
QStringList entryList(QDir::Filters filters, QDir::SortFlags sort) const const
QDir home()
virtual bool open(QIODevice::OpenMode mode) override
void setSceneRect(const QRectF &rect)
void setScene(QGraphicsScene *scene)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
QByteArray readAll()
virtual int exec() override
void setInformativeText(const QString &text)
void setWindowTitle(const QString &title)
void setStandardButtons(QMessageBox::StandardButtons buttons)
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
const QSize & size() const const
bool contains(const T &value) const const
QSet::iterator insert(const T &value)
int width() const const
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
void addWidget(QWidget *widget)
void setSizes(const QList< int > &list)
QString fromStdString(const std::string &str)
QString fromUtf8(const char *str, int size)
std::string toStdString() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
Horizontal
void selectRow(int row)
int addTab(QWidget *page, const QString &label)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setEnabled(bool)
virtual void resizeEvent(QResizeEvent *event)
void setLayout(QLayout *layout)
QStyle * style() const const
QWidget * window() const const
void setWindowTitle(const QString &)