comparison src/gui/pages/statistics.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/gui/pages/statistics.cpp@d3e9310598b1
children d02fdf1d6708
comparison
equal deleted inserted replaced
80:825506f0e221 81:9b2b41f83a5e
1 #include "gui/pages/statistics.h"
2 #include "core/anime_db.h"
3 #include "core/session.h"
4 #include "gui/pages/anime_list.h"
5 #include "gui/widgets/text.h"
6 #include <QString>
7 #include <QTextDocument>
8 #include <QTextStream>
9 #include <QTimer>
10 #include <QVBoxLayout>
11 #include <QWidget>
12 #include <sstream>
13
14 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) {
15 QVBoxLayout* layout = new QVBoxLayout(this);
16
17 setFrameShape(QFrame::Box);
18 setFrameShadow(QFrame::Sunken);
19
20 QPalette pal = QPalette();
21 pal.setColor(QPalette::Window, pal.color(QPalette::Base));
22 setPalette(pal);
23 setAutoFillBackground(true);
24
25 TextWidgets::LabelledSection* anime_list_paragraph = new TextWidgets::LabelledSection(
26 tr("Anime list"),
27 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"),
28 "\n\n\n\n\n\n", this);
29 anime_list_data = anime_list_paragraph->GetParagraph();
30
31 TextWidgets::LabelledSection* application_paragraph =
32 new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this);
33 application_data = application_paragraph->GetParagraph();
34
35 layout->addWidget(anime_list_paragraph);
36 layout->addWidget(application_paragraph);
37 layout->addStretch();
38
39 QTimer* timer = new QTimer(this);
40 connect(timer, &QTimer::timeout, this, [this] {
41 if (isVisible())
42 UpdateStatistics();
43 });
44 timer->start(1000); // update statistics every second
45 }
46
47 void StatisticsPage::showEvent(QShowEvent*) {
48 UpdateStatistics();
49 }
50
51 /* me abusing macros :) */
52 #define ADD_TIME_SEGMENT(r, x, s, p) \
53 if (x > 0) \
54 r << x << ((x == 1) ? s : p)
55 std::string StatisticsPage::MinutesToDateString(int minutes) {
56 /* ew */
57 int years = (minutes * (1 / 525949.2F));
58 int months = (minutes * (1 / 43829.1F)) - (years * 12);
59 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F);
60 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
61 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60);
62 std::ostringstream return_stream;
63 ADD_TIME_SEGMENT(return_stream, years, " year ", " years ");
64 ADD_TIME_SEGMENT(return_stream, months, " month ", " months ");
65 ADD_TIME_SEGMENT(return_stream, days, " day ", " days ");
66 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours ");
67 if (rest_minutes > 0 || return_stream.str().size() == 0)
68 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes");
69 return return_stream.str();
70 }
71
72 std::string StatisticsPage::SecondsToDateString(int sec) {
73 /* this is all fairly unnecessary, but works:tm: */
74 int years = sec * (1 / 31556952.0F);
75 int months = sec * (1 / 2629746.0F) - (years * 12);
76 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F);
77 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
78 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F);
79 int seconds =
80 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F);
81 std::ostringstream return_stream;
82 ADD_TIME_SEGMENT(return_stream, years, " year ", " years ");
83 ADD_TIME_SEGMENT(return_stream, months, " month ", " months ");
84 ADD_TIME_SEGMENT(return_stream, days, " day ", " days ");
85 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours ");
86 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes ");
87 if (seconds > 0 || return_stream.str().size() == 0)
88 return_stream << seconds << ((seconds == 1) ? " second" : " seconds");
89 return return_stream.str();
90 }
91 #undef ADD_TIME_SEGMENT
92
93 void StatisticsPage::UpdateStatistics() {
94 /* Anime list */
95 QString string = "";
96 QTextStream ts(&string);
97 ts << Anime::db.GetTotalAnimeAmount() << '\n';
98 ts << Anime::db.GetTotalEpisodeAmount() << '\n';
99 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n';
100 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n';
101 ts << Anime::db.GetAverageScore() << '\n';
102 ts << Anime::db.GetScoreDeviation();
103 anime_list_data->SetText(string);
104
105 string = "";
106 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n';
107 ts << session.GetRequests();
108 /* Application */
109 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
110 application_data->SetText(string);
111 }
112
113 #include "gui/pages/moc_statistics.cpp"