From 0f74ad0ba8ee18921266f12e1020df8f821ef81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kyle=20=F0=9F=90=86?= Date: Sat, 25 Jul 2026 20:59:23 -0400 Subject: [PATCH] GUI: Add RPC console option to follow the wallet displayed in the main window --- src/qt/bitcoingui.cpp | 4 +--- src/qt/rpcconsole.cpp | 41 ++++++++++++++++++++++++++++++++--------- src/qt/rpcconsole.h | 9 +++++++-- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 12a00a5ad56d..675ab5b7ffd5 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -454,7 +454,6 @@ void BitcoinGUI::createActions() connect(action, &QAction::triggered, [this, path] { auto activity = new OpenWalletActivity(m_wallet_controller, this); connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet, Qt::QueuedConnection); - connect(activity, &OpenWalletActivity::opened, rpcConsole, &RPCConsole::setCurrentWallet, Qt::QueuedConnection); activity->open(path); }); } @@ -488,7 +487,6 @@ void BitcoinGUI::createActions() auto activity = new RestoreWalletActivity(m_wallet_controller, this); connect(activity, &RestoreWalletActivity::restored, this, &BitcoinGUI::setCurrentWallet, Qt::QueuedConnection); - connect(activity, &RestoreWalletActivity::restored, rpcConsole, &RPCConsole::setCurrentWallet, Qt::QueuedConnection); auto backup_file_path = fs::PathFromString(backup_file.toStdString()); activity->restore(backup_file_path, wallet_name.toStdString()); @@ -853,6 +851,7 @@ void BitcoinGUI::setCurrentWallet(WalletModel* wallet_model) { if (!walletFrame || !m_wallet_controller) return; walletFrame->setCurrentWallet(wallet_model); + rpcConsole->setDisplayedWallet(wallet_model); for (int index = 0; index < m_wallet_selector->count(); ++index) { if (m_wallet_selector->itemData(index).value() == wallet_model) { m_wallet_selector->setCurrentIndex(index); @@ -1330,7 +1329,6 @@ void BitcoinGUI::createWallet() #ifdef ENABLE_WALLET auto activity = new CreateWalletActivity(getWalletController(), this); connect(activity, &CreateWalletActivity::created, this, &BitcoinGUI::setCurrentWallet); - connect(activity, &CreateWalletActivity::created, rpcConsole, &RPCConsole::setCurrentWallet); activity->create(); #endif // ENABLE_WALLET } diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 460b13366487..df20cd6898c8 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -67,6 +67,12 @@ const int INITIAL_TRAFFIC_GRAPH_MINS = 30; const QSize FONT_RANGE(4, 40); const char fontSizeSettingsKey[] = "consoleFontSize"; +#ifdef ENABLE_WALLET +//! Index of the wallet selector entry that follows the wallet displayed in the main window. +//! Entry 0 is "(none)", from the form; wallets are appended after this one. +const int WALLET_SELECTOR_DISPLAYED = 1; +#endif // ENABLE_WALLET + const struct { const char *url; const char *source; @@ -626,6 +632,11 @@ RPCConsole::RPCConsole(interfaces::Node& node, const PlatformStyle *_platformSty connect(ui->fontSmallerButton, &QAbstractButton::clicked, this, &RPCConsole::fontSmaller); connect(ui->btnClearTrafficGraph, &QPushButton::clicked, ui->trafficGraph, &TrafficGraphWidget::clear); +#ifdef ENABLE_WALLET + ui->WalletSelector->insertItem(WALLET_SELECTOR_DISPLAYED, QString()); + setDisplayedWallet(nullptr); +#endif // ENABLE_WALLET + // disable the wallet selector by default ui->WalletSelector->setVisible(false); ui->WalletSelectorLabel->setVisible(false); @@ -981,11 +992,11 @@ void RPCConsole::addWallet(WalletModel * const walletModel) { // use name for text and wallet model for internal data object (to allow to move to a wallet id later) ui->WalletSelector->addItem(walletModel->getDisplayName(), QVariant::fromValue(walletModel)); - if (ui->WalletSelector->count() == 2) { - // First wallet added, set to default to match wallet RPC behavior - ui->WalletSelector->setCurrentIndex(1); + if (ui->WalletSelector->count() == 3) { + // First wallet added, follow the wallet displayed in the main window + ui->WalletSelector->setCurrentIndex(WALLET_SELECTOR_DISPLAYED); } - if (ui->WalletSelector->count() > 2) { + if (ui->WalletSelector->count() > 3) { ui->WalletSelector->setVisible(true); ui->WalletSelectorLabel->setVisible(true); } @@ -994,16 +1005,28 @@ void RPCConsole::addWallet(WalletModel * const walletModel) void RPCConsole::removeWallet(WalletModel * const walletModel) { ui->WalletSelector->removeItem(ui->WalletSelector->findData(QVariant::fromValue(walletModel))); - if (ui->WalletSelector->count() == 2) { + if (ui->WalletSelector->count() == 3) { ui->WalletSelector->setVisible(false); ui->WalletSelectorLabel->setVisible(false); } + // The main window switches to another wallet before removing this one, so + // this only hits when the last wallet is unloaded + if (m_displayed_wallet_model == walletModel) setDisplayedWallet(nullptr); +} + +void RPCConsole::setDisplayedWallet(WalletModel* const wallet_model) +{ + m_displayed_wallet_model = wallet_model; + ui->WalletSelector->setItemText(WALLET_SELECTOR_DISPLAYED, + wallet_model ? tr("Use displayed wallet (%1)").arg(wallet_model->getDisplayName()) + : tr("Use displayed wallet")); } -void RPCConsole::setCurrentWallet(WalletModel* const wallet_model) +WalletModel* RPCConsole::currentWalletModel() const { - QVariant data = QVariant::fromValue(wallet_model); - ui->WalletSelector->setCurrentIndex(ui->WalletSelector->findData(data)); + const int index = ui->WalletSelector->currentIndex(); + if (index == WALLET_SELECTOR_DISPLAYED) return m_displayed_wallet_model; + return ui->WalletSelector->itemData(index).value(); } #endif @@ -1259,7 +1282,7 @@ void RPCConsole::on_lineEdit_returnPressed() WalletModel* wallet_model{nullptr}; #ifdef ENABLE_WALLET - wallet_model = ui->WalletSelector->currentData().value(); + wallet_model = currentWalletModel(); if (m_last_wallet_model != wallet_model) { if (wallet_model) { diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index c7b4802155ae..711055110477 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -153,8 +153,8 @@ public Q_SLOTS: /** set which tab has the focus (is visible) */ void setTabFocus(enum TabTypes tabType); #ifdef ENABLE_WALLET - /** Set the current (ie - active) wallet */ - void setCurrentWallet(WalletModel* const wallet_model); + /** Set the wallet displayed in the main window, followed when the selector is on "Use displayed wallet" */ + void setDisplayedWallet(WalletModel* const wallet_model); #endif // ENABLE_WALLET private: @@ -198,6 +198,11 @@ public Q_SLOTS: QThread thread; RPCExecutor* m_executor{nullptr}; WalletModel* m_last_wallet_model{nullptr}; +#ifdef ENABLE_WALLET + /** Wallet the selector resolves to while "Use displayed wallet" is selected */ + WalletModel* m_displayed_wallet_model{nullptr}; + WalletModel* currentWalletModel() const; +#endif // ENABLE_WALLET bool m_is_executing{false}; QByteArray m_peer_widget_header_state; QByteArray m_banlist_widget_header_state;