Skip to content

Commit

Permalink
merge bitcoin-core/gui#398: Pass WalletModel object to the WalletView…
Browse files Browse the repository at this point in the history
… constructor
  • Loading branch information
kwvg committed Feb 5, 2025
1 parent 9e58f8c commit 3f9dca5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ void BitcoinGUI::addWallet(WalletModel* walletModel)
{
if (!walletFrame) return;

WalletView* wallet_view = new WalletView(walletFrame);
if (!walletFrame->addWallet(walletModel, wallet_view)) return;
WalletView* wallet_view = new WalletView(walletModel, walletFrame);
if (!walletFrame->addView(wallet_view)) return;

rpcConsole->addWallet(walletModel);
if (m_wallet_selector->count() == 0) {
Expand Down
9 changes: 4 additions & 5 deletions src/qt/walletframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ void WalletFrame::setClientModel(ClientModel *_clientModel)
}
}

bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
bool WalletFrame::addView(WalletView* walletView)
{
if (!clientModel || !walletModel) return false;
if (!clientModel) return false;

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

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

WalletView* current_wallet_view = currentWalletView();
Expand All @@ -96,7 +95,7 @@ bool WalletFrame::addWallet(WalletModel* walletModel, WalletView* walletView)
}

walletStack->addWidget(walletView);
mapWalletViews[walletModel] = walletView;
mapWalletViews[walletView->getWalletModel()] = walletView;

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WalletFrame : public QFrame

void setClientModel(ClientModel *clientModel);

bool addWallet(WalletModel* walletModel, WalletView* walletView);
bool addView(WalletView* walletView);
void setCurrentWallet(WalletModel* wallet_model);
void removeWallet(WalletModel* wallet_model);
void removeAllWallets();
Expand Down
92 changes: 36 additions & 56 deletions src/qt/walletview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,23 @@
#include <QSettings>
#include <QVBoxLayout>

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

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

transactionsPage = new QWidget(this);
QVBoxLayout *vbox = new QVBoxLayout();
QHBoxLayout *hbox_buttons = new QHBoxLayout();
transactionView = new TransactionView(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 Down Expand Up @@ -75,11 +80,19 @@ WalletView::WalletView(QWidget* parent) :
transactionsPage->setLayout(vbox);

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

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

coinJoinCoinsPage = new SendCoinsDialog(true);
coinJoinCoinsPage->setModel(walletModel);

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

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

addWidget(overviewPage);
addWidget(transactionsPage);
Expand All @@ -90,6 +103,7 @@ WalletView::WalletView(QWidget* parent) :
QSettings settings;
if (settings.value("fShowMasternodesTab").toBool()) {
masternodeListPage = new MasternodeList();
masternodeListPage->setWalletModel(walletModel);
addWidget(masternodeListPage);
}
if (settings.value("fShowGovernanceTab").toBool()) {
Expand Down Expand Up @@ -123,6 +137,21 @@ WalletView::WalletView(QWidget* parent) :

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);

GUIUtil::disableMacFocusRect(this);
}

Expand Down Expand Up @@ -150,50 +179,15 @@ void WalletView::setClientModel(ClientModel *_clientModel)
if (settings.value("fShowGovernanceTab").toBool() && governanceListPage != nullptr) {
governanceListPage->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);
QSettings settings;
if (settings.value("fShowMasternodesTab").toBool()) {
masternodeListPage->setWalletModel(_walletModel);
}
receiveCoinsPage->setModel(_walletModel);
sendCoinsPage->setModel(_walletModel);
coinJoinCoinsPage->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 @@ -302,8 +296,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 @@ -340,10 +332,7 @@ void WalletView::changePassphrase()

void WalletView::unlockWallet(bool fForMixingOnly)
{
if(!walletModel)
return;
// Unlock wallet when requested by wallet model

if (walletModel->getEncryptionStatus() == WalletModel::Locked || walletModel->getEncryptionStatus() == WalletModel::UnlockedForMixingOnly)
{
AskPassphraseDialog dlg(fForMixingOnly ? AskPassphraseDialog::UnlockMixing : AskPassphraseDialog::Unlock, this);
Expand All @@ -354,25 +343,16 @@ void WalletView::unlockWallet(bool fForMixingOnly)

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

walletModel->setWalletLocked(true);
}

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 @@ -37,27 +37,27 @@ class WalletView : public QStackedWidget
Q_OBJECT

public:
explicit WalletView(QWidget* parent);
explicit WalletView(WalletModel* wallet_model, 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

0 comments on commit 3f9dca5

Please sign in to comment.