Skip to content

Commit 503194d

Browse files
committed
Merge bitcoin-core#398: refactor: Pass WalletModel object to the WalletView constructor
d319c4d qt, refactor: Replace WalletFrame::addWallet with WalletFrame::addView (Hennadii Stepanov) 92ddc02 qt, refactor: Declare getWalletModel with const and noexcept qualifiers (Hennadii Stepanov) ca0e680 qt, refactor: Drop redundant checks of walletModel (Hennadii Stepanov) 404373b qt, refactor: Pass WalletModel object to WalletView constructor (Hennadii Stepanov) Pull request description: An instance of the `WalletView` class without the `walletModel` data member being set is invalid. So, it is better to set it in the constructor. Establishing one more `WalletView` class's invariant in constructor: - allows to drop all of checks of the`walletModel` in member functions - makes reasoning about the code that uses instances of the `WalletView` class easier Possible follow ups could extend this approach to other classes, e.g., `OverviewPage`, `TransactionView`, `ReceiveCoinsDialog`, `SendCoinsDialog`, `AddressBookPage`. ACKs for top commit: ShaMan239: Code review ACK d319c4d promag: Code review ACK d319c4d. jarolrod: ACK d319c4d Tree-SHA512: b0c61f82811bb5aba2738067b53dc9ea4439230d547ce5c8fd85c480d8d70ea15f9942dbf13842383acbce467fba1ab4e132e37c56b654b46ba897301a41066e
2 parents 6718fbe + d319c4d commit 503194d

File tree

5 files changed

+49
-64
lines changed

5 files changed

+49
-64
lines changed

src/qt/bitcoingui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ void BitcoinGUI::addWallet(WalletModel* walletModel)
676676
{
677677
if (!walletFrame) return;
678678

679-
WalletView* wallet_view = new WalletView(platformStyle, walletFrame);
680-
if (!walletFrame->addWallet(walletModel, wallet_view)) return;
679+
WalletView* wallet_view = new WalletView(walletModel, platformStyle, walletFrame);
680+
if (!walletFrame->addView(wallet_view)) return;
681681

682682
rpcConsole->addWallet(walletModel);
683683
if (m_wallet_selector->count() == 0) {

src/qt/walletframe.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ void WalletFrame::setClientModel(ClientModel *_clientModel)
6464
}
6565
}
6666

67-
bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
67+
bool WalletFrame::addView(WalletView* walletView)
6868
{
69-
if (!clientModel || !walletModel) return false;
69+
if (!clientModel) return false;
7070

71-
if (mapWalletViews.count(walletModel) > 0) return false;
71+
if (mapWalletViews.count(walletView->getWalletModel()) > 0) return false;
7272

7373
walletView->setClientModel(clientModel);
74-
walletView->setWalletModel(walletModel);
7574
walletView->showOutOfSyncWarning(bOutOfSync);
7675

7776
WalletView* current_wallet_view = currentWalletView();
@@ -82,7 +81,7 @@ bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
8281
}
8382

8483
walletStack->addWidget(walletView);
85-
mapWalletViews[walletModel] = walletView;
84+
mapWalletViews[walletView->getWalletModel()] = walletView;
8685

8786
return true;
8887
}

src/qt/walletframe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class WalletFrame : public QFrame
3535

3636
void setClientModel(ClientModel *clientModel);
3737

38-
bool addWallet(WalletModel* walletModel, WalletView* walletView);
38+
bool addView(WalletView* walletView);
3939
void setCurrentWallet(WalletModel* wallet_model);
4040
void removeWallet(WalletModel* wallet_model);
4141
void removeAllWallets();

src/qt/walletview.cpp

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@
3030
#include <QPushButton>
3131
#include <QVBoxLayout>
3232

