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"
+ − 5 #include "gui/ui_utils.h"
+ − 6 #include <QString>
+ − 7 #include <QTextDocument>
+ − 8 #include <QTextStream>
+ − 9 #include <QTimer>
+ − 10 #include <QVBoxLayout>
+ − 11 #include <QWidget>
+ − 12 #include <sstream>
+ − 13
+ − 14 StatisticsWidget::StatisticsWidget(QWidget* parent) : QFrame(parent) {
+ − 15 setLayout(new QVBoxLayout);
+ − 16
+ − 17 setFrameShape(QFrame::Panel);
+ − 18 setFrameShadow(QFrame::Sunken);
+ − 19
+ − 20 UiUtils::LabelledTextParagraph* anime_list_paragraph = new UiUtils::LabelledTextParagraph(
15
+ − 21 "Anime list",
+ − 22 "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", "\n\n\n\n\n",
+ − 23 this);
9
+ − 24 anime_list_data = anime_list_paragraph->GetParagraph();
+ − 25
+ − 26 UiUtils::LabelledTextParagraph* application_paragraph =
15
+ − 27 new UiUtils::LabelledTextParagraph("Minori", "Uptime:", "", this);
9
+ − 28 application_data = application_paragraph->GetParagraph();
+ − 29
+ − 30 layout()->addWidget(anime_list_paragraph);
+ − 31 layout()->addWidget(application_paragraph);
+ − 32 ((QBoxLayout*)layout())->addStretch();
+ − 33
+ − 34 QPalette pal = QPalette();
+ − 35 pal.setColor(QPalette::Window, Qt::white);
+ − 36 setAutoFillBackground(true);
+ − 37 setPalette(pal);
+ − 38
+ − 39 QTimer* timer = new QTimer(this);
+ − 40 connect(timer, &QTimer::timeout, this, [this] {
+ − 41 if (isVisible())
+ − 42 UpdateStatistics();
+ − 43 });
+ − 44 timer->start(1000); // update statistics every second
+ − 45 }
+ − 46
+ − 47 void StatisticsWidget::showEvent(QShowEvent*) {
+ − 48 UpdateStatistics();
+ − 49 }
+ − 50
+ − 51 /* me abusing macros :) */
15
+ − 52 #define ADD_TIME_SEGMENT(r, x, s, p) \
+ − 53 { \
+ − 54 if (x > 0) \
+ − 55 r << x << ((x == 1) ? s : p); \
+ − 56 }
9
+ − 57 std::string StatisticsWidget::MinutesToDateString(int minutes) {
+ − 58 /* ew */
+ − 59 int years = (minutes * (1 / 525949.2F));
+ − 60 int months = (minutes * (1 / 43829.1F)) - (years * 12);
+ − 61 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F);
+ − 62 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
+ − 63 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60);
+ − 64 std::ostringstream return_stream;
+ − 65 ADD_TIME_SEGMENT(return_stream, years, " year ", " years ");
+ − 66 ADD_TIME_SEGMENT(return_stream, months, " month ", " months ");
+ − 67 ADD_TIME_SEGMENT(return_stream, days, " day ", " days ");
+ − 68 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours ");
+ − 69 if (rest_minutes > 0 || return_stream.str().size() == 0)
+ − 70 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes");
+ − 71 return return_stream.str();
+ − 72 }
+ − 73
10
+ − 74 std::string StatisticsWidget::SecondsToDateString(int sec) {
9
+ − 75 /* this is all fairly unnecessary, but works:tm: */
10
+ − 76 int years = sec * (1 / 31556952.0F);
+ − 77 int months = sec * (1 / 2629746.0F) - (years * 12);
+ − 78 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F);
+ − 79 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24);
+ − 80 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F);
15
+ − 81 int seconds =
+ − 82 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F);
9
+ − 83 std::ostringstream return_stream;
10
+ − 84 ADD_TIME_SEGMENT(return_stream, years, " year ", " years ");
+ − 85 ADD_TIME_SEGMENT(return_stream, months, " month ", " months ");
+ − 86 ADD_TIME_SEGMENT(return_stream, days, " day ", " days ");
+ − 87 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours ");
+ − 88 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes ");
+ − 89 if (seconds > 0 || return_stream.str().size() == 0)
+ − 90 return_stream << seconds << ((seconds == 1) ? " second" : " seconds");
9
+ − 91 return return_stream.str();
+ − 92 }
+ − 93 #undef ADD_TIME_SEGMENT
+ − 94
+ − 95 void StatisticsWidget::UpdateStatistics() {
+ − 96 /* Anime list */
+ − 97 QString string = "";
+ − 98 QTextStream ts(&string);
10
+ − 99 ts << Anime::db.GetTotalAnimeAmount() << '\n';
+ − 100 ts << Anime::db.GetTotalEpisodeAmount() << '\n';
+ − 101 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n';
+ − 102 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n';
+ − 103 ts << Anime::db.GetAverageScore() << '\n';
+ − 104 ts << Anime::db.GetScoreDeviation();
9
+ − 105 UiUtils::SetPlainTextEditData(anime_list_data, string);
+ − 106
+ − 107 /* Application */
+ − 108 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
+ − 109 UiUtils::SetPlainTextEditData(application_data, QString(SecondsToDateString(session.uptime() / 1000).c_str()));
+ − 110 }
+ − 111
10
+ − 112 #include "gui/pages/moc_statistics.cpp"