forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
receivewidget.cpp
277 lines (241 loc) · 10.2 KB
/
receivewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2019-2020 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "qt/pivx/receivewidget.h"
#include "qt/pivx/forms/ui_receivewidget.h"
#include "qt/pivx/requestdialog.h"
#include "qt/pivx/addnewcontactdialog.h"
#include "qt/pivx/qtutils.h"
#include "qt/pivx/myaddressrow.h"
#include "qt/pivx/furlistrow.h"
#include "qt/pivx/addressholder.h"
#include "walletmodel.h"
#include "guiutil.h"
#include "pairresult.h"
#include <QModelIndex>
#include <QColor>
#include <QDateTime>
#define DECORATION_SIZE 70
#define NUM_ITEMS 3
ReceiveWidget::ReceiveWidget(PIVXGUI* parent) :
PWidget(parent),
ui(new Ui::ReceiveWidget)
{
ui->setupUi(this);
this->setStyleSheet(parent->styleSheet());
delegate = new FurAbstractListItemDelegate(
DECORATION_SIZE,
new AddressHolder(isLightTheme()),
this
);
// Containers
setCssProperty(ui->left, "container");
ui->left->setContentsMargins(20,20,20,20);
setCssProperty(ui->right, "container-right");
ui->right->setContentsMargins(0,9,0,0);
// Title
ui->labelTitle->setText(tr("Receive"));
ui->labelSubtitle1->setText(tr("Scan the QR code or copy the address to receive PIV."));
setCssTitleScreen(ui->labelTitle);
setCssSubtitleScreen(ui->labelSubtitle1);
// Address
ui->labelAddress->setText(tr("No address "));
setCssProperty(ui->labelAddress, "label-address-box");
ui->labelDate->setText("Dec. 19, 2018");
setCssSubtitleScreen(ui->labelDate);
ui->labelLabel->setText("");
setCssSubtitleScreen(ui->labelLabel);
// Options
ui->btnMyAddresses->setTitleClassAndText("btn-title-grey", "My Addresses");
ui->btnMyAddresses->setSubTitleClassAndText("text-subtitle", "List your own addresses.");
ui->btnMyAddresses->layout()->setMargin(0);
ui->btnMyAddresses->setRightIconClass("ic-arrow");
ui->btnRequest->setTitleClassAndText("btn-title-grey", "Create Request");
ui->btnRequest->setSubTitleClassAndText("text-subtitle", "Request payment with a fixed amount.");
ui->btnRequest->layout()->setMargin(0);
ui->pushButtonLabel->setText(tr("Add Label"));
ui->pushButtonLabel->setLayoutDirection(Qt::RightToLeft);
setCssProperty(ui->pushButtonLabel, "btn-secundary-label");
ui->pushButtonNewAddress->setText(tr("Generate Address"));
ui->pushButtonNewAddress->setLayoutDirection(Qt::RightToLeft);
setCssProperty(ui->pushButtonNewAddress, "btn-secundary-new-address");
ui->pushButtonCopy->setText(tr("Copy"));
ui->pushButtonCopy->setLayoutDirection(Qt::RightToLeft);
setCssProperty(ui->pushButtonCopy, "btn-secundary-copy");
// List Addresses
setCssProperty(ui->listViewAddress, "container");
ui->listViewAddress->setItemDelegate(delegate);
ui->listViewAddress->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
ui->listViewAddress->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
ui->listViewAddress->setAttribute(Qt::WA_MacShowFocusRect, false);
ui->listViewAddress->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->listViewAddress->setUniformItemSizes(true);
spacer = new QSpacerItem(40, 20, QSizePolicy::Maximum, QSizePolicy::Expanding);
ui->btnMyAddresses->setChecked(true);
ui->container_right->addItem(spacer);
ui->listViewAddress->setVisible(false);
// Connect
connect(ui->pushButtonLabel, SIGNAL(clicked()), this, SLOT(onLabelClicked()));
connect(ui->pushButtonCopy, SIGNAL(clicked()), this, SLOT(onCopyClicked()));
connect(ui->pushButtonNewAddress, SIGNAL(clicked()), this, SLOT(onNewAddressClicked()));
connect(ui->listViewAddress, SIGNAL(clicked(QModelIndex)), this, SLOT(handleAddressClicked(QModelIndex)));
connect(ui->btnRequest, SIGNAL(clicked()), this, SLOT(onRequestClicked()));
connect(ui->btnMyAddresses, SIGNAL(clicked()), this, SLOT(onMyAddressesClicked()));
}
void ReceiveWidget::loadWalletModel(){
if(walletModel) {
this->addressTableModel = walletModel->getAddressTableModel();
this->filter = new AddressFilterProxyModel(AddressTableModel::Receive, this);
this->filter->setSourceModel(addressTableModel);
ui->listViewAddress->setModel(this->filter);
ui->listViewAddress->setModelColumn(AddressTableModel::Address);
if(!info) info = new SendCoinsRecipient();
refreshView();
// data change
connect(this->addressTableModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(refreshView()));
}
}
void ReceiveWidget::refreshView(QString refreshAddress){
try {
QString latestAddress = (refreshAddress.isEmpty()) ? this->addressTableModel->getAddressToShow() : refreshAddress;
if (latestAddress.isEmpty()) { // new default address
CBitcoinAddress newAddress;
PairResult r = walletModel->getNewAddress(newAddress, "Default");
// Check for generation errors
if (!r.result) {
ui->labelQrImg->setText(tr("No available address, try unlocking the wallet"));
inform(tr("Error generating address"));
return;
}
latestAddress = QString::fromStdString(newAddress.ToString());
}
ui->labelAddress->setText(latestAddress);
int64_t time = walletModel->getKeyCreationTime(CBitcoinAddress(latestAddress.toStdString()));
ui->labelDate->setText(GUIUtil::dateTimeStr(QDateTime::fromTime_t(static_cast<uint>(time))));
updateQr(latestAddress);
updateLabel();
} catch (const std::runtime_error& error){
ui->labelQrImg->setText(tr("No available address, try unlocking the wallet"));
inform(tr("Error generating address"));
}
}
void ReceiveWidget::updateLabel(){
if(!info->address.isEmpty()) {
// Check if address label exists
QString label = addressTableModel->labelForAddress(info->address);
if (!label.isEmpty()) {
ui->labelLabel->setVisible(true);
ui->labelLabel->setText(label);
ui->pushButtonLabel->setText(tr("Edit Label"));
}else{
ui->labelLabel->setVisible(false);
}
}
}
void ReceiveWidget::updateQr(QString address){
info->address = address;
QString uri = GUIUtil::formatBitcoinURI(*info);
ui->labelQrImg->setText("");
QString error;
QColor qrColor("#382d4d");
QPixmap pixmap = encodeToQr(uri, error, qrColor);
if(!pixmap.isNull()){
qrImage = &pixmap;
ui->labelQrImg->setPixmap(qrImage->scaled(ui->labelQrImg->width(), ui->labelQrImg->height()));
}else{
ui->labelQrImg->setText(!error.isEmpty() ? error : "Error encoding address");
}
}
void ReceiveWidget::handleAddressClicked(const QModelIndex &index){
QModelIndex rIndex = filter->mapToSource(index);
refreshView(rIndex.data(Qt::DisplayRole).toString());
}
void ReceiveWidget::onLabelClicked(){
if(walletModel && !isShowingDialog) {
isShowingDialog = true;
showHideOp(true);
AddNewContactDialog *dialog = new AddNewContactDialog(window);
dialog->setTexts(tr("Edit Address Label"));
dialog->setData(info->address, addressTableModel->labelForAddress(info->address));
if (openDialogWithOpaqueBackgroundY(dialog, window, 3.5, 6)) {
QString label = dialog->getLabel();
const CBitcoinAddress address = CBitcoinAddress(info->address.toUtf8().constData());
if (!label.isEmpty() && walletModel->updateAddressBookLabels(
address.Get(),
label.toUtf8().constData(),
AddressBook::AddressBookPurpose::RECEIVE
)
) {
// update label status (icon color)
updateLabel();
inform(tr("Address label saved"));
} else {
inform(tr("Error storing address label"));
}
}
isShowingDialog = false;
}
}
void ReceiveWidget::onNewAddressClicked(){
try {
if (!verifyWalletUnlocked()) return;
CBitcoinAddress address;
PairResult r = walletModel->getNewAddress(address, "");
// Check for validity
if(!r.result) {
inform(r.status->c_str());
return;
}
updateQr(QString::fromStdString(address.ToString()));
ui->labelAddress->setText(!info->address.isEmpty() ? info->address : tr("No address"));
updateLabel();
inform(tr("New address created"));
} catch (const std::runtime_error& error){
// Error generating address
inform("Error generating address");
}
}
void ReceiveWidget::onCopyClicked(){
GUIUtil::setClipboard(info->address);
inform(tr("Address copied"));
}
void ReceiveWidget::onRequestClicked(){
showAddressGenerationDialog(true);
}
void ReceiveWidget::showAddressGenerationDialog(bool isPaymentRequest) {
if(walletModel && !isShowingDialog) {
if (!verifyWalletUnlocked()) return;
isShowingDialog = true;
showHideOp(true);
RequestDialog *dialog = new RequestDialog(window);
dialog->setWalletModel(walletModel);
dialog->setPaymentRequest(isPaymentRequest);
openDialogWithOpaqueBackgroundY(dialog, window, 3.5, 12);
if (dialog->res == 1){
inform(tr("URI copied to clipboard"));
} else if (dialog->res == 2){
inform(tr("Address copied to clipboard"));
}
dialog->deleteLater();
isShowingDialog = false;
}
}
void ReceiveWidget::onMyAddressesClicked(){
bool isVisible = ui->listViewAddress->isVisible();
if(!isVisible){
ui->btnMyAddresses->setRightIconClass("btn-dropdown", true);
ui->listViewAddress->setVisible(true);
ui->container_right->removeItem(spacer);
ui->listViewAddress->update();
}else{
ui->btnMyAddresses->setRightIconClass("ic-arrow", true);
ui->container_right->addItem(spacer);
ui->listViewAddress->setVisible(false);
}
}
void ReceiveWidget::changeTheme(bool isLightTheme, QString& theme){
static_cast<AddressHolder*>(this->delegate->getRowFactory())->isLightTheme = isLightTheme;
}
ReceiveWidget::~ReceiveWidget(){
delete ui;
}