Mercurial > minori
annotate src/gui/pages/statistics.cc @ 106:c8c72278f6fd
*: #if -> #ifdef, remove outdated comments in sys/win32/dark_theme.cc
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 05 Nov 2023 04:01:58 -0500 |
| parents | 6d8da6e64d61 |
| children | 45a0967485f1 |
| rev | line source |
|---|---|
| 9 | 1 #include "gui/pages/statistics.h" |
| 15 | 2 #include "core/anime_db.h" |
| 3 #include "core/session.h" | |
| 9 | 4 #include "gui/pages/anime_list.h" |
| 46 | 5 #include "gui/widgets/text.h" |
| 93 | 6 #include "gui/widgets/graph.h" |
| 9 | 7 #include <QString> |
| 8 #include <QTextDocument> | |
| 9 #include <QTextStream> | |
| 10 #include <QTimer> | |
| 11 #include <QVBoxLayout> | |
| 12 #include <QWidget> | |
| 13 #include <sstream> | |
| 14 | |
| 64 | 15 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { |
| 16 QVBoxLayout* layout = new QVBoxLayout(this); | |
| 9 | 17 |
| 64 | 18 setFrameShape(QFrame::Box); |
| 9 | 19 setFrameShadow(QFrame::Sunken); |
| 20 | |
| 46 | 21 setAutoFillBackground(true); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
22 |
| 93 | 23 _anime_list.reset(new TextWidgets::LabelledSection( |
| 51 | 24 tr("Anime list"), |
| 63 | 25 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
| 93 | 26 "", this)); |
| 27 | |
| 28 QWidget* score_dist_widget = new QWidget(this); | |
| 29 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); | |
| 9 | 30 |
|
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
31 score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), score_dist_widget)); |
|
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
32 |
|
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
33 /* I have to explain myself here: I hate this :). This makes a widget as a layer to create a margin, |
|
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
34 similar to what I do in text.cc with sections. I hate it and it should really be put into a separate |
|
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
35 class, but whatever. */ |
| 93 | 36 QWidget* content = new QWidget(score_dist_widget); |
| 37 QVBoxLayout* content_layout = new QVBoxLayout(content); | |
| 38 _score_distribution_graph.reset(new Graph<int>(content)); | |
| 39 content_layout->addWidget(_score_distribution_graph.get()); | |
| 102 | 40 content_layout->setSpacing(0); |
| 41 /* For some reason, when we set the margin to 12 on any paragraphs it | |
| 42 actually doesn't draw them 12 pixels away. It draws them ~15 pixels | |
| 43 away! I'm assuming this is just Qt's widgets being weird (they usually are) | |
| 44 and I hope it's nothing I *really* have to worry about... */ | |
| 45 content_layout->setContentsMargins(15, 0, 0, 0); | |
| 46 content->setContentsMargins(0, 0, 0, 0); | |
| 9 | 47 |
| 93 | 48 score_dist_layout->addWidget(content); |
| 49 score_dist_layout->setContentsMargins(0, 0, 0, 0); | |
| 50 | |
| 51 _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this)); | |
| 52 | |
| 53 layout->addWidget(_anime_list.get()); | |
| 54 layout->addWidget(score_dist_widget); | |
| 55 layout->addWidget(_application.get()); | |
| 64 | 56 layout->addStretch(); |
| 9 | 57 |
| 58 QTimer* timer = new QTimer(this); | |
| 59 connect(timer, &QTimer::timeout, this, [this] { | |
| 60 if (isVisible()) | |
| 61 UpdateStatistics(); | |
| 62 }); | |
| 63 timer->start(1000); // update statistics every second | |
| 64 } | |
| 65 | |
| 64 | 66 void StatisticsPage::showEvent(QShowEvent*) { |
| 9 | 67 UpdateStatistics(); |
| 68 } | |
| 69 | |
| 70 /* me abusing macros :) */ | |
| 15 | 71 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
| 64 | 72 if (x > 0) \ |
| 73 r << x << ((x == 1) ? s : p) | |
| 102 | 74 std::string StatisticsPage::MinutesToDateString(const int minutes) { |
| 9 | 75 /* ew */ |
| 76 int years = (minutes * (1 / 525949.2F)); | |
| 77 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
| 78 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
| 79 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 80 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
| 81 std::ostringstream return_stream; | |
| 82 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
| 83 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 84 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 85 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 86 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
| 87 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
| 88 return return_stream.str(); | |
| 89 } | |
| 90 | |
| 102 | 91 std::string StatisticsPage::SecondsToDateString(const int sec) { |
| 9 | 92 /* this is all fairly unnecessary, but works:tm: */ |
| 10 | 93 int years = sec * (1 / 31556952.0F); |
| 94 int months = sec * (1 / 2629746.0F) - (years * 12); | |
| 95 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
| 96 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 97 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
| 15 | 98 int seconds = |
| 99 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
| 9 | 100 std::ostringstream return_stream; |
| 10 | 101 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
| 102 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 103 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 104 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 105 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
| 106 if (seconds > 0 || return_stream.str().size() == 0) | |
| 107 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
| 9 | 108 return return_stream.str(); |
| 109 } | |
| 110 #undef ADD_TIME_SEGMENT | |
| 111 | |
| 102 | 112 inline int GetTotalWithScore(const int score) { |
| 93 | 113 int count = 0; |
| 114 for (const auto& item : Anime::db.items) | |
| 115 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
| 116 count++; | |
| 117 return count; | |
| 118 } | |
| 119 | |
| 64 | 120 void StatisticsPage::UpdateStatistics() { |
| 9 | 121 /* Anime list */ |
| 122 QString string = ""; | |
| 123 QTextStream ts(&string); | |
| 10 | 124 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
| 125 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
| 126 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
| 127 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
| 128 ts << Anime::db.GetAverageScore() << '\n'; | |
| 129 ts << Anime::db.GetScoreDeviation(); | |
| 93 | 130 _anime_list->GetParagraph()->SetText(string); |
| 131 | |
| 132 _score_distribution_graph->Clear(); | |
| 133 for (int i = 10; i <= 100; i += 10) | |
| 134 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
| 9 | 135 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
136 string = ""; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
137 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n'; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
138 ts << session.GetRequests(); |
| 9 | 139 /* Application */ |
| 140 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
| 93 | 141 _application->GetParagraph()->SetText(string); |
| 9 | 142 } |
| 143 | |
| 10 | 144 #include "gui/pages/moc_statistics.cpp" |
