Fabcoin Core  0.16.2
P2P Digital Currency
contractbookpage.cpp
Go to the documentation of this file.
1 #include <contractbookpage.h>
2 #include <ui_contractbookpage.h>
3 
4 #include <contracttablemodel.h>
5 #include <csvmodelwriter.h>
7 #include <guiutil.h>
8 #include <platformstyle.h>
9 #include <styleSheet.h>
10 
11 #include <QIcon>
12 #include <QMenu>
13 #include <QSortFilterProxyModel>
14 
15 ContractBookPage::ContractBookPage(const PlatformStyle *platformStyle, QWidget *parent) :
16  QDialog(parent),
17  ui(new Ui::ContractBookPage),
18  model(0)
19 {
20  ui->setupUi(this);
21 
22  SetObjectStyleSheet(ui->tableView, StyleSheetNames::TableViewLight);
23 
24  if (!platformStyle->getImagesOnButtons()) {
25  ui->newContractInfo->setIcon(QIcon());
26  ui->copyAddress->setIcon(QIcon());
27  ui->deleteContractInfo->setIcon(QIcon());
28  ui->exportButton->setIcon(QIcon());
29  } else {
30  ui->newContractInfo->setIcon(platformStyle->MultiStatesIcon(":/icons/add", PlatformStyle::PushButton, 0x5a5a5d));
31  ui->copyAddress->setIcon(platformStyle->MultiStatesIcon(":/icons/editcopy", PlatformStyle::PushButton, 0x5a5a5d));
32  ui->deleteContractInfo->setIcon(platformStyle->MultiStatesIcon(":/icons/remove", PlatformStyle::PushButton, 0x5a5a5d));
33  ui->exportButton->setIcon(platformStyle->MultiStatesIcon(":/icons/export"));
34  }
35 
36  SetObjectStyleSheet(ui->newContractInfo, StyleSheetNames::ButtonWhite);
37  SetObjectStyleSheet(ui->copyAddress, StyleSheetNames::ButtonWhite);
38  SetObjectStyleSheet(ui->deleteContractInfo, StyleSheetNames::ButtonWhite);
39  SetObjectStyleSheet(ui->exportButton, StyleSheetNames::ButtonBlue);
40  SetObjectStyleSheet(ui->chooseContractInfo, StyleSheetNames::ButtonBlue);
41 
42  setWindowTitle(tr("Choose the contract for send/call"));
43  ui->labelExplanation->setText(tr("These are your saved contracts. Always check the contract address and the ABI before sending/calling."));
44 
45  // Context menu actions
46  QAction *copyAddressAction = new QAction(tr("Copy &Address"), this);
47  QAction *copyNameAction = new QAction(tr("Copy &Name"), this);
48  QAction *copyABIAction = new QAction(tr("Copy &Interface"), this);
49  QAction *editAction = new QAction(tr("&Edit"), this);
50  QAction *deleteAction = new QAction(tr("&Delete"), this);
51 
52  // Build context menu
53  contextMenu = new QMenu(this);
54  contextMenu->addAction(copyAddressAction);
55  contextMenu->addAction(copyNameAction);
56  contextMenu->addAction(copyABIAction);
57  contextMenu->addAction(editAction);
58  contextMenu->addAction(deleteAction);
59  contextMenu->addSeparator();
60 
61  // Connect signals for context menu actions
62  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyAddress_clicked()));
63  connect(copyNameAction, SIGNAL(triggered()), this, SLOT(onCopyNameAction()));
64  connect(copyABIAction, SIGNAL(triggered()), this, SLOT(onCopyABIAction()));
65  connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
66  connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteContractInfo_clicked()));
67 
68  connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
69  connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
70 
71  connect(ui->chooseContractInfo, SIGNAL(clicked()), this, SLOT(accept()));
72 }
73 
75 {
76  delete ui;
77 }
78 
80 {
81  this->model = _model;
82  if(!_model)
83  return;
84 
85  proxyModel = new QSortFilterProxyModel(this);
86  proxyModel->setSourceModel(_model);
87  proxyModel->setDynamicSortFilter(true);
88  proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
89 
90  ui->tableView->setModel(proxyModel);
91  ui->tableView->sortByColumn(0, Qt::AscendingOrder);
92 
93  // Set column widths
94  ui->tableView->setColumnWidth(ContractTableModel::Label, ColumnWidths::LABEL_COLUMN_WIDTH);
95  ui->tableView->setColumnWidth(ContractTableModel::Address, ColumnWidths::ADDRESS_COLUMN_WIDTH);
96 #if QT_VERSION < 0x050000
97  ui->tableView->horizontalHeader()->setResizeMode(ContractTableModel::Label, QHeaderView::Fixed);
98  ui->tableView->horizontalHeader()->setResizeMode(ContractTableModel::Address, QHeaderView::Fixed);
99  ui->tableView->horizontalHeader()->setResizeMode(ContractTableModel::ABI, QHeaderView::Stretch);
100 #else
101  ui->tableView->horizontalHeader()->setSectionResizeMode(ContractTableModel::Label, QHeaderView::Fixed);
102  ui->tableView->horizontalHeader()->setSectionResizeMode(ContractTableModel::Address, QHeaderView::Fixed);
103  ui->tableView->horizontalHeader()->setSectionResizeMode(ContractTableModel::ABI, QHeaderView::Stretch);
104 #endif
105 
106  connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
107  this, SLOT(selectionChanged()));
108 
109  // Select row for newly created contract info
110  connect(_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(selectNewContractInfo(QModelIndex,int,int)));
111 
113 }
114 
116 {
118 }
119 
121 {
123 }
124 
126 {
128 }
129 
131 {
132  if(!model)
133  return;
134 
135  if(!ui->tableView->selectionModel())
136  return;
137  QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
138  if(indexes.isEmpty())
139  return;
140 
142  dlg.setModel(model);
143  QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
144  dlg.loadRow(origIndex.row());
145  dlg.exec();
146 }
147 
149 {
150  if(!model)
151  return;
152 
154  dlg.setModel(model);
155  if(dlg.exec())
156  {
158  }
159 }
160 
162 {
163  QTableView *table = ui->tableView;
164  if(!table->selectionModel())
165  return;
166 
167  QModelIndexList indexes = table->selectionModel()->selectedRows();
168  if(!indexes.isEmpty())
169  {
170  int row = indexes.at(0).row();
171  QModelIndex index = table->model()->index(row, ContractTableModel::Address);
172  QString contractAddress = table->model()->data(index).toString();
173  QString message = tr("Are you sure you want to delete the address \"%1\" from your contract address list?");
174  if(QMessageBox::Yes == QMessageBox::question(this, tr("Delete contact address"), message.arg(contractAddress),
175  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel))
176  {
177  table->model()->removeRow(row);
178  }
179  }
180 }
181 
183 {
184  // Set button states based on selection
185  QTableView *table = ui->tableView;
186  if(!table->selectionModel())
187  return;
188 
189  if(table->selectionModel()->hasSelection())
190  {
191  ui->deleteContractInfo->setEnabled(true);
192  ui->copyAddress->setEnabled(true);
193  }
194  else
195  {
196  ui->deleteContractInfo->setEnabled(false);
197  ui->copyAddress->setEnabled(false);
198  }
199 }
200 
201 void ContractBookPage::done(int retval)
202 {
203  QTableView *table = ui->tableView;
204  if(!table->selectionModel() || !table->model())
205  return;
206 
207  // Figure out which contract info was selected, and return it
208  QModelIndexList indexes = table->selectionModel()->selectedRows(ContractTableModel::Address);
209 
210  Q_FOREACH (const QModelIndex& index, indexes) {
211  QVariant address = table->model()->data(index);
212  QVariant ABI = table->model()->index(index.row(), ContractTableModel::ABI).data();
213  addressValue = address.toString();
214  ABIValue = ABI.toString();
215  }
216 
217  if(addressValue.isEmpty())
218  {
219  // If no contract info entry selected, return rejected
220  retval = Rejected;
221  }
222 
223  QDialog::done(retval);
224 }
225 
227 {
228  // CSV is currently the only supported format
229  QString filename = GUIUtil::getSaveFileName(this,
230  tr("Export Contract List"), QString(),
231  tr("Comma separated file (*.csv)"), NULL);
232 
233  if (filename.isNull())
234  return;
235 
236  CSVModelWriter writer(filename);
237 
238  // name, column, role
239  writer.setModel(proxyModel);
240  writer.addColumn("Label", ContractTableModel::Label, Qt::EditRole);
241  writer.addColumn("Address", ContractTableModel::Address, Qt::EditRole);
242  writer.addColumn("ABI", ContractTableModel::ABI, Qt::EditRole);
243 
244  if(!writer.write()) {
245  QMessageBox::critical(this, tr("Exporting Failed"),
246  tr("There was an error trying to save the address list to %1. Please try again.").arg(filename));
247  }
248 }
249 
250 void ContractBookPage::contextualMenu(const QPoint &point)
251 {
252  QModelIndex index = ui->tableView->indexAt(point);
253  if(index.isValid())
254  {
255  contextMenu->exec(QCursor::pos());
256  }
257 }
258 
259 void ContractBookPage::selectNewContractInfo(const QModelIndex &parent, int begin, int)
260 {
261  QModelIndex idx = proxyModel->mapFromSource(model->index(begin, ContractTableModel::Address, parent));
262  if(idx.isValid() && (idx.data(Qt::EditRole).toString() == newContractInfoToSelect))
263  {
264  // Select row of newly created address, once
265  ui->tableView->setFocus();
266  ui->tableView->selectRow(idx.row());
267  newContractInfoToSelect.clear();
268  }
269 }
QPushButton * exportButton
void addColumn(const QString &title, int column, int role=Qt::EditRole)
void contextualMenu(const QPoint &point)
Spawn contextual menu (right mouse menu) for contract info book entry.
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
Dialog for editing a contract information.
void done(int retval)
void on_newContractInfo_clicked()
Create a new contract info.
void selectionChanged()
Set button states based on selection.
QPushButton * deleteContractInfo
void selectNewContractInfo(const QModelIndex &parent, int begin, int)
New entry/entries were added to contract info table.
Export a Qt table model to a CSV file.
void onCopyNameAction()
Copy label of currently selected contract info entry to clipboard (no button)
void on_copyAddress_clicked()
Copy address of currently selected contract info entry to clipboard.
QPushButton * newContractInfo
ContractTableModel * model
void on_exportButton_clicked()
Export button clicked.
void setModel(ContractTableModel *model)
void setModel(ContractTableModel *model)
QPushButton * chooseContractInfo
QModelIndex index(int row, int column, const QModelIndex &parent) const
Qt model of the contract book in the core.
QSortFilterProxyModel * proxyModel
void setModel(const QAbstractItemModel *model)
Ui::ContractBookPage * ui
QIcon MultiStatesIcon(const QString &resourcename, StateType type=NavBar, QColor color=Qt::white, QColor colorAlt=0x2d2d2d) const
Get multi-states icon.
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
Definition: guiutil.cpp:309
void onCopyABIAction()
Copy ABI of currently selected contract info entry to clipboard (no button)
void on_deleteContractInfo_clicked()
Delete currently selected contract info entry.
ContractBookPage(const PlatformStyle *platformStyle, QWidget *parent=0)
bool getImagesOnButtons() const
Definition: platformstyle.h:21
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
void setupUi(QWidget *ContractBookPage)
void copyEntryData(QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:277
User specified label.
void onEditAction()
Edit currently selected contract info entry (no button)
uint8_t const * data
Definition: sha3.h:19
bool write()
Perform export of the model to CSV.
QString newContractInfoToSelect