Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Pass WalletModel object to the WalletView constructor #398

Merged
merged 4 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ void BitcoinGUI::addWallet(WalletModel* walletModel)
{
if (!walletFrame) return;

WalletView* wallet_view = new WalletView(platformStyle, walletFrame);
WalletView* wallet_view = new WalletView(walletModel, platformStyle, walletFrame);
if (!walletFrame->addWallet(walletModel, wallet_view)) return;
hebasto marked this conversation as resolved.
Show resolved Hide resolved

rpcConsole->addWallet(walletModel);
Expand Down
1 change: 0 additions & 1 deletion src/qt/walletframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
if (mapWalletViews.count(walletModel) > 0) return false;

walletView->setClientModel(clientModel);
walletView->setWalletModel(walletModel);
walletView->showOutOfSyncWarning(bOutOfSync);

WalletView* current_wallet_view = currentWalletView();
Expand Down
82 changes: 34 additions & 48 deletions src/qt/walletview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,24 @@
#include <QPushButton>
#include <QVBoxLayout>

WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
QStackedWidget(parent),
clientModel(nullptr),
walletModel(nullptr),
platformStyle(_platformStyle)
WalletView::WalletView(WalletModel* wallet_model, const PlatformStyle* _platformStyle, QWidget* parent)
: QStackedWidget(parent),
clientModel(nullptr),
walletModel(wallet_model),
platformStyle(_platformStyle)
{
assert(walletModel);

// Create tabs
overviewPage = new OverviewPage(platformStyle);
overviewPage->setWalletModel(walletModel);

transactionsPage = new QWidget(this);
QVBoxLayout *vbox = new QVBoxLayout();
QHBoxLayout *hbox_buttons = new QHBoxLayout();
transactionView = new TransactionView(platformStyle, this);
transactionView->setModel(walletModel);

vbox->addWidget(transactionView);
QPushButton *exportButton = new QPushButton(tr("&Export"), this);
exportButton->setToolTip(tr("Export the data in the current tab to a file"));
Expand All @@ -55,10 +60,16 @@ WalletView::WalletView(const PlatformStyle *_platformStyle, QWidget *parent):
transactionsPage->setLayout(vbox);

receiveCoinsPage = new ReceiveCoinsDialog(platformStyle);
receiveCoinsPage->setModel(walletModel);

sendCoinsPage = new SendCoinsDialog(platformStyle);
sendCoinsPage->setModel(walletModel);

usedSendingAddressesPage = new AddressBookPage(platformStyle, AddressBookPage::ForEditing, AddressBookPage::SendingTab, this);
usedSendingAddressesPage->setModel(walletModel->getAddressTableModel());

usedReceivingAddressesPage = new AddressBookPage(platformStyle, AddressBookPage::ForEditing, AddressBookPage::ReceivingTab, this);
usedReceivingAddressesPage->setModel(walletModel->getAddressTableModel());

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

connect(this, &WalletView::setPrivacy, overviewPage, &OverviewPage::setPrivacy);

// Receive and pass through messages from wallet model
connect(walletModel, &WalletModel::message, this, &WalletView::message);

// Handle changes in encryption status
connect(walletModel, &WalletModel::encryptionStatusChanged, this, &WalletView::encryptionStatusChanged);

// Balloon pop-up for new transaction
connect(walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);

// Ask for passphrase if needed
connect(walletModel, &WalletModel::requireUnlock, this, &WalletView::unlockWallet);

// Show progress dialog
connect(walletModel, &WalletModel::showProgress, this, &WalletView::showProgress);
}

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

overviewPage->setClientModel(_clientModel);
sendCoinsPage->setClientModel(_clientModel);
if (walletModel) walletModel->setClientModel(_clientModel);
}

void WalletView::setWalletModel(WalletModel *_walletModel)
{
this->walletModel = _walletModel;

// Put transaction list in tabs
transactionView->setModel(_walletModel);
overviewPage->setWalletModel(_walletModel);
receiveCoinsPage->setModel(_walletModel);
sendCoinsPage->setModel(_walletModel);
usedReceivingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);
usedSendingAddressesPage->setModel(_walletModel ? _walletModel->getAddressTableModel() : nullptr);

if (_walletModel)
{
// Receive and pass through messages from wallet model
connect(_walletModel, &WalletModel::message, this, &WalletView::message);

// Handle changes in encryption status
connect(_walletModel, &WalletModel::encryptionStatusChanged, this, &WalletView::encryptionStatusChanged);

// Balloon pop-up for new transaction
connect(_walletModel->getTransactionTableModel(), &TransactionTableModel::rowsInserted, this, &WalletView::processNewTransaction);

// Ask for passphrase if needed
connect(_walletModel, &WalletModel::requireUnlock, this, &WalletView::unlockWallet);

// Show progress dialog
connect(_walletModel, &WalletModel::showProgress, this, &WalletView::showProgress);
}
walletModel->setClientModel(_clientModel);
}

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

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

void WalletView::encryptWallet()
{
if(!walletModel)
return;
AskPassphraseDialog dlg(AskPassphraseDialog::Encrypt, this);
dlg.setModel(walletModel);
dlg.exec();
Expand Down Expand Up @@ -247,8 +241,6 @@ void WalletView::changePassphrase()

void WalletView::unlockWallet()
{
if(!walletModel)
return;
// Unlock wallet when requested by wallet model
if (walletModel->getEncryptionStatus() == WalletModel::Locked)
{
Expand All @@ -260,17 +252,11 @@ void WalletView::unlockWallet()

void WalletView::usedSendingAddresses()
{
if(!walletModel)
return;

GUIUtil::bringToFront(usedSendingAddressesPage);
}

void WalletView::usedReceivingAddresses()
{
if(!walletModel)
return;

GUIUtil::bringToFront(usedReceivingAddressesPage);
}

Expand Down
16 changes: 8 additions & 8 deletions src/qt/walletview.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ class WalletView : public QStackedWidget
Q_OBJECT

public:
explicit WalletView(const PlatformStyle *platformStyle, QWidget *parent);
explicit WalletView(WalletModel* wallet_model, const PlatformStyle* platformStyle, QWidget* parent);
~WalletView();

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

bool handlePaymentRequest(const SendCoinsRecipient& recipient);

void showOutOfSyncWarning(bool fShow);

private:
ClientModel *clientModel;
WalletModel *walletModel;

//!
//! The wallet model represents a bitcoin wallet, and offers access to
//! the list of transactions, address book and sending functionality.
//!
WalletModel* const walletModel;

OverviewPage *overviewPage;
QWidget *transactionsPage;
Expand Down