Use WalletBalances struct in Qt

Suggested by John Newbery <john@johnnewbery.com>
https://github.com/bitcoin/bitcoin/pull/10244#discussion_r177504284
This commit is contained in:
Russell Yanofsky 2018-03-31 06:41:33 -04:00 committed by John Newbery
parent 56f33ca349
commit 9a61eed1fc
6 changed files with 32 additions and 56 deletions

View file

@ -21,6 +21,8 @@
#define DECORATION_SIZE 54 #define DECORATION_SIZE 54
#define NUM_ITEMS 5 #define NUM_ITEMS 5
Q_DECLARE_METATYPE(interface::WalletBalances)
class TxViewDelegate : public QAbstractItemDelegate class TxViewDelegate : public QAbstractItemDelegate
{ {
Q_OBJECT Q_OBJECT
@ -113,16 +115,12 @@ OverviewPage::OverviewPage(const PlatformStyle *platformStyle, QWidget *parent)
ui(new Ui::OverviewPage), ui(new Ui::OverviewPage),
clientModel(0), clientModel(0),
walletModel(0), walletModel(0),
currentBalance(-1),
currentUnconfirmedBalance(-1),
currentImmatureBalance(-1),
currentWatchOnlyBalance(-1),
currentWatchUnconfBalance(-1),
currentWatchImmatureBalance(-1),
txdelegate(new TxViewDelegate(platformStyle, this)) txdelegate(new TxViewDelegate(platformStyle, this))
{ {
ui->setupUi(this); ui->setupUi(this);
m_balances.balance = -1;
// use a SingleColorIcon for the "out of sync warning" icon // use a SingleColorIcon for the "out of sync warning" icon
QIcon icon = platformStyle->SingleColorIcon(":/icons/warning"); QIcon icon = platformStyle->SingleColorIcon(":/icons/warning");
icon.addPixmap(icon.pixmap(QSize(64,64), QIcon::Normal), QIcon::Disabled); // also set the disabled icon because we are using a disabled QPushButton to work around missing HiDPI support of QLabel (https://bugreports.qt.io/browse/QTBUG-42503) icon.addPixmap(icon.pixmap(QSize(64,64), QIcon::Normal), QIcon::Disabled); // also set the disabled icon because we are using a disabled QPushButton to work around missing HiDPI support of QLabel (https://bugreports.qt.io/browse/QTBUG-42503)
@ -159,28 +157,23 @@ OverviewPage::~OverviewPage()
delete ui; delete ui;
} }
void OverviewPage::setBalance(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance) void OverviewPage::setBalance(const interface::WalletBalances& balances)
{ {
int unit = walletModel->getOptionsModel()->getDisplayUnit(); int unit = walletModel->getOptionsModel()->getDisplayUnit();
currentBalance = balance; m_balances = balances;
currentUnconfirmedBalance = unconfirmedBalance; ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balances.balance, false, BitcoinUnits::separatorAlways));
currentImmatureBalance = immatureBalance; ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, balances.unconfirmed_balance, false, BitcoinUnits::separatorAlways));
currentWatchOnlyBalance = watchOnlyBalance; ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, balances.immature_balance, false, BitcoinUnits::separatorAlways));
currentWatchUnconfBalance = watchUnconfBalance; ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balances.balance + balances.unconfirmed_balance + balances.immature_balance, false, BitcoinUnits::separatorAlways));
currentWatchImmatureBalance = watchImmatureBalance; ui->labelWatchAvailable->setText(BitcoinUnits::formatWithUnit(unit, balances.watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(unit, balance, false, BitcoinUnits::separatorAlways)); ui->labelWatchPending->setText(BitcoinUnits::formatWithUnit(unit, balances.unconfirmed_watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(unit, unconfirmedBalance, false, BitcoinUnits::separatorAlways)); ui->labelWatchImmature->setText(BitcoinUnits::formatWithUnit(unit, balances.immature_watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(unit, immatureBalance, false, BitcoinUnits::separatorAlways)); ui->labelWatchTotal->setText(BitcoinUnits::formatWithUnit(unit, balances.watch_only_balance + balances.unconfirmed_watch_only_balance + balances.immature_watch_only_balance, false, BitcoinUnits::separatorAlways));
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(unit, balance + unconfirmedBalance + immatureBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchAvailable->setText(BitcoinUnits::formatWithUnit(unit, watchOnlyBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchPending->setText(BitcoinUnits::formatWithUnit(unit, watchUnconfBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchImmature->setText(BitcoinUnits::formatWithUnit(unit, watchImmatureBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchTotal->setText(BitcoinUnits::formatWithUnit(unit, watchOnlyBalance + watchUnconfBalance + watchImmatureBalance, false, BitcoinUnits::separatorAlways));
// only show immature (newly mined) balance if it's non-zero, so as not to complicate things // only show immature (newly mined) balance if it's non-zero, so as not to complicate things
// for the non-mining users // for the non-mining users
bool showImmature = immatureBalance != 0; bool showImmature = balances.immature_balance != 0;
bool showWatchOnlyImmature = watchImmatureBalance != 0; bool showWatchOnlyImmature = balances.immature_watch_only_balance != 0;
// for symmetry reasons also show immature label when the watch-only one is shown // for symmetry reasons also show immature label when the watch-only one is shown
ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature); ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature);
@ -233,9 +226,8 @@ void OverviewPage::setWalletModel(WalletModel *model)
// Keep up to date with wallet // Keep up to date with wallet
interface::Wallet& wallet = model->wallet(); interface::Wallet& wallet = model->wallet();
interface::WalletBalances balances = wallet.getBalances(); interface::WalletBalances balances = wallet.getBalances();
setBalance(balances.balance, balances.unconfirmed_balance, balances.immature_balance, setBalance(balances);
balances.watch_only_balance, balances.unconfirmed_watch_only_balance, balances.immature_watch_only_balance); connect(model, SIGNAL(balanceChanged(interface::WalletBalances)), this, SLOT(setBalance(interface::WalletBalances)));
connect(model, SIGNAL(balanceChanged(CAmount,CAmount,CAmount,CAmount,CAmount,CAmount)), this, SLOT(setBalance(CAmount,CAmount,CAmount,CAmount,CAmount,CAmount)));
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit())); connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
@ -251,9 +243,9 @@ void OverviewPage::updateDisplayUnit()
{ {
if(walletModel && walletModel->getOptionsModel()) if(walletModel && walletModel->getOptionsModel())
{ {
if(currentBalance != -1) if (m_balances.balance != -1) {
setBalance(currentBalance, currentUnconfirmedBalance, currentImmatureBalance, setBalance(m_balances);
currentWatchOnlyBalance, currentWatchUnconfBalance, currentWatchImmatureBalance); }
// Update txdelegate->unit with the current unit // Update txdelegate->unit with the current unit
txdelegate->unit = walletModel->getOptionsModel()->getDisplayUnit(); txdelegate->unit = walletModel->getOptionsModel()->getDisplayUnit();

View file

@ -5,7 +5,7 @@
#ifndef BITCOIN_QT_OVERVIEWPAGE_H #ifndef BITCOIN_QT_OVERVIEWPAGE_H
#define BITCOIN_QT_OVERVIEWPAGE_H #define BITCOIN_QT_OVERVIEWPAGE_H
#include <amount.h> #include <interface/wallet.h>
#include <QWidget> #include <QWidget>
#include <memory> #include <memory>
@ -38,8 +38,7 @@ public:
void showOutOfSyncWarning(bool fShow); void showOutOfSyncWarning(bool fShow);
public Q_SLOTS: public Q_SLOTS:
void setBalance(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, void setBalance(const interface::WalletBalances& balances);
const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
Q_SIGNALS: Q_SIGNALS:
void transactionClicked(const QModelIndex &index); void transactionClicked(const QModelIndex &index);
@ -49,12 +48,7 @@ private:
Ui::OverviewPage *ui; Ui::OverviewPage *ui;
ClientModel *clientModel; ClientModel *clientModel;
WalletModel *walletModel; WalletModel *walletModel;
CAmount currentBalance; interface::WalletBalances m_balances;
CAmount currentUnconfirmedBalance;
CAmount currentImmatureBalance;
CAmount currentWatchOnlyBalance;
CAmount currentWatchUnconfBalance;
CAmount currentWatchImmatureBalance;
TxViewDelegate *txdelegate; TxViewDelegate *txdelegate;
std::unique_ptr<TransactionFilterProxy> filter; std::unique_ptr<TransactionFilterProxy> filter;

View file

@ -150,9 +150,8 @@ void SendCoinsDialog::setModel(WalletModel *_model)
} }
interface::WalletBalances balances = _model->wallet().getBalances(); interface::WalletBalances balances = _model->wallet().getBalances();
setBalance(balances.balance, balances.unconfirmed_balance, balances.immature_balance, setBalance(balances);
balances.watch_only_balance, balances.unconfirmed_watch_only_balance, balances.immature_watch_only_balance); connect(_model, SIGNAL(balanceChanged(interface::WalletBalances)), this, SLOT(setBalance(interface::WalletBalances)));
connect(_model, SIGNAL(balanceChanged(CAmount,CAmount,CAmount,CAmount,CAmount,CAmount)), this, SLOT(setBalance(CAmount,CAmount,CAmount,CAmount,CAmount,CAmount)));
connect(_model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit())); connect(_model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
updateDisplayUnit(); updateDisplayUnit();
@ -516,24 +515,17 @@ bool SendCoinsDialog::handlePaymentRequest(const SendCoinsRecipient &rv)
return true; return true;
} }
void SendCoinsDialog::setBalance(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, void SendCoinsDialog::setBalance(const interface::WalletBalances& balances)
const CAmount& watchBalance, const CAmount& watchUnconfirmedBalance, const CAmount& watchImmatureBalance)
{ {
Q_UNUSED(unconfirmedBalance);
Q_UNUSED(immatureBalance);
Q_UNUSED(watchBalance);
Q_UNUSED(watchUnconfirmedBalance);
Q_UNUSED(watchImmatureBalance);
if(model && model->getOptionsModel()) if(model && model->getOptionsModel())
{ {
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), balance)); ui->labelBalance->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), balances.balance));
} }
} }
void SendCoinsDialog::updateDisplayUnit() void SendCoinsDialog::updateDisplayUnit()
{ {
setBalance(model->wallet().getBalance(), 0, 0, 0, 0, 0); setBalance(model->wallet().getBalances());
ui->customFee->setDisplayUnit(model->getOptionsModel()->getDisplayUnit()); ui->customFee->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
updateMinFeeLabel(); updateMinFeeLabel();
updateSmartFeeLabel(); updateSmartFeeLabel();

View file

@ -51,8 +51,7 @@ public Q_SLOTS:
void accept(); void accept();
SendCoinsEntry *addEntry(); SendCoinsEntry *addEntry();
void updateTabsAndLabels(); void updateTabsAndLabels();
void setBalance(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, void setBalance(const interface::WalletBalances& balances);
const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
Q_SIGNALS: Q_SIGNALS:
void coinsSent(const uint256& txid); void coinsSent(const uint256& txid);

View file

@ -93,7 +93,7 @@ void WalletModel::checkBalanceChanged(const interface::WalletBalances& new_balan
{ {
if(new_balances.balanceChanged(m_cached_balances)) { if(new_balances.balanceChanged(m_cached_balances)) {
m_cached_balances = new_balances; m_cached_balances = new_balances;
Q_EMIT balanceChanged(new_balances.balance, new_balances.unconfirmed_balance, new_balances.immature_balance, new_balances.watch_only_balance, new_balances.unconfirmed_watch_only_balance, new_balances.immature_watch_only_balance); Q_EMIT balanceChanged(new_balances);
} }
} }

View file

@ -237,8 +237,7 @@ private:
Q_SIGNALS: Q_SIGNALS:
// Signal that balance in wallet changed // Signal that balance in wallet changed
void balanceChanged(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance, void balanceChanged(const interface::WalletBalances& balances);
const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance);
// Encryption status of wallet changed // Encryption status of wallet changed
void encryptionStatusChanged(); void encryptionStatusChanged();