PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/qt/walletview.cpp

https://gitlab.com/Ltaimao/bitcoin
C++ | 309 lines | 234 code | 52 blank | 23 comment | 27 complexity | 00266508f6504ba7d1ec2a51693c52df MD5 | raw file
  1. // Copyright (c) 2011-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "walletview.h"
  5. #include "addressbookpage.h"
  6. #include "askpassphrasedialog.h"
  7. #include "bitcoingui.h"
  8. #include "clientmodel.h"
  9. #include "guiutil.h"
  10. #include "optionsmodel.h"
  11. #include "overviewpage.h"
  12. #include "receivecoinsdialog.h"
  13. #include "sendcoinsdialog.h"
  14. #include "signverifymessagedialog.h"
  15. #include "transactiontablemodel.h"
  16. #include "transactionview.h"
  17. #include "walletmodel.h"
  18. #include "ui_interface.h"
  19. #include <QAction>
  20. #include <QActionGroup>
  21. #include <QFileDialog>
  22. #include <QHBoxLayout>
  23. #include <QProgressDialog>
  24. #include <QPushButton>
  25. #include <QVBoxLayout>
  26. WalletView::WalletView(QWidget *parent):
  27. QStackedWidget(parent),
  28. clientModel(0),
  29. walletModel(0)
  30. {
  31. // Create tabs
  32. overviewPage = new OverviewPage();
  33. transactionsPage = new QWidget(this);
  34. QVBoxLayout *vbox = new QVBoxLayout();
  35. QHBoxLayout *hbox_buttons = new QHBoxLayout();
  36. transactionView = new TransactionView(this);
  37. vbox->addWidget(transactionView);
  38. QPushButton *exportButton = new QPushButton(tr("&Export"), this);
  39. exportButton->setToolTip(tr("Export the data in the current tab to a file"));
  40. #ifndef Q_OS_MAC // Icons on push buttons are very uncommon on Mac
  41. exportButton->setIcon(QIcon(":/icons/export"));
  42. #endif
  43. hbox_buttons->addStretch();
  44. hbox_buttons->addWidget(exportButton);
  45. vbox->addLayout(hbox_buttons);
  46. transactionsPage->setLayout(vbox);
  47. receiveCoinsPage = new ReceiveCoinsDialog();
  48. sendCoinsPage = new SendCoinsDialog();
  49. addWidget(overviewPage);
  50. addWidget(transactionsPage);
  51. addWidget(receiveCoinsPage);
  52. addWidget(sendCoinsPage);
  53. // Clicking on a transaction on the overview pre-selects the transaction on the transaction history page
  54. connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), transactionView, SLOT(focusTransaction(QModelIndex)));
  55. // Double-clicking on a transaction on the transaction history page shows details
  56. connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
  57. // Clicking on "Export" allows to export the transaction list
  58. connect(exportButton, SIGNAL(clicked()), transactionView, SLOT(exportClicked()));
  59. // Pass through messages from sendCoinsPage
  60. connect(sendCoinsPage, SIGNAL(message(QString,QString,unsigned int)), this, SIGNAL(message(QString,QString,unsigned int)));
  61. // Pass through messages from transactionView
  62. connect(transactionView, SIGNAL(message(QString,QString,unsigned int)), this, SIGNAL(message(QString,QString,unsigned int)));
  63. }
  64. WalletView::~WalletView()
  65. {
  66. }
  67. void WalletView::setBitcoinGUI(BitcoinGUI *gui)
  68. {
  69. if (gui)
  70. {
  71. // Clicking on a transaction on the overview page simply sends you to transaction history page
  72. connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), gui, SLOT(gotoHistoryPage()));
  73. // Receive and report messages
  74. connect(this, SIGNAL(message(QString,QString,unsigned int)), gui, SLOT(message(QString,QString,unsigned int)));
  75. // Pass through encryption status changed signals
  76. connect(this, SIGNAL(encryptionStatusChanged(int)), gui, SLOT(setEncryptionStatus(int)));
  77. // Pass through transaction notifications
  78. connect(this, SIGNAL(incomingTransaction(QString,int,CAmount,QString,QString)), gui, SLOT(incomingTransaction(QString,int,CAmount,QString,QString)));
  79. }
  80. }
  81. void WalletView::setClientModel(ClientModel *clientModel)
  82. {
  83. this->clientModel = clientModel;
  84. overviewPage->setClientModel(clientModel);
  85. sendCoinsPage->setClientModel(clientModel);
  86. }
  87. void WalletView::setWalletModel(WalletModel *walletModel)
  88. {
  89. this->walletModel = walletModel;
  90. // Put transaction list in tabs
  91. transactionView->setModel(walletModel);
  92. overviewPage->setWalletModel(walletModel);
  93. receiveCoinsPage->setModel(walletModel);
  94. sendCoinsPage->setModel(walletModel);
  95. if (walletModel)
  96. {
  97. // Receive and pass through messages from wallet model
  98. connect(walletModel, SIGNAL(message(QString,QString,unsigned int)), this, SIGNAL(message(QString,QString,unsigned int)));
  99. // Handle changes in encryption status
  100. connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SIGNAL(encryptionStatusChanged(int)));
  101. updateEncryptionStatus();
  102. // Balloon pop-up for new transaction
  103. connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
  104. this, SLOT(processNewTransaction(QModelIndex,int,int)));
  105. // Ask for passphrase if needed
  106. connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
  107. // Show progress dialog
  108. connect(walletModel, SIGNAL(showProgress(QString,int)), this, SLOT(showProgress(QString,int)));
  109. }
  110. }
  111. void WalletView::processNewTransaction(const QModelIndex& parent, int start, int /*end*/)
  112. {
  113. // Prevent balloon-spam when initial block download is in progress
  114. if (!walletModel || !clientModel || clientModel->inInitialBlockDownload())
  115. return;
  116. TransactionTableModel *ttm = walletModel->getTransactionTableModel();
  117. if (!ttm || ttm->processingQueuedTransactions())
  118. return;
  119. QString date = ttm->index(start, TransactionTableModel::Date, parent).data().toString();
  120. qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent).data(Qt::EditRole).toULongLong();
  121. QString type = ttm->index(start, TransactionTableModel::Type, parent).data().toString();
  122. QString address = ttm->index(start, TransactionTableModel::ToAddress, parent).data().toString();
  123. emit incomingTransaction(date, walletModel->getOptionsModel()->getDisplayUnit(), amount, type, address);
  124. }
  125. void WalletView::gotoOverviewPage()
  126. {
  127. setCurrentWidget(overviewPage);
  128. }
  129. void WalletView::gotoHistoryPage()
  130. {
  131. setCurrentWidget(transactionsPage);
  132. }
  133. void WalletView::gotoReceiveCoinsPage()
  134. {
  135. setCurrentWidget(receiveCoinsPage);
  136. }
  137. void WalletView::gotoSendCoinsPage(QString addr)
  138. {
  139. setCurrentWidget(sendCoinsPage);
  140. if (!addr.isEmpty())
  141. sendCoinsPage->setAddress(addr);
  142. }
  143. void WalletView::gotoSignMessageTab(QString addr)
  144. {
  145. // calls show() in showTab_SM()
  146. SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(this);
  147. signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
  148. signVerifyMessageDialog->setModel(walletModel);
  149. signVerifyMessageDialog->showTab_SM(true);
  150. if (!addr.isEmpty())
  151. signVerifyMessageDialog->setAddress_SM(addr);
  152. }
  153. void WalletView::gotoVerifyMessageTab(QString addr)
  154. {
  155. // calls show() in showTab_VM()
  156. SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(this);
  157. signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
  158. signVerifyMessageDialog->setModel(walletModel);
  159. signVerifyMessageDialog->showTab_VM(true);
  160. if (!addr.isEmpty())
  161. signVerifyMessageDialog->setAddress_VM(addr);
  162. }
  163. bool WalletView::handlePaymentRequest(const SendCoinsRecipient& recipient)
  164. {
  165. return sendCoinsPage->handlePaymentRequest(recipient);
  166. }
  167. void WalletView::showOutOfSyncWarning(bool fShow)
  168. {
  169. overviewPage->showOutOfSyncWarning(fShow);
  170. }
  171. void WalletView::updateEncryptionStatus()
  172. {
  173. emit encryptionStatusChanged(walletModel->getEncryptionStatus());
  174. }
  175. void WalletView::encryptWallet(bool status)
  176. {
  177. if(!walletModel)
  178. return;
  179. AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt : AskPassphraseDialog::Decrypt, this);
  180. dlg.setModel(walletModel);
  181. dlg.exec();
  182. updateEncryptionStatus();
  183. }
  184. void WalletView::backupWallet()
  185. {
  186. QString filename = GUIUtil::getSaveFileName(this,
  187. tr("Backup Wallet"), QString(),
  188. tr("Wallet Data (*.dat)"), NULL);
  189. if (filename.isEmpty())
  190. return;
  191. if (!walletModel->backupWallet(filename)) {
  192. emit message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1.").arg(filename),
  193. CClientUIInterface::MSG_ERROR);
  194. }
  195. else {
  196. emit message(tr("Backup Successful"), tr("The wallet data was successfully saved to %1.").arg(filename),
  197. CClientUIInterface::MSG_INFORMATION);
  198. }
  199. }
  200. void WalletView::changePassphrase()
  201. {
  202. AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
  203. dlg.setModel(walletModel);
  204. dlg.exec();
  205. }
  206. void WalletView::unlockWallet()
  207. {
  208. if(!walletModel)
  209. return;
  210. // Unlock wallet when requested by wallet model
  211. if (walletModel->getEncryptionStatus() == WalletModel::Locked)
  212. {
  213. AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
  214. dlg.setModel(walletModel);
  215. dlg.exec();
  216. }
  217. }
  218. void WalletView::usedSendingAddresses()
  219. {
  220. if(!walletModel)
  221. return;
  222. AddressBookPage *dlg = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab, this);
  223. dlg->setAttribute(Qt::WA_DeleteOnClose);
  224. dlg->setModel(walletModel->getAddressTableModel());
  225. dlg->show();
  226. }
  227. void WalletView::usedReceivingAddresses()
  228. {
  229. if(!walletModel)
  230. return;
  231. AddressBookPage *dlg = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab, this);
  232. dlg->setAttribute(Qt::WA_DeleteOnClose);
  233. dlg->setModel(walletModel->getAddressTableModel());
  234. dlg->show();
  235. }
  236. void WalletView::showProgress(const QString &title, int nProgress)
  237. {
  238. if (nProgress == 0)
  239. {
  240. progressDialog = new QProgressDialog(title, "", 0, 100);
  241. progressDialog->setWindowModality(Qt::ApplicationModal);
  242. progressDialog->setMinimumDuration(0);
  243. progressDialog->setCancelButton(0);
  244. progressDialog->setAutoClose(false);
  245. progressDialog->setValue(0);
  246. }
  247. else if (nProgress == 100)
  248. {
  249. if (progressDialog)
  250. {
  251. progressDialog->close();
  252. progressDialog->deleteLater();
  253. }
  254. }
  255. else if (progressDialog)
  256. progressDialog->setValue(nProgress);
  257. }