Fabcoin Core  0.16.2
P2P Digital Currency
abiparam.cpp
Go to the documentation of this file.
1 #include <abiparam.h>
2 #include <contractabi.h>
3 #include <abiparamitem.h>
4 #include <platformstyle.h>
5 
6 #include <QHBoxLayout>
7 #include <QRegularExpressionValidator>
8 
9 ABIParam::ABIParam(const PlatformStyle *platformStyle, int ID, const ParameterABI &param, QWidget *parent) :
10  QWidget(parent),
11  m_ParamID(ID),
12  m_paramName(0),
13  m_mainLayout(0),
14  m_paramItemsLayout(0),
15  m_param(param),
16  m_platformStyle(platformStyle),
17  m_vSpacer(0),
18  m_hSpacer(0)
19 {
20  m_paramName = new QLabel(this);
21  m_mainLayout = new QHBoxLayout(this);
22  m_paramItemsLayout = new QVBoxLayout();
23  m_vSpacer = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
24  m_hSpacer = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
25 
26  m_mainLayout->setSpacing(10);
27  m_mainLayout->setContentsMargins(0,0,0,0);
28 
29  m_paramItemsLayout->setSpacing(3);
30  m_paramItemsLayout->setContentsMargins(0,0,0,0);
31 
32  m_paramName->setToolTip(tr("%1 %2").arg(QString::fromStdString(param.type)).arg(QString::fromStdString(param.name)));
33  m_paramName->setFixedWidth(160);
34  m_paramName->setFixedHeight(27);
35 
36  QFontMetrics metrix(m_paramName->font());
37  int width = m_paramName->width() + 10;
38  QString text(QString("%2 <b>%1").arg(QString::fromStdString(param.name)).arg(QString::fromStdString(param.type)));
39  QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
40  m_paramName->setText(clippedText);
41 
42  QVBoxLayout *vLayout = new QVBoxLayout();
43  vLayout->addWidget(m_paramName);
44  vLayout->addSpacerItem(m_vSpacer);
45  m_mainLayout->addLayout(vLayout);
46 
47  if(param.decodeType().isList())
48  {
49  if(param.decodeType().isDynamic())
50  {
51  addNewParamItem(0);
52  }
53  else
54  {
55  for(size_t i = 0; i < param.decodeType().length(); i++)
56  {
57  ABIParamItem *m_paramValue = new ABIParamItem(m_platformStyle, m_param, this);
58  m_paramValue->setFixed(true);
59  m_paramItemsLayout->addWidget(m_paramValue);
60  m_listParamItems.append(m_paramValue);
61  }
62  if(param.decodeType().length() > 1)
63  {
64  m_vSpacer->changeSize(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);
65  }
66  }
67  }
68  else
69  {
70  ABIParamItem *m_paramValue = new ABIParamItem(m_platformStyle, m_param, this);
71  m_paramValue->setFixed(true);
72  m_paramItemsLayout->addWidget(m_paramValue);
73  m_listParamItems.append(m_paramValue);
74  }
75  m_mainLayout->addLayout(m_paramItemsLayout);
76  m_mainLayout->addSpacerItem(m_hSpacer);
77  setLayout(m_mainLayout);
78 }
79 
80 QStringList ABIParam::getValue()
81 {
82  QStringList valuesList;
83  for(int i = 0; i < m_listParamItems.count(); i++)
84  {
85  if(!m_listParamItems[i]->getIsDeleted())
86  valuesList.append(m_listParamItems[i]->getValue());
87  }
88  return valuesList;
89 }
90 
92 {
93  bool isValid = true;
94  for(int i = 0; i < m_listParamItems.count(); i++)
95  {
96  if(!m_listParamItems[i]->getIsDeleted() && !m_listParamItems[i]->isValid())
97  isValid = false;
98  }
99  return isValid;
100 }
101 
103 {
104  for(int i = 0; i < m_paramItemsLayout->count(); i++)
105  {
106  m_listParamItems[i]->setPosition(i);
107  }
108 }
109 
110 void ABIParam::addNewParamItem(int position)
111 {
112  if(m_listParamItems.count() == 1 && m_listParamItems[0]->getIsDeleted())
113  {
114  m_hSpacer->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
115  m_listParamItems[0]->setIsDeleted(false);
116  }
117  else
118  {
119  ABIParamItem *item = new ABIParamItem(m_platformStyle, m_param, this);
120  m_listParamItems.insert(position, item);
121  m_paramItemsLayout->insertWidget(position, item);
122 
123  if(m_paramItemsLayout->count() > 1)
124  {
125  m_vSpacer->changeSize(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);
126  }
127 
129 
130  connect(item, SIGNAL(on_addItemClicked(int)), this, SLOT(addNewParamItem(int)));
131  connect(item, SIGNAL(on_removeItemClicked(int)), this, SLOT(removeParamItem(int)));
132  }
133 }
134 
135 void ABIParam::removeParamItem(int position)
136 {
137  if(m_listParamItems.count() == 1)
138  {
139  m_listParamItems[0]->setIsDeleted(true);
140  m_hSpacer->changeSize(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed);
141  }
142  else
143  {
144  QLayoutItem *item = m_paramItemsLayout->takeAt(position);
145  QWidget * widget = item->widget();
146  if(widget != NULL)
147  {
148  m_paramItemsLayout->removeWidget(widget);
149  disconnect(widget, 0, 0, 0);
150  widget->setParent(NULL);
151  delete widget;
152  m_listParamItems.removeAt(position);
153  }
154 
155  if(m_paramItemsLayout->count() < 2)
156  {
157  m_vSpacer->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
158  }
159 
161  }
162 }
size_t length() const
length Length of the list, applicable for list only
void updateParamItemsPosition()
Definition: abiparam.cpp:102
void removeParamItem(int position)
Definition: abiparam.cpp:135
bool isList() const
isList Is the data type list
QSpacerItem * m_vSpacer
Definition: abiparam.h:51
bool isDynamic() const
isDynamic Check if the type is dynamic.
bool isValid()
Definition: abiparam.cpp:91
QLabel * m_paramName
Definition: abiparam.h:45
ABIParam(const PlatformStyle *platformStyle, int ID, const ParameterABI &param, QWidget *parent=0)
ABIParam Constructor.
Definition: abiparam.cpp:9
std::string name
Definition: contractabi.h:142
const PlatformStyle * m_platformStyle
Definition: abiparam.h:50
QStringList getValue()
getValue Get the value of the parameter
Definition: abiparam.cpp:80
std::string type
Definition: contractabi.h:143
const ParameterType & decodeType() const
void addNewParamItem(int position)
Definition: abiparam.cpp:110
ParameterABI m_param
Definition: abiparam.h:48
void setFixed(bool isFixed)
QList< ABIParamItem * > m_listParamItems
Definition: abiparam.h:49
QHBoxLayout * m_mainLayout
Definition: abiparam.h:46
QVBoxLayout * m_paramItemsLayout
Definition: abiparam.h:47
QSpacerItem * m_hSpacer
Definition: abiparam.h:52