forked from PIVX-Project/PIVX
/
overviewpage.cpp
405 lines (350 loc) · 19.2 KB
/
overviewpage.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
// Copyright (c) 2011-2014 The Bitcoin developers
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2018 The PIVX developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "overviewpage.h"
#include "ui_overviewpage.h"
#include "bitcoinunits.h"
#include "clientmodel.h"
#include "guiconstants.h"
#include "guiutil.h"
#include "init.h"
#include "obfuscation.h"
#include "obfuscationconfig.h"
#include "optionsmodel.h"
#include "transactionfilterproxy.h"
#include "transactionrecord.h"
#include "transactiontablemodel.h"
#include "walletmodel.h"
#include <QAbstractItemDelegate>
#include <QPainter>
#include <QSettings>
#include <QTimer>
#define DECORATION_SIZE 48
#define ICON_OFFSET 16
#define NUM_ITEMS 9
extern CWallet* pwalletMain;
class TxViewDelegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
TxViewDelegate() : QAbstractItemDelegate(), unit(BitcoinUnits::PIV)
{
}
inline void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
painter->save();
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QRect mainRect = option.rect;
mainRect.moveLeft(ICON_OFFSET);
QRect decorationRect(mainRect.topLeft(), QSize(DECORATION_SIZE, DECORATION_SIZE));
int xspace = DECORATION_SIZE + 8;
int ypad = 6;
int halfheight = (mainRect.height() - 2 * ypad) / 2;
QRect amountRect(mainRect.left() + xspace, mainRect.top() + ypad, mainRect.width() - xspace - ICON_OFFSET, halfheight);
QRect addressRect(mainRect.left() + xspace, mainRect.top() + ypad + halfheight, mainRect.width() - xspace, halfheight);
icon.paint(painter, decorationRect);
QDateTime date = index.data(TransactionTableModel::DateRole).toDateTime();
QString address = index.data(Qt::DisplayRole).toString();
qint64 amount = index.data(TransactionTableModel::AmountRole).toLongLong();
bool confirmed = index.data(TransactionTableModel::ConfirmedRole).toBool();
// Check transaction status
int nStatus = index.data(TransactionTableModel::StatusRole).toInt();
bool fConflicted = false;
if (nStatus == TransactionStatus::Conflicted || nStatus == TransactionStatus::NotAccepted) {
fConflicted = true; // Most probably orphaned, but could have other reasons as well
}
bool fImmature = false;
if (nStatus == TransactionStatus::Immature) {
fImmature = true;
}
QVariant value = index.data(Qt::ForegroundRole);
QColor foreground = COLOR_BLACK;
if (value.canConvert<QBrush>()) {
QBrush brush = qvariant_cast<QBrush>(value);
foreground = brush.color();
}
painter->setPen(foreground);
QRect boundingRect;
painter->drawText(addressRect, Qt::AlignLeft | Qt::AlignVCenter, address, &boundingRect);
if (index.data(TransactionTableModel::WatchonlyRole).toBool()) {
QIcon iconWatchonly = qvariant_cast<QIcon>(index.data(TransactionTableModel::WatchonlyDecorationRole));
QRect watchonlyRect(boundingRect.right() + 5, mainRect.top() + ypad + halfheight, 16, halfheight);
iconWatchonly.paint(painter, watchonlyRect);
}
if(fConflicted) { // No need to check anything else for conflicted transactions
foreground = COLOR_CONFLICTED;
} else if (!confirmed || fImmature) {
foreground = COLOR_UNCONFIRMED;
} else if (amount < 0) {
foreground = COLOR_NEGATIVE;
} else {
foreground = COLOR_BLACK;
}
painter->setPen(foreground);
QString amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::separatorAlways);
if (!confirmed) {
amountText = QString("[") + amountText + QString("]");
}
painter->drawText(amountRect, Qt::AlignRight | Qt::AlignVCenter, amountText);
painter->setPen(COLOR_BLACK);
painter->drawText(amountRect, Qt::AlignLeft | Qt::AlignVCenter, GUIUtil::dateTimeStr(date));
painter->restore();
}
inline QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
return QSize(DECORATION_SIZE, DECORATION_SIZE);
}
int unit;
};
#include "overviewpage.moc"
OverviewPage::OverviewPage(QWidget* parent) : QWidget(parent),
ui(new Ui::OverviewPage),
clientModel(0),
walletModel(0),
currentBalance(-1),
currentUnconfirmedBalance(-1),
currentImmatureBalance(-1),
currentZerocoinBalance(-1),
currentUnconfirmedZerocoinBalance(-1),
currentimmatureZerocoinBalance(-1),
currentWatchOnlyBalance(-1),
currentWatchUnconfBalance(-1),
currentWatchImmatureBalance(-1),
txdelegate(new TxViewDelegate()),
filter(0)
{
nDisplayUnit = 0; // just make sure it's not unitialized
ui->setupUi(this);
// Recent transactions
ui->listTransactions->setItemDelegate(txdelegate);
ui->listTransactions->setIconSize(QSize(DECORATION_SIZE, DECORATION_SIZE));
ui->listTransactions->setMinimumHeight(NUM_ITEMS * (DECORATION_SIZE + 2));
ui->listTransactions->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(ui->listTransactions, SIGNAL(clicked(QModelIndex)), this, SLOT(handleTransactionClicked(QModelIndex)));
// init "out of sync" warning labels
ui->labelWalletStatus->setText("(" + tr("out of sync") + ")");
ui->labelTransactionsStatus->setText("(" + tr("out of sync") + ")");
// start with displaying the "out of sync" warnings
showOutOfSyncWarning(true);
}
void OverviewPage::handleTransactionClicked(const QModelIndex& index)
{
if (filter)
emit transactionClicked(filter->mapToSource(index));
}
OverviewPage::~OverviewPage()
{
delete ui;
}
void OverviewPage::getPercentage(CAmount nUnlockedBalance, CAmount nZerocoinBalance, QString& sPIVPercentage, QString& szPIVPercentage)
{
int nPrecision = 2;
double dzPercentage = 0.0;
if (nZerocoinBalance <= 0){
dzPercentage = 0.0;
}
else{
if (nUnlockedBalance <= 0){
dzPercentage = 100.0;
}
else{
dzPercentage = 100.0 * (double)(nZerocoinBalance / (double)(nZerocoinBalance + nUnlockedBalance));
}
}
double dPercentage = 100.0 - dzPercentage;
szPIVPercentage = "(" + QLocale(QLocale::system()).toString(dzPercentage, 'f', nPrecision) + " %)";
sPIVPercentage = "(" + QLocale(QLocale::system()).toString(dPercentage, 'f', nPrecision) + " %)";
}
void OverviewPage::setBalance(const CAmount& balance, const CAmount& unconfirmedBalance, const CAmount& immatureBalance,
const CAmount& zerocoinBalance, const CAmount& unconfirmedZerocoinBalance, const CAmount& immatureZerocoinBalance,
const CAmount& watchOnlyBalance, const CAmount& watchUnconfBalance, const CAmount& watchImmatureBalance)
{
currentBalance = balance;
currentUnconfirmedBalance = unconfirmedBalance;
currentImmatureBalance = immatureBalance;
currentZerocoinBalance = zerocoinBalance;
currentUnconfirmedZerocoinBalance = unconfirmedZerocoinBalance;
currentimmatureZerocoinBalance = immatureZerocoinBalance;
currentWatchOnlyBalance = watchOnlyBalance;
currentWatchUnconfBalance = watchUnconfBalance;
currentWatchImmatureBalance = watchImmatureBalance;
CAmount nLockedBalance = 0;
CAmount nWatchOnlyLockedBalance = 0;
if (pwalletMain) {
nLockedBalance = pwalletMain->GetLockedCoins();
nWatchOnlyLockedBalance = pwalletMain->GetLockedWatchOnlyBalance();
}
// PIV Balance
CAmount nTotalBalance = balance + unconfirmedBalance + nLockedBalance;
CAmount pivAvailableBalance = balance - immatureBalance;
CAmount nTotalWatchBalance = watchOnlyBalance + watchUnconfBalance + watchImmatureBalance;
CAmount nUnlockedBalance = nTotalBalance - nLockedBalance - nLockedBalance; // increment nLockedBalance twice because it was added to
// nTotalBalance above
// zPIV Balance
CAmount matureZerocoinBalance = zerocoinBalance - unconfirmedZerocoinBalance - immatureZerocoinBalance;
// Percentages
QString szPercentage = "";
QString sPercentage = "";
getPercentage(nUnlockedBalance, zerocoinBalance, sPercentage, szPercentage);
// Combined balances
CAmount availableTotalBalance = pivAvailableBalance + matureZerocoinBalance;
CAmount sumTotalBalance = nTotalBalance + zerocoinBalance;
// PIV labels
ui->labelBalance->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, pivAvailableBalance, false, BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, unconfirmedBalance, false, BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, immatureBalance, false, BitcoinUnits::separatorAlways));
ui->labelLockedBalance->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, nLockedBalance, false, BitcoinUnits::separatorAlways));
ui->labelTotal->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, nTotalBalance, false, BitcoinUnits::separatorAlways));
// Watchonly labels
ui->labelWatchAvailable->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchOnlyBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchPending->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchUnconfBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchImmature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, watchImmatureBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchLocked->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, nWatchOnlyLockedBalance, false, BitcoinUnits::separatorAlways));
ui->labelWatchTotal->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, nTotalWatchBalance, false, BitcoinUnits::separatorAlways));
// zPIV labels
ui->labelzBalance->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, zerocoinBalance, false, BitcoinUnits::separatorAlways));
ui->labelzBalanceUnconfirmed->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, unconfirmedZerocoinBalance, false, BitcoinUnits::separatorAlways));
ui->labelzBalanceMature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, matureZerocoinBalance, false, BitcoinUnits::separatorAlways));
ui->labelzBalanceImmature->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, immatureZerocoinBalance, false, BitcoinUnits::separatorAlways));
// Combined labels
ui->labelBalancez->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, availableTotalBalance, false, BitcoinUnits::separatorAlways));
ui->labelTotalz->setText(BitcoinUnits::floorHtmlWithUnit(nDisplayUnit, sumTotalBalance, false, BitcoinUnits::separatorAlways));
// Percentage labels
ui->labelPIVPercent->setText(sPercentage);
ui->labelzPIVPercent->setText(szPercentage);
// Adjust bubble-help according to AutoMint settings
QString automintHelp = tr("Current percentage of zPIV.\nIf AutoMint is enabled this percentage will settle around the configured AutoMint percentage (default = 10%).\n");
bool fEnableZeromint = GetBoolArg("-enablezeromint", true);
int nZeromintPercentage = GetArg("-zeromintpercentage", 10);
if (fEnableZeromint) {
automintHelp += tr("AutoMint is currently enabled and set to ") + QString::number(nZeromintPercentage) + "%.\n";
automintHelp += tr("To disable AutoMint add 'enablezeromint=0' in pivx.conf.");
}
else {
automintHelp += tr("AutoMint is currently disabled.\nTo enable AutoMint change 'enablezeromint=0' to 'enablezeromint=1' in pivx.conf");
}
// Only show most balances if they are non-zero for the sake of simplicity
QSettings settings;
bool settingShowAllBalances = !settings.value("fHideZeroBalances").toBool();
bool showSumAvailable = settingShowAllBalances || sumTotalBalance != availableTotalBalance;
ui->labelBalanceTextz->setVisible(showSumAvailable);
ui->labelBalancez->setVisible(showSumAvailable);
bool showPIVAvailable = settingShowAllBalances || pivAvailableBalance != nTotalBalance;
bool showWatchOnlyPIVAvailable = watchOnlyBalance != nTotalWatchBalance;
bool showPIVPending = settingShowAllBalances || unconfirmedBalance != 0;
bool showWatchOnlyPIVPending = watchUnconfBalance != 0;
bool showPIVLocked = settingShowAllBalances || nLockedBalance != 0;
bool showWatchOnlyPIVLocked = nWatchOnlyLockedBalance != 0;
bool showImmature = settingShowAllBalances || immatureBalance != 0;
bool showWatchOnlyImmature = watchImmatureBalance != 0;
bool showWatchOnly = nTotalWatchBalance != 0;
ui->labelBalance->setVisible(showPIVAvailable || showWatchOnlyPIVAvailable);
ui->labelBalanceText->setVisible(showPIVAvailable || showWatchOnlyPIVAvailable);
ui->labelWatchAvailable->setVisible(showPIVAvailable && showWatchOnly);
ui->labelUnconfirmed->setVisible(showPIVPending || showWatchOnlyPIVPending);
ui->labelPendingText->setVisible(showPIVPending || showWatchOnlyPIVPending);
ui->labelWatchPending->setVisible(showPIVPending && showWatchOnly);
ui->labelLockedBalance->setVisible(showPIVLocked || showWatchOnlyPIVLocked);
ui->labelLockedBalanceText->setVisible(showPIVLocked || showWatchOnlyPIVLocked);
ui->labelWatchLocked->setVisible(showPIVLocked && showWatchOnly);
ui->labelImmature->setVisible(showImmature || showWatchOnlyImmature); // for symmetry reasons also show immature label when the watch-only one is shown
ui->labelImmatureText->setVisible(showImmature || showWatchOnlyImmature);
ui->labelWatchImmature->setVisible(showImmature && showWatchOnly); // show watch-only immature balance
bool showzPIVAvailable = settingShowAllBalances || zerocoinBalance != matureZerocoinBalance;
bool showzPIVUnconfirmed = settingShowAllBalances || unconfirmedZerocoinBalance != 0;
bool showzPIVImmature = settingShowAllBalances || immatureZerocoinBalance != 0;
ui->labelzBalanceMature->setVisible(showzPIVAvailable);
ui->labelzBalanceMatureText->setVisible(showzPIVAvailable);
ui->labelzBalanceUnconfirmed->setVisible(showzPIVUnconfirmed);
ui->labelzBalanceUnconfirmedText->setVisible(showzPIVUnconfirmed);
ui->labelzBalanceImmature->setVisible(showzPIVImmature);
ui->labelzBalanceImmatureText->setVisible(showzPIVImmature);
bool showPercentages = ! (zerocoinBalance == 0 && nTotalBalance == 0);
ui->labelPIVPercent->setVisible(showPercentages);
ui->labelzPIVPercent->setVisible(showPercentages);
static int cachedTxLocks = 0;
if (cachedTxLocks != nCompleteTXLocks) {
cachedTxLocks = nCompleteTXLocks;
ui->listTransactions->update();
}
}
// show/hide watch-only labels
void OverviewPage::updateWatchOnlyLabels(bool showWatchOnly)
{
ui->labelSpendable->setVisible(showWatchOnly); // show spendable label (only when watch-only is active)
ui->labelWatchonly->setVisible(showWatchOnly); // show watch-only label
ui->labelWatchAvailable->setVisible(showWatchOnly); // show watch-only available balance
ui->labelWatchPending->setVisible(showWatchOnly); // show watch-only pending balance
ui->labelWatchLocked->setVisible(showWatchOnly); // show watch-only total balance
ui->labelWatchTotal->setVisible(showWatchOnly); // show watch-only total balance
if (!showWatchOnly) {
ui->labelWatchImmature->hide();
} else {
ui->labelBalance->setIndent(20);
ui->labelUnconfirmed->setIndent(20);
ui->labelLockedBalance->setIndent(20);
ui->labelImmature->setIndent(20);
ui->labelTotal->setIndent(20);
}
}
void OverviewPage::setClientModel(ClientModel* model)
{
this->clientModel = model;
if (model) {
// Show warning if this is a prerelease version
connect(model, SIGNAL(alertsChanged(QString)), this, SLOT(updateAlerts(QString)));
updateAlerts(model->getStatusBarWarnings());
}
}
void OverviewPage::setWalletModel(WalletModel* model)
{
this->walletModel = model;
if (model && model->getOptionsModel()) {
// Set up transaction list
filter = new TransactionFilterProxy();
filter->setSourceModel(model->getTransactionTableModel());
filter->setLimit(NUM_ITEMS);
filter->setDynamicSortFilter(true);
filter->setSortRole(Qt::EditRole);
filter->setShowInactive(false);
filter->sort(TransactionTableModel::Date, Qt::DescendingOrder);
ui->listTransactions->setModel(filter);
ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
// Keep up to date with wallet
setBalance(model->getBalance(), model->getUnconfirmedBalance(), model->getImmatureBalance(),
model->getZerocoinBalance(), model->getUnconfirmedZerocoinBalance(), model->getImmatureZerocoinBalance(),
model->getWatchBalance(), model->getWatchUnconfirmedBalance(), model->getWatchImmatureBalance());
connect(model, SIGNAL(balanceChanged(CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount)), this,
SLOT(setBalance(CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount, CAmount)));
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
connect(model->getOptionsModel(), SIGNAL(hideZeroBalancesChanged(bool)), this, SLOT(updateDisplayUnit()));
updateWatchOnlyLabels(model->haveWatchOnly());
connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyLabels(bool)));
}
// update the display unit, to not use the default ("PIV")
updateDisplayUnit();
}
void OverviewPage::updateDisplayUnit()
{
if (walletModel && walletModel->getOptionsModel()) {
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
if (currentBalance != -1)
setBalance(currentBalance, currentUnconfirmedBalance, currentImmatureBalance, currentZerocoinBalance, currentUnconfirmedZerocoinBalance, currentimmatureZerocoinBalance,
currentWatchOnlyBalance, currentWatchUnconfBalance, currentWatchImmatureBalance);
// Update txdelegate->unit with the current unit
txdelegate->unit = nDisplayUnit;
ui->listTransactions->update();
}
}
void OverviewPage::updateAlerts(const QString& warnings)
{
this->ui->labelAlerts->setVisible(!warnings.isEmpty());
this->ui->labelAlerts->setText(warnings);
}
void OverviewPage::showOutOfSyncWarning(bool fShow)
{
ui->labelWalletStatus->setVisible(fShow);
ui->labelTransactionsStatus->setVisible(fShow);
}