Bitcoin Core  27.99.0
P2P Digital Currency
walletview.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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 
5 #include <qt/walletview.h>
6 
7 #include <qt/addressbookpage.h>
9 #include <qt/clientmodel.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/overviewpage.h>
13 #include <qt/platformstyle.h>
14 #include <qt/receivecoinsdialog.h>
15 #include <qt/sendcoinsdialog.h>
18 #include <qt/transactionview.h>
19 #include <qt/walletmodel.h>
20 
21 #include <interfaces/node.h>
22 #include <node/interface_ui.h>
23 #include <util/strencodings.h>
24 
25 #include <QAction>
26 #include <QFileDialog>
27 #include <QHBoxLayout>
28 #include <QProgressDialog>
29 #include <QPushButton>
30 #include <QVBoxLayout>
31 
32 WalletView::WalletView(WalletModel* wallet_model, const PlatformStyle* _platformStyle, QWidget* parent)
33  : QStackedWidget(parent),
34  walletModel(wallet_model),
35  platformStyle(_platformStyle)
36 {
38 
39  // Create tabs
42 
43  transactionsPage = new QWidget(this);
44  QVBoxLayout *vbox = new QVBoxLayout();
45  QHBoxLayout *hbox_buttons = new QHBoxLayout();
48 
49  vbox->addWidget(transactionView);
50  QPushButton *exportButton = new QPushButton(tr("&Export"), this);
51  exportButton->setToolTip(tr("Export the data in the current tab to a file"));
53  exportButton->setIcon(platformStyle->SingleColorIcon(":/icons/export"));
54  }
55  hbox_buttons->addStretch();
56  hbox_buttons->addWidget(exportButton);
57  vbox->addLayout(hbox_buttons);
58  transactionsPage->setLayout(vbox);
59 
62 
65 
68 
71 
72  addWidget(overviewPage);
73  addWidget(transactionsPage);
74  addWidget(receiveCoinsPage);
75  addWidget(sendCoinsPage);
76 
78  // Clicking on a transaction on the overview pre-selects the transaction on the transaction history page
80 
82 
84  // Highlight transaction after send
86 
87  // Clicking on "Export" allows to export the transaction list
88  connect(exportButton, &QPushButton::clicked, transactionView, &TransactionView::exportClicked);
89 
90  // Pass through messages from sendCoinsPage
92  // Pass through messages from transactionView
94 
97 
98  // Receive and pass through messages from wallet model
100 
101  // Handle changes in encryption status
103 
104  // Balloon pop-up for new transaction
105  connect(walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);
106 
107  // Ask for passphrase if needed
109 
110  // Show progress dialog
112 }
113 
114 WalletView::~WalletView() = default;
115 
117 {
118  this->clientModel = _clientModel;
119 
120  overviewPage->setClientModel(_clientModel);
121  sendCoinsPage->setClientModel(_clientModel);
122  walletModel->setClientModel(_clientModel);
123 }
124 
125 void WalletView::processNewTransaction(const QModelIndex& parent, int start, int /*end*/)
126 {
127  // Prevent balloon-spam when initial block download is in progress
129  return;
130  }
131 
133  if (!ttm || ttm->processingQueuedTransactions())
134  return;
135 
136  QString date = ttm->index(start, TransactionTableModel::Date, parent).data().toString();
137  qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent).data(Qt::EditRole).toLongLong();
138  QString type = ttm->index(start, TransactionTableModel::Type, parent).data().toString();
139  QModelIndex index = ttm->index(start, 0, parent);
140  QString address = ttm->data(index, TransactionTableModel::AddressRole).toString();
141  QString label = GUIUtil::HtmlEscape(ttm->data(index, TransactionTableModel::LabelRole).toString());
142 
143  Q_EMIT incomingTransaction(date, walletModel->getOptionsModel()->getDisplayUnit(), amount, type, address, label, GUIUtil::HtmlEscape(walletModel->getWalletName()));
144 }
145 
147 {
148  setCurrentWidget(overviewPage);
149 }
150 
152 {
153  setCurrentWidget(transactionsPage);
154 }
155 
157 {
158  setCurrentWidget(receiveCoinsPage);
159 }
160 
162 {
163  setCurrentWidget(sendCoinsPage);
164 
165  if (!addr.isEmpty())
166  sendCoinsPage->setAddress(addr);
167 }
168 
170 {
171  // calls show() in showTab_SM()
172  SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(platformStyle, this);
173  signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
174  signVerifyMessageDialog->setModel(walletModel);
175  signVerifyMessageDialog->showTab_SM(true);
176 
177  if (!addr.isEmpty())
178  signVerifyMessageDialog->setAddress_SM(addr);
179 }
180 
182 {
183  // calls show() in showTab_VM()
184  SignVerifyMessageDialog *signVerifyMessageDialog = new SignVerifyMessageDialog(platformStyle, this);
185  signVerifyMessageDialog->setAttribute(Qt::WA_DeleteOnClose);
186  signVerifyMessageDialog->setModel(walletModel);
187  signVerifyMessageDialog->showTab_VM(true);
188 
189  if (!addr.isEmpty())
190  signVerifyMessageDialog->setAddress_VM(addr);
191 }
192 
194 {
195  return sendCoinsPage->handlePaymentRequest(recipient);
196 }
197 
199 {
201 }
202 
204 {
206  dlg->setModel(walletModel);
207  connect(dlg, &QDialog::finished, this, &WalletView::encryptionStatusChanged);
209 }
210 
212 {
213  QString filename = GUIUtil::getSaveFileName(this,
214  tr("Backup Wallet"), QString(),
215  //: Name of the wallet data file format.
216  tr("Wallet Data") + QLatin1String(" (*.dat)"), nullptr);
217 
218  if (filename.isEmpty())
219  return;
220 
221  if (!walletModel->wallet().backupWallet(filename.toLocal8Bit().data())) {
222  Q_EMIT message(tr("Backup Failed"), tr("There was an error trying to save the wallet data to %1.").arg(filename),
224  }
225  else {
226  Q_EMIT message(tr("Backup Successful"), tr("The wallet data was successfully saved to %1.").arg(filename),
228  }
229 }
230 
232 {
234  dlg->setModel(walletModel);
236 }
237 
239 {
240  // Unlock wallet when requested by wallet model
243  dlg.setModel(walletModel);
244  // A modal dialog must be synchronous here as expected
245  // in the WalletModel::requestUnlock() function.
246  dlg.exec();
247  }
248 }
249 
251 {
253 }
254 
256 {
258 }
259 
260 void WalletView::showProgress(const QString &title, int nProgress)
261 {
262  if (nProgress == 0) {
263  progressDialog = new QProgressDialog(title, tr("Cancel"), 0, 100);
265  progressDialog->setWindowModality(Qt::ApplicationModal);
266  progressDialog->setAutoClose(false);
267  progressDialog->setValue(0);
268  } else if (nProgress == 100) {
269  if (progressDialog) {
270  progressDialog->close();
271  progressDialog->deleteLater();
272  progressDialog = nullptr;
273  }
274  } else if (progressDialog) {
275  if (progressDialog->wasCanceled()) {
277  } else {
278  progressDialog->setValue(nProgress);
279  }
280  }
281 }
282 
284 {
285  transactionView->setDisabled(disable);
286 }
Widget that shows a list of sending or receiving addresses.
@ ForEditing
Open address book for editing.
void setModel(AddressTableModel *model)
Multifunctional dialog to ask for passphrases.
void setModel(WalletModel *model)
@ Unlock
Ask passphrase and unlock.
@ Encrypt
Ask passphrase twice and encrypt.
@ ChangePass
Ask old passphrase + new passphrase twice.
@ MSG_INFORMATION
Predefined combinations for certain default usage cases.
Definition: interface_ui.h:65
Model for Bitcoin network client.
Definition: clientmodel.h:54
interfaces::Node & node() const
Definition: clientmodel.h:63
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:104
Overview ("home") page widget.
Definition: overviewpage.h:29
void setWalletModel(WalletModel *walletModel)
void setClientModel(ClientModel *clientModel)
void transactionClicked(const QModelIndex &index)
void outOfSyncWarningClicked()
void showOutOfSyncWarning(bool fShow)
void setPrivacy(bool privacy)
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
bool getImagesOnButtons() const
Definition: platformstyle.h:21
Dialog for requesting payment of bitcoins.
void setModel(WalletModel *model)
Dialog for sending bitcoins.
void setClientModel(ClientModel *clientModel)
void setModel(WalletModel *model)
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
void setAddress(const QString &address)
void message(const QString &title, const QString &message, unsigned int style)
void coinsSent(const uint256 &txid)
void setAddress_SM(const QString &address)
void setModel(WalletModel *model)
void setAddress_VM(const QString &address)
UI model for the transaction table of a wallet.
@ LabelRole
Label of address related to transaction.
@ AddressRole
Address of transaction.
QVariant data(const QModelIndex &index, int role) const override
bool processingQueuedTransactions() const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Widget showing the transaction list for a wallet, including a filter row.
void setModel(WalletModel *model)
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
void focusTransaction(const QModelIndex &)
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:48
AddressTableModel * getAddressTableModel() const
void showProgress(const QString &title, int nProgress)
void message(const QString &title, const QString &message, unsigned int style)
void setClientModel(ClientModel *client_model)
Definition: walletmodel.cpp:74
interfaces::Wallet & wallet() const
Definition: walletmodel.h:138
EncryptionStatus getEncryptionStatus() const
TransactionTableModel * getTransactionTableModel() const
OptionsModel * getOptionsModel() const
void requireUnlock()
void encryptionStatusChanged()
QString getWalletName() const
const PlatformStyle * platformStyle
Definition: walletview.h:71
void gotoHistoryPage()
Switch to history (transactions) page.
Definition: walletview.cpp:151
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletview.cpp:181
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletview.cpp:193
void disableTransactionView(bool disable)
Definition: walletview.cpp:283
WalletModel *const walletModel
The wallet model represents a bitcoin wallet, and offers access to the list of transactions,...
Definition: walletview.h:59
void gotoSendCoinsPage(QString addr="")
Switch to send coins page.
Definition: walletview.cpp:161
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletview.cpp:231
QProgressDialog * progressDialog
Definition: walletview.h:70
SendCoinsDialog * sendCoinsPage
Definition: walletview.h:64
OverviewPage * overviewPage
Definition: walletview.h:61
void setClientModel(ClientModel *clientModel)
Set the client model.
Definition: walletview.cpp:116
void outOfSyncWarningClicked()
Notify that the out of sync warning icon has been pressed.
void gotoReceiveCoinsPage()
Switch to receive coins page.
Definition: walletview.cpp:156
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletview.cpp:169
QWidget * transactionsPage
Definition: walletview.h:62
void usedSendingAddresses()
Show used sending addresses.
Definition: walletview.cpp:250
ReceiveCoinsDialog * receiveCoinsPage
Definition: walletview.h:63
void encryptWallet()
Encrypt the wallet.
Definition: walletview.cpp:203
TransactionView * transactionView
Definition: walletview.h:68
WalletView(WalletModel *wallet_model, const PlatformStyle *platformStyle, QWidget *parent)
Definition: walletview.cpp:32
void showProgress(const QString &title, int nProgress)
Show progress dialog e.g.
Definition: walletview.cpp:260
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
AddressBookPage * usedReceivingAddressesPage
Definition: walletview.h:66
void backupWallet()
Backup the wallet.
Definition: walletview.cpp:211
void incomingTransaction(const QString &date, BitcoinUnit unit, const CAmount &amount, const QString &type, const QString &address, const QString &label, const QString &walletName)
Notify that a new transaction appeared.
ClientModel * clientModel
Definition: walletview.h:53
AddressBookPage * usedSendingAddressesPage
Definition: walletview.h:65
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletview.cpp:238
WalletModel * getWalletModel() const noexcept
Definition: walletview.h:46
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletview.cpp:146
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletview.cpp:255
void transactionClicked()
void coinsSent()
void processNewTransaction(const QModelIndex &parent, int start, int)
Show incoming transaction notification for new transactions.
Definition: walletview.cpp:125
void setPrivacy(bool privacy)
void showOutOfSyncWarning(bool fShow)
Definition: walletview.cpp:198
void encryptionStatusChanged()
Encryption status of wallet changed.
virtual bool isInitialBlockDownload()=0
Is initial block download.
virtual void abortRescan()=0
Abort a rescan.
virtual bool backupWallet(const std::string &filename)=0
Back up wallet.
QString HtmlEscape(const QString &str, bool fMultiLine)
Definition: guiutil.cpp:250
void ShowModalDialogAsynchronously(QDialog *dialog)
Shows a QDialog instance asynchronously, and deletes it on close.
Definition: guiutil.cpp:1003
void PolishProgressDialog(QProgressDialog *dialog)
Definition: guiutil.cpp:895
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:314
void bringToFront(QWidget *w)
Definition: guiutil.cpp:407
assert(!tx.IsCoinBase())