HAL  v4.5.0-147-gff98e36c6
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  // "(Auto detect)" has no path, so there is no single gate library file that could be copied
82  }
83 
84  void GateLibrarySelection::handleInvokeFileDialog()
85  {
86  QString glFilename = QFileDialog::getOpenFileName(this, "Select Gate Library", QDir::currentPath(), "HGL Files (*.hgl);;Lib Files (*.lib)");
87  if (glFilename.isEmpty()) return;
88  int inx = static_cast<GateLibrarySelectionTable*>(mComboGatelib->model())->addGateLibrary(glFilename);
89  mComboGatelib->setCurrentIndex(inx);
90  }
91 
93  {
94  const GateLibrarySelectionTable* glst = static_cast<const GateLibrarySelectionTable*>(mComboGatelib->model());
95  return glst->gateLibraryPath(mComboGatelib->currentIndex());
96  }
97 
98  void GateLibrarySelection::handleShowFullPath(bool checked)
99  {
100  int inx = mComboGatelib->currentIndex();
101  static_cast<GateLibrarySelectionTable*>(mComboGatelib->model())->handleShowFullPath(checked);
102  mComboGatelib->setCurrentIndex(inx);
103  }
104 
105  //-----------------------------------------------
106 
107  QVariant GateLibrarySelectionEntry::data(int column, bool fullPath) const
108  {
109  if (fullPath)
110  {
111  if (mCount<0)
112  return mName;
113  return mPath;
114  }
115 
116  switch(column)
117  {
118  case 0:
119  if (mCount <= 0) return mName;
120  return QString("%1 (%2)").arg(mName).arg(mCount+1);
121  case 1: return mPath;
122  }
123  return QVariant();
124  }
125 
127  : QAbstractTableModel(parent), mShowFullPath(false), mWarnSubstitute(false)
128  {
129  if (addAutoDetect)
130  mEntries.append(GateLibrarySelectionEntry("(Auto detect)", "", -1));
131  else
132  mEntries.append(GateLibrarySelectionEntry("", "", -1));
133  QMap<QString,int> nameMap;
134  for (const std::filesystem::path& path : gate_library_manager::get_all_path())
135  {
136  QString name = QString::fromStdString(path.filename());
137  mEntries.append(GateLibrarySelectionEntry(name, QString::fromStdString(path.string()), nameMap[name]++));
138  }
139  }
140 
142  {
143  Q_UNUSED(index);
144  return 2;
145  }
146 
148  {
149  Q_UNUSED(index);
150  return mEntries.size();
151  }
152 
154  {
155  if (role != Qt::DisplayRole) return QVariant();
156  if (index.row() >= mEntries.size()) return QVariant();
157  return (mEntries.at(index.row()).data(index.column(),mShowFullPath));
158  }
159 
161  {
162  int inx = 0;
163  QMap<QString,int> nameMap;
164 
165  for (const GateLibrarySelectionEntry& glse : mEntries)
166  {
167  if (glse.path() == path) return inx; // path already in table
168  nameMap[glse.name()]++;
169  inx++;
170  }
171 
172  QString name = QFileInfo(path).fileName();
173  beginResetModel();
174  mEntries.append(GateLibrarySelectionEntry(name,path,nameMap[name]++));
175  return inx;
176  }
177 
179  {
180  // try exact path
181  int inx = 0;
182  for (const GateLibrarySelectionEntry& glse : mEntries)
183  {
184  if (glse.path() == path) return inx;
185  ++inx;
186  }
187 
188  // existing gatelib path not in list : add it
189  if (QFileInfo(path).exists())
190  return addGateLibrary(path);
191 
192  // try name
193  QString name = QFileInfo(path).fileName();
194  inx = 0;
195  for (const GateLibrarySelectionEntry& glse : mEntries)
196  {
197  if (glse.name() == name)
198  {
199  log_info("gui", "Requested gate library '{}' not found, suggest '{}' instead.", path.toStdString(), glse.path().toStdString());
200  mWarnSubstitute = true;
201  return inx;
202  }
203  ++inx;
204  }
205 
206  log_info("gui", "Requested gate library '{}' not found.", path.toStdString());
207  return -1;
208  }
209 
210 
212  {
213  if (inx < 0 || inx >= mEntries.size()) return QString();
214  return mEntries.at(inx).path();
215  }
216 
218  {
219  beginResetModel();
220  mShowFullPath = checked;
221  endResetModel();
222  }
223 }
QVariant data(int column, bool fullPath) const
void gatelibSelected(bool singleFile)
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
Q_EMITQ_EMIT
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