Dash Core  0.12.2.1
P2P Digital Currency
sendcoinsentry.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2017 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "sendcoinsentry.h"
7 #include "ui_sendcoinsentry.h"
8 
9 #include "addressbookpage.h"
10 #include "addresstablemodel.h"
11 #include "guiutil.h"
12 #include "optionsmodel.h"
13 #include "platformstyle.h"
14 #include "walletmodel.h"
15 
16 #include <QApplication>
17 #include <QClipboard>
18 
19 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent) :
20  QStackedWidget(parent),
21  ui(new Ui::SendCoinsEntry),
22  model(0),
23  platformStyle(platformStyle)
24 {
25  ui->setupUi(this);
26 
27  setCurrentWidget(ui->SendCoins);
28 
30  ui->payToLayout->setSpacing(4);
31 #if QT_VERSION >= 0x040700
32  ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
33 #endif
34 
35  QString theme = GUIUtil::getThemeName();
36 
37  // These icons are needed on Mac also!
38  ui->addressBookButton->setIcon(QIcon(":/icons/" + theme + "/address-book"));
39  ui->pasteButton->setIcon(QIcon(":/icons/" + theme + "/editpaste"));
40  ui->deleteButton->setIcon(QIcon(":/icons/" + theme + "/remove"));
41  ui->deleteButton_is->setIcon(QIcon(":/icons/" + theme + "/remove"));
42  ui->deleteButton_s->setIcon(QIcon(":/icons/" + theme + "/remove"));
43 
44  // normal dash address field
46  // just a label for displaying dash address(es)
47  ui->payTo_is->setFont(GUIUtil::fixedPitchFont());
48 
49  // Connect signals
50  connect(ui->payAmount, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
51  connect(ui->checkboxSubtractFeeFromAmount, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
52  connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
53  connect(ui->deleteButton_is, SIGNAL(clicked()), this, SLOT(deleteClicked()));
54  connect(ui->deleteButton_s, SIGNAL(clicked()), this, SLOT(deleteClicked()));
55 }
56 
58 {
59  delete ui;
60 }
61 
63 {
64  // Paste text from clipboard into recipient field
65  ui->payTo->setText(QApplication::clipboard()->text());
66 }
67 
69 {
70  if(!model)
71  return;
74  if(dlg.exec())
75  {
76  ui->payTo->setText(dlg.getReturnValue());
77  ui->payAmount->setFocus();
78  }
79 }
80 
81 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
82 {
83  updateLabel(address);
84 }
85 
87 {
88  this->model = model;
89 
90  if (model && model->getOptionsModel())
91  connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
92 
93  clear();
94 }
95 
97 {
98  // clear UI elements for normal payment
99  ui->payTo->clear();
100  ui->addAsLabel->clear();
101  ui->payAmount->clear();
102  ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
103  ui->messageTextLabel->clear();
104  ui->messageTextLabel->hide();
105  ui->messageLabel->hide();
106  // clear UI elements for unauthenticated payment request
107  ui->payTo_is->clear();
108  ui->memoTextLabel_is->clear();
109  ui->payAmount_is->clear();
110  // clear UI elements for authenticated payment request
111  ui->payTo_s->clear();
112  ui->memoTextLabel_s->clear();
113  ui->payAmount_s->clear();
114 
115  // update the display unit, to not use the default ("BTC")
117 }
118 
120 {
121  Q_EMIT removeEntry(this);
122 }
123 
125 {
126  if (!model)
127  return false;
128 
129  // Check input validity
130  bool retval = true;
131 
132  // Skip checks for payment request
134  return retval;
135 
136  if (!model->validateAddress(ui->payTo->text()))
137  {
138  ui->payTo->setValid(false);
139  retval = false;
140  }
141 
142  if (!ui->payAmount->validate())
143  {
144  retval = false;
145  }
146 
147  // Sending a zero amount is invalid
148  if (ui->payAmount->value(0) <= 0)
149  {
150  ui->payAmount->setValid(false);
151  retval = false;
152  }
153 
154  // Reject dust outputs:
155  if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
156  ui->payAmount->setValid(false);
157  retval = false;
158  }
159 
160  return retval;
161 }
162 
164 {
165  // Payment request
167  return recipient;
168 
169  // Normal payment
170  recipient.address = ui->payTo->text();
171  recipient.label = ui->addAsLabel->text();
174  recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
175 
176  return recipient;
177 }
178 
179 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
180 {
181  QWidget::setTabOrder(prev, ui->payTo);
182  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
183  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
184  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
185  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
186  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
187  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
188  return ui->deleteButton;
189 }
190 
192 {
193  recipient = value;
194 
195  if (recipient.paymentRequest.IsInitialized()) // payment request
196  {
197  if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
198  {
199  ui->payTo_is->setText(recipient.address);
202  ui->payAmount_is->setReadOnly(true);
203  setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
204  }
205  else // authenticated
206  {
208  ui->memoTextLabel_s->setText(recipient.message);
210  ui->payAmount_s->setReadOnly(true);
211  setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
212  }
213  }
214  else // normal payment
215  {
216  // message
218  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
219  ui->messageLabel->setVisible(!recipient.message.isEmpty());
220 
221  ui->addAsLabel->clear();
222  ui->payTo->setText(recipient.address); // this may set a label from addressbook
223  if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
224  ui->addAsLabel->setText(recipient.label);
226  }
227 }
228 
229 void SendCoinsEntry::setAddress(const QString &address)
230 {
231  ui->payTo->setText(address);
232  ui->payAmount->setFocus();
233 }
234 
236 {
237  return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
238 }
239 
241 {
242  ui->payTo->setFocus();
243 }
244 
246 {
247  if(model && model->getOptionsModel())
248  {
249  // Update payAmount with the current unit
253  }
254 }
255 
256 bool SendCoinsEntry::updateLabel(const QString &address)
257 {
258  if(!model)
259  return false;
260 
261  // Fill in label from address book, if address has an associated label
262  QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
263  if(!associatedLabel.isEmpty())
264  {
265  ui->addAsLabel->setText(associatedLabel);
266  return true;
267  }
268 
269  return false;
270 }
void subtractFeeFromAmountChanged()
void removeEntry(SendCoinsEntry *entry)
QValidatedLineEdit * payTo
SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent=0)
QToolButton * addressBookButton
QToolButton * deleteButton
void setupUi(QStackedWidget *SendCoinsEntry)
QHBoxLayout * payToLayout
BitcoinAmountField * payAmount_is
BitcoinAmountField * payAmount_s
QCheckBox * checkboxSubtractFeeFromAmount
bool fSubtractFeeFromAmount
Definition: walletmodel.h:63
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:112
bool updateLabel(const QString &address)
void setValue(const SendCoinsRecipient &value)
SendCoinsRecipient getValue()
void setDisplayUnit(int unit)
QToolButton * deleteButton_s
WalletModel * model
QFrame * SendCoins_AuthenticatedPaymentRequest
QFrame * SendCoins_UnauthenticatedPaymentRequest
QString labelForAddress(const QString &address) const
void setAddress(const QString &address)
SendCoinsRecipient recipient
bool isDust(const QString &address, const CAmount &amount)
Definition: guiutil.cpp:253
OptionsModel * getOptionsModel()
void setValid(bool valid)
QToolButton * deleteButton_is
QLineEdit * addAsLabel
QToolButton * pasteButton
QWidget * setupTabChain(QWidget *prev)
bool validateAddress(const QString &address)
void on_pasteButton_clicked()
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:59
void on_addressBookButton_clicked()
const QString & getReturnValue() const
void setValid(bool valid)
Ui::SendCoinsEntry * ui
bool getUseExtraSpacing() const
Definition: platformstyle.h:22
void setModel(WalletModel *model)
QString getThemeName()
Definition: guiutil.cpp:902
QString authenticatedMerchant
Definition: walletmodel.h:61
const PlatformStyle * platformStyle
void on_payTo_textChanged(const QString &address)
BitcoinAmountField * payAmount
int getDisplayUnit()
Definition: optionsmodel.h:73
QWidget * setupTabChain(QWidget *prev)
void setValue(const CAmount &value)
AddressTableModel * getAddressTableModel()
void setReadOnly(bool fReadOnly)
void setModel(AddressTableModel *model)
QFont fixedPitchFont()
Definition: guiutil.cpp:97