Bitcoin Core  24.99.0
P2P Digital Currency
sendcoinsentry.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/sendcoinsentry.h>
10 #include <qt/forms/ui_sendcoinsentry.h>
11 
12 #include <qt/addressbookpage.h>
13 #include <qt/addresstablemodel.h>
14 #include <qt/guiutil.h>
15 #include <qt/optionsmodel.h>
16 #include <qt/platformstyle.h>
17 #include <qt/walletmodel.h>
18 
19 #include <QApplication>
20 #include <QClipboard>
21 
22 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
23  QWidget(parent),
24  ui(new Ui::SendCoinsEntry),
25  platformStyle(_platformStyle)
26 {
27  ui->setupUi(this);
28 
29  ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
30  ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
31  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
32 
34  ui->payToLayout->setSpacing(4);
35 
36  GUIUtil::setupAddressWidget(ui->payTo, this);
37 
38  // Connect signals
40  connect(ui->checkboxSubtractFeeFromAmount, &QCheckBox::toggled, this, &SendCoinsEntry::subtractFeeFromAmountChanged);
41  connect(ui->deleteButton, &QPushButton::clicked, this, &SendCoinsEntry::deleteClicked);
42  connect(ui->useAvailableBalanceButton, &QPushButton::clicked, this, &SendCoinsEntry::useAvailableBalanceClicked);
43 }
44 
46 {
47  delete ui;
48 }
49 
51 {
52  // Paste text from clipboard into recipient field
53  ui->payTo->setText(QApplication::clipboard()->text());
54 }
55 
57 {
58  if(!model)
59  return;
62  if(dlg.exec())
63  {
64  ui->payTo->setText(dlg.getReturnValue());
65  ui->payAmount->setFocus();
66  }
67 }
68 
69 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
70 {
71  updateLabel(address);
72 }
73 
75 {
76  this->model = _model;
77 
78  if (_model && _model->getOptionsModel())
80 
81  clear();
82 }
83 
85 {
86  // clear UI elements for normal payment
87  ui->payTo->clear();
88  ui->addAsLabel->clear();
89  ui->payAmount->clear();
90  if (model && model->getOptionsModel()) {
91  ui->checkboxSubtractFeeFromAmount->setChecked(model->getOptionsModel()->getSubFeeFromAmount());
92  }
93  ui->messageTextLabel->clear();
94  ui->messageTextLabel->hide();
95  ui->messageLabel->hide();
96 
97  // update the display unit, to not use the default ("BTC")
99 }
100 
102 {
103  ui->checkboxSubtractFeeFromAmount->setChecked(true);
104 }
105 
107 {
108  Q_EMIT removeEntry(this);
109 }
110 
112 {
113  Q_EMIT useAvailableBalance(this);
114 }
115 
117 {
118  if (!model)
119  return false;
120 
121  // Check input validity
122  bool retval = true;
123 
124  if (!model->validateAddress(ui->payTo->text()))
125  {
126  ui->payTo->setValid(false);
127  retval = false;
128  }
129 
130  if (!ui->payAmount->validate())
131  {
132  retval = false;
133  }
134 
135  // Sending a zero amount is invalid
136  if (ui->payAmount->value(nullptr) <= 0)
137  {
138  ui->payAmount->setValid(false);
139  retval = false;
140  }
141 
142  // Reject dust outputs:
143  if (retval && GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
144  ui->payAmount->setValid(false);
145  retval = false;
146  }
147 
148  return retval;
149 }
150 
152 {
153  recipient.address = ui->payTo->text();
154  recipient.label = ui->addAsLabel->text();
155  recipient.amount = ui->payAmount->value();
156  recipient.message = ui->messageTextLabel->text();
157  recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
158 
159  return recipient;
160 }
161 
162 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
163 {
164  QWidget::setTabOrder(prev, ui->payTo);
165  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
166  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
167  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
168  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
169  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
170  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
171  return ui->deleteButton;
172 }
173 
175 {
176  recipient = value;
177  {
178  // message
179  ui->messageTextLabel->setText(recipient.message);
180  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
181  ui->messageLabel->setVisible(!recipient.message.isEmpty());
182 
183  ui->addAsLabel->clear();
184  ui->payTo->setText(recipient.address); // this may set a label from addressbook
185  if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
186  ui->addAsLabel->setText(recipient.label);
187  ui->payAmount->setValue(recipient.amount);
188  }
189 }
190 
191 void SendCoinsEntry::setAddress(const QString &address)
192 {
193  ui->payTo->setText(address);
194  ui->payAmount->setFocus();
195 }
196 
198 {
199  ui->payAmount->setValue(amount);
200 }
201 
203 {
204  return ui->payTo->text().isEmpty();
205 }
206 
208 {
209  ui->payTo->setFocus();
210 }
211 
213 {
214  if (model && model->getOptionsModel()) {
215  ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
216  }
217 }
218 
220 {
221  if (e->type() == QEvent::PaletteChange) {
222  ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/address-book")));
223  ui->pasteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/editpaste")));
224  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
225  }
226 
227  QWidget::changeEvent(e);
228 }
229 
230 bool SendCoinsEntry::updateLabel(const QString &address)
231 {
232  if(!model)
233  return false;
234 
235  // Fill in label from address book, if address has an associated label
236  QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
237  if(!associatedLabel.isEmpty())
238  {
239  ui->addAsLabel->setText(associatedLabel);
240  return true;
241  }
242 
243  return false;
244 }
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Widget that shows a list of sending or receiving addresses.
@ ForSelection
Open address book to pick address.
void setModel(AddressTableModel *model)
const QString & getReturnValue() const
QString labelForAddress(const QString &address) const
Look up label for address in address book, if not found return empty string.
bool getSubFeeFromAmount() const
Definition: optionsmodel.h:98
void displayUnitChanged(BitcoinUnit unit)
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:94
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
bool getUseExtraSpacing() const
Definition: platformstyle.h:22
A single entry in the dialog for sending bitcoins.
WalletModel * model
void setFocus()
bool updateLabel(const QString &address)
void setAddress(const QString &address)
bool isClear()
Return whether the entry is still empty and unedited.
void subtractFeeFromAmountChanged()
void useAvailableBalance(SendCoinsEntry *entry)
~SendCoinsEntry()
SendCoinsRecipient recipient
void setValue(const SendCoinsRecipient &value)
void changeEvent(QEvent *e) override
void updateDisplayUnit()
void on_payTo_textChanged(const QString &address)
void on_pasteButton_clicked()
SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent=nullptr)
void setModel(WalletModel *model)
void useAvailableBalanceClicked()
void removeEntry(SendCoinsEntry *entry)
void payAmountChanged()
void setAmount(const CAmount &amount)
QWidget * setupTabChain(QWidget *prev)
Set up the tab chain manually, as Qt messes up the tab chain by default in some cases (issue https://...
const PlatformStyle * platformStyle
void deleteClicked()
void clear()
void on_addressBookButton_clicked()
bool validate(interfaces::Node &node)
Ui::SendCoinsEntry * ui
void checkSubtractFeeFromAmount()
SendCoinsRecipient getValue()
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:53
AddressTableModel * getAddressTableModel() const
bool validateAddress(const QString &address) const
OptionsModel * getOptionsModel() const
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:70
bool isDust(interfaces::Node &node, const QString &address, const CAmount &amount)
Definition: guiutil.cpp:233
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:123
Definition: init.h:25