Skip to content

Commit b862f6a

Browse files
committed
dashboard stakes chart connected (main logic left)
1 parent 1304c66 commit b862f6a

File tree

5 files changed

+151
-63
lines changed

5 files changed

+151
-63
lines changed

src/qt/pivx/dashboardwidget.cpp

Lines changed: 109 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "qt/pivx/sendconfirmdialog.h"
44
#include "qt/pivx/txrow.h"
55
#include "qt/pivx/qtutils.h"
6+
#include "guiutil.h"
67
#include "walletmodel.h"
78
#include "optionsmodel.h"
89
#include "qt/pivx/settings/settingsfaqwidget.h"
@@ -15,16 +16,17 @@
1516
#include <QPaintEngine>
1617
#include <QGraphicsDropShadowEffect>
1718
#include <iostream>
19+
#include <cstdlib>
20+
21+
#include <QMap>
1822

19-
/*
2023
// Chart
2124
#include <QtCharts/QChartView>
2225
#include <QtCharts/QBarSeries>
2326
#include <QtCharts/QBarSet>
2427
#include <QtCharts/QLegend>
2528
#include <QtCharts/QBarCategoryAxis>
2629
#include <QtCharts/QValueAxis>
27-
*/
2830
#include <QGraphicsLayout>
2931

3032
#define DECORATION_SIZE 65
@@ -158,13 +160,11 @@ DashboardWidget::DashboardWidget(PIVXGUI* _window, QWidget *parent) :
158160
//ui->pushImgEmptyChart->setProperty("cssClass", "img-empty-staking-off");
159161
//ui->labelEmptyChart->setText("Staking off");
160162

161-
ui->labelEmptyChart->setText("Staking on");
163+
ui->labelEmptyChart->setText("Staking off");
162164
ui->labelEmptyChart->setProperty("cssClass", "text-empty");
163165

164166
ui->labelMessageEmpty->setText("You can activate and deactivate the Staking mode in the status bar at the top right of the wallet");
165167
ui->labelMessageEmpty->setProperty("cssClass", "text-subtitle");
166-
// load chart
167-
loadChart();
168168

169169
// Chart State
170170
ui->layoutChart->setVisible(false);
@@ -192,68 +192,117 @@ void DashboardWidget::handleTransactionClicked(const QModelIndex &index){
192192

193193
}
194194

