diff src/gui/window.cc @ 112:80f49f623d30

locale: allow switching locales without restarting
author Paper <mrpapersonic@gmail.com>
date Mon, 06 Nov 2023 13:41:30 -0500
parents 2004b41d4a59
children 32afe0e940bf
line wrap: on
line diff
--- a/src/gui/window.cc	Mon Nov 06 02:03:55 2023 -0500
+++ b/src/gui/window.cc	Mon Nov 06 13:41:30 2023 -0500
@@ -31,6 +31,7 @@
 #include <QTimer>
 #include <QToolBar>
 #include <QToolButton>
+#include <iostream>
 #ifdef MACOSX
 #	include "sys/osx/dark_theme.h"
 #elif defined(WIN32)
@@ -49,46 +50,14 @@
 	TORRENTS
 };
 
-static void AsyncSynchronize(QStackedWidget* stack) {
-	QThreadPool::globalInstance()->start([stack] {
-		Services::Synchronize();
-		reinterpret_cast<AnimeListPage*>(stack->widget(static_cast<int>(Pages::ANIME_LIST)))->Refresh();
-	});
-}
-
 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
 	setWindowIcon(QIcon(":/favicon.png"));
 
-	main_widget = new QWidget(parent);
-
-	sidebar = new SideBar(main_widget);
-	sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png"));
-	sidebar->AddSeparator();
-	sidebar->AddItem(tr("Anime List"), SideBar::CreateIcon(":/icons/16x16/document-list.png"));
-	sidebar->AddItem(tr("History"), SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png"));
-	sidebar->AddItem(tr("Statistics"), SideBar::CreateIcon(":/icons/16x16/chart.png"));
-	sidebar->AddSeparator();
-	sidebar->AddItem(tr("Search"), SideBar::CreateIcon(":/icons/16x16/magnifier.png"));
-	sidebar->AddItem(tr("Seasons"), SideBar::CreateIcon(":/icons/16x16/calendar.png"));
-	sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png"));
-	sidebar->setFixedWidth(128);
-	sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
+	main_widget = new QWidget(this);
+	/*QHBoxLayout* layout = */new QHBoxLayout(main_widget);
 
-	stack = new QStackedWidget(main_widget);
-	stack->addWidget(new NowPlayingPage(main_widget));
-	stack->addWidget(new AnimeListPage(main_widget));
-	stack->addWidget(new HistoryPage(main_widget));
-	stack->addWidget(new StatisticsPage(main_widget));
-	stack->addWidget(new SearchPage(main_widget));
-	stack->addWidget(new SeasonsPage(main_widget));
-	stack->addWidget(new TorrentsPage(main_widget));
+	AddMainWidgets();
 
-	connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex);
-	sidebar->SetCurrentItem(static_cast<int>(Pages::ANIME_LIST));
-
-	QHBoxLayout* layout = new QHBoxLayout(main_widget);
-	layout->addWidget(sidebar);
-	layout->addWidget(stack);
 	setCentralWidget(main_widget);
 
 	CreateBars();
@@ -110,6 +79,53 @@
 	timer->start(5000);
 }
 
