Mercurial > minori
annotate src/gui/pages/statistics.cc @ 295:b82841e76e79
*: better support on Windows
things now look much better in dark mode
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Sun, 12 May 2024 20:24:09 -0400 |
parents | 9a88e1725fd2 |
children | 91ac90a34003 |
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) { |
295 | 25 setBackgroundRole(QPalette::Base); |
26 | |
64 | 27 QVBoxLayout* layout = new QVBoxLayout(this); |
9 | 28 |
64 | 29 setFrameShape(QFrame::Box); |
9 | 30 setFrameShadow(QFrame::Sunken); |
31 | |
46 | 32 setAutoFillBackground(true); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
33 |
93 | 34 _anime_list.reset(new TextWidgets::LabelledSection( |
51 | 35 tr("Anime list"), |
63 | 36 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
93 | 37 "", this)); |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
38 layout->addWidget(_anime_list.get()); |
93 | 39 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
40 { |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
41 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
|
42 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); |
9 | 43 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
44 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
|
45 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
46 /* Ew */ |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
47 { |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
48 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
|
49 QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
50 |
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
51 _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
|
52 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
|
53 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
54 score_parent_layout->setSpacing(0); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
55 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
|
56 |
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 _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
|
66 layout->addWidget(_application.get()); |
93 | 67 |
64 | 68 layout->addStretch(); |
9 | 69 |
70 QTimer* timer = new QTimer(this); | |
71 connect(timer, &QTimer::timeout, this, [this] { | |
72 if (isVisible()) | |
73 UpdateStatistics(); | |
74 }); | |
75 timer->start(1000); // update statistics every second | |
76 } | |
77 | |
64 | 78 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 79 UpdateStatistics(); |
80 } | |
81 | |
250 | 82 /* [in] enum TimeUnits unit: |
83 * which unit to stop on | |
84 * [in] int amount: | |
85 * amount of units to parse | |
86 * [in, defaults to 1.0] double unit_in_seconds: | |
87 * equivalent of one of 'amount' in seconds, e.g. minutes would be 60.0 | |
258 | 88 */ |
250 | 89 static std::string TimeToDateString(TimeUnits unit, int amount, double unit_in_seconds = 1.0) { |
90 /* avoid calculating this twice */ | |
91 const double years_conv = (31556952.0 / unit_in_seconds); | |
92 const double months_conv = (2629746.0 / unit_in_seconds); | |
93 const double days_conv = (86400.0 / unit_in_seconds); | |
94 const double hours_conv = (3600.0 / unit_in_seconds); | |
95 const double minutes_conv = (60.0 / unit_in_seconds); | |
96 const double seconds_conv = (1.0 / unit_in_seconds); | |
189 | 97 |
258 | 98 const int years = amount / years_conv; |
99 const int months = std::fmod(amount, years_conv) / months_conv; | |
100 const int days = std::fmod(amount, months_conv) / days_conv; | |
101 const int hours = std::fmod(amount, days_conv) / hours_conv; | |
102 const int minutes = std::fmod(amount, hours_conv) / minutes_conv; | |
250 | 103 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv; |
104 | |
258 | 105 const auto add_time_segment = [](std::ostringstream& str, int amount, const std::string_view& singular, |
106 const std::string_view& plural, bool always = false) { | |
250 | 107 if (amount > 0 || always) |
108 str << amount << ((amount == 1) ? singular : plural); | |
109 }; | |
9 | 110 |
295 | 111 /* for now, this function is very en_US specific */ |
112 | |
250 | 113 std::ostringstream string; |
114 add_time_segment(string, years, " year ", " years "); | |
115 add_time_segment(string, months, " month ", " months "); | |
116 add_time_segment(string, days, " day ", " days "); | |
117 add_time_segment(string, hours, " hour ", " hours "); | |
118 | |
119 if (unit == TimeUnits::MINUTES) { | |
120 add_time_segment(string, minutes, " minute", " minutes", true); | |
121 return string.str(); | |
122 } else { | |
123 add_time_segment(string, minutes, " minute ", " minutes "); | |
124 } | |
125 | |
126 add_time_segment(string, seconds, " second", " seconds", true); | |
127 return string.str(); | |
9 | 128 } |
129 | |
102 | 130 inline int GetTotalWithScore(const int score) { |
93 | 131 int count = 0; |
132 for (const auto& item : Anime::db.items) | |
133 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
134 count++; | |
135 return count; | |
136 } | |
137 | |
64 | 138 void StatisticsPage::UpdateStatistics() { |
9 | 139 /* Anime list */ |
140 QString string = ""; | |
141 QTextStream ts(&string); | |
10 | 142 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
143 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
250 | 144 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; |
145 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; | |
10 | 146 ts << Anime::db.GetAverageScore() << '\n'; |
147 ts << Anime::db.GetScoreDeviation(); | |
291 | 148 _anime_list->GetData()->setText(string); |
93 | 149 |
150 _score_distribution_graph->Clear(); | |
151 for (int i = 10; i <= 100; i += 10) | |
152 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
9 | 153 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
154 string = ""; |
250 | 155 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
|
156 ts << session.GetRequests(); |
9 | 157 /* Application */ |
158 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
291 | 159 _application->GetData()->setText(string); |
9 | 160 } |