HAL
generalinfo_wizardpage.cpp
Go to the documentation of this file.
2 
5 
6 namespace hal
7 {
9  : QAbstractTableModel(parent)
10  {
11  for (int i=(int)GateTypeProperty::combinational; i<=(int)GateTypeProperty::c_lut; i++)
12  mList.append({(GateTypeProperty)i,false});
13  }
14 
15  QVariant ListPropertyModel::data(const QModelIndex& index, int role) const
16  {
17  if (role != Qt::DisplayRole || index.row() >= mList.size()) return QVariant();
18 
19  GateTypeProperty gtp = mList.at(index.row()).property;
20  return QString::fromStdString(enum_to_string<GateTypeProperty>(gtp));
21  }
22 
23  void ListPropertyModel::setSelected(GateTypeProperty gtp, bool select)
24  {
25  for (int irow=0; irow < mList.size(); irow++)
26  {
27  ListPropertyEntry& lpe = mList[irow];
28  if (lpe.property == gtp)
29  {
30  if (lpe.isSelected == select) return; // nothing to do
31  lpe.isSelected = select;
32  QModelIndex inx = index(irow,0);
33  Q_EMIT dataChanged(inx,inx);
34  return;
35  }
36  }
37  }
38 
39  ListPropertyProxy::ListPropertyProxy(bool showSel, QObject* parent)
40  : QSortFilterProxyModel(parent), mShowSelected(showSel)
41  {;}
42 
43  bool ListPropertyProxy::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
44  {
45  Q_UNUSED(sourceParent);
46  const ListPropertyModel* model = static_cast<const ListPropertyModel*>(sourceModel());
47 
48  return (model->isSelected(sourceRow) == mShowSelected);
49  }
50 
52  {
53  setTitle("General Information");
54  setSubTitle("Add general information about the gate, such as name and properties");
55  mLayout = new QGridLayout(this);
56  mName = new QLineEdit(this);
57  mPropertyModel = new ListPropertyModel(this);
58 
59  mPropertiesSelected = new QListView(this);
60  ListPropertyProxy* selectedProxy = new ListPropertyProxy(true, this);
61  selectedProxy->setSourceModel(mPropertyModel);
62  mPropertiesSelected->setModel(selectedProxy);
63 
64  mPropertiesAvailable = new QListView(this);
65  ListPropertyProxy* availableProxy = new ListPropertyProxy(false, this);
66  availableProxy->setSourceModel(mPropertyModel);
67  mPropertiesAvailable->setModel(availableProxy);
68 
69  mGateLibrary = gt;
70 
71  QLabel* labName = new QLabel("Name *", this);
72  QLabel* labProperties = new QLabel("Properties:", this);
73  labProperties->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
74  labProperties->setObjectName("labProperties");
75  QLabel* labPropertiesSelected = new QLabel(" Selected properties *", this);
76  QLabel* labPropertiesAvailable = new QLabel(" Available properties", this);
79 
80  mAddBtn = new QPushButton(this);
82  mAddBtn->setObjectName("arrowButton");
83  mDelBtn = new QPushButton(this);
85  mDelBtn->setObjectName("arrowButton");
86 
87  QStyle* s = style();
88 
89  s->unpolish(this);
90  s->polish(this);
91 
92  mLeftArrowIcon = gui_utility::getStyledSvgIcon(mEnabledIconStyle, mLeftArrowIconPath, mDisabledIconStyle);
93  mRightArrowIcon = gui_utility::getStyledSvgIcon(mEnabledIconStyle, mRightArrowIconPath, mDisabledIconStyle);
94  mAddBtn->setIcon(mLeftArrowIcon);
95  mDelBtn->setIcon(mRightArrowIcon);
96  mAddBtn->setIconSize(QSize(24,24));
97  mDelBtn->setIconSize(QSize(24,24));
98 
99  mLayout->addWidget(labName, 0, 0);
100  mLayout->addWidget(mName, 0, 1, 1, 2);
101  mLayout->addWidget(labProperties, 1, 0, 1, 3);
102  mLayout->addWidget(labPropertiesSelected, 2, 0);
103  mLayout->addWidget(labPropertiesAvailable, 2, 2);
104  mLayout->addWidget(mPropertiesSelected, 3, 0, 3, 1);
105  mLayout->addWidget(mAddBtn, 3, 1);
106  mLayout->addWidget(mDelBtn, 4, 1);
107  mLayout->addWidget(mPropertiesAvailable, 3, 2, 3, 1);
108 
109  setLayout(mLayout);
110 
111  connect(mAddBtn, &QPushButton::clicked, this, &GeneralInfoWizardPage::addProperty);
112  connect(mDelBtn, &QPushButton::clicked, this, &GeneralInfoWizardPage::deleteProperty);
113  connect(mName, &QLineEdit::textChanged, this, &GeneralInfoWizardPage::handleNameChanged);
114 
115  QRegExp rx("[A-z]([A-z]|\\d|_)*");
116  mValidator = new QRegExpValidator(rx, this);
117  mName->setValidator(mValidator);
118  }
119 
121  {
122  mWizard = static_cast<GateLibraryWizard*>(wizard());
123  mWizard->mEditMode = true;
124  }
125 
126  void GeneralInfoWizardPage::setData(QString name, const std::vector<GateTypeProperty>& properties)
127  {
128  mName->setText(name);
129 
130  mNameInit = name;
131 
132  for (GateTypeProperty gtp : properties)
133  {
134  mPropertyModel->setSelected(gtp, true);
135  }
136 
138  }
139 
141  {
142  return mName->text();
143  }
144 
146  {
148  for (int irow = 0; irow < mPropertyModel->rowCount(); irow++)
149  if (mPropertyModel->isSelected(irow))
150  retval.append(mPropertyModel->property(irow));
151 
152  return retval;
153  }
154 
155  void GeneralInfoWizardPage::addProperty()
156  {
157  QModelIndex inx = mPropertiesAvailable->currentIndex();
158  if (!inx.isValid()) return;
159  GateTypeProperty gtp = enum_from_string<GateTypeProperty>(mPropertiesAvailable->model()->data(inx).toString().toStdString(),GateTypeProperty::combinational);
160  mPropertyModel->setSelected(gtp,true);
161  mAddBtn->setEnabled(mPropertiesAvailable->model()->rowCount());
162  mDelBtn->setEnabled(mPropertiesSelected->model()->rowCount());
164  }
165 
166  void GeneralInfoWizardPage::deleteProperty()
167  {
168  QModelIndex inx = mPropertiesSelected->currentIndex();
169  if (!inx.isValid()) return;
170  GateTypeProperty gtp = enum_from_string<GateTypeProperty>(mPropertiesSelected->model()->data(inx).toString().toStdString(),GateTypeProperty::combinational);
171  mPropertyModel->setSelected(gtp,false);
172  mAddBtn->setEnabled(mPropertiesAvailable->model()->rowCount());
173  mDelBtn->setEnabled(mPropertiesSelected->model()->rowCount());
175  }
176 
177  void GeneralInfoWizardPage::handleNameChanged(const QString &txt)
178  {
179  Q_UNUSED (txt);
181  }
182 
184  {
185  if (getProperties().isEmpty() || mName->text().isEmpty()) return false;
186 
187  if (mName->text() == mNameInit) return true; // name of existing type unchanged
188 
189  for (auto it : mGateLibrary->get_gate_types())
190  {
191  if (QString::fromStdString(it.first) == mName->text())
192  return false;
193  }
194  mWizard->mEditMode = false;
195  return true;
196  }
197 }
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
QList< GateTypeProperty > getProperties() const
GeneralInfoWizardPage(const GateLibrary *gt, QWidget *parent=nullptr)
void setData(QString name, const std::vector< GateTypeProperty > &properties)
int rowCount(const QModelIndex &index=QModelIndex()) const override
bool isSelected(int irow) const
void setSelected(GateTypeProperty gtp, bool select)
ListPropertyModel(QObject *parent=nullptr)
GateTypeProperty property(int irow) const
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent=QModelIndex()) const override
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
std::string name
void clicked(bool checked)
void setIcon(const QIcon &icon)
void setIconSize(const QSize &size)
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
QModelIndex currentIndex() const const
QAbstractItemModel * model() const const
virtual void setModel(QAbstractItemModel *model)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setAlignment(Qt::Alignment)
void setValidator(const QValidator *v)
void setText(const QString &)
void textChanged(const QString &text)
void append(const T &value)
bool isValid() 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)
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
QString fromStdString(const std::string &str)
std::string toStdString() const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
AlignLeft
DisplayRole
QString toString() const const
void setEnabled(bool)
void setLayout(QLayout *layout)
void setSizePolicy(QSizePolicy)
QStyle * style() const const
void completeChanged()
void setSubTitle(const QString &subTitle)
void setTitle(const QString &title)
QWizard * wizard() const const
GateTypeProperty property
bool isSelected