99#include " qt/pivx/settings/settingsfaqwidget.h"
1010#include < QPainter>
1111#include < QModelIndex>
12- #include < QMap>
1312#include < QList>
1413
1514// Chart
@@ -73,13 +72,19 @@ DashboardWidget::DashboardWidget(PIVXGUI* _window, QWidget *parent) :
7372 ui->labelAmountPiv ->setProperty (" cssClass" , " text-stake-piv-disable" );
7473 ui->labelAmountZpiv ->setProperty (" cssClass" , " text-stake-zpiv-disable" );
7574
76- ui->pushButtonHour ->setProperty (" cssClass" , " btn-check-time" );
77- ui->pushButtonDay ->setProperty (" cssClass" , " btn-check-time" );
78- ui->pushButtonWeek ->setProperty (" cssClass" , " btn-check-time" );
75+ ui->pushButtonAll ->setProperty (" cssClass" , " btn-check-time" );
7976 ui->pushButtonMonth ->setProperty (" cssClass" , " btn-check-time" );
8077 ui->pushButtonYear ->setProperty (" cssClass" , " btn-check-time" );
78+ ui->comboBoxMonths ->setProperty (" cssClass" , " btn-combo" );
79+ ui->comboBoxYears ->setProperty (" cssClass" , " btn-combo" );
80+ ui->comboBoxMonths ->setView (new QListView ());
81+ ui->comboBoxMonths ->setStyleSheet (" selection-background-color:transparent; selection-color:transparent;" );
82+ ui->comboBoxYears ->setView (new QListView ());
83+ ui->comboBoxYears ->setStyleSheet (" selection-background-color:transparent; selection-color:transparent;" );
8184 ui->pushButtonYear ->setChecked (true );
8285
86+ connect (ui->comboBoxYears , SIGNAL (currentIndexChanged (const QString&)), this ,SLOT (onChartYearChanged (const QString&)));
87+
8388 // Sort Transactions
8489 ui->comboBoxSort ->setProperty (" cssClass" , " btn-combo" );
8590 ui->comboBoxSort ->setEditable (true );
@@ -143,15 +148,18 @@ DashboardWidget::DashboardWidget(PIVXGUI* _window, QWidget *parent) :
143148 if (window)
144149 connect (window, SIGNAL (windowResizeEvent (QResizeEvent*)), this , SLOT (windowResizeEvent (QResizeEvent*)));
145150
146- connect (ui->pushButtonYear , &QPushButton::clicked, [this ](){
147- this ->chartShow =0 ;
148- refreshChart ();
151+ connect (ui->pushButtonYear , &QPushButton::clicked, [this ](){setChartShow (YEAR);});
152+ connect (ui->pushButtonMonth , &QPushButton::clicked, [this ](){setChartShow (MONTH);});
153+ connect (ui->pushButtonAll , &QPushButton::clicked, [this ](){
154+ yearFilter = 0 ;
155+ monthFilter = 0 ;
156+ setChartShow (ALL);
149157 });
158+ }
150159
151- connect (ui->pushButtonMonth , &QPushButton::clicked, [this ](){
152- this ->chartShow =2 ;
153- refreshChart ();
154- });
160+ void DashboardWidget::setChartShow (ChartShowType type) {
161+ this ->chartShow = type;
162+ refreshChart ();
155163}
156164
157165void DashboardWidget::handleTransactionClicked (const QModelIndex &index){
@@ -208,6 +216,7 @@ void DashboardWidget::openFAQ(){
208216 showHideOp (true );
209217 SettingsFaqWidget* dialog = new SettingsFaqWidget (window);
210218 openDialogWithOpaqueBackgroundFullScreen (dialog, window);
219+ dialog->deleteLater ();
211220}
212221
213222void DashboardWidget::onTxArrived () {
@@ -259,16 +268,23 @@ void DashboardWidget::changeTheme(bool isLightTheme, QString& theme){
259268 this ->changeChartColors ();
260269}
261270
271+ const char * monthsNames[12 ] = {" Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" , " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" };
272+
262273void DashboardWidget::loadChart (){
263274 int size = stakesFilter->rowCount ();
264275 if (size > 0 ) {
265276 if (!chart) {
266277 ui->layoutChart ->setVisible (true );
267278 ui->emptyContainerChart ->setVisible (false );
268279 initChart ();
280+ for (int i = 0 ; i < 12 ; ++i) {
281+ ui->comboBoxMonths ->addItem (QString (monthsNames[i]), QVariant (i + 1 ));
282+ }
283+ connect (ui->comboBoxMonths , SIGNAL (currentIndexChanged (const QString&)), this ,SLOT (onChartMonthChanged (const QString&)));
269284 }
270285 refreshChart ();
271286 changeChartColors ();
287+ isChartInitialized = true ;
272288 } else {
273289 ui->layoutChart ->setVisible (false );
274290 ui->emptyContainerChart ->setVisible (true );
@@ -326,51 +342,119 @@ void DashboardWidget::changeChartColors(){
326342
327343// pair PIV, zPIV
328344QMap<int , std::pair<qint64, qint64>> DashboardWidget::getAmountBy () {
345+ bool filterByMonth = false ;
346+ if (monthFilter != 0 && chartShow == MONTH) {
347+ filterByMonth = true ;
348+ }
349+ if (yearFilter != 0 ) {
350+ if (filterByMonth) {
351+ QDate monthFirst = QDate (yearFilter, monthFilter, 1 );
352+ stakesFilter->setDateRange (
353+ QDateTime (monthFirst),
354+ QDateTime (QDate (yearFilter, monthFilter, monthFirst.daysInMonth ()))
355+ );
356+ } else {
357+ stakesFilter->setDateRange (
358+ QDateTime (QDate (yearFilter, 1 , 1 )),
359+ QDateTime (QDate (yearFilter, 12 , 31 ))
360+ );
361+ }
362+ } else if (filterByMonth){
363+ QDate currentDate = QDate::currentDate ();
364+ QDate monthFirst = QDate (currentDate.year (), monthFilter, 1 );
365+ stakesFilter->setDateRange (
366+ QDateTime (monthFirst),
367+ QDateTime (QDate (currentDate.year (), monthFilter, monthFirst.daysInMonth ()))
368+ );
369+ ui->comboBoxYears ->setCurrentText (QString::number (currentDate.year ()));
370+ } else {
371+ stakesFilter->clearDateRange ();
372+ }
329373 int size = stakesFilter->rowCount ();
330- QMap<int , std::pair<qint64, qint64>> amountByMonths ;
374+ QMap<int , std::pair<qint64, qint64>> amountBy ;
331375 // Get all of the stakes
332376 for (int i = 0 ; i < size; ++i) {
333377 QModelIndex modelIndex = stakesFilter->index (i, TransactionTableModel::ToAddress);
334378 qint64 amount = llabs (modelIndex.data (TransactionTableModel::AmountRole).toLongLong ());
335379 QDateTime datetime = modelIndex.data (TransactionTableModel::DateRole).toDateTime ();
336380 bool isPiv = modelIndex.data (TransactionTableModel::TypeRole).toInt () != TransactionRecord::StakeZPIV;
337381
338- int month = 0 ;
382+ /* *
383+ * If this is ALL, order this by years.
384+ * To do that, the amountBy map is a map of:
385+ * year --> pair<PIV,zPIV>
386+ * ---
387+ * If this is YEAR, show the 12 months of the year (need a filter by year).
388+ * To do that, the amountBy map is a map of:
389+ * month --> pair<PIV,zPIV>
390+ * ---
391+ * If this is MONTH, order this by days (need a filter by month and year).
392+ * To do that, then amountBy map is a map of:
393+ * day --> pair<PIV,zPIV>
394+ * ---
395+ * If this is WEEK, order this by weeks (need a filter by year, month and week number).
396+ * To do that, the amountBy map is a map of:
397+ * week num --> pair<PIV,zPIV>
398+ * ---
399+ * If this is DAY, order this by hours (need a filter by year, month and day).
400+ * To do that, the amountBy map is a map of:
401+ * day --> pair<PIV,zPIV>
402+ */
403+
404+ int time = 0 ;
339405 switch (chartShow) {
340- case 0 : {
341- month = datetime.date ().month ();
406+ case YEAR : {
407+ time = datetime.date ().month ();
342408 break ;
343409 }
344- case 1 : {
345- month = datetime.date ().year ();
410+ case ALL : {
411+ time = datetime.date ().year ();
346412 break ;
347413 }
348- case 2 : {
349- month = datetime.date ().day ();
414+ case MONTH : {
415+ time = datetime.date ().day ();
350416 break ;
351417 }
352418 default :
353419 inform (tr (" Error loading chart, invalid show option" ));
354- return amountByMonths ;
420+ return amountBy ;
355421 }
356- if (amountByMonths .contains (month )) {
422+ if (amountBy .contains (time )) {
357423 if (isPiv) {
358- amountByMonths[month ].first += amount;
424+ amountBy[time ].first += amount;
359425 } else
360- amountByMonths[month ].second += amount;
426+ amountBy[time ].second += amount;
361427 } else {
362428 if (isPiv) {
363- amountByMonths[month ] = std::make_pair (amount, 0 );
429+ amountBy[time ] = std::make_pair (amount, 0 );
364430 } else {
365- amountByMonths[month ] = std::make_pair (0 , amount);
431+ amountBy[time ] = std::make_pair (0 , amount);
366432 hasZpivStakes = true ;
367433 }
368434 }
369435 }
370- return amountByMonths ;
436+ return amountBy ;
371437}
372438
373- const char * monthsNames[12 ] = {" Jan" , " Feb" , " Mar" , " Apr" , " May" , " Jun" , " Jul" , " Aug" , " Sep" , " Oct" , " Nov" , " Dec" };
439+ void DashboardWidget::onChartYearChanged (const QString& yearStr) {
440+ if (isChartInitialized) {
441+ int newYear = (yearStr == " All" ) ? 0 : yearStr.toInt ();
442+ if (newYear != yearFilter) {
443+ yearFilter = newYear;
444+ refreshChart ();
445+ }
446+ }
447+ }
448+
449+ void DashboardWidget::onChartMonthChanged (const QString& monthStr) {
450+ if (isChartInitialized) {
451+ int newMonth = (monthStr == " All" ) ? 0 : ui->comboBoxMonths ->currentData ().toInt ();
452+ if (newMonth != monthFilter) {
453+ monthFilter = newMonth;
454+ refreshChart ();
455+ }
456+ }
457+ }
374458
375459void DashboardWidget::refreshChart (){
376460 if (chart) {
@@ -389,11 +473,11 @@ void DashboardWidget::refreshChart(){
389473 series->attachAxis (axisY);
390474
391475 // pair PIV, zPIV
392- QMap< int , std::pair<qint64, qint64>> amountByMonths = getAmountBy ();
476+ amountsByCache = getAmountBy ();
393477
394478 QStringList months;
395479 isChartMin = width () < 1350 ;
396- bool withMonthNames = !isChartMin && (chartShow == 0 );
480+ bool withMonthNames = !isChartMin && (chartShow == YEAR );
397481
398482 qreal maxValue = 0 ;
399483 qint64 totalPiv = 0 ;
@@ -402,12 +486,12 @@ void DashboardWidget::refreshChart(){
402486 QList<qreal> valuesPiv;
403487 QList<qreal> valueszPiv;
404488
405- std::pair<int ,int > range = getChartRange ();
489+ std::pair<int ,int > range = getChartRange (amountsByCache );
406490 for (int j = range.first ; j < range.second ; j++) {
407491 qreal piv = 0 ;
408492 qreal zpiv = 0 ;
409- if (amountByMonths .contains (j)) {
410- std::pair<qint64, qint64> pair = amountByMonths [j];
493+ if (amountsByCache .contains (j)) {
494+ std::pair<qint64, qint64> pair = amountsByCache [j];
411495 piv = (pair.first != 0 ) ? pair.first / 100000000 : 0 ;
412496 zpiv = (pair.second != 0 ) ? pair.second / 100000000 : 0 ;
413497 totalPiv += pair.first ;
@@ -446,22 +530,70 @@ void DashboardWidget::refreshChart(){
446530 chart->addSeries (series);
447531
448532 // bar width
449- if (chartShow == 0 )
533+ if (chartShow == YEAR )
450534 series->setBarWidth (0.8 );
451535 else {
452536 series->setBarWidth (0.3 );
453537 }
454538 axisX->append (months);
455539 axisY->setRange (0 ,maxValue);
540+
541+ // Controllers
542+ switch (chartShow) {
543+ case ALL: {
544+ ui->container_chart_dropboxes ->setVisible (false );
545+ break ;
546+ }
547+ case YEAR: {
548+ ui->container_chart_dropboxes ->setVisible (true );
549+ ui->containerBoxMonths ->setVisible (false );
550+ break ;
551+ }
552+ case MONTH: {
553+ ui->container_chart_dropboxes ->setVisible (true );
554+ ui->containerBoxMonths ->setVisible (true );
555+ break ;
556+ }
557+ default :
558+ // add day.
559+ break ;
560+ }
561+
562+ // Refresh years filter, first address created is the start
563+ int yearStart = QDateTime::fromTime_t (static_cast <uint>(walletModel->getCreationTime ())).date ().year ();
564+ int currentYear = QDateTime::currentDateTime ().date ().year ();
565+
566+ QString selection;
567+ if (ui->comboBoxYears ->count () > 0 ) {
568+ selection = ui->comboBoxYears ->currentText ();
569+ isChartInitialized = false ;
570+ }
571+ ui->comboBoxYears ->clear ();
572+ ui->comboBoxYears ->addItem (" All" );
573+ if (yearStart == currentYear) {
574+ ui->comboBoxYears ->addItem (QString::number (currentYear));
575+ } else {
576+ for (int i = yearStart; i < (currentYear + 1 ); ++i)ui->comboBoxYears ->addItem (QString::number (i));
577+ }
578+
579+ if (!selection.isEmpty ()) {
580+ ui->comboBoxYears ->setCurrentText (selection);
581+ isChartInitialized = true ;
582+ } else {
583+ ui->comboBoxYears ->setCurrentText (" All" );
584+ }
456585}
457586
458- std::pair<int , int > DashboardWidget::getChartRange () {
587+ std::pair<int , int > DashboardWidget::getChartRange (QMap< int , std::pair<qint64, qint64>> amountsBy ) {
459588 switch (chartShow) {
460- case 0 :
589+ case YEAR :
461590 return std::make_pair (1 , 13 );
462- case 1 :
463- return std::make_pair (1 , 4 );
464- case 2 :
591+ case ALL: {
592+ QList<int > keys = amountsBy.uniqueKeys ();
593+ qSort (keys);
594+ return std::make_pair (keys.first (), keys.last () + 1 );
595+ }
596+ case MONTH:
465597 return std::make_pair (1 , 32 );
466598 default :
467599 inform (tr (" Error loading chart, invalid show option" ));
@@ -472,8 +604,8 @@ std::pair<int, int> DashboardWidget::getChartRange() {
472604void DashboardWidget::updateAxisX (const char *arg[]) {
473605 axisX->clear ();
474606 QStringList months;
475- std::pair<int ,int > range = getChartRange ();
476- for (int i = range.first ; i < getChartRange () .second ; i++) months << ((arg) ? arg[i-1 ] : QString::number (i));
607+ std::pair<int ,int > range = getChartRange (amountsByCache );
608+ for (int i = range.first ; i < range .second ; i++) months << ((arg) ? arg[i-1 ] : QString::number (i));
477609 axisX->append (months);
478610}
479611
@@ -483,15 +615,15 @@ void DashboardWidget::windowResizeEvent(QResizeEvent *event){
483615 if (isChartMin) {
484616 isChartMin = false ;
485617 switch (chartShow) {
486- case 0 : {
618+ case YEAR : {
487619 updateAxisX (monthsNames);
488620 break ;
489621 }
490- case 1 : {
622+ case ALL : {
491623 // TODO: Complete me..
492624 break ;
493625 }
494- case 2 : {
626+ case MONTH : {
495627 updateAxisX ();
496628 break ;
497629 }
0 commit comments