Mercurial > minori
diff src/gui/pages/statistics.cc @ 81:9b2b41f83a5e
boring: mass rename to cc
because this is a very unix-y project, it makes more sense to use the
'cc' extension
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 23 Oct 2023 12:07:27 -0400 |
parents | src/gui/pages/statistics.cpp@d3e9310598b1 |
children | d02fdf1d6708 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gui/pages/statistics.cc Mon Oct 23 12:07:27 2023 -0400 @@ -0,0 +1,113 @@ +#include "gui/pages/statistics.h" +#include "core/anime_db.h" +#include "core/session.h" +#include "gui/pages/anime_list.h" +#include "gui/widgets/text.h" +#include <QString> +#include <QTextDocument> +#include <QTextStream> +#include <QTimer> +#include <QVBoxLayout> +#include <QWidget> +#include <sstream> + +StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { + QVBoxLayout* layout = new QVBoxLayout(this); + + setFrameShape(QFrame::Box); + setFrameShadow(QFrame::Sunken); + + QPalette pal = QPalette(); + pal.setColor(QPalette::Window, pal.color(QPalette::Base)); + setPalette(pal); + setAutoFillBackground(true); + + TextWidgets::LabelledSection* anime_list_paragraph = new TextWidgets::LabelledSection( + tr("Anime list"), + tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), + "\n\n\n\n\n\n", this); + anime_list_data = anime_list_paragraph->GetParagraph(); + + TextWidgets::LabelledSection* application_paragraph = + new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this); + application_data = application_paragraph->GetParagraph(); + + layout->addWidget(anime_list_paragraph); + layout->addWidget(application_paragraph); + layout->addStretch(); + + QTimer* timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this] { + if (isVisible()) + UpdateStatistics(); + }); + timer->start(1000); // update statistics every second +} + +void StatisticsPage::showEvent(QShowEvent*) { + UpdateStatistics(); +} + +/* me abusing macros :) */ +#define ADD_TIME_SEGMENT(r, x, s, p) \ + if (x > 0) \ + r << x << ((x == 1) ? s : p) +std::string StatisticsPage::MinutesToDateString(int minutes) { + /* ew */ + int years = (minutes * (1 / 525949.2F)); + int months = (minutes * (1 / 43829.1F)) - (years * 12); + int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); + int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); + int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); + std::ostringstream return_stream; + ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); + ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); + ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); + ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); + if (rest_minutes > 0 || return_stream.str().size() == 0) + return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); + return return_stream.str(); +} + +std::string StatisticsPage::SecondsToDateString(int sec) { + /* this is all fairly unnecessary, but works:tm: */ + int years = sec * (1 / 31556952.0F); + int months = sec * (1 / 2629746.0F) - (years * 12); + int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); + int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); + int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); + int seconds = + sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); + std::ostringstream return_stream; + ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); + ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); + ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); + ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); + ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); + if (seconds > 0 || return_stream.str().size() == 0) + return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); + return return_stream.str(); +} +#undef ADD_TIME_SEGMENT + +void StatisticsPage::UpdateStatistics() { + /* Anime list */ + QString string = ""; + QTextStream ts(&string); + ts << Anime::db.GetTotalAnimeAmount() << '\n'; + ts << Anime::db.GetTotalEpisodeAmount() << '\n'; + ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; + ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; + ts << Anime::db.GetAverageScore() << '\n'; + ts << Anime::db.GetScoreDeviation(); + anime_list_data->SetText(string); + + string = ""; + ts << QString::fromUtf8(SecondsToDateString(session.uptime() / 1000).c_str()) << '\n'; + ts << session.GetRequests(); + /* Application */ + // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); + application_data->SetText(string); +} + +#include "gui/pages/moc_statistics.cpp"