comparison 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
comparison
equal deleted inserted replaced
7:07a9095eaeed 8:b1f73678ef61
6 #include <QTextDocument> 6 #include <QTextDocument>
7 #include <QVBoxLayout> 7 #include <QVBoxLayout>
8 #include "anime_list.h" 8 #include "anime_list.h"
9 #include "ui_utils.h" 9 #include "ui_utils.h"
10 #include "statistics.h" 10 #include "statistics.h"
11 #include "session.h"
11 12
12 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent) 13 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
13 : QFrame(parent) { 14 : QFrame(parent) {
14 setLayout(new QVBoxLayout); 15 setLayout(new QVBoxLayout);
15 anime_list = listwidget; 16 anime_list = listwidget;
16 17
17 setFrameShape(QFrame::Panel); 18 setFrameShape(QFrame::Panel);
18 setFrameShadow(QFrame::Plain); 19 setFrameShadow(QFrame::Plain);
19 20
20 layout()->addWidget((anime_list_data = UiUtils::CreateTextParagraphWithLabels(this, "Anime list", "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", ""))->parentWidget()->parentWidget()); 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);
22 anime_list_data = anime_list_pg->GetParagraph();
23
24 UiUtils::LabelledTextParagraph* application_pg = new UiUtils::LabelledTextParagraph("Weeaboo", "Uptime:", "", this);
25 application_data = application_pg->GetParagraph();
26
27 layout()->addWidget(anime_list_pg);
28 layout()->addWidget(application_pg);
21 ((QBoxLayout*)layout())->addStretch(); 29 ((QBoxLayout*)layout())->addStretch();
22 30
23 QPalette pal = QPalette(); 31 QPalette pal = QPalette();
24 pal.setColor(QPalette::Window, Qt::white); 32 pal.setColor(QPalette::Window, Qt::white);
25 setAutoFillBackground(true); 33 setAutoFillBackground(true);
33 UpdateStatistics(); 41 UpdateStatistics();
34 }); 42 });
35 timer->start(1000); // update statistics every second 43 timer->start(1000); // update statistics every second
36 } 44 }
37 45
46 #define ADD_TIME_SEGMENT(r, x, s, p) \
47 if (x.count() > 0) \
48 r << x.count() << ((x.count() == 1) ? s : p)
38 std::string StatisticsWidget::MinutesToDateString(int minutes) { 49 std::string StatisticsWidget::MinutesToDateString(int minutes) {
39 /* NOTE: these duration_casts may not be needed... */ 50 /* NOTE: these duration_casts may not be needed... */
40 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes); 51 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
41 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins); 52 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
42 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years); 53 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
43 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months); 54 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
44 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days); 55 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
45 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours); 56 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
46 std::ostringstream return_stream; 57 std::ostringstream return_stream;
47 if (int_years.count() > 0) { 58 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
48 return_stream << int_years.count() << " years "; 59 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
49 } 60 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
50 if (int_months.count() > 0) { 61 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
51 return_stream << int_months.count() << " months "; 62 if (int_minutes.count() > 0 || return_stream.str().size() == 0)
52 } 63 return_stream << int_minutes.count() << ((int_minutes.count() == 1) ? " minute" : " minutes");
53 if (int_days.count() > 0) {
54 return_stream << int_days.count() << " days ";
55 }
56 if (int_hours.count() > 0) {
57 return_stream << int_hours.count() << " hours ";
58 }
59 return_stream << int_minutes.count() << " minutes"; // return minutes anyway
60 return return_stream.str(); 64 return return_stream.str();
61 } 65 }
62 66
67 std::string StatisticsWidget::SecondsToDateString(int seconds) {
68 /* this is all fairly unnecessary, but works:tm: */
69 std::chrono::duration<int, std::ratio<1>> int_total_mins(seconds);
70 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
71 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
72 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
73 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
74 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
75 auto int_seconds = std::chrono::duration_cast<std::chrono::seconds>(int_total_mins-int_years-int_months-int_days-int_hours-int_minutes);
76 std::ostringstream return_stream;
77 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
78 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
79 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
80 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
81 ADD_TIME_SEGMENT(return_stream, int_minutes, " minute ", " minutes ");
82 if (int_seconds.count() > 0 || return_stream.str().size() == 0)
83 return_stream << int_seconds.count() << ((int_seconds.count() == 1) ? " second" : " seconds");
84 return return_stream.str();
85 }
86 #undef ADD_TIME_SEGMENT
87
63 void StatisticsWidget::UpdateStatistics() { 88 void StatisticsWidget::UpdateStatistics() {
89 /* Anime list */
64 QString string = ""; 90 QString string = "";
65 QTextStream ts(&string); 91 QTextStream ts(&string);
66 ts << anime_list->GetTotalAnimeAmount() << '\n'; 92 ts << anime_list->GetTotalAnimeAmount() << '\n';
67 ts << anime_list->GetTotalEpisodeAmount() << '\n'; 93 ts << anime_list->GetTotalEpisodeAmount() << '\n';
68 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n'; 94 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
69 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n'; 95 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
70 ts << anime_list->GetAverageScore() << '\n'; 96 ts << anime_list->GetAverageScore() << '\n';
71 ts << anime_list->GetScoreDeviation() << '\n'; 97 ts << anime_list->GetScoreDeviation() << '\n';
72 UiUtils::SetPlainTextEditData(anime_list_data, string); 98 UiUtils::SetPlainTextEditData(anime_list_data, string);
99
100 /* Application */
101 //UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
102 UiUtils::SetPlainTextEditData(application_data, QString(SecondsToDateString(session.uptime() / 1000).c_str()));
73 } 103 }