Mercurial > minori
annotate src/gui/pages/statistics.cpp @ 64:fe719c109dbc
*: update
1. add media tracking ability, and it displays info on the `now playing` page
2. the `now playing` page now actually shows something
3. renamed every page class to be more accurate to what it is
4. ...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 01 Oct 2023 23:15:43 -0400 |
parents | 3d2decf093bb |
children | d3e9310598b1 |
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" |
9 | 6 #include <QString> |
7 #include <QTextDocument> | |
8 #include <QTextStream> | |
9 #include <QTimer> | |
10 #include <QVBoxLayout> | |
11 #include <QWidget> | |
12 #include <sstream> | |
13 | |
64 | 14 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { |
15 QVBoxLayout* layout = new QVBoxLayout(this); | |
9 | 16 |
64 | 17 setFrameShape(QFrame::Box); |
9 | 18 setFrameShadow(QFrame::Sunken); |
19 | |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
20 QPalette pal = QPalette(); |
49 | 21 pal.setColor(QPalette::Window, pal.color(QPalette::Base)); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
22 setPalette(pal); |
46 | 23 setAutoFillBackground(true); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
24 |
46 | 25 TextWidgets::LabelledTextParagraph* anime_list_paragraph = new TextWidgets::LabelledTextParagraph( |
51 | 26 tr("Anime list"), |
63 | 27 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
28 "\n\n\n\n\n\n", this); | |
9 | 29 anime_list_data = anime_list_paragraph->GetParagraph(); |
30 | |
46 | 31 TextWidgets::LabelledTextParagraph* application_paragraph = |
51 | 32 new TextWidgets::LabelledTextParagraph(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this); |
9 | 33 application_data = application_paragraph->GetParagraph(); |
34 | |
64 | 35 layout->addWidget(anime_list_paragraph); |
36 layout->addWidget(application_paragraph); | |
37 layout->addStretch(); | |
9 | 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 | |
64 | 47 void StatisticsPage::showEvent(QShowEvent*) { |
9 | 48 UpdateStatistics(); |
49 } | |
50 | |
51 /* me abusing macros :) */ | |
15 | 52 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
64 | 53 if (x > 0) \ |
54 r << x << ((x == 1) ? s : p) | |
55 std::string StatisticsPage::MinutesToDateString(int minutes) { | |
9 | 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 | |
64 | 72 std::string StatisticsPage::SecondsToDateString(int sec) { |
9 | 73 /* this is all fairly unnecessary, but works:tm: */ |
10 | 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); | |
15 | 79 int seconds = |
80 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
9 | 81 std::ostringstream return_stream; |
10 | 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"); | |
9 | 89 return return_stream.str(); |
90 } | |
91 #undef ADD_TIME_SEGMENT | |
92 | |
64 | 93 void StatisticsPage::UpdateStatistics() { |
9 | 94 /* Anime list */ |
95 QString string = ""; | |
96 QTextStream ts(&string); | |
10 | 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(); | |
64 | 103 anime_list_data->SetText(string); |
9 | 104 |
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
105 string = ""; |
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
106 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
|
107 ts << session.GetRequests(); |
9 | 108 /* Application */ |
109 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
64 | 110 application_data->SetText(string); |
9 | 111 } |
112 | |
10 | 113 #include "gui/pages/moc_statistics.cpp" |