comparison 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
comparison
equal deleted inserted replaced
6:1d82f6e04d7d 7:07a9095eaeed
1 #include "window.h" 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 #include "ui_utils.h" 9 #include "ui_utils.h"
3 #include <sstream> 10 #include "statistics.h"
4 11
5 StatisticsTimer::StatisticsTimer(Statistics* caller) { 12 StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
6 statistics = caller; 13 : QFrame(parent) {
14 setLayout(new QVBoxLayout);
15 anime_list = listwidget;
16
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();
22
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
7 } 36 }
8 37
9 void StatisticsTimer::Notify() { 38 std::string StatisticsWidget::MinutesToDateString(int minutes) {
10 if (status.current_page == PAGE_STATISTICS)
11 statistics->UpdateStatistics();
12 }
13
14 Statistics::Statistics(page_t* page, wxPanel* frame) {
15 page->panel = new wxPanel(frame, wxID_ANY, wxPoint(0, 0), wxSize(400, 600));
16 page->panel->Show(false);
17 panel = new wxPanel(page->panel, wxID_ANY, wxPoint(12, 12), wxSize(376, 576));
18 anime_list = ((WeeabooFrame*)frame->GetParent())->GetAnimeList();
19
20 /* FIXME: this should be moved to a separate function, it's also used in information.cpp */
21 // wxWindow* parent, const char* title, const char* label, const char* data, int width, int height, int x = 0, int y = 0, int selectable = 0
22 anime_list_data = UiUtils::CreateTextParagraphWithLabels(panel, L"Anime list", L"Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:", L"", 376, 94);
23
24 UpdateStatistics(); // load in statistics as soon as possible
25 timer = new StatisticsTimer(this);
26 timer->Start(1000); // update statistics every second
27 }
28
29 std::string Statistics::MinutesToDateString(int minutes) {
30 /* NOTE: these duration_casts may not be needed... */ 39 /* NOTE: these duration_casts may not be needed... */
31 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes); 40 std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
32 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins); 41 auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
33 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years); 42 auto int_months = std::chrono::duration_cast<std::chrono::months>(int_total_mins-int_years);
34 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months); 43 auto int_days = std::chrono::duration_cast<std::chrono::days>(int_total_mins-int_years-int_months);
49 } 58 }
50 return_stream << int_minutes.count() << " minutes"; // return minutes anyway 59 return_stream << int_minutes.count() << " minutes"; // return minutes anyway
51 return return_stream.str(); 60 return return_stream.str();
52 } 61 }
53 62
54 void Statistics::UpdateStatistics() { 63 void StatisticsWidget::UpdateStatistics() {
55 wxString string = ""; 64 QString string = "";
56 string << anime_list->GetTotalAnimeAmount() << '\n'; 65 QTextStream ts(&string);
57 string << anime_list->GetTotalEpisodeAmount() << '\n'; 66 ts << anime_list->GetTotalAnimeAmount() << '\n';
58 string << MinutesToDateString(anime_list->GetTotalWatchedAmount()) << '\n'; 67 ts << anime_list->GetTotalEpisodeAmount() << '\n';
59 string << MinutesToDateString(anime_list->GetTotalPlannedAmount()) << '\n'; 68 ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
60 string << anime_list->GetAverageScore() << '\n'; 69 ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
61 string << anime_list->GetScoreDeviation() << '\n'; 70 ts << anime_list->GetAverageScore() << '\n';
62 anime_list_data->SetLabel(string); 71 ts << anime_list->GetScoreDeviation() << '\n';
72 UiUtils::SetPlainTextEditData(anime_list_data, string);
63 } 73 }