Bitcoin Core  27.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 #include <qt/sendcoinsentry.h>
6 #include <qt/forms/ui_sendcoinsentry.h>
7 
8 #include <qt/addressbookpage.h>
9 #include <qt/addresstablemodel.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/platformstyle.h>
13 #include <qt/walletmodel.h>
14 
15 #include <QApplication>
16 #include <QClipboard>
17 
18 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
19  QWidget(parent),
20  ui(new Ui::SendCoinsEntry),
21  platformStyle(_platformStyle)
22 {
23  ui->setupUi(this);
24 
25  ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
26  ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
27  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
28 
30  ui->payToLayout->setSpacing(4);
31 
32  GUIUtil::setupAddressWidget(ui->payTo, this);
33 
34  // Connect signals
36  connect(ui->checkboxSubtractFeeFromAmount, &QCheckBox::toggled, this, &SendCoinsEntry::subtractFeeFromAmountChanged);
37  connect(ui->deleteButton, &QPushButton::clicked, this, &SendCoinsEntry::deleteClicked);
38  connect(ui->useAvailableBalanceButton, &QPushButton::clicked, this, &SendCoinsEntry::useAvailableBalanceClicked);
39 }
40 
42 {
43  delete ui;
44 }
45 
47 {
48  // Paste text from clipboard into recipient field
49  ui->payTo->setText(QApplication::clipboard()->text());
50 }
51 
53 {
54  if(!model)
55  return;
58  if(dlg.exec())
59  {
60  ui->payTo->setText(dlg.getReturnValue());
61  ui->payAmount->setFocus();
62  }
63 }
64 
65 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
66 {
67  updateLabel(address);
68 }
69 
71 {
72  this->model = _model;
73 
74  if (_model && _model->getOptionsModel())
76 
77  clear();
78 }
79 
81 {
82  // clear UI elements for normal payment
83  ui->payTo->clear();
84  ui->addAsLabel->clear();
85  ui->payAmount->clear();
86  if (model && model->getOptionsModel()) {
87  ui->checkboxSubtractFeeFromAmount->setChecked(model->getOptionsModel()->getSubFeeFromAmount());
88  }
89  ui->messageTextLabel->clear();
90  ui->messageTextLabel->hide();
91  ui->messageLabel->hide();
92 
93  // update the display unit, to not use the default ("BTC")
95 }
96 
98 {
99  ui->checkboxSubtractFeeFromAmount->setChecked(true);
100 }
101 
103 {
104  Q_EMIT removeEntry(this);
105 }
106 
108 {
109  Q_EMIT useAvailableBalance(this);
110 }
111 
113 {
114  if (!model)
115  return false;
116 
117  // Check input validity
118  bool retval = true;
119 
120  if (!model->validateAddress(ui->payTo->text()))
121  {
122  ui->payTo->setValid(false);
123  retval = false;
124  }
125 
126  if (!ui->payAmount->validate())
127  {
128  retval = false;
129  }
130 
131  // Sending a zero amount is invalid
132  if (ui->payAmount->value(nullptr) <= 0)
133  {
134  ui->payAmount->setValid(false);
135  retval = false;
136  }
137 
138  // Reject dust outputs:
139  if (retval && GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
140  ui->payAmount->setValid(false);
141  retval = false;
142  }
143 
144  return retval;
145 }
146 
148 {
149  recipient.address = ui->payTo->text();
150  recipient.label = ui->addAsLabel->text();
151  recipient.amount = ui->payAmount->value();
152  recipient.message = ui->messageTextLabel->text();
153  recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
154 
155  return recipient;
156 }
157 
158 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
159 {
160  QWidget::setTabOrder(prev, ui->payTo);
161  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
162  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
163  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
164  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
165  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
166  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
167  return ui->deleteButton;
168 }
169 
171 {
172  recipient = value;
173  {
174  // message
175  ui->messageTextLabel->setText(recipient.message);
176  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
177  ui->messageLabel->setVisible(!recipient.message.isEmpty());
178 
179  ui->addAsLabel->clear();
180  ui->payTo->setText(recipient.address); // this may set a label from addressbook
181  if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
182  ui->addAsLabel->setText(recipient.label);
183  ui->payAmount->setValue(recipient.amount);
184  }
185 }
186 
187 void SendCoinsEntry::setAddress(const QString &address)
188 {
189  ui->payTo->setText(address);
190  ui->payAmount->setFocus();
191 }
192 
194 {
195  ui->payAmount->setValue(amount);
196 }
197 
199 {
200  return ui->payTo->text().isEmpty();
201 }
202 
204 {
205  ui->payTo->setFocus();
206 }
207 
209 {
210  if (model && model->getOptionsModel()) {
211  ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
212  }
213 }
214 
216 {
217  if (e->type() == QEvent::PaletteChange) {
218  ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/address-book")));
219  ui->pasteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/editpaste")));
220  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
221  }
222 
223  QWidget::changeEvent(e);
224 }
225 
226 bool SendCoinsEntry::updateLabel(const QString &address)
227 {
228  if(!model)
229  return false;
230 
231  // Fill in label from address book, if address has an associated label
232  QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
233  if(!associatedLabel.isEmpty())
234  {
235  ui->addAsLabel->setText(associatedLabel);
236  return true;
237  }
238 
239  return false;
240 }
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:108
void displayUnitChanged(BitcoinUnit unit)
BitcoinUnit getDisplayUnit() const
Definition: optionsmodel.h:104
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:48
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:240
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:130
Definition: init.h:25