You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addressbookpage.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright (c) 2011-2015 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #if defined(HAVE_CONFIG_H)
  5. #include "config/bitcoin-config.h"
  6. #endif
  7. #include "addressbookpage.h"
  8. #include "ui_addressbookpage.h"
  9. #include "addresstablemodel.h"
  10. #include "bitcoingui.h"
  11. #include "csvmodelwriter.h"
  12. #include "editaddressdialog.h"
  13. #include "guiutil.h"
  14. #include "platformstyle.h"
  15. #include <QIcon>
  16. #include <QMenu>
  17. #include <QMessageBox>
  18. #include <QSortFilterProxyModel>
  19. AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode mode, Tabs tab, QWidget *parent) :
  20. QDialog(parent),
  21. ui(new Ui::AddressBookPage),
  22. model(0),
  23. mode(mode),
  24. tab(tab)
  25. {
  26. ui->setupUi(this);
  27. if (!platformStyle->getImagesOnButtons()) {
  28. ui->newAddress->setIcon(QIcon());
  29. ui->copyAddress->setIcon(QIcon());
  30. ui->deleteAddress->setIcon(QIcon());
  31. ui->exportButton->setIcon(QIcon());
  32. } else {
  33. ui->newAddress->setIcon(platformStyle->SingleColorIcon(":/icons/add"));
  34. ui->copyAddress->setIcon(platformStyle->SingleColorIcon(":/icons/editcopy"));
  35. ui->deleteAddress->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
  36. ui->exportButton->setIcon(platformStyle->SingleColorIcon(":/icons/export"));
  37. }
  38. switch(mode)
  39. {
  40. case ForSelection:
  41. switch(tab)
  42. {
  43. case SendingTab: setWindowTitle(tr("Choose the address to send coins to")); break;
  44. case ReceivingTab: setWindowTitle(tr("Choose the address to receive coins with")); break;
  45. }
  46. connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
  47. ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  48. ui->tableView->setFocus();
  49. ui->closeButton->setText(tr("C&hoose"));
  50. ui->exportButton->hide();
  51. break;
  52. case ForEditing:
  53. switch(tab)
  54. {
  55. case SendingTab: setWindowTitle(tr("Sending addresses")); break;
  56. case ReceivingTab: setWindowTitle(tr("Receiving addresses")); break;
  57. }
  58. break;
  59. }
  60. switch(tab)
  61. {
  62. case SendingTab:
  63. ui->labelExplanation->setText(tr("These are your Bitcoin addresses for sending payments. Always check the amount and the receiving address before sending coins."));
  64. ui->deleteAddress->setVisible(true);
  65. break;
  66. case ReceivingTab:
  67. ui->labelExplanation->setText(tr("These are your Bitcoin addresses for receiving payments. It is recommended to use a new receiving address for each transaction."));
  68. ui->deleteAddress->setVisible(false);
  69. break;
  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. deleteAction = new QAction(ui->deleteAddress->text(), this);
  76. // Build context menu
  77. contextMenu = new QMenu();
  78. contextMenu->addAction(copyAddressAction);
  79. contextMenu->addAction(copyLabelAction);
  80. contextMenu->addAction(editAction);
  81. if(tab == SendingTab)
  82. contextMenu->addAction(deleteAction);
  83. contextMenu->addSeparator();
  84. // Connect signals for context menu actions
  85. connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyAddress_clicked()));
  86. connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
  87. connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
  88. connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteAddress_clicked()));
  89. connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
  90. connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(accept()));
  91. }
  92. AddressBookPage::~AddressBookPage()
  93. {
  94. delete ui;
  95. }
  96. void AddressBookPage::setModel(AddressTableModel *model)
  97. {
  98. this->model = model;
  99. if(!model)
  100. return;
  101. proxyModel = new QSortFilterProxyModel(this);
  102. proxyModel->setSourceModel(model);
  103. proxyModel->setDynamicSortFilter(true);
  104. proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
  105. proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
  106. switch(tab)
  107. {
  108. case ReceivingTab:
  109. // Receive filter
  110. proxyModel->setFilterRole(AddressTableModel::TypeRole);
  111. proxyModel->setFilterFixedString(AddressTableModel::Receive);
  112. break;
  113. case SendingTab:
  114. // Send filter
  115. proxyModel->setFilterRole(AddressTableModel::TypeRole);
  116. proxyModel->setFilterFixedString(AddressTableModel::Send);
  117. break;
  118. }
  119. ui->tableView->setModel(proxyModel);
  120. ui->tableView->sortByColumn(0, Qt::AscendingOrder);
  121. // Set column widths
  122. #if QT_VERSION < 0x050000
  123. ui->tableView->horizontalHeader()->setResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
  124. ui->tableView->horizontalHeader()->setResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
  125. #else
  126. ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Label, QHeaderView::Stretch);
  127. ui->tableView->horizontalHeader()->setSectionResizeMode(AddressTableModel::Address, QHeaderView::ResizeToContents);
  128. #endif
  129. connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  130. this, SLOT(selectionChanged()));
  131. // Select row for newly created address
  132. connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(selectNewAddress(QModelIndex,int,int)));
  133. selectionChanged();
  134. }
  135. void AddressBookPage::on_copyAddress_clicked()
  136. {
  137. GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
  138. }
  139. void AddressBookPage::onCopyLabelAction()
  140. {
  141. GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
  142. }
  143. void AddressBookPage::onEditAction()
  144. {
  145. if(!model)
  146. return;
  147. if(!ui->tableView->selectionModel())
  148. return;
  149. QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
  150. if(indexes.isEmpty())
  151. return;
  152. EditAddressDialog dlg(
  153. tab == SendingTab ?
  154. EditAddressDialog::EditSendingAddress :
  155. EditAddressDialog::EditReceivingAddress, this);
  156. dlg.setModel(model);
  157. QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
  158. dlg.loadRow(origIndex.row());
  159. dlg.exec();
  160. }
  161. void AddressBookPage::on_newAddress_clicked()
  162. {
  163. if(!model)
  164. return;
  165. EditAddressDialog dlg(
  166. tab == SendingTab ?
  167. EditAddressDialog::NewSendingAddress :
  168. EditAddressDialog::NewReceivingAddress, this);
  169. dlg.setModel(model);
  170. if(dlg.exec())
  171. {
  172. newAddressToSelect = dlg.getAddress();
  173. }
  174. }
  175. void AddressBookPage::on_deleteAddress_clicked()
  176. {
  177. QTableView *table = ui->tableView;
  178. if(!table->selectionModel())
  179. return;
  180. QModelIndexList indexes = table->selectionModel()->selectedRows();
  181. if(!indexes.isEmpty())
  182. {
  183. table->model()->removeRow(indexes.at(0).row());
  184. }
  185. }
  186. void AddressBookPage::selectionChanged()
  187. {
  188. // Set button states based on selected tab and selection
  189. QTableView *table = ui->tableView;
  190. if(!table->selectionModel())
  191. return;
  192. if(table->selectionModel()->hasSelection())
  193. {
  194. switch(tab)
  195. {
  196. case SendingTab:
  197. // In sending tab, allow deletion of selection
  198. ui->deleteAddress->setEnabled(true);
  199. ui->deleteAddress->setVisible(true);
  200. deleteAction->setEnabled(true);
  201. break;
  202. case ReceivingTab:
  203. // Deleting receiving addresses, however, is not allowed
  204. ui->deleteAddress->setEnabled(false);
  205. ui->deleteAddress->setVisible(false);
  206. deleteAction->setEnabled(false);
  207. break;
  208. }
  209. ui->copyAddress->setEnabled(true);
  210. }
  211. else
  212. {
  213. ui->deleteAddress->setEnabled(false);
  214. ui->copyAddress->setEnabled(false);
  215. }
  216. }
  217. void AddressBookPage::done(int retval)
  218. {
  219. QTableView *table = ui->tableView;
  220. if(!table->selectionModel() || !table->model())
  221. return;
  222. // Figure out which address was selected, and return it
  223. QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
  224. Q_FOREACH (const QModelIndex& index, indexes) {
  225. QVariant address = table->model()->data(index);
  226. returnValue = address.toString();
  227. }
  228. if(returnValue.isEmpty())
  229. {
  230. // If no address entry selected, return rejected
  231. retval = Rejected;
  232. }
  233. QDialog::done(retval);
  234. }
  235. void AddressBookPage::on_exportButton_clicked()
  236. {
  237. // CSV is currently the only supported format
  238. QString filename = GUIUtil::getSaveFileName(this,
  239. tr("Export Address List"), QString(),
  240. tr("Comma separated file (*.csv)"), NULL);
  241. if (filename.isNull())
  242. return;
  243. CSVModelWriter writer(filename);
  244. // name, column, role
  245. writer.setModel(proxyModel);
  246. writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
  247. writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
  248. if(!writer.write()) {
  249. QMessageBox::critical(this, tr("Exporting Failed"),
  250. tr("There was an error trying to save the address list to %1. Please try again.").arg(filename));
  251. }
  252. }
  253. void AddressBookPage::contextualMenu(const QPoint &point)
  254. {
  255. QModelIndex index = ui->tableView->indexAt(point);
  256. if(index.isValid())
  257. {
  258. contextMenu->exec(QCursor::pos());
  259. }
  260. }
  261. void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int /*end*/)
  262. {
  263. QModelIndex idx = proxyModel->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
  264. if(idx.isValid() && (idx.data(Qt::EditRole).toString() == newAddressToSelect))
  265. {
  266. // Select row of newly created address, once
  267. ui->tableView->setFocus();
  268. ui->tableView->selectRow(idx.row());
  269. newAddressToSelect.clear();
  270. }
  271. }