You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sendcoinsentry.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "sendcoinsentry.h"
  5. #include "ui_sendcoinsentry.h"
  6. #include "addressbookpage.h"
  7. #include "addresstablemodel.h"
  8. #include "guiutil.h"
  9. #include "optionsmodel.h"
  10. #include "platformstyle.h"
  11. #include "walletmodel.h"
  12. #include <QApplication>
  13. #include <QClipboard>
  14. SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
  15. QStackedWidget(parent),
  16. ui(new Ui::SendCoinsEntry),
  17. model(0),
  18. platformStyle(_platformStyle)
  19. {
  20. ui->setupUi(this);
  21. ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
  22. ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
  23. ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
  24. ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
  25. ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
  26. setCurrentWidget(ui->SendCoins);
  27. if (platformStyle->getUseExtraSpacing())
  28. ui->payToLayout->setSpacing(4);
  29. #if QT_VERSION >= 0x040700
  30. ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
  31. #endif
  32. // normal starwels address field
  33. GUIUtil::setupAddressWidget(ui->payTo, this);
  34. // just a label for displaying starwels address(es)
  35. ui->payTo_is->setFont(GUIUtil::fixedPitchFont());
  36. // Connect signals
  37. connect(ui->payAmount, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
  38. connect(ui->checkboxSubtractFeeFromAmount, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
  39. connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
  40. connect(ui->deleteButton_is, SIGNAL(clicked()), this, SLOT(deleteClicked()));
  41. connect(ui->deleteButton_s, SIGNAL(clicked()), this, SLOT(deleteClicked()));
  42. }
  43. SendCoinsEntry::~SendCoinsEntry()
  44. {
  45. delete ui;
  46. }
  47. void SendCoinsEntry::on_pasteButton_clicked()
  48. {
  49. // Paste text from clipboard into recipient field
  50. ui->payTo->setText(QApplication::clipboard()->text());
  51. }
  52. void SendCoinsEntry::on_addressBookButton_clicked()
  53. {
  54. if(!model)
  55. return;
  56. AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
  57. dlg.setModel(model->getAddressTableModel());
  58. if(dlg.exec())
  59. {
  60. ui->payTo->setText(dlg.getReturnValue());
  61. ui->payAmount->setFocus();
  62. }
  63. }
  64. void SendCoinsEntry::on_payTo_textChanged(const QString &address)
  65. {
  66. updateLabel(address);
  67. }
  68. void SendCoinsEntry::setModel(WalletModel *_model)
  69. {
  70. this->model = _model;
  71. if (_model && _model->getOptionsModel())
  72. connect(_model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  73. clear();
  74. }
  75. void SendCoinsEntry::clear()
  76. {
  77. // clear UI elements for normal payment
  78. ui->payTo->clear();
  79. ui->addAsLabel->clear();
  80. ui->payAmount->clear();
  81. ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
  82. ui->messageTextLabel->clear();
  83. ui->messageTextLabel->hide();
  84. ui->messageLabel->hide();
  85. // clear UI elements for unauthenticated payment request
  86. ui->payTo_is->clear();
  87. ui->memoTextLabel_is->clear();
  88. ui->payAmount_is->clear();
  89. // clear UI elements for authenticated payment request
  90. ui->payTo_s->clear();
  91. ui->memoTextLabel_s->clear();
  92. ui->payAmount_s->clear();
  93. // update the display unit, to not use the default ("MAI")
  94. updateDisplayUnit();
  95. }
  96. void SendCoinsEntry::deleteClicked()
  97. {
  98. Q_EMIT removeEntry(this);
  99. }
  100. bool SendCoinsEntry::validate()
  101. {
  102. if (!model)
  103. return false;
  104. // Check input validity
  105. bool retval = true;
  106. // Skip checks for payment request
  107. if (recipient.paymentRequest.IsInitialized())
  108. return retval;
  109. if (!model->validateAddress(ui->payTo->text()))
  110. {
  111. ui->payTo->setValid(false);
  112. retval = false;
  113. }
  114. if (!ui->payAmount->validate())
  115. {
  116. retval = false;
  117. }
  118. // Sending a zero amount is invalid
  119. if (ui->payAmount->value(0) <= 0)
  120. {
  121. ui->payAmount->setValid(false);
  122. retval = false;
  123. }
  124. // Reject dust outputs:
  125. if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
  126. ui->payAmount->setValid(false);
  127. retval = false;
  128. }
  129. return retval;
  130. }
  131. SendCoinsRecipient SendCoinsEntry::getValue()
  132. {
  133. // Payment request
  134. if (recipient.paymentRequest.IsInitialized())
  135. return recipient;
  136. // Normal payment
  137. recipient.address = ui->payTo->text();
  138. recipient.label = ui->addAsLabel->text();
  139. recipient.amount = ui->payAmount->value();
  140. recipient.message = ui->messageTextLabel->text();
  141. recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
  142. return recipient;
  143. }
  144. QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
  145. {
  146. QWidget::setTabOrder(prev, ui->payTo);
  147. QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
  148. QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
  149. QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
  150. QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
  151. QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
  152. QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
  153. return ui->deleteButton;
  154. }
  155. void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
  156. {
  157. recipient = value;
  158. if (recipient.paymentRequest.IsInitialized()) // payment request
  159. {
  160. if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
  161. {
  162. ui->payTo_is->setText(recipient.address);
  163. ui->memoTextLabel_is->setText(recipient.message);
  164. ui->payAmount_is->setValue(recipient.amount);
  165. ui->payAmount_is->setReadOnly(true);
  166. setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
  167. }
  168. else // authenticated
  169. {
  170. ui->payTo_s->setText(recipient.authenticatedMerchant);
  171. ui->memoTextLabel_s->setText(recipient.message);
  172. ui->payAmount_s->setValue(recipient.amount);
  173. ui->payAmount_s->setReadOnly(true);
  174. setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
  175. }
  176. }
  177. else // normal payment
  178. {
  179. // message
  180. ui->messageTextLabel->setText(recipient.message);
  181. ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
  182. ui->messageLabel->setVisible(!recipient.message.isEmpty());
  183. ui->addAsLabel->clear();
  184. ui->payTo->setText(recipient.address); // this may set a label from addressbook
  185. if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
  186. ui->addAsLabel->setText(recipient.label);
  187. ui->payAmount->setValue(recipient.amount);
  188. }
  189. }
  190. void SendCoinsEntry::setAddress(const QString &address)
  191. {
  192. ui->payTo->setText(address);
  193. ui->payAmount->setFocus();
  194. }
  195. bool SendCoinsEntry::isClear()
  196. {
  197. return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
  198. }
  199. void SendCoinsEntry::setFocus()
  200. {
  201. ui->payTo->setFocus();
  202. }
  203. void SendCoinsEntry::updateDisplayUnit()
  204. {
  205. if(model && model->getOptionsModel())
  206. {
  207. // Update payAmount with the current unit
  208. ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
  209. ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
  210. ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
  211. }
  212. }
  213. bool SendCoinsEntry::updateLabel(const QString &address)
  214. {
  215. if(!model)
  216. return false;
  217. // Fill in label from address book, if address has an associated label
  218. QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
  219. if(!associatedLabel.isEmpty())
  220. {
  221. ui->addAsLabel->setText(associatedLabel);
  222. return true;
  223. }
  224. return false;
  225. }