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