Skip to content

Commit

Permalink
[GUI][Model] GetBalance exluding delegations - add UI controls to send
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Mar 14, 2020
1 parent 3ea22e5 commit 4715915
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 35 deletions.
21 changes: 17 additions & 4 deletions src/qt/pivx/forms/send.ui
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>send</class>
<widget class="QWidget" name="send">
Expand Down Expand Up @@ -366,17 +366,30 @@
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>20</number>
</property>
<property name="rightMargin">
<number>20</number>
</property>
<item>
<widget class="QCheckBox" name="checkBoxDelegations">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Include delegated</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<width>10</width>
<height>20</height>
</size>
</property>
Expand All @@ -402,7 +415,7 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<width>10</width>
<height>20</height>
</size>
</property>
Expand Down Expand Up @@ -431,7 +444,7 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<width>10</width>
<height>20</height>
</size>
</property>
Expand Down
69 changes: 45 additions & 24 deletions src/qt/pivx/send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ SendWidget::SendWidget(PIVXGUI* parent) :
ui->pushRight->setText("zPIV");
setCssProperty(ui->pushRight, "btn-check-right");

/* CheckBox */
ui->checkBoxDelegations->setToolTip(tr("Possibly spend coins delegated for cold-staking, if available"));

/* Subtitle */
ui->labelSubtitle1->setText(tr("You can transfer public coins (PIV) or private coins (zPIV)"));
setCssProperty(ui->labelSubtitle1, "text-subtitle");
Expand Down Expand Up @@ -99,6 +102,7 @@ SendWidget::SendWidget(PIVXGUI* parent) :
connect(ui->btnChangeAddress, SIGNAL(clicked()), this, SLOT(onChangeAddressClicked()));
connect(ui->btnUri, SIGNAL(clicked()), this, SLOT(onOpenUriClicked()));
connect(ui->pushButtonReset, &QPushButton::clicked, [this](){ onResetCustomOptions(true); });
connect(ui->checkBoxDelegations, SIGNAL(stateChanged(int)), this, SLOT(onCheckBoxChanged()));

setCssProperty(ui->coinWidget, "container-coin-type");
setCssProperty(ui->labelLine, "container-divider");
Expand Down Expand Up @@ -142,22 +146,16 @@ SendWidget::SendWidget(PIVXGUI* parent) :
connect(ui->pushButtonClear, SIGNAL(clicked()), this, SLOT(clearAll()));
}

