Line data Source code
1 : // Copyright (c) 2011-2013 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 : #include "sendcoinsentry.h"
6 : #include "ui_sendcoinsentry.h"
7 :
8 : #include "addressbookpage.h"
9 : #include "addresstablemodel.h"
10 : #include "guiutil.h"
11 : #include "optionsmodel.h"
12 : #include "platformstyle.h"
13 : #include "walletmodel.h"
14 :
15 : #include <QApplication>
16 : #include <QClipboard>
17 :
18 0 : SendCoinsEntry::SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent) :
19 : QStackedWidget(parent),
20 0 : ui(new Ui::SendCoinsEntry),
21 : model(0),
22 0 : platformStyle(platformStyle)
23 : {
24 0 : ui->setupUi(this);
25 :
26 0 : ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
27 0 : ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
28 0 : ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
29 0 : ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
30 0 : ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
31 :
32 0 : setCurrentWidget(ui->SendCoins);
33 :
34 0 : if (platformStyle->getUseExtraSpacing())
35 0 : ui->payToLayout->setSpacing(4);
36 : #if QT_VERSION >= 0x040700
37 0 : ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
38 : #endif
39 :
40 : // normal bitcoin address field
41 0 : GUIUtil::setupAddressWidget(ui->payTo, this);
42 : // just a label for displaying bitcoin address(es)
43 0 : ui->payTo_is->setFont(GUIUtil::bitcoinAddressFont());
44 :
45 : // Connect signals
46 0 : connect(ui->payAmount, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
47 0 : connect(ui->checkboxSubtractFeeFromAmount, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
48 0 : connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
49 0 : connect(ui->deleteButton_is, SIGNAL(clicked()), this, SLOT(deleteClicked()));
50 0 : connect(ui->deleteButton_s, SIGNAL(clicked()), this, SLOT(deleteClicked()));
51 0 : }
52 :
53 0 : SendCoinsEntry::~SendCoinsEntry()
54 : {
55 0 : delete ui;
56 0 : }
57 :
58 0 : void SendCoinsEntry::on_pasteButton_clicked()
59 : {
60 : // Paste text from clipboard into recipient field
61 0 : ui->payTo->setText(QApplication::clipboard()->text());
62 0 : }
63 :
64 0 : void SendCoinsEntry::on_addressBookButton_clicked()
65 : {
66 0 : if(!model)
67 0 : return;
68 0 : AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
69 0 : dlg.setModel(model->getAddressTableModel());
70 0 : if(dlg.exec())
71 : {
72 0 : ui->payTo->setText(dlg.getReturnValue());
73 0 : ui->payAmount->setFocus();
74 0 : }
75 : }
76 :
77 0 : void SendCoinsEntry::on_payTo_textChanged(const QString &address)
78 : {
79 0 : updateLabel(address);
80 0 : }
81 :
82 0 : void SendCoinsEntry::setModel(WalletModel *model)
83 : {
84 0 : this->model = model;
85 :
86 0 : if (model && model->getOptionsModel())
87 0 : connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
88 :
89 0 : clear();
90 0 : }
91 :
92 0 : void SendCoinsEntry::clear()
93 : {
94 : // clear UI elements for normal payment
95 0 : ui->payTo->clear();
96 0 : ui->addAsLabel->clear();
97 0 : ui->payAmount->clear();
98 0 : ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
99 0 : ui->messageTextLabel->clear();
100 0 : ui->messageTextLabel->hide();
101 0 : ui->messageLabel->hide();
102 : // clear UI elements for unauthenticated payment request
103 0 : ui->payTo_is->clear();
104 0 : ui->memoTextLabel_is->clear();
105 0 : ui->payAmount_is->clear();
106 : // clear UI elements for authenticated payment request
107 0 : ui->payTo_s->clear();
108 0 : ui->memoTextLabel_s->clear();
109 0 : ui->payAmount_s->clear();
110 :
111 : // update the display unit, to not use the default ("BTC")
112 0 : updateDisplayUnit();
113 0 : }
114 :
115 0 : void SendCoinsEntry::deleteClicked()
116 : {
117 0 : Q_EMIT removeEntry(this);
118 0 : }
119 :
120 0 : bool SendCoinsEntry::validate()
121 : {
122 0 : if (!model)
123 : return false;
124 :
125 : // Check input validity
126 0 : bool retval = true;
127 :
128 : // Skip checks for payment request
129 0 : if (recipient.paymentRequest.IsInitialized())
130 : return retval;
131 :
132 0 : if (!model->validateAddress(ui->payTo->text()))
133 : {
134 0 : ui->payTo->setValid(false);
135 0 : retval = false;
136 : }
137 :
138 0 : if (!ui->payAmount->validate())
139 : {
140 0 : retval = false;
141 : }
142 :
143 : // Sending a zero amount is invalid
144 0 : if (ui->payAmount->value(0) <= 0)
145 : {
146 0 : ui->payAmount->setValid(false);
147 0 : retval = false;
148 : }
149 :
150 : // Reject dust outputs:
151 0 : if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
152 0 : ui->payAmount->setValid(false);
153 0 : retval = false;
154 : }
155 :
156 0 : return retval;
157 : }
158 :
159 0 : SendCoinsRecipient SendCoinsEntry::getValue()
160 : {
161 : // Payment request
162 0 : if (recipient.paymentRequest.IsInitialized())
163 0 : return recipient;
164 :
165 : // Normal payment
166 0 : recipient.address = ui->payTo->text();
167 0 : recipient.label = ui->addAsLabel->text();
168 0 : recipient.amount = ui->payAmount->value();
169 0 : recipient.message = ui->messageTextLabel->text();
170 0 : recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
171 :
172 0 : return recipient;
173 : }
174 :
175 0 : QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
176 : {
177 0 : QWidget::setTabOrder(prev, ui->payTo);
178 0 : QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
179 0 : QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
180 0 : QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
181 0 : QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
182 0 : QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
183 0 : QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
184 0 : return ui->deleteButton;
185 : }
186 :
187 0 : void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
188 : {
189 0 : recipient = value;
190 :
191 0 : if (recipient.paymentRequest.IsInitialized()) // payment request
192 : {
193 0 : if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
194 : {
195 0 : ui->payTo_is->setText(recipient.address);
196 0 : ui->memoTextLabel_is->setText(recipient.message);
197 0 : ui->payAmount_is->setValue(recipient.amount);
198 0 : ui->payAmount_is->setReadOnly(true);
199 0 : setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
200 : }
201 : else // authenticated
202 : {
203 0 : ui->payTo_s->setText(recipient.authenticatedMerchant);
204 0 : ui->memoTextLabel_s->setText(recipient.message);
205 0 : ui->payAmount_s->setValue(recipient.amount);
206 0 : ui->payAmount_s->setReadOnly(true);
207 0 : setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
208 : }
209 : }
210 : else // normal payment
211 : {
212 : // message
213 0 : ui->messageTextLabel->setText(recipient.message);
214 0 : ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
215 0 : ui->messageLabel->setVisible(!recipient.message.isEmpty());
216 :
217 0 : ui->addAsLabel->clear();
218 0 : ui->payTo->setText(recipient.address); // this may set a label from addressbook
219 0 : if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
220 0 : ui->addAsLabel->setText(recipient.label);
221 0 : ui->payAmount->setValue(recipient.amount);
222 : }
223 0 : }
224 :
225 0 : void SendCoinsEntry::setAddress(const QString &address)
226 : {
227 0 : ui->payTo->setText(address);
228 0 : ui->payAmount->setFocus();
229 0 : }
230 :
231 0 : bool SendCoinsEntry::isClear()
232 : {
233 0 : return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
234 : }
235 :
236 0 : void SendCoinsEntry::setFocus()
237 : {
238 0 : ui->payTo->setFocus();
239 0 : }
240 :
241 0 : void SendCoinsEntry::updateDisplayUnit()
242 : {
243 0 : if(model && model->getOptionsModel())
244 : {
245 : // Update payAmount with the current unit
246 0 : ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
247 0 : ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
248 0 : ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
249 : }
250 0 : }
251 :
252 0 : bool SendCoinsEntry::updateLabel(const QString &address)
253 : {
254 0 : if(!model)
255 : return false;
256 :
257 : // Fill in label from address book, if address has an associated label
258 0 : QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
259 0 : if(!associatedLabel.isEmpty())
260 : {
261 0 : ui->addAsLabel->setText(associatedLabel);
262 : return true;
263 : }
264 :
265 0 : return false;
266 0 : }
|