Mercurial > minori
annotate src/gui/pages/statistics.cpp @ 39:bed33f89a982
ci/osx: darling is different from wine
darling needs a separate 'shell' ARG
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 22 Sep 2023 09:41:10 -0400 |
parents | 9ae9365dd4ea |
children | d0adc4aedfc8 |
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" |
5 #include "gui/ui_utils.h" | |
6 #include <QString> | |
7 #include <QTextDocument> | |
8 #include <QTextStream> | |
9 #include <QTimer> | |
10 #include <QVBoxLayout> | |
11 #include <QWidget> | |
12 #include <sstream> | |
13 | |
14 StatisticsWidget::StatisticsWidget(QWidget* parent) : QFrame(parent) { | |
15 setLayout(new QVBoxLayout); | |
16 | |
17 setFrameShape(QFrame::Panel); | |
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(); |
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
21 setAutoFillBackground(true); |
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
22 setPalette(pal); |
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
23 |
9 | 24 UiUtils::LabelledTextParagraph* anime_list_paragraph = new UiUtils::LabelledTextParagraph( |
15 | 25 "Anime list", |
26 "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", "\n\n\n\n\n", | |
27 this); | |
9 | 28 anime_list_data = anime_list_paragraph->GetParagraph(); |
29 | |
30 UiUtils::LabelledTextParagraph* application_paragraph = | |
15 | 31 new UiUtils::LabelledTextParagraph("Minori", "Uptime:", "", this); |
9 | 32 application_data = application_paragraph->GetParagraph(); |
33 | |
34 layout()->addWidget(anime_list_paragraph); | |
35 layout()->addWidget(application_paragraph); | |
36 ((QBoxLayout*)layout())->addStretch(); | |
37 | |
38 QTimer* timer = new QTimer(this); | |
39 connect(timer, &QTimer::timeout, this, [this] { | |
40 if (isVisible()) | |
41 UpdateStatistics(); | |
42 }); | |
43 timer->start(1000); // update statistics every second | |
44 } | |
45 | |
46 void StatisticsWidget::showEvent(QShowEvent*) { | |
47 UpdateStatistics(); | |
48 } | |
49 | |
50 /* me abusing macros :) */ | |
15 | 51 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
52 { \ | |
53 if (x > 0) \ | |
54 r << x << ((x == 1) ? s : p); \ | |
55 } | |
9 | 56 std::string StatisticsWidget::MinutesToDateString(int minutes) { |
57 /* ew */ | |
58 int years = (minutes * (1 / 525949.2F)); | |
59 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
60 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
61 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
62 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
63 std::ostringstream return_stream; | |
64 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
65 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
66 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
67 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
68 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
69 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
70 return return_stream.str(); | |
71 } | |
72 | |
10 | 73 std::string StatisticsWidget::SecondsToDateString(int sec) { |
9 | 74 /* this is all fairly unnecessary, but works:tm: */ |
10 | 75 int years = sec * (1 / 31556952.0F); |
76 int months = sec * (1 / 2629746.0F) - (years * 12); | |
77 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
78 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
79 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
15 | 80 int seconds = |
81 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
9 | 82 std::ostringstream return_stream; |
10 | 83 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
84 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
85 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
86 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
87 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
88 if (seconds > 0 || return_stream.str().size() == 0) | |
89 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
9 | 90 return return_stream.str(); |
91 } | |
92 #undef ADD_TIME_SEGMENT | |
93 | |
94 void StatisticsWidget::UpdateStatistics() { | |
95 /* Anime list */ | |
96 QString string = ""; | |
97 QTextStream ts(&string); | |
10 | 98 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
99 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
100 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
101 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
102 ts << Anime::db.GetAverageScore() << '\n'; | |
103 ts << Anime::db.GetScoreDeviation(); | |
9 | 104 UiUtils::SetPlainTextEditData(anime_list_data, string); |
105 | |
106 /* Application */ | |
107 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
108 UiUtils::SetPlainTextEditData(application_data, QString(SecondsToDateString(session.uptime() / 1000).c_str())); | |
109 } | |
110 | |
10 | 111 #include "gui/pages/moc_statistics.cpp" |