HAL  v4.5.0-232-g8053ac4b3
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
parameter_value_delegate.cpp
Go to the documentation of this file.
2 
5 
6 #include <QComboBox>
7 #include <QLineEdit>
8 #include <QLocale>
9 
10 namespace hal
11 {
13  {
14  }
15 
17  {
18  switch (parameterType(index))
19  {
21  auto comboBox = new QComboBox(parent);
22  comboBox->addItem("true");
23  comboBox->addItem("false");
24  comboBox->setFocusPolicy(Qt::StrongFocus);
25  return comboBox;
26  }
28  auto textField = new QLineEdit(parent);
29  textField->setValidator(new QRegularExpressionValidator(
30  QRegularExpression("^-?(?:0[xX][0-9a-fA-F]+|0[oO][0-7]+|0[bB][01]+|\\d+)$"), textField));
31  return textField;
32  }
34  auto textField = new QLineEdit(parent);
35  auto validator = new QDoubleValidator(textField);
36  QLocale cLocale = QLocale::c();
38  validator->setLocale(cLocale);
39  textField->setValidator(validator);
40  return textField;
41  }
43  auto textField = new QLineEdit(parent);
44  textField->setValidator(new QRegularExpressionValidator(
45  QRegularExpression("^((0[xX][0-9a-fA-F]+)|(0[oO][0-7]+)|(0[bB][01]+))$"), textField));
46  return textField;
47  }
49  auto textField = new QLineEdit(parent);
50  textField->setValidator(new QRegularExpressionValidator(
51  QRegularExpression("^((0[xX][0-9a-fA-FxXzZuUlLhHwW-]+)|(0[oO][0-7xXzZuUlLhHwW-]+)|(0[bB][01xXzZuUlLhHwW-]+))$"), textField));
52  return textField;
53  }
54  case Parameter::Type::Time: {
55  auto textField = new QLineEdit(parent);
56  textField->setValidator(new QRegularExpressionValidator(
57  QRegularExpression("^(?:\\d+\\.?\\d*|\\.\\d+)(fs|ps|ns|us|ms|min|s|h)$"), textField));
58  return textField;
59  }
60  case Parameter::Type::Enum: {
61  auto comboBox = new QComboBox(parent);
62  comboBox->setFocusPolicy(Qt::StrongFocus);
63  comboBox->addItems(index.data(ParameterTableModel::EnumValuesRole).toStringList());
64  return comboBox;
65  }
67  return new QLineEdit(parent);
68  }
69  default: {
70  return nullptr;
71  }
72  }
73  }
74 
76  {
77  if (auto* comboBox = qobject_cast<QComboBox*>(editor))
78  {
79  model->setData(index, comboBox->currentText(), Qt::EditRole);
80  }
81  else if (auto* lineEdit = qobject_cast<QLineEdit*>(editor))
82  {
83  if (parameterType(index) == Parameter::Type::Integer)
84  {
85  bool ok = false;
86  const QString decimal = integerToDecimalString(lineEdit->text(), ok);
87  if (!ok)
88  {
89  return;
90  }
91  model->setData(index, decimal, Qt::EditRole);
92  }
93  else
94  {
95  model->setData(index, lineEdit->text(), Qt::EditRole);
96  }
97  }
98  }
99 
101  {
102  editor->setGeometry(option.rect);
103  }
104 
105  Parameter::Type ParameterValueDelegate::parameterType(const QModelIndex& index)
106  {
107  const QString typeString = index.sibling(index.row(), 1).data(Qt::DisplayRole).toString();
108  return enum_from_string<Parameter::Type>(typeString.toStdString(), Parameter::Type::String);
109  }
110 
111  QString ParameterValueDelegate::integerToDecimalString(const QString& text, bool& ok)
112  {
113  ok = false;
114 
115  QString digits = text.trimmed();
116 
117  bool negative = false;
118  if (digits.startsWith('-'))
119  {
120  negative = true;
121  digits.remove(0, 1);
122  }
123 
124  int base = 10;
125  if (digits.startsWith("0x", Qt::CaseInsensitive))
126  {
127  base = 16;
128  digits.remove(0, 2);
129  }
130  else if (digits.startsWith("0o", Qt::CaseInsensitive))
131  {
132  base = 8;
133  digits.remove(0, 2);
134  }
135  else if (digits.startsWith("0b", Qt::CaseInsensitive))
136  {
137  base = 2;
138  digits.remove(0, 2);
139  }
140 
141  const qlonglong value = digits.toLongLong(&ok, base);
142  if (!ok)
143  {
144  return QString();
145  }
146 
147  return QString::number(negative ? -value : value);
148  }
149 } // namespace hal
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
ParameterValueDelegate(QObject *parent=nullptr)
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: defines.h:45
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
RejectGroupSeparator
QLocale c()
void setNumberOptions(QLocale::NumberOptions options)
QObject * parent() const const
QString number(int n, int base)
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
qlonglong toLongLong(bool *ok, int base) const const
std::string toStdString() const const
QString trimmed() const const
CaseInsensitive
StrongFocus
EditRole
void setGeometry(int x, int y, int w, int h)