195-
void DashboardWidget::loadChart(){
196-
/*
195+
void DashboardWidget::initChart() {
197196
set0 = new QBarSet("PIV");
198197
set0->setColor(QColor(176,136,255));
199198

200199
set1 = new QBarSet("zPIV");
201200
set1->setColor(QColor(92,75,125));
202201

203-
*set0 << 4 << 2 << 4 << 6;
204-
*set1 << 6 << 9 << 3 << 6;
202+
chart = new QChart();
203+
}
204+
205+
const char * monthsNames[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
205206

206-
QBarSeries *series = new QBarSeries();
207-
series->append(set0);
208-
series->append(set1);
207+
void DashboardWidget::loadChart(){
209208

210-
// bar width
211-
series->setBarWidth(0.8);
209+
int size = stakesFilter->rowCount();
210+
if (size > 0) {
211+
ui->layoutChart->setVisible(true);
212+
ui->emptyContainerChart->setVisible(false);
213+
initChart();
214+
215+
// pair PIV, zPIV
216+
QMap<int, std::pair<qint64, qint64>> amountByMonths;
217+
// get all of the stakes
218+
for (int i = 0; i < size; ++i) {
219+
QModelIndex modelIndex = stakesFilter->index(i, TransactionTableModel::ToAddress);
220+
qint64 amount = llabs(modelIndex.data(TransactionTableModel::AmountRole).toLongLong());
221+
QDateTime datetime = modelIndex.data(TransactionTableModel::DateRole).toDateTime();
222+
bool isPiv = modelIndex.data(TransactionTableModel::TypeRole).toInt() != TransactionRecord::StakeZPIV;
223+
int month = datetime.date().month();
224+
if (amountByMonths.contains(month)) {
225+
if (isPiv) {
226+
amountByMonths[month].first += amount;
227+
} else
228+
amountByMonths[month].second += amount;
229+
} else {
230+
if (isPiv) {
231+
amountByMonths[month] = std::make_pair(amount, 0);
232+
} else {
233+
amountByMonths[month] = std::make_pair(0, amount);
234+
}
235+
}
212236

213-
chart = new QChart();
214-
chart->addSeries(series);
215-
// title
216-
//chart->setTitle("Simple barchart example");
217-
chart->setAnimationOptions(QChart::SeriesAnimations);
218-
219-
QStringList categories;
220-
categories << "Jan" << "Feb" << "Mar" << "Apr";
221-
axisX = new QBarCategoryAxis();
222-
axisX->append(categories);
223-
chart->addAxis(axisX, Qt::AlignBottom);
224-
series->attachAxis(axisX);
225-
226-
axisY = new QValueAxis();
227-
axisY->setRange(0,12);
228-
chart->addAxis(axisY, Qt::AlignRight);
229-
series->attachAxis(axisY);
230-
231-
// Legend
232-
chart->legend()->setVisible(false);
233-
chart->legend()->setAlignment(Qt::AlignTop);
234-
235-
QChartView *chartView = new QChartView(chart);
236-
chartView->setRenderHint(QPainter::Antialiasing);
237-
238-
QVBoxLayout *baseScreensContainer = new QVBoxLayout(this);
239-
baseScreensContainer->setMargin(0);
240-
ui->chartContainer->setLayout(baseScreensContainer);
241-
242-
ui->chartContainer->layout()->addWidget(chartView);
243-
ui->chartContainer->setProperty("cssClass", "container-chart");
237+
}
238+
239+
QStringList months;
240+
qreal maxValue = 0;
241+
for (int j = 12; j > 6; j--) {
242+
qreal piv = 0;
243+
qreal zpiv = 0;
244+
if (amountByMonths.contains(j)) {
245+
std::pair<qint64, qint64> pair = amountByMonths[j];
246+
piv = (pair.first != 0) ? pair.first / 100000000 : 0;
247+
zpiv = (pair.second != 0) ? pair.second / 100000000 : 0;
248+
}
249+
months << monthsNames[j - 1];
250+
set0->append(piv);
251+
set1->append(zpiv);
252+
253+
int max = std::max(piv, zpiv);
254+
if (max > maxValue) {
255+
maxValue = max;
256+
}
257+
}
244258

245-
// Set colors
246-
changeChartColors();
259+
QBarSeries *series = new QtCharts::QBarSeries();
260+
series->append(set0);
261+
series->append(set1);
247262

248-
// Chart margin removed.
249-
chart->layout()->setContentsMargins(0, 0, 0, 0);
250-
chart->setBackgroundRoundness(0);
251-
*/
263+
// bar width
264+
series->setBarWidth(0.8);
265+
266+
chart->addSeries(series);
267+
chart->setAnimationOptions(QChart::SeriesAnimations);
268+
269+
axisX = new QBarCategoryAxis();
270+
axisX->append(months);
271+
chart->addAxis(axisX, Qt::AlignBottom);
272+
series->attachAxis(axisX);
273+
274+
axisY = new QValueAxis();
275+
axisY->setRange(0,maxValue);
276+
chart->addAxis(axisY, Qt::AlignRight);
277+
series->attachAxis(axisY);
278+
279+
// Legend
280+
chart->legend()->setVisible(false);
281+
chart->legend()->setAlignment(Qt::AlignTop);
282+
283+
QChartView *chartView = new QChartView(chart);
284+
chartView->setRenderHint(QPainter::Antialiasing);
285+
286+
QVBoxLayout *baseScreensContainer = new QVBoxLayout(this);
287+
baseScreensContainer->setMargin(0);
288+
ui->chartContainer->setLayout(baseScreensContainer);
289+
290+
ui->chartContainer->layout()->addWidget(chartView);
291+
ui->chartContainer->setProperty("cssClass", "container-chart");
292+
293+
// Set colors
294+
changeChartColors();
295+
296+
// Chart margin removed.
297+
chart->layout()->setContentsMargins(0, 0, 0, 0);
298+
chart->setBackgroundRoundness(0);
299+
} else {
300+
ui->layoutChart->setVisible(false);
301+
ui->emptyContainerChart->setVisible(true);
302+
}
252303
}
253304

254305
void DashboardWidget::changeChartColors(){
255-
/*
256-
// Colors
257306
QColor gridLineColorX;
258307
QColor linePenColorY;
259308
QColor backgroundColor;
@@ -275,7 +324,6 @@ void DashboardWidget::changeChartColors(){
275324
chart->setBackgroundBrush(QBrush(backgroundColor));
276325
set0->setBorderColor(gridLineColorX);
277326
set1->setBorderColor(gridLineColorX);
278-
*/
279327
}
280328

281329
void DashboardWidget::loadWalletModel(){
@@ -300,6 +348,13 @@ void DashboardWidget::loadWalletModel(){
300348
connect(ui->pushImgEmpty, SIGNAL(clicked()), this, SLOT(openFAQ()));
301349
connect(ui->btnHowTo, SIGNAL(clicked()), this, SLOT(openFAQ()));
302350
}
351+
352+
// chart filter
353+
stakesFilter = new TransactionFilterProxy();
354+
stakesFilter->setSourceModel(txModel);
355+
stakesFilter->sort(TransactionTableModel::Date, Qt::DescendingOrder);
356+
stakesFilter->setOnlyStakes(true);
357+
loadChart();
303358
}
304359
// update the display unit, to not use the default ("PIV")
305360
updateDisplayUnit();