33-
WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
34-
QStackedWidget(parent),
35-
clientModel(nullptr),
36-
walletModel(nullptr),
37-
platformStyle(_platformStyle)
33+
WalletView::WalletView(WalletModel* wallet_model, const PlatformStyle* _platformStyle, QWidget* parent)
34+
: QStackedWidget(parent),
35+
clientModel(nullptr),
36+
walletModel(wallet_model),
37+
platformStyle(_platformStyle)
3838
{
39+
assert(walletModel);
40+
3941
// Create tabs
4042
overviewPage = new OverviewPage(platformStyle);
43+
overviewPage->setWalletModel(walletModel);
4144

4245
transactionsPage = new QWidget(this);
4346
QVBoxLayout *vbox = new QVBoxLayout();
4447
QHBoxLayout *hbox_buttons = new QHBoxLayout();
4548
transactionView = new TransactionView(platformStyle, this);
49+
transactionView->setModel(walletModel);
50+
4651
vbox->addWidget(transactionView);
4752
QPushButton *exportButton = new QPushButton(tr("&Export"), this);
4853
exportButton->setToolTip(tr("Export the data in the current tab to a file"));
@@ -55,10 +60,16 @@ WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
5560
transactionsPage->setLayout(vbox);
5661

5762
receiveCoinsPage = new ReceiveCoinsDialog(platformStyle);
63+
receiveCoinsPage->setModel(walletModel);
64+
5865
sendCoinsPage = new SendCoinsDialog(platformStyle);
66+
sendCoinsPage->setModel(walletModel);
5967

6068
usedSendingAddressesPage = new AddressBookPage(platformStyle, AddressBookPage::ForEditing, AddressBookPage::SendingTab, this);
69+
usedSendingAddressesPage->setModel(walletModel->getAddressTableModel());
70+
6171
usedReceivingAddressesPage = new AddressBookPage(platformStyle, AddressBookPage::ForEditing, AddressBookPage::ReceivingTab, this);
72+
usedReceivingAddressesPage->setModel(walletModel->getAddressTableModel());
6273

6374
addWidget(overviewPage);
6475
addWidget(transactionsPage);
@@ -84,6 +95,21 @@ WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
8495
connect(transactionView, &TransactionView::message, this, &WalletView::message);
8596

8697
connect(this, &WalletView::setPrivacy, overviewPage, &OverviewPage::setPrivacy);
98+
99+
// Receive and pass through messages from wallet model
100+
connect(walletModel, &WalletModel::message, this, &WalletView::message);
101+
102+
// Handle changes in encryption status
103+
connect(walletModel, &WalletModel::encryptionStatusChanged, this, &WalletView::encryptionStatusChanged);
104+
105+
// Balloon pop-up for new transaction
106+
connect(walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);
107+
108+
// Ask for passphrase if needed
109+
connect(walletModel, &WalletModel::requireUnlock, this, &WalletView::unlockWallet);
110+
111+
// Show progress dialog
112+
connect(walletModel, &WalletModel::showProgress, this, &WalletView::showProgress);
87113
}
88114

89115
WalletView::~WalletView()
@@ -96,45 +122,15 @@ void WalletView::setClientModel(ClientModel *_clientModel)
96122

97123
overviewPage->setClientModel(_clientModel);
98124
sendCoinsPage->setClientModel(_clientModel);
99-
if (walletModel) walletModel->setClientModel(_clientModel);
100-
}
101-
102-
void WalletView::setWalletModel(WalletModel *_walletModel)
103-
{
104-
this->walletModel = _walletModel;
105-
106-
// Put transaction list in tabs
107-
transactionView->setModel(_walletModel);
108-
overviewPage->setWalletModel(_walletModel);
109-
receiveCoinsPage->setModel(_walletModel);
110-
sendCoinsPage->setModel(_walletModel);
111-
usedReceivingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);
112-
usedSendingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);
113-
114-
if (_walletModel)
115-
{
116-
// Receive and pass through messages from wallet model
117-
connect(_walletModel, &WalletModel::message, this, &WalletView::message);
118-
119-
// Handle changes in encryption status
120-
connect(_walletModel, &WalletModel::encryptionStatusChanged, this, &WalletView::encryptionStatusChanged);
121-
122-
// Balloon pop-up for new transaction
123-
connect(_walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);
124-
125-
// Ask for passphrase if needed
126-
connect(_walletModel, &WalletModel::requireUnlock, this, &WalletView::unlockWallet);
127-
128-
// Show progress dialog
129-
connect(_walletModel, &WalletModel::showProgress, this, &WalletView::showProgress);
130-
}
125+
walletModel->setClientModel(_clientModel);
131126
}
132127

