-
Notifications
You must be signed in to change notification settings - Fork 6
/
accountpage.cpp
432 lines (360 loc) · 12.6 KB
/
accountpage.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <QDebug>
#include <QPainter>
#include <QTimer>
#include <QMouseEvent>
#include <QScrollBar>
#include <QTextItem>
#include <QtCore/qmath.h>
#include <QPushButton>
#include <QHBoxLayout>
#include <QClipboard>
#include "accountpage.h"
#include "ui_accountpage.h"
#include "goopal.h"
#include "debug_log.h"
#include "namedialog.h"
#include "deleteaccountdialog.h"
#include "accountcellwidget.h"
#include "exportdialog.h"
#include "importdialog.h"
#include "commondialog.h"
#include "showcontentdialog.h"
#include "incomecellwidget.h"
#include "dialog/chooseexportdialog.h"
#include "control/rightclickmenudialog.h"
#include "control/chooseaddaccountdialog.h"
#include "dialog/renamedialog.h"
#include "control/accountdetailwidget.h"
#include "frame.h"
#include "datamgr.h"
#include "thirddatamgr.h"
const int tabelWidgetHeight = 57;
AddressWidget::AddressWidget()
{
addressLabel = new QLabel(this);
addressLabel->setObjectName(QStringLiteral("identityLabel"));
addressLabel->setGeometry(5, 0, 0, 0);
addressLabel->setFont(QFont("Microsoft Yahei", 9));
addressLabel->setStyleSheet(QStringLiteral("background:transparent;"));
addressLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
copyBtn = new QToolButton(this);
copyBtn->setGeometry(5, 0, 11, 13);
copyBtn->setCursor(QCursor(Qt::PointingHandCursor));
copyBtn->setStyleSheet(QString("QToolButton{background-image:url(%1pic2/copyBtn.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}"
"QToolButton:hover{background-image:url(%2pic2/copyBtn_hover.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-clip: padding;border-style: flat;}").arg(DataMgr::getDataMgr()->getWorkPath()).arg(DataMgr::getDataMgr()->getWorkPath()));
connect(copyBtn, SIGNAL(clicked()), this, SLOT(copyBtnClicked()));
}
void AddressWidget::copyBtnClicked()
{
QClipboard* clipBoard = QApplication::clipboard();
clipBoard->setText(_address);
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText(tr("Copy to clipboard"));
commonDialog.pop();
}
void AddressWidget::setAddress(const QString& address)
{
_address = address;
addressLabel->setText(address);
addressLabel->adjustSize();
addressLabel->move(5, (tabelWidgetHeight - addressLabel->size().height()) / 2);
copyBtn->move(5 + addressLabel->size().width() + 5, (tabelWidgetHeight - copyBtn->size().height()) / 2);
}
OperationsWidget::OperationsWidget(AccountPage* page)
{
accountPage = page;
deleteBtn = new QToolButton(this);
deleteBtn->setGeometry(QRect(4, (tabelWidgetHeight - 30) / 2, 76, 30));
QFont font2;
deleteBtn->setFont(font2);
deleteBtn->setCursor(QCursor(Qt::PointingHandCursor));
deleteBtn->setFont(QFont("Microsoft Yahei", 11));
deleteBtn->setText(tr("Hide"));
deleteBtn->setStyleSheet(QLatin1String("QToolButton{background-color:#999999;color:#ffffff;border:0px solid rgb(64,153,255);border-radius:3px;}"
"QToolButton:hover{background-color:#62a9f8;}"));
connect(deleteBtn, SIGNAL(clicked()), this, SLOT(deleteBtnClicked()));
exportBtn = new QToolButton(this);
exportBtn->setGeometry(QRect(84, (tabelWidgetHeight - 30) / 2, 76, 30));
exportBtn->setFont(font2);
exportBtn->setCursor(QCursor(Qt::PointingHandCursor));
exportBtn->setFont(QFont("Microsoft Yahei", 11));
exportBtn->setText(tr("Export"));
exportBtn->setStyleSheet(QLatin1String("QToolButton{background-color:#999999;color:#ffffff;border:1px solid rgb(187,187,187);border-radius:3px;}"
"QToolButton:hover{background-color:#62a9f8;}"));
connect(exportBtn, SIGNAL(clicked()), this, SLOT(exportBtnClicked()));
}
void OperationsWidget::deleteBtnClicked()
{
accountPage->deleteAccount(accountName);
}
void OperationsWidget::exportBtnClicked()
{
ExportDialog exportDialog(accountName);
exportDialog.hiddenCancelBtn();
exportDialog.pop();
}
void OperationsWidget::setAccountName(const QString& account)
{
accountName = account;
}
AccountPage::AccountPage(QWidget *parent) :
QWidget(parent),
hasDelegateOrNot(false),
detailOrNot(false),
ui(new Ui::AccountPage)
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
ui->setupUi(this);
setAutoFillBackground(true);
ui->accountTableWidget->installEventFilter(this);
ui->accountTableWidget->setSelectionMode(QAbstractItemView::NoSelection);
ui->accountTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->accountTableWidget->setFocusPolicy(Qt::NoFocus);
ui->accountTableWidget->setShowGrid(false);
ui->accountTableWidget->setFrameShape(QFrame::NoFrame);
ui->accountTableWidget->setMouseTracking(true);
ui->accountTableWidget->setStyleSheet("QTableWidget{background:transparent;}");
ui->accountTableWidget->setColumnWidth(0, 100);
ui->accountTableWidget->setColumnWidth(1, 315);
ui->accountTableWidget->setColumnWidth(2, 175);
ui->accountTableWidget->setColumnWidth(3, 186);
retranslator(DataMgr::getInstance()->getLanguage());
ui->accountTableWidget->hide();
ui->loadingLabel->show();
ui->initLabel->hide();
detailWidget = new AccountDetailWidget(this);
connect(detailWidget, SIGNAL(back()), this, SLOT(hideDetailWidget()));
connect(detailWidget, SIGNAL(upgrade(QString)), this, SIGNAL(showUpgradePage(QString)));
connect(detailWidget, SIGNAL(applyDelegate(QString)), this, SIGNAL(showApplyDelegatePage(QString)));
detailWidget->move(827, 93);
detailWidget->raise();
detailWidget->show();
QLabel* addAccountBtnLabel = new QLabel(ui->addAccountBtn);
addAccountBtnLabel->setGeometry(23, 12, 12, 12);
refreshPage();
Frame* frame = dynamic_cast<Frame*>(Goopal::getInstance()->mainFrame);
connect(frame, &Frame::updateAccountBalance, this, &AccountPage::refreshPage);
DLOG_QT_WALLET_FUNCTION_END;
}
AccountPage::~AccountPage()
{
delete ui;
}
void AccountPage::importAccount()
{
ImportDialog importDialog;
if (importDialog.pop())
{
refreshPage();
emit refreshAccountInfo();
}
}
void AccountPage::addAccount()
{
NameDialog nameDialog;
QString name = nameDialog.pop();
if (!name.isEmpty())
{
newAccountName = name;
bool ret = DataMgr::getInstance()->walletAccountCreate(name);
if (ret)
{
CommonDialog commonDialog(CommonDialog::OkOnly);
commonDialog.setText(tr("Your account has been added successfully. Please save your private key now."));
bool choice = commonDialog.pop();
if (choice) {
ExportDialog exportDialog(newAccountName);
exportDialog.hiddenCancelBtn();
exportDialog.pop();
}
refreshPage();
emit refreshAccountInfo();
}
}
}
void AccountPage::updateAccountList()
{
accountNameList = DataMgr::getInstance()->getAccountInfo()->keys();
if (accountNameList.size() == 0) // 如果还没有账户
{
ui->initLabel->show();
ui->loadingLabel->hide();
ui->accountTableWidget->hide();
return;
}
else {
ui->initLabel->hide();
ui->loadingLabel->show();
ui->accountTableWidget->show();
}
int rowNum = 0;
int size = DataMgr::getInstance()->getAccountInfo()->size();
ui->accountTableWidget->setRowCount(size);
for (int i = 0; i < size; i++)
{
QString accountName = accountNameList.at(i);
ui->accountTableWidget->setRowHeight(rowNum, tabelWidgetHeight);
//account name
QTableWidgetItem* item0 = ui->accountTableWidget->item(rowNum, 0);
if (item0 == nullptr)
{
item0 = new QTableWidgetItem();
item0->setFont(QFont("Microsoft Yahei", 11));
ui->accountTableWidget->setItem(rowNum, 0, item0);
}
item0->setText(accountName);
//address
AddressWidget* item1 = dynamic_cast<AddressWidget*>(ui->accountTableWidget->cellWidget(rowNum, 1));
if (item1 == nullptr)
{
item1 = new AddressWidget();
item1->setFont(QFont("Microsoft Yahei", 11));
ui->accountTableWidget->setCellWidget(rowNum, 1, item1);
}
QString address = DataMgr::getInstance()->getAccountInfo()->value(accountName).address;
item1->setAddress(address);
//total price
double totalPrice = 0;
const auto& accountInfo = DataMgr::getInstance()->getAccountInfo()->value(accountName);
auto itr = accountInfo.balances.begin();
for (; itr != accountInfo.balances.end(); itr++)
{
double price = ThirdDataMgr::getInstance()->getAssetPrice(itr.key());
double balance = itr.value().toDouble();
if (price > 0 && balance > 0)
totalPrice += price * balance;
}
QTableWidgetItem* item2 = ui->accountTableWidget->item(rowNum, 2);
if (item2 == NULL)
{
item2 = new QTableWidgetItem();
item2->setFont(QFont("Microsoft Yahei", 11));
ui->accountTableWidget->setItem(rowNum, 2, item2);
}
QString currencySymbol = ThirdDataMgr::getInstance()->getCurrentCurrencySymbol();
item2->setText(doubleToStr(totalPrice) + currencySymbol);
//operations buttons
OperationsWidget* item3 = dynamic_cast<OperationsWidget*>(ui->accountTableWidget->cellWidget(rowNum, 3));
if (item3 == nullptr)
{
item3 = new OperationsWidget(this);
ui->accountTableWidget->setCellWidget(rowNum, 3, item3);
}
item3->setAccountName(accountName);
rowNum++;
}
ui->accountTableWidget->show();
ui->loadingLabel->hide();
}
void AccountPage::on_addAccountBtn_clicked()
{
DLOG_QT_WALLET_FUNCTION_BEGIN;
ChooseAddAccountDialog* chooseAddAccountDialog = new ChooseAddAccountDialog(this);
chooseAddAccountDialog->move(ui->addAccountBtn->mapToGlobal(QPoint(0, -75)));
connect(chooseAddAccountDialog, SIGNAL(newAccount()), this, SLOT(addAccount()));
connect(chooseAddAccountDialog, SIGNAL(importAccount()), this, SLOT(importAccount()));
chooseAddAccountDialog->setModal(false);
chooseAddAccountDialog->show();
DLOG_QT_WALLET_FUNCTION_END;
}
void AccountPage::on_accountTableWidget_cellClicked(int row, int column)
{
//auto& accountName = accountNameList[row];
//showDetailWidget(accountName);
}
void AccountPage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setBrush(QColor(255, 255, 255));
painter.setPen(QPen(QColor(187, 187, 187), 2));
painter.drawRect(QRect(0, 0, 826, 532));
painter.drawRect(QRect(30, 70, 770, 42));
}
void AccountPage::retranslator(QString language)
{
ui->retranslateUi(this);
ui->addAccountBtn->setStyleSheet("QToolButton{background-color:#4dc8cf;color:#ffffff;border:0px solid rgb(64,153,255);border-radius:3px;}"
"QToolButton:hover{background-color:#7dd9df;}");
updateAccountCountLabel();
}
void AccountPage::updateAccountCountLabel()
{
int totalCount = 0;
int hasAssetCount = 0;
auto accounts = DataMgr::getInstance()->getAccountInfo();
for (auto itr = accounts->begin(); itr != accounts->end(); itr++)
{
totalCount++;
auto balancesItr = itr->balances.begin();
for (; balancesItr != itr->balances.end(); balancesItr++)
{
if (balancesItr.value().toDouble() > 0)
{
hasAssetCount++;
break;
}
}
}
QString account_str(tr("account"));
QString accounts_str(tr("accounts"));
QString account_1 = totalCount == 1 ? account_str : accounts_str;
QString account_2 = hasAssetCount == 1 ? account_str : accounts_str;
auto countTip = QString(tr("You currently have %1 %2 and %3 %4 have assets.")).arg(totalCount).arg(account_1).arg(hasAssetCount).arg(account_2);
ui->accountCountLabel->setText(countTip);
}
void AccountPage::refreshPage()
{
updateAccountList();
updateAccountCountLabel();
detailWidget->setAccount(detailWidget->accountName);
}
void AccountPage::showExportDialog(QString name)
{
ExportDialog exportDialog(name);
exportDialog.pop();
}
void AccountPage::renameAccount(QString name)
{
RenameDialog renameDialog;
QString newName = renameDialog.pop();
if (!newName.isEmpty() && newName != name)
{
detailWidget->accountName = newName;
emit refreshAccountInfo();
}
}
void AccountPage::deleteAccount(QString name)
{
DeleteAccountDialog deleteACcountDialog(name);
if (deleteACcountDialog.pop())
{
refreshPage();
CommonDialog tipDialog(CommonDialog::OkOnly);
auto text = QString(tr("You have successfully hidden the account \"%1\".\nYou can retrieve the account with the private key in Add Account-Import Account.")).arg(name);
tipDialog.setText(text, Qt::AlignLeft | Qt::AlignVCenter);
tipDialog.pop();
}
}
void AccountPage::showDetailWidget(QString name)
{
detailOrNot = true;
// ui->accountTableWidget->setGeometry(50, 124, 245, 263);
ui->accountTableWidget->setColumnWidth(0, 100);
ui->accountTableWidget->setColumnWidth(1, 1);
ui->accountTableWidget->setColumnWidth(2, 158);
ui->accountTableWidget->setColumnWidth(3, 1);
// ui->addAccountBtn->move(153, 470);
detailWidget->setAccount(name);
detailWidget->dynamicShow();
}
void AccountPage::hideDetailWidget()
{
detailOrNot = false;
ui->accountTableWidget->setGeometry(50, 124, 776, 341);
ui->accountTableWidget->setColumnWidth(0, 100);
ui->accountTableWidget->setColumnWidth(1, 315);
ui->accountTableWidget->setColumnWidth(2, 175);
ui->accountTableWidget->setColumnWidth(3, 186);
ui->addAccountBtn->move(675, 470);
detailWidget->dynamicHide();
}