comparison src/gui/window.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 975a3f0965e2
children 84e0a3c4737a
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
34 #include <QThreadPool> 34 #include <QThreadPool>
35 #include <QTimer> 35 #include <QTimer>
36 #include <QToolBar> 36 #include <QToolBar>
37 #include <QToolButton> 37 #include <QToolButton>
38 38
39 #include <iostream>
40
39 #ifdef MACOSX 41 #ifdef MACOSX
40 # include "sys/osx/dark_theme.h" 42 # include "sys/osx/dark_theme.h"
41 #elif defined(WIN32) 43 #elif defined(WIN32)
42 # include "sys/win32/dark_theme.h" 44 # include "sys/win32/dark_theme.h"
43 #endif 45 #endif
44 46
45 Q_DECLARE_METATYPE(std::vector<std::string>);
46
47 class Thread : public QThread {
48 Q_OBJECT
49
50 public:
51 Thread(QObject* object = nullptr) : QThread(object) {}
52
53 private:
54 void run() override {
55 std::vector<std::string> files;
56 Track::Media::GetCurrentlyPlaying(files);
57 emit Done(files);
58 }
59
60 signals:
61 void Done(const std::vector<std::string>& files);
62 };
63
64 enum class Pages { 47 enum class Pages {
65 NOW_PLAYING, 48 NOW_PLAYING,
66 49
67 ANIME_LIST, 50 ANIME_LIST,
68 HISTORY, 51 HISTORY,
71 SEARCH, 54 SEARCH,
72 SEASONS, 55 SEASONS,
73 TORRENTS 56 TORRENTS
74 }; 57 };
75 58
59 void PlayingThread::run() {
60 std::vector<std::string> files;
61 Track::Media::GetCurrentlyPlaying(files);
62 emit Done(files);
63 }
64
76 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { 65 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
77 setWindowIcon(QIcon(":/favicon.png")); 66 setWindowIcon(QIcon(":/favicon.png"));
78 67
79 main_widget.reset(new QWidget(this)); 68 main_widget.reset(new QWidget(this));
80 /*QHBoxLayout* layout = */new QHBoxLayout(main_widget.get()); 69 new QHBoxLayout(main_widget.get());
81 70
82 AddMainWidgets(); 71 AddMainWidgets();
83 72
84 setCentralWidget(main_widget.get()); 73 setCentralWidget(main_widget.get());
85 74
87 76
88 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); 77 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING)));
89 78
90 qRegisterMetaType<std::vector<std::string>>(); 79 qRegisterMetaType<std::vector<std::string>>();
91 80
92 QTimer* timer = new QTimer; 81 /* This thread will be destroyed on
93 timer->start(5000); 82 * close of the program OR on the destruction
83 * of MainWindow
84 */
85 thread.reset(new PlayingThread(this));
86
87 QTimer* timer = new QTimer(this);
94 88
95 connect(timer, &QTimer::timeout, this, [this, page] { 89 connect(timer, &QTimer::timeout, this, [this, page] {
96 Thread* thread = new Thread(this); 90 if (!thread.get() || thread->isRunning())
97 connect(thread, &QThread::finished, thread, &QThread::deleteLater); 91 return;
98 connect(thread, &Thread::Done, this, [page](const std::vector<std::string>& files) { 92
93 connect(thread.get(), &PlayingThread::Done, this, [page](const std::vector<std::string>& files) {
99 for (const auto& file : files) { 94 for (const auto& file : files) {
100 anitomy::Anitomy anitomy; 95 anitomy::Anitomy anitomy;
101 anitomy.Parse(Strings::ToWstring(file)); 96 anitomy.Parse(Strings::ToWstring(file));
102 97
103 const auto& elements = anitomy.elements(); 98 const auto& elements = anitomy.elements();
110 break; 105 break;
111 } 106 }
112 }); 107 });
113 thread->start(); 108 thread->start();
114 }); 109 });
110
111 timer->start(5000);
115 } 112 }
116 113
117 void MainWindow::AddMainWidgets() { 114 void MainWindow::AddMainWidgets() {
118 int page = static_cast<int>(Pages::ANIME_LIST); 115 int page = static_cast<int>(Pages::ANIME_LIST);
119 if (sidebar.get()) { 116 if (sidebar.get()) {
330 page_to_index_map[action] = 6; 327 page_to_index_map[action] = 6;
331 } 328 }
332 329
333 /* pain in my ass */ 330 /* pain in my ass */
334 connect(sidebar.get(), &SideBar::CurrentItemChanged, this, 331 connect(sidebar.get(), &SideBar::CurrentItemChanged, this,
335 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); }); 332 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); });
336 333
337 connect(pages_group, &QActionGroup::triggered, this, 334 connect(pages_group, &QActionGroup::triggered, this,
338 [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); }); 335 [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); });
339 } 336 }
340 337
341 menu->addSeparator(); 338 menu->addSeparator();
342 339
343 // { 340 // {
378 375
379 { 376 {
380 /* Toolbar */ 377 /* Toolbar */
381 QToolBar* toolbar = new QToolBar(this); 378 QToolBar* toolbar = new QToolBar(this);
382 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), 379 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"),
383 [this] { AsyncSynchronize(stack.get()); }); 380 [this] { AsyncSynchronize(stack.get()); });
384 381
385 toolbar->addSeparator(); 382 toolbar->addSeparator();
386 383
387 { 384 {
388 QToolButton* button = new QToolButton(toolbar); 385 QToolButton* button = new QToolButton(toolbar);
480 #endif 477 #endif
481 } 478 }
482 479
483 void MainWindow::closeEvent(QCloseEvent* event) { 480 void MainWindow::closeEvent(QCloseEvent* event) {
484 session.config.Save(); 481 session.config.Save();
482 Anime::db.SaveDatabaseToDisk();
485 event->accept(); 483 event->accept();
486 } 484 }
487 485
488 #include "gui/moc_window.cpp" 486 #include "gui/moc_window.cpp"
489 #include "gui/window.moc"