diff src/gui/window.cc @ 250:c130f47f6f48

*: many many changes e.g. the search page is actually implemented now!
author Paper <paper@paper.us.eu.org>
date Sun, 04 Feb 2024 21:17:17 -0500
parents 06d6c351925c
children fe702c8f161f
line wrap: on
line diff
--- a/src/gui/window.cc	Wed Jan 24 20:18:59 2024 -0500
+++ b/src/gui/window.cc	Sun Feb 04 21:17:17 2024 -0500
@@ -48,18 +48,6 @@
 #	include "sys/win32/dark_theme.h"
 #endif
 
-enum class Pages {
-	NOW_PLAYING,
-
-	ANIME_LIST,
-	HISTORY,
-	STATISTICS,
-
-	SEARCH,
-	SEASONS,
-	TORRENTS
-};
-
 void PlayingThread::run() {
 	std::vector<std::string> files;
 	Track::Media::GetCurrentlyPlaying(files);
@@ -124,6 +112,7 @@
 
 void MainWindow::AddMainWidgets() {
 	int page = static_cast<int>(Pages::ANIME_LIST);
+
 	if (sidebar.get()) {
 		main_widget->layout()->removeWidget(sidebar.get());
 		sidebar.reset();
@@ -149,10 +138,13 @@
 	sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png"));
 
 	stack.reset(new QStackedWidget(main_widget.get()));
+
 	stack->addWidget(new NowPlayingPage(main_widget.get()));
+	/* ---- */
 	stack->addWidget(new AnimeListPage(main_widget.get()));
 	stack->addWidget(new HistoryPage(main_widget.get()));
 	stack->addWidget(new StatisticsPage(main_widget.get()));
+	/* ---- */
 	stack->addWidget(new SearchPage(main_widget.get()));
 	stack->addWidget(new SeasonsPage(main_widget.get()));
 	stack->addWidget(new TorrentsPage(main_widget.get()));
@@ -166,7 +158,6 @@
 
 void MainWindow::CreateBars() {
 	QMenuBar* menubar = new QMenuBar(this);
-	QMenu* folder_menu; /* this is used twice, so we declare it here */
 	QAction* sync_action;
 
 	{
@@ -176,36 +167,7 @@
 		{
 			folder_menu = menu->addMenu(tr("&Library folders"));
 
-			/* add in all of our existing folders... */
-			std::size_t i = 0;
-			for (const auto& path : session.config.library.paths) {
-				const QString folder = Strings::ToQString(path);
-				QAction* action = folder_menu->addAction(folder, [folder]{
-					QDesktopServices::openUrl(QUrl::fromLocalFile(folder));
-				});
-				if (i < 9)
-					action->setShortcut(QKeySequence(Qt::ALT | static_cast<Qt::Modifier>(Qt::Key_1 + i)));
-				else if (i == 9)
-					action->setShortcut(QKeySequence(Qt::ALT | Qt::Key_0));
-				/* don't bother with a shortcut in case of more... */
-				i++;
-			}
-
-			folder_menu->addSeparator();
-
-			{
-				folder_menu->addAction(tr("&Add new folder..."), [this]{
-					const QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
-																		  QDir::homePath(),
-																		  QFileDialog::ShowDirsOnly
-																		  | QFileDialog::DontResolveSymlinks);
-					if (dir.isEmpty())
-						return;
-					session.config.library.paths.insert(Strings::ToUtf8String(dir));
-					/* we have to recreate the menu bar to add the new folder */
-					CreateBars();
-				});
-			}
+			UpdateFolderMenu();
 		}
 
 		{
@@ -229,7 +191,7 @@
 		menu->addSeparator();
 
 		{
-			QAction* action = menu->addAction(tr("E&xit"), qApp, &QApplication::quit);
+			QAction* action = menu->addAction(tr("E&xit"), this, &MainWindow::close);
 			action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));
 		}
 	}
@@ -314,7 +276,7 @@
 			QAction* action = menu->addAction(tr("&Settings"), [this] {
 				SettingsDialog dialog(this);
 				dialog.exec();
-				CreateBars();
+				UpdateFolderMenu();
 			});
 			action->setMenuRole(QAction::PreferencesRole);
 		}
@@ -326,60 +288,78 @@
 
 		{
 			/* Pages... */
-			std::map<QAction*, int> page_to_index_map = {};
-
-			QActionGroup* pages_group = new QActionGroup(this);
+			QActionGroup* pages_group = new QActionGroup(menu);
 			pages_group->setExclusive(true);
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("&Now Playing")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 0;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(0);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("&Anime List")));
 				action->setCheckable(true);
 				action->setChecked(true);
