BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
sendcoinsentry.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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 
18 #include <QApplication>
19 #include <QClipboard>
20 
21 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
22  QStackedWidget(parent),
23  ui(new Ui::SendCoinsEntry),
24  model(0),
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  ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
33  ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
34 
35  setCurrentWidget(ui->SendCoins);
36 
38  ui->payToLayout->setSpacing(4);
39  ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
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  ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
101  ui->messageTextLabel->clear();
102  ui->messageTextLabel->hide();
103  ui->messageLabel->hide();
104  // clear UI elements for unauthenticated payment request
105  ui->payTo_is->clear();
106  ui->memoTextLabel_is->clear();
107  ui->payAmount_is->clear();
108  // clear UI elements for authenticated payment request
109  ui->payTo_s->clear();
110  ui->memoTextLabel_s->clear();
111  ui->payAmount_s->clear();
112 
113  // update the display unit, to not use the default ("BTC")
115 }
116 
118 {
119  ui->checkboxSubtractFeeFromAmount->setChecked(true);
120 }
121 
123 {
124  Q_EMIT removeEntry(this);
125 }
126 
128 {
129  Q_EMIT useAvailableBalance(this);
130 }
131 
133 {
134  if (!model)
135  return false;
136 
137  // Check input validity
138  bool retval = true;
139 
140 #ifdef ENABLE_BIP70
141  // Skip checks for payment request
142  if (recipient.paymentRequest.IsInitialized())
143  return retval;
144 #endif
145 
146  if (!model->validateAddress(ui->payTo->text()))
147  {
148  ui->payTo->setValid(false);
149  retval = false;
150  }
151 
152  if (!ui->payAmount->validate())
153  {
154  retval = false;
155  }
156 
157  // Sending a zero amount is invalid
158  if (ui->payAmount->value(0) <= 0)
159  {
160  ui->payAmount->setValid(false);
161  retval = false;
162  }
163 
164  // Reject dust outputs:
165  if (retval && GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
166  ui->payAmount->setValid(false);
167  retval = false;
168  }
169 
170  return retval;
171 }
172 
174 {
175 #ifdef ENABLE_BIP70
176  // Payment request
177  if (recipient.paymentRequest.IsInitialized())
178  return recipient;
179 #endif
180 
181  // Normal payment
182  recipient.address = ui->payTo->text();
183  recipient.label = ui->addAsLabel->text();
184  recipient.amount = ui->payAmount->value();
185  recipient.message = ui->messageTextLabel->text();
186  recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
187 
188  return recipient;
189 }
190 
191 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
192 {
193  QWidget::setTabOrder(prev, ui->payTo);
194  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
195  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
196  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
197  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
198  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
199  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
200  return ui->deleteButton;
201 }
202 
204 {
205  recipient = value;
206 
207 #ifdef ENABLE_BIP70
208  if (recipient.paymentRequest.IsInitialized()) // payment request
209  {
210  if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
211  {
212  ui->payTo_is->setText(recipient.address);
213  ui->memoTextLabel_is->setText(recipient.message);
214  ui->payAmount_is->setValue(recipient.amount);
215  ui->payAmount_is->setReadOnly(true);
216  setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
217  }
218  else // authenticated
219  {
220  ui->payTo_s->setText(recipient.authenticatedMerchant);
221  ui->memoTextLabel_s->setText(recipient.message);
222  ui->payAmount_s->setValue(recipient.amount);
223  ui->payAmount_s->setReadOnly(true);
224  setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
225  }
226  }
227  else // normal payment
228 #endif
229  {
230  // message
231  ui->messageTextLabel->setText(recipient.message);
232  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
233  ui->messageLabel->setVisible(!recipient.message.isEmpty());
234 
235  ui->addAsLabel->clear();
236  ui->payTo->setText(recipient.address); // this may set a label from addressbook
237  if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
238  ui->addAsLabel->setText(recipient.label);
239  ui->payAmount->setValue(recipient.amount);
240  }
241 }
242 
243 void SendCoinsEntry::setAddress(const QString &address)
244 {
245  ui->payTo->setText(address);
246  ui->payAmount->setFocus();
247 }
248 
250 {
251  ui->payAmount->setValue(amount);
252 }
253 
255 {
256  return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
257 }
258 
260 {
261  ui->payTo->setFocus();
262 }
263 
265 {
266  if(model && model->getOptionsModel())
267  {
268  // Update payAmount with the current unit
269  ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
270  ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
271  ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
272  }
273 }
274 
275 bool SendCoinsEntry::updateLabel(const QString &address)
276 {
277  if(!model)
278  return false;
279 
280  // Fill in label from address book, if address has an associated label
281  QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
282  if(!associatedLabel.isEmpty())
283  {
284  ui->addAsLabel->setText(associatedLabel);
285  return true;
286  }
287 
288  return false;
289 }
Ui::SendCoinsEntry * ui
void setValue(const SendCoinsRecipient &value)
void payAmountChanged()
QFont fixedPitchFont()
Definition: guiutil.cpp:75
void setFocus()
SendCoinsRecipient getValue()
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:208
void setAddress(const QString &address)
~SendCoinsEntry()
bool updateLabel(const QString &address)
void deleteClicked()
void on_payTo_textChanged(const QString &address)
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Open address book to pick address.
AddressTableModel * getAddressTableModel()
void updateDisplayUnit()
A single entry in the dialog for sending bitcoins.
int getDisplayUnit() const
Definition: optionsmodel.h:74
QWidget * setupTabChain(QWidget *prev)
Set up the tab chain manually, as Qt messes up the tab chain by default in some cases (issue https://...
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:104
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 displayUnitChanged(int unit)
void checkSubtractFeeFromAmount()
bool isClear()
Return whether the entry is still empty and unedited.
bool validateAddress(const QString &address)
void subtractFeeFromAmountChanged()
void on_pasteButton_clicked()
WalletModel * model
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:125
SendCoinsRecipient recipient
void setAmount(const CAmount &amount)
void on_addressBookButton_clicked()
void setModel(WalletModel *model)
void useAvailableBalance(SendCoinsEntry *entry)
SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent=0)
bool fSubtractFeeFromAmount
Definition: walletmodel.h:83
const PlatformStyle * platformStyle
void useAvailableBalanceClicked()
QString authenticatedMerchant
Definition: walletmodel.h:81
Top-level interface for a bitcoin node (bsha3d process).
Definition: node.h:35
OptionsModel * getOptionsModel()
const QString & getReturnValue() const