Skip to content

Commit 72c3a9b

Browse files
committed
qml: Add total calculation to SendRecipientsListModel
1 parent 6ef1148 commit 72c3a9b

8 files changed

+42
-19
lines changed

src/qml/bitcoinamount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void BitcoinAmount::flipUnit()
8888
Q_EMIT amountChanged();
8989
}
9090

91-
QString BitcoinAmount::satsToBtc(qint64 sat)
91+
QString BitcoinAmount::satsToBtcString(qint64 sat)
9292
{
9393
const bool negative = sat < 0;
9494
qint64 absSat = negative ? -sat : sat;
@@ -112,7 +112,7 @@ QString BitcoinAmount::toDisplay() const
112112
if (m_unit == Unit::SAT) {
113113
return QString::number(m_satoshi);
114114
} else {
115-
return satsToBtc(m_satoshi);
115+
return satsToBtcString(m_satoshi);
116116
}
117117
}
118118

src/qml/bitcoinamount.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class BitcoinAmount : public QObject
3636
qint64 satoshi() const;
3737
void setSatoshi(qint64 new_amount);
3838

39+
static QString satsToBtcString(qint64 sat);
40+
3941
public Q_SLOTS:
4042
void flipUnit();
4143
void clear();
@@ -46,7 +48,6 @@ public Q_SLOTS:
4648

4749
private:
4850
QString sanitize(const QString& text);
49-
static QString satsToBtc(qint64 sat);
5051
static qint64 btcToSats(const QString& btc);
5152

5253
qint64 m_satoshi{0};

src/qml/models/coinslistmodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ QString CoinsListModel::totalSelected() const
122122

123123
QString CoinsListModel::changeAmount() const
124124
{
125-
CAmount change = m_total_amount - m_wallet_model->sendRecipient()->cAmount();
125+
CAmount change = m_total_amount - m_wallet_model->sendRecipientList()->totalAmountSatoshi();
126126
change = std::abs(change);
127127
return BitcoinUnits::format(BitcoinUnits::Unit::BTC, change);
128128
}
129129

130130
bool CoinsListModel::overRequiredAmount() const
131131
{
132-
return m_total_amount > m_wallet_model->sendRecipient()->cAmount();
132+
return m_total_amount > m_wallet_model->sendRecipientList()->totalAmountSatoshi();
133133
}
134134

135135
int CoinsListModel::coinCount() const

src/qml/models/sendrecipientslistmodel.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <qml/models/sendrecipientslistmodel.h>
6-
#include <qobjectdefs.h>
76

87
#include <qml/models/sendrecipient.h>
98

109
SendRecipientsListModel::SendRecipientsListModel(QObject* parent)
1110
: QAbstractListModel(parent)
1211
{
13-
m_recipients.append(new SendRecipient(this));
12+
auto* recipient = new SendRecipient(this);
13+
connect(recipient->amount(), &BitcoinAmount::amountChanged,
14+
this, &SendRecipientsListModel::updateTotalAmount);
15+
m_recipients.append(recipient);
1416
}
1517

1618
int SendRecipientsListModel::rowCount(const QModelIndex&) const
@@ -48,7 +50,10 @@ void SendRecipientsListModel::add()
4850
{
4951
const int row = m_recipients.size();
5052
beginInsertRows(QModelIndex(), row, row);
51-
m_recipients.append(new SendRecipient(this));
53+
auto* recipient = new SendRecipient(this);
54+
connect(recipient->amount(), &BitcoinAmount::amountChanged,
55+
this, &SendRecipientsListModel::updateTotalAmount);
56+
m_recipients.append(recipient);
5257
endInsertRows();
5358
Q_EMIT countChanged();
5459
setCurrentIndex(row);
@@ -98,3 +103,18 @@ SendRecipient* SendRecipientsListModel::currentRecipient() const
98103

99104
return m_recipients[m_current];
100105
}
106+
107+
void SendRecipientsListModel::updateTotalAmount()
108+
{
109+
qint64 total = 0;
110+
for (const auto& recipient : m_recipients) {
111+
total += recipient->amount()->satoshi();
112+
}
113+
m_totalAmount = total;
114+
Q_EMIT totalAmountChanged();
115+
}
116+
117+
QString SendRecipientsListModel::totalAmount() const
118+
{
119+
return BitcoinAmount::satsToBtcString(m_totalAmount);
120+
}

