Mercurial > minori
annotate src/gui/pages/statistics.cc @ 250:c130f47f6f48
*: many many changes
e.g. the search page is actually implemented now!
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 04 Feb 2024 21:17:17 -0500 |
parents | 4d461ef7d424 |
children | d14f8e0e40c3 |
rev | line source |
---|---|
9 | 1 #include "gui/pages/statistics.h" |
15 | 2 #include "core/anime_db.h" |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
3 #include "core/strings.h" |
15 | 4 #include "core/session.h" |
9 | 5 #include "gui/pages/anime_list.h" |
46 | 6 #include "gui/widgets/text.h" |
93 | 7 #include "gui/widgets/graph.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 |
9 | 16 #include <sstream> |
250 | 17 #include <cmath> |
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_graph_parent->setContentsMargins(3, 0, 0, 0); |
9 | 56 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
57 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
|
58 } |
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 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
|
61 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
62 layout->addWidget(score_dist_widget); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
63 } |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
64 |
93 | 65 |
66 _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
|
67 layout->addWidget(_application.get()); |
93 | 68 |
64 | 69 layout->addStretch(); |
9 | 70 |
71 QTimer* timer = new QTimer(this); | |
72 connect(timer, &QTimer::timeout, this, [this] { | |
73 if (isVisible()) | |
74 UpdateStatistics(); | |
75 }); | |
76 timer->start(1000); // update statistics every second | |
77 } | |
78 | |
64 | 79 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 80 UpdateStatistics(); |
81 } | |
82 | |
250 | 83 /* [in] enum TimeUnits unit: |
84 * which unit to stop on | |
85 * [in] int amount: | |
86 * amount of units to parse | |
87 * [in, defaults to 1.0] double unit_in_seconds: | |
88 * equivalent of one of 'amount' in seconds, e.g. minutes would be 60.0 | |
89 */ | |
90 static std::string TimeToDateString(TimeUnits unit, int amount, double unit_in_seconds = 1.0) { | |
91 /* avoid calculating this twice */ | |
92 const double years_conv = (31556952.0 / unit_in_seconds); | |
93 const double months_conv = (2629746.0 / unit_in_seconds); | |
94 const double days_conv = (86400.0 / unit_in_seconds); | |
95 const double hours_conv = (3600.0 / unit_in_seconds); | |
96 const double minutes_conv = (60.0 / unit_in_seconds); | |
97 const double seconds_conv = (1.0 / unit_in_seconds); | |
189 | 98 |
250 | 99 const int years = amount / years_conv; |
100 const int months = std::fmod(amount, years_conv) / months_conv; | |
101 const int days = std::fmod(amount, months_conv) / days_conv; | |
102 const int hours = std::fmod(amount, days_conv) / hours_conv; | |
103 const int minutes = std::fmod(amount, hours_conv) / minutes_conv; | |
104 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; | |
105 | |
106 const auto add_time_segment = [](std::ostringstream& str, int amount, const std::string_view& singular, const std::string_view& plural, bool always = false) { | |
107 if (amount > 0 || always) | |
108 str << amount << ((amount == 1) ? singular : plural); | |
109 }; | |
9 | 110 |
250 | 111 std::ostringstream string; |
112 add_time_segment(string, years, " year ", " years "); | |
113 add_time_segment(string, months, " month ", " months "); | |
114 add_time_segment(string, days, " day ", " days "); | |
115 add_time_segment(string, hours, " hour ", " hours "); | |
116 | |
117 if (unit == TimeUnits::MINUTES) { | |
118 add_time_segment(string, minutes, " minute", " minutes", true); | |
119 return string.str(); | |
120 } else { | |
121 add_time_segment(string, minutes, " minute ", " minutes "); | |
122 } | |
123 | |
124 add_time_segment(string, seconds, " second", " seconds", true); | |
125 return string.str(); | |
9 | 126 } |
127 | |
102 | 128 inline int GetTotalWithScore(const int score) { |
93 | 129 int count = 0; |
130 for (const auto& item : Anime::db.items) | |
131 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
132 count++; | |
133 return count; | |
134 } | |
135 | |
64 | 136 void StatisticsPage::UpdateStatistics() { |
9 | 137 /* Anime list */ |
138 QString string = ""; | |
139 QTextStream ts(&string); | |
10 | 140 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
141 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
250 | 142 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; |
143 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; | |
10 | 144 ts << Anime::db.GetAverageScore() << '\n'; |
145 ts << Anime::db.GetScoreDeviation(); | |
93 | 146 _anime_list->GetParagraph()->SetText(string); |
147 | |
148 _score_distribution_graph->Clear(); | |
149 for (int i = 10; i <= 100; i += 10) | |
150 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
9 | 151 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
152 string = ""; |
250 | 153 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
|
154 ts << session.GetRequests(); |
9 | 155 /* Application */ |
156 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
93 | 157 _application->GetParagraph()->SetText(string); |
9 | 158 } |