comparison src/gui/pages/statistics.cpp @ 9:5c0397762b53

INCOMPLETE: megacommit :)
author Paper <mrpapersonic@gmail.com>
date Sun, 10 Sep 2023 03:59:16 -0400
parents
children 4b198a111713
comparison
equal deleted inserted replaced
8:b1f73678ef61 9:5c0397762b53
1 #include "gui/pages/statistics.h"
2 #include "gui/pages/anime_list.h"
3 #include "gui/ui_utils.h"
4 #include "session.h"
5 #include <QString>
6 #include <QTextDocument>
7 #include <QTextStream>
8 #include <QTimer>
9 #include <QVBoxLayout>
10 #include <QWidget>
11 #include <sstream>
12
13 StatisticsWidget::StatisticsWidget(QWidget* parent) : QFrame(parent) {
14 setLayout(new QVBoxLayout);
15
16 setFrameShape(QFrame::Panel);
17 setFrameShadow(QFrame::Sunken);
18
19 UiUtils::LabelledTextParagraph* anime_list_paragraph = new UiUtils::LabelledTextParagraph(
20 "Anime list",
21 "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", "",
22 this);
23 anime_list_data = anime_list_paragraph->GetParagraph();
24
25 UiUtils::LabelledTextParagraph* application_paragraph =
26 new UiUtils::LabelledTextParagraph("Weeaboo", "Uptime:", "", this);
27 application_data = application_paragraph->GetParagraph();
28
29 layout()->addWidget(anime_list_paragraph);
30 layout()->addWidget(application_paragraph);
31 ((QBoxLayout*)layout())->addStretch();
32
33 QPalette pal = QPalette();
34 pal.setColor(QPalette::Window, Qt::white);
35 setAutoFillBackground(true);
36 setPalette(pal);
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 :) */
51 #define ADD_TIME_SEGMENT(r, x, s, p) \
52 if (x > 0) \
53 r << x << ((x == 1) ? s : p)
54 std::string StatisticsWidget::MinutesToDateString(int minutes) {
55 /* ew */
56 int years = (minutes * (1 / 525949.2F));
57 int months = (minutes * (1 / 43829.1F)) - (years * 12);
58 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F);
59 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
60 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60);
61 std::ostringstream return_stream;
62 ADD_TIME_SEGMENT(return_stream, years, " year ", " years ");
63 ADD_TIME_SEGMENT(return_stream, months, " month ", " months ");
64 ADD_TIME_SEGMENT(return_stream, days, " day ", " days ");
65 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours ");
66 if (rest_minutes > 0 || return_stream.str().size() == 0)
67 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes");
68 return return_stream.str();
69 }
70
71 std::string StatisticsWidget::SecondsToDateString(int seconds) {
72 /* this is all fairly unnecessary, but works:tm: */
73 std::chrono::duration<int, std::ratio<1>> int_total_mins(seconds);
74 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
75 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins - int_years);
76 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins - int_years - int_months);
77 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins - int_years - int_months - int_days);
78 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins - int_years - int_months -
79 int_days - int_hours);
80 auto int_seconds = std::chrono::duration_cast<std::chrono::seconds>(int_total_mins - int_years - int_months -
81 int_days - int_hours - int_minutes);
82 std::ostringstream return_stream;
83 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
84 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
85 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
86 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
87 ADD_TIME_SEGMENT(return_stream, int_minutes, " minute ", " minutes ");
88 if (int_seconds.count() > 0 || return_stream.str().size() == 0)
89 return_stream << int_seconds.count() << ((int_seconds.count() == 1) ? " second" : " seconds");
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);
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();
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
111 #include "gui/pages/moc_statistics.h"