Mercurial > minori
annotate src/gui/pages/statistics.cpp @ 47:d8eb763e6661
information.cpp: add widgets to the list tab, and add an
"optional date" widget like taiga has so users can specify whether to
set the date or not
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Mon, 25 Sep 2023 00:43:38 -0400 |
| parents | d0adc4aedfc8 |
| children | e613772f41d5 |
| rev | line source |
|---|---|
| 9 | 1 #include "gui/pages/statistics.h" |
| 15 | 2 #include "core/anime_db.h" |
| 3 #include "core/session.h" | |
| 9 | 4 #include "gui/pages/anime_list.h" |
| 46 | 5 #include "gui/widgets/text.h" |
| 9 | 6 #include <QString> |
| 7 #include <QTextDocument> | |
| 8 #include <QTextStream> | |
| 9 #include <QTimer> | |
| 10 #include <QVBoxLayout> | |
| 11 #include <QWidget> | |
| 12 #include <sstream> | |
| 13 | |
| 14 StatisticsWidget::StatisticsWidget(QWidget* parent) : QFrame(parent) { | |
| 15 setLayout(new QVBoxLayout); | |
| 16 | |
| 17 setFrameShape(QFrame::Panel); | |
| 18 setFrameShadow(QFrame::Sunken); | |
| 19 | |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
20 QPalette pal = QPalette(); |
| 46 | 21 pal.setColor(QPalette::Window, Qt::white); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
22 setPalette(pal); |
| 46 | 23 setAutoFillBackground(true); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
24 |
| 46 | 25 TextWidgets::LabelledTextParagraph* anime_list_paragraph = new TextWidgets::LabelledTextParagraph( |
| 15 | 26 "Anime list", |
| 27 "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", "\n\n\n\n\n", | |
| 28 this); | |
| 9 | 29 anime_list_data = anime_list_paragraph->GetParagraph(); |
| 30 | |
| 46 | 31 TextWidgets::LabelledTextParagraph* application_paragraph = |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
32 new TextWidgets::LabelledTextParagraph("Minori", "Uptime:\nRequests made:", "", this); |
| 9 | 33 application_data = application_paragraph->GetParagraph(); |
| 34 | |
| 35 layout()->addWidget(anime_list_paragraph); | |
| 36 layout()->addWidget(application_paragraph); | |
| 37 ((QBoxLayout*)layout())->addStretch(); | |
| 38 | |
| 39 QTimer* timer = new QTimer(this); | |
| 40 connect(timer, &QTimer::timeout, this, [this] { | |
| 41 if (isVisible()) | |
| 42 UpdateStatistics(); | |
| 43 }); | |
| 44 timer->start(1000); // update statistics every second | |
| 45 } | |
| 46 | |
| 47 void StatisticsWidget::showEvent(QShowEvent*) { | |
| 48 UpdateStatistics(); | |
| 49 } | |
| 50 | |
| 51 /* me abusing macros :) */ | |
| 15 | 52 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
| 53 { \ | |
| 54 if (x > 0) \ | |
| 55 r << x << ((x == 1) ? s : p); \ | |
| 56 } | |
| 9 | 57 std::string StatisticsWidget::MinutesToDateString(int minutes) { |
| 58 /* ew */ | |
| 59 int years = (minutes * (1 / 525949.2F)); | |
| 60 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
| 61 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
| 62 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 63 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
| 64 std::ostringstream return_stream; | |
| 65 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
| 66 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 67 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 68 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 69 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
| 70 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
| 71 return return_stream.str(); | |
| 72 } | |
| 73 | |
| 10 | 74 std::string StatisticsWidget::SecondsToDateString(int sec) { |
| 9 | 75 /* this is all fairly unnecessary, but works:tm: */ |
| 10 | 76 int years = sec * (1 / 31556952.0F); |
| 77 int months = sec * (1 / 2629746.0F) - (years * 12); | |
| 78 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
| 79 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 80 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
| 15 | 81 int seconds = |
| 82 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
| 9 | 83 std::ostringstream return_stream; |
| 10 | 84 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
| 85 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 86 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 87 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 88 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
| 89 if (seconds > 0 || return_stream.str().size() == 0) | |
| 90 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
| 9 | 91 return return_stream.str(); |
| 92 } | |
| 93 #undef ADD_TIME_SEGMENT | |
| 94 | |
| 95 void StatisticsWidget::UpdateStatistics() { | |
| 96 /* Anime list */ | |
| 97 QString string = ""; | |
| 98 QTextStream ts(&string); | |
| 10 | 99 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
| 100 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
| 101 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
| 102 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
| 103 ts << Anime::db.GetAverageScore() << '\n'; | |
| 104 ts << Anime::db.GetScoreDeviation(); | |
| 46 | 105 TextWidgets::SetPlainTextEditData(anime_list_data, string); |
| 9 | 106 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
107 string = ""; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
108 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n'; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
109 ts << session.GetRequests(); |
| 9 | 110 /* Application */ |
| 111 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
112 TextWidgets::SetPlainTextEditData(application_data, string); |
| 9 | 113 } |
| 114 | |
| 10 | 115 #include "gui/pages/moc_statistics.cpp" |
