comparison 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
comparison
equal deleted inserted replaced
63:3d2decf093bb 64:fe719c109dbc
9 #include <QTimer> 9 #include <QTimer>
10 #include <QVBoxLayout> 10 #include <QVBoxLayout>
11 #include <QWidget> 11 #include <QWidget>
12 #include <sstream> 12 #include <sstream>
13 13
14 StatisticsWidget::StatisticsWidget(QWidget* parent) : QFrame(parent) { 14 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) {
15 setLayout(new QVBoxLayout); 15 QVBoxLayout* layout = new QVBoxLayout(this);
16 16
17 setFrameShape(QFrame::Panel); 17 setFrameShape(QFrame::Box);
18 setFrameShadow(QFrame::Sunken); 18 setFrameShadow(QFrame::Sunken);
19 19
20 QPalette pal = QPalette(); 20 QPalette pal = QPalette();
21 pal.setColor(QPalette::Window, pal.color(QPalette::Base)); 21 pal.setColor(QPalette::Window, pal.color(QPalette::Base));
22 setPalette(pal); 22 setPalette(pal);
30 30
31 TextWidgets::LabelledTextParagraph* application_paragraph = 31 TextWidgets::LabelledTextParagraph* application_paragraph =
32 new TextWidgets::LabelledTextParagraph(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this); 32 new TextWidgets::LabelledTextParagraph(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this);
33 application_data = application_paragraph->GetParagraph(); 33 application_data = application_paragraph->GetParagraph();
34 34
35 layout()->addWidget(anime_list_paragraph); 35 layout->addWidget(anime_list_paragraph);
36 layout()->addWidget(application_paragraph); 36 layout->addWidget(application_paragraph);
37 ((QBoxLayout*)layout())->addStretch(); 37 layout->addStretch();
38 38
39 QTimer* timer = new QTimer(this); 39 QTimer* timer = new QTimer(this);
40 connect(timer, &QTimer::timeout, this, [this] { 40 connect(timer, &QTimer::timeout, this, [this] {
41 if (isVisible()) 41 if (isVisible())
42 UpdateStatistics(); 42 UpdateStatistics();
43 }); 43 });
44 timer->start(1000); // update statistics every second 44 timer->start(1000); // update statistics every second
45 } 45 }
46 46
47 void StatisticsWidget::showEvent(QShowEvent*) { 47 void StatisticsPage::showEvent(QShowEvent*) {
48 UpdateStatistics(); 48 UpdateStatistics();
49 } 49 }
50 50
51 /* me abusing macros :) */ 51 /* me abusing macros :) */
52 #define ADD_TIME_SEGMENT(r, x, s, p) \ 52 #define ADD_TIME_SEGMENT(r, x, s, p) \
53 { \ 53 if (x > 0) \
54 if (x > 0) \ 54 r << x << ((x == 1) ? s : p)
55 r << x << ((x == 1) ? s : p); \ 55 std::string StatisticsPage::MinutesToDateString(int minutes) {
56 }
57 std::string StatisticsWidget::MinutesToDateString(int minutes) {
58 /* ew */ 56 /* ew */
59 int years = (minutes * (1 / 525949.2F)); 57 int years = (minutes * (1 / 525949.2F));
60 int months = (minutes * (1 / 43829.1F)) - (years * 12); 58 int months = (minutes * (1 / 43829.1F)) - (years * 12);
61 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); 59 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F);
62 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); 60 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
69 if (rest_minutes > 0 || return_stream.str().size() == 0) 67 if (rest_minutes > 0 || return_stream.str().size() == 0)
70 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); 68 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes");
71 return return_stream.str(); 69 return return_stream.str();
72 } 70 }
73 71
74 std::string StatisticsWidget::SecondsToDateString(int sec) { 72 std::string StatisticsPage::SecondsToDateString(int sec) {
75 /* this is all fairly unnecessary, but works:tm: */ 73 /* this is all fairly unnecessary, but works:tm: */
76 int years = sec * (1 / 31556952.0F); 74 int years = sec * (1 / 31556952.0F);
77 int months = sec * (1 / 2629746.0F) - (years * 12); 75 int months = sec * (1 / 2629746.0F) - (years * 12);
78 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); 76 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F);
79 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); 77 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
90 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); 88 return_stream << seconds << ((seconds == 1) ? " second" : " seconds");
91 return return_stream.str(); 89 return return_stream.str();
92 } 90 }
93 #undef ADD_TIME_SEGMENT 91 #undef ADD_TIME_SEGMENT
94 92
95 void StatisticsWidget::UpdateStatistics() { 93 void StatisticsPage::UpdateStatistics() {
96 /* Anime list */ 94 /* Anime list */
97 QString string = ""; 95 QString string = "";
98 QTextStream ts(&string); 96 QTextStream ts(&string);
99 ts << Anime::db.GetTotalAnimeAmount() << '\n'; 97 ts << Anime::db.GetTotalAnimeAmount() << '\n';
100 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; 98 ts << Anime::db.GetTotalEpisodeAmount() << '\n';
101 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; 99 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n';
102 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; 100 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n';
103 ts << Anime::db.GetAverageScore() << '\n'; 101 ts << Anime::db.GetAverageScore() << '\n';
104 ts << Anime::db.GetScoreDeviation(); 102 ts << Anime::db.GetScoreDeviation();
105 TextWidgets::SetPlainTextEditData(anime_list_data, string); 103 anime_list_data->SetText(string);
106 104
107 string = ""; 105 string = "";
108 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n'; 106 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n';
109 ts << session.GetRequests(); 107 ts << session.GetRequests();
110 /* Application */ 108 /* Application */
111 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); 109 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
112 TextWidgets::SetPlainTextEditData(application_data, string); 110 application_data->SetText(string);
113 } 111 }
114 112
115 #include "gui/pages/moc_statistics.cpp" 113 #include "gui/pages/moc_statistics.cpp"