void SendWidget::refreshView(){
QString btnText;
if(ui->pushLeft->isChecked()){
btnText = tr("Send PIV");
ui->pushButtonAddRecipient->setVisible(true);
}else{
btnText = tr("Send zPIV");
ui->pushButtonAddRecipient->setVisible(false);
}
ui->pushButtonSave->setText(btnText);

void SendWidget::refreshView()
{
const bool isChecked = ui->pushLeft->isChecked();
ui->pushButtonSave->setText(isChecked ? tr("Send PIV") : tr("Send zPIV"));
ui->pushButtonAddRecipient->setVisible(isChecked);
refreshAmounts();
}

void SendWidget::refreshAmounts() {

void SendWidget::refreshAmounts()
{
CAmount total = 0;
QMutableListIterator<SendMultiRow*> it(entries);
while (it.hasNext()) {
Expand All @@ -173,14 +171,20 @@ void SendWidget::refreshAmounts() {
ui->labelAmountSend->setText(GUIUtil::formatBalance(total, nDisplayUnit, isZpiv));

CAmount totalAmount = 0;
if (CoinControlDialog::coinControl->HasSelected()){
if (CoinControlDialog::coinControl->HasSelected()) {
// Set remaining balance to the sum of the coinControl selected inputs
totalAmount = walletModel->getBalance(CoinControlDialog::coinControl) - total;
ui->labelTitleTotalRemaining->setText(tr("Total remaining from the selected UTXO"));
// Hide delegations checkbox
if (ui->checkBoxDelegations->isVisible()) ui->checkBoxDelegations->setVisible(false);
} else {
// Wallet's balance
totalAmount = (isZpiv ? walletModel->getZerocoinBalance() : walletModel->getBalance()) - total;
totalAmount = (isZpiv ?
walletModel->getZerocoinBalance() :
walletModel->getBalance(nullptr, fDelegationsChecked)) - total;
ui->labelTitleTotalRemaining->setText(tr("Total remaining"));
// Show delegations checkbox (if not Zpiv, else hide)
if (ui->checkBoxDelegations->isVisible() == isZpiv) ui->checkBoxDelegations->setVisible(!isZpiv);
}
ui->labelAmountRemaining->setText(
GUIUtil::formatBalance(
Expand All @@ -191,15 +195,17 @@ void SendWidget::refreshAmounts() {
);
}

void SendWidget::loadClientModel(){
void SendWidget::loadClientModel()
{
if (clientModel) {
connect(clientModel, &ClientModel::numBlocksChanged, [this](){
if (customFeeDialog) customFeeDialog->updateFee();
});
}
}

void SendWidget::loadWalletModel() {
void SendWidget::loadWalletModel()
{
if (walletModel && walletModel->getOptionsModel()) {
// display unit
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
Expand All @@ -221,7 +227,8 @@ void SendWidget::loadWalletModel() {
}
}

void SendWidget::clearAll(){
void SendWidget::clearAll()
{
onResetCustomOptions(false);
if(customFeeDialog) customFeeDialog->clear();
ui->pushButtonFee->setText(tr("Customize Fee"));
Expand All @@ -230,16 +237,20 @@ void SendWidget::clearAll(){
refreshAmounts();
}

void SendWidget::onResetCustomOptions(bool fRefreshAmounts){
void SendWidget::onResetCustomOptions(bool fRefreshAmounts)
{
CoinControlDialog::coinControl->SetNull();
ui->btnChangeAddress->setActive(false);
ui->btnCoinControl->setActive(false);
if (!ui->checkBoxDelegations->isVisible()) ui->checkBoxDelegations->setVisible(true);
if (ui->checkBoxDelegations->isChecked()) ui->checkBoxDelegations->setChecked(false);
if (fRefreshAmounts) {
refreshAmounts();
}
}

void SendWidget::clearEntries(){
void SendWidget::clearEntries()
{
int num = entries.length();
for (int i = 0; i < num; ++i) {
ui->scrollAreaWidgetContents->layout()->takeAt(0)->widget()->deleteLater();
Expand Down Expand Up @@ -296,8 +307,8 @@ void SendWidget::resizeEvent(QResizeEvent *event){
}


void SendWidget::onSendClicked(){

void SendWidget::onSendClicked()
{
if (!walletModel || !walletModel->getOptionsModel())
return;

Expand Down Expand Up @@ -613,11 +624,21 @@ void SendWidget::onCoinControlClicked(){
}
}

void SendWidget::onValueChanged() {
void SendWidget::onValueChanged()
{
refreshAmounts();
}

void SendWidget::onPIVSelected(bool _isPIV){
void SendWidget::onCheckBoxChanged()
{
const bool checked = ui->checkBoxDelegations->isChecked();
if (checked != fDelegationsChecked) {
fDelegationsChecked = checked;
refreshAmounts();
}
}
void SendWidget::onPIVSelected(bool _isPIV)
{
isPIV = _isPIV;
setCssProperty(coinIcon, _isPIV ? "coin-icon-piv" : "coin-icon-zpiv");
refreshView();
Expand Down
2 changes: 2 additions & 0 deletions src/qt/pivx/send.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private Q_SLOTS:
void clearEntries();
void clearAll();
void refreshView();
void onCheckBoxChanged();
void onContactMultiClicked();
void onDeleteClicked();
void onResetCustomOptions(bool fRefreshAmounts);
Expand All @@ -77,6 +78,7 @@ private Q_SLOTS:

SendCustomFeeDialog* customFeeDialog = nullptr;
bool isCustomFeeSelected = false;
bool fDelegationsChecked = false;

int nDisplayUnit;
QList<SendMultiRow*> entries;
Expand Down
4 changes: 2 additions & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool WalletModel::isStakingStatusActive() const {
return wallet->pStakerStatus->IsActive();
}

CAmount WalletModel::getBalance(const CCoinControl* coinControl) const
CAmount WalletModel::getBalance(const CCoinControl* coinControl, bool fIncludeDelegated) const
{
if (coinControl) {
CAmount nBalance = 0;
Expand All @@ -89,7 +89,7 @@ CAmount WalletModel::getBalance(const CCoinControl* coinControl) const
return nBalance;
}

return wallet->GetBalance();
return wallet->GetBalance(fIncludeDelegated);
}

CAmount WalletModel::getMinColdStakingAmount() const
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class WalletModel : public QObject
/* current staking status from the miner thread **/
bool isStakingStatusActive() const;

CAmount getBalance(const CCoinControl* coinControl = NULL) const;
CAmount getBalance(const CCoinControl* coinControl = NULL, bool fIncludeDelegated = true) const;
CAmount getUnconfirmedBalance() const;
CAmount getImmatureBalance() const;
CAmount getLockedBalance() const;
Expand Down
8 changes: 5 additions & 3 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1754,11 +1754,13 @@ CAmount CWallet::loopTxsBalance(std::function<void(const uint256&, const CWallet
return nTotal;
}

CAmount CWallet::GetBalance() const
CAmount CWallet::GetBalance(bool fIncludeDelegated) const
{
return loopTxsBalance([](const uint256& id, const CWalletTx& pcoin, CAmount& nTotal){
if (pcoin.IsTrusted())
return loopTxsBalance([fIncludeDelegated](const uint256& id, const CWalletTx& pcoin, CAmount& nTotal){
if (pcoin.IsTrusted()) {
nTotal += pcoin.GetAvailableCredit();
if (!fIncludeDelegated) nTotal -= pcoin.GetStakeDelegationCredit();
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
void ResendWalletTransactions();

CAmount loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)>method) const;
CAmount GetBalance() const;
CAmount GetBalance(bool fIncludeDelegated = true) const;
CAmount GetColdStakingBalance() const; // delegated coins for which we have the staking key
CAmount GetImmatureColdStakingBalance() const;
CAmount GetStakingBalance(const bool fIncludeColdStaking = true) const;
Expand Down

0 comments on commit 4715915

Please sign in to comment.