HAL  v4.5.0-232-g8053ac4b3
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
parameter_table_model.cpp
Go to the documentation of this file.
2 
6 
7 #include <algorithm>
8 
9 namespace hal
10 {
12  {
13  }
14 
16  {
17  return 5;
18  }
19 
20  int ParameterTableModel::rowCount(const QModelIndex& parent) const
21  {
22  return mRows.size();
23  }
24 
26  {
27  if (index.row() < 0 || index.row() >= rowCount())
28  {
29  return QVariant();
30  }
31 
32  const ParameterRow& row = mRows[index.row()];
33 
34  if (role == Qt::DisplayRole && index.column() < columnCount())
35  {
36  switch (index.column())
37  {
38  case 0:
39  return row.name;
40  case 1:
41  return row.type;
42  case 2:
43  return row.size;
44  case 3:
45  return row.defaultValue;
46  case 4:
47  return row.value;
48  }
49  }
50 
51  if (role == Qt::EditRole && index.column() == 4)
52  {
53  return row.value;
54  }
55 
56  if (role == EnumValuesRole && index.column() == 4)
57  {
58  return row.enumValues;
59  }
60 
61  if (role == Qt::TextAlignmentRole)
62  {
63  return Qt::AlignLeft;
64  }
65 
66  return QVariant();
67  }
68 
69  QVariant ParameterTableModel::headerData(int section, Qt::Orientation orientation, int role) const
70  {
71  const char* horizontalHeader[] = {"Name", "Type", "Size", "Default", "Value"};
72  if (orientation == Qt::Horizontal && role == Qt::DisplayRole && section < columnCount())
73  {
74  return QString(horizontalHeader[section]);
75  }
76 
77  return QVariant();
78  }
79 
81  {
83 
84  for (const auto& [name, declAndValue] : gate->get_parameters())
85  {
86  const Parameter& decl = declAndValue.first;
87 
88  ParameterRow row;
89  row.name = QString::fromStdString(name);
90  row.type = QString::fromStdString(enum_to_string<Parameter::Type>(decl.get_type()));
91  row.size = QString::number(decl.get_size());
92  row.defaultValue = QString::fromStdString(decl.get_default_value());
93  row.value = QString::fromStdString(declAndValue.second);
94 
95  if (decl.get_type() == Parameter::Type::Enum)
96  {
97  for (const std::string& enumValue : decl.get_enum_values())
98  {
99  row.enumValues.append(QString::fromStdString(enumValue));
100  }
101  }
102 
103  rows.append(row);
104  }
105 
106  // get_parameters() returns an unordered_map, so sort by name for a stable display order.
107  std::sort(rows.begin(), rows.end(), [](const ParameterRow& a, const ParameterRow& b) { return a.name < b.name; });
108 
109  // Editing a value triggers this update synchronously from within setData(). A model reset at that point
110  // would discard the editor that is still committing, so values of the same gate are updated in place.
111  bool sameRows = (gate == mGate && rows.size() == mRows.size());
112  for (int i = 0; sameRows && i < rows.size(); ++i)
113  {
114  sameRows = (rows.at(i).name == mRows.at(i).name);
115  }
116 
117  if (sameRows)
118  {
119  mRows = rows;
120  if (!mRows.isEmpty())
121  {
122  Q_EMIT dataChanged(index(0, 0), index(mRows.size() - 1, columnCount() - 1));
123  }
124  return;
125  }
126 
127  beginResetModel();
128  mGate = gate;
129  mRows = rows;
130  endResetModel();
131  }
132 
133  bool ParameterTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
134  {
135  if (role != Qt::EditRole || index.column() != 4)
136  {
137  return false;
138  }
139 
140  auto act = new ActionSetParameterValue(mRows[index.row()].name, value.toString());
141  act->setObject(UserActionObject(mGate->get_id(), UserActionObjectType::Gate));
142  return act->exec();
143  }
144 
146  {
147  if (index.column() == 4)
148  {
150  }
152  }
153 } // namespace hal
Sets the value of an existing typed parameter on a gate.
const std::unordered_map< std::string, std::pair< Parameter, std::string > > & get_parameters() const
Definition: gate.h:59
u32 get_id() const
Definition: gate.cpp:132
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
ParameterTableModel(QObject *parent=nullptr)
QVariant data(const QModelIndex &index, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
The UserActionObject class represents a single object used in UserAction.
Definition: defines.h:45
std::string name
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int column() const const
int row() const const
Q_EMITQ_EMIT
QString fromStdString(const std::string &str)
QString number(int n, int base)
AlignLeft
DisplayRole
typedef ItemFlags
Orientation
QString toString() const const
void append(const T &value)
const T & at(int i) const const
QVector::iterator begin()
QVector::iterator end()
bool isEmpty() const const
int size() const const
u16 get_size() const
Definition: parameter.cpp:304
Type get_type() const
Definition: parameter.cpp:299
const std::vector< std::string > & get_enum_values() const
Definition: parameter.cpp:314
const std::string & get_default_value() const
Definition: parameter.cpp:309