HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
pin_delegate.cpp
Go to the documentation of this file.
2 
3 #include "gui/gui_globals.h"
5 
6 #include <QComboBox>
7 #include <QApplication>
8 #include <QLineEdit>
9 #include <QPainter>
10 #include <QPushButton>
11 #include <QSpinBox>
12 #include <gui/pin_model/pin_item.h>
14 
15 namespace hal
16 {
17 
19  {
20 
21  }
22 
23  bool PinDelegate::containsLutType(const PinItem* item)
24  {
25  if(item->getPinType() == PinType::lut)
26  return true;
27 
28  // editing a pin group propagates type and direction to all of its pins, so a group has to be
29  // treated like a lut pin as soon as one of its pins is of that type
30  for(const BaseTreeItem* child : item->getChildren())
31  {
32  if(static_cast<const PinItem*>(child)->getPinType() == PinType::lut)
33  return true;
34  }
35  return false;
36  }
37 
38  bool PinDelegate::containsInputDirection(const PinItem* item)
39  {
40  if(item->getDirection() == PinDirection::input)
41  return true;
42 
43  for(const BaseTreeItem* child : item->getChildren())
44  {
45  if(static_cast<const PinItem*>(child)->getDirection() == PinDirection::input)
46  return true;
47  }
48  return false;
49  }
50 
52  {
53  const PinItem* pinItem = static_cast<const PinItem*>(index.internalPointer());
54  PinItem::TreeItemType itemType = pinItem->getItemType();
55  //TODO create editors
56 
57  const bool allowInput = !containsLutType(pinItem) || pinItem->getDirection() == PinDirection::input;
58  const bool allowLut = !containsInputDirection(pinItem) || pinItem->getPinType() == PinType::lut;
59 
60  // Column 0: Name, Column 1: Type, Column 2: Direction, Column 3: Delete button
61  switch(index.column()){
62  case 0:{
63  auto lineEdit = new QLineEdit(parent);
64  lineEdit->setFocusPolicy(Qt::StrongFocus);
65  return lineEdit;
66  }
67  case 1: {
68  //dont create a widget for dummyEntries
70  return nullptr;
71  auto comboBox = new QComboBox(parent);
72  //TODO provide enum to string method
73  if(allowInput)
74  comboBox->addItem("input");
75  comboBox->addItem("output");
76  comboBox->addItem("inout");
77  comboBox->addItem("internal");
78  comboBox->setFocusPolicy(Qt::StrongFocus);
79  return comboBox;
80  }
81  case 2:{
82  //dont create a widget for dummyEntries
84  return nullptr;
85 
86  auto comboBox = new QComboBox(parent);
87  //TODO provide enum to string method
88  comboBox->addItem("none");
89  comboBox->addItem("power");
90  comboBox->addItem("ground");
91  if(allowLut)
92  comboBox->addItem("lut");
93  comboBox->addItem("state");
94  comboBox->addItem("neg_state");
95  comboBox->addItem("clock");
96  comboBox->addItem("enable");
97  comboBox->addItem("set");
98  comboBox->addItem("reset");
99  comboBox->addItem("data");
100  comboBox->addItem("address");
101  comboBox->addItem("io_pad");
102  comboBox->addItem("select");
103  comboBox->addItem("carry");
104  comboBox->addItem("sum");
105  comboBox->setFocusPolicy(Qt::StrongFocus);
106  return comboBox;
107  }
108  case 3:{
109  qInfo() << "create editor for 3";
110  }
111  }
112 
113 
114  return nullptr;
115  }
116 
118  {
119  //TODO set editor data if direction or type is selected
120  //qInfo() << "Editing row: " << index.row() << " column: " << index.column();
121  auto pinItem = static_cast<PinItem*>(index.internalPointer());
122  PinItem::TreeItemType itemType = pinItem->getItemType();
123 
124  switch(index.column()){
125  case 0: {
126  //name editor
127  auto lineEdit = static_cast<QLineEdit*>(editor);
129  lineEdit->setText(pinItem->getName());
130  }
131  break;
132  }
133  case 1: {
134  //direction editor
135  //TODO currently only pins can change the direction while groups does not allow modification
137  break;
138  auto comboBox = static_cast<QComboBox*>(editor);
139  comboBox->setCurrentText(pinItem->getDirectionAsText());
140  break;
141  }
142  case 2: {
143  //type editor
145  break;
146  auto comboBox = static_cast<QComboBox*>(editor);
147  comboBox->setCurrentText(pinItem->getPinTypeAsText());
148  break;
149  }
150  }
151 
152  }
153 
155  {
156  //TODO cast editor to corresponding column
157  PinItem::TreeItemType itemType = static_cast<PinItem*>(index.internalPointer())->getItemType();
158  auto pinModel = static_cast<PinModel*>(model);
159 
160  switch(index.column()){
161  case 0: {
162  //name column
163  auto lineEdit = static_cast<QLineEdit*>(editor);
164  QString text = lineEdit->text();
165  if (!text.isEmpty())
166  pinModel->handleEditName(index, text);
167  break;
168  }
169  case 1:{
170  //direction column
171  //TODO currently only pins can change the direction while groups does not allow modification
173  break;
174  auto comboBox = static_cast<QComboBox*>(editor);
175  QString text = comboBox->currentText();
176  if(!text.isEmpty())
177  pinModel->handleEditDirection(index, text);
178  break;
179  }
180  case 2:{
181  //type column
183  break;
184  auto comboBox = static_cast<QComboBox*>(editor);
185  QString text = comboBox->currentText();
186  if(!text.isEmpty())
187  pinModel->handleEditType(index, text);
188  break;
189  }
190  }
191  }
192 
193 
195  {
196  //QStyledItemDelegate::updateEditorGeometry(editor, option, index);
197  editor->setGeometry(option.rect);
198  }
199 
200  void PinDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
201  {
202  //TODO colorize dummy entries and invalid pins
203 
204  PinItem::TreeItemType itemType = static_cast<PinItem*>(index.internalPointer())->getItemType();
206  painter->fillRect(option.rect, Qt::darkRed);
207  }
208  QStyledItemDelegate::paint(painter, option, index);
209  }
210 }
(Future) Base class for all tree models related to the details widget.
virtual QList< BaseTreeItem * > getChildren() const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
PinDelegate(QObject *parent=0)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *editor, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
An item in the PinModel.
Definition: pin_item.h:48
PinDirection getDirection() const
Definition: pin_item.cpp:96
TreeItemType getItemType() const
Definition: pin_item.cpp:132
PinType getPinType() const
Definition: pin_item.cpp:86
Definition: defines.h:45
void setCurrentText(const QString &text)
void setText(const QString &)
QObject * parent() const const
void fillRect(const QRectF &rectangle, const QBrush &brush)
bool isEmpty() const const
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const override
StrongFocus
void setGeometry(int x, int y, int w, int h)