Mercurial > minori
annotate src/gui/pages/statistics.cc @ 93:d5efb81540b3
statistics: add graph!
also now we have a VERY VERY simple graph widget that probably
sucks and doesn't work half the time :)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 01 Nov 2023 13:52:34 -0400 |
parents | d02fdf1d6708 |
children | 2f373d48f889 |
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 | |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
21 QPalette pal = QPalette(); |
49 | 22 pal.setColor(QPalette::Window, pal.color(QPalette::Base)); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
23 setPalette(pal); |
46 | 24 setAutoFillBackground(true); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
25 |
93 | 26 _anime_list.reset(new TextWidgets::LabelledSection( |
51 | 27 tr("Anime list"), |
63 | 28 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
93 | 29 "", this)); |
30 | |
31 /* spaghetti incoming */ | |
32 QWidget* score_dist_widget = new QWidget(this); | |
33 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); | |
9 | 34 |
93 | 35 QWidget* content = new QWidget(score_dist_widget); |
36 QVBoxLayout* content_layout = new QVBoxLayout(content); | |
37 score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), content)); | |
38 _score_distribution_graph.reset(new Graph<int>(content)); | |
39 content_layout->addWidget(_score_distribution_graph.get()); | |
40 content_layout->setContentsMargins(0, 0, 0, 0); | |
41 content->setContentsMargins(12, 0, 0, 0); | |
9 | 42 |
93 | 43 score_dist_layout->addWidget(content); |
44 score_dist_layout->setContentsMargins(0, 0, 0, 0); | |
45 | |
46 _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this)); | |
47 | |
48 layout->addWidget(_anime_list.get()); | |
49 layout->addWidget(score_dist_widget); | |
50 layout->addWidget(_application.get()); | |
64 | 51 layout->addStretch(); |
9 | 52 |
53 QTimer* timer = new QTimer(this); | |
54 connect(timer, &QTimer::timeout, this, [this] { | |
55 if (isVisible()) | |
56 UpdateStatistics(); | |
57 }); | |
58 timer->start(1000); // update statistics every second | |
59 } | |
60 | |
64 | 61 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 62 UpdateStatistics(); |
63 } | |
64 | |
65 /* me abusing macros :) */ | |
15 | 66 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
64 | 67 if (x > 0) \ |
68 r << x << ((x == 1) ? s : p) | |
69 std::string StatisticsPage::MinutesToDateString(int minutes) { | |
9 | 70 /* ew */ |
71 int years = (minutes * (1 / 525949.2F)); | |
72 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
73 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
74 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
75 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
76 std::ostringstream return_stream; | |
77 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
78 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
79 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
80 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
81 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
82 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
83 return return_stream.str(); | |
84 } | |
85 | |
64 | 86 std::string StatisticsPage::SecondsToDateString(int sec) { |
9 | 87 /* this is all fairly unnecessary, but works:tm: */ |
10 | 88 int years = sec * (1 / 31556952.0F); |
89 int months = sec * (1 / 2629746.0F) - (years * 12); | |
90 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
91 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
92 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
15 | 93 int seconds = |
94 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
9 | 95 std::ostringstream return_stream; |
10 | 96 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
97 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
98 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
99 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
100 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
101 if (seconds > 0 || return_stream.str().size() == 0) | |
102 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
9 | 103 return return_stream.str(); |
104 } | |
105 #undef ADD_TIME_SEGMENT | |
106 | |
93 | 107 inline int GetTotalWithScore(int score) { |
108 int count = 0; | |
109 for (const auto& item : Anime::db.items) | |
110 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
111 count++; | |
112 return count; | |
113 } | |
114 | |
64 | 115 void StatisticsPage::UpdateStatistics() { |
9 | 116 /* Anime list */ |
117 QString string = ""; | |
118 QTextStream ts(&string); | |
10 | 119 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
120 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
121 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
122 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
123 ts << Anime::db.GetAverageScore() << '\n'; | |
124 ts << Anime::db.GetScoreDeviation(); | |
93 | 125 _anime_list->GetParagraph()->SetText(string); |
126 | |
127 _score_distribution_graph->Clear(); | |
128 for (int i = 10; i <= 100; i += 10) | |
129 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
9 | 130 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
131 string = ""; |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
132 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
|
133 ts << session.GetRequests(); |
9 | 134 /* Application */ |
135 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
93 | 136 _application->GetParagraph()->SetText(string); |
9 | 137 } |
138 | |
10 | 139 #include "gui/pages/moc_statistics.cpp" |