forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
send.cpp
798 lines (684 loc) · 27.8 KB
/
send.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright (c) 2019-2020 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "qt/pivx/send.h"
#include "qt/pivx/forms/ui_send.h"
#include "qt/pivx/addnewcontactdialog.h"
#include "qt/pivx/qtutils.h"
#include "qt/pivx/sendchangeaddressdialog.h"
#include "qt/pivx/optionbutton.h"
#include "qt/pivx/sendconfirmdialog.h"
#include "qt/pivx/myaddressrow.h"
#include "qt/pivx/guitransactionsutils.h"
#include "clientmodel.h"
#include "optionsmodel.h"
#include "addresstablemodel.h"
#include "coincontrol.h"
#include "script/standard.h"
#include "zpiv/deterministicmint.h"
#include "openuridialog.h"
#include "zpivcontroldialog.h"
SendWidget::SendWidget(PIVXGUI* parent) :
PWidget(parent),
ui(new Ui::send),
coinIcon(new QPushButton()),
btnContacts(new QPushButton())
{
ui->setupUi(this);
this->setStyleSheet(parent->styleSheet());
/* Containers */
setCssProperty(ui->left, "container");
ui->left->setContentsMargins(0,20,0,20);
setCssProperty(ui->right, "container-right");
ui->right->setContentsMargins(20,10,20,20);
/* Light Font */
QFont fontLight;
fontLight.setWeight(QFont::Light);
/* Title */
ui->labelTitle->setText(tr("Send"));
setCssProperty(ui->labelTitle, "text-title-screen");
ui->labelTitle->setFont(fontLight);
/* Button Group */
ui->pushLeft->setText("PIV");
setCssProperty(ui->pushLeft, "btn-check-left");
ui->pushLeft->setChecked(true);
ui->pushRight->setText("zPIV");
setCssProperty(ui->pushRight, "btn-check-right");
/* Subtitle */
ui->labelSubtitle1->setText(tr("You can transfer public coins (PIV) or private coins (zPIV)"));
setCssProperty(ui->labelSubtitle1, "text-subtitle");
ui->labelSubtitle2->setText(tr("Select coin type to spend"));
setCssProperty(ui->labelSubtitle2, "text-subtitle");
/* Address */
ui->labelSubtitleAddress->setText(tr("PIVX address or contact label"));
setCssProperty(ui->labelSubtitleAddress, "text-title");
/* Amount */
ui->labelSubtitleAmount->setText(tr("Amount"));
setCssProperty(ui->labelSubtitleAmount, "text-title");
/* Buttons */
ui->pushButtonFee->setText(tr("Customize fee"));
setCssBtnSecondary(ui->pushButtonFee);
ui->pushButtonClear->setText(tr("Clear all"));
setCssProperty(ui->pushButtonClear, "btn-secundary-clear");
ui->pushButtonAddRecipient->setText(tr("Add recipient"));
setCssProperty(ui->pushButtonAddRecipient, "btn-secundary-add");
setCssBtnPrimary(ui->pushButtonSave);
ui->pushButtonReset->setText(tr("Reset to default"));
setCssBtnSecondary(ui->pushButtonReset);
// Coin control
ui->btnCoinControl->setTitleClassAndText("btn-title-grey", "Coin Control");
ui->btnCoinControl->setSubTitleClassAndText("text-subtitle", "Select the source of the coins.");
// Change address option
ui->btnChangeAddress->setTitleClassAndText("btn-title-grey", "Change Address");
ui->btnChangeAddress->setSubTitleClassAndText("text-subtitle", "Customize the change address.");
// Uri
ui->btnUri->setTitleClassAndText("btn-title-grey", "Open URI");
ui->btnUri->setSubTitleClassAndText("text-subtitle", "Parse a payment request.");
connect(ui->pushButtonFee, SIGNAL(clicked()), this, SLOT(onChangeCustomFeeClicked()));
connect(ui->btnCoinControl, SIGNAL(clicked()), this, SLOT(onCoinControlClicked()));
connect(ui->btnChangeAddress, SIGNAL(clicked()), this, SLOT(onChangeAddressClicked()));
connect(ui->btnUri, SIGNAL(clicked()), this, SLOT(onOpenUriClicked()));
connect(ui->pushButtonReset, &QPushButton::clicked, [this](){ onResetCustomOptions(true); });
setCssProperty(ui->coinWidget, "container-coin-type");
setCssProperty(ui->labelLine, "container-divider");
// Total Send
ui->labelTitleTotalSend->setText(tr("Total to send"));
setCssProperty(ui->labelTitleTotalSend, "text-title");
ui->labelAmountSend->setText("0.00 PIV");
setCssProperty(ui->labelAmountSend, "text-body1");
// Total Remaining
setCssProperty(ui->labelTitleTotalRemaining, "text-title");
setCssProperty(ui->labelAmountRemaining, "text-body1");
// Icon Send
ui->stackedWidget->addWidget(coinIcon);
coinIcon->show();
coinIcon->raise();
setCssProperty(coinIcon, "coin-icon-piv");
QSize BUTTON_SIZE = QSize(24, 24);
coinIcon->setMinimumSize(BUTTON_SIZE);
coinIcon->setMaximumSize(BUTTON_SIZE);
int posX = 0;
int posY = 20;
coinIcon->move(posX, posY);
// Entry
addEntry();
// Connect
connect(ui->pushLeft, &QPushButton::clicked, [this](){onPIVSelected(true);});
connect(ui->pushRight, &QPushButton::clicked, [this](){onPIVSelected(false);});
connect(ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(onSendClicked()));
connect(ui->pushButtonAddRecipient, SIGNAL(clicked()), this, SLOT(onAddEntryClicked()));
connect(ui->pushButtonClear, SIGNAL(clicked()), this, SLOT(clearAll()));
}
void SendWidget::refreshView(){
QString btnText;
if(ui->pushLeft->isChecked()){
btnText = tr("Send PIV");
ui->pushButtonAddRecipient->setVisible(true);
}else{
btnText = tr("Send zPIV");
ui->pushButtonAddRecipient->setVisible(false);
}
ui->pushButtonSave->setText(btnText);
refreshAmounts();
}
void SendWidget::refreshAmounts() {
CAmount total = 0;
QMutableListIterator<SendMultiRow*> it(entries);
while (it.hasNext()) {
SendMultiRow* entry = it.next();
CAmount amount = entry->getAmountValue();
if (amount > 0)
total += amount;
}
bool isZpiv = ui->pushRight->isChecked();
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
ui->labelAmountSend->setText(GUIUtil::formatBalance(total, nDisplayUnit, isZpiv));
CAmount totalAmount = 0;
if (CoinControlDialog::coinControl->HasSelected()){
// Set remaining balance to the sum of the coinControl selected inputs
totalAmount = walletModel->getBalance(CoinControlDialog::coinControl) - total;
ui->labelTitleTotalRemaining->setText(tr("Total remaining from the selected UTXO"));
} else {
// Wallet's balance
totalAmount = (isZpiv ? walletModel->getZerocoinBalance() : walletModel->getBalance()) - total;
ui->labelTitleTotalRemaining->setText(tr("Total remaining"));
}
ui->labelAmountRemaining->setText(
GUIUtil::formatBalance(
totalAmount,
nDisplayUnit,
isZpiv
)
);
}
void SendWidget::loadClientModel(){
if (clientModel) {
connect(clientModel, &ClientModel::numBlocksChanged, [this](){
if (customFeeDialog) customFeeDialog->updateFee();
});
}
}
void SendWidget::loadWalletModel() {
if (walletModel && walletModel->getOptionsModel()) {
// display unit
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
for(SendMultiRow *entry : entries){
if(entry){
entry->setWalletModel(walletModel);
}
}
// Refresh view
refreshView();
// TODO: This only happen when the coin control features are modified in other screen, check before do this if the wallet has another screen modifying it.
// Coin Control
//connect(model->getOptionsModel(), SIGNAL(coinControlFeaturesChanged(bool)), this, SLOT(coinControlFeatureChanged(bool)));
//ui->frameCoinControl->setVisible(model->getOptionsModel()->getCoinControlFeatures());
//coinControlUpdateLabels();
}
}
void SendWidget::clearAll(){
onResetCustomOptions(false);
if(customFeeDialog) customFeeDialog->clear();
ui->pushButtonFee->setText(tr("Customize Fee"));
if(walletModel) walletModel->setWalletDefaultFee();
clearEntries();
refreshAmounts();
}
void SendWidget::onResetCustomOptions(bool fRefreshAmounts){
CoinControlDialog::coinControl->SetNull();
ui->btnChangeAddress->setActive(false);
ui->btnCoinControl->setActive(false);
if (fRefreshAmounts) {
refreshAmounts();
}
}
void SendWidget::clearEntries(){
int num = entries.length();
for (int i = 0; i < num; ++i) {
ui->scrollAreaWidgetContents->layout()->takeAt(0)->widget()->deleteLater();
}
entries.clear();
addEntry();
}
void SendWidget::addEntry(){
if(entries.isEmpty()){
createEntry();
} else {
if (entries.length() == 1) {
SendMultiRow *entry = entries.at(0);
entry->hideLabels();
entry->setNumber(1);
}else if(entries.length() == MAX_SEND_POPUP_ENTRIES){
inform(tr("Maximum amount of outputs reached"));
return;
}
SendMultiRow *sendMultiRow = createEntry();
sendMultiRow->setNumber(entries.length());
sendMultiRow->hideLabels();
}
}
SendMultiRow* SendWidget::createEntry(){
SendMultiRow *sendMultiRow = new SendMultiRow(this);
if(this->walletModel) sendMultiRow->setWalletModel(this->walletModel);
entries.append(sendMultiRow);
ui->scrollAreaWidgetContents->layout()->addWidget(sendMultiRow);
connect(sendMultiRow, &SendMultiRow::onContactsClicked, this, &SendWidget::onContactsClicked);
connect(sendMultiRow, &SendMultiRow::onMenuClicked, this, &SendWidget::onMenuClicked);
connect(sendMultiRow, &SendMultiRow::onValueChanged, this, &SendWidget::onValueChanged);
return sendMultiRow;
}
void SendWidget::onAddEntryClicked(){
// Check prev valid entries before add a new one.
for (SendMultiRow* entry : entries){
if(!entry || !entry->validate()) {
inform(tr("Invalid entry, previous entries must be valid before add a new one"));
return;
}
}
addEntry();
}
void SendWidget::resizeEvent(QResizeEvent *event){
resizeMenu();
QWidget::resizeEvent(event);
}
void SendWidget::onSendClicked(){
if (!walletModel || !walletModel->getOptionsModel())
return;
QList<SendCoinsRecipient> recipients;
for (SendMultiRow* entry : entries){
// TODO: Check UTXO splitter here..
// Validate send..
if(entry && entry->validate()) {
recipients.append(entry->getValue());
}else{
inform(tr("Invalid entry"));
return;
}
}
if (recipients.isEmpty()) {
inform(tr("No set recipients"));
return;
}
bool sendPiv = ui->pushLeft->isChecked();
// request unlock only if was locked or unlocked for mixing:
// this way we let users unlock by walletpassphrase or by menu
// and make many transactions while unlocking through this dialog
// will call relock
if(!GUIUtil::requestUnlock(walletModel, sendPiv ? AskPassphraseDialog::Context::Send_PIV : AskPassphraseDialog::Context::Send_zPIV, true)){
// Unlock wallet was cancelled
inform(tr("Cannot send, wallet locked"));
return;
}
if((sendPiv) ? send(recipients) : sendZpiv(recipients)) {
updateEntryLabels(recipients);
}
}
bool SendWidget::send(QList<SendCoinsRecipient> recipients){
// prepare transaction for getting txFee earlier
WalletModelTransaction currentTransaction(recipients);
WalletModel::SendCoinsReturn prepareStatus;
prepareStatus = walletModel->prepareTransaction(currentTransaction, CoinControlDialog::coinControl);
// process prepareStatus and on error generate message shown to user
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
this,
prepareStatus,
walletModel,
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(),
currentTransaction.getTransactionFee()),
true
);
if (prepareStatus.status != WalletModel::OK) {
inform(tr("Cannot create transaction."));
return false;
}
showHideOp(true);
QString warningStr = QString();
if (currentTransaction.getTransaction()->fStakeDelegationVoided)
warningStr = tr("WARNING:\nTransaction spends a cold-stake delegation, voiding it.\n"
"These coins will no longer be cold-staked.");
TxDetailDialog* dialog = new TxDetailDialog(window, true, warningStr);
dialog->setDisplayUnit(walletModel->getOptionsModel()->getDisplayUnit());
dialog->setData(walletModel, currentTransaction);
dialog->adjustSize();
openDialogWithOpaqueBackgroundY(dialog, window, 3, 5);
if(dialog->isConfirm()){
// now send the prepared transaction
WalletModel::SendCoinsReturn sendStatus = dialog->getStatus();
// process sendStatus and on error generate message shown to user
GuiTransactionsUtils::ProcessSendCoinsReturnAndInform(
this,
sendStatus,
walletModel
);
if (sendStatus.status == WalletModel::OK) {
clearAll();
inform(tr("Transaction sent"));
dialog->deleteLater();
return true;
}
}
dialog->deleteLater();
return false;
}
bool SendWidget::sendZpiv(QList<SendCoinsRecipient> recipients){
if (!walletModel || !walletModel->getOptionsModel())
return false;
if(sporkManager.IsSporkActive(SPORK_16_ZEROCOIN_MAINTENANCE_MODE)) {
emit message(tr("Spend Zerocoin"), tr("zPIV is currently undergoing maintenance."), CClientUIInterface::MSG_ERROR);
return false;
}
std::list<std::pair<CBitcoinAddress*, CAmount>> outputs;
CAmount total = 0;
for (SendCoinsRecipient rec : recipients){
total += rec.amount;
outputs.push_back(std::pair<CBitcoinAddress*, CAmount>(new CBitcoinAddress(rec.address.toStdString()),rec.amount));
}
// use mints from zPIV selector if applicable
std::vector<CMintMeta> vMintsToFetch;
std::vector<CZerocoinMint> vMintsSelected;
if (!ZPivControlDialog::setSelectedMints.empty()) {
vMintsToFetch = ZPivControlDialog::GetSelectedMints();
for (auto& meta : vMintsToFetch) {
CZerocoinMint mint;
if (!walletModel->getMint(meta.hashSerial, mint)){
inform(tr("Coin control mint not found"));
return false;
}
vMintsSelected.emplace_back(mint);
}
}
QString sendBody = outputs.size() == 1 ?
tr("Sending %1 to address %2\n")
.arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), total, false, BitcoinUnits::separatorAlways))
.arg(recipients.first().address)
:
tr("Sending %1 to addresses:\n%2")
.arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), total, false, BitcoinUnits::separatorAlways))
.arg(recipientsToString(recipients));
bool ret = false;
emit message(
tr("Spend Zerocoin"),
sendBody,
CClientUIInterface::MSG_INFORMATION | CClientUIInterface::BTN_MASK | CClientUIInterface::MODAL,
&ret);
if(!ret) return false;
CZerocoinSpendReceipt receipt;
std::string changeAddress = "";
if(!boost::get<CNoDestination>(&CoinControlDialog::coinControl->destChange)){
changeAddress = CBitcoinAddress(CoinControlDialog::coinControl->destChange).ToString();
}else{
changeAddress = walletModel->getAddressTableModel()->getAddressToShow().toStdString();
}
if (walletModel->sendZpiv(
vMintsSelected,
true,
true,
receipt,
outputs,
changeAddress
)
) {
inform(tr("zPIV transaction sent!"));
ZPivControlDialog::setSelectedMints.clear();
clearAll();
return true;
} else {
QString body;
if (receipt.GetStatus() == ZPIV_SPEND_V1_SEC_LEVEL) {
body = tr("Version 1 zPIV require a security level of 100 to successfully spend.");
} else {
int nNeededSpends = receipt.GetNeededSpends(); // Number of spends we would need for this transaction
const int nMaxSpends = Params().Zerocoin_MaxSpendsPerTransaction(); // Maximum possible spends for one zPIV transaction
if (nNeededSpends > nMaxSpends) {
body = tr("Too much inputs (") + QString::number(nNeededSpends, 10) +
tr(") needed.\nMaximum allowed: ") + QString::number(nMaxSpends, 10);
body += tr(
"\nEither mint higher denominations (so fewer inputs are needed) or reduce the amount to spend.");
} else {
body = QString::fromStdString(receipt.GetStatusMessage());
}
}
emit message("zPIV transaction failed", body, CClientUIInterface::MSG_ERROR);
return false;
}
}
QString SendWidget::recipientsToString(QList<SendCoinsRecipient> recipients){
QString s = "";
for (SendCoinsRecipient rec : recipients){
s += rec.address + " -> " + BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), rec.amount, false, BitcoinUnits::separatorAlways) + "\n";
}
return s;
}
void SendWidget::updateEntryLabels(QList<SendCoinsRecipient> recipients){
for (SendCoinsRecipient rec : recipients){
QString label = rec.label;
if(!label.isNull()) {
QString labelOld = walletModel->getAddressTableModel()->labelForAddress(rec.address);
if(label.compare(labelOld) != 0) {
CTxDestination dest = CBitcoinAddress(rec.address.toStdString()).Get();
if (!walletModel->updateAddressBookLabels(dest, label.toStdString(),
this->walletModel->isMine(dest) ?
AddressBook::AddressBookPurpose::RECEIVE :
AddressBook::AddressBookPurpose::SEND)) {
// Label update failed
emit message("", tr("Address label update failed for address: %1").arg(rec.address), CClientUIInterface::MSG_ERROR);
return;
}
}
}
}
}
void SendWidget::onChangeAddressClicked(){
showHideOp(true);
SendChangeAddressDialog* dialog = new SendChangeAddressDialog(window);
if(!boost::get<CNoDestination>(&CoinControlDialog::coinControl->destChange)){
dialog->setAddress(QString::fromStdString(CBitcoinAddress(CoinControlDialog::coinControl->destChange).ToString()));
}
if(openDialogWithOpaqueBackgroundY(dialog, window, 3, 5)) {
if(dialog->selected) {
QString ret;
if (dialog->getAddress(walletModel, &ret)) {
CoinControlDialog::coinControl->destChange = CBitcoinAddress(ret.toStdString()).Get();
ui->btnChangeAddress->setActive(true);
}else{
inform(tr("Invalid change address"));
ui->btnChangeAddress->setActive(false);
}
}
}
dialog->deleteLater();
}
void SendWidget::onOpenUriClicked(){
showHideOp(true);
OpenURIDialog *dlg = new OpenURIDialog(window);
if (openDialogWithOpaqueBackgroundY(dlg, window, 3, 5)) {
SendCoinsRecipient rcp;
if (!GUIUtil::parseBitcoinURI(dlg->getURI(), &rcp)) {
inform(tr("Invalid URI"));
return;
}
if (!walletModel->validateAddress(rcp.address)) {
inform(tr("Invalid address in URI"));
return;
}
int listSize = entries.size();
if (listSize == 1) {
SendMultiRow *entry = entries[0];
entry->setAddressAndLabelOrDescription(rcp.address, rcp.message);
entry->setAmount(BitcoinUnits::format(nDisplayUnit, rcp.amount, false));
} else {
// Use the last one if it's invalid or add a new one
SendMultiRow *entry = entries[listSize - 1];
if (!entry->validate()) {
addEntry();
entry = entries[listSize];
}
entry->setAddressAndLabelOrDescription(rcp.address, rcp.message);
entry->setAmount(BitcoinUnits::format(nDisplayUnit, rcp.amount, false));
}
emit receivedURI(dlg->getURI());
}
dlg->deleteLater();
}
void SendWidget::onChangeCustomFeeClicked(){
showHideOp(true);
if (!customFeeDialog) {
customFeeDialog = new SendCustomFeeDialog(window);
customFeeDialog->setWalletModel(walletModel);
}
if (openDialogWithOpaqueBackgroundY(customFeeDialog, window, 3, 5)){
ui->pushButtonFee->setText(tr("Custom Fee %1").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, customFeeDialog->getFeeRate().GetFeePerK()) + "/kB"));
isCustomFeeSelected = true;
walletModel->setWalletDefaultFee(customFeeDialog->getFeeRate().GetFeePerK());
} else {
ui->pushButtonFee->setText(tr("Customize Fee"));
isCustomFeeSelected = false;
walletModel->setWalletDefaultFee();
}
}
void SendWidget::onCoinControlClicked(){
if(isPIV){
if (walletModel->getBalance() > 0) {
if (!coinControlDialog) {
coinControlDialog = new CoinControlDialog();
coinControlDialog->setModel(walletModel);
} else {
coinControlDialog->refreshDialog();
}
coinControlDialog->exec();
ui->btnCoinControl->setActive(CoinControlDialog::coinControl->HasSelected());
refreshAmounts();
} else {
inform(tr("You don't have any PIV to select."));
}
}else{
if (walletModel->getZerocoinBalance() > 0) {
ZPivControlDialog *zPivControl = new ZPivControlDialog(this);
zPivControl->setModel(walletModel);
zPivControl->exec();
ui->btnCoinControl->setActive(!ZPivControlDialog::setSelectedMints.empty());
zPivControl->deleteLater();
} else {
inform(tr("You don't have any zPIV in your balance to select."));
}
}
}
void SendWidget::onValueChanged() {
refreshAmounts();
}
void SendWidget::onPIVSelected(bool _isPIV){
isPIV = _isPIV;
setCssProperty(coinIcon, _isPIV ? "coin-icon-piv" : "coin-icon-zpiv");
refreshView();
updateStyle(coinIcon);
}
void SendWidget::onContactsClicked(SendMultiRow* entry){
focusedEntry = entry;
if(menu && menu->isVisible()){
menu->hide();
}
int contactsSize = walletModel->getAddressTableModel()->sizeSend();
if(contactsSize == 0) {
inform(tr("No contacts available, you can go to the contacts screen and add some there!"));
return;
}
int height = (contactsSize <= 2) ? entry->getEditHeight() * ( 2 * (contactsSize + 1 )) : entry->getEditHeight() * 4;
int width = entry->getEditWidth();
if(!menuContacts){
menuContacts = new ContactsDropdown(
width,
height,
this
);
menuContacts->setWalletModel(walletModel, AddressTableModel::Send);
connect(menuContacts, &ContactsDropdown::contactSelected, [this](QString address, QString label){
if(focusedEntry){
focusedEntry->setLabel(label);
focusedEntry->setAddress(address);
}
});
}
if(menuContacts->isVisible()){
menuContacts->hide();
return;
}
menuContacts->resizeList(width, height);
menuContacts->setStyleSheet(this->styleSheet());
menuContacts->adjustSize();
QPoint pos;
if (entries.size() > 1){
pos = entry->pos();
pos.setY((pos.y() + (focusedEntry->getEditHeight() - 12) * 4));
} else {
pos = focusedEntry->getEditLineRect().bottomLeft();
pos.setY((pos.y() + (focusedEntry->getEditHeight() - 12) * 3));
}
pos.setX(pos.x() + 20);
menuContacts->move(pos);
menuContacts->show();
}
void SendWidget::onMenuClicked(SendMultiRow* entry){
focusedEntry = entry;
if(menuContacts && menuContacts->isVisible()){
menuContacts->hide();
}
QPoint pos = entry->pos();
pos.setX(pos.x() + (entry->width() - entry->getMenuBtnWidth()));
pos.setY(pos.y() + entry->height() + (entry->getMenuBtnWidth()));
if(!this->menu){
this->menu = new TooltipMenu(window, this);
this->menu->setCopyBtnVisible(false);
this->menu->setEditBtnText(tr("Save contact"));
this->menu->setMinimumSize(this->menu->width() + 30,this->menu->height());
connect(this->menu, &TooltipMenu::message, this, &AddressesWidget::message);
connect(this->menu, SIGNAL(onEditClicked()), this, SLOT(onContactMultiClicked()));
connect(this->menu, SIGNAL(onDeleteClicked()), this, SLOT(onDeleteClicked()));
}else {
this->menu->hide();
}
menu->move(pos);
menu->show();
}
void SendWidget::onContactMultiClicked(){
if(focusedEntry) {
QString address = focusedEntry->getAddress();
if (address.isEmpty()) {
inform(tr("Address field is empty"));
return;
}
if (!walletModel->validateAddress(address)) {
inform(tr("Invalid address"));
return;
}
CBitcoinAddress pivAdd = CBitcoinAddress(address.toStdString());
if (walletModel->isMine(pivAdd)) {
inform(tr("Cannot store your own address as contact"));
return;
}
showHideOp(true);
AddNewContactDialog *dialog = new AddNewContactDialog(window);
QString label = walletModel->getAddressTableModel()->labelForAddress(address);
if (!label.isNull()){
dialog->setTexts(tr("Update Contact"), "Edit label for the selected address:\n%1");
dialog->setData(address, label);
} else {
dialog->setTexts(tr("Create New Contact"), "Save label for the selected address:\n%1");
dialog->setData(address, "");
}
openDialogWithOpaqueBackgroundY(dialog, window, 3, 5);
if (dialog->res) {
if (label == dialog->getLabel()) {
return;
}
if (walletModel->updateAddressBookLabels(pivAdd.Get(), dialog->getLabel().toStdString(),
AddressBook::AddressBookPurpose::SEND)) {
inform(tr("New Contact Stored"));
} else {
inform(tr("Error Storing Contact"));
}
}
dialog->deleteLater();
}
}
void SendWidget::onDeleteClicked(){
if (focusedEntry) {
focusedEntry->hide();
focusedEntry->deleteLater();
int entryNumber = focusedEntry->getNumber();
// remove selected entry and update row number for the others
QMutableListIterator<SendMultiRow*> it(entries);
while (it.hasNext()) {
SendMultiRow* entry = it.next();
if (focusedEntry == entry){
it.remove();
} else if (focusedEntry && entry->getNumber() > entryNumber){
entry->setNumber(entry->getNumber() - 1);
}
}
if (entries.size() == 1) {
SendMultiRow* sendMultiRow = QMutableListIterator<SendMultiRow*>(entries).next();
sendMultiRow->setNumber(entries.length());
sendMultiRow->showLabels();
}
focusedEntry = nullptr;
// Update total amounts
refreshAmounts();
}
}
void SendWidget::resizeMenu(){
if(menuContacts && menuContacts->isVisible() && focusedEntry){
int width = focusedEntry->getEditWidth();
menuContacts->resizeList(width, menuContacts->height());
menuContacts->resize(width, menuContacts->height());
QPoint pos = focusedEntry->getEditLineRect().bottomLeft();
pos.setX(pos.x() + 20);
pos.setY(pos.y() + ((focusedEntry->getEditHeight() - 12) * 3));
menuContacts->move(pos);
}
}
void SendWidget::changeTheme(bool isLightTheme, QString& theme){
if (coinControlDialog) coinControlDialog->setStyleSheet(theme);
}
SendWidget::~SendWidget(){
delete ui;
}