Fabcoin Core  0.16.2
P2P Digital Currency
contractresult.cpp
Go to the documentation of this file.
1 #include <contractresult.h>
2 #include <ui_contractresult.h>
3 #include <guiconstants.h>
4 #include <contractabi.h>
5 #include <styleSheet.h>
6 
7 #include <QMessageBox>
8 
10  QStackedWidget(parent),
11  ui(new Ui::ContractResult)
12 {
13  ui->setupUi(this);
14 }
15 
17 {
18  delete ui;
19 }
20 
21 void ContractResult::setResultData(QVariant result, FunctionABI function, QList<QStringList> paramValues, ContractTxType type)
22 {
23  switch (type) {
24  case CreateResult:
25  updateCreateResult(result);
26  setCurrentWidget(ui->pageCreateOrSendToResult);
27  break;
28 
29  case SendToResult:
30  updateSendToResult(result);
31  setCurrentWidget(ui->pageCreateOrSendToResult);
32  break;
33 
34  case CallResult:
35  updateCallResult(result, function, paramValues);
36  setCurrentWidget(ui->pageCallResult);
37  break;
38 
39  default:
40  break;
41  }
42 }
43 
44 void ContractResult::setParamsData(FunctionABI function, QList<QStringList> paramValues)
45 {
46  // Remove previous widget from scroll area
47  QWidget *scrollWidget = ui->scrollAreaParams->widget();
48  if(scrollWidget)
49  scrollWidget->deleteLater();
50 
51  // Don't show empty list
52  if(function.inputs.size() == 0)
53  {
54  ui->scrollAreaParams->setVisible(false);
55  return;
56  }
57 
58  QWidget *widgetParams = new QWidget(this);
59  widgetParams->setObjectName("scrollAreaWidgetContents");
60 
61  QVBoxLayout *mainLayout = new QVBoxLayout(widgetParams);
62  mainLayout->setSpacing(6);
63  mainLayout->setContentsMargins(0,0,30,0);
64 
65  // Add rows with params and values sent
66  int i = 0;
67  for(std::vector<ParameterABI>::const_iterator param = function.inputs.begin(); param != function.inputs.end(); ++param)
68  {
69  QHBoxLayout *hLayout = new QHBoxLayout();
70  hLayout->setSpacing(10);
71  hLayout->setContentsMargins(0,0,0,0);
72  QVBoxLayout *vNameLayout = new QVBoxLayout();
73  vNameLayout->setSpacing(3);
74  vNameLayout->setContentsMargins(0,0,0,0);
75  QVBoxLayout *paramValuesLayout = new QVBoxLayout();
76  paramValuesLayout->setSpacing(3);
77  paramValuesLayout->setContentsMargins(0,0,0,0);
78 
79  QLabel *paramName = new QLabel(this);
80  paramName->setFixedWidth(160);
81  paramName->setFixedHeight(19);
82  QFontMetrics metrix(paramName->font());
83  int width = paramName->width() + 10;
84  QString text(QString("%2 <b>%1").arg(QString::fromStdString(param->name)).arg(QString::fromStdString(param->type)));
85  QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
86  paramName->setText(clippedText);
87  paramName->setToolTip(QString("%2 %1").arg(QString::fromStdString(param->name)).arg(QString::fromStdString(param->type)));
88 
89  vNameLayout->addWidget(paramName);
90  hLayout->addLayout(vNameLayout);
91  QStringList listValues = paramValues[i];
92  if(listValues.size() > 0)
93  {
94  int spacerSize = 0;
95  for(int j = 0; j < listValues.count(); j++)
96  {
97  QLineEdit *paramValue = new QLineEdit(this);
98  paramValue->setReadOnly(true);
99  paramValue->setText(listValues[j]);
100  paramValuesLayout->addWidget(paramValue);
101  if(j > 0)
102  spacerSize += 30; // Line edit height + spacing
103  }
104  if(spacerSize > 0)
105  vNameLayout->addSpacerItem(new QSpacerItem(20, spacerSize, QSizePolicy::Fixed, QSizePolicy::Fixed));
106 
107  hLayout->addLayout(paramValuesLayout);
108  }
109  else
110  {
111  hLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed));
112  }
113 
114  mainLayout->addLayout(hLayout);
115  i++;
116  }
117  widgetParams->setLayout(mainLayout);
118  widgetParams->adjustSize();
119  if(widgetParams->sizeHint().height() < 70)
120  ui->scrollAreaParams->setMaximumHeight(widgetParams->sizeHint().height() + 2);
121  else
122  ui->scrollAreaParams->setMaximumHeight(140);
123  ui->scrollAreaParams->setWidget(widgetParams);
124  ui->scrollAreaParams->setVisible(true);
125 }
126 
128 {
129  ui->lineEditContractAddress->setVisible(true);
130  ui->labelContractAddress->setVisible(true);
131 
132  QVariantMap variantMap = result.toMap();
133 
134  ui->lineEditTxID->setText(variantMap.value("txid").toString());
135  ui->lineEditSenderAddress->setText(variantMap.value("sender").toString());
136  ui->lineEditHash160->setText(variantMap.value("hash160").toString());
137  ui->lineEditContractAddress->setText(variantMap.value("address").toString());
138 }
139 
141 {
142  ui->lineEditContractAddress->setVisible(false);
143  ui->labelContractAddress->setVisible(false);
144 
145  QVariantMap variantMap = result.toMap();
146 
147  ui->lineEditTxID->setText(variantMap.value("txid").toString());
148  ui->lineEditSenderAddress->setText(variantMap.value("sender").toString());
149  ui->lineEditHash160->setText(variantMap.value("hash160").toString());
150 }
151 
152 void ContractResult::updateCallResult(QVariant result, FunctionABI function, QList<QStringList> paramValues)
153 {
154  QVariantMap variantMap = result.toMap();
155  QVariantMap executionResultMap = variantMap.value("executionResult").toMap();
156 
157  ui->lineEditCallContractAddress->setText(variantMap.value("address").toString());
158  ui->lineEditFunction->setText(QString::fromStdString(function.name) + "()");
159 
160  setParamsData(function, paramValues);
161 
162  ui->lineEditCallSenderAddress->setText(variantMap.value("sender").toString());
163  std::string rawData = executionResultMap.value("output").toString().toStdString();
164  std::vector<std::vector<std::string>> values;
165  std::vector<ParameterABI::ErrorType> errors;
166  if(function.abiOut(rawData, values, errors))
167  {
168  // Remove previous widget from scroll area
169  QWidget *scrollWidget = ui->scrollAreaResult->widget();
170  if(scrollWidget)
171  scrollWidget->deleteLater();
172 
173  if(values.size() > 0)
174  {
175  QWidget *widgetResults = new QWidget(this);
176  widgetResults->setObjectName("scrollAreaWidgetContents");
177 
178  QVBoxLayout *mainLayout = new QVBoxLayout(widgetResults);
179  mainLayout->setSpacing(6);
180  mainLayout->setContentsMargins(0,6,0,6);
181  widgetResults->setLayout(mainLayout);
182 
183  for(size_t i = 0; i < values.size(); i++)
184  {
185  QHBoxLayout *hLayout = new QHBoxLayout();
186  hLayout->setSpacing(10);
187  hLayout->setContentsMargins(0,0,0,0);
188  QVBoxLayout *vNameLayout = new QVBoxLayout();
189  vNameLayout->setSpacing(3);
190  vNameLayout->setContentsMargins(0,0,0,0);
191  QVBoxLayout *paramValuesLayout = new QVBoxLayout();
192  paramValuesLayout->setSpacing(3);
193  paramValuesLayout->setContentsMargins(0,0,0,0);
194 
195  QLabel *resultName = new QLabel(this);
196  resultName->setFixedWidth(160);
197  resultName->setFixedHeight(19);
198  QFontMetrics metrix(resultName->font());
199  int width = resultName->width() + 10;
200  QString text(QString("%2 <b>%1").arg(QString::fromStdString(function.outputs[i].name)).arg(QString::fromStdString(function.outputs[i].type)));
201  QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
202  resultName->setText(clippedText);
203  resultName->setToolTip(QString("%2 %1").arg(QString::fromStdString(function.outputs[i].name)).arg(QString::fromStdString(function.outputs[i].type)));
204 
205  vNameLayout->addWidget(resultName);
206  std::vector<std::string> listValues = values[i];
207  hLayout->addLayout(vNameLayout);
208  if(listValues.size() > 0)
209  {
210  int spacerSize = 0;
211  for(size_t j = 0; j < listValues.size(); j++)
212  {
213  QLineEdit *resultValue = new QLineEdit(this);
214  resultValue->setReadOnly(true);
215  resultValue->setText(QString::fromStdString(listValues[j]));
216  paramValuesLayout->addWidget(resultValue);
217  if(j > 0)
218  spacerSize += 30; // Line edit height + spacing
219  }
220  if(spacerSize > 0)
221  vNameLayout->addSpacerItem(new QSpacerItem(20, spacerSize, QSizePolicy::Fixed, QSizePolicy::Fixed));
222  hLayout->addLayout(paramValuesLayout);
223  }
224  else
225  {
226  hLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Fixed));
227  }
228  mainLayout->addLayout(hLayout);
229  }
230  widgetResults->adjustSize();
231  if(widgetResults->sizeHint().height() < 70)
232  {
233  ui->scrollAreaResult->setMaximumHeight(widgetResults->sizeHint().height() + 2);
234  }
235  else
236  {
237  ui->scrollAreaResult->setMaximumHeight(140);
238  }
239  ui->scrollAreaResult->setWidget(widgetResults);
240  ui->groupBoxResult->setVisible(true);
241  }
242  else
243  {
244  ui->groupBoxResult->setVisible(false);
245  }
246  }
247  else
248  {
249  QString errorMessage;
250  errorMessage = function.errorMessage(errors, false);
251  QMessageBox::warning(this, tr("Create contract"), errorMessage);
252  }
253 }
QLabel * labelContractAddress
QLineEdit * lineEditCallSenderAddress
void updateCreateResult(QVariant result)
QLineEdit * lineEditTxID
QScrollArea * scrollAreaResult
QLineEdit * lineEditSenderAddress
void updateSendToResult(QVariant result)
void setResultData(QVariant result, FunctionABI function, QList< QStringList > paramValues, ContractTxType type)
QScrollArea * scrollAreaParams
QLineEdit * lineEditContractAddress
ContractResult(QWidget *parent=0)
T abiOut(bytes const &_data)
Definition: ABI.h:88
const char * name
Definition: rest.cpp:36
Ui::ContractResult * ui
void updateCallResult(QVariant result, FunctionABI function, QList< QStringList > paramValues)
QLineEdit * lineEditHash160
QLineEdit * lineEditFunction
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
QWidget * pageCreateOrSendToResult
QLineEdit * lineEditCallContractAddress
void setParamsData(FunctionABI function, QList< QStringList > paramValues)
QGroupBox * groupBoxResult
void setupUi(QStackedWidget *ContractResult)