annotate src/pages/statistics.cpp @ 8:b1f73678ef61

update text paragraphs are now their own objects, as they should be
author Paper <mrpapersonic@gmail.com>
date Sat, 26 Aug 2023 03:39:34 -0400
parents 07a9095eaeed
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
1 #include <sstream>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
2 #include <QWidget>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
3 #include <QTimer>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
4 #include <QTextStream>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
5 #include <QString>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
6 #include <QTextDocument>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
7 #include <QVBoxLayout>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
8 #include "anime_list.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 #include "ui_utils.h"
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
10 #include "statistics.h"
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
11 #include "session.h"
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
12
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
13 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
14 : QFrame(parent) {
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
15 setLayout(new QVBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
16 anime_list = listwidget;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
17
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
18 setFrameShape(QFrame::Panel);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
19 setFrameShadow(QFrame::Plain);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
20
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
21 UiUtils::LabelledTextParagraph* anime_list_pg = new UiUtils::LabelledTextParagraph("Anime list", "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", "", this);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
22 anime_list_data = anime_list_pg->GetParagraph();
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
23
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
24 UiUtils::LabelledTextParagraph* application_pg = new UiUtils::LabelledTextParagraph("Weeaboo", "Uptime:", "", this);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
25 application_data = application_pg->GetParagraph();
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
26
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
27 layout()->addWidget(anime_list_pg);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
28 layout()->addWidget(application_pg);
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
29 ((QBoxLayout*)layout())->addStretch();
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
31 QPalette pal = QPalette();
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
32 pal.setColor(QPalette::Window, Qt::white);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
33 setAutoFillBackground(true);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
34 setPalette(pal);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
35
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
36 UpdateStatistics(); // load in statistics as soon as possible
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
37
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
38 QTimer* timer = new QTimer(this);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
39 connect(timer, &QTimer::timeout, this, [this]{
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
40 if (isVisible())
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
41 UpdateStatistics();
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
42 });
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
43 timer->start(1000); // update statistics every second
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
46 #define ADD_TIME_SEGMENT(r, x, s, p) \
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
47 if (x.count() > 0) \
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
48 r << x.count() << ((x.count() == 1) ? s : p)
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
49 std::string StatisticsWidget::MinutesToDateString(int minutes) {
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
50 /* NOTE: these duration_casts may not be needed... */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
51 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
52 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
54 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 std::ostringstream return_stream;
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
58 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
59 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
60 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
61 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
62 if (int_minutes.count() > 0 || return_stream.str().size() == 0)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
63 return_stream << int_minutes.count() << ((int_minutes.count() == 1) ? " minute" : " minutes");
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
64 return return_stream.str();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
65 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
66
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
67 std::string StatisticsWidget::SecondsToDateString(int seconds) {
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
68 /* this is all fairly unnecessary, but works:tm: */
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
69 std::chrono::duration<int, std::ratio<1>> int_total_mins(seconds);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
70 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
71 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
72 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
73 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
74 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
75 auto int_seconds = std::chrono::duration_cast<std::chrono::seconds>(int_total_mins-int_years-int_months-int_days-int_hours-int_minutes);
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
76 std::ostringstream return_stream;
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
77 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
78 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
79 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
80 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
81 ADD_TIME_SEGMENT(return_stream, int_minutes, " minute ", " minutes ");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
82 if (int_seconds.count() > 0 || return_stream.str().size() == 0)
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
83 return_stream << int_seconds.count() << ((int_seconds.count() == 1) ? " second" : " seconds");
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
84 return return_stream.str();
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
85 }
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
86 #undef ADD_TIME_SEGMENT
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
87
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
88 void StatisticsWidget::UpdateStatistics() {
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
89 /* Anime list */
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
90 QString string = "";
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
91 QTextStream ts(&string);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
92 ts << anime_list->GetTotalAnimeAmount() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
93 ts << anime_list->GetTotalEpisodeAmount() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
94 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
95 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
96 ts << anime_list->GetAverageScore() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
97 ts << anime_list->GetScoreDeviation() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
98 UiUtils::SetPlainTextEditData(anime_list_data, string);
8
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
99
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
100 /* Application */
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
101 //UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
Paper <mrpapersonic@gmail.com>
parents: 7
diff changeset
102 UiUtils::SetPlainTextEditData(application_data, QString(SecondsToDateString(session.uptime() / 1000).c_str()));
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
103 }