-				page_to_index_map[action] = 1;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(1);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("&History")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 2;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(2);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("&Statistics")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 3;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(3);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("S&earch")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 4;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(4);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("Se&asons")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 5;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(5);
+				});
 			}
 
 			{
 				QAction* action = pages_group->addAction(menu->addAction(tr("&Torrents")));
 				action->setCheckable(true);
-				page_to_index_map[action] = 6;
+				connect(action, &QAction::toggled, this, [this] {
+					sidebar->SetCurrentItem(6);
+				});
 			}
 
 			/* pain in my ass */
-			connect(sidebar.get(), &SideBar::CurrentItemChanged, this,
-					[pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
+			connect(sidebar.get(), &SideBar::CurrentItemChanged, this, [pages_group](int index) {
+				QAction* checked = pages_group->checkedAction();
 
-			connect(pages_group, &QActionGroup::triggered, this,
-					[this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
+				const QList<QAction*>& actions = pages_group->actions();
+				if (index > actions.size())
+					return;
+
+				if (checked)
+					checked->setChecked(false);
+				actions[index]->setChecked(true);
+			});
 		}
 
 		menu->addSeparator();
@@ -479,12 +459,55 @@
 		toolbar->addAction(QIcon(":/icons/24x24/gear.png"), tr("S&ettings"), [this] {
 			SettingsDialog dialog(this);
 			dialog.exec();
-			CreateBars();
+			/* library folders might have changed! */
+			UpdateFolderMenu();
 		});
 		addToolBar(toolbar);
 	}
 }
 
+void MainWindow::UpdateFolderMenu() {
+	if (!folder_menu)
+		return;
+
+	folder_menu->clear();
+
+	/* add in all of our existing folders... */
+	std::size_t i = 0;
+	for (const auto& path : session.config.library.paths) {
+		const QString folder = Strings::ToQString(path);
+		QAction* action = folder_menu->addAction(folder, [folder]{
+			QDesktopServices::openUrl(QUrl::fromLocalFile(folder));
+		});
+
+		if (i < 9) {
+			/* Qt::Key_1 is equivalent to 1 in ASCII, so we can use the same
+			 * stupid `'0' + i` trick here
+			*/
+			action->setShortcut(QKeySequence(Qt::ALT | static_cast<Qt::Modifier>(Qt::Key_1 + i)));
+		} else if (i == 9) {
+			action->setShortcut(QKeySequence(Qt::ALT | Qt::Key_0));
+		}
+		/* don't bother with a shortcut in case of more... */
+		i++;
+	}
+
+	folder_menu->addSeparator();
+
+	{
+		folder_menu->addAction(tr("&Add new folder..."), [this]{
+			const QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
+																  QDir::homePath(),
+																  QFileDialog::ShowDirsOnly
+																  | QFileDialog::DontResolveSymlinks);
+			if (dir.isEmpty())
+				return;
+			session.config.library.paths.insert(Strings::ToUtf8String(dir));
+			UpdateFolderMenu();
+		});
+	}
+}
+
 void MainWindow::SetActivePage(QWidget* page) {
 	this->setCentralWidget(page);
 }