Skip to content

Commit 404373b

Browse files
committed
qt, refactor: Pass WalletModel object to WalletView constructor
An instance of the WalletView class without walletModel data member being set is invalid. So, it is better to set it in the constructor.
1 parent 774a4f5 commit 404373b

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

src/qt/bitcoingui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ void BitcoinGUI::addWallet(WalletModel* walletModel)
676676
{
677677
if (!walletFrame) return;
678678

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

682682
rpcConsole->addWallet(walletModel);

src/qt/walletframe.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
7171
if (mapWalletViews.count(walletModel) > 0) return false;
7272

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

7776
WalletView* current_wallet_view = currentWalletView();

src/qt/walletview.cpp

Lines changed: 31 additions & 36 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()
@@ -99,37 +125,6 @@ void WalletView::setClientModel(ClientModel *_clientModel)
99125
if (walletModel) walletModel->setClientModel(_clientModel);
100126
}
101127

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-
}
131-
}
132-
133128
void WalletView::processNewTransaction(const QModelIndex& parent, int start, int /*end*/)
134129
{
135130
// Prevent balloon-spam when initial block download is in progress

src/qt/walletview.h

Lines changed: 7 additions & 7 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);
4545
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);
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)