Bitcoin ABC  0.24.7
P2P Digital Currency
sendcoinsentry.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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)
6 #include <config/bitcoin-config.h>
7 #endif
8 
9 #include <qt/forms/ui_sendcoinsentry.h>
10 #include <qt/sendcoinsentry.h>
11 
12 #include <config.h>
13 #include <qt/addressbookpage.h>
14 #include <qt/addresstablemodel.h>
15 #include <qt/guiutil.h>
16 #include <qt/optionsmodel.h>
17 #include <qt/platformstyle.h>
18 #include <qt/walletmodel.h>
19 
20 #include <QApplication>
21 #include <QClipboard>
22 
24  WalletModel *_model, QWidget *parent)
25  : QStackedWidget(parent), ui(new Ui::SendCoinsEntry), model(_model),
26  platformStyle(_platformStyle) {
27  ui->setupUi(this);
28 
29  ui->addressBookButton->setIcon(
30  platformStyle->SingleColorIcon(":/icons/address-book"));
31  ui->pasteButton->setIcon(
32  platformStyle->SingleColorIcon(":/icons/editpaste"));
33  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
34  ui->deleteButton_is->setIcon(
35  platformStyle->SingleColorIcon(":/icons/remove"));
36  ui->deleteButton_s->setIcon(
37  platformStyle->SingleColorIcon(":/icons/remove"));
38 
39  setCurrentWidget(ui->SendCoins);
40 
42  ui->payToLayout->setSpacing(4);
43  }
44 
45  // normal bitcoin address field
46  GUIUtil::setupAddressWidget(ui->payTo, this);
47  // just a label for displaying bitcoin address(es)
48  ui->payTo_is->setFont(GUIUtil::fixedPitchFont());
49 
50  // Connect signals
51  connect(ui->payAmount, &BitcoinAmountField::valueChanged, this,
53  connect(ui->checkboxSubtractFeeFromAmount, &QCheckBox::toggled, this,
55  connect(ui->deleteButton, &QPushButton::clicked, this,
57  connect(ui->deleteButton_is, &QPushButton::clicked, this,
59  connect(ui->deleteButton_s, &QPushButton::clicked, this,
61  connect(ui->useAvailableBalanceButton, &QPushButton::clicked, this,
63 
64  // Set the model properly.
65  setModel(model);
66 }
67 
69  delete ui;
70 }
71 
73  // Paste text from clipboard into recipient field
74  ui->payTo->setText(QApplication::clipboard()->text());
75 }
76 
78  if (!model) {
79  return;
80  }
84  if (dlg.exec()) {
85  ui->payTo->setText(dlg.getReturnValue());
86  ui->payAmount->setFocus();
87  }
88 }
89 
90 void SendCoinsEntry::on_payTo_textChanged(const QString &address) {
91  updateLabel(address);
92 }
93 
95  this->model = _model;
96 
97  if (_model) {
98  ui->messageTextLabel->setToolTip(
99  tr("A message that was attached to the %1 URI which will be stored "
100  "with the transaction for your reference. Note: This message "
101  "will not be sent over the Bitcoin network.")
102  .arg(QString::fromStdString(
103  _model->getChainParams().CashAddrPrefix())));
104  }
105 
106  if (_model && _model->getOptionsModel()) {
109  }
110 
111  clear();
112 }
113 
115  // clear UI elements for normal payment
116  ui->payTo->clear();
117  ui->addAsLabel->clear();
118  ui->payAmount->clear();
119  ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
120  ui->messageTextLabel->clear();
121  ui->messageTextLabel->hide();
122  ui->messageLabel->hide();
123  // clear UI elements for unauthenticated payment request
124  ui->payTo_is->clear();
125  ui->memoTextLabel_is->clear();
126  ui->payAmount_is->clear();
127  // clear UI elements for authenticated payment request
128  ui->payTo_s->clear();
129  ui->memoTextLabel_s->clear();
130  ui->payAmount_s->clear();
131 
132  // update the display unit, to not use the default ("XEC")
134 }
135 
137  ui->checkboxSubtractFeeFromAmount->setChecked(true);
138 }
139 
141  Q_EMIT removeEntry(this);
142 }
143 
145  Q_EMIT useAvailableBalance(this);
146 }
147 
149  if (!model) {
150  return false;
151  }
152 
153  // Check input validity
154  bool retval = true;
155 
156 #ifdef ENABLE_BIP70
157  // Skip checks for payment request
158  if (recipient.paymentRequest.IsInitialized()) {
159  return retval;
160  }
161 #endif
162 
163  if (!model->validateAddress(ui->payTo->text())) {
164  ui->payTo->setValid(false);
165  retval = false;
166  }
167 
168  if (!ui->payAmount->validate()) {
169  retval = false;
170  }
171 
172  // Sending a zero amount is invalid
173  if (ui->payAmount->value(nullptr) <= Amount::zero()) {
174  ui->payAmount->setValid(false);
175  retval = false;
176  }
177 
178  // Reject dust outputs:
179  if (retval &&
180  GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value(),
181  model->getChainParams())) {
182  ui->payAmount->setValid(false);
183  retval = false;
184  }
185 
186  return retval;
187 }
188 
190 #ifdef ENABLE_BIP70
191  // Payment request
192  if (recipient.paymentRequest.IsInitialized()) {
193  return recipient;
194  }
195 #endif
196 
197  // Normal payment
198  recipient.address = ui->payTo->text();
199  recipient.label = ui->addAsLabel->text();
200  recipient.amount = ui->payAmount->value();
201  recipient.message = ui->messageTextLabel->text();
203  (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
204 
205  return recipient;
206 }
207 
208 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev) {
209  QWidget::setTabOrder(prev, ui->payTo);
210  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
211  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
212  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
213  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount,
214  ui->addressBookButton);
215  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
216  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
217  return ui->deleteButton;
218 }
219 
221  recipient = value;
222 
223 #ifdef ENABLE_BIP70
224  // payment request
225  if (recipient.paymentRequest.IsInitialized()) {
226  // unauthenticated
227  if (recipient.authenticatedMerchant.isEmpty()) {
228  ui->payTo_is->setText(recipient.address);
229  ui->memoTextLabel_is->setText(recipient.message);
230  ui->payAmount_is->setValue(recipient.amount);
231  ui->payAmount_is->setReadOnly(true);
232  setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
233  }
234 
235  // authenticated
236  else {
237  ui->payTo_s->setText(recipient.authenticatedMerchant);
238  ui->memoTextLabel_s->setText(recipient.message);
239  ui->payAmount_s->setValue(recipient.amount);
240  ui->payAmount_s->setReadOnly(true);
241  setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
242  }
243  }
244 
245  // normal payment
246  else
247 #endif
248  {
249  // message
250  ui->messageTextLabel->setText(recipient.message);
251  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
252  ui->messageLabel->setVisible(!recipient.message.isEmpty());
253 
254  ui->addAsLabel->clear();
255  // this may set a label from addressbook
256  ui->payTo->setText(recipient.address);
257  // if a label had been set from the addressbook, don't overwrite with an
258  // empty label
259  if (!recipient.label.isEmpty()) {
260  ui->addAsLabel->setText(recipient.label);
261  }
262  ui->payAmount->setValue(recipient.amount);
263  }
264 }
265 
266 void SendCoinsEntry::setAddress(const QString &address) {
267  ui->payTo->setText(address);
268  ui->payAmount->setFocus();
269 }
270 
271 void SendCoinsEntry::setAmount(const Amount amount) {
272  ui->payAmount->setValue(amount);
273 }
274 
276  return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() &&
277  ui->payTo_s->text().isEmpty();
278 }
279 
281  ui->payTo->setFocus();
282 }
283 
285  if (model && model->getOptionsModel()) {
286  // Update payAmount with the current unit
287  ui->payAmount->setDisplayUnit(
289  ui->payAmount_is->setDisplayUnit(
291  ui->payAmount_s->setDisplayUnit(
293  }
294 }
295 
296 bool SendCoinsEntry::updateLabel(const QString &address) {
297  if (!model) {
298  return false;
299  }
300 
301  // Fill in label from address book, if address has an associated label
302  QString associatedLabel =
304  if (!associatedLabel.isEmpty()) {
305  ui->addAsLabel->setText(associatedLabel);
306  return true;
307  }
308 
309  return false;
310 }
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:315
SendCoinsEntry::setAddress
void setAddress(const QString &address)
Definition: sendcoinsentry.cpp:266
AddressBookPage::setModel
void setModel(AddressTableModel *model)
Definition: addressbookpage.cpp:162
AddressBookPage::SendingTab
@ SendingTab
Definition: addressbookpage.h:29
SendCoinsEntry::SendCoinsEntry
SendCoinsEntry(const PlatformStyle *platformStyle, WalletModel *model, QWidget *parent=nullptr)
Definition: sendcoinsentry.cpp:23
SendCoinsEntry::~SendCoinsEntry
~SendCoinsEntry()
Definition: sendcoinsentry.cpp:68
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:47
SendCoinsRecipient::amount
Amount amount
Definition: sendcoinsrecipient.h:41
SendCoinsEntry::payAmountChanged
void payAmountChanged()
SendCoinsEntry::setValue
void setValue(const SendCoinsRecipient &value)
Definition: sendcoinsentry.cpp:220
BitcoinAmountField::valueChanged
void valueChanged()
PlatformStyle::SingleColorIcon
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Definition: platformstyle.cpp:97
addressbookpage.h
walletmodel.h
SendCoinsEntry::updateLabel
bool updateLabel(const QString &address)
Definition: sendcoinsentry.cpp:296
SendCoinsEntry::deleteClicked
void deleteClicked()
Definition: sendcoinsentry.cpp:140
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:319
SendCoinsEntry::setFocus
void setFocus()
Definition: sendcoinsentry.cpp:280
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:386
SendCoinsRecipient::label
QString label
Definition: sendcoinsrecipient.h:40
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:208
SendCoinsEntry::ui
Ui::SendCoinsEntry * ui
Definition: sendcoinsentry.h:75
OptionsModel::displayUnitChanged
void displayUnitChanged(int unit)
SendCoinsRecipient::authenticatedMerchant
QString authenticatedMerchant
Definition: sendcoinsrecipient.h:54
SendCoinsEntry::on_payTo_textChanged
void on_payTo_textChanged(const QString &address)
Definition: sendcoinsentry.cpp:90
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
GUIUtil::fixedPitchFont
QFont fixedPitchFont()
Definition: guiutil.cpp:76
SendCoinsEntry::clear
void clear()
Definition: sendcoinsentry.cpp:114
SendCoinsEntry::updateDisplayUnit
void updateDisplayUnit()
Definition: sendcoinsentry.cpp:284
SendCoinsRecipient
Definition: sendcoinsrecipient.h:23
PlatformStyle::getUseExtraSpacing
bool getUseExtraSpacing() const
Definition: platformstyle.h:21
WalletModel::validateAddress
bool validateAddress(const QString &address)
Definition: walletmodel.cpp:128
sendcoinsentry.h
CChainParams::CashAddrPrefix
const std::string & CashAddrPrefix() const
Definition: chainparams.h:91
GUIUtil::isDust
bool isDust(interfaces::Node &node, const QString &address, const Amount amount, const CChainParams &chainParams)
Definition: guiutil.cpp:232
WalletModel::getChainParams
const CChainParams & getChainParams() const
Definition: walletmodel.cpp:531
SendCoinsEntry::isClear
bool isClear()
Return whether the entry is still empty and unedited.
Definition: sendcoinsentry.cpp:275
AddressBookPage
Widget that shows a list of sending or receiving addresses.
Definition: addressbookpage.h:25
guiutil.h
SendCoinsEntry::validate
bool validate(interfaces::Node &node)
Definition: sendcoinsentry.cpp:148
SendCoinsEntry::model
WalletModel * model
Definition: sendcoinsentry.h:76
Amount
Definition: amount.h:19
SendCoinsEntry::setAmount
void setAmount(const Amount amount)
Definition: sendcoinsentry.cpp:271
interfaces::Node
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:55
SendCoinsEntry::setModel
void setModel(WalletModel *model)
Definition: sendcoinsentry.cpp:94
platformstyle.h
SendCoinsEntry::useAvailableBalance
void useAvailableBalance(SendCoinsEntry *entry)
Ui
Definition: addressbookpage.h:14
SendCoinsEntry::recipient
SendCoinsRecipient recipient
Definition: sendcoinsentry.h:74
OptionsModel::getDisplayUnit
int getDisplayUnit() const
Definition: optionsmodel.h:97
SendCoinsRecipient::address
QString address
Definition: sendcoinsrecipient.h:39
SendCoinsEntry::useAvailableBalanceClicked
void useAvailableBalanceClicked()
Definition: sendcoinsentry.cpp:144
SendCoinsEntry::removeEntry
void removeEntry(SendCoinsEntry *entry)
SendCoinsEntry::checkSubtractFeeFromAmount
void checkSubtractFeeFromAmount()
Definition: sendcoinsentry.cpp:136
SendCoinsEntry
A single entry in the dialog for sending bitcoins.
Definition: sendcoinsentry.h:28
config.h
SendCoinsEntry::on_addressBookButton_clicked
void on_addressBookButton_clicked()
Definition: sendcoinsentry.cpp:77
AddressBookPage::getReturnValue
const QString & getReturnValue() const
Definition: addressbookpage.h:41
SendCoinsEntry::subtractFeeFromAmountChanged
void subtractFeeFromAmountChanged()
SendCoinsEntry::platformStyle
const PlatformStyle * platformStyle
Definition: sendcoinsentry.h:77
optionsmodel.h
SendCoinsEntry::on_pasteButton_clicked
void on_pasteButton_clicked()
Definition: sendcoinsentry.cpp:72
PlatformStyle
Definition: platformstyle.h:13
GUIUtil::setupAddressWidget
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:119
addresstablemodel.h
AddressBookPage::ForSelection
@ ForSelection
Open address book to pick address.
Definition: addressbookpage.h:32
SendCoinsRecipient::fSubtractFeeFromAmount
bool fSubtractFeeFromAmount
Definition: sendcoinsrecipient.h:57
SendCoinsEntry::getValue
SendCoinsRecipient getValue()
Definition: sendcoinsentry.cpp:189
SendCoinsRecipient::message
QString message
Definition: sendcoinsrecipient.h:43