Skip to content

Commit 0f7bdd9

Browse files
committed
qml: Add remove button to multiple recipients
1 parent c3cae16 commit 0f7bdd9

File tree

5 files changed

+99
-27
lines changed

5 files changed

+99
-27
lines changed

src/qml/models/sendrecipientslistmodel.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
#include <qml/models/sendrecipient.h>
66
#include <qml/models/sendrecipientslistmodel.h>
7+
#include <qobjectdefs.h>
78

89
SendRecipientsListModel::SendRecipientsListModel(QObject* parent)
910
: QAbstractListModel(parent)
1011
{
12+
m_recipients.append(new SendRecipient(this));
1113
}
1214

1315
int SendRecipientsListModel::rowCount(const QModelIndex&) const
@@ -48,5 +50,50 @@ void SendRecipientsListModel::add()
4850
m_recipients.append(new SendRecipient(this));
4951
endInsertRows();
5052
Q_EMIT countChanged();
51-
setCurrentIndex(row)
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];
5299
}

src/qml/models/sendrecipientslistmodel.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
#include <QAbstractListModel>
1111
#include <QList>
12+
#include <qobjectdefs.h>
1213

1314
class SendRecipientsListModel : public QAbstractListModel
1415
{
1516
Q_OBJECT
16-
Q_PROPERTY(int recipientIndex READ recipientIndex NOTIFY recipientIndexChanged)
17+
Q_PROPERTY(int currentIndex READ currentIndex NOTIFY currentIndexChanged)
1718
Q_PROPERTY(int count READ count NOTIFY countChanged)
18-
Q_PROPERTY(SendRecipient* currentRecipient READ currentRecipient NOTIFY currentRecipientChanged)
19+
Q_PROPERTY(SendRecipient* current READ currentRecipient NOTIFY currentRecipientChanged)
1920

2021
public:
2122
enum Roles {
@@ -34,9 +35,9 @@ class SendRecipientsListModel : public QAbstractListModel
3435
Q_INVOKABLE void add();
3536
Q_INVOKABLE void next();
3637
Q_INVOKABLE void prev();
37-
Q_INVOKABLE void clearAll();
38+
Q_INVOKABLE void remove();
3839

39-
int currentIndex() const { return m_current; }
40+
int currentIndex() const { return m_current+1; }
4041
void setCurrentIndex(int row);
4142
SendRecipient* currentRecipient() const;
4243
int count() const { return m_recipients.size(); }

src/qml/models/walletqmlmodel.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
12
// Copyright (c) 2024 The Bitcoin Core developers
23
// Distributed under the MIT software license, see the accompanying
34
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
45

56
#include <qml/models/walletqmlmodel.h>
67

8+
#include <qml/models/activitylistmodel.h>
9+
#include <qml/models/paymentrequest.h>
10+
#include <qml/models/sendrecipient.h>
11+
#include <qml/models/sendrecipientslistmodel.h>
12+
#include <qml/models/walletqmlmodeltransaction.h>
13+
714
#include <consensus/amount.h>
815
#include <interfaces/wallet.h>
916
#include <key_io.h>
@@ -24,6 +31,7 @@ WalletQmlModel::WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObje
2431
m_activity_list_model = new ActivityListModel(this);
2532
m_coins_list_model = new CoinsListModel(this);
2633
m_current_recipient = new SendRecipient(this);
34+
m_send_recipients = new SendRecipientsListModel(this);
2735
}
2836

2937
WalletQmlModel::WalletQmlModel(QObject* parent)
@@ -32,6 +40,7 @@ WalletQmlModel::WalletQmlModel(QObject* parent)
3240
m_activity_list_model = new ActivityListModel(this);
3341
m_coins_list_model = new CoinsListModel(this);
3442
m_current_recipient = new SendRecipient(this);
43+
m_send_recipients = new SendRecipientsListModel(this);
3544
}
3645

3746
WalletQmlModel::~WalletQmlModel()
@@ -204,13 +213,3 @@ std::vector<COutPoint> WalletQmlModel::listSelectedCoins() const
204213
{
205214
return m_coin_control.ListSelected();
206215
}
207-
208-
int WalletQmlModel::recipientIndex() const
209-
{
210-
return 1;
211-
}
212-
213-
int WalletQmlModel::recipientsCount() const
214-
{
215-
return 1;
216-
}

src/qml/models/walletqmlmodel.h

