Mercurial > minori
comparison src/gui/window.cc @ 170:c8375765f0fc
window: make threading somewhat sane
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 21 Nov 2023 11:04:13 -0500 |
parents | 79a2a24453fa |
children | f88eda79c60a |
comparison
equal
deleted
inserted
replaced
169:e44b7c428d7c | 170:c8375765f0fc |
---|---|
40 # include "sys/osx/dark_theme.h" | 40 # include "sys/osx/dark_theme.h" |
41 #elif defined(WIN32) | 41 #elif defined(WIN32) |
42 # include "sys/win32/dark_theme.h" | 42 # include "sys/win32/dark_theme.h" |
43 #endif | 43 #endif |
44 | 44 |
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 { | 45 enum class Pages { |
65 NOW_PLAYING, | 46 NOW_PLAYING, |
66 | 47 |
67 ANIME_LIST, | 48 ANIME_LIST, |
68 HISTORY, | 49 HISTORY, |
71 SEARCH, | 52 SEARCH, |
72 SEASONS, | 53 SEASONS, |
73 TORRENTS | 54 TORRENTS |
74 }; | 55 }; |
75 | 56 |
57 void PlayingThread::run() { | |
58 std::vector<std::string> files; | |
59 Track::Media::GetCurrentlyPlaying(files); | |
60 emit Done(files); | |
61 } | |
62 | |
76 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { | 63 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { |
77 setWindowIcon(QIcon(":/favicon.png")); | 64 setWindowIcon(QIcon(":/favicon.png")); |
78 | 65 |
79 main_widget.reset(new QWidget(this)); | 66 main_widget.reset(new QWidget(this)); |
80 /*QHBoxLayout* layout = */new QHBoxLayout(main_widget.get()); | 67 /*QHBoxLayout* layout = */new QHBoxLayout(main_widget.get()); |
87 | 74 |
88 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); | 75 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); |
89 | 76 |
90 qRegisterMetaType<std::vector<std::string>>(); | 77 qRegisterMetaType<std::vector<std::string>>(); |
91 | 78 |
79 thread.reset(new PlayingThread(this)); | |
80 | |
92 QTimer* timer = new QTimer; | 81 QTimer* timer = new QTimer; |
93 timer->start(5000); | |
94 | 82 |
95 connect(timer, &QTimer::timeout, this, [this, page] { | 83 connect(timer, &QTimer::timeout, this, [this, page] { |
96 Thread* thread = new Thread(this); | 84 if (!thread.get() || thread->isRunning()) |
97 connect(thread, &QThread::finished, thread, &QThread::deleteLater); | 85 return; |
98 connect(thread, &Thread::Done, this, [page](const std::vector<std::string>& files) { | 86 connect(thread.get(), &QThread::finished, thread.get(), &QThread::deleteLater); |
87 connect(thread.get(), &PlayingThread::Done, this, [page](const std::vector<std::string>& files) { | |
99 for (const auto& file : files) { | 88 for (const auto& file : files) { |
100 anitomy::Anitomy anitomy; | 89 anitomy::Anitomy anitomy; |
101 anitomy.Parse(Strings::ToWstring(file)); | 90 anitomy.Parse(Strings::ToWstring(file)); |
102 | 91 |
103 const auto& elements = anitomy.elements(); | 92 const auto& elements = anitomy.elements(); |
110 break; | 99 break; |
111 } | 100 } |
112 }); | 101 }); |
113 thread->start(); | 102 thread->start(); |
114 }); | 103 }); |
104 | |
105 timer->start(5000); | |
106 timer->moveToThread(thread.get()); | |
115 } | 107 } |
116 | 108 |
117 void MainWindow::AddMainWidgets() { | 109 void MainWindow::AddMainWidgets() { |
118 int page = static_cast<int>(Pages::ANIME_LIST); | 110 int page = static_cast<int>(Pages::ANIME_LIST); |
119 if (sidebar.get()) { | 111 if (sidebar.get()) { |
330 page_to_index_map[action] = 6; | 322 page_to_index_map[action] = 6; |
331 } | 323 } |
332 | 324 |
333 /* pain in my ass */ | 325 /* pain in my ass */ |
334 connect(sidebar.get(), &SideBar::CurrentItemChanged, this, | 326 connect(sidebar.get(), &SideBar::CurrentItemChanged, this, |
335 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); }); | 327 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); }); |
336 | 328 |
337 connect(pages_group, &QActionGroup::triggered, this, | 329 connect(pages_group, &QActionGroup::triggered, this, |
338 [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); }); | 330 [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); }); |
339 } | 331 } |
340 | 332 |
341 menu->addSeparator(); | 333 menu->addSeparator(); |
342 | 334 |
343 // { | 335 // { |
378 | 370 |
379 { | 371 { |
380 /* Toolbar */ | 372 /* Toolbar */ |
381 QToolBar* toolbar = new QToolBar(this); | 373 QToolBar* toolbar = new QToolBar(this); |
382 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), | 374 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), |
383 [this] { AsyncSynchronize(stack.get()); }); | 375 [this] { AsyncSynchronize(stack.get()); }); |
384 | 376 |
385 toolbar->addSeparator(); | 377 toolbar->addSeparator(); |
386 | 378 |
387 { | 379 { |
388 QToolButton* button = new QToolButton(toolbar); | 380 QToolButton* button = new QToolButton(toolbar); |
484 session.config.Save(); | 476 session.config.Save(); |
485 event->accept(); | 477 event->accept(); |
486 } | 478 } |
487 | 479 |
488 #include "gui/moc_window.cpp" | 480 #include "gui/moc_window.cpp" |
489 #include "gui/window.moc" |