HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
gatelibrary_selection.cpp
Go to the documentation of this file.
6 #include <filesystem>
7 #include <QFileInfo>
8 #include <QLabel>
9 #include <QVBoxLayout>
10 #include <QHBoxLayout>
11 #include <QComboBox>
12 #include <QPushButton>
13 #include <QCheckBox>
14 #include <QFileDialog>
15 
16 namespace hal {
18  : QFrame(parent)
19  {
20  QStyle* s = style();
21  s->unpolish(this);
22  s->polish(this);
23 
24  QVBoxLayout* vlayout = new QVBoxLayout(this);
25  QLabel* labGatelib = new QLabel("Gate library:", this);
26  vlayout->addWidget(labGatelib);
27 
28 
29  QHBoxLayout* hlayout = new QHBoxLayout;
32 
33  mComboGatelib = new QComboBox(this);
34  GateLibrarySelectionTable* glTable = new GateLibrarySelectionTable(defaultGl.isEmpty(),this);
35  mComboGatelib->setModel(glTable);
36 
38 
39  hlayout->addWidget(mComboGatelib);
40 
41  mWarningMsg = new QLabel(this);
42  mWarningMsg->setObjectName("warningMsg");
43  if (!defaultGl.isEmpty())
44  {
45  int inx = glTable->getIndexByPath(defaultGl);
46  if (inx < 0)
47  {
48  mWarningMsg->setText("Gate library '" + defaultGl + "' not found,\nplease select gate library from list.");
49  }
50  else
51  {
52  mComboGatelib->setCurrentIndex(inx);
53  if (glTable->isWarnSubstitute())
54  mWarningMsg->setText("Gate library '" + defaultGl + "' not found,\na substitute has been suggested.");
55  }
56  }
57 
58 
59  mInvokeFileDialog = new QPushButton(gui_utility::getStyledSvgIcon(mSaveIconStyle, mSaveIconPath),"",this);
60  connect(mInvokeFileDialog, &QPushButton::clicked, this, &GateLibrarySelection::handleInvokeFileDialog);
61  hlayout->addWidget(mInvokeFileDialog);
62 
63  vlayout->addLayout(hlayout);
64  vlayout->addWidget(mWarningMsg);
65  if (mWarningMsg->text().isEmpty())
66  mWarningMsg->hide();
67 
68  mCheckFullPath = new QCheckBox("Show full path");
69  mCheckFullPath->setChecked(false);
70  connect(mCheckFullPath,&QCheckBox::toggled,this,&GateLibrarySelection::handleShowFullPath);
71  vlayout->addWidget(mCheckFullPath);
72  connect(mComboGatelib,QOverload<int>::of(&QComboBox::currentIndexChanged),this,&GateLibrarySelection::handleGatelibIndexChanged);
73  }
74 
75  void GateLibrarySelection::handleGatelibIndexChanged(int inx)
76  {
77  Q_UNUSED(inx);
78  mWarningMsg->clear();
79  mWarningMsg->hide();
80  }
81 
82  void GateLibrarySelection::handleInvokeFileDialog()
83  {
84  QString glFilename = QFileDialog::getOpenFileName(this, "Select Gate Library", QDir::currentPath(), "HGL Files (*.hgl);;Lib Files (*.lib)");
85  if (glFilename.isEmpty()) return;
86  int inx = static_cast<GateLibrarySelectionTable*>(mComboGatelib->model())->addGateLibrary(glFilename);
87  mComboGatelib->setCurrentIndex(inx);
88  }
89 
91  {
92  const GateLibrarySelectionTable* glst = static_cast<const GateLibrarySelectionTable*>(mComboGatelib->model());
93  return glst->gateLibraryPath(mComboGatelib->currentIndex());
94  }
95 
96  void GateLibrarySelection::handleShowFullPath(bool checked)
97  {
98  int inx = mComboGatelib->currentIndex();
99  static_cast<GateLibrarySelectionTable*>(mComboGatelib->model())->handleShowFullPath(checked);
100  mComboGatelib->setCurrentIndex(inx);
101  }
102 
103  //-----------------------------------------------
104 
105  QVariant GateLibrarySelectionEntry::data(int column, bool fullPath) const
106  {
107  if (fullPath)
108  {
109  if (mCount<0)
110  return mName;
111  return mPath;
112  }
113 
114  switch(column)
115  {
116  case 0:
117  if (mCount <= 0) return mName;
118  return QString("%1 (%2)").arg(mName).arg(mCount+1);
119  case 1: return mPath;
120  }
121  return QVariant();
122  }
123 
125  : QAbstractTableModel(parent), mShowFullPath(false), mWarnSubstitute(false)
126  {
127  if (addAutoDetect)
128  mEntries.append(GateLibrarySelectionEntry("(Auto detect)", "", -1));
129  else
130  mEntries.append(GateLibrarySelectionEntry("", "", -1));
131  QMap<QString,int> nameMap;
132  for (const std::filesystem::path& path : gate_library_manager::get_all_path())
133  {
134  QString name = QString::fromStdString(path.filename());
135  mEntries.append(GateLibrarySelectionEntry(name, QString::fromStdString(path.string()), nameMap[name]++));
136  }
137  }
138 
140  {
141  Q_UNUSED(index);
142  return 2;
143  }
144 
146  {
147  Q_UNUSED(index);
148  return mEntries.size();
149  }
150 
152  {
153  if (role != Qt::DisplayRole) return QVariant();
154  if (index.row() >= mEntries.size()) return QVariant();
155  return (mEntries.at(index.row()).data(index.column(),mShowFullPath));
156  }
157 
159  {
160  int inx = 0;
161  QMap<QString,int> nameMap;
162 
163  for (const GateLibrarySelectionEntry& glse : mEntries)
164  {
165  if (glse.path() == path) return inx; // path already in table
166  nameMap[glse.name()]++;
167  inx++;
168  }
169 
170  QString name = QFileInfo(path).fileName();
171  beginResetModel();
172  mEntries.append(GateLibrarySelectionEntry(name,path,nameMap[name]++));
173  return inx;
174  }
175 
177  {
178  // try exact path
179  int inx = 0;
180  for (const GateLibrarySelectionEntry& glse : mEntries)
181  {
182  if (glse.path() == path) return inx;
183  ++inx;
184  }
185 
186  // existing gatelib path not in list : add it
187  if (QFileInfo(path).exists())
188  return addGateLibrary(path);
189 
190  // try name
191  QString name = QFileInfo(path).fileName();
192  inx = 0;
193  for (const GateLibrarySelectionEntry& glse : mEntries)
194  {
195  if (glse.name() == name)
196  {
197  log_info("gui", "Requested gate library '{}' not found, suggest '{}' instead.", path.toStdString(), glse.path().toStdString());
198  mWarnSubstitute = true;
199  return inx;
200  }
201  ++inx;
202  }
203 
204  log_info("gui", "Requested gate library '{}' not found.", path.toStdString());
205  return -1;
206  }
207 
208 
210  {
211  if (inx < 0 || inx >= mEntries.size()) return QString();
212  return mEntries.at(inx).path();
213  }
214 
216  {
217  beginResetModel();
218  mShowFullPath = checked;
219  endResetModel();
220  }
221 }
QVariant data(int column, bool fullPath) const
GateLibrarySelection(const QString &defaultGl, QWidget *parent=nullptr)
int columnCount(const QModelIndex &index=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int addGateLibrary(const QString &path)
int getIndexByPath(const QString &path)
GateLibrarySelectionTable(bool addAutoDetect, QObject *parent=nullptr)
int rowCount(const QModelIndex &index=QModelIndex()) const override
#define log_info(channel,...)
Definition: log.h:70
std::vector< std::filesystem::path > get_all_path()
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
std::string name
void setChecked(bool)
void clicked(bool checked)
void toggled(bool checked)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void addLayout(QLayout *layout, int stretch)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setCurrentIndex(int index)
void currentIndexChanged(int index)
QAbstractItemModel * model() const const
void setModel(QAbstractItemModel *model)
QString currentPath()
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString fileName() const const
void setFrameStyle(int style)
void clear()
void setText(const QString &)
int column() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setObjectName(const QString &name)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
std::string toStdString() const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
DisplayRole
void hide()
void setSizePolicy(QSizePolicy)
QStyle * style() const const