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