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)
6 #include <config/bitcoin-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
SendCoinsEntry(const PlatformStyle * _platformStyle,QWidget * parent)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
38 if (platformStyle->getUseExtraSpacing())
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
47 connect(ui->payAmount, &BitcoinAmountField::valueChanged, this, &SendCoinsEntry::payAmountChanged);
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
~SendCoinsEntry()55 SendCoinsEntry::~SendCoinsEntry()
56 {
57 delete ui;
58 }
59
on_pasteButton_clicked()60 void SendCoinsEntry::on_pasteButton_clicked()
61 {
62 // Paste text from clipboard into recipient field
63 ui->payTo->setText(QApplication::clipboard()->text());
64 }
65
on_addressBookButton_clicked()66 void SendCoinsEntry::on_addressBookButton_clicked()
67 {
68 if(!model)
69 return;
70 AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
71 dlg.setModel(model->getAddressTableModel());
72 if(dlg.exec())
73 {
74 ui->payTo->setText(dlg.getReturnValue());
75 ui->payAmount->setFocus();
76 }
77 }
78
on_payTo_textChanged(const QString & address)79 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
80 {
81 updateLabel(address);
82 }
83
setModel(WalletModel * _model)84 void SendCoinsEntry::setModel(WalletModel *_model)
85 {
86 this->model = _model;
87
88 if (_model && _model->getOptionsModel())
89 connect(_model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &SendCoinsEntry::updateDisplayUnit);
90
91 clear();
92 }
93
clear()94 void SendCoinsEntry::clear()
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")
114 updateDisplayUnit();
115 }
116
checkSubtractFeeFromAmount()117 void SendCoinsEntry::checkSubtractFeeFromAmount()
118 {
119 ui->checkboxSubtractFeeFromAmount->setChecked(true);
120 }
121
deleteClicked()122 void SendCoinsEntry::deleteClicked()
123 {
124 Q_EMIT removeEntry(this);
125 }
126
useAvailableBalanceClicked()127 void SendCoinsEntry::useAvailableBalanceClicked()
128 {
129 Q_EMIT useAvailableBalance(this);
130 }
131
validate(interfaces::Node & node)132 bool SendCoinsEntry::validate(interfaces::Node& node)
133 {
134 if (!model)
135 return false;
136
137 // Check input validity
138 bool retval = true;
139
140 if (!model->validateAddress(ui->payTo->text()))
141 {
142 ui->payTo->setValid(false);
143 retval = false;
144 }
145
146 if (!ui->payAmount->validate())
147 {
148 retval = false;
149 }
150
151 // Sending a zero amount is invalid
152 if (ui->payAmount->value(nullptr) <= 0)
153 {
154 ui->payAmount->setValid(false);
155 retval = false;
156 }
157
158 // Reject dust outputs:
159 if (retval && GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
160 ui->payAmount->setValid(false);
161 retval = false;
162 }
163
164 return retval;
165 }
166
getValue()167 SendCoinsRecipient SendCoinsEntry::getValue()
168 {
169 recipient.address = ui->payTo->text();
170 recipient.label = ui->addAsLabel->text();
171 recipient.amount = ui->payAmount->value();
172 recipient.message = ui->messageTextLabel->text();
173 recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
174
175 return recipient;
176 }
177
setupTabChain(QWidget * prev)178 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
179 {
180 QWidget::setTabOrder(prev, ui->payTo);
181 QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
182 QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
183 QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
184 QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
185 QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
186 QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
187 return ui->deleteButton;
188 }
189
setValue(const SendCoinsRecipient & value)190 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
191 {
192 recipient = value;
193 {
194 // message
195 ui->messageTextLabel->setText(recipient.message);
196 ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
197 ui->messageLabel->setVisible(!recipient.message.isEmpty());
198
199 ui->addAsLabel->clear();
200 ui->payTo->setText(recipient.address); // this may set a label from addressbook
201 if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
202 ui->addAsLabel->setText(recipient.label);
203 ui->payAmount->setValue(recipient.amount);
204 }
205 }
206
setAddress(const QString & address)207 void SendCoinsEntry::setAddress(const QString &address)
208 {
209 ui->payTo->setText(address);
210 ui->payAmount->setFocus();
211 }
212
setAmount(const CAmount & amount)213 void SendCoinsEntry::setAmount(const CAmount &amount)
214 {
215 ui->payAmount->setValue(amount);
216 }
217
isClear()218 bool SendCoinsEntry::isClear()
219 {
220 return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
221 }
222
setFocus()223 void SendCoinsEntry::setFocus()
224 {
225 ui->payTo->setFocus();
226 }
227
updateDisplayUnit()228 void SendCoinsEntry::updateDisplayUnit()
229 {
230 if(model && model->getOptionsModel())
231 {
232 // Update payAmount with the current unit
233 ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
234 ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
235 ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
236 }
237 }
238
changeEvent(QEvent * e)239 void SendCoinsEntry::changeEvent(QEvent* e)
240 {
241 if (e->type() == QEvent::PaletteChange) {
242 ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/address-book")));
243 ui->pasteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/editpaste")));
244 ui->deleteButton->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
245 ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
246 ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(QStringLiteral(":/icons/remove")));
247 }
248
249 QStackedWidget::changeEvent(e);
250 }
251
updateLabel(const QString & address)252 bool SendCoinsEntry::updateLabel(const QString &address)
253 {
254 if(!model)
255 return false;
256
257 // Fill in label from address book, if address has an associated label
258 QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
259 if(!associatedLabel.isEmpty())
260 {
261 ui->addAsLabel->setText(associatedLabel);
262 return true;
263 }
264
265 return false;
266 }
267