Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

addressbookpage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2019 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #if defined(HAVE_CONFIG_H)
7 #include <config/dash-config.h>
8 #endif
9 
10 #include <qt/addressbookpage.h>
11 #include <qt/forms/ui_addressbookpage.h>
12 
13 #include <qt/addresstablemodel.h>
14 #include <qt/bitcoingui.h>
15 #include <qt/csvmodelwriter.h>
16 #include <qt/editaddressdialog.h>
17 #include <qt/guiutil.h>
18 #include <qt/optionsmodel.h>
19 #include <qt/qrdialog.h>
20 
21 #include <QIcon>
22 #include <QMenu>
23 #include <QMessageBox>
24 #include <QSortFilterProxyModel>
25 
26 AddressBookPage::AddressBookPage(Mode _mode, Tabs _tab, QWidget *parent) :
27  QDialog(parent),
28  ui(new Ui::AddressBookPage),
29  model(0),
30  mode(_mode),
31  tab(_tab)
32 {
33  ui->setupUi(this);
34 
35  ui->showAddressQRCode->setIcon(QIcon());
36 
37  switch(mode)
38  {
39  case ForSelection:
40  switch(tab)
41  {
42  case SendingTab: setWindowTitle(tr("Choose the address to send coins to")); break;
43  case ReceivingTab: setWindowTitle(tr("Choose the address to receive coins with")); break;
44  }
45  connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
46  ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
47  ui->tableView->setFocus();
48  ui->closeButton->setText(tr("C&hoose"));
49  ui->exportButton->hide();
50  break;
51  case ForEditing:
52  switch(tab)
53  {
54  case SendingTab: setWindowTitle(tr("Sending addresses")); break;
55  case ReceivingTab: setWindowTitle(tr("Receiving addresses")); break;
56  }
57  break;
58  }
59  switch(tab)
60  {
61  case SendingTab:
62  ui->labelExplanation->setText(tr("These are your Dash addresses for sending payments. Always check the amount and the receiving address before sending coins."));
63  ui->deleteAddress->setVisible(true);
64  break;
65  case ReceivingTab:
66  ui->labelExplanation->setText(tr("These are your Dash addresses for receiving payments. It is recommended to use a new receiving address for each transaction."));
67  ui->deleteAddress->setVisible(false);
68  break;
69  }
70 
71  // Context menu actions
72  QAction *copyAddressAction = new QAction(tr("&Copy Address"), this);
73  QAction *copyLabelAction = new QAction(tr("Copy &Label"), this);
74  QAction *editAction = new QAction(tr("&Edit"), this);
75  QAction *showAddressQRCodeAction = new QAction(tr("&Show address QR code"), this);
76  deleteAction = new QAction(ui->deleteAddress->text(), this);
77 
78  // Build context menu
79  contextMenu = new QMenu(this);
80  contextMenu->addAction(copyAddressAction);
81  contextMenu->addAction(copyLabelAction);
82  contextMenu->addAction(editAction);
83  if(tab == SendingTab)
84  contextMenu->addAction(deleteAction);
85  contextMenu->addSeparator();
86  contextMenu->addAction(showAddressQRCodeAction);
87 
88  // Connect signals for context menu actions
89  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyAddress_clicked()));
90  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
91  connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
92  connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteAddress_clicked()));
93  connect(showAddressQRCodeAction, SIGNAL(triggered()), this, SLOT(on_showAddressQRCode_clicked()));
94 
95  connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
96 
97  connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(accept()));
98 
100 
102 }
103 
105 {
106  delete ui;
107 }
108 
110 {
111  this->model = _model;
112  if(!_model)
113  return;
114 
115  proxyModel = new QSortFilterProxyModel(this);
116  proxyModel->setSourceModel(_model);
117  proxyModel->setDynamicSortFilter(true);
118  proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
119  proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
120  switch(tab)
121  {
122  case ReceivingTab:
123  // Receive filter
124  proxyModel->setFilterRole(AddressTableModel::TypeRole);
125  proxyModel->setFilterFixedString(AddressTableModel::Receive);
126  break;
127  case SendingTab:
128  // Send filter
129  proxyModel->setFilterRole(AddressTableModel::TypeRole);
130  proxyModel->setFilterFixedString(AddressTableModel::Send);
131  break;
132  }
133  ui->tableView->setModel(proxyModel);
134  ui->tableView->sortByColumn(0, Qt::AscendingOrder);
135 
136  // Set column widths
137 #if QT_VERSION < 0x050000
138  ui->tableView->horizontalHeader()->setResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
139  ui->tableView->horizontalHeader()->setResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
140 #else
141  ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
142  ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
143 #endif
144 
145  connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
146  this, SLOT(selectionChanged()));
147 
148  // Select row for newly created address
149  connect(_model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(selectNewAddress(QModelIndex,int,int)));
150 
152 }
153 
155 {
157 }
158 
160 {
162 }
163 
165 {
166  if(!model)
167  return;
168 
169  if(!ui->tableView->selectionModel())
170  return;
171  QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
172  if(indexes.isEmpty())
173  return;
174 
175  EditAddressDialog dlg(
176  tab == SendingTab ?
179  dlg.setModel(model);
180  QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
181  dlg.loadRow(origIndex.row());
182  dlg.exec();
183 }
184 
186 {
187  if(!model)
188  return;
189 
190  EditAddressDialog dlg(
191  tab == SendingTab ?
194  dlg.setModel(model);
195  if(dlg.exec())
196  {
198  }
199 }
200 
202 {
203  QTableView *table = ui->tableView;
204  if(!table->selectionModel())
205  return;
206 
207  QModelIndexList indexes = table->selectionModel()->selectedRows();
208  if(!indexes.isEmpty())
209  {
210  table->model()->removeRow(indexes.at(0).row());
211  }
212 }
213 
215 {
216  QList<QModelIndex> entries = GUIUtil::getEntryData(ui->tableView, AddressTableModel::Address);
217  if (entries.empty()) {
218  return;
219  }
220 
221  QString strAddress = entries.at(0).data(Qt::EditRole).toString();
222  QRDialog* dialog = new QRDialog(this);
223  OptionsModel *model = new OptionsModel(nullptr, false);
224 
225  dialog->setModel(model);
226  dialog->setAttribute(Qt::WA_DeleteOnClose);
227  dialog->setInfo(tr("QR code"), "dash:"+strAddress, "", strAddress);
228  dialog->show();
229 }
230 
232 {
233  // Set button states based on selected tab and selection
234  QTableView *table = ui->tableView;
235  if(!table->selectionModel())
236  return;
237 
238  if(table->selectionModel()->hasSelection())
239  {
240  switch(tab)
241  {
242  case SendingTab:
243  // In sending tab, allow deletion of selection
244  ui->deleteAddress->setEnabled(true);
245  ui->deleteAddress->setVisible(true);
246  deleteAction->setEnabled(true);
247  break;
248  case ReceivingTab:
249  // Deleting receiving addresses, however, is not allowed
250  ui->deleteAddress->setEnabled(false);
251  ui->deleteAddress->setVisible(false);
252  deleteAction->setEnabled(false);
253  break;
254  }
255  ui->copyAddress->setEnabled(true);
256  ui->showAddressQRCode->setEnabled(true);
257  }
258  else
259  {
260  ui->deleteAddress->setEnabled(false);
261  ui->copyAddress->setEnabled(false);
262  ui->showAddressQRCode->setEnabled(false);
263  }
264 }
265 
266 void AddressBookPage::done(int retval)
267 {
268  QTableView *table = ui->tableView;
269  if(!table->selectionModel() || !table->model())
270  return;
271 
272  // Figure out which address was selected, and return it
273  QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
274 
275  for (const QModelIndex& index : indexes) {
276  QVariant address = table->model()->data(index);
277  returnValue = address.toString();
278  }
279 
280  if(returnValue.isEmpty())
281  {
282  // If no address entry selected, return rejected
283  retval = Rejected;
284  }
285 
286  QDialog::done(retval);
287 }
288 
290 {
291  // CSV is currently the only supported format
292  QString filename = GUIUtil::getSaveFileName(this,
293  tr("Export Address List"), QString(),
294  tr("Comma separated file (*.csv)"), nullptr);
295 
296  if (filename.isNull())
297  return;
298 
299  CSVModelWriter writer(filename);
300 
301  // name, column, role
302  writer.setModel(proxyModel);
303  writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
304  writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
305 
306  if(!writer.write()) {
307  QMessageBox::critical(this, tr("Exporting Failed"),
308  tr("There was an error trying to save the address list to %1. Please try again.").arg(filename));
309  }
310 }
311 
312 void AddressBookPage::contextualMenu(const QPoint &point)
313 {
314  QModelIndex index = ui->tableView->indexAt(point);
315  if(index.isValid())
316  {
317  contextMenu->exec(QCursor::pos());
318  }
319 }
320 
321 void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int /*end*/)
322 {
323  QModelIndex idx = proxyModel->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
324  if(idx.isValid() && (idx.data(Qt::EditRole).toString() == newAddressToSelect))
325  {
326  // Select row of newly created address, once
327  ui->tableView->setFocus();
328  ui->tableView->selectRow(idx.row());
329  newAddressToSelect.clear();
330  }
331 }
void on_newAddress_clicked()
Create a new address for receiving coins and / or add a new address book entry.
void onCopyLabelAction()
Copy label of currently selected address entry to clipboard (no button)
void addColumn(const QString &title, int column, int role=Qt::EditRole)
void on_showAddressQRCode_clicked()
Show QR code for the currently selected address.
void setInfo(QString strWindowtitle, QString strQRCode, QString strTextInfo, QString strQRCodeTitle)
Definition: qrdialog.cpp:128
QList< QModelIndex > getEntryData(QAbstractItemView *view, int column)
Return a field of the currently selected entry as a QString.
Definition: guiutil.cpp:514
QModelIndex index(int row, int column, const QModelIndex &parent) const
void setModel(AddressTableModel *model)
void setModel(OptionsModel *model)
Definition: qrdialog.cpp:117
void onEditAction()
Edit currently selected address entry (no button)
AddressTableModel * model
QSortFilterProxyModel * proxyModel
void on_exportButton_clicked()
Export button clicked.
Open address book for editing.
Open address book to pick address.
Export a Qt table model to a CSV file.
QString newAddressToSelect
AddressBookPage(Mode mode, Tabs tab, QWidget *parent)
Ui::AddressBookPage * ui
static const QString Send
Specifies send address.
void selectNewAddress(const QModelIndex &parent, int begin, int)
New entry/entries were added to address table.
QAction * deleteAction
bool done
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
void setModel(AddressTableModel *model)
void done(int retval)
void on_copyAddress_clicked()
Copy address of currently selected address entry to clipboard.
Widget that shows a list of sending or receiving addresses.
Qt model of the address book in the core.
void disableMacFocusRect(const QWidget *w)
Disable the OS default focus rect for macOS because we have custom focus rects set in the css files...
Definition: guiutil.cpp:1789
void selectionChanged()
Set button states based on selected tab and selection.
void setModel(const QAbstractItemModel *model)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:25
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:521
static const QString Receive
Specifies receive address.
Dialog for editing an address and associated information.
QString getAddress() const
User specified label.
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:501
void contextualMenu(const QPoint &point)
Spawn contextual menu (right mouse menu) for address book entry.
void on_deleteAddress_clicked()
Delete currently selected address entry.
bool write()
Perform export of the model to CSV.
Type of address (Send or Receive)
Released under the MIT license