Skip to content

Commit 20d6bcd

Browse files
committed
qml: Introduce SendRecipientsListModel
The SendRecipientsListModel is owned by WalletQmlModel
1 parent 25a18f0 commit 20d6bcd

File tree

5 files changed

+170
-6
lines changed

5 files changed

+170
-6
lines changed

src/Makefile.qt.include

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ QT_MOC_CPP = \
4444
qml/models/moc_nodemodel.cpp \
4545
qml/models/moc_options_model.cpp \
4646
qml/models/moc_peerdetailsmodel.cpp \
47-
qml/models/moc_peerlistsortproxy.cpp \
47+
qml/models/moc_peerlistsortproxy.cpp \\
48+
qml/models/moc_sendrecipient.cpp \
49+
qml/models/moc_sendrecipientslistmodel.cpp \
4850
qml/models/moc_transaction.cpp \
4951
qml/models/moc_sendrecipient.cpp \
5052
qml/models/moc_walletlistmodel.cpp \
@@ -138,6 +140,7 @@ BITCOIN_QT_H = \
138140
qml/models/peerlistsortproxy.h \
139141
qml/models/transaction.h \
140142
qml/models/sendrecipient.h \
143+
qml/models/sendrecipientslistmodel.h \
141144
qml/models/walletlistmodel.h \
142145
qml/models/walletqmlmodel.h \
143146
qml/models/walletqmlmodeltransaction.h \
@@ -339,6 +342,7 @@ BITCOIN_QML_BASE_CPP = \
339342
qml/models/peerlistsortproxy.cpp \
340343
qml/models/transaction.cpp \
341344
qml/models/sendrecipient.cpp \
345+
qml/models/sendrecipientslistmodel.cpp \
342346
qml/models/walletlistmodel.cpp \
343347
qml/models/walletqmlmodel.cpp \
344348
qml/models/walletqmlmodeltransaction.cpp \
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) 2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <qml/models/sendrecipientslistmodel.h>
6+
7+
#include <qml/models/sendrecipient.h>
8+
9+
SendRecipientsListModel::SendRecipientsListModel(QObject* parent)
10+
: QAbstractListModel(parent)
11+
{
12+
m_recipients.append(new SendRecipient(this));
13+
}
14+
15+
int SendRecipientsListModel::rowCount(const QModelIndex&) const
16+
{
17+
return m_recipients.size();
18+
}
19+
20+
QVariant SendRecipientsListModel::data(const QModelIndex& index, int role) const
21+
{
22+
if (!index.isValid() || index.row() >= m_recipients.size())
23+
return {};
24+
25+
const auto& r = m_recipients[index.row()];
26+
switch (role) {
27+
case AddressRole: return r->address();
28+
case LabelRole: return r->label();
29+
case AmountRole: return r->amount();
30+
case MessageRole: return r->message();
31+
default: return {};
32+
}
33+
return {};
34+
}
35+
36+
QHash<int, QByteArray> SendRecipientsListModel::roleNames() const
37+
{
38+
return {
39+
{AddressRole, "address"},
40+
{LabelRole, "label"},
41+
{AmountRole, "amount"},
42+
{MessageRole, "message"},
43+
};
44+
}
45+
46+
void SendRecipientsListModel::add()
47+
{
48+
const int row = m_recipients.size();
49+
beginInsertRows(QModelIndex(), row, row);
50+
m_recipients.append(new SendRecipient(this));
51+
endInsertRows();
52+
Q_EMIT countChanged();
53+
setCurrentIndex(row);
54+
}
55+
56+
void SendRecipientsListModel::setCurrentIndex(int row)
57+
{
58+
if (row < 0 || row >= m_recipients.size())
59+
return;
60+
61+
if (row == m_current)
62+
return;
63+
64+
m_current = row;
65+
66+
Q_EMIT currentIndexChanged();
67+
Q_EMIT currentRecipientChanged();
68+
}
69+
70+
void SendRecipientsListModel::next()
71+
{
72+
setCurrentIndex(m_current + 1);
73+
}
74+
75+
void SendRecipientsListModel::prev()
76+
{
77+
setCurrentIndex(m_current - 1);
78+
}
79+
80+
void SendRecipientsListModel::remove()
81+
{
82+
if (m_recipients.size() == 1) {
83+
return;
84+
}
85+
beginRemoveRows(QModelIndex(), m_current, m_current);
86+
delete m_recipients.takeAt(m_current);
87+
endRemoveRows();
88+
Q_EMIT countChanged();
89+
90+
setCurrentIndex(m_current - 1);
91+
}
92+
93+
SendRecipient* SendRecipientsListModel::currentRecipient() const
94+
{
95+
if (m_current < 0 || m_current >= m_recipients.size())
96+
return nullptr;
97+
98+
return m_recipients[m_current];
99+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QML_MODELS_SENDRECIPIENTSLISTMODEL_H
6+
#define BITCOIN_QML_MODELS_SENDRECIPIENTSLISTMODEL_H
7+
8+
#include <qml/models/sendrecipient.h>
9+
10+
#include <QAbstractListModel>
11+
#include <QList>
12+
13+
class SendRecipientsListModel : public QAbstractListModel
14+
{
15+
Q_OBJECT
16+
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
17+
Q_PROPERTY(int count READ count NOTIFY countChanged)
18+
Q_PROPERTY(SendRecipient* current READ currentRecipient NOTIFY currentRecipientChanged)
19+
20+
public:
21+
enum Roles {
22+
AddressRole = Qt::UserRole + 1,
23+
LabelRole,
24+
AmountRole,
25+
MessageRole
26+
};
27+
28+
explicit SendRecipientsListModel(QObject* parent = nullptr);
29+
30+
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
31+
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
32+
QHash<int, QByteArray> roleNames() const override;
33+
34+
Q_INVOKABLE void add();
35+
Q_INVOKABLE void next();
36+
Q_INVOKABLE void prev();
37+
Q_INVOKABLE void remove();
38+
39+
int currentIndex() const { return m_current + 1; }
40+
void setCurrentIndex(int row);
41+
SendRecipient* currentRecipient() const;
42+
int count() const { return m_recipients.size(); }
43+
QList<SendRecipient*> recipients() const { return m_recipients; }
44+
45+
Q_SIGNALS:
46+
void currentIndexChanged();
47+
void currentRecipientChanged();
48+
void countChanged();
49+
50+
private:
51+
QList<SendRecipient*> m_recipients;
52+
int m_current{0};
53+
};
54+
55+
#endif // BITCOIN_QML_MODELS_SENDRECIPIENTSLISTMODEL_H

src/qml/models/walletqmlmodel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ WalletQmlModel::WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObje
2525
m_activity_list_model = new ActivityListModel(this);
2626
m_coins_list_model = new CoinsListModel(this);
2727
m_current_recipient = new SendRecipient(this);
28+
m_send_recipients = new SendRecipientsListModel(this);
2829
}
2930

