Skip to content

Commit 9013f23

Browse files
committed
Merge bitcoin-core/gui#342: wallet: Move wallets loading out from the main GUI thread
2fe69ef qt, wallet: Drop no longer used WalletController::getOpenWallets() (Hennadii Stepanov) f6991cb qt, wallet: Add LoadWalletsActivity class (Hennadii Stepanov) 4a024fc qt, wallet, refactor: Move connection to QObject::deleteLater to ctor (Hennadii Stepanov) f9b633e qt, wallet: Move activity progress dialog from data member to local (Hennadii Stepanov) Pull request description: This PR improves the GUI responsiveness during initial wallets loading at startup (especially ones that have tons of txs), and shows a standard progress dialog for long loading: ![DeepinScreenshot_select-area_20210522230626](https://user-images.githubusercontent.com/32963518/119239625-0b3a9380-bb53-11eb-9a54-34980d8a1194.png) Fixes #247. ACKs for top commit: ryanofsky: Code review ACK 2fe69ef. Just suggested changes since last review: squashing commits and dropping unused method (thanks!) shaavan: reACK 2fe69ef promag: Code review ACK 2fe69ef. Tree-SHA512: 2ac3cb48886e0005fc36b3fd0c2b35abd557186be16db3145d753c34d94188e4f4ff14dc07fb0fb7558944f84498204a3988f8284fd56c6d85b47bc9081e71a6
2 parents 2d8e0c0 + 2fe69ef commit 9013f23

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

src/qt/bitcoingui.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
107107
walletFrame = new WalletFrame(_platformStyle, this);
108108
connect(walletFrame, &WalletFrame::createWalletButtonClicked, [this] {
109109
auto activity = new CreateWalletActivity(getWalletController(), this);
110-
connect(activity, &CreateWalletActivity::finished, activity, &QObject::deleteLater);
111110
activity->create();
112111
});
113112
connect(walletFrame, &WalletFrame::message, [this](const QString& title, const QString& message, unsigned int style) {
@@ -418,7 +417,6 @@ void BitcoinGUI::createActions()
418417
connect(action, &QAction::triggered, [this, path] {
419418
auto activity = new OpenWalletActivity(m_wallet_controller, this);
420419
connect(activity, &OpenWalletActivity::opened, this, &BitcoinGUI::setCurrentWallet);
421-
connect(activity, &OpenWalletActivity::finished, activity, &QObject::deleteLater);
422420
activity->open(path);
423421
});
424422
}
@@ -433,7 +431,6 @@ void BitcoinGUI::createActions()
433431
connect(m_create_wallet_action, &QAction::triggered, [this] {
434432
auto activity = new CreateWalletActivity(m_wallet_controller, this);
435433
connect(activity, &CreateWalletActivity::created, this, &BitcoinGUI::setCurrentWallet);
436-
connect(activity, &CreateWalletActivity::finished, activity, &QObject::deleteLater);
437434
activity->create();
438435
});
439436
connect(m_close_all_wallets_action, &QAction::triggered, [this] {
@@ -664,9 +661,8 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller)
664661
GUIUtil::ExceptionSafeConnect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
665662
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);
666663

667-
for (WalletModel* wallet_model : m_wallet_controller->getOpenWallets()) {
668-
addWallet(wallet_model);
669-
}
664+
auto activity = new LoadWalletsActivity(m_wallet_controller, this);
665+
activity->load();
670666
}
671667

672668
WalletController* BitcoinGUI::getWalletController()

src/qt/walletcontroller.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ WalletController::WalletController(ClientModel& client_model, const PlatformStyl
4141
getOrCreateWallet(std::move(wallet));
4242
});
4343

