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>
14 #include <QApplication>
17 SendCoinsEntry::SendCoinsEntry(const PlatformStyle
*_platformStyle
, QWidget
*parent
) :
18 QStackedWidget(parent
),
19 ui(new Ui::SendCoinsEntry
),
21 platformStyle(_platformStyle
)
25 ui
->addressBookButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/address-book"));
26 ui
->pasteButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/editpaste"));
27 ui
->deleteButton
->setIcon(platformStyle
->SingleColorIcon(":/icons/remove"));
28 ui
->deleteButton_is
->setIcon(platformStyle
->SingleColorIcon(":/icons/remove"));
29 ui
->deleteButton_s
->setIcon(platformStyle
->SingleColorIcon(":/icons/remove"));
31 setCurrentWidget(ui
->SendCoins
);
33 if (platformStyle
->getUseExtraSpacing())
34 ui
->payToLayout
->setSpacing(4);
35 #if QT_VERSION >= 0x040700
36 ui
->addAsLabel
->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
39 // normal bitcoin address field
40 GUIUtil::setupAddressWidget(ui
->payTo
, this);
41 // just a label for displaying bitcoin address(es)
42 ui
->payTo_is
->setFont(GUIUtil::fixedPitchFont());
45 connect(ui
->payAmount
, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
46 connect(ui
->checkboxSubtractFeeFromAmount
, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
47 connect(ui
->deleteButton
, SIGNAL(clicked()), this, SLOT(deleteClicked()));
48 connect(ui
->deleteButton_is
, SIGNAL(clicked()), this, SLOT(deleteClicked()));
49 connect(ui
->deleteButton_s
, SIGNAL(clicked()), this, SLOT(deleteClicked()));
50 connect(ui
->useAvailableBalanceButton
, SIGNAL(clicked()), this, SLOT(useAvailableBalanceClicked()));
53 SendCoinsEntry::~SendCoinsEntry()
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()
68 AddressBookPage
dlg(platformStyle
, AddressBookPage::ForSelection
, AddressBookPage::SendingTab
, this);
69 dlg
.setModel(model
->getAddressTableModel());
72 ui
->payTo
->setText(dlg
.getReturnValue());
73 ui
->payAmount
->setFocus();
77 void SendCoinsEntry::on_payTo_textChanged(const QString
&address
)
82 void SendCoinsEntry::setModel(WalletModel
*_model
)
86 if (_model
&& _model
->getOptionsModel())
87 connect(_model
->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
92 void SendCoinsEntry::clear()
94 // clear UI elements for normal payment
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")
115 void SendCoinsEntry::checkSubtractFeeFromAmount()
117 ui
->checkboxSubtractFeeFromAmount
->setChecked(true);
120 void SendCoinsEntry::deleteClicked()
122 Q_EMIT
removeEntry(this);
125 void SendCoinsEntry::useAvailableBalanceClicked()
127 Q_EMIT
useAvailableBalance(this);
130 bool SendCoinsEntry::validate()
135 // Check input validity
138 // Skip checks for payment request
139 if (recipient
.paymentRequest
.IsInitialized())
142 if (!model
->validateAddress(ui
->payTo
->text()))
144 ui
->payTo
->setValid(false);
148 if (!ui
->payAmount
->validate())
153 // Sending a zero amount is invalid
154 if (ui
->payAmount
->value(0) <= 0)
156 ui
->payAmount
->setValid(false);
160 // Reject dust outputs:
161 if (retval
&& GUIUtil::isDust(ui
->payTo
->text(), ui
->payAmount
->value())) {
162 ui
->payAmount
->setValid(false);
169 SendCoinsRecipient
SendCoinsEntry::getValue()
172 if (recipient
.paymentRequest
.IsInitialized())
176 recipient
.address
= ui
->payTo
->text();
177 recipient
.label
= ui
->addAsLabel
->text();
178 recipient
.amount
= ui
->payAmount
->value();
179 recipient
.message
= ui
->messageTextLabel
->text();
180 recipient
.fSubtractFeeFromAmount
= (ui
->checkboxSubtractFeeFromAmount
->checkState() == Qt::Checked
);
185 QWidget
*SendCoinsEntry::setupTabChain(QWidget
*prev
)
187 QWidget::setTabOrder(prev
, ui
->payTo
);
188 QWidget::setTabOrder(ui
->payTo
, ui
->addAsLabel
);
189 QWidget
*w
= ui
->payAmount
->setupTabChain(ui
->addAsLabel
);
190 QWidget::setTabOrder(w
, ui
->checkboxSubtractFeeFromAmount
);
191 QWidget::setTabOrder(ui
->checkboxSubtractFeeFromAmount
, ui
->addressBookButton
);
192 QWidget::setTabOrder(ui
->addressBookButton
, ui
->pasteButton
);
193 QWidget::setTabOrder(ui
->pasteButton
, ui
->deleteButton
);
194 return ui
->deleteButton
;
197 void SendCoinsEntry::setValue(const SendCoinsRecipient
&value
)
201 if (recipient
.paymentRequest
.IsInitialized()) // payment request
203 if (recipient
.authenticatedMerchant
.isEmpty()) // unauthenticated
205 ui
->payTo_is
->setText(recipient
.address
);
206 ui
->memoTextLabel_is
->setText(recipient
.message
);
207 ui
->payAmount_is
->setValue(recipient
.amount
);
208 ui
->payAmount_is
->setReadOnly(true);
209 setCurrentWidget(ui
->SendCoins_UnauthenticatedPaymentRequest
);
211 else // authenticated
213 ui
->payTo_s
->setText(recipient
.authenticatedMerchant
);
214 ui
->memoTextLabel_s
->setText(recipient
.message
);
215 ui
->payAmount_s
->setValue(recipient
.amount
);
216 ui
->payAmount_s
->setReadOnly(true);
217 setCurrentWidget(ui
->SendCoins_AuthenticatedPaymentRequest
);
220 else // normal payment
223 ui
->messageTextLabel
->setText(recipient
.message
);
224 ui
->messageTextLabel
->setVisible(!recipient
.message
.isEmpty());
225 ui
->messageLabel
->setVisible(!recipient
.message
.isEmpty());
227 ui
->addAsLabel
->clear();
228 ui
->payTo
->setText(recipient
.address
); // this may set a label from addressbook
229 if (!recipient
.label
.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
230 ui
->addAsLabel
->setText(recipient
.label
);
231 ui
->payAmount
->setValue(recipient
.amount
);
235 void SendCoinsEntry::setAddress(const QString
&address
)
237 ui
->payTo
->setText(address
);
238 ui
->payAmount
->setFocus();
241 void SendCoinsEntry::setAmount(const CAmount
&amount
)
243 ui
->payAmount
->setValue(amount
);
246 bool SendCoinsEntry::isClear()
248 return ui
->payTo
->text().isEmpty() && ui
->payTo_is
->text().isEmpty() && ui
->payTo_s
->text().isEmpty();
251 void SendCoinsEntry::setFocus()
253 ui
->payTo
->setFocus();
256 void SendCoinsEntry::updateDisplayUnit()
258 if(model
&& model
->getOptionsModel())
260 // Update payAmount with the current unit
261 ui
->payAmount
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
262 ui
->payAmount_is
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
263 ui
->payAmount_s
->setDisplayUnit(model
->getOptionsModel()->getDisplayUnit());
267 bool SendCoinsEntry::updateLabel(const QString
&address
)
272 // Fill in label from address book, if address has an associated label
273 QString associatedLabel
= model
->getAddressTableModel()->labelForAddress(address
);
274 if(!associatedLabel
.isEmpty())
276 ui
->addAsLabel
->setText(associatedLabel
);