Skip to content

Commit 379e5f2

Browse files
committed
GUI: send screen, move refresh amounts calculation to a background worker thread.
1 parent 76bc08b commit 379e5f2

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

src/qt/pivx/send.cpp

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "openuridialog.h"
2121

2222
#define REQUEST_PREPARE_TX 1
23+
#define REQUEST_REFRESH_BALANCE 2
2324

2425
SendWidget::SendWidget(PIVXGUI* parent) :
2526
PWidget(parent),
@@ -142,9 +143,9 @@ void SendWidget::refreshAmounts()
142143
}
143144

144145
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
145-
ui->labelAmountSend->setText(GUIUtil::formatBalance(total, nDisplayUnit, false));
146146

147147
CAmount totalAmount = 0;
148+
QString titleTotalRemaining;
148149
if (coinControlDialog->coinControl->HasSelected()) {
149150
// Set remaining balance to the sum of the coinControl selected inputs
150151
std::vector<OutPointWrapper> coins;
@@ -154,20 +155,27 @@ void SendWidget::refreshAmounts()
154155
selectedBalance += coin.value;
155156
}
156157
totalAmount = selectedBalance - total;
157-
ui->labelTitleTotalRemaining->setText(tr("Total remaining from the selected UTXO"));
158+
titleTotalRemaining = tr("Total remaining from the selected UTXO");
158159
} else {
159160
// Wallet's unlocked balance.
160161
totalAmount = isTransparent ? (walletModel->getUnlockedBalance(nullptr, fDelegationsChecked, false) - total)
161162
: (walletModel->GetWalletBalances().shielded_balance - total);
162-
ui->labelTitleTotalRemaining->setText(tr("Unlocked remaining"));
163+
titleTotalRemaining = tr("Unlocked remaining");
163164
}
165+
164166
QString type = isTransparent ? "transparent" : "shielded";
165-
ui->labelAmountRemaining->setText(
166-
GUIUtil::formatBalance(
167-
totalAmount,
168-
nDisplayUnit,
169-
false) + " " + type
170-
);
167+
QString labelAmountRemaining = GUIUtil::formatBalance( totalAmount, nDisplayUnit, false) + " " + type;
168+
QMetaObject::invokeMethod(this, "updateAmounts", Qt::QueuedConnection,
169+
Q_ARG(QString, titleTotalRemaining),
170+
Q_ARG(QString, GUIUtil::formatBalance(total, nDisplayUnit, false)),
171+
Q_ARG(QString, labelAmountRemaining));
172+
}
173+
174+
void SendWidget::updateAmounts(const QString& _titleTotalRemaining, const QString& _labelAmountSend, const QString& _labelAmountRemaining)
175+
{
176+
ui->labelTitleTotalRemaining->setText(_titleTotalRemaining);
177+
ui->labelAmountSend->setText(_labelAmountSend);
178+
ui->labelAmountRemaining->setText(_labelAmountRemaining);
171179
// show or hide delegations checkbox if need be
172180
showHideCheckBoxDelegations();
173181
}
@@ -203,9 +211,6 @@ void SendWidget::loadWalletModel()
203211
setCustomFeeSelected(true, nCustomFee);
204212
}
205213

206-
// Refresh
207-
refreshAmounts();
208-
209214
// TODO: This only happen when the coin control features are modified in other screen, check before do this if the wallet has another screen modifying it.
210215
// Coin Control
211216
//connect(walletModel->getOptionsModel(), &OptionsModel::coinControlFeaturesChanged, [this](){});
@@ -227,7 +232,7 @@ void SendWidget::clearAll(bool fClearSettings)
227232
if (fClearSettings) onResetSettings();
228233
hideContactsMenu();
229234
clearEntries();
230-
refreshAmounts();
235+
tryRefreshAmounts();
231236
}
232237

233238
void SendWidget::onResetSettings()
@@ -243,7 +248,7 @@ void SendWidget::onResetCustomOptions(bool fRefreshAmounts)
243248
if (ui->checkBoxDelegations->isChecked()) ui->checkBoxDelegations->setChecked(false);
244249
resetCoinControl();
245250
if (fRefreshAmounts) {
246-
refreshAmounts();
251+
tryRefreshAmounts();
247252
}
248253
}
249254

@@ -326,13 +331,7 @@ void SendWidget::showEvent(QShowEvent *event)
326331
{
327332
// Set focus on last recipient address when Send-window is displayed
328333
setFocusOnLastEntry();
329-
330-
// Update cached delegated balance
331-
CAmount cachedDelegatedBalance_new = walletModel->getDelegatedBalance();
332-
if (cachedDelegatedBalance != cachedDelegatedBalance_new) {
333-
cachedDelegatedBalance = cachedDelegatedBalance_new;
334-
refreshAmounts();
335-
}
334+
tryRefreshAmounts();
336335
}
337336

