7
|
1 #include <sstream>
|
|
2 #include <QWidget>
|
|
3 #include <QTimer>
|
|
4 #include <QTextStream>
|
|
5 #include <QString>
|
|
6 #include <QTextDocument>
|
|
7 #include <QVBoxLayout>
|
|
8 #include "anime_list.h"
|
2
|
9 #include "ui_utils.h"
|
7
|
10 #include "statistics.h"
|
|
11
|
|
12 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
|
|
13 : QFrame(parent) {
|
|
14 setLayout(new QVBoxLayout);
|
|
15 anime_list = listwidget;
|
2
|
16
|
7
|
17 setFrameShape(QFrame::Panel);
|
|
18 setFrameShadow(QFrame::Plain);
|
|
19
|
|
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());
|
|
21 ((QBoxLayout*)layout())->addStretch();
|
2
|
22
|
7
|
23 QPalette pal = QPalette();
|
|
24 pal.setColor(QPalette::Window, Qt::white);
|
|
25 setAutoFillBackground(true);
|
|
26 setPalette(pal);
|
|
27
|
|
28 UpdateStatistics(); // load in statistics as soon as possible
|
|
29
|
|
30 QTimer* timer = new QTimer(this);
|
|
31 connect(timer, &QTimer::timeout, this, [this]{
|
|
32 if (isVisible())
|
|
33 UpdateStatistics();
|
|
34 });
|
|
35 timer->start(1000); // update statistics every second
|
2
|
36 }
|
|
37
|
7
|
38 std::string StatisticsWidget::MinutesToDateString(int minutes) {
|
2
|
39 /* NOTE: these duration_casts may not be needed... */
|
|
40 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
|
|
41 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
|
|
42 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
|
|
43 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
|
|
44 auto int_hours = std::chrono::duration_cast<std::chrono::hours>(int_total_mins-int_years-int_months-int_days);
|
|
45 auto int_minutes = std::chrono::duration_cast<std::chrono::minutes>(int_total_mins-int_years-int_months-int_days-int_hours);
|
|
46 std::ostringstream return_stream;
|
|
47 if (int_years.count() > 0) {
|
|
48 return_stream << int_years.count() << " years ";
|
|
49 }
|
|
50 if (int_months.count() > 0) {
|
|
51 return_stream << int_months.count() << " months ";
|
|
52 }
|
|
53 if (int_days.count() > 0) {
|
|
54 return_stream << int_days.count() << " days ";
|
|
55 }
|
|
56 if (int_hours.count() > 0) {
|
|
57 return_stream << int_hours.count() << " hours ";
|
|
58 }
|
|
59 return_stream << int_minutes.count() << " minutes"; // return minutes anyway
|
|
60 return return_stream.str();
|
|
61 }
|
|
62
|
7
|
63 void StatisticsWidget::UpdateStatistics() {
|
|
64 QString string = "";
|
|
65 QTextStream ts(&string);
|
|
66 ts << anime_list->GetTotalAnimeAmount() << '\n';
|
|
67 ts << anime_list->GetTotalEpisodeAmount() << '\n';
|
|
68 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
|
|
69 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
|
|
70 ts << anime_list->GetAverageScore() << '\n';
|
|
71 ts << anime_list->GetScoreDeviation() << '\n';
|
|
72 UiUtils::SetPlainTextEditData(anime_list_data, string);
|
2
|
73 }
|