Fabcoin Core  0.16.2
P2P Digital Currency
abifunctionfield.cpp
Go to the documentation of this file.
1 #include <abifunctionfield.h>
2 #include <abiparamsfield.h>
3 #include <contractabi.h>
4 #include <platformstyle.h>
5 #include <QVBoxLayout>
6 #include <QHBoxLayout>
7 #include <QLabel>
8 #include <QStringListModel>
9 #include <QPainter>
10 
11 #include <iostream>
12 ABIFunctionField::ABIFunctionField(const PlatformStyle *platformStyle, FunctionType type, QWidget *parent) :
13  QWidget(parent),
14  m_contractABI(0),
15  m_func(new QWidget(this)),
16  m_comboBoxFunc(new QComboBox(this)),
17  m_paramsField(new QStackedWidget(this)),
18  m_functionType(type)
19 {
20  m_platformStyle = platformStyle;
21  // Setup layouts
22  m_comboBoxFunc->setFixedWidth(370);
23  QVBoxLayout *mainLayout = new QVBoxLayout(this);
24  mainLayout->setSpacing(10);
25  mainLayout->setContentsMargins(0, 0, 0, 0);
26 
27  QHBoxLayout *topLayout = new QHBoxLayout(m_func);
28  topLayout->setSpacing(10);
29  topLayout->setContentsMargins(0, 0, 0, 0);
30 
31  m_labelFunction = new QLabel(tr("Function"));
32  m_labelFunction->setFixedWidth(160);
33  topLayout->addWidget(m_labelFunction);
34 
35  topLayout->addWidget(m_comboBoxFunc);
36  topLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed));
37 
38  m_func->setLayout(topLayout);
39  mainLayout->addWidget(m_func);
40  mainLayout->addWidget(m_paramsField);
41  mainLayout->addSpacerItem(new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding));
42  connect(m_comboBoxFunc, SIGNAL(currentIndexChanged(int)), m_paramsField, SLOT(setCurrentIndex(int)));
43  connect(m_paramsField, SIGNAL(currentChanged(int)), this, SLOT(on_currentIndexChanged()));
44 
45  m_func->setVisible(false);
46 }
47 
49 {
50  // Clear the content
51  clear();
52 
53  if(m_contractABI != NULL)
54  {
55  // Populate the control with functions
56  std::vector<FunctionABI> functions = m_contractABI->functions;
57  QStringList functionList;
58  QStringListModel *functionModel = new QStringListModel(this);
59  bool bFieldCreate = m_functionType == Create;
60  bool bFieldCall = m_functionType == Call;
61  bool bFieldSendTo = m_functionType == SendTo;
62  bool bFieldFunc = bFieldCall || bFieldSendTo;
63  for (int func = 0; func < (int)functions.size(); ++func)
64  {
65  const FunctionABI &function = functions[func];
66  bool bTypeConstructor = function.type == "constructor";
67  bool bTypeEvent = function.type == "event";
68  bool bTypeDefault = function.type == "default";
69  bool bIsConstant = function.constant;
70  if((bFieldCreate && !bTypeConstructor) ||
71  (bFieldFunc && bTypeConstructor) ||
72  (bFieldFunc && bTypeEvent) ||
73  (bFieldCall && !bIsConstant && !bTypeDefault) ||
74  (bFieldSendTo && bIsConstant && !bTypeDefault))
75  {
76  continue;
77  }
78 
79  ABIParamsField *abiParamsField = new ABIParamsField(m_platformStyle, this);
80  abiParamsField->updateParamsField(function);
81 
82  m_paramsField->addWidget(abiParamsField);
83  QString funcName = QString::fromStdString(function.name);
84  QString funcSelector = QString::fromStdString(function.selector());
85  functionList.append(QString(funcName + "(" + funcSelector + ")"));
86 
87  m_abiFunctionList.append(func);
88  }
89  functionModel->setStringList(functionList);
90  m_comboBoxFunc->setModel(functionModel);
91 
92  if(bFieldFunc)
93  {
94  bool visible = m_abiFunctionList.size() > 0;
95  m_func->setVisible(visible);
96  }
98  }
99 }
100 
102 {
103  m_comboBoxFunc->clear();
104  m_abiFunctionList.clear();
105  for(int i = m_paramsField->count() - 1; i >= 0; i--)
106  {
107  QWidget* widget = m_paramsField->widget(i);
108  m_paramsField->removeWidget(widget);
109  widget->deleteLater();
110  }
111 }
112 
113 void ABIFunctionField::paintEvent(QPaintEvent *)
114 {
115  QStyleOption opt;
116  opt.init(this);
117  QPainter p(this);
118  style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
119 }
120 
122 {
123  m_contractABI = contractABI;
125 }
126 
127 QStringList ABIFunctionField::getParamValue(int paramID)
128 {
129  if(m_paramsField->currentWidget() == 0)
130  return QStringList();
131 
132  return ((ABIParamsField*)m_paramsField->currentWidget())->getParamValue(paramID);
133 }
134 
136 {
137  if(m_paramsField->currentWidget() == 0)
138  return QList<QStringList>();
139 
140  return ((ABIParamsField*)m_paramsField->currentWidget())->getParamsValues();
141 }
142 
143 std::vector<std::vector<std::string>> ABIFunctionField::getValuesVector()
144 {
145  QList<QStringList> qlist = getParamsValues();
146  std::vector<std::vector<std::string>> result;
147  for (int i=0; i<qlist.size(); i++)
148  {
149  std::vector<std::string> itemParam;
150  QStringList qlistVlaues = qlist[i];
151  for(int j=0; j<qlistVlaues.size(); j++)
152  {
153  itemParam.push_back(qlistVlaues.at(j).toUtf8().data());
154  }
155  result.push_back(itemParam);
156  }
157  return result;
158 }
159 
161 {
162  // Get the currently selected function
163  int currentFunc = m_comboBoxFunc->currentIndex();
164  if(currentFunc == -1)
165  return -1;
166 
167  return m_abiFunctionList[currentFunc];
168 }
169 
171 {
172  if(m_paramsField->currentWidget() == 0)
173  return true;
174 
175  return ((ABIParamsField*)m_paramsField->currentWidget())->isValid();
176 }
177 
179 {
180  for (int i = 0; i < m_paramsField->count (); ++i)
181  {
182  QSizePolicy::Policy policy = QSizePolicy::Ignored;
183  if (i == m_paramsField->currentIndex())
184  policy = QSizePolicy::Expanding;
185 
186  QWidget* pPage = m_paramsField->widget(i);
187  pPage->setSizePolicy(policy, policy);
188  }
189  Q_EMIT(functionChanged());
190 }
191 
QLabel * m_labelFunction
const PlatformStyle * m_platformStyle
QStringList getParamValue(int paramID)
getParamValue Get the value of the parameter with the id from the currently selected function ...
ABIFunctionField(const PlatformStyle *platformStyle, FunctionType type, QWidget *parent=0)
ABIFunctionField Constructor.
QStackedWidget * m_paramsField
ContractABI * m_contractABI
QVector< int > m_abiFunctionList
std::vector< FunctionABI > functions
Definition: contractabi.h:205
std::vector< std::vector< std::string > > getValuesVector()
getValuesVector Get params values vector List of all parameters that can be sent to a function...
void setContractABI(ContractABI *contractABI)
setContractABI Set the contract ABI (list of functions from the contract)
QComboBox * m_comboBoxFunc
FunctionType
The FunctionType enum Function type to display.
FunctionType m_functionType
const char * name
Definition: rest.cpp:36
std::string type
Definition: contractabi.h:181
int getSelectedFunction() const
getSelectedFunction Get the ABI for the selected function from the contract
void updateABIFunctionField()
updateABIFunctionField Populate the GUI control with functions
The ABIParamsField class List of parameters for contract function.
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
QList< QStringList > getParamsValues()
getParamsValues Get the values of the whole list of input parameters for the selected function ...
void updateParamsField(const FunctionABI &function)
updateParamsField Populate the GUI control with function parameters
void clear()
clear Clear the GUI control
void paintEvent(QPaintEvent *)