+void MainWindow::AddMainWidgets() {
+	int page = static_cast<int>(Pages::ANIME_LIST);
+	if (sidebar) {
+		main_widget->layout()->removeWidget(sidebar);
+		delete sidebar;
+	}
+
+	if (stack) {
+		page = stack->currentIndex();
+		main_widget->layout()->removeWidget(stack);
+		delete stack;
+	}
+
+	sidebar = new SideBar(main_widget);
+	sidebar->setFixedWidth(128);
+	sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
+
+	sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png"));
+	sidebar->AddSeparator();
+	sidebar->AddItem(tr("Anime List"), SideBar::CreateIcon(":/icons/16x16/document-list.png"));
+	sidebar->AddItem(tr("History"), SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png"));
+	sidebar->AddItem(tr("Statistics"), SideBar::CreateIcon(":/icons/16x16/chart.png"));
+	sidebar->AddSeparator();
+	sidebar->AddItem(tr("Search"), SideBar::CreateIcon(":/icons/16x16/magnifier.png"));
+	sidebar->AddItem(tr("Seasons"), SideBar::CreateIcon(":/icons/16x16/calendar.png"));
+	sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png"));
+
+	stack = new QStackedWidget(main_widget);
+	stack->addWidget(new NowPlayingPage(main_widget));
+	stack->addWidget(new AnimeListPage(main_widget));
+	stack->addWidget(new HistoryPage(main_widget));
+	stack->addWidget(new StatisticsPage(main_widget));
+	stack->addWidget(new SearchPage(main_widget));
+	stack->addWidget(new SeasonsPage(main_widget));
+	stack->addWidget(new TorrentsPage(main_widget));
+
+	connect(sidebar, &SideBar::CurrentItemChanged, this, [](int index){
+		std::cout << index << std::endl;
+	});
+
+	connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex);
+	sidebar->SetCurrentItem(page);
+
+	main_widget->layout()->addWidget(sidebar);
+	main_widget->layout()->addWidget(stack);
+}
+
 void MainWindow::CreateBars() {
 	/* Menu Bar */
 	QAction* action;
@@ -227,13 +243,17 @@
 	action = menu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
 	action->setMenuRole(QAction::AboutQtRole);
 
+	/* QMainWindow will delete the old one for us,
+	   according to the docs */
 	setMenuBar(menubar);
 
 	/* Toolbar */
 	/* remove old toolbar(s) */
 	QList<QToolBar*> toolbars = findChildren<QToolBar*>();
-	for (auto& t : toolbars)
+	for (auto& t : toolbars) {
 		removeToolBar(t);
+		delete t;
+	}
 
 	QToolBar* toolbar = new QToolBar(this);
 	toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"),
@@ -272,9 +292,56 @@
 	this->setCentralWidget(page);
 }
 
+void MainWindow::AsyncSynchronize(QStackedWidget* stack) {
+	if (session.config.service == Anime::Services::NONE) {
+		QMessageBox msg;
+		msg.setWindowTitle(tr("Error synchronizing with service!"));
+		msg.setText(tr("It seems you haven't yet selected a service to use."));
+		msg.setInformativeText(tr("Would you like to select one now?"));
+		msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+		msg.setDefaultButton(QMessageBox::Yes);
+		int ret = msg.exec();
+		if (ret == QMessageBox::Yes) {
+			SettingsDialog dialog;
+			dialog.exec();
+		}
+	}
+	QThreadPool::globalInstance()->start([stack] {
+		Services::Synchronize();
+		reinterpret_cast<AnimeListPage*>(stack->widget(static_cast<int>(Pages::ANIME_LIST)))->Refresh();
+	});
+}
+
+void MainWindow::RetranslateUI() {
+	/* Temporarily disable UI updates so we don't flash the screen */
+	setUpdatesEnabled(false);
+	AddMainWidgets();
+	CreateBars();
+	setUpdatesEnabled(true);
+}
+
+void MainWindow::changeEvent(QEvent* event) {
+	if (event) { /* is this really necessary */
+		switch (event->type()) {
+			// this event is send if a translator is loaded
+			case QEvent::LanguageChange:
+				RetranslateUI();
+				break;
+
+			default:
+				break;
+		}
+	}
+	QMainWindow::changeEvent(event);
+}
+
 void MainWindow::showEvent(QShowEvent* event) {
 	QMainWindow::showEvent(event);
 #ifdef WIN32
+	/* Technically this *should* be
+	   session.config.theme.IsInDarkTheme() && win32::IsInDarkTheme()
+	   but I prefer the title bar being black even when light mode
+	   is enabled :/ */
 	win32::SetTitleBarsToBlack(session.config.theme.IsInDarkTheme());
 #endif
 }