Merge #10114: [tests] sync_with_ping should assert that ping hasn't timed out
[bitcoinplatinum.git] / src / qt / sendcoinsentry.cpp
blobbb0f47b21c0f4c9ae15363d99725af7a291c8432
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.
5 #include "sendcoinsentry.h"
6 #include "ui_sendcoinsentry.h"
8 #include "addressbookpage.h"
9 #include "addresstablemodel.h"
10 #include "guiutil.h"
11 #include "optionsmodel.h"
12 #include "platformstyle.h"
13 #include "walletmodel.h"
15 #include <QApplication>
16 #include <QClipboard>
18 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
19 QStackedWidget(parent),
20 ui(new Ui::SendCoinsEntry),
21 model(0),
22 platformStyle(_platformStyle)
24 ui->setupUi(this);
26 ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
27 ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
28 ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
29 ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
30 ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
32 setCurrentWidget(ui->SendCoins);
34 if (platformStyle->getUseExtraSpacing())
35 ui->payToLayout->setSpacing(4);
36 #if QT_VERSION >= 0x040700
37 ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
38 #endif
40 // normal bitcoin address field
41 GUIUtil::setupAddressWidget(ui->payTo, this);
42 // just a label for displaying bitcoin address(es)
43 ui->payTo_is->setFont(GUIUtil::fixedPitchFont());
45 // Connect signals
46 connect(ui->payAmount, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
47 connect(ui->checkboxSubtractFeeFromAmount, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
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()));
53 SendCoinsEntry::~SendCoinsEntry()
55 delete ui;
58 void SendCoinsEntry::on_pasteButton_clicked()
60 // Paste text from clipboard into recipient field
61 ui->payTo->setText(QApplication::clipboard()->text());
64 void SendCoinsEntry::on_addressBookButton_clicked()
66 if(!model)
67 return;
68 AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
69 dlg.setModel(model->getAddressTableModel());
70 if(dlg.exec())
72 ui->payTo->setText(dlg.getReturnValue());
73 ui->payAmount->setFocus();
77 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
79 updateLabel(address);
82 void SendCoinsEntry::setModel(WalletModel *_model)
84 this->model = _model;
86 if (_model && _model->getOptionsModel())
87 connect(_model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
89 clear();
92 void SendCoinsEntry::clear()
94 // clear UI elements for normal payment
95 ui->payTo->clear();
96 ui->addAsLabel->clear();
97 ui->payAmount->clear();
98 ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
99 ui->messageTextLabel->clear();
100 ui->messageTextLabel->hide();
101 ui->messageLabel->hide();
102 // clear UI elements for unauthenticated payment request
103 ui->payTo_is->clear();
104 ui->memoTextLabel_is->clear();
105 ui->payAmount_is->clear();
106 // clear UI elements for authenticated payment request
107 ui->payTo_s->clear();
108 ui->memoTextLabel_s->clear();
109 ui->payAmount_s->clear();
111 // update the display unit, to not use the default ("BTC")
112 updateDisplayUnit();
115 void SendCoinsEntry::deleteClicked()
117 Q_EMIT removeEntry(this);
120 bool SendCoinsEntry::validate()
122 if (!model)
123 return false;
125 // Check input validity
126 bool retval = true;
128 // Skip checks for payment request
129 if (recipient.paymentRequest.IsInitialized())
130 return retval;
132 if (!model->validateAddress(ui->payTo->text()))
134 ui->payTo->setValid(false);
135 retval = false;
138 if (!ui->payAmount->validate())
140 retval = false;
143 // Sending a zero amount is invalid
144 if (ui->payAmount->value(0) <= 0)
146 ui->payAmount->setValid(false);
147 retval = false;
150 // Reject dust outputs:
151 if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
152 ui->payAmount->setValid(false);
153 retval = false;
156 return retval;
159 SendCoinsRecipient SendCoinsEntry::getValue()
161 // Payment request
162 if (recipient.paymentRequest.IsInitialized())
163 return recipient;
165 // Normal payment
166 recipient.address = ui->payTo->text();
167 recipient.label = ui->addAsLabel->text();
168 recipient.amount = ui->payAmount->value();
169 recipient.message = ui->messageTextLabel->text();
170 recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
172 return recipient;
175 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
177 QWidget::setTabOrder(prev, ui->payTo);
178 QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
179 QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
180 QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
181 QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
182 QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
183 QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
184 return ui->deleteButton;
187 void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
189 recipient = value;
191 if (recipient.paymentRequest.IsInitialized()) // payment request
193 if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
195 ui->payTo_is->setText(recipient.address);
196 ui->memoTextLabel_is->setText(recipient.message);
197 ui->payAmount_is->setValue(recipient.amount);
198 ui->payAmount_is->setReadOnly(true);
199 setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
201 else // authenticated
203 ui->payTo_s->setText(recipient.authenticatedMerchant);
204 ui->memoTextLabel_s->setText(recipient.message);
205 ui->payAmount_s->setValue(recipient.amount);
206 ui->payAmount_s->setReadOnly(true);
207 setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
210 else // normal payment
212 // message
213 ui->messageTextLabel->setText(recipient.message);
214 ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
215 ui->messageLabel->setVisible(!recipient.message.isEmpty());
217 ui->addAsLabel->clear();
218 ui->payTo->setText(recipient.address); // this may set a label from addressbook
219 if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
220 ui->addAsLabel->setText(recipient.label);
221 ui->payAmount->setValue(recipient.amount);
225 void SendCoinsEntry::setAddress(const QString &address)
227 ui->payTo->setText(address);
228 ui->payAmount->setFocus();
231 bool SendCoinsEntry::isClear()
233 return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
236 void SendCoinsEntry::setFocus()
238 ui->payTo->setFocus();
241 void SendCoinsEntry::updateDisplayUnit()
243 if(model && model->getOptionsModel())
245 // Update payAmount with the current unit
246 ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
247 ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
248 ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
252 bool SendCoinsEntry::updateLabel(const QString &address)
254 if(!model)
255 return false;
257 // Fill in label from address book, if address has an associated label
258 QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
259 if(!associatedLabel.isEmpty())
261 ui->addAsLabel->setText(associatedLabel);
262 return true;
265 return false;