Mercurial > minori
annotate src/gui/window.cpp @ 77:6f7385bd334c
*: update
formatted all source files, no more subclassing QThread... many other
changes :)
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 06 Oct 2023 06:18:53 -0400 |
parents | 27a19dd6cba1 |
children | c489dd4434af |
rev | line source |
---|---|
9 | 1 #include "gui/window.h" |
64 | 2 #include "core/anime_db.h" |
9 | 3 #include "core/config.h" |
4 #include "core/session.h" | |
64 | 5 #include "core/strings.h" |
46 | 6 #include "gui/dark_theme.h" |
62 | 7 #include "gui/dialog/about.h" |
9 | 8 #include "gui/dialog/settings.h" |
9 #include "gui/pages/anime_list.h" | |
62 | 10 #include "gui/pages/history.h" |
9 | 11 #include "gui/pages/now_playing.h" |
62 | 12 #include "gui/pages/search.h" |
13 #include "gui/pages/seasons.h" | |
9 | 14 #include "gui/pages/statistics.h" |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
51
diff
changeset
|
15 #include "gui/pages/torrents.h" |
46 | 16 #include "gui/widgets/sidebar.h" |
15 | 17 #include "services/services.h" |
64 | 18 #include "track/media.h" |
63 | 19 #include <QActionGroup> |
7 | 20 #include <QApplication> |
64 | 21 #include <QDebug> |
9 | 22 #include <QFile> |
69 | 23 #include <QHBoxLayout> |
7 | 24 #include <QMainWindow> |
25 #include <QMenuBar> | |
63 | 26 #include <QMessageBox> |
7 | 27 #include <QPlainTextEdit> |
28 #include <QStackedWidget> | |
63 | 29 #include <QTextStream> |
77 | 30 #include <QThreadPool> |
62 | 31 #include <QTimer> |
69 | 32 #include <QToolBar> |
33 #include <QToolButton> | |
5 | 34 #if MACOSX |
15 | 35 # include "sys/osx/dark_theme.h" |
46 | 36 #elif defined(WIN32) |
15 | 37 # include "sys/win32/dark_theme.h" |
2 | 38 #endif |
39 | |
10 | 40 enum class Pages { |
41 NOW_PLAYING, | |
42 | |
43 ANIME_LIST, | |
44 HISTORY, | |
45 STATISTICS, | |
46 | |
47 SEARCH, | |
48 SEASONS, | |
49 TORRENTS | |
50 }; | |
51 | |
77 | 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 | |
9 | 59 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
60 main_widget = new QWidget(parent); |
10 | 61 |
62 SideBar* sidebar = new SideBar(main_widget); | |
51 | 63 sidebar->AddItem(tr("Now Playing"), SideBar::CreateIcon(":/icons/16x16/film.png")); |
10 | 64 sidebar->AddSeparator(); |
51 | 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")); | |
10 | 68 sidebar->AddSeparator(); |
51 | 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")); | |
10 | 72 sidebar->setFixedWidth(128); |
36 | 73 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); |
10 | 74 |
75 QStackedWidget* stack = new QStackedWidget(main_widget); | |
64 | 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)); | |
10 | 83 |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
51
diff
changeset
|
84 connect(sidebar, &SideBar::CurrentItemChanged, stack, &QStackedWidget::setCurrentIndex); |
64 | 85 sidebar->SetCurrentItem(static_cast<int>(Pages::ANIME_LIST)); |
10 | 86 |
2 | 87 /* Menu Bar */ |
88 QAction* action; | |
89 QMenuBar* menubar = new QMenuBar(parent); | |
51 | 90 QMenu* menu = menubar->addMenu(tr("&File")); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
91 |
51 | 92 QMenu* submenu = menu->addMenu(tr("&Library folders")); |
93 action = submenu->addAction(tr("&Add new folder...")); | |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
94 |
51 | 95 action = menu->addAction(tr("&Scan available episodes")); |
2 | 96 |
97 menu->addSeparator(); | |
98 | |
51 | 99 action = menu->addAction(tr("Play &next episode")); |
62 | 100 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N)); |
51 | 101 action = menu->addAction(tr("Play &random episode")); |
62 | 102 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_R)); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
103 |
2 | 104 menu->addSeparator(); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
105 |
51 | 106 action = menu->addAction(tr("E&xit"), qApp, &QApplication::quit); |
2 | 107 |
51 | 108 menu = menubar->addMenu(tr("&Services")); |
77 | 109 action = menu->addAction(tr("Synchronize &list"), [stack] { AsyncSynchronize(stack); }); |
62 | 110 action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); |
2 | 111 |
112 menu->addSeparator(); | |
113 | |
51 | 114 submenu = menu->addMenu(tr("&AniList")); |
115 action = submenu->addAction(tr("Go to my &profile")); | |
116 action = submenu->addAction(tr("Go to my &stats")); | |
2 | 117 |
51 | 118 submenu = menu->addMenu(tr("&Kitsu")); |
119 action = submenu->addAction(tr("Go to my &feed")); | |
120 action = submenu->addAction(tr("Go to my &library")); | |
121 action = submenu->addAction(tr("Go to my &profile")); | |
2 | 122 |
51 | 123 submenu = menu->addMenu(tr("&MyAnimeList")); |
124 action = submenu->addAction(tr("Go to my p&anel")); | |
125 action = submenu->addAction(tr("Go to my &profile")); | |
126 action = submenu->addAction(tr("Go to my &history")); | |
2 | 127 |
51 | 128 menu = menubar->addMenu(tr("&Tools")); |
129 submenu = menu->addMenu(tr("&Export anime list")); | |
130 action = submenu->addAction(tr("Export as &Markdown...")); | |
131 action = submenu->addAction(tr("Export as MyAnimeList &XML...")); | |
2 | 132 |
133 menu->addSeparator(); | |
134 | |
51 | 135 action = menu->addAction(tr("Enable anime &recognition")); |
2 | 136 action->setCheckable(true); |
51 | 137 action = menu->addAction(tr("Enable auto &sharing")); |
2 | 138 action->setCheckable(true); |
51 | 139 action = menu->addAction(tr("Enable &auto synchronization")); |
2 | 140 action->setCheckable(true); |
141 | |
142 menu->addSeparator(); | |
143 | |
51 | 144 action = menu->addAction(tr("&Settings"), [this] { |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
145 SettingsDialog dialog(this); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
146 dialog.exec(); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
147 }); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
148 action->setMenuRole(QAction::PreferencesRole); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
149 |
51 | 150 menu = menubar->addMenu(tr("&View")); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
151 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
152 std::map<QAction*, int> page_to_index_map = {}; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
153 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
154 QActionGroup* pages_group = new QActionGroup(this); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
155 pages_group->setExclusive(true); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
156 |
51 | 157 action = pages_group->addAction(menu->addAction(tr("&Now Playing"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
158 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
159 page_to_index_map[action] = 0; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
160 |
51 | 161 action = pages_group->addAction(menu->addAction(tr("&Anime List"))); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
162 page_to_index_map[action] = 1; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
163 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
164 action->setChecked(true); |
58
b7a1c0010ffd
sidebar: link view menu and sidebar together
Paper <mrpapersonic@gmail.com>
parents:
54
diff
changeset
|
165 |
51 | 166 action = pages_group->addAction(menu->addAction(tr("&History"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
167 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
168 page_to_index_map[action] = 2; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
169 |
51 | 170 action = pages_group->addAction(menu->addAction(tr("&Statistics"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
171 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
172 page_to_index_map[action] = 3; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
173 |
51 | 174 action = pages_group->addAction(menu->addAction(tr("S&earch"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
175 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
176 page_to_index_map[action] = 4; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
177 |
51 | 178 action = pages_group->addAction(menu->addAction(tr("Se&asons"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
179 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
180 page_to_index_map[action] = 5; |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
181 |
51 | 182 action = pages_group->addAction(menu->addAction(tr("&Torrents"))); |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
183 action->setCheckable(true); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
184 page_to_index_map[action] = 6; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
185 |
63 | 186 connect(sidebar, &SideBar::CurrentItemChanged, this, |
187 [pages_group](int index) { pages_group->actions()[index]->setChecked(true); }); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
51
diff
changeset
|
188 |
63 | 189 connect(pages_group, &QActionGroup::triggered, this, |
190 [page_to_index_map, sidebar](QAction* action) { sidebar->SetCurrentItem(page_to_index_map.at(action)); }); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
51
diff
changeset
|
191 |
48
e613772f41d5
statistics.cpp: show requests made
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
192 menu->addSeparator(); |
51 | 193 menu->addAction(tr("Show sidebar")); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
194 |
51 | 195 menu = menubar->addMenu(tr("&Help")); |
64 | 196 action = menu->addAction(tr("&About Minori"), this, [this] { |
51 | 197 AboutWindow dialog(this); |
198 dialog.exec(); | |
199 }); | |
200 action = menu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt); | |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
201 action->setMenuRole(QAction::AboutQtRole); |
2 | 202 |
203 setMenuBar(menubar); | |
7 | 204 |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
205 /* Toolbar */ |
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
206 QToolBar* toolbar = new QToolBar(this); |
77 | 207 toolbar->addAction(QIcon(":/icons/24x24/arrow-circle-double-135.png"), tr("&Synchronize"), |
208 [stack] { AsyncSynchronize(stack); }); | |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
209 toolbar->addSeparator(); |
69 | 210 |
211 QToolButton* button = new QToolButton(toolbar); | |
212 | |
213 menu = new QMenu(button); | |
214 action = menu->addAction(tr("Add new folder...")); | |
215 | |
216 button->setMenu(menu); | |
217 button->setIcon(QIcon(":/icons/24x24/folder-open.png")); | |
218 button->setPopupMode(QToolButton::InstantPopup); | |
219 toolbar->addWidget(button); | |
220 | |
221 button = new QToolButton(toolbar); | |
222 | |
223 menu = new QMenu(button); | |
224 action = menu->addAction(tr("Placeholder")); | |
225 | |
226 button->setMenu(menu); | |
227 button->setIcon(QIcon(":/icons/24x24/application-export.png")); | |
228 button->setPopupMode(QToolButton::InstantPopup); | |
229 toolbar->addWidget(button); | |
230 | |
231 toolbar->addSeparator(); | |
232 toolbar->addAction(QIcon(":/icons/24x24/gear.png"), tr("S&ettings"), [this] { | |
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
233 SettingsDialog dialog(this); |
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
234 dialog.exec(); |
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
235 }); |
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
236 addToolBar(toolbar); |
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
237 |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
238 QHBoxLayout* layout = new QHBoxLayout(main_widget); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
239 layout->addWidget(sidebar); |
7 | 240 layout->addWidget(stack); |
241 setCentralWidget(main_widget); | |
2 | 242 |
64 | 243 QTimer* timer = new QTimer(this); |
244 connect(timer, &QTimer::timeout, this, [stack] { | |
245 NowPlayingPage* page = reinterpret_cast<NowPlayingPage*>(stack->widget(static_cast<int>(Pages::NOW_PLAYING))); | |
246 | |
247 Filesystem::Path p = Track::Media::GetCurrentPlaying(); | |
248 std::string title = Track::Media::GetFileTitle(p); | |
249 int id = Anime::db.GetAnimeFromTitle(title); | |
250 if (id == 0) { | |
251 page->SetDefault(); | |
252 return; | |
253 } | |
69 | 254 |
64 | 255 page->SetPlaying(id); |
256 }); | |
257 timer->start(5000); | |
258 | |
46 | 259 DarkTheme::SetTheme(session.config.theme); |
2 | 260 } |
261 | |
262 void MainWindow::SetActivePage(QWidget* page) { | |
263 this->setCentralWidget(page); | |
264 } | |
265 | |
266 void MainWindow::closeEvent(QCloseEvent* event) { | |
267 session.config.Save(); | |
268 event->accept(); | |
269 } | |
270 | |
9 | 271 #include "gui/moc_window.cpp" |