133128
void WalletView::processNewTransaction(const QModelIndex& parent, int start, int /*end*/)
134129
{
135130
// Prevent balloon-spam when initial block download is in progress
136-
if (!walletModel || !clientModel || clientModel->node().isInitialBlockDownload())
131+
if (!clientModel || clientModel->node().isInitialBlockDownload()) {
137132
return;
133+
}
138134

139135
TransactionTableModel *ttm = walletModel->getTransactionTableModel();
140136
if (!ttm || ttm->processingQueuedTransactions())
@@ -209,8 +205,6 @@ void WalletView::showOutOfSyncWarning(bool fShow)
209205

210206
void WalletView::encryptWallet()
211207
{
212-
if(!walletModel)
213-
return;
214208
AskPassphraseDialog dlg(AskPassphraseDialog::Encrypt, this);
215209
dlg.setModel(walletModel);
216210
dlg.exec();
@@ -247,8 +241,6 @@ void WalletView::changePassphrase()
247241

248242
void WalletView::unlockWallet()
249243
{
250-
if(!walletModel)
251-
return;
252244
// Unlock wallet when requested by wallet model
253245
if (walletModel->getEncryptionStatus() == WalletModel::Locked)
254246
{
@@ -260,17 +252,11 @@ void WalletView::unlockWallet()
260252

261253
void WalletView::usedSendingAddresses()
262254
{
263-
if(!walletModel)
264-
return;
265-
266255
GUIUtil::bringToFront(usedSendingAddressesPage);
267256
}
268257

269258
void WalletView::usedReceivingAddresses()
270259
{
271-
if(!walletModel)
272-
return;
273-
274260
GUIUtil::bringToFront(usedReceivingAddressesPage);
275261
}
276262

src/qt/walletview.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ class WalletView : public QStackedWidget
3535
Q_OBJECT
3636

3737
public:
38-
explicit WalletView(const PlatformStyle *platformStyle, QWidget *parent);
38+
explicit WalletView(WalletModel* wallet_model, const PlatformStyle* platformStyle, QWidget* parent);
3939
~WalletView();
4040

4141
/** Set the client model.
4242
The client model represents the part of the core that communicates with the P2P network, and is wallet-agnostic.
4343
*/
4444
void setClientModel(ClientModel *clientModel);
45-
WalletModel *getWalletModel() { return walletModel; }
46-
/** Set the wallet model.
47-
The wallet model represents a bitcoin wallet, and offers access to the list of transactions, address book and sending
48-
functionality.
49-
*/
50-
void setWalletModel(WalletModel *walletModel);
45+
WalletModel* getWalletModel() const noexcept { return walletModel; }
5146

5247
bool handlePaymentRequest(const SendCoinsRecipient& recipient);
5348

5449
void showOutOfSyncWarning(bool fShow);
5550

5651
private:
5752
ClientModel *clientModel;
58-
WalletModel *walletModel;
53+
54+
//!
55+
//! The wallet model represents a bitcoin wallet, and offers access to
56+
//! the list of transactions, address book and sending functionality.
57+
//!
58+
WalletModel* const walletModel;
5959

6060
OverviewPage *overviewPage;
6161
QWidget *transactionsPage;

0 commit comments

Comments
 (0)