Mercurial > minori
annotate src/gui/pages/statistics.cc @ 177:122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Fri, 01 Dec 2023 13:32:29 -0500 |
| parents | 45a0967485f1 |
| children | bc8d2ccff09c |
| 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" |
| 93 | 6 #include "gui/widgets/graph.h" |
| 9 | 7 #include <QString> |
| 8 #include <QTextDocument> | |
| 9 #include <QTextStream> | |
| 10 #include <QTimer> | |
| 11 #include <QVBoxLayout> | |
| 12 #include <QWidget> | |
| 13 #include <sstream> | |
| 14 | |
| 64 | 15 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { |
| 16 QVBoxLayout* layout = new QVBoxLayout(this); | |
| 9 | 17 |
| 64 | 18 setFrameShape(QFrame::Box); |
| 9 | 19 setFrameShadow(QFrame::Sunken); |
| 20 | |
| 46 | 21 setAutoFillBackground(true); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
22 |
| 93 | 23 _anime_list.reset(new TextWidgets::LabelledSection( |
| 51 | 24 tr("Anime list"), |
| 63 | 25 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
| 93 | 26 "", this)); |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
27 layout->addWidget(_anime_list.get()); |
| 93 | 28 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
29 { |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
30 QWidget* score_dist_widget = new QWidget(this); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
31 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); |
| 9 | 32 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
33 score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), score_dist_widget)); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
34 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
35 /* Ew */ |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
36 { |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
37 QWidget* score_graph_parent = new QWidget(score_dist_widget); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
38 QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); |
|
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
39 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
40 _score_distribution_graph.reset(new Graph<int>(score_graph_parent)); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
41 score_parent_layout->addWidget(_score_distribution_graph.get()); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
42 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
43 score_parent_layout->setSpacing(0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
44 score_parent_layout->setContentsMargins(12, 0, 0, 0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
45 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
46 score_graph_parent->setContentsMargins(3, 0, 0, 0); |
| 9 | 47 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
48 score_dist_layout->addWidget(score_graph_parent); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
49 } |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
50 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
51 score_dist_layout->setContentsMargins(0, 0, 0, 0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
52 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
53 layout->addWidget(score_dist_widget); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
54 } |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
55 |
| 93 | 56 |
| 57 _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this)); | |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
58 layout->addWidget(_application.get()); |
| 93 | 59 |
| 64 | 60 layout->addStretch(); |
| 9 | 61 |
| 62 QTimer* timer = new QTimer(this); | |
| 63 connect(timer, &QTimer::timeout, this, [this] { | |
| 64 if (isVisible()) | |
| 65 UpdateStatistics(); | |
| 66 }); | |
| 67 timer->start(1000); // update statistics every second | |
| 68 } | |
| 69 | |
| 64 | 70 void StatisticsPage::showEvent(QShowEvent*) { |
| 9 | 71 UpdateStatistics(); |
| 72 } | |
| 73 | |
| 74 /* me abusing macros :) */ | |
| 15 | 75 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
| 64 | 76 if (x > 0) \ |
| 77 r << x << ((x == 1) ? s : p) | |
| 102 | 78 std::string StatisticsPage::MinutesToDateString(const int minutes) { |
| 9 | 79 /* ew */ |
| 80 int years = (minutes * (1 / 525949.2F)); | |
| 81 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
| 82 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
| 83 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 84 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
| 85 std::ostringstream return_stream; | |
| 86 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
| 87 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 88 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 89 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 90 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
| 91 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
| 92 return return_stream.str(); | |
| 93 } | |
| 94 | |
| 102 | 95 std::string StatisticsPage::SecondsToDateString(const int sec) { |
| 9 | 96 /* this is all fairly unnecessary, but works:tm: */ |
| 10 | 97 int years = sec * (1 / 31556952.0F); |
| 98 int months = sec * (1 / 2629746.0F) - (years * 12); | |
| 99 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
| 100 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 101 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
| 15 | 102 int seconds = |
| 103 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
| 9 | 104 std::ostringstream return_stream; |
| 10 | 105 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
| 106 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 107 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 108 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 109 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
| 110 if (seconds > 0 || return_stream.str().size() == 0) | |
| 111 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
| 9 | 112 return return_stream.str(); |
| 113 } | |
| 114 #undef ADD_TIME_SEGMENT | |
| 115 | |
| 102 | 116 inline int GetTotalWithScore(const int score) { |
| 93 | 117 int count = 0; |
| 118 for (const auto& item : Anime::db.items) | |
| 119 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
| 120 count++; | |
| 121 return count; | |
| 122 } | |
| 123 | |
| 64 | 124 void StatisticsPage::UpdateStatistics() { |
| 9 | 125 /* Anime list */ |
| 126 QString string = ""; | |
| 127 QTextStream ts(&string); | |
| 10 | 128 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
| 129 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
| 130 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
| 131 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
| 132 ts << Anime::db.GetAverageScore() << '\n'; | |
| 133 ts << Anime::db.GetScoreDeviation(); | |
| 93 | 134 _anime_list->GetParagraph()->SetText(string); |
| 135 | |
| 136 _score_distribution_graph->Clear(); | |
| 137 for (int i = 10; i <= 100; i += 10) | |
| 138 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
| 9 | 139 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
140 string = ""; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
141 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
|
142 ts << session.GetRequests(); |
| 9 | 143 /* Application */ |
| 144 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
| 93 | 145 _application->GetParagraph()->SetText(string); |
| 9 | 146 } |
| 147 | |
| 10 | 148 #include "gui/pages/moc_statistics.cpp" |
