Raven Core  3.0.0
P2P Digital Currency
sendcoinsentry.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven 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 #include "guiconstants.h"
16 #include "darkstyle.h"
17 
18 #include <QGraphicsDropShadowEffect>
19 #include <QApplication>
20 #include <QClipboard>
21 
22 SendCoinsEntry::SendCoinsEntry(const PlatformStyle *_platformStyle, QWidget *parent) :
23  QStackedWidget(parent),
24  ui(new Ui::SendCoinsEntry),
25  model(0),
26  platformStyle(_platformStyle)
27 {
28  ui->setupUi(this);
29 
30  ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
31  ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
32  ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
33  ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
34  ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
35 
36  setCurrentWidget(ui->SendCoins);
37 
39  ui->payToLayout->setSpacing(4);
40 #if QT_VERSION >= 0x040700
41  ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
42 #endif
43 
44  // normal raven address field
45  GUIUtil::setupAddressWidget(ui->payTo, this);
46  // just a label for displaying raven address(es)
47  ui->payTo_is->setFont(GUIUtil::getSubLabelFont());
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  this->setStyleSheet(QString(".SendCoinsEntry {background-color: %1; padding-top: 10px; padding-right: 30px; border: none;}").arg(platformStyle->SendEntriesBackGroundColor().name()));
57 
58  this->setGraphicsEffect(GUIUtil::getShadowEffect());
59 
60  ui->payToLabel->setStyleSheet(STRING_LABEL_COLOR);
61  ui->payToLabel->setFont(GUIUtil::getSubLabelFont());
62 
63  ui->labellLabel->setStyleSheet(STRING_LABEL_COLOR);
64  ui->labellLabel->setFont(GUIUtil::getSubLabelFont());
65 
66  ui->amountLabel->setStyleSheet(STRING_LABEL_COLOR);
67  ui->amountLabel->setFont(GUIUtil::getSubLabelFont());
68 
69  ui->messageLabel->setStyleSheet(STRING_LABEL_COLOR);
70  ui->messageLabel->setFont(GUIUtil::getSubLabelFont());
71 
72  ui->checkboxSubtractFeeFromAmount->setStyleSheet(QString(".QCheckBox{ %1; }").arg(STRING_LABEL_COLOR));
73  ui->payTo->setFont(GUIUtil::getSubLabelFont());
74  ui->addAsLabel->setFont(GUIUtil::getSubLabelFont());
75  ui->payAmount->setFont(GUIUtil::getSubLabelFont());
76  ui->messageTextLabel->setFont(GUIUtil::getSubLabelFont());
77 }
78 
80 {
81  delete ui;
82 }
83 
85 {
86  // Paste text from clipboard into recipient field
87  ui->payTo->setText(QApplication::clipboard()->text());
88 }
89 
91 {
92  if(!model)
93  return;
96  if(dlg.exec())
97  {
98  ui->payTo->setText(dlg.getReturnValue());
99  ui->payAmount->setFocus();
100  }
101 }
102 
103 void SendCoinsEntry::on_payTo_textChanged(const QString &address)
104 {
105  updateLabel(address);
106 }
107 
109 {
110  this->model = _model;
111 
112  if (_model && _model->getOptionsModel())
113  connect(_model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
114 
115  clear();
116 }
117 
119 {
120  // clear UI elements for normal payment
121  ui->payTo->clear();
122  ui->addAsLabel->clear();
123  ui->payAmount->clear();
124  ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
125  ui->messageTextLabel->clear();
126  ui->messageTextLabel->hide();
127  ui->messageLabel->hide();
128  // clear UI elements for unauthenticated payment request
129  ui->payTo_is->clear();
130  ui->memoTextLabel_is->clear();
131  ui->payAmount_is->clear();
132  // clear UI elements for authenticated payment request
133  ui->payTo_s->clear();
134  ui->memoTextLabel_s->clear();
135  ui->payAmount_s->clear();
136 
137  // update the display unit, to not use the default ("RVN")
139 }
140 
142 {
143  Q_EMIT removeEntry(this);
144 }
145 
147 {
148  if (!model)
149  return false;
150 
151  // Check input validity
152  bool retval = true;
153 
154  // Skip checks for payment request
156  return retval;
157 
158  if (!model->validateAddress(ui->payTo->text()))
159  {
160  ui->payTo->setValid(false);
161  retval = false;
162  }
163 
164  if (!ui->payAmount->validate())
165  {
166  retval = false;
167  }
168 
169  // Sending a zero amount is invalid
170  if (ui->payAmount->value(0) <= 0)
171  {
172  ui->payAmount->setValid(false);
173  retval = false;
174  }
175 
176  // Reject dust outputs:
177  if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
178  ui->payAmount->setValid(false);
179  retval = false;
180  }
181 
182  return retval;
183 }
184 
186 {
187  // Payment request
189  return recipient;
190 
191  // Normal payment
192  recipient.address = ui->payTo->text();
193  recipient.label = ui->addAsLabel->text();
194  recipient.amount = ui->payAmount->value();
195  recipient.message = ui->messageTextLabel->text();
196  recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
197 
198  return recipient;
199 }
200 
201 QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
202 {
203  QWidget::setTabOrder(prev, ui->payTo);
204  QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
205  QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
206  QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
207  QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
208  QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
209  QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
210  return ui->deleteButton;
211 }
212 
214 {
215  recipient = value;
216 
217  if (recipient.paymentRequest.IsInitialized()) // payment request
218  {
219  if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
220  {
221  ui->payTo_is->setText(recipient.address);
222  ui->memoTextLabel_is->setText(recipient.message);
223  ui->payAmount_is->setValue(recipient.amount);
224  ui->payAmount_is->setReadOnly(true);
225  setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
226  }
227  else // authenticated
228  {
229  ui->payTo_s->setText(recipient.authenticatedMerchant);
230  ui->memoTextLabel_s->setText(recipient.message);
231  ui->payAmount_s->setValue(recipient.amount);
232  ui->payAmount_s->setReadOnly(true);
233  setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
234  }
235  }
236  else // normal payment
237  {
238  // message
239  ui->messageTextLabel->setText(recipient.message);
240  ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
241  ui->messageLabel->setVisible(!recipient.message.isEmpty());
242 
243  ui->addAsLabel->clear();
244  ui->payTo->setText(recipient.address); // this may set a label from addressbook
245  if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
246  ui->addAsLabel->setText(recipient.label);
247  ui->payAmount->setValue(recipient.amount);
248  }
249 }
250 
251 void SendCoinsEntry::setAddress(const QString &address)
252 {
253  ui->payTo->setText(address);
254  ui->payAmount->setFocus();
255 }
256 
258 {
259  return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
260 }
261 
263 {
264  ui->payTo->setFocus();
265 }
266 
268 {
269  if(model && model->getOptionsModel())
270  {
271  // Update payAmount with the current unit
272  ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
273  ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
274  ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
275  }
276 }
277 
278 bool SendCoinsEntry::updateLabel(const QString &address)
279 {
280  if(!model)
281  return false;
282 
283  // Fill in label from address book, if address has an associated label
284  QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
285  if(!associatedLabel.isEmpty())
286  {
287  ui->addAsLabel->setText(associatedLabel);
288  return true;
289  }
290 
291  return false;
292 }
bool isDust(const QString &address, const CAmount &amount)
Definition: guiutil.cpp:328
Ui::SendCoinsEntry * ui
void setValue(const SendCoinsRecipient &value)
void payAmountChanged()
PaymentRequestPlus paymentRequest
Definition: walletmodel.h:60
QGraphicsDropShadowEffect * getShadowEffect()
Definition: guiutil.cpp:146
void setFocus()
SendCoinsRecipient getValue()
void setModel(AddressTableModel *model)
bool getUseExtraSpacing() const
Definition: platformstyle.h:25
void setAddress(const QString &address)
~SendCoinsEntry()
bool updateLabel(const QString &address)
void deleteClicked()
void on_payTo_textChanged(const QString &address)
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Open address book to pick address.
AddressTableModel * getAddressTableModel()
void updateDisplayUnit()
bool validate()
A single entry in the dialog for sending ravens.
int getDisplayUnit() const
Definition: optionsmodel.h:68
QWidget * setupTabChain(QWidget *prev)
Set up the tab chain manually, as Qt messes up the tab chain by default in some cases (issue https://...
QFont getSubLabelFont()
Definition: guiutil.cpp:86
void clear()
void setupAddressWidget(QValidatedLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:201
#define STRING_LABEL_COLOR
Definition: guiconstants.h:90
QString labelForAddress(const QString &address) const
Widget that shows a list of sending or receiving addresses.
void removeEntry(SendCoinsEntry *entry)
bool isClear()
Return whether the entry is still empty and unedited.
bool validateAddress(const QString &address)
void subtractFeeFromAmountChanged()
void on_pasteButton_clicked()
WalletModel * model
Interface to Raven wallet from Qt view code.
Definition: walletmodel.h:165
SendCoinsRecipient recipient
void on_addressBookButton_clicked()
void setModel(WalletModel *model)
QColor SendEntriesBackGroundColor() const
SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent=0)
bool fSubtractFeeFromAmount
Definition: walletmodel.h:64
const PlatformStyle * platformStyle
QString authenticatedMerchant
Definition: walletmodel.h:62
OptionsModel * getOptionsModel()
const QString & getReturnValue() const