44-
for (std::unique_ptr<interfaces::Wallet>& wallet : m_node.walletClient().getWallets()) {
45-
getOrCreateWallet(std::move(wallet));
46-
}
47-
4844
m_activity_worker->moveToThread(m_activity_thread);
4945
m_activity_thread->start();
5046
QTimer::singleShot(0, m_activity_worker, []() {
@@ -61,12 +57,6 @@ WalletController::~WalletController()
6157
delete m_activity_worker;
6258
}
6359

64-
std::vector<WalletModel*> WalletController::getOpenWallets() const
65-
{
66-
QMutexLocker locker(&m_mutex);
67-
return m_wallets;
68-
}
69-
7060
std::map<std::string, bool> WalletController::listWalletDir() const
7161
{
7262
QMutexLocker locker(&m_mutex);
@@ -191,33 +181,23 @@ WalletControllerActivity::WalletControllerActivity(WalletController* wallet_cont
191181
, m_wallet_controller(wallet_controller)
192182
, m_parent_widget(parent_widget)
193183
{
194-
}
195-
196-
WalletControllerActivity::~WalletControllerActivity()
197-
{
198-
delete m_progress_dialog;
184+
connect(this, &WalletControllerActivity::finished, this, &QObject::deleteLater);
199185
}
200186

201187
void WalletControllerActivity::showProgressDialog(const QString& label_text)
202188
{
203-
assert(!m_progress_dialog);
204-
m_progress_dialog = new QProgressDialog(m_parent_widget);
205-
206-
m_progress_dialog->setLabelText(label_text);
207-
m_progress_dialog->setRange(0, 0);
208-
m_progress_dialog->setCancelButton(nullptr);
209-
m_progress_dialog->setWindowModality(Qt::ApplicationModal);
210-
GUIUtil::PolishProgressDialog(m_progress_dialog);
189+
auto progress_dialog = new QProgressDialog(m_parent_widget);
190+
progress_dialog->setAttribute(Qt::WA_DeleteOnClose);
191+
connect(this, &WalletControllerActivity::finished, progress_dialog, &QWidget::close);
192+
193+
progress_dialog->setLabelText(label_text);
194+
progress_dialog->setRange(0, 0);
195+
progress_dialog->setCancelButton(nullptr);
196+
progress_dialog->setWindowModality(Qt::ApplicationModal);
197+
GUIUtil::PolishProgressDialog(progress_dialog);
211198
// The setValue call forces QProgressDialog to start the internal duration estimation.
212199
// See details in https://bugreports.qt.io/browse/QTBUG-47042.
213-
m_progress_dialog->setValue(0);
214-
}
215-
216-
void WalletControllerActivity::destroyProgressDialog()
217-
{
218-
assert(m_progress_dialog);
219-
delete m_progress_dialog;
220-
m_progress_dialog = nullptr;
200+
progress_dialog->setValue(0);
221201
}
222202

223203
CreateWalletActivity::CreateWalletActivity(WalletController* wallet_controller, QWidget* parent_widget)
@@ -279,8 +259,6 @@ void CreateWalletActivity::createWallet()
279259

280260
void CreateWalletActivity::finish()
281261
{
282-
destroyProgressDialog();
283-
284262
if (!m_error_message.empty()) {
285263
QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), QString::fromStdString(m_error_message.translated));
286264
} else if (!m_warning_message.empty()) {
@@ -329,8 +307,6 @@ OpenWalletActivity::OpenWalletActivity(WalletController* wallet_controller, QWid
329307

330308
void OpenWalletActivity::finish()
331309
{
332-
destroyProgressDialog();
333-
334310
if (!m_error_message.empty()) {
335311
QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message.translated));
336312
} else if (!m_warning_message.empty()) {
@@ -356,3 +332,21 @@ void OpenWalletActivity::open(const std::string& path)
356332
QTimer::singleShot(0, this, &OpenWalletActivity::finish);
357333
});
358334
}
335+
336+
LoadWalletsActivity::LoadWalletsActivity(WalletController* wallet_controller, QWidget* parent_widget)
337+
: WalletControllerActivity(wallet_controller, parent_widget)
338+
{
339+
}
340+
341+
void LoadWalletsActivity::load()
342+
{
343+
showProgressDialog(tr("Loading wallets…"));
344+
345+
QTimer::singleShot(0, worker(), [this] {
346+
for (auto& wallet : node().walletClient().getWallets()) {
347+
m_wallet_controller->getOrCreateWallet(std::move(wallet));
348+
}
349+
350+
QTimer::singleShot(0, this, [this] { Q_EMIT finished(); });
351+
});
352+
}

src/qt/walletcontroller.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ class WalletController : public QObject
5252
WalletController(ClientModel& client_model, const PlatformStyle* platform_style, QObject* parent);
5353
~WalletController();
5454

55-
//! Returns wallet models currently open.
56-
std::vector<WalletModel*> getOpenWallets() const;
57-
5855
WalletModel* getOrCreateWallet(std::unique_ptr<interfaces::Wallet> wallet);
5956

6057
//! Returns all wallet names in the wallet dir mapped to whether the wallet
@@ -90,7 +87,7 @@ class WalletControllerActivity : public QObject
9087

9188
public:
9289
WalletControllerActivity(WalletController* wallet_controller, QWidget* parent_widget);
93-
virtual ~WalletControllerActivity();
90+
virtual ~WalletControllerActivity() = default;
9491

9592
Q_SIGNALS:
9693
void finished();
@@ -100,11 +97,9 @@ class WalletControllerActivity : public QObject
10097
QObject* worker() const { return m_wallet_controller->m_activity_worker; }
10198

10299
void showProgressDialog(const QString& label_text);
103-
void destroyProgressDialog();
104100

105101
WalletController* const m_wallet_controller;
106102
QWidget* const m_parent_widget;
107-
QProgressDialog* m_progress_dialog{nullptr};
108103
WalletModel* m_wallet_model{nullptr};
109104
bilingual_str m_error_message;
110105
std::vector<bilingual_str> m_warning_message;
@@ -150,4 +145,14 @@ class OpenWalletActivity : public WalletControllerActivity
150145
void finish();
151146
};
152147

148+
class LoadWalletsActivity : public WalletControllerActivity
149+
{
150+
Q_OBJECT
151+
152+
public:
153+
LoadWalletsActivity(WalletController* wallet_controller, QWidget* parent_widget);
154+
155+
void load();
156+
};
157+
153158
#endif // BITCOIN_QT_WALLETCONTROLLER_H

0 commit comments

Comments
 (0)