Lines changed: 5 additions & 10 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,10 +28,8 @@ 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)
30-
Q_PROPERTY(SendRecipientsListModel* sendRecipientList READ sendRecipientList CONSTANT)
31+
Q_PROPERTY(SendRecipientsListModel* recipients READ sendRecipientList CONSTANT)
3132
Q_PROPERTY(WalletQmlModelTransaction* currentTransaction READ currentTransaction NOTIFY currentTransactionChanged)
32-
Q_PROPERTY(int recipientIndex READ recipientIndex NOTIFY recipientIndexChanged)
33-
Q_PROPERTY(int recipientsCount READ recipientsCount NOTIFY recipientsCountChanged)
3433

3534
public:
3635
WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObject* parent = nullptr);
@@ -41,8 +40,8 @@ class WalletQmlModel : public QObject
4140
QString balance() const;
4241
ActivityListModel* activityListModel() const { return m_activity_list_model; }
4342
CoinsListModel* coinsListModel() const { return m_coins_list_model; }
44-
SendRecipient* sendRecipient() const { return m_current_recipient; }
45-
SendRecipientList* sendRecipientList() const { return m_current_recipient; }
43+
SendRecipient* sendRecipient() const { return m_send_recipients->currentRecipient(); }
44+
SendRecipientsListModel* sendRecipientList() const { return m_send_recipients; }
4645
WalletQmlModelTransaction* currentTransaction() const { return m_current_transaction; }
4746
Q_INVOKABLE bool prepareTransaction();
4847
Q_INVOKABLE void sendTransaction();
@@ -72,20 +71,16 @@ class WalletQmlModel : public QObject
7271
bool isSelectedCoin(const COutPoint& output);
7372
std::vector<COutPoint> listSelectedCoins() const;
7473

75-
int recipientIndex() const;
76-
int recipientsCount() const;
77-
7874
Q_SIGNALS:
7975
void nameChanged();
8076
void balanceChanged();
8177
void currentTransactionChanged();
82-
void recipientIndexChanged();
83-
void recipientsCountChanged();
8478

8579
private:
8680
std::unique_ptr<interfaces::Wallet> m_wallet;
8781
ActivityListModel* m_activity_list_model{nullptr};
8882
CoinsListModel* m_coins_list_model{nullptr};
83+
SendRecipientsListModel* m_send_recipients{nullptr};
8984
SendRecipient* m_current_recipient{nullptr};
9085
WalletQmlModelTransaction* m_current_transaction{nullptr};
9186
wallet::CCoinControl m_coin_control;

src/qml/pages/wallet/Send.qml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PageStack {
1616
vertical: true
1717

1818
property WalletQmlModel wallet: walletController.selectedWallet
19-
property SendRecipient recipient: wallet.sendRecipient
19+
property SendRecipient recipient: wallet.recipients.current
2020

2121
signal transactionPrepared()
2222

@@ -94,6 +94,9 @@ PageStack {
9494
iconWidth: 30
9595
iconHeight: 30
9696
iconSource: "image://images/caret-left"
97+
onClicked: {
98+
wallet.recipients.prev()
99+
}
97100
}
98101

99102
NavButton {
@@ -102,14 +105,41 @@ PageStack {
102105
iconWidth: 30
103106
iconHeight: 30
104107
iconSource: "image://images/caret-right"
108+
onClicked: {
109+
wallet.recipients.next()
110+
}
105111
}
106112

107113
CoreText {
114+
Layout.fillWidth: true
115+
Layout.alignment: Qt.AlignLeft
108116
id: selectAndAddRecipientsLabel
109-
text: qsTr("Recipient %1 of %2").arg(wallet.recipientIndex).arg(wallet.recipientsCount)
117+
text: qsTr("Recipient %1 of %2").arg(wallet.recipients.currentIndex).arg(wallet.recipients.count)
110118
font.pixelSize: 18
111119
color: Theme.color.neutral9
112120
}
121+
122+
NavButton {
123+
Layout.preferredWidth: 30
124+
Layout.preferredHeight: 30
125+
iconWidth: 20
126+
iconHeight: 20
127+
iconSource: "image://images/plus"
128+
onClicked: {
129+
wallet.recipients.add()
130+
}
131+
}
132+
NavButton {
133+
Layout.preferredWidth: 30
134+
Layout.preferredHeight: 30
135+
iconWidth: 20
136+
iconHeight: 20
137+
iconSource: "image://images/minus"
138+
visible: wallet.recipients.count > 1
139+
onClicked: {
140+
wallet.recipients.remove()
141+
}
142+
}
113143
}
114144

115145
Separator {

0 commit comments

Comments
 (0)