src/qt/pivx/dashboardwidget.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
#include <QWidget>
1212
#include <QLineEdit>
13-
/*
14-
#include <QBarCategoryAxis>
15-
#include <QBarSet>
16-
#include <QChart>
17-
#include <QValueAxis>
1813

19-
QT_CHARTS_USE_NAMESPACE
20-
*/
14+
#include <QtCharts/QBarCategoryAxis>
15+
#include <QtCharts/QBarSet>
16+
#include <QtCharts/QChart>
17+
#include <QtCharts/QValueAxis>
18+
19+
QT_CHARTS_USE_NAMESPACE
20+
21+
using namespace QtCharts;
2122

2223
class PIVXGUI;
2324
class WalletModel;
@@ -74,11 +75,12 @@ private slots:
7475
// Painter delegate
7576
FurAbstractListItemDelegate* txViewDelegate;
7677
TransactionFilterProxy* filter;
78+
TransactionFilterProxy* stakesFilter;
7779
TxViewHolder* txHolder;
7880
TransactionTableModel* txModel;
7981
int nDisplayUnit = -1;
8082

81-
/*
83+
8284
// Chart
8385
QBarSet *set0;
8486
QBarSet *set1;
@@ -87,7 +89,8 @@ private slots:
8789
QValueAxis *axisY;
8890

8991
QChart *chart;
90-
*/
92+
93+
void initChart();
9194
};
9295

9396
#endif // DASHBOARDWIDGET_H

src/qt/transactionfilterproxy.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ bool TransactionFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex&
6161
if (fOnlyZc && !isZcTx(type)){
6262
return false;
6363
}
64+
if (fOnlyStakes && !isStakeTx(type))
65+
return false;
6466

6567
return true;
6668
}
@@ -118,6 +120,11 @@ void TransactionFilterProxy::setShowZcTxes(bool fOnlyZc){
118120
invalidateFilter();
119121
}
120122

123+
void TransactionFilterProxy::setOnlyStakes(bool fOnlyStakes){
124+
this->fOnlyStakes = fOnlyStakes;
125+
invalidateFilter();
126+
}
127+
121128
int TransactionFilterProxy::rowCount(const QModelIndex& parent) const
122129
{
123130
if (limitRows != -1) {
@@ -138,3 +145,13 @@ bool TransactionFilterProxy::isZcTx(int type) const {
138145
return (type == TransactionRecord::ZerocoinMint || type == TransactionRecord::ZerocoinSpend || type == TransactionRecord::ZerocoinSpend_Change_zPiv
139146
|| type == TransactionRecord::ZerocoinSpend_FromMe || type == TransactionRecord::RecvFromZerocoinSpend);
140147
}
148+
149+
bool TransactionFilterProxy::isStakeTx(int type) const {
150+
return (type == TransactionRecord::StakeMint || type == TransactionRecord::Generated || type == TransactionRecord::StakeZPIV);
151+
}
152+
153+
/*QVariant TransactionFilterProxy::dataFromSourcePos(int sourceRow, int role) const {
154+
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
155+
return index.data(index, role);
156+
}
157+
*/

src/qt/transactionfilterproxy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ class TransactionFilterProxy : public QSortFilterProxyModel
5757
/** Only zc txes **/
5858
void setShowZcTxes(bool fOnlyZc);
5959

60+
/** Only stakes txes **/
61+
void setOnlyStakes(bool fOnlyStakes);
62+
6063
int rowCount(const QModelIndex& parent = QModelIndex()) const;
6164
static bool isOrphan(const int status, const int type);
6265

66+
//QVariant dataFromSourcePos(int sourceRow, int role) const;
67+
6368
protected:
6469
bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
6570

@@ -74,8 +79,10 @@ class TransactionFilterProxy : public QSortFilterProxyModel
7479
bool showInactive;
7580
bool fHideOrphans = true;
7681
bool fOnlyZc = false;
82+
bool fOnlyStakes = false;
7783

7884
bool isZcTx(int type) const;
85+
bool isStakeTx(int type) const;
7986
};
8087

8188
#endif // BITCOIN_QT_TRANSACTIONFILTERPROXY_H

src/qt/transactionrecord.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet*
238238
if (wallet->IsDenominatedAmount(txout.nValue)) sub.type = TransactionRecord::ObfuscationCreateDenominations;
239239
if (nDebit - wtx.GetValueOut() == OBFUSCATION_COLLATERAL) sub.type = TransactionRecord::ObfuscationCollateralPayment;
240240
}
241+
242+
// Label for payment to self
243+
CTxDestination address;
244+
if (ExtractDestination(wtx.vout[0].scriptPubKey, address)) {
245+
sub.address = CBitcoinAddress(address).ToString();
246+
}
241247
}
242248

243249
CAmount nChange = wtx.GetChange();

0 commit comments

Comments
 (0)