comparison src/gui/pages/statistics.cc @ 93:d5efb81540b3

statistics: add graph! also now we have a VERY VERY simple graph widget that probably sucks and doesn't work half the time :)
author Paper <mrpapersonic@gmail.com>
date Wed, 01 Nov 2023 13:52:34 -0400
parents d02fdf1d6708
children 2f373d48f889
comparison
equal deleted inserted replaced
92:2c27582a177c 93:d5efb81540b3
1 #include "gui/pages/statistics.h" 1 #include "gui/pages/statistics.h"
2 #include "core/anime_db.h" 2 #include "core/anime_db.h"
3 #include "core/session.h" 3 #include "core/session.h"
4 #include "gui/pages/anime_list.h" 4 #include "gui/pages/anime_list.h"
5 #include "gui/widgets/text.h" 5 #include "gui/widgets/text.h"
6 #include "gui/widgets/graph.h"
6 #include <QString> 7 #include <QString>
7 #include <QTextDocument> 8 #include <QTextDocument>
8 #include <QTextStream> 9 #include <QTextStream>
9 #include <QTimer> 10 #include <QTimer>
10 #include <QVBoxLayout> 11 #include <QVBoxLayout>
20 QPalette pal = QPalette(); 21 QPalette pal = QPalette();
21 pal.setColor(QPalette::Window, pal.color(QPalette::Base)); 22 pal.setColor(QPalette::Window, pal.color(QPalette::Base));
22 setPalette(pal); 23 setPalette(pal);
23 setAutoFillBackground(true); 24 setAutoFillBackground(true);
24 25
25 TextWidgets::LabelledSection* anime_list_paragraph = new TextWidgets::LabelledSection( 26 _anime_list.reset(new TextWidgets::LabelledSection(
26 tr("Anime list"), 27 tr("Anime list"),
27 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), 28 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"),
28 "", this); 29 "", this));
29 anime_list_data = anime_list_paragraph->GetParagraph();
30 30
31 TextWidgets::LabelledSection* application_paragraph = 31 /* spaghetti incoming */
32 new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this); 32 QWidget* score_dist_widget = new QWidget(this);
33 application_data = application_paragraph->GetParagraph(); 33 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget);
34 34
35 layout->addWidget(anime_list_paragraph); 35 QWidget* content = new QWidget(score_dist_widget);
36 layout->addWidget(application_paragraph); 36 QVBoxLayout* content_layout = new QVBoxLayout(content);
37 score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), content));
38 _score_distribution_graph.reset(new Graph<int>(content));
39 content_layout->addWidget(_score_distribution_graph.get());
40 content_layout->setContentsMargins(0, 0, 0, 0);
41 content->setContentsMargins(12, 0, 0, 0);
42
43 score_dist_layout->addWidget(content);
44 score_dist_layout->setContentsMargins(0, 0, 0, 0);
45
46 _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this));
47
48 layout->addWidget(_anime_list.get());
49 layout->addWidget(score_dist_widget);
50 layout->addWidget(_application.get());
37 layout->addStretch(); 51 layout->addStretch();
38 52
39 QTimer* timer = new QTimer(this); 53 QTimer* timer = new QTimer(this);
40 connect(timer, &QTimer::timeout, this, [this] { 54 connect(timer, &QTimer::timeout, this, [this] {
41 if (isVisible()) 55 if (isVisible())
88 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); 102 return_stream << seconds << ((seconds == 1) ? " second" : " seconds");
89 return return_stream.str(); 103 return return_stream.str();
90 } 104 }
91 #undef ADD_TIME_SEGMENT 105 #undef ADD_TIME_SEGMENT
92 106
107 inline int GetTotalWithScore(int score) {
108 int count = 0;
109 for (const auto& item : Anime::db.items)
110 if (item.second.IsInUserList() && item.second.GetUserScore() == score)
111 count++;
112 return count;
113 }
114
93 void StatisticsPage::UpdateStatistics() { 115 void StatisticsPage::UpdateStatistics() {
94 /* Anime list */ 116 /* Anime list */
95 QString string = ""; 117 QString string = "";
96 QTextStream ts(&string); 118 QTextStream ts(&string);
97 ts << Anime::db.GetTotalAnimeAmount() << '\n'; 119 ts << Anime::db.GetTotalAnimeAmount() << '\n';
98 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; 120 ts << Anime::db.GetTotalEpisodeAmount() << '\n';
99 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; 121 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n';
100 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; 122 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n';
101 ts << Anime::db.GetAverageScore() << '\n'; 123 ts << Anime::db.GetAverageScore() << '\n';
102 ts << Anime::db.GetScoreDeviation(); 124 ts << Anime::db.GetScoreDeviation();
103 anime_list_data->SetText(string); 125 _anime_list->GetParagraph()->SetText(string);
126
127 _score_distribution_graph->Clear();
128 for (int i = 10; i <= 100; i += 10)
129 _score_distribution_graph->AddItem(i, GetTotalWithScore(i));
104 130
105 string = ""; 131 string = "";
106 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n'; 132 ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n';
107 ts << session.GetRequests(); 133 ts << session.GetRequests();
108 /* Application */ 134 /* Application */
109 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); 135 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
110 application_data->SetText(string); 136 _application->GetParagraph()->SetText(string);
111 } 137 }
112 138
113 #include "gui/pages/moc_statistics.cpp" 139 #include "gui/pages/moc_statistics.cpp"