Fabcoin Core  0.16.2
P2P Digital Currency
editcontractinfodialog.cpp
Go to the documentation of this file.
3 
4 #include <contracttablemodel.h>
5 #include <contractabi.h>
6 #include <styleSheet.h>
7 
8 #include <QDataWidgetMapper>
9 #include <QMessageBox>
10 #include <QRegularExpressionValidator>
11 #include <QPushButton>
12 
14  QDialog(parent),
15  ui(new Ui::EditContractInfoDialog),
16  mapper(0),
17  mode(_mode),
18  model(0),
19  m_contractABI(0)
20 {
21  m_contractABI = new ContractABI();
22 
23  ui->setupUi(this);
24 
25  SetObjectStyleSheet(ui->buttonBox->button(QDialogButtonBox::Cancel), StyleSheetNames::ButtonWhite);
26  SetObjectStyleSheet(ui->buttonBox->button(QDialogButtonBox::Ok), StyleSheetNames::ButtonBlue);
27 
28  switch(mode)
29  {
30  case NewContractInfo:
31  setWindowTitle(tr("New contract info"));
32  break;
33  case EditContractInfo:
34  setWindowTitle(tr("Edit contract info"));
35  break;
36  }
37 
38  mapper = new QDataWidgetMapper(this);
39  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
40 
41  connect(ui->ABIEdit, SIGNAL(textChanged()), SLOT(on_newContractABI()));
42 
43  // Set contract address validator
44  QRegularExpression regEx;
45  regEx.setPattern(paternAddress);
46  QRegularExpressionValidator *addressValidator = new QRegularExpressionValidator(ui->addressEdit);
47  addressValidator->setRegularExpression(regEx);
48  ui->addressEdit->setCheckValidator(addressValidator);
50 }
51 
53 {
54  delete m_contractABI;
55  delete ui;
56 }
57 
59 {
61  return ui->addressEdit->isValid();
62 }
63 
65 {
67  return ui->ABIEdit->isValid();
68 }
69 
71 {
72  bool dataValid = true;
73 
75  dataValid = false;
76  if(!isValidInterfaceABI())
77  dataValid = false;
78 
79  return dataValid;
80 }
81 
83 {
84  this->model = _model;
85  if(!_model)
86  return;
87 
88  mapper->setModel(_model);
91  mapper->addMapping(ui->ABIEdit, ContractTableModel::ABI, "plainText");
92 }
93 
95 {
96  mapper->setCurrentIndex(row);
97 }
98 
100 {
101  if(!model)
102  return false;
103 
105  switch(mode)
106  {
107  case NewContractInfo:
108  address = model->addRow(
109  ui->labelEdit->text(),
110  ui->addressEdit->text(),
111  ui->ABIEdit->toPlainText());
112  if(!address.isEmpty())
113  this->ABI = ui->ABIEdit->toPlainText();
114  break;
115  case EditContractInfo:
116  if(mapper->submit())
117  {
118  this->address = ui->addressEdit->text();
119  this->ABI = ui->ABIEdit->toPlainText();
120  }
121  break;
122  }
124  return !address.isEmpty() && !editError;
125 }
126 
128 {
129  if(isDataValid())
130  {
131  if(!model)
132  return;
133 
134  if(!saveCurrentRow())
135  {
136  switch(model->getEditStatus())
137  {
139  // Failed with unknown reason. Just reject.
140  break;
142  // No changes were made during edit operation. Just reject.
143  break;
145  QMessageBox::warning(this, windowTitle(),
146  tr("The entered address \"%1\" is already in the contract book.").arg(ui->addressEdit->text()),
147  QMessageBox::Ok, QMessageBox::Ok);
148  break;
149 
150  }
151  return;
152  }
153  QDialog::accept();
154  }
155 }
156 
158 {
159  std::string json_data = ui->ABIEdit->toPlainText().toStdString();
160  if(!m_contractABI->loads(json_data))
161  {
162  ui->ABIEdit->setIsValidManually(false);
163  }
164  else
165  {
166  ui->ABIEdit->setIsValidManually(true);
167  }
168  m_contractABI->clean();
169 }
170 
172 {
173  return address;
174 }
175 
176 void EditContractInfoDialog::setAddress(const QString &_address)
177 {
178  this->address = _address;
179  ui->addressEdit->setText(_address);
180 }
181 
183 {
184  return ABI;
185 }
186 
187 void EditContractInfoDialog::setABI(const QString &_ABI)
188 {
189  this->ABI = _ABI;
190  ui->ABIEdit->setText(_ABI);
191 }
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
EditContractInfoDialog(Mode mode, QWidget *parent=0)
Address already in contract book.
Dialog for editing a contract information.
void setABI(const QString &ABI)
QString addRow(const QString &label, const QString &address, const QString &abi)
void setupUi(QDialog *EditContractInfoDialog)
evm_mode mode
Definition: SmartVM.cpp:47
#define paternAddress
Definition: contractabi.h:11
void setIsValidManually(bool value)
ContractTableModel * model
No changes were made during edit operation.
void setEmptyIsValid(bool value)
QDataWidgetMapper * mapper
void setModel(ContractTableModel *model)
Qt model of the contract book in the core.
void setCheckValidator(const QValidator *v)
bool loads(const std::string &json_data)
Definition: contractabi.cpp:54
User specified label.
EditStatus getEditStatus() const
Ui::EditContractInfoDialog * ui
void setAddress(const QString &address)