3031
WalletQmlModel::WalletQmlModel(QObject* parent)
@@ -33,13 +34,15 @@ WalletQmlModel::WalletQmlModel(QObject* parent)
3334
m_activity_list_model = new ActivityListModel(this);
3435
m_coins_list_model = new CoinsListModel(this);
3536
m_current_recipient = new SendRecipient(this);
37+
m_send_recipients = new SendRecipientsListModel(this);
3638
}
3739

3840
WalletQmlModel::~WalletQmlModel()
3941
{
4042
delete m_activity_list_model;
4143
delete m_coins_list_model;
4244
delete m_current_recipient;
45+
delete m_send_recipients;
4346
if (m_current_transaction) {
4447
delete m_current_transaction;
4548
}

src/qml/models/walletqmlmodel.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <qml/models/activitylistmodel.h>
1111
#include <qml/models/coinslistmodel.h>
1212
#include <qml/models/sendrecipient.h>
13+
#include <qml/models/sendrecipientslistmodel.h>
1314
#include <qml/models/walletqmlmodeltransaction.h>
1415
#include <wallet/coincontrol.h>
1516

@@ -27,6 +28,7 @@ class WalletQmlModel : public QObject
2728
Q_PROPERTY(ActivityListModel* activityListModel READ activityListModel CONSTANT)
2829
Q_PROPERTY(CoinsListModel* coinsListModel READ coinsListModel CONSTANT)
2930
Q_PROPERTY(SendRecipient* sendRecipient READ sendRecipient CONSTANT)
31+
Q_PROPERTY(SendRecipientsListModel* recipients READ sendRecipientList CONSTANT)
3032
Q_PROPERTY(WalletQmlModelTransaction* currentTransaction READ currentTransaction NOTIFY currentTransactionChanged)
3133

3234
public:
@@ -38,6 +40,11 @@ class WalletQmlModel : public QObject
3840
QString balance() const;
3941
ActivityListModel* activityListModel() const { return m_activity_list_model; }
4042
CoinsListModel* coinsListModel() const { return m_coins_list_model; }
43+
SendRecipient* sendRecipient() const { return m_send_recipients->currentRecipient(); }
44+
SendRecipientsListModel* sendRecipientList() const { return m_send_recipients; }
45+
WalletQmlModelTransaction* currentTransaction() const { return m_current_transaction; }
46+
Q_INVOKABLE bool prepareTransaction();
47+
Q_INVOKABLE void sendTransaction();
4148

4249
std::set<interfaces::WalletTx> getWalletTxs() const;
4350
interfaces::WalletTx getWalletTx(const uint256& hash) const;
@@ -46,11 +53,6 @@ class WalletQmlModel : public QObject
4653
int& num_blocks,
4754
int64_t& block_time) const;
4855

49-
SendRecipient* sendRecipient() const { return m_current_recipient; }
50-
WalletQmlModelTransaction* currentTransaction() const { return m_current_transaction; }
51-
Q_INVOKABLE bool prepareTransaction();
52-
Q_INVOKABLE void sendTransaction();
53-
5456
using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
5557
virtual std::unique_ptr<interfaces::Handler> handleTransactionChanged(TransactionChangedFn fn);
5658

@@ -73,6 +75,7 @@ class WalletQmlModel : public QObject
7375
std::unique_ptr<interfaces::Wallet> m_wallet;
7476
ActivityListModel* m_activity_list_model{nullptr};
7577
CoinsListModel* m_coins_list_model{nullptr};
78+
SendRecipientsListModel* m_send_recipients{nullptr};
7679
SendRecipient* m_current_recipient{nullptr};
7780
WalletQmlModelTransaction* m_current_transaction{nullptr};
7881
wallet::CCoinControl m_coin_control;

0 commit comments

Comments
 (0)