src/qml/models/sendrecipientslistmodel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SendRecipientsListModel : public QAbstractListModel
1717
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
1818
Q_PROPERTY(int count READ count NOTIFY countChanged)
1919
Q_PROPERTY(SendRecipient* current READ currentRecipient NOTIFY currentRecipientChanged)
20+
Q_PROPERTY(QString totalAmount READ totalAmount NOTIFY totalAmountChanged)
2021

2122
public:
2223
enum Roles {
@@ -42,15 +43,21 @@ class SendRecipientsListModel : public QAbstractListModel
4243
SendRecipient* currentRecipient() const;
4344
int count() const { return m_recipients.size(); }
4445
QList<SendRecipient*> recipients() const { return m_recipients; }
46+
QString totalAmount() const;
47+
qint64 totalAmountSatoshi() const { return m_totalAmount; }
4548

4649
Q_SIGNALS:
4750
void currentIndexChanged();
4851
void currentRecipientChanged();
4952
void countChanged();
53+
void totalAmountChanged();
5054

5155
private:
56+
void updateTotalAmount();
57+
5258
QList<SendRecipient*> m_recipients;
5359
int m_current{0};
60+
qint64 m_totalAmount{0};
5461
};
5562

5663
#endif // BITCOIN_QML_MODELS_SENDRECIPIENTSLISTMODEL_H

src/qml/models/walletqmlmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool WalletQmlModel::prepareTransaction()
130130
delete m_current_transaction;
131131
}
132132
CTransactionRef newTx = *res;
133-
m_current_transaction = new WalletQmlModelTransaction(m_current_recipient, this);
133+
m_current_transaction = new WalletQmlModelTransaction(m_send_recipients, this);
134134
m_current_transaction->setWtx(newTx);
135135
m_current_transaction->setTransactionFee(nFeeRequired);
136136
Q_EMIT currentTransactionChanged();

src/qml/models/walletqmlmodeltransaction.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
#include <qml/models/walletqmlmodeltransaction.h>
66

77
#include <policy/policy.h>
8-
#include <qobject.h>
98

10-
WalletQmlModelTransaction::WalletQmlModelTransaction(const SendRecipient* recipient, QObject* parent)
11-
: QObject(parent), m_address(recipient->address()), m_amount(recipient->cAmount()), m_fee(0), m_label(recipient->label()), m_wtx(nullptr)
9+
WalletQmlModelTransaction::WalletQmlModelTransaction(const SendRecipientsListModel* recipient, QObject* parent)
10+
: QObject(parent), m_address(recipient->recipients().at(0)->address()), m_amount(recipient->totalAmountSatoshi()), m_fee(0), m_label(recipient->recipients().at(0)->label()), m_wtx(nullptr)
1211
{
1312
}
1413

src/qml/models/walletqmlmodeltransaction.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
#ifndef BITCOIN_QML_MODELS_WALLETQMLMODELTRANSACTION_H
66
#define BITCOIN_QML_MODELS_WALLETQMLMODELTRANSACTION_H
77

8-
#include <primitives/transaction.h>
9-
#include <qml/models/sendrecipient.h>
8+
#include <qml/models/sendrecipientslistmodel.h>
109

1110
#include <consensus/amount.h>
12-
13-
#include <QObject>
11+
#include <primitives/transaction.h>
1412

1513

1614
class WalletQmlModelTransaction : public QObject
@@ -22,16 +20,14 @@ class WalletQmlModelTransaction : public QObject
2220
Q_PROPERTY(QString fee READ fee NOTIFY feeChanged)
2321
Q_PROPERTY(QString total READ total NOTIFY totalChanged)
2422
public:
25-
explicit WalletQmlModelTransaction(const SendRecipient* recipient, QObject* parent = nullptr);
23+
explicit WalletQmlModelTransaction(const SendRecipientsListModel* recipient, QObject* parent = nullptr);
2624

2725
QString address() const;
2826
QString amount() const;
2927
QString fee() const;
3028
QString label() const;
3129
QString total() const;
3230

33-
QList<SendRecipient> getRecipients() const;
34-
3531
CTransactionRef& getWtx();
3632
void setWtx(const CTransactionRef&);
3733

0 commit comments

Comments
 (0)