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 
79  {
80  QModelIndex idx = mTableView->indexAt(pos);
81 
82  if(!idx.isValid())
83  return;
84 
85  QMenu menu;
86 
87  menu.addAction(mEditAction);
88  menu.addAction(mDeleteAction);
89 
90  menu.move(mapToGlobal(pos));
91  menu.exec();
92  }
93 
95  {
96  mGateLibrary = gl;
97  }
98 
99  void GatelibraryContentWidget::setGateLibraryPath(std::filesystem::path p)
100  {
101  mPath = p;
102  }
103 
104  void GatelibraryContentWidget::handleEditAction()
105  {
106  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
107  if (inx.isValid())
108  Q_EMIT triggerEditType(inx);
109  }
110 
111  void GatelibraryContentWidget::handleDeleteAction()
112  {
113  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
114  if (inx.isValid())
116  }
117 
118  void GatelibraryContentWidget::handleCurrentSelectionChanged(QModelIndex prevIndex){
119  QModelIndex inx = mPinProxyModel->mapToSource(mTableView->currentIndex());
120  if (inx.isValid())
121  Q_EMIT triggerCurrentSelectionChanged(inx, prevIndex);
122  }
123 
124  void GatelibraryContentWidget::handleDoubleClicked(QModelIndex index)
125  {
126  QModelIndex inx = mPinProxyModel->mapToSource(index);
127  if (inx.isValid())
129  }
130 
132  {
133  if(mCreationMode)
135  else
136  {
139  if(gate_library_manager::save(mPath,mGateLibrary,true))
140  {
142  window()->setWindowTitle(mTitle);
143  }
144  }
145  mCreationMode = false;
146  }
147 
149  {
150  std::filesystem::path oldPath = mGateLibrary->get_path();
151  QString path = QDir::currentPath();
152  QFile gldpath(":/path/gate_library_definitions");
153  if (gldpath.open(QIODevice::ReadOnly))
154  path = QString::fromUtf8(gldpath.readAll());
155 
158 
159  QString filename = "";
160  QFileDialog fd(this, "Save as", path, "HGL *.hgl");
161  fd.setDefaultSuffix(".hgl");
163  if (fd.exec()) {
164  filename = fd.selectedFiles().front();
165  }
166  if (!filename.isEmpty() && gate_library_manager::save(filename.toStdString(),mGateLibrary,true))
167  {
168  mPath = filename.toStdString();
170  window()->setWindowTitle(filename);
171  mGateLibrary->set_path(std::filesystem::path(mPath));
172  QString glName = filename.mid(filename.lastIndexOf('/')+1); //truncate path until first '/'
173  std::string gatelibName = glName.left(glName.length()-4).toStdString(); //remove file extension to get the name
174  if(mPath != oldPath)
175  mGateLibrary->set_name(gatelibName);
176  }
177  std::string p = mGateLibrary->get_path().generic_string();
178  mCreationMode = false;
179  }
180 
181  void GatelibraryContentWidget::handleUnsavedChanges()
182  {
183  if(!(window()->windowTitle().right(1) == "*"))
184  {
185  mTitle = window()->windowTitle();
186  window()->setWindowTitle(mTitle + " *");
187  }
188  }
189 
190  void GatelibraryContentWidget::toggleSearchbar()
191  {
192  if (!mSearchAction->isEnabled())
193  return;
194 
195  if (mSearchbar->isHidden())
196  {
197  mSearchbar->show();
198  mSearchbar->setFocus();
199  }
200  else
201  {
202  mSearchbar->hide();
203  setFocus();
204  }
205  }
206 
208  {
209  QStyle* s = style();
210 
211  s->unpolish(this);
212  s->polish(this);
213 
214  mReadOnly = readOnly;
215  toggleReadOnlyMode(readOnly);
216 
217  mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mEnabledIconStyle,mSearchIconPath));
218 
219  }
220 
222  {
223  mReadOnly = readOnly;
224  mDeleteAction->setEnabled(!readOnly);
225  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle, mDeleteIconPath));
226  mEditAction->setEnabled(!readOnly);
227  mEditAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mEditTypeIconPath));
228 
229  mAddAction->setEnabled(!readOnly);
230  mAddAction->setIcon(gui_utility::getStyledSvgIcon(readOnly ? mDisabledIconStyle : mEnabledIconStyle,mAddTypeIconPath));
231 
232  }
233 
235  {
236  mDeleteAction->setEnabled(selected);
237  mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle, mDeleteIconPath));
238  mEditAction->setEnabled(selected);
239  mEditAction->setIcon(gui_utility::getStyledSvgIcon(selected ? mEnabledIconStyle : mDisabledIconStyle,mEditTypeIconPath));
240 
241  }
242 
244  {
245  return mDisabledIconStyle;
246  }
247 
249  {
250  return mEnabledIconStyle;
251  }
252 
254  {
255  return mAddTypeIconPath;
256  }
257 
259  {
260  return mEditTypeIconPath;
261  }
262 
264  {
265  return mSearchIconPath;
266  }
267 
269  {
270  return mSearchIconStyle;
271  }
272 
274  {
275  return mSearchActiveIconStyle;
276  }
277 
279  {
280  return mDeleteIconPath;
281  }
282 
284  {
285  return mDeleteIconStyle;
286  }
287 
289  {
290  mDisabledIconStyle = s;
291  }
292 
294  {
295  mEnabledIconStyle = s;
296  }
297 
299  {
300  mAddTypeIconPath = s;
301  }
302 
304  {
305  mEditTypeIconPath = s;
306  }
307 
309  {
310  mSearchIconPath = s;
311  }
312 
314  {
315  mSearchIconStyle = s;
316  }
317 
319  {
320  mSearchActiveIconStyle = s;
321  }
322 
324  {
325  mDeleteIconPath = s;
326  }
327 
329  {
330  mDeleteIconStyle = s;
331  }
332 }
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())
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)
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
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()
virtual bool open(QIODevice::OpenMode mode) override
void setAcceptMode(QFileDialog::AcceptMode mode)
void setDefaultSuffix(const QString &suffix)
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 &)