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 <qt/sendcoinsentry.h>
6 #include <qt/forms/ui_sendcoinsentry.h>
8 #include <qt/addressbookpage.h>
9 #include <qt/addresstablemodel.h>
10 #include <qt/guiutil.h>
11 #include <qt/optionsmodel.h>
12 #include <qt/platformstyle.h>
13 #include <qt/walletmodel.h>
15 #include <QApplication>
18 SendCoinsEntry::SendCoinsEntry(const PlatformStyle
*_platformStyle
, QWidget
*parent
) :
19 QStackedWidget(parent
),
20 ui(new Ui::SendCoinsEntry
),
22 platformStyle(_platformStyle
)
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"));
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());
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()));
51 connect(ui
->useAvailableBalanceButton
, SIGNAL(clicked()), this, SLOT(useAvailableBalanceClicked()));
54 SendCoinsEntry::~SendCoinsEntry()
59 void SendCoinsEntry::on_pasteButton_clicked()
61 // Paste text from clipboard into recipient field
62 ui
->payTo
->setText(QApplication::clipboard()->text());
65 void SendCoinsEntry::on_addressBookButton_clicked()
69 AddressBookPage
dlg(platformStyle
, AddressBookPage::ForSelection
, AddressBookPage::SendingTab
, this);
70 dlg
.setModel(model
->getAddressTableModel());
73 ui
->payTo
->setText(dlg
.getReturnValue());
74 ui
->payAmount
->setFocus();
78 void SendCoinsEntry::on_payTo_textChanged(const QString
&address
)
83 void SendCoinsEntry::setModel(WalletModel
*_model
)
87 if (_model
&& _model
->getOptionsModel())
88 connect(_model
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
93 void SendCoinsEntry::clear()
95 // clear UI elements for normal payment
97 ui
->addAsLabel
->clear();
98 ui
->payAmount
->clear();
99 ui
->checkboxSubtractFeeFromAmount
->setCheckState(Qt::Unchecked
);
100 ui
->messageTextLabel
->clear();
101 ui
->messageTextLabel
->hide();
102 ui
->messageLabel
->hide();
103 // clear UI elements for unauthenticated payment request
104 ui
->payTo_is
->clear();
105 ui
->memoTextLabel_is
->clear();
106 ui
->payAmount_is
->clear();
107 // clear UI elements for authenticated payment request
108 ui
->payTo_s
->clear();
109 ui
->memoTextLabel_s
->clear();
110 ui
->payAmount_s
->clear();
112 // update the display unit, to not use the default ("BTC")
116 void SendCoinsEntry::checkSubtractFeeFromAmount()
118 ui
->checkboxSubtractFeeFromAmount
->setChecked(true);
121 void SendCoinsEntry::deleteClicked()
123 Q_EMIT
removeEntry(this);
126 void SendCoinsEntry::useAvailableBalanceClicked()
128 Q_EMIT
useAvailableBalance(this);
131 bool SendCoinsEntry::validate()
136 // Check input validity
139 // Skip checks for payment request
140 if (recipient
.paymentRequest
.IsInitialized())
143 if (!model
->validateAddress(ui
->payTo
->text()))
145 ui
->payTo
->setValid(false);
149 if (!ui
->payAmount
->validate())
154 // Sending a zero amount is invalid
155 if (ui
->payAmount
->value(0) <= 0)
157 ui
->payAmount
->setValid(false);
161 // Reject dust outputs:
162 if (retval
&& GUIUtil::isDust(ui
->payTo
->text(), ui
->payAmount
->value())) {
163 ui
->payAmount
->setValid(false);
170 SendCoinsRecipient
SendCoinsEntry::getValue()
173 if (recipient
.paymentRequest
.IsInitialized())
177 recipient
.address
= ui
->payTo
->text();
178 recipient
.label
= ui
->addAsLabel
->text();
179 recipient
.amount
= ui
->payAmount
->value();
180 recipient
.message
= ui
->messageTextLabel
->text();
181 recipient
.fSubtractFeeFromAmount
= (ui
->checkboxSubtractFeeFromAmount
->checkState() == Qt::Checked
);
186 QWidget
*SendCoinsEntry::setupTabChain(QWidget
*prev
)
188 QWidget::setTabOrder(prev
, ui
->payTo
);
189 QWidget::setTabOrder(ui
->payTo
, ui
->addAsLabel
);
190 QWidget
*w
= ui
->payAmount
->setupTabChain(ui
->addAsLabel
);
191 QWidget::setTabOrder(w
, ui
->checkboxSubtractFeeFromAmount
);
192 QWidget::setTabOrder(ui
->checkboxSubtractFeeFromAmount
, ui
->addressBookButton
);
193 QWidget::setTabOrder(ui
->addressBookButton
, ui
->pasteButton
);
194 QWidget::setTabOrder(ui
->pasteButton
, ui
->deleteButton
);
195 return ui
->deleteButton
;
198 void SendCoinsEntry::setValue(const SendCoinsRecipient
&value
)
202 if (recipient
.paymentRequest
.IsInitialized()) // payment request
204 if (recipient
.authenticatedMerchant
.isEmpty()) // unauthenticated
206 ui
->payTo_is
->setText(recipient
.address
);
207 ui
->memoTextLabel_is
->setText(recipient
.message
);
208 ui
->payAmount_is
->setValue(recipient
.amount
);
209 ui
->payAmount_is
->setReadOnly(true);
210 setCurrentWidget(ui
->SendCoins_UnauthenticatedPaymentRequest
);
212 else // authenticated
214 ui
->payTo_s
->setText(recipient
.authenticatedMerchant
);
215 ui
->memoTextLabel_s
->setText(recipient
.message
);
216 ui
->payAmount_s
->setValue(recipient
.amount
);
217 ui
->payAmount_s
->setReadOnly(true);
218 setCurrentWidget(ui
->SendCoins_AuthenticatedPaymentRequest
);
221 else // normal payment
224 ui
->messageTextLabel
->setText(recipient
.message
);
225 ui
->messageTextLabel
->setVisible(!recipient
.message
.isEmpty());
226 ui
->messageLabel
->setVisible(!recipient
.message
.isEmpty());
228 ui
->addAsLabel
->clear();
229 ui
->payTo
->setText(recipient
.address
); // this may set a label from addressbook
230 if (!recipient
.label
.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
231 ui
->addAsLabel
->setText(recipient
.label
);
232 ui
->payAmount
->setValue(recipient
.amount
);
236 void SendCoinsEntry::setAddress(const QString
&address
)
238 ui
->payTo
->setText(address
);
239 ui
->payAmount
->setFocus();
242 void SendCoinsEntry::setAmount(const CAmount
&amount
)
244 ui
->payAmount
->setValue(amount
);
247 bool SendCoinsEntry::isClear()
249 return ui
->payTo
->text().isEmpty() && ui
->payTo_is
->text().isEmpty() && ui
->payTo_s
->text().isEmpty();
252 void SendCoinsEntry::setFocus()
254 ui
->payTo
->setFocus();
257 void SendCoinsEntry::updateDisplayUnit()
259 if(model
&& model
->getOptionsModel())
261 // Update payAmount with the current unit
262 ui
->payAmount
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
263 ui
->payAmount_is
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
264 ui
->payAmount_s
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
268 bool SendCoinsEntry::updateLabel(const QString
&address
)
273 // Fill in label from address book, if address has an associated label
274 QString associatedLabel
= model
->getAddressTableModel()->labelForAddress(address
);
275 if(!associatedLabel
.isEmpty())
277 ui
->addAsLabel
->setText(associatedLabel
);