Mercurial > minori
comparison src/gui/window.cc @ 81:9b2b41f83a5e
boring: mass rename to cc
because this is a very unix-y project, it makes more sense to use the
'cc' extension
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 23 Oct 2023 12:07:27 -0400 |
parents | src/gui/window.cpp@825506f0e221 |
children | 8b65c417c225 |
comparison
equal
deleted
inserted
replaced
80:825506f0e221 | 81:9b2b41f83a5e |
---|---|
1 #include "gui/window.h" | |
2 #include "core/anime_db.h" | |
3 #include "core/config.h" | |
4 #include "core/session.h" | |
5 #include "core/strings.h" | |
6 #include "gui/dark_theme.h" | |
7 #include "gui/dialog/about.h" | |
8 #include "gui/dialog/settings.h" | |
9 #include "gui/pages/anime_list.h" | |
10 #include "gui/pages/history.h" | |
11 #include "gui/pages/now_playing.h" | |
12 #include "gui/pages/search.h" | |
13 #include "gui/pages/seasons.h" | |
14 #include "gui/pages/statistics.h" | |
15 #include "gui/pages/torrents.h" | |
16 #include "gui/widgets/sidebar.h" | |
17 #include "services/services.h" | |
18 #include "track/media.h" | |
19 #include <QActionGroup> | |
20 #include <QApplication> | |
21 #include <QDebug> | |
22 #include <QFile> | |
23 #include <QHBoxLayout> | |
24 #include <QMainWindow> | |
25 #include <QMenuBar> | |
26 #include <QMessageBox> | |
27 #include <QPlainTextEdit> | |
28 #include <QStackedWidget> | |
29 #include <QTextStream> | |
30 #include <QThreadPool> | |
31 #include <QTimer> | |
32 #include <QToolBar> | |
33 #include <QToolButton> | |
34 #if MACOSX | |
35 # include "sys/osx/dark_theme.h" | |
36 #elif defined(WIN32) | |
37 # include "sys/win32/dark_theme.h" | |
38 #endif | |
39 | |
40 enum class Pages { | |
41 NOW_PLAYING, | |
42 | |
43 ANIME_LIST, | |
44 HISTORY, | |
45 STATISTICS, | |
46 | |
47 SEARCH, | |
48 SEASONS, | |
49 TORRENTS | |
50 }; | |
51 | |
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) { | |
60 main_widget = new QWidget(parent); | |
61 | |
62 sidebar = new SideBar(main_widget); | |
63 sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png")); | |
64 sidebar->AddSeparator(); | |
65 sidebar->AddItem(tr("Anime List"), SideBar::CreateIcon(":/icons/16x16/document-list.png")); | |
66 sidebar->AddItem(tr("History"), SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png")); | |
67 sidebar->AddItem(tr("Statistics"), SideBar::CreateIcon(":/icons/16x16/chart.png")); | |
68 sidebar->AddSeparator(); | |
69 sidebar->AddItem(tr("Search"), SideBar::CreateIcon(":/icons/16x16/magnifier.png")); | |
70 sidebar->AddItem(tr("Seasons"), SideBar::CreateIcon(":/icons/16x16/calendar.png")); | |
71 sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/16x16/feed.png")); | |
72 sidebar->setFixedWidth(128); | |
73 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); | |
74 | |
75 stack = new QStackedWidget(main_widget); | |
76 stack->addWidget(new NowPlayingPage(main_widget)); | |
77 stack->addWidget(new AnimeListPage(main_widget)); | |
78 stack->addWidget(new HistoryPage(main_widget)); | |
79 stack->addWidget(new StatisticsPage(main_widget)); | |
80 stack->addWidget(new SearchPage(main_widget)); | |
81 stack->addWidget(new SeasonsPage(main_widget)); | |
82 stack->addWidget(new TorrentsPage(main_widget)); | |
83 | |
84 connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex); | |
85 sidebar->SetCurrentItem(static_cast<int>(Pages::ANIME_LIST)); | |
86 | |
87 QHBoxLayout* layout = new QHBoxLayout(main_widget); | |
88 layout->addWidget(sidebar); | |
89 layout->addWidget(stack); | |
90 setCentralWidget(main_widget); | |
91 | |
92 CreateBars(); | |
93 | |
94 QTimer* timer = new QTimer(this); | |
95 connect(timer, &QTimer::timeout, this, [this] { | |
96 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); | |
97 | |
98 Filesystem::Path p = Track::Media::GetCurrentPlaying(); | |
99 std::unordered_map<std::string, std::string> elements = Track::Media::GetFileElements(p); | |
100 int id = Anime::db.GetAnimeFromTitle(elements["title"]); | |
101 if (id == 0) { | |
102 page->SetDefault(); | |
103 return; | |
104 } | |
105 | |
106 page->SetPlaying(id, elements); | |
107 }); | |
108 timer->start(5000); | |
109 | |
110 DarkTheme::SetTheme(session.config.theme); | |
111 } | |
112 | |
113 void MainWindow::CreateBars() { | |
114 /* Menu Bar */ | |
115 QAction* action; | |
116 QMenuBar* menubar = new QMenuBar(this); | |
117 QMenu* menu = menubar->addMenu(tr("&File")); | |
118 | |
119 QMenu* submenu = menu->addMenu(tr("&Library folders")); | |
120 action = submenu->addAction(tr("&Add new folder...")); | |
121 | |
122 action = menu->addAction(tr("&Scan available episodes")); | |
123 | |
124 menu->addSeparator(); | |
125 | |
126 action = menu->addAction(tr("Play &next episode")); | |
127 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N)); | |
128 action = menu->addAction(tr("Play &random episode")); | |
129 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_R)); | |
130 | |
131 menu->addSeparator(); | |
132 | |
133 action = menu->addAction(tr("E&xit"), qApp, &QApplication::quit); | |
134 | |
135 menu = menubar->addMenu(tr("&Services")); | |
136 action = menu->addAction(tr("Synchronize &list"), [this] { AsyncSynchronize(stack); }); | |
137 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); | |
138 | |
139 menu->addSeparator(); | |
140 | |
141 submenu = menu->addMenu(tr("&AniList")); | |
142 action = submenu->addAction(tr("Go to my &profile")); | |
143 action = submenu->addAction(tr("Go to my &stats")); | |
144 | |
145 submenu = menu->addMenu(tr("&Kitsu")); | |
146 action = submenu->addAction(tr("Go to my &feed")); | |
147 action = submenu->addAction(tr("Go to my &library")); | |
148 action = submenu->addAction(tr("Go to my &profile")); | |
149 | |
150 submenu = menu->addMenu(tr("&MyAnimeList")); | |
151 action = submenu->addAction(tr("Go to my p&anel")); | |
152 action = submenu->addAction(tr("Go to my &profile")); | |
153 action = submenu->addAction(tr("Go to my &history")); | |
154 | |
155 menu = menubar->addMenu(tr("&Tools")); | |
156 submenu = menu->addMenu(tr("&Export anime list")); | |
157 action = submenu->addAction(tr("Export as &Markdown...")); | |
158 action = submenu->addAction(tr("Export as MyAnimeList &XML...")); | |
159 | |
160 menu->addSeparator(); | |
161 | |
162 action = menu->addAction(tr("Enable anime &recognition")); | |
163 action->setCheckable(true); | |
164 action = menu->addAction(tr("Enable auto &sharing")); | |
165 action->setCheckable(true); | |
166 action = menu->addAction(tr("Enable &auto synchronization")); | |
167 action->setCheckable(true); | |
168 | |
169 menu->addSeparator(); | |
170 | |
171 action = menu->addAction(tr("&Settings"), [this] { | |
172 SettingsDialog dialog(this); | |
173 dialog.exec(); | |
174 }); | |
175 action->setMenuRole(QAction::PreferencesRole); | |
176 | |
177 menu = menubar->addMenu(tr("&View")); | |
178 | |
179 std::map<QAction*, int> page_to_index_map = {}; | |
180 | |
181 QActionGroup* pages_group = new QActionGroup(this); | |
182 pages_group->setExclusive(true); | |
183 | |
184 action = pages_group->addAction(menu->addAction(tr("&Now Playing"))); | |
185 action->setCheckable(true); | |
186 page_to_index_map[action] = 0; | |
187 | |
188 action = pages_group->addAction(menu->addAction(tr("&Anime List"))); | |
189 page_to_index_map[action] = 1; | |
190 action->setCheckable(true); | |
191 action->setChecked(true); | |
192 | |
193 action = pages_group->addAction(menu->addAction(tr("&History"))); | |
194 action->setCheckable(true); | |
195 page_to_index_map[action] = 2; | |
196 | |
197 action = pages_group->addAction(menu->addAction(tr("&Statistics"))); | |
198 action->setCheckable(true); | |
199 page_to_index_map[action] = 3; | |
200 | |
201 action = pages_group->addAction(menu->addAction(tr("S&earch"))); | |
202 action->setCheckable(true); | |
203 page_to_index_map[action] = 4; | |
204 | |
205 action = pages_group->addAction(menu->addAction(tr("Se&asons"))); | |
206 action->setCheckable(true); | |
207 page_to_index_map[action] = 5; | |
208 | |
209 action = pages_group->addAction(menu->addAction(tr("&Torrents"))); | |
210 action->setCheckable(true); | |
211 page_to_index_map[action] = 6; | |
212 | |
213 connect(sidebar, &SideBar::CurrentItemChanged, this, | |
214 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); }); | |
215 | |
216 connect(pages_group, &QActionGroup::triggered, this, | |
217 [this, page_to_index_map](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); }); | |
218 | |
219 menu->addSeparator(); | |
220 menu->addAction(tr("Show sidebar")); | |
221 | |
222 menu = menubar->addMenu(tr("&Help")); | |
223 action = menu->addAction(tr("&About Minori"), this, [this] { | |
224 AboutWindow dialog(this); | |
225 dialog.exec(); | |
226 }); | |
227 action = menu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); | |
228 action->setMenuRole(QAction::AboutQtRole); | |
229 | |
230 setMenuBar(menubar); | |
231 | |
232 /* Toolbar */ | |
233 /* remove old toolbar(s) */ | |
234 QList<QToolBar*> toolbars = findChildren<QToolBar*>(); | |
235 for (auto& t : toolbars) | |
236 removeToolBar(t); | |
237 | |
238 QToolBar* toolbar = new QToolBar(this); | |
239 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), | |
240 [this] { AsyncSynchronize(stack); }); | |
241 toolbar->addSeparator(); | |
242 | |
243 QToolButton* button = new QToolButton(toolbar); | |
244 | |
245 menu = new QMenu(button); | |
246 action = menu->addAction(tr("Add new folder...")); | |
247 | |
248 button->setMenu(menu); | |
249 button->setIcon(QIcon(":/icons/24x24/folder-open.png")); | |
250 button->setPopupMode(QToolButton::InstantPopup); | |
251 toolbar->addWidget(button); | |
252 | |
253 button = new QToolButton(toolbar); | |
254 | |
255 menu = new QMenu(button); | |
256 action = menu->addAction(tr("Placeholder")); | |
257 | |
258 button->setMenu(menu); | |
259 button->setIcon(QIcon(":/icons/24x24/application-export.png")); | |
260 button->setPopupMode(QToolButton::InstantPopup); | |
261 toolbar->addWidget(button); | |
262 | |
263 toolbar->addSeparator(); | |
264 toolbar->addAction(QIcon(":/icons/24x24/gear.png"), tr("S&ettings"), [this] { | |
265 SettingsDialog dialog(this); | |
266 dialog.exec(); | |
267 }); | |
268 addToolBar(toolbar); | |
269 | |
270 } | |
271 | |
272 void MainWindow::SetActivePage(QWidget* page) { | |
273 this->setCentralWidget(page); | |
274 } | |
275 | |
276 void MainWindow::closeEvent(QCloseEvent* event) { | |
277 session.config.Save(); | |
278 event->accept(); | |
279 } | |
280 | |
281 #include "gui/moc_window.cpp" |