comparison 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
comparison
equal deleted inserted replaced
111:fcae8bece0ec 112:80f49f623d30
29 #include <QTextStream> 29 #include <QTextStream>
30 #include <QThreadPool> 30 #include <QThreadPool>
31 #include <QTimer> 31 #include <QTimer>
32 #include <QToolBar> 32 #include <QToolBar>
33 #include <QToolButton> 33 #include <QToolButton>
34 #include <iostream>
34 #ifdef MACOSX 35 #ifdef MACOSX
35 # include "sys/osx/dark_theme.h" 36 # include "sys/osx/dark_theme.h"
36 #elif defined(WIN32) 37 #elif defined(WIN32)
37 # include "sys/win32/dark_theme.h" 38 # include "sys/win32/dark_theme.h"
38 #endif 39 #endif
47 SEARCH, 48 SEARCH,
48 SEASONS, 49 SEASONS,
49 TORRENTS 50 TORRENTS
50 }; 51 };
51 52
52 static void AsyncSynchronize(QStackedWidget* stack) {
53 QThreadPool::globalInstance()->start([stack] {
54 Services::Synchronize();
55 reinterpret_cast<AnimeListPage*>(stack->widget(static_cast<int>(Pages::ANIME_LIST)))->Refresh();
56 });
57 }
58
59 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { 53 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
60 setWindowIcon(QIcon(":/favicon.png")); 54 setWindowIcon(QIcon(":/favicon.png"));
61 55
62 main_widget = new QWidget(parent); 56 main_widget = new QWidget(this);
57 /*QHBoxLayout* layout = */new QHBoxLayout(main_widget);
58
59 AddMainWidgets();
60
61 setCentralWidget(main_widget);
62
63 CreateBars();
64
65 QTimer* timer = new QTimer(this);
66 connect(timer, &QTimer::timeout, this, [this] {
67 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
68
69 Filesystem::Path p = Track::Media::GetCurrentPlaying();
70 std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(p);
71 int id = Anime::db.GetAnimeFromTitle(elements["title"]);
72 if (id <= 0) {
73 page->SetDefault();
74 return;
75 }
76
77 page->SetPlaying(Anime::db.items[id], elements);
78 });
79 timer->start(5000);
80 }
81
82 void MainWindow::AddMainWidgets() {
83 int page = static_cast<int>(Pages::ANIME_LIST);
84 if (sidebar) {
85 main_widget->layout()->removeWidget(sidebar);
86 delete sidebar;
87 }
88
89 if (stack) {
90 page = stack->currentIndex();
91 main_widget->layout()->removeWidget(stack);
92 delete stack;
93 }
63 94
64 sidebar = new SideBar(main_widget); 95 sidebar = new SideBar(main_widget);
96 sidebar->setFixedWidth(128);
97 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
98
65 sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png")); 99 sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png"));
66 sidebar->AddSeparator(); 100 sidebar->AddSeparator();
67 sidebar->AddItem(tr("Anime List"), SideBar::CreateIcon(":/icons/16x16/document-list.png")); 101 sidebar->AddItem(tr("Anime List"), SideBar::CreateIcon(":/icons/16x16/document-list.png"));
68 sidebar->AddItem(tr("History"), SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png")); 102 sidebar->AddItem(tr("History"), SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png"));
69 sidebar->AddItem(tr("Statistics"), SideBar::CreateIcon(":/icons/16x16/chart.png")); 103 sidebar->AddItem(tr("Statistics"), SideBar::CreateIcon(":/icons/16x16/chart.png"));
70 sidebar->AddSeparator(); 104 sidebar->AddSeparator();
71 sidebar->AddItem(tr("Search"), SideBar::CreateIcon(":/icons/16x16/magnifier.png")); 105 sidebar->AddItem(tr("Search"), SideBar::CreateIcon(":/icons/16x16/magnifier.png"));
72 sidebar->AddItem(tr("Seasons"), SideBar::CreateIcon(":/icons/16x16/calendar.png")); 106 sidebar->AddItem(tr("Seasons"), SideBar::CreateIcon(":/icons/16x16/calendar.png"));
73 sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png")); 107 sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png"));
74 sidebar->setFixedWidth(128);
75 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
76 108
77 stack = new QStackedWidget(main_widget); 109 stack = new QStackedWidget(main_widget);
78 stack->addWidget(new NowPlayingPage(main_widget)); 110 stack->addWidget(new NowPlayingPage(main_widget));
79 stack->addWidget(new AnimeListPage(main_widget)); 111 stack->addWidget(new AnimeListPage(main_widget));
80 stack->addWidget(new HistoryPage(main_widget)); 112 stack->addWidget(new HistoryPage(main_widget));
81 stack->addWidget(new StatisticsPage(main_widget)); 113 stack->addWidget(new StatisticsPage(main_widget));
82 stack->addWidget(new SearchPage(main_widget)); 114 stack->addWidget(new SearchPage(main_widget));
83 stack->addWidget(new SeasonsPage(main_widget)); 115 stack->addWidget(new SeasonsPage(main_widget));
84 stack->addWidget(new TorrentsPage(main_widget)); 116 stack->addWidget(new TorrentsPage(main_widget));
85 117
118 connect(sidebar, &SideBar::CurrentItemChanged, this, [](int index){
119 std::cout << index << std::endl;
120 });
121
86 connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex); 122 connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex);
87 sidebar->SetCurrentItem(static_cast<int>(Pages::ANIME_LIST)); 123 sidebar->SetCurrentItem(page);
88 124
89 QHBoxLayout* layout = new QHBoxLayout(main_widget); 125 main_widget->layout()->addWidget(sidebar);
90 layout->addWidget(sidebar); 126 main_widget->layout()->addWidget(stack);
91 layout->addWidget(stack);
92 setCentralWidget(main_widget);
93
94 CreateBars();
95
96 QTimer* timer = new QTimer(this);
97 connect(timer, &QTimer::timeout, this, [this] {
98 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
99
100 Filesystem::Path p = Track::Media::GetCurrentPlaying();
101 std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(p);
102 int id = Anime::db.GetAnimeFromTitle(elements["title"]);
103 if (id <= 0) {
104 page->SetDefault();
105 return;
106 }
107
108 page->SetPlaying(Anime::db.items[id], elements);
109 });
110 timer->start(5000);
111 } 127 }
112 128
113 void MainWindow::CreateBars() { 129 void MainWindow::CreateBars() {
114 /* Menu Bar */ 130 /* Menu Bar */
115 QAction* action; 131 QAction* action;
225 dialog.exec(); 241 dialog.exec();
226 }); 242 });
227 action = menu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); 243 action = menu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
228 action->setMenuRole(QAction::AboutQtRole); 244 action->setMenuRole(QAction::AboutQtRole);
229 245
246 /* QMainWindow will delete the old one for us,
247 according to the docs */
230 setMenuBar(menubar); 248 setMenuBar(menubar);
231 249
232 /* Toolbar */ 250 /* Toolbar */
233 /* remove old toolbar(s) */ 251 /* remove old toolbar(s) */
234 QList<QToolBar*> toolbars = findChildren<QToolBar*>(); 252 QList<QToolBar*> toolbars = findChildren<QToolBar*>();
235 for (auto& t : toolbars) 253 for (auto& t : toolbars) {
236 removeToolBar(t); 254 removeToolBar(t);
255 delete t;
256 }
237 257
238 QToolBar* toolbar = new QToolBar(this); 258 QToolBar* toolbar = new QToolBar(this);
239 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), 259 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"),
240 [this] { AsyncSynchronize(stack); }); 260 [this] { AsyncSynchronize(stack); });
241 toolbar->addSeparator(); 261 toolbar->addSeparator();
270 290
271 void MainWindow::SetActivePage(QWidget* page) { 291 void MainWindow::SetActivePage(QWidget* page) {
272 this->setCentralWidget(page); 292 this->setCentralWidget(page);
273 } 293 }
274 294
295 void MainWindow::AsyncSynchronize(QStackedWidget* stack) {
296 if (session.config.service == Anime::Services::NONE) {
297 QMessageBox msg;
298 msg.setWindowTitle(tr("Error synchronizing with service!"));
299 msg.setText(tr("It seems you haven't yet selected a service to use."));
300 msg.setInformativeText(tr("Would you like to select one now?"));
301 msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
302 msg.setDefaultButton(QMessageBox::Yes);
303 int ret = msg.exec();
304 if (ret == QMessageBox::Yes) {
305 SettingsDialog dialog;
306 dialog.exec();
307 }
308 }
309 QThreadPool::globalInstance()->start([stack] {
310 Services::Synchronize();
311 reinterpret_cast<AnimeListPage*>(stack->widget(static_cast<int>(Pages::ANIME_LIST)))->Refresh();
312 });
313 }
314
315 void MainWindow::RetranslateUI() {
316 /* Temporarily disable UI updates so we don't flash the screen */
317 setUpdatesEnabled(false);
318 AddMainWidgets();
319 CreateBars();
320 setUpdatesEnabled(true);
321 }
322
323 void MainWindow::changeEvent(QEvent* event) {
324 if (event) { /* is this really necessary */
325 switch (event->type()) {
326 // this event is send if a translator is loaded
327 case QEvent::LanguageChange:
328 RetranslateUI();
329 break;
330
331 default:
332 break;
333 }
334 }
335 QMainWindow::changeEvent(event);
336 }
337
275 void MainWindow::showEvent(QShowEvent* event) { 338 void MainWindow::showEvent(QShowEvent* event) {
276 QMainWindow::showEvent(event); 339 QMainWindow::showEvent(event);
277 #ifdef WIN32 340 #ifdef WIN32
341 /* Technically this *should* be
342 session.config.theme.IsInDarkTheme() && win32::IsInDarkTheme()
343 but I prefer the title bar being black even when light mode
344 is enabled :/ */
278 win32::SetTitleBarsToBlack(session.config.theme.IsInDarkTheme()); 345 win32::SetTitleBarsToBlack(session.config.theme.IsInDarkTheme());
279 #endif 346 #endif
280 } 347 }
281 348
282 void MainWindow::closeEvent(QCloseEvent* event) { 349 void MainWindow::closeEvent(QCloseEvent* event) {