PRCYCoin  2.0.0.7rc1
P2P Digital Currency
receivecoinsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2014 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "receivecoinsdialog.h"
6 #include "ui_receivecoinsdialog.h"
7 
8 #include "addressbookpage.h"
9 #include "addresstablemodel.h"
10 #include "bitcoinunits.h"
11 #include "guiutil.h"
12 #include "optionsmodel.h"
13 #include "receiverequestdialog.h"
14 #include "walletmodel.h"
15 
16 #include <QAction>
17 #include <QClipboard>
18 #include <QCursor>
19 #include <QItemSelection>
20 #include <QMessageBox>
21 #include <QScrollBar>
22 #include <QTextDocument>
23 #include <QStylePainter>
24 #include <QDesktopWidget>
25 #include <QInputDialog>
26 
27 ReceiveCoinsDialog::ReceiveCoinsDialog(QWidget* parent) : QDialog(parent, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint),
28  ui(new Ui::ReceiveCoinsDialog),
29  // m_SizeGrip(this),
30  model(0)
31 {
32  ui->setupUi(this);
33 
34 
35  // context menu actions
36  QAction* copyLabelAction = new QAction(tr("Copy label"), this);
37  QAction* copyMessageAction = new QAction(tr("Copy message"), this);
38  QAction* copyAmountAction = new QAction(tr("Copy amount"), this);
39  QAction* copyAddressAction = new QAction(tr("Copy address"), this);
40 
41  // context menu
42  contextMenu = new QMenu(this);
43  contextMenu->setAttribute(Qt::WA_DeleteOnClose);
44  contextMenu->addAction(copyLabelAction);
45  contextMenu->addAction(copyMessageAction);
46  contextMenu->addAction(copyAmountAction);
47  contextMenu->addAction(copyAddressAction);
48 
49  // Show privacy account address
50  ui->lineEditAddress->setStyleSheet("border:none; background: transparent; text-align:center;");
51  ui->pushButtonCP->setStyleSheet("background:transparent;");
52  ui->pushButtonCP->setIcon(QIcon(":/icons/editcopy"));
53  ui->pushButtonGenerate->setIcon(QIcon(":/icons/add"));
54  ui->pushButtonGenerate->setStyleSheet("background:transparent;");
55  ui->pushButtonRemove->setIcon(QIcon(":/icons/remove"));
56  ui->pushButtonRemove->setStyleSheet("background:transparent;");
57  connect(ui->pushButtonCP, SIGNAL(clicked()), this, SLOT(copyAddress()));
58  connect(ui->pushButtonGenerate, SIGNAL(clicked()), this, SLOT(generateAddress()));
59  connect(ui->pushButtonRemove, SIGNAL(clicked()), this, SLOT(removeAddress()));
60  connect(ui->reqAddress, SIGNAL(currentIndexChanged(int)), this, SLOT(changeAddress(int)));
61 
62  //Create privacy account (wallet is unlocked first launch so !pwalletMain->IsLocked() works here)
63  if (pwalletMain && !pwalletMain->IsLocked()) {
64  CPubKey temp;
67  }
68 
69  QLocale lo(QLocale::C);
70  lo.setNumberOptions(QLocale::RejectGroupSeparator);
71  QDoubleValidator *dblVal = new QDoubleValidator(0, MAX_MONEY_OUT, 8, ui->reqAmount);
72  dblVal->setNotation(QDoubleValidator::StandardNotation);
73  dblVal->setLocale(lo);
74  ui->reqAmount->setValidator(dblVal);
75 }
76 
77 static inline int64_t roundint64(double d)
78 {
79  return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
80 }
81 
83  double dAmount = ui->reqAmount->text().toDouble();
84  CAmount nAmount = roundint64(dAmount * COIN);
85  return nAmount;
86 }
87 
89 {
90  this->model = model;
91 
92  if (model && model->getOptionsModel()) {
93  connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
95  loadAccount();
96  }
97 }
98 
100 {
101  QRect rec = QApplication::desktop()->availableGeometry();
102  int screenWidth = rec.width();
103  QString addr;
104  std::string address;
105  pwalletMain->ComputeStealthPublicAddress("masteraccount", address);
106 
107  if (screenWidth <= 1280) {
108  //(truncated for screen with less availableGeometry than 1280px)
109  addr = "Master Account - " + QString(address.substr(0, 30).c_str()) + "..." + QString(address.substr(address.length() - 30, 30).c_str());
110  } else {
111  addr = "Master Account - " + QString(address.c_str());
112  }
113  ui->reqAddress->addItem(addr);
114 
115  //Set lineEditAddress to Master Account address for copy to clipboard
116  if (screenWidth <= 1024) {
117  //(truncated for screen with less availableGeometry than 1024px)
118  ui->lineEditAddress->setText(QString(address.substr(0, 30).c_str()) + "..." + QString(address.substr(address.length() - 30, 30).c_str()));
119  } else {
120  ui->lineEditAddress->setText(QString(address.c_str()));
121  }
122 
123 }
124 
125 /*void ReceiveCoinsDialog::loadAccount() {
126  QRect rec = QApplication::desktop()->availableGeometry();
127  int screenWidth = rec.width();
128  QString addr;
129 
130  //Set reqAddress as the master stealth address
131  std::vector<std::string> addrList, accountList;
132  CWallet* wl = model->getCWallet();
133  QList<QString> stringsList;
134  wl->AllMyPublicAddresses(addrList, accountList);
135  for(size_t i = 0; i < addrList.size(); i++) {
136  if (accountList[i] == "masteraccount") continue;
137  bool isDuplicate = false;
138  if (screenWidth <= 1280) {
139  //(truncated for screen with less availableGeometry than 1280px)
140  addr = QString(accountList[i].c_str()) + " - " + QString(addrList[i].substr(0, 30).c_str()) + "..." + QString(addrList[i].substr(addrList[i].length() - 30, 30).c_str());
141  } else {
142  addr = QString(accountList[i].c_str()) + " - " + QString(addrList[i].c_str());
143  }
144  for (size_t i = 0; i < (size_t)ui->reqAddress->count(); i++) {
145  if (stringsList.contains(QString(addrList[i].substr(0, 30).c_str()) + "..." + QString(addrList[i].substr(addrList[i].length() - 30, 30).c_str()))) {
146  isDuplicate = true;
147  break;
148  }
149  }
150  if (!isDuplicate) {
151  stringsList.append(addr);
152  }
153  }
154  ui->reqAddress->addItems(stringsList);
155  //Set lineEditAddress to Master Account address for copy to clipboard
156  if (screenWidth <= 1024) {
157  //(truncated for screen with less availableGeometry than 1024px)
158  ui->lineEditAddress->setText(QString(addrList[0].substr(0, 30).c_str()) + "..." + QString(addrList[0].substr(addrList[0].length() - 30, 30).c_str()));
159  } else {
160  ui->lineEditAddress->setText(QString(addrList[0].c_str()));
161  }
162 }
163 */
164 
166 {
167  delete ui;
168 }
169 
171 {
173 }
174 
176 {
177  clear();
178 }
179 
181 {
182  clear();
183 }
184 
186 {
187 }
188 
190 {
191  double dAmount = ui->reqAmount->text().toDouble();
192  if (dAmount < 0.0 || dAmount > MAX_MONEY_OUT) {
193  QMessageBox msgBox;
194  msgBox.setWindowTitle("Invalid Amount");
195  msgBox.setText("Invalid amount entered. Please enter an amount less than 2.1B PRCY.");
196  msgBox.setStyleSheet(GUIUtil::loadStyleSheet());
197  msgBox.setIcon(QMessageBox::Warning);
198  msgBox.exec();
199  return;
200  }
202  return;
203 
204  QString str = ui->reqAddress->currentText();
205  QStringList list = str.split("-");
206 
207  QString address = list[1].trimmed();
208  QString label = list[0].trimmed();
209  QString reqMes = ui->reqID->text();
210 
211  SendCoinsRecipient info(address, label, getValidatedAmount(), reqMes);
212  ReceiveRequestDialog* dialog = new ReceiveRequestDialog(this);
213  dialog->setAttribute(Qt::WA_DeleteOnClose);
214  dialog->setModel(model->getOptionsModel());
215  dialog->setInfo(info);
216  dialog->show();
217  clear();
218 }
219 
220 // We override the virtual resizeEvent of the QWidget to adjust tables column
221 // sizes as the tables width is proportional to the dialogs width.
222 void ReceiveCoinsDialog::resizeEvent(QResizeEvent* event)
223 {
224  QWidget::resizeEvent(event);
225 }
226 
227 void ReceiveCoinsDialog::keyPressEvent(QKeyEvent* event)
228 {
229  if (event->key() == Qt::Key_Return) {
230  // press return -> submit form
231  if (ui->reqAddress->hasFocus() || ui->reqAmount->hasFocus() || ui->reqID->hasFocus()) {
232  event->ignore();
234  return;
235  }
236  }
237 
238  this->QDialog::keyPressEvent(event);
239 }
240 
242  QClipboard *clipboard = QApplication::clipboard();
243  if (ui->lineEditAddress->text().contains(".")) {
244  // It's a smaller screen, don't copy the line text as it is truncated
245  QString addr;
246  std::string address;
247  pwalletMain->ComputeStealthPublicAddress("masteraccount", address);
248  clipboard->setText(QString(address.c_str()));
249  } else {
250  clipboard->setText(ui->lineEditAddress->text());
251  }
252 }
253 
255 {
256  uint64_t paymentID = 0;
257  QClipboard *clipboard = QApplication::clipboard();
258  QString addAccountString;
259  std::string address;
260  address = pwalletMain->GenerateIntegratedAddressWithRandomPaymentID("masteraccount", paymentID);
261 
262  bool ok;
263  QString label = QInputDialog::getText(this, tr("Enter Label"),
264  tr("Label (Payment ID is added by default)"), QLineEdit::Normal,
265  QString::number(paymentID), &ok);
266 
267  if (ok && !label.isEmpty()) {
268  if (label.contains(QString::number(paymentID))) {
269  addAccountString = label.append(" - ").append(QString(address.c_str()));
270  } else {
271  addAccountString = label.append(" (").append(QString::number(paymentID)).append(") - ").append(QString(address.c_str()));
272  }
273  } else if (!ok) {
274  return;
275  }
276 
277  ui->reqAddress->addItem(addAccountString);
278  ui->reqAddress->setCurrentIndex(ui->reqAddress->count() - 1);
279  clipboard->setText(QString(address.c_str()));
280 }
281 
283 {
284  if (ui->reqAddress->currentText().contains("Master Account")) return;
285  ui->reqAddress->removeItem(ui->reqAddress->currentIndex());
286 }
287 
289 {
290  QString str = ui->reqAddress->currentText();
291  QStringList list = str.split("-");
292  QString address = list[1].trimmed();
293  //Set lineEditAddress to Currently Selected Account address for copy to clipboard
294  ui->lineEditAddress->setText(address);
295 }
WalletModel::getOptionsModel
OptionsModel * getOptionsModel()
Definition: walletmodel.cpp:358
ReceiveCoinsDialog::generateAddress
void generateAddress()
Definition: receivecoinsdialog.cpp:254
ReceiveCoinsDialog
Dialog for requesting payment of bitcoins.
Definition: receivecoinsdialog.h:33
ReceiveCoinsDialog::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
Definition: receivecoinsdialog.cpp:227
ReceiveCoinsDialog::setModel
void setModel(WalletModel *model)
Definition: receivecoinsdialog.cpp:88
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:102
addressbookpage.h
walletmodel.h
CWallet::GetKeyFromPool
bool GetKeyFromPool(CPubKey &key)
Definition: wallet.cpp:4829
ReceiveCoinsDialog::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Definition: receivecoinsdialog.cpp:222
WalletModel::getAddressTableModel
AddressTableModel * getAddressTableModel()
Definition: walletmodel.cpp:363
ReceiveCoinsDialog::ui
Ui::ReceiveCoinsDialog * ui
Definition: receivecoinsdialog.h:64
ReceiveCoinsDialog::loadAccount
void loadAccount()
Definition: receivecoinsdialog.cpp:99
ReceiveCoinsDialog::on_receiveButton_clicked
void on_receiveButton_clicked()
Definition: receivecoinsdialog.cpp:189
CWallet::ComputeStealthPublicAddress
bool ComputeStealthPublicAddress(const std::string &accountName, std::string &pubAddress)
Definition: wallet.cpp:6379
ReceiveCoinsDialog::model
WalletModel * model
Definition: receivecoinsdialog.h:66
ReceiveCoinsDialog::updateDisplayUnit
void updateDisplayUnit()
Definition: receivecoinsdialog.cpp:185
ReceiveRequestDialog::setInfo
void setInfo(const SendCoinsRecipient &info)
Definition: receiverequestdialog.cpp:119
SendCoinsRecipient
Definition: walletmodel.h:46
ReceiveCoinsDialog::contextMenu
QMenu * contextMenu
Definition: receivecoinsdialog.h:67
ReceiveCoinsDialog::getValidatedAmount
CAmount getValidatedAmount()
Definition: receivecoinsdialog.cpp:82
CAmount
int64_t CAmount
Amount in PRCY (Can be negative)
Definition: amount.h:17
guiutil.h
ReceiveCoinsDialog::removeAddress
void removeAddress()
Definition: receivecoinsdialog.cpp:282
ReceiveRequestDialog
Definition: receiverequestdialog.h:48
ReceiveCoinsDialog::~ReceiveCoinsDialog
~ReceiveCoinsDialog()
Definition: receivecoinsdialog.cpp:165
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
Ui
Definition: 2faconfirmdialog.h:7
receivecoinsdialog.h
ReceiveRequestDialog::setModel
void setModel(OptionsModel *model)
Definition: receiverequestdialog.cpp:108
ReceiveCoinsDialog::changeAddress
void changeAddress(int)
Definition: receivecoinsdialog.cpp:288
bitcoinunits.h
ReceiveCoinsDialog::clear
void clear()
Definition: receivecoinsdialog.cpp:170
optionsmodel.h
ReceiveCoinsDialog::ReceiveCoinsDialog
ReceiveCoinsDialog(QWidget *parent=0)
Definition: receivecoinsdialog.cpp:27
GUIUtil::loadStyleSheet
QString loadStyleSheet()
Load global CSS theme.
Definition: guiutil.cpp:721
pwalletMain
CWallet * pwalletMain
Definition: wallet.cpp:49
receiverequestdialog.h
ReceiveCoinsDialog::reject
void reject()
Definition: receivecoinsdialog.cpp:175
ReceiveCoinsDialog::accept
void accept()
Definition: receivecoinsdialog.cpp:180
addresstablemodel.h
CWallet::CreatePrivacyAccount
void CreatePrivacyAccount(bool force=false)
Definition: wallet.cpp:4766
ReceiveCoinsDialog::copyAddress
void copyAddress()
Definition: receivecoinsdialog.cpp:241
CWallet::GenerateIntegratedAddressWithRandomPaymentID
std::string GenerateIntegratedAddressWithRandomPaymentID(std::string accountName, uint64_t &paymentID)
Definition: wallet.cpp:6501
CCryptoKeyStore::IsLocked
bool IsLocked() const
Definition: crypter.h:159