7
|
1 #include <sstream>
|
|
2 #include <QWidget>
|
|
3 #include <QTimer>
|
|
4 #include <QTextStream>
|
|
5 #include <QString>
|
|
6 #include <QTextDocument>
|
|
7 #include <QVBoxLayout>
|
|
8 #include "anime_list.h"
|
2
|
9 #include "ui_utils.h"
|
7
|
10 #include "statistics.h"
|
8
|
11 #include "session.h"
|
7
|
12
|
|
13 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
|
|
14 : QFrame(parent) {
|
|
15 setLayout(new QVBoxLayout);
|
|
16 anime_list = listwidget;
|
2
|
17
|
7
|
18 setFrameShape(QFrame::Panel);
|
|
19 setFrameShadow(QFrame::Plain);
|
|
20
|
8
|
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);
|
7
|
29 ((QBoxLayout*)layout())->addStretch();
|
2
|
30
|
7
|
31 QPalette pal = QPalette();
|
|
32 pal.setColor(QPalette::Window, Qt::white);
|
|
33 setAutoFillBackground(true);
|
|
34 setPalette(pal);
|
|
35
|
|
36 UpdateStatistics(); // load in statistics as soon as possible
|
|
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
|
2
|
44 }
|
|
45
|
8
|
46 #define ADD_TIME_SEGMENT(r, x, s, p) \
|
|
47 if (x.count() > 0) \
|
|
48 r << x.count() << ((x.count() == 1) ? s : p)
|
7
|
49 std::string StatisticsWidget::MinutesToDateString(int minutes) {
|
2
|
50 /* NOTE: these duration_casts may not be needed... */
|
|
51 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
|
|
52 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
|
|
53 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
|
|
54 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
|
|
55 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
|
|
56 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
|
|
57 std::ostringstream return_stream;
|
8
|
58 ADD_TIME_SEGMENT(return_stream, int_years, " year ", " years ");
|
|
59 ADD_TIME_SEGMENT(return_stream, int_months, " month ", " months ");
|
|
60 ADD_TIME_SEGMENT(return_stream, int_days, " day ", " days ");
|
|
61 ADD_TIME_SEGMENT(return_stream, int_hours, " hour ", " hours ");
|
|
62 if (int_minutes.count() > 0 || return_stream.str().size() == 0)
|
|
63 return_stream << int_minutes.count() << ((int_minutes.count() == 1) ? " minute" : " minutes");
|
2
|
64 return return_stream.str();
|
|
65 }
|
|
66
|
8
|
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
|
7
|
88 void StatisticsWidget::UpdateStatistics() {
|
8
|
89 /* Anime list */
|
7
|
90 QString string = "";
|
|
91 QTextStream ts(&string);
|
|
92 ts << anime_list->GetTotalAnimeAmount() << '\n';
|
|
93 ts << anime_list->GetTotalEpisodeAmount() << '\n';
|
|
94 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
|
|
95 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
|
|
96 ts << anime_list->GetAverageScore() << '\n';
|
|
97 ts << anime_list->GetScoreDeviation() << '\n';
|
|
98 UiUtils::SetPlainTextEditData(anime_list_data, string);
|
8
|
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()));
|
2
|
103 }
|