diff 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
line wrap: on
line diff
--- a/src/pages/statistics.cpp	Wed Aug 16 00:49:17 2023 -0400
+++ b/src/pages/statistics.cpp	Thu Aug 24 23:11:38 2023 -0400
@@ -1,32 +1,41 @@
-#include "window.h"
+#include <sstream>
+#include <QWidget>
+#include <QTimer>
+#include <QTextStream>
+#include <QString>
+#include <QTextDocument>
+#include <QVBoxLayout>
+#include "anime_list.h"
 #include "ui_utils.h"
-#include <sstream>
+#include "statistics.h"
+
+StatisticsWidget::StatisticsWidget(AnimeListWidget* listwidget, QWidget* parent)
+	: QFrame(parent) {
+	setLayout(new QVBoxLayout);
+	anime_list = listwidget;
 
-StatisticsTimer::StatisticsTimer(Statistics* caller) {
-	statistics = caller;
-}
+	setFrameShape(QFrame::Panel);
+	setFrameShadow(QFrame::Plain);
+
+	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());
+	((QBoxLayout*)layout())->addStretch();
 
-void StatisticsTimer::Notify() {
-	if (status.current_page == PAGE_STATISTICS)
-		statistics->UpdateStatistics();
+	QPalette pal = QPalette();
+	pal.setColor(QPalette::Window, Qt::white);
+	setAutoFillBackground(true); 
+	setPalette(pal);
+
+	UpdateStatistics(); // load in statistics as soon as possible
+
+	QTimer* timer = new QTimer(this);
+	connect(timer, &QTimer::timeout, this, [this]{
+		if (isVisible())
+			UpdateStatistics();
+	});
+	timer->start(1000); // update statistics every second
 }
 
-Statistics::Statistics(page_t* page, wxPanel* frame) {
-	page->panel = new wxPanel(frame, wxID_ANY, wxPoint(0, 0), wxSize(400, 600));
-	page->panel->Show(false);
-	panel = new wxPanel(page->panel, wxID_ANY, wxPoint(12, 12), wxSize(376, 576));
-	anime_list = ((WeeabooFrame*)frame->GetParent())->GetAnimeList();
-
-	/* FIXME: this should be moved to a separate function, it's also used in information.cpp */
-	// wxWindow* parent, const char* title, const char* label, const char* data, int width, int height, int x = 0, int y = 0, int selectable = 0
-	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);
-
-	UpdateStatistics(); // load in statistics as soon as possible
-	timer = new StatisticsTimer(this);
-	timer->Start(1000); // update statistics every second
-}
-
-std::string Statistics::MinutesToDateString(int minutes) {
+std::string StatisticsWidget::MinutesToDateString(int minutes) {
 	/* NOTE: these duration_casts may not be needed... */
 	std::chrono::duration<int, std::ratio<60>> int_total_mins(minutes);
 	auto int_years = std::chrono::duration_cast<std::chrono::years>(int_total_mins);
@@ -51,13 +60,14 @@
 	return return_stream.str();
 }
 
-void Statistics::UpdateStatistics() {
-	wxString string = "";
-	string << anime_list->GetTotalAnimeAmount() << '\n';
-	string << anime_list->GetTotalEpisodeAmount() << '\n';
-	string << MinutesToDateString(anime_list->GetTotalWatchedAmount()) << '\n';
-	string << MinutesToDateString(anime_list->GetTotalPlannedAmount()) << '\n';
-	string << anime_list->GetAverageScore() << '\n';
-	string << anime_list->GetScoreDeviation() << '\n';
-	anime_list_data->SetLabel(string);
+void StatisticsWidget::UpdateStatistics() {
+	QString string = "";
+	QTextStream ts(&string);
+	ts << anime_list->GetTotalAnimeAmount() << '\n';
+	ts << anime_list->GetTotalEpisodeAmount() << '\n';
+	ts << MinutesToDateString(anime_list->GetTotalWatchedAmount()).c_str() << '\n';
+	ts << MinutesToDateString(anime_list->GetTotalPlannedAmount()).c_str() << '\n';
+	ts << anime_list->GetAverageScore() << '\n';
+	ts << anime_list->GetScoreDeviation() << '\n';
+	UiUtils::SetPlainTextEditData(anime_list_data, string);
 }