sendcoinsentry.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "sendcoinsentry.h"
  3. #include "ui_sendcoinsentry.h"
  4. #include "guiutil.h"
  5. #include "ecoinunits.h"
  6. #include "addressbookpage.h"
  7. #include "walletmodel.h"
  8. #include "optionsmodel.h"
  9. #include "addresstablemodel.h"
  10. #include <QApplication>
  11. #include <QClipboard>
  12. SendCoinsEntry::SendCoinsEntry(QWidget *parent) :
  13. QFrame(parent),
  14. ui(new Ui::SendCoinsEntry),
  15. model(0)
  16. {
  17. ui->setupUi(this);
  18. #ifdef Q_OS_MAC
  19. ui->payToLayout->setSpacing(4);
  20. #endif
  21. #if QT_VERSION >= 0x040700
  22. /* Do not move this to the XML file, Qt before 4.7 will choke on it */
  23. ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
  24. ui->payTo->setPlaceholderText(tr("Enter a Ecoin address (e.g. EJiA1K71didR1ovdVUtse1AJVWye2V1jeV)"));
  25. #endif
  26. setFocusPolicy(Qt::TabFocus);
  27. setFocusProxy(ui->payTo);
  28. GUIUtil::setupAddressWidget(ui->payTo, this);
  29. }
  30. SendCoinsEntry::~SendCoinsEntry()
  31. {
  32. delete ui;
  33. }
  34. void SendCoinsEntry::on_pasteButton_clicked()
  35. {
  36. // Paste text from clipboard into recipient field
  37. ui->payTo->setText(QApplication::clipboard()->text());
  38. }
  39. void SendCoinsEntry::on_addressBookButton_clicked()
  40. {
  41. if(!model)
  42. return;
  43. AddressBookPage dlg(AddressBookPage::ForSending, AddressBookPage::SendingTab, this);
  44. dlg.setModel(model->getAddressTableModel());
  45. if(dlg.exec())
  46. {
  47. ui->payTo->setText(dlg.getReturnValue());
  48. ui->payAmount->setFocus();
  49. }
  50. }
  51. void SendCoinsEntry::on_payTo_textChanged(const QString &address)
  52. {
  53. if(!model)
  54. return;
  55. // Fill in label from address book, if address has an associated label
  56. QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
  57. if(!associatedLabel.isEmpty())
  58. ui->addAsLabel->setText(associatedLabel);
  59. }
  60. void SendCoinsEntry::setModel(WalletModel *model)
  61. {
  62. this->model = model;
  63. if(model && model->getOptionsModel())
  64. connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  65. connect(ui->payAmount, SIGNAL(textChanged()), this, SIGNAL(payAmountChanged()));
  66. clear();
  67. }
  68. void SendCoinsEntry::setRemoveEnabled(bool enabled)
  69. {
  70. ui->deleteButton->setEnabled(enabled);
  71. }
  72. void SendCoinsEntry::clear()
  73. {
  74. ui->payTo->clear();
  75. ui->addAsLabel->clear();
  76. ui->payAmount->clear();
  77. ui->payTo->setFocus();
  78. // update the display unit, to not use the default ("ECO")
  79. updateDisplayUnit();
  80. }
  81. void SendCoinsEntry::on_deleteButton_clicked()
  82. {
  83. emit removeEntry(this);
  84. }
  85. bool SendCoinsEntry::validate()
  86. {
  87. // Check input validity
  88. bool retval = true;
  89. if(!ui->payAmount->validate())
  90. {
  91. retval = false;
  92. }
  93. else
  94. {
  95. if(ui->payAmount->value() <= 0)
  96. {
  97. // Cannot send 0 coins or less
  98. ui->payAmount->setValid(false);
  99. retval = false;
  100. }
  101. }
  102. if(!ui->payTo->hasAcceptableInput() ||
  103. (model && !model->validateAddress(ui->payTo->text())))
  104. {
  105. ui->payTo->setValid(false);
  106. retval = false;
  107. }
  108. return retval;
  109. }
  110. SendCoinsRecipient SendCoinsEntry::getValue()
  111. {
  112. SendCoinsRecipient rv;
  113. rv.address = ui->payTo->text();
  114. rv.label = ui->addAsLabel->text();
  115. rv.amount = ui->payAmount->value();
  116. return rv;
  117. }
  118. QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
  119. {
  120. QWidget::setTabOrder(prev, ui->payTo);
  121. QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
  122. QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
  123. QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
  124. QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
  125. return ui->payAmount->setupTabChain(ui->addAsLabel);
  126. }
  127. void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
  128. {
  129. ui->payTo->setText(value.address);
  130. ui->addAsLabel->setText(value.label);
  131. ui->payAmount->setValue(value.amount);
  132. }
  133. bool SendCoinsEntry::isClear()
  134. {
  135. return ui->payTo->text().isEmpty();
  136. }
  137. void SendCoinsEntry::setFocus()
  138. {
  139. ui->payTo->setFocus();
  140. }
  141. void SendCoinsEntry::updateDisplayUnit()
  142. {
  143. if(model && model->getOptionsModel())
  144. {
  145. // Update payAmount with the current unit
  146. ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
  147. }
  148. }