annotate src/pages/statistics.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 23d0d9319a00
children b1f73678ef61
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
1 #include <sstream>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
2 #include <QWidget>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
3 #include <QTimer>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
4 #include <QTextStream>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
5 #include <QString>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
6 #include <QTextDocument>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
7 #include <QVBoxLayout>
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
8 #include "anime_list.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 #include "ui_utils.h"
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
10 #include "statistics.h"
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
11
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
12 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
13 : QFrame(parent) {
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
14 setLayout(new QVBoxLayout);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
15 anime_list = listwidget;
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
17 setFrameShape(QFrame::Panel);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
18 setFrameShadow(QFrame::Plain);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
19
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
20 layout()->addWidget((anime_list_data = UiUtils::CreateTextParagraphWithLabels(this, "Anime list", "Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", ""))->parentWidget()->parentWidget());
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
21 ((QBoxLayout*)layout())->addStretch();
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
23 QPalette pal = QPalette();
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
24 pal.setColor(QPalette::Window, Qt::white);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
25 setAutoFillBackground(true);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
26 setPalette(pal);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
27
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
28 UpdateStatistics(); // load in statistics as soon as possible
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
29
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
30 QTimer* timer = new QTimer(this);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
31 connect(timer, &QTimer::timeout, this, [this]{
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
32 if (isVisible())
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
33 UpdateStatistics();
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
34 });
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
35 timer->start(1000); // update statistics every second
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
36 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
37
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
38 std::string StatisticsWidget::MinutesToDateString(int minutes) {
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39 /* NOTE: these duration_casts may not be needed... */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
40 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 std::ostringstream return_stream;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 if (int_years.count() > 0) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 return_stream << int_years.count() << " years ";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
49 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
50 if (int_months.count() > 0) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
51 return_stream << int_months.count() << " months ";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
52 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 if (int_days.count() > 0) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
54 return_stream << int_days.count() << " days ";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 if (int_hours.count() > 0) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 return_stream << int_hours.count() << " hours ";
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 return_stream << int_minutes.count() << " minutes"; // return minutes anyway
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 return return_stream.str();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
61 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62
7
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
63 void StatisticsWidget::UpdateStatistics() {
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
64 QString string = "";
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
65 QTextStream ts(&string);
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
66 ts << anime_list->GetTotalAnimeAmount() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
67 ts << anime_list->GetTotalEpisodeAmount() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
68 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
69 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
70 ts << anime_list->GetAverageScore() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
71 ts << anime_list->GetScoreDeviation() << '\n';
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
72 UiUtils::SetPlainTextEditData(anime_list_data, string);
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 }