338337
void SendWidget::setFocusOnLastEntry()
@@ -537,8 +536,8 @@ bool SendWidget::sendFinalStep()
537536

538537
void SendWidget::run(int type)
539538
{
540-
assert(!processingResult);
541539
if (type == REQUEST_PREPARE_TX) {
540+
assert(!processingResult);
542541
if (!isProcessing) {
543542
isProcessing = true;
544543
OperationResult result(false);
@@ -553,6 +552,12 @@ void SendWidget::run(int type)
553552
}
554553
isProcessing = false;
555554
}
555+
} else if (type == REQUEST_REFRESH_BALANCE) {
556+
if (!isUpdatingBalance) {
557+
isUpdatingBalance = true;
558+
refreshAmounts();
559+
isUpdatingBalance = false;
560+
}
556561
}
557562
}
558563

@@ -562,9 +567,16 @@ void SendWidget::onError(QString error, int type)
562567
processingResultError = error;
563568
}
564569

565-
void SendWidget::updateEntryLabels(QList<SendCoinsRecipient> recipients)
570+
void SendWidget::tryRefreshAmounts()
571+
{
572+
if (!execute(REQUEST_REFRESH_BALANCE)) {
573+
inform(tr("Processing full, refreshing amounts later"));
574+
}
575+
}
576+
577+
void SendWidget::updateEntryLabels(const QList<SendCoinsRecipient>& recipients)
566578
{
567-
for (SendCoinsRecipient rec : recipients) {
579+
for (const SendCoinsRecipient& rec : recipients) {
568580
QString label = rec.label;
569581
if (!label.isNull()) {
570582
QString labelOld = walletModel->getAddressTableModel()->labelForAddress(rec.address);
@@ -669,7 +681,7 @@ void SendWidget::onCoinControlClicked()
669681
setCoinControlPayAmounts();
670682
coinControlDialog->exec();
671683
ui->btnCoinControl->setActive(coinControlDialog->coinControl->HasSelected());
672-
refreshAmounts();
684+
tryRefreshAmounts();
673685
} else {
674686
inform(tr("You don't have any %1 to select.").arg(CURRENCY_UNIT.c_str()));
675687
}
@@ -752,15 +764,15 @@ void SendWidget::setCoinControlPayAmounts()
752764

753765
void SendWidget::onValueChanged()
754766
{
755-
refreshAmounts();
767+
tryRefreshAmounts();
756768
}
757769

758770
void SendWidget::onCheckBoxChanged()
759771
{
760772
const bool checked = ui->checkBoxDelegations->isChecked();
761773
if (checked != fDelegationsChecked) {
762774
fDelegationsChecked = checked;
763-
refreshAmounts();
775+
tryRefreshAmounts();
764776
}
765777
}
766778

@@ -776,7 +788,7 @@ void SendWidget::onPIVSelected(bool _isTransparent)
776788

777789
resetChangeAddress();
778790
resetCoinControl();
779-
refreshAmounts();
791+
tryRefreshAmounts();
780792
updateStyle(coinIcon);
781793
}
782794

@@ -946,7 +958,7 @@ void SendWidget::onDeleteClicked()
946958
focusedEntry = nullptr;
947959

948960
// Update total amounts
949-
refreshAmounts();
961+
tryRefreshAmounts();
950962
setFocusOnLastEntry();
951963
}
952964
}

src/qt/pivx/send.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public Q_SLOTS:
5757
void onValueChanged();
5858
void refreshAmounts();
5959
void changeTheme(bool isLightTheme, QString &theme) override;
60+
void updateAmounts(const QString& titleTotalRemaining, const QString& labelAmountSend, const QString& labelAmountRemaining);
6061

6162
protected:
6263
void resizeEvent(QResizeEvent *event) override;
@@ -99,6 +100,9 @@ private Q_SLOTS:
99100
Optional<QString> processingResultError{nullopt};
100101
std::atomic<bool> processingResult{false};
101102

103+
// Balance update
104+
std::atomic<bool> isUpdatingBalance{false};
105+
102106
ContactsDropdown *menuContacts = nullptr;
103107
TooltipMenu *menu = nullptr;
104108
// Current focus entry
@@ -114,12 +118,13 @@ private Q_SLOTS:
114118
bool sendFinalStep();
115119
void setFocusOnLastEntry();
116120
void showHideCheckBoxDelegations();
117-
void updateEntryLabels(QList<SendCoinsRecipient> recipients);
121+
void updateEntryLabels(const QList<SendCoinsRecipient>& recipients);
118122
void setCustomFeeSelected(bool isSelected, const CAmount& customFee = DEFAULT_TRANSACTION_FEE);
119123
void setCoinControlPayAmounts();
120124
void resetCoinControl();
121125
void resetChangeAddress();
122126
void hideContactsMenu();
127+
void tryRefreshAmounts();
123128
};
124129

125130
#endif // SEND_H

0 commit comments

Comments
 (0)