diff src/gui/pages/seasons.cc @ 348:6b0768158dcd

text: redesign almost every widget i.e. Paragraph is now a QLabel, etc etc, some things will probably break, idc
author Paper <paper@paper.us.eu.org>
date Tue, 25 Jun 2024 11:19:54 -0400
parents b5d6c27c308f
children 83e6ba09338f
line wrap: on
line diff
--- a/src/gui/pages/seasons.cc	Sun Jun 23 10:32:09 2024 -0400
+++ b/src/gui/pages/seasons.cc	Tue Jun 25 11:19:54 2024 -0400
@@ -15,6 +15,8 @@
 #include <QToolButton>
 #include <QVBoxLayout>
 
+#include <iostream>
+
 SeasonsPageSearchThread::SeasonsPageSearchThread(QObject* parent) : QThread(parent) {
 }
 
@@ -56,7 +58,7 @@
 void SeasonsPage::Refresh() {
 	setUpdatesEnabled(false);
 
-	if (!buttons || !season_button)
+	if (!buttons)
 		return;
 
 	buttons->clear();
@@ -70,7 +72,7 @@
 		buttons->setItemWidget(item, button);
 	}
 
-	season_button->setText(Strings::ToQString(Translate::ToLocalString(season_)));
+	season_button.setText(Strings::ToQString(Translate::ToLocalString(season_)));
 
 	setUpdatesEnabled(true);
 }
@@ -96,47 +98,50 @@
 		toolbar->setMovable(false);
 
 		{
-			/* currently this is VERY hardcoded to en_US */
-			static constexpr Date::Year last_year = 1960;
+			/* XXX this last year probably shouldn't be hardcoded */
+			static const Anime::Season last_season(Anime::Season::Name::Winter, 1960);
+			Anime::Season current_season(Date(QDate::currentDate()));
+			const Date::Year year_before_collapse = GetClosestDecade(current_season.year) - 10;
 
-			auto create_year_menu = [this](QWidget* parent, QMenu* parent_menu, Date::Year year){
-				const QString year_s = QString::number(year);
+			/* year -> menu for that year */
+			std::map<Date::Year, QMenu*> menu_map;
 
-				QMenu* menu = new QMenu(year_s, parent);
-				for (const auto& season : Anime::Season::Names) {
-					QAction* action = menu->addAction(Strings::ToQString(Translate::ToLocalString(Anime::Season(season, year))));
-					connect(action, &QAction::triggered, this, [this, season, year] {
-						SetSeason({season, year});
-					});
-				}
-				parent_menu->addMenu(menu);
+			auto create_season_menu = [&](QWidget* parent, Anime::Season season){
+				QMenu*& menu = menu_map[season.year];
+				if (!menu)
+					menu = new QMenu(QString::number(season.year), parent);
+
+				QAction* action = menu->addAction(Strings::ToQString(Translate::ToLocalString(season)));
+				connect(action, &QAction::triggered, this, [this, season] {
+					SetSeason(season);
+				});
 			};
 
-			auto create_decade_menu = [create_year_menu](QWidget* parent, QMenu* parent_menu, Date::Year decade) {
-				QMenu* menu = new QMenu(QString::number(decade) + "s", parent);
-				for (int i = 9; i >= 0; i--)
-					create_year_menu(parent, menu, decade + i);
-				parent_menu->addMenu(menu);
-			};
+			for (Anime::Season s = current_season; s >= last_season; --s)
+				create_season_menu(&season_button, s);
+
+			/* ------------------------------------------------------- */
+			/* now actually generate the full menu */
 
-			/* we'll be extinct by the time this code breaks, so I guess it's fine :) */
-			const Date::Year current_year = static_cast<Date::Year>(QDate::currentDate().year());
-			const Date::Year year_before_collapse = GetClosestDecade(current_year) - 10;
-			season_button = new QToolButton(toolbar);
-			QMenu* full_season_menu = new QMenu(season_button);
+			QMenu* full_menu = new QMenu(&season_button);
+
+			for (Date::Year c = current_season.year; c >= year_before_collapse; c--)
+				full_menu->addMenu(menu_map[c]);
+
+			full_menu->addSeparator();
 
-			for (Date::Year c = current_year; c >= year_before_collapse; c--)
-				create_year_menu(season_button, full_season_menu, c);
-
-			full_season_menu->addSeparator();
+			/* collapse each menu into a decade */
+			for (Date::Year c = year_before_collapse - 10; c >= last_season.year; c -= 10) {
+				QMenu* decade_menu = new QMenu(tr("%1s").arg(QString::number(c)), parent);
+				for (Date::Year i = c + 9; i >= c; i--)
+					decade_menu->addMenu(menu_map[i]);
+				full_menu->addMenu(decade_menu);
+			}
 
-			for (Date::Year c = year_before_collapse - 10; c >= last_year; c -= 10)
-				create_decade_menu(season_button, full_season_menu, c);
+			season_button.setMenu(full_menu);
+			season_button.setPopupMode(QToolButton::InstantPopup);
 
-			season_button->setMenu(full_season_menu);
-			season_button->setPopupMode(QToolButton::InstantPopup);
-
-			toolbar->addWidget(season_button);
+			toolbar->addWidget(&season_button);
 		}
 
 		toolbar->addSeparator();