PRCYCoin  2.0.0.7rc1
P2P Digital Currency
addressbookpage.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Copyright (c) 2014-2015 The Dash developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include "addressbookpage.h"
13 #include "ui_addressbookpage.h"
14 
15 #include "addresstablemodel.h"
16 #include "bitcoingui.h"
17 #include "csvmodelwriter.h"
18 #include "editaddressdialog.h"
19 #include "guiutil.h"
20 
21 #include <QIcon>
22 #include <QMenu>
23 #include <QMessageBox>
24 #include <QSortFilterProxyModel>
25 
26 AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget* parent) : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
27  ui(new Ui::AddressBookPage),
28  model(0),
29  mode(mode),
30  tab(tab)
31 {
32  ui->setupUi(this);
33 
34 #ifdef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
35  ui->newAddress->setIcon(QIcon());
36  ui->copyAddress->setIcon(QIcon());
37  ui->deleteAddress->setIcon(QIcon());
38  ui->exportButton->setIcon(QIcon());
39 #endif
40  switch (mode) {
41  case ForSelection:
42  switch (tab) {
43  case SendingTab:
44  setWindowTitle(tr("Choose the address to send coins to"));
45  break;
46  case ReceivingTab:
47  setWindowTitle(tr("Choose the address to receive coins with"));
48  break;
49  }
50  connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
51  ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
52  ui->tableView->setFocus();
53  ui->closeButton->setText(tr("C&hoose"));
54  ui->exportButton->hide();
55  break;
56  case ForEditing:
57  switch (tab) {
58  case SendingTab:
59  setWindowTitle(tr("Sending addresses"));
60  break;
61  case ReceivingTab:
62  setWindowTitle(tr("Receiving addresses"));
63  break;
64  }
65  break;
66  }
67  switch (tab) {
68  case SendingTab:
69  ui->labelExplanation->setText(tr("These are your PRCY addresses for sending payments. Always check the amount and the receiving address before sending coins."));
70  ui->deleteAddress->setVisible(true);
71  break;
72  case ReceivingTab:
73  ui->labelExplanation->setText(tr("These are your PRCY addresses for receiving payments. It is recommended to use a new receiving address for each transaction."));
74  ui->deleteAddress->setVisible(false);
75  break;
76  }
77 
78  // Context menu actions
79  QAction* copyAddressAction = new QAction(tr("&Copy Address"), this);
80  QAction* copyLabelAction = new QAction(tr("Copy &Label"), this);
81  QAction* editAction = new QAction(tr("&Edit"), this);
82  deleteAction = new QAction(ui->deleteAddress->text(), this);
83 
84  // Build context menu
85  contextMenu = new QMenu(this);
86  contextMenu->setAttribute(Qt::WA_DeleteOnClose);
87  contextMenu->addAction(copyAddressAction);
88  contextMenu->addAction(copyLabelAction);
89  contextMenu->addAction(editAction);
90  if (tab == SendingTab)
91  contextMenu->addAction(deleteAction);
92  contextMenu->addSeparator();
93 
94  // Connect signals for context menu actions
95  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyAddress_clicked()));
96  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
97  connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
98  connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteAddress_clicked()));
99 
100  connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
101 
102  connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(accept()));
103 }
104 
106 {
107  delete ui;
108 }
109 
111 {
112  this->model = model;
113  if (!model)
114  return;
115 
116  proxyModel = new QSortFilterProxyModel(this);
117  proxyModel->setSourceModel(model);
118  proxyModel->setDynamicSortFilter(true);
119  proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
120  proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
121  switch (tab) {
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 
135  // Set column widths
136  ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
137  ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
138 
139  connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
140  this, SLOT(selectionChanged()));
141 
142  // Select row for newly created address
143  connect(model, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(selectNewAddress(QModelIndex, int, int)));
144 
146 }
147 
149 {
151 }
152 
154 {
156 }
157 
159 {
160  if (!model)
161  return;
162 
163  if (!ui->tableView->selectionModel())
164  return;
165  QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
166  if (indexes.isEmpty())
167  return;
168 
169  EditAddressDialog dlg(
170  tab == SendingTab ?
173  this);
174  dlg.setModel(model);
175  QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
176  dlg.loadRow(origIndex.row());
177  dlg.exec();
178 }
179 
181 {
182  if (!model)
183  return;
184 
185  EditAddressDialog dlg(
186  tab == SendingTab ?
189  this);
190  dlg.setModel(model);
191  if (dlg.exec()) {
193  }
194 }
195 
197 {
198  QTableView* table = ui->tableView;
199  if (!table->selectionModel())
200  return;
201 
202  QModelIndexList indexes = table->selectionModel()->selectedRows();
203  if (!indexes.isEmpty()) {
204  table->model()->removeRow(indexes.at(0).row());
205  }
206 }
207 
209 {
210  // Set button states based on selected tab and selection
211  QTableView* table = ui->tableView;
212  if (!table->selectionModel())
213  return;
214 
215  if (table->selectionModel()->hasSelection()) {
216  switch (tab) {
217  case SendingTab:
218  // In sending tab, allow deletion of selection
219  ui->deleteAddress->setEnabled(true);
220  ui->deleteAddress->setVisible(true);
221  deleteAction->setEnabled(true);
222  break;
223  case ReceivingTab:
224  // Deleting receiving addresses, however, is not allowed
225  ui->deleteAddress->setEnabled(false);
226  ui->deleteAddress->setVisible(false);
227  deleteAction->setEnabled(false);
228  break;
229  }
230  ui->copyAddress->setEnabled(true);
231  } else {
232  ui->deleteAddress->setEnabled(false);
233  ui->copyAddress->setEnabled(false);
234  }
235 }
236 
237 void AddressBookPage::done(int retval)
238 {
239  QTableView* table = ui->tableView;
240  if (!table->selectionModel() || !table->model())
241  return;
242 
243  // Figure out which address was selected, and return it
244  QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
245 
246  Q_FOREACH (QModelIndex index, indexes) {
247  QVariant address = table->model()->data(index);
248  returnValue = address.toString();
249  }
250 
251  if (returnValue.isEmpty()) {
252  // If no address entry selected, return rejected
253  retval = Rejected;
254  }
255 
256  QDialog::done(retval);
257 }
258 
260 {
261  // CSV is currently the only supported format
262  QString filename = GUIUtil::getSaveFileName(this,
263  tr("Export Address List"), QString(),
264  tr("Comma separated file (*.csv)"), NULL);
265 
266  if (filename.isNull())
267  return;
268 
269  CSVModelWriter writer(filename);
270 
271  // name, column, role
272  writer.setModel(proxyModel);
273  writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
274  writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
275 
276  if (!writer.write()) {
277  QMessageBox::critical(this, tr("Exporting Failed"),
278  tr("There was an error trying to save the address list to %1. Please try again.").arg(filename));
279  }
280 }
281 
282 void AddressBookPage::contextualMenu(const QPoint& point)
283 {
284  QModelIndex index = ui->tableView->indexAt(point);
285  if (index.isValid()) {
286  contextMenu->exec(QCursor::pos());
287  }
288 }
289 
290 void AddressBookPage::selectNewAddress(const QModelIndex& parent, int begin, int /*end*/)
291 {
292  QModelIndex idx = proxyModel->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
293  if (idx.isValid() && (idx.data(Qt::EditRole).toString() == newAddressToSelect)) {
294  // Select row of newly created address, once
295  ui->tableView->setFocus();
296  ui->tableView->selectRow(idx.row());
297  newAddressToSelect.clear();
298  }
299 }
AddressBookPage::setModel
void setModel(AddressTableModel *model)
Definition: addressbookpage.cpp:110
AddressBookPage::SendingTab
@ SendingTab
Definition: addressbookpage.h:34
EditAddressDialog::loadRow
void loadRow(int row)
Definition: editaddressdialog.cpp:64
csvmodelwriter.h
AddressBookPage::on_deleteAddress_clicked
void on_deleteAddress_clicked()
Delete currently selected address entry.
Definition: addressbookpage.cpp:196
AddressBookPage::proxyModel
QSortFilterProxyModel * proxyModel
Definition: addressbookpage.h:58
AddressBookPage::newAddressToSelect
QString newAddressToSelect
Definition: addressbookpage.h:61
AddressBookPage::on_exportButton_clicked
void on_exportButton_clicked()
Export button clicked.
Definition: addressbookpage.cpp:259
AddressBookPage::contextualMenu
void contextualMenu(const QPoint &point)
Spawn contextual menu (right mouse menu) for address book entry.
Definition: addressbookpage.cpp:282
addressbookpage.h
CSVModelWriter::write
bool write()
Perform export of the model to CSV.
Definition: csvmodelwriter.cpp:48
EditAddressDialog::EditReceivingAddress
@ EditReceivingAddress
Definition: editaddressdialog.h:31
CSVModelWriter::addColumn
void addColumn(const QString &title, int column, int role=Qt::EditRole)
Definition: csvmodelwriter.cpp:21
AddressTableModel
Qt model of the address book in the core.
Definition: addresstablemodel.h:19
AddressTableModel::Receive
static const QString Receive
Specifies receive address.
Definition: addresstablemodel.h:47
AddressBookPage::on_newAddress_clicked
void on_newAddress_clicked()
Create a new address for receiving coins and / or add a new address book entry.
Definition: addressbookpage.cpp:180
AddressBookPage::contextMenu
QMenu * contextMenu
Definition: addressbookpage.h:59
EditAddressDialog::setModel
void setModel(AddressTableModel *model)
Definition: editaddressdialog.cpp:53
EditAddressDialog::EditSendingAddress
@ EditSendingAddress
Definition: editaddressdialog.h:32
AddressBookPage::Mode
Mode
Definition: addressbookpage.h:38
AddressBookPage::onEditAction
void onEditAction()
Edit currently selected address entry (no button)
Definition: addressbookpage.cpp:158
CSVModelWriter
Export a Qt table model to a CSV file.
Definition: csvmodelwriter.h:18
AddressBookPage::on_copyAddress_clicked
void on_copyAddress_clicked()
Copy address of currently selected address entry to clipboard.
Definition: addressbookpage.cpp:148
EditAddressDialog
Dialog for editing an address and associated information.
Definition: editaddressdialog.h:23
AddressBookPage::model
AddressTableModel * model
Definition: addressbookpage.h:54
prcycoin-config.h
AddressBookPage::AddressBookPage
AddressBookPage(Mode mode, Tabs tab, QWidget *parent)
Definition: addressbookpage.cpp:26
AddressBookPage::returnValue
QString returnValue
Definition: addressbookpage.h:57
AddressTableModel::index
QModelIndex index(int row, int column, const QModelIndex &parent) const
Definition: addresstablemodel.cpp:303
CSVModelWriter::setModel
void setModel(const QAbstractItemModel *model)
Definition: csvmodelwriter.cpp:16
AddressBookPage::selectionChanged
void selectionChanged()
Set button states based on selected tab and selection.
Definition: addressbookpage.cpp:208
AddressBookPage::ui
Ui::AddressBookPage * ui
Definition: addressbookpage.h:53
AddressBookPage
Widget that shows a list of sending or receiving addresses.
Definition: addressbookpage.h:28
AddressBookPage::selectNewAddress
void selectNewAddress(const QModelIndex &parent, int begin, int)
New entry/entries were added to address table.
Definition: addressbookpage.cpp:290
AddressBookPage::done
void done(int retval)
Definition: addressbookpage.cpp:237
GUIUtil::copyEntryData
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:215
guiutil.h
AddressTableModel::Label
@ Label
User specified label.
Definition: addresstablemodel.h:28
AddressBookPage::deleteAction
QAction * deleteAction
Definition: addressbookpage.h:60
EditAddressDialog::NewSendingAddress
@ NewSendingAddress
Definition: editaddressdialog.h:30
AddressBookPage::~AddressBookPage
~AddressBookPage()
Definition: addressbookpage.cpp:105
editaddressdialog.h
GUIUtil::getSaveFileName
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:239
Ui
Definition: 2faconfirmdialog.h:7
AddressTableModel::TypeRole
@ TypeRole
Type of address (Send or Receive)
Definition: addresstablemodel.h:33
AddressBookPage::tab
Tabs tab
Definition: addressbookpage.h:56
bitcoingui.h
EditAddressDialog::getAddress
QString getAddress() const
Definition: editaddressdialog.cpp:131
AddressTableModel::Send
static const QString Send
Specifies send address.
Definition: addresstablemodel.h:46
AddressBookPage::onCopyLabelAction
void onCopyLabelAction()
Copy label of currently selected address entry to clipboard (no button)
Definition: addressbookpage.cpp:153
AddressBookPage::mode
Mode mode
Definition: addressbookpage.h:55
addresstablemodel.h
AddressBookPage::Tabs
Tabs
Definition: addressbookpage.h:33
AddressBookPage::ForEditing
@ ForEditing
Open address book for editing.
Definition: addressbookpage.h:40
AddressTableModel::Address
@ Address
Bitcoin address.
Definition: addresstablemodel.h:29
AddressBookPage::ForSelection
@ ForSelection
Open address book to pick address.
Definition: addressbookpage.h:39
EditAddressDialog::NewReceivingAddress
@ NewReceivingAddress
Definition: editaddressdialog.h:29
AddressBookPage::ReceivingTab
@ ReceivingTab
Definition: addressbookpage.h:35