Mercurial > minori
annotate src/gui/pages/statistics.cc @ 258:862d0d8619f6
*: HUUUGE changes
animia has been renamed to animone, so instead of thinking of a
health condition, you think of a beautiful flower :)
I've also edited some of the code for animone, but I have no idea
if it even works or not because I don't have a mac or windows
machine lying around. whoops!
... anyway, all of the changes divergent from Anisthesia are now
licensed under BSD. it's possible that I could even rewrite most
of the code to where I don't even have to keep the MIT license,
but that's thinking too far into the future
I've been slacking off on implementing the anime seasons page,
mostly out of laziness. I think I'd have to create another db file
specifically for the seasons
anyway, this code is being pushed *primarily* because the hard drive
it's on is failing! yay :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 01 Apr 2024 02:43:44 -0400 |
parents | d14f8e0e40c3 |
children | 9a88e1725fd2 |
rev | line source |
---|---|
9 | 1 #include "gui/pages/statistics.h" |
15 | 2 #include "core/anime_db.h" |
258 | 3 #include "core/session.h" |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
4 #include "core/strings.h" |
9 | 5 #include "gui/pages/anime_list.h" |
258 | 6 #include "gui/widgets/graph.h" |
46 | 7 #include "gui/widgets/text.h" |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
8 |
9 | 9 #include <QString> |
10 #include <QTextDocument> | |
11 #include <QTextStream> | |
12 #include <QTimer> | |
13 #include <QVBoxLayout> | |
14 #include <QWidget> | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
15 |
258 | 16 #include <cmath> |
9 | 17 #include <sstream> |
250 | 18 |
19 enum class TimeUnits { | |
20 SECONDS, | |
21 MINUTES | |
22 }; | |
9 | 23 |
64 | 24 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { |
25 QVBoxLayout* layout = new QVBoxLayout(this); | |
9 | 26 |
64 | 27 setFrameShape(QFrame::Box); |
9 | 28 setFrameShadow(QFrame::Sunken); |
29 | |
46 | 30 setAutoFillBackground(true); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
31 |
93 | 32 _anime_list.reset(new TextWidgets::LabelledSection( |
51 | 33 tr("Anime list"), |
63 | 34 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
93 | 35 "", this)); |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
36 layout->addWidget(_anime_list.get()); |
93 | 37 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
38 { |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
39 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
|
40 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); |
9 | 41 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
42 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
|
43 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
44 /* Ew */ |
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 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
|
47 QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
48 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
49 _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
|
50 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
|
51 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
52 score_parent_layout->setSpacing(0); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
53 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
|
54 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
55 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
|
56 } |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
57 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
58 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
|
59 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
60 layout->addWidget(score_dist_widget); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
61 } |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
62 |
93 | 63 _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
|
64 layout->addWidget(_application.get()); |
93 | 65 |
64 | 66 layout->addStretch(); |
9 | 67 |
68 QTimer* timer = new QTimer(this); | |
69 connect(timer, &QTimer::timeout, this, [this] { | |
70 if (isVisible()) | |
71 UpdateStatistics(); | |
72 }); | |
73 timer->start(1000); // update statistics every second | |
74 } | |
75 | |
64 | 76 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 77 UpdateStatistics(); |
78 } | |
79 | |
250 | 80 /* [in] enum TimeUnits unit: |
81 * which unit to stop on | |
82 * [in] int amount: | |
83 * amount of units to parse | |
84 * [in, defaults to 1.0] double unit_in_seconds: | |
85 * equivalent of one of 'amount' in seconds, e.g. minutes would be 60.0 | |
258 | 86 */ |
250 | 87 static std::string TimeToDateString(TimeUnits unit, int amount, double unit_in_seconds = 1.0) { |
88 /* avoid calculating this twice */ | |
89 const double years_conv = (31556952.0 / unit_in_seconds); | |
90 const double months_conv = (2629746.0 / unit_in_seconds); | |
91 const double days_conv = (86400.0 / unit_in_seconds); | |
92 const double hours_conv = (3600.0 / unit_in_seconds); | |
93 const double minutes_conv = (60.0 / unit_in_seconds); | |
94 const double seconds_conv = (1.0 / unit_in_seconds); | |
189 | 95 |
258 | 96 const int years = amount / years_conv; |
97 const int months = std::fmod(amount, years_conv) / months_conv; | |
98 const int days = std::fmod(amount, months_conv) / days_conv; | |
99 const int hours = std::fmod(amount, days_conv) / hours_conv; | |
100 const int minutes = std::fmod(amount, hours_conv) / minutes_conv; | |
250 | 101 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; |
102 | |
258 | 103 const auto add_time_segment = [](std::ostringstream& str, int amount, const std::string_view& singular, |
104 const std::string_view& plural, bool always = false) { | |
250 | 105 if (amount > 0 || always) |
106 str << amount << ((amount == 1) ? singular : plural); | |
107 }; | |
9 | 108 |
250 | 109 std::ostringstream string; |
110 add_time_segment(string, years, " year ", " years "); | |
111 add_time_segment(string, months, " month ", " months "); | |
112 add_time_segment(string, days, " day ", " days "); | |
113 add_time_segment(string, hours, " hour ", " hours "); | |
114 | |
115 if (unit == TimeUnits::MINUTES) { | |
116 add_time_segment(string, minutes, " minute", " minutes", true); | |
117 return string.str(); | |
118 } else { | |
119 add_time_segment(string, minutes, " minute ", " minutes "); | |
120 } | |
121 | |
122 add_time_segment(string, seconds, " second", " seconds", true); | |
123 return string.str(); | |
9 | 124 } |
125 | |
102 | 126 inline int GetTotalWithScore(const int score) { |
93 | 127 int count = 0; |
128 for (const auto& item : Anime::db.items) | |
129 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
130 count++; | |
131 return count; | |
132 } | |
133 | |
64 | 134 void StatisticsPage::UpdateStatistics() { |
9 | 135 /* Anime list */ |
136 QString string = ""; | |
137 QTextStream ts(&string); | |
10 | 138 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
139 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
250 | 140 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; |
141 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; | |
10 | 142 ts << Anime::db.GetAverageScore() << '\n'; |
143 ts << Anime::db.GetScoreDeviation(); | |
93 | 144 _anime_list->GetParagraph()->SetText(string); |
145 | |
146 _score_distribution_graph->Clear(); | |
147 for (int i = 10; i <= 100; i += 10) | |
148 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
9 | 149 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
150 string = ""; |
250 | 151 ts << Strings::ToQString(TimeToDateString(TimeUnits::SECONDS, session.uptime() / 1000)) << '\n'; |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
152 ts << session.GetRequests(); |
9 | 153 /* Application */ |
154 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
93 | 155 _application->GetParagraph()->SetText(string); |
9 | 156 } |