diff src/gui/pages/seasons.cc @ 260:dd211ff68b36

pages/seasons: add initial functionality the menu doesn't work yet, but it's a good start
author Paper <paper@paper.us.eu.org>
date Wed, 03 Apr 2024 19:48:38 -0400
parents 862d0d8619f6
children 96416310ea14
line wrap: on
line diff
--- a/src/gui/pages/seasons.cc	Mon Apr 01 18:11:15 2024 -0400
+++ b/src/gui/pages/seasons.cc	Wed Apr 03 19:48:38 2024 -0400
@@ -1,8 +1,12 @@
 #include "gui/pages/seasons.h"
 
 #include "core/anime_db.h"
+#include "core/anime_season_db.h"
+#include "core/strings.h"
 #include "gui/widgets/anime_button.h"
+#include "gui/translate/anime.h"
 
+#include <QDate>
 #include <QFrame>
 #include <QListWidget>
 #include <QListWidgetItem>
@@ -11,6 +15,23 @@
 #include <QToolButton>
 #include <QVBoxLayout>
 
+static constexpr Date::Year GetClosestDecade(Date::Year year) {
+	return year - (year % 10);
+}
+
+void SeasonsPage::SetSeason(Anime::SeriesSeason season, Date::Year year) {
+	buttons->clear();
+
+	for (const auto& id : Anime::Season::GetAllAnimeForSeason(season, year)) {
+		QListWidgetItem* item = new QListWidgetItem;
+		AnimeButton* button = new AnimeButton(this);
+		button->SetAnime(Anime::db.items[id]);
+		item->setSizeHint(button->sizeHint());
+		buttons->addItem(item);
+		buttons->setItemWidget(item, button);
+	}
+}
+
 SeasonsPage::SeasonsPage(QWidget* parent) : QWidget(parent) {
 	QVBoxLayout* full_layout = new QVBoxLayout(this);
 
@@ -22,22 +43,44 @@
 		toolbar->setMovable(false);
 
 		{
-			{
-				QAction* action = new QAction(toolbar);
-				action->setIcon(QIcon(":/icons/16x16/calendar-previous.png"));
-				action->setToolTip(tr("Previous season"));
-				toolbar->addAction(action);
-			}
+			/* hard-coded this value */
+			static constexpr Date::Year last_year = 1960;
+
+			auto create_year_menu = [](QWidget* parent, QMenu* parent_menu, Date::Year year){
+				const QString year_s = QString::number(year);
+
+				QMenu* menu = new QMenu(year_s, parent);
+				for (const auto& season : Anime::SeriesSeasons)
+					menu->addAction(Strings::ToQString(Translate::ToLocalString(season)) + " " + year_s);
+				parent_menu->addMenu(menu);
+			};
+
+			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);
+			};
 
-			{
-				QAction* action = new QAction(toolbar);
-				action->setIcon(QIcon(":/icons/16x16/calendar-next.png"));
-				action->setToolTip(tr("Next season"));
-				toolbar->addAction(action);
-			}
+			/* we'll be extinct by the time this code breaks, so I guess it's fine :) */
+			const Date::Year year = static_cast<Date::Year>(QDate::currentDate().year());
+			const Date::Year year_before_collapse = GetClosestDecade(year) - 10;
+			QToolButton* season_button = new QToolButton(toolbar);
+			QMenu* full_season_menu = new QMenu(season_button);
+
+			for (Date::Year c = year; c >= year_before_collapse; c--)
+				create_year_menu(season_button, full_season_menu, c);
 
-			toolbar->addAction(QIcon(":/icons/16x16/calendar.png"),
-			                   "Fall 2024"); // this must be named the name of the season
+			full_season_menu->addSeparator();
+
+			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_season_menu);
+			season_button->setText("Summer 2011");
+			season_button->setPopupMode(QToolButton::InstantPopup);
+
+			toolbar->addWidget(season_button);
 		}
 
 		toolbar->addSeparator();
@@ -123,39 +166,9 @@
 		buttons->setSpacing(2);
 		buttons->setResizeMode(QListView::Adjust);
 
-		{
-			QListWidgetItem* item = new QListWidgetItem;
-			AnimeButton* button = new AnimeButton(this);
-			button->SetAnime(Anime::db.items[Anime::db.GetAnimeFromTitle("Another")]);
-			item->setSizeHint(button->sizeHint());
-			buttons->addItem(item);
-			buttons->setItemWidget(item, button);
-		}
-		{
-			QListWidgetItem* item = new QListWidgetItem;
-			AnimeButton* button = new AnimeButton(this);
-			button->SetAnime(Anime::db.items[Anime::db.GetAnimeFromTitle("Another")]);
-			item->setSizeHint(button->sizeHint());
-			buttons->addItem(item);
-			buttons->setItemWidget(item, button);
-		}
-		{
-			QListWidgetItem* item = new QListWidgetItem;
-			AnimeButton* button = new AnimeButton(this);
-			button->SetAnime(Anime::db.items[Anime::db.GetAnimeFromTitle("Another")]);
-			item->setSizeHint(button->sizeHint());
-			buttons->addItem(item);
-			buttons->setItemWidget(item, button);
-		}
-		{
-			QListWidgetItem* item = new QListWidgetItem;
-			AnimeButton* button = new AnimeButton(this);
-			button->SetAnime(Anime::db.items[Anime::db.GetAnimeFromTitle("Another")]);
-			item->setSizeHint(button->sizeHint());
-			buttons->addItem(item);
-			buttons->setItemWidget(item, button);
-		}
-
 		full_layout->addWidget(buttons);
 	}
+
+	/* Do NOT move this up in this function, buttons HAS to be initialized */
+	SetSeason(Anime::SeriesSeason::SUMMER, 2011);
 }