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