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