Mercurial > minori
annotate src/gui/pages/statistics.cc @ 365:f81bed4e04ac
*: megacommit that probably breaks things
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 02 Oct 2024 23:06:43 -0400 |
parents | 6b0768158dcd |
children |
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" |
305
91ac90a34003
core/time: remove Duration class, use regular functions instead
Paper <paper@paper.us.eu.org>
parents:
295
diff
changeset
|
5 #include "core/time.h" |
9 | 6 #include "gui/pages/anime_list.h" |
258 | 7 #include "gui/widgets/graph.h" |
46 | 8 #include "gui/widgets/text.h" |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
9 |
9 | 10 #include <QString> |
11 #include <QTextDocument> | |
12 #include <QTextStream> | |
13 #include <QTimer> | |
14 #include <QVBoxLayout> | |
15 #include <QWidget> | |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
16 |
258 | 17 #include <cmath> |
9 | 18 #include <sstream> |
250 | 19 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
20 StatisticsPage::StatisticsPage(QWidget* parent) |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
21 : QFrame(parent) { |
295 | 22 setBackgroundRole(QPalette::Base); |
23 | |
64 | 24 QVBoxLayout* layout = new QVBoxLayout(this); |
9 | 25 |
64 | 26 setFrameShape(QFrame::Box); |
9 | 27 setFrameShadow(QFrame::Sunken); |
28 | |
46 | 29 setAutoFillBackground(true); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
30 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
31 const std::vector<std::pair<std::string, std::string>> al_data_template = { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
32 {Strings::Translate("Anime count:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
33 {Strings::Translate("Episode count:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
34 {Strings::Translate("Time spent watching:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
35 {Strings::Translate("Time to complete:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
36 {Strings::Translate("Average score:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
37 {Strings::Translate("Score deviation:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
38 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
39 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
40 _anime_list.GetHeader().SetText(Strings::Translate("Anime List")); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
41 _anime_list.GetContent().SetData(al_data_template); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
42 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
43 layout->addWidget(&_anime_list); |
93 | 44 |
172
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_dist_widget = new QWidget(this); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
47 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); |
9 | 48 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
49 TextWidgets::Header* hdr = new TextWidgets::Header(score_dist_widget); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
50 hdr->SetText(Strings::Translate("Score distribution")); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
51 score_dist_layout->addWidget(hdr); |
172
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 /* Ew */ |
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 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
|
56 QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); |
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
57 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
58 score_parent_layout->addWidget(&_score_distribution_graph); |
172
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_parent_layout->setSpacing(0); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
61 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
|
62 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
63 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
|
64 } |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
65 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
66 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
|
67 |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
68 layout->addWidget(score_dist_widget); |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
69 } |
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
70 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
71 const std::vector<std::pair<std::string, std::string>> app_data_template = { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
72 {Strings::Translate("Uptime:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
73 {Strings::Translate("Requests made:"), ""}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
74 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
75 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
76 _application.GetHeader().SetText(Strings::Translate("Minori")); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
77 _application.GetContent().SetData(app_data_template); |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
78 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
79 layout->addWidget(&_application); |
93 | 80 |
64 | 81 layout->addStretch(); |
9 | 82 |
83 QTimer* timer = new QTimer(this); | |
84 connect(timer, &QTimer::timeout, this, [this] { | |
85 if (isVisible()) | |
86 UpdateStatistics(); | |
87 }); | |
88 timer->start(1000); // update statistics every second | |
89 } | |
90 | |
64 | 91 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 92 UpdateStatistics(); |
93 } | |
94 | |
102 | 95 inline int GetTotalWithScore(const int score) { |
93 | 96 int count = 0; |
97 for (const auto& item : Anime::db.items) | |
98 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
99 count++; | |
100 return count; | |
101 } | |
102 | |
64 | 103 void StatisticsPage::UpdateStatistics() { |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
104 const std::vector<std::pair<std::string, std::string>> al_data = { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
105 {Strings::Translate("Anime count:"), Strings::ToUtf8String(Anime::db.GetTotalAnimeAmount())}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
106 {Strings::Translate("Episode count:"), Strings::ToUtf8String(Anime::db.GetTotalEpisodeAmount())}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
107 {Strings::Translate("Time spent watching:"), Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalWatchedAmount(), 60.0)}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
108 {Strings::Translate("Time to complete:"), Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalPlannedAmount(), 60.0)}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
109 {Strings::Translate("Average score:"), Strings::ToUtf8String(Anime::db.GetAverageScore())}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
110 {Strings::Translate("Score deviation:"), Strings::ToUtf8String(Anime::db.GetScoreDeviation())}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
111 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
112 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
113 _anime_list.GetContent().SetData(al_data); |
93 | 114 |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
115 _score_distribution_graph.Clear(); |
93 | 116 for (int i = 10; i <= 100; i += 10) |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
117 _score_distribution_graph.AddItem(i, GetTotalWithScore(i)); |
9 | 118 |
119 /* Application */ | |
348
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
120 const std::vector<std::pair<std::string, std::string>> app_data_template = { |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
121 {Strings::Translate("Uptime:"), Time::GetSecondsAsAbsoluteString(Time::Units::Seconds, session.uptime() / 1000)}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
122 {Strings::Translate("Requests made:"), Strings::ToUtf8String(session.GetRequests())}, |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
123 }; |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
124 |
6b0768158dcd
text: redesign almost every widget
Paper <paper@paper.us.eu.org>
parents:
305
diff
changeset
|
125 _application.GetContent().SetData(app_data_template); |
9 | 126 } |