Mercurial > minori
annotate src/gui/window.cpp @ 44:619cbd6e69f9
filesystem: fix CreateDirectories function
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 22 Sep 2023 13:52:11 -0400 |
parents | 9ae9365dd4ea |
children | d0adc4aedfc8 |
rev | line source |
---|---|
9 | 1 #include "gui/window.h" |
2 #include "core/config.h" | |
3 #include "core/session.h" | |
4 #include "gui/dialog/settings.h" | |
5 #include "gui/pages/anime_list.h" | |
6 #include "gui/pages/now_playing.h" | |
7 #include "gui/pages/statistics.h" | |
8 #include "gui/sidebar.h" | |
9 #include "gui/ui_utils.h" | |
15 | 10 #include "services/services.h" |
7 | 11 #include <QApplication> |
9 | 12 #include <QFile> |
7 | 13 #include <QMainWindow> |
14 #include <QMenuBar> | |
15 #include <QPlainTextEdit> | |
16 #include <QStackedWidget> | |
17 #include <QTextStream> | |
5 | 18 #if MACOSX |
15 | 19 # include "sys/osx/dark_theme.h" |
2 | 20 #elif WIN32 |
15 | 21 # include "sys/win32/dark_theme.h" |
2 | 22 #endif |
23 | |
10 | 24 enum class Pages { |
25 NOW_PLAYING, | |
26 | |
27 ANIME_LIST, | |
28 HISTORY, | |
29 STATISTICS, | |
30 | |
31 SEARCH, | |
32 SEASONS, | |
33 TORRENTS | |
34 }; | |
35 | |
9 | 36 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
37 main_widget = new QWidget(parent); |
10 | 38 |
39 SideBar* sidebar = new SideBar(main_widget); | |
40 sidebar->AddItem("Now Playing", SideBar::CreateIcon(":/icons/16x16/film.png")); | |
41 sidebar->AddSeparator(); | |
42 sidebar->AddItem("Anime List", SideBar::CreateIcon(":/icons/16x16/document-list.png")); | |
43 sidebar->AddItem("History", SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png")); | |
44 sidebar->AddItem("Statistics", SideBar::CreateIcon(":/icons/16x16/chart.png")); | |
45 sidebar->AddSeparator(); | |
46 sidebar->AddItem("Search", SideBar::CreateIcon(":/icons/16x16/magnifier.png")); | |
47 sidebar->AddItem("Seasons", SideBar::CreateIcon(":/icons/16x16/calendar.png")); | |
48 sidebar->AddItem("Torrents", SideBar::CreateIcon(":/icons/16x16/feed.png")); | |
49 sidebar->setFixedWidth(128); | |
36 | 50 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); |
10 | 51 |
52 QStackedWidget* stack = new QStackedWidget(main_widget); | |
53 stack->addWidget(new NowPlayingWidget(main_widget)); | |
54 stack->addWidget(new AnimeListWidget(main_widget)); | |
55 stack->addWidget(new StatisticsWidget(main_widget)); | |
56 | |
57 connect(sidebar, &SideBar::CurrentItemChanged, stack, [stack](int index) { | |
58 switch (index) { | |
59 case 0: | |
60 case 1: stack->setCurrentIndex(index); break; | |
61 case 3: stack->setCurrentIndex(2); break; | |
62 default: break; | |
63 } | |
64 }); | |
65 sidebar->setCurrentRow(2); | |
66 | |
2 | 67 /* Menu Bar */ |
68 QAction* action; | |
69 QMenuBar* menubar = new QMenuBar(parent); | |
70 QMenu* menu = menubar->addMenu("&File"); | |
71 QMenu* submenu = menu->addMenu("&Library folders"); | |
10 | 72 action = submenu->addAction("&Add new folder..."); |
73 action = menu->addAction("&Scan available episodes"); | |
2 | 74 |
75 menu->addSeparator(); | |
76 | |
77 action = menu->addAction("Play &next episode"); | |
78 action = menu->addAction("Play &random episode"); | |
79 menu->addSeparator(); | |
80 action = menu->addAction("E&xit", qApp, &QApplication::quit); | |
81 | |
82 menu = menubar->addMenu("&Services"); | |
10 | 83 action = menu->addAction("Synchronize &list", [this, stack] { |
84 Services::Synchronize(); | |
15 | 85 ((AnimeListWidget*)stack->widget((int)Pages::ANIME_LIST))->Refresh(); |
10 | 86 }); |
2 | 87 |
88 menu->addSeparator(); | |
89 | |
90 submenu = menu->addMenu("&AniList"); | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
91 action = submenu->addAction("Go to my &profile"); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
92 action = submenu->addAction("Go to my &stats"); |
2 | 93 |
94 submenu = menu->addMenu("&Kitsu"); | |
95 action = submenu->addAction("Go to my &feed"); | |
96 action = submenu->addAction("Go to my &library"); | |
97 action = submenu->addAction("Go to my &profile"); | |
98 | |
99 submenu = menu->addMenu("&MyAnimeList"); | |
100 action = submenu->addAction("Go to my p&anel"); | |
101 action = submenu->addAction("Go to my &profile"); | |
102 action = submenu->addAction("Go to my &history"); | |
103 | |
104 menu = menubar->addMenu("&Tools"); | |
105 submenu = menu->addMenu("&Export anime list"); | |
106 action = submenu->addAction("Export as &Markdown..."); | |
107 action = submenu->addAction("Export as MyAnimeList &XML..."); | |
108 | |
109 menu->addSeparator(); | |
110 | |
111 action = menu->addAction("Enable anime &recognition"); | |
112 action->setCheckable(true); | |
113 action = menu->addAction("Enable auto &sharing"); | |
114 action->setCheckable(true); | |
115 action = menu->addAction("Enable &auto synchronization"); | |
116 action->setCheckable(true); | |
117 | |
118 menu->addSeparator(); | |
119 | |
9 | 120 action = menu->addAction("&Settings", [this] { |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
121 SettingsDialog dialog(this); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
122 dialog.exec(); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
123 }); |
44
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
124 action->setMenuRole(QAction::PreferencesRole); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
125 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
126 menu = menubar->addMenu("&View"); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
127 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
128 std::map<QAction*, int> page_to_index_map = {}; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
129 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
130 QActionGroup* pages_group = new QActionGroup(this); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
131 pages_group->setExclusive(true); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
132 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
133 action = pages_group->addAction(menu->addAction("&Now Playing")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
134 page_to_index_map[action] = 0; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
135 action = pages_group->addAction(menu->addAction("&Anime List")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
136 page_to_index_map[action] = 1; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
137 action->setChecked(true); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
138 action = pages_group->addAction(menu->addAction("&History")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
139 page_to_index_map[action] = 2; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
140 action = pages_group->addAction(menu->addAction("&Statistics")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
141 page_to_index_map[action] = 3; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
142 action = pages_group->addAction(menu->addAction("S&earch")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
143 page_to_index_map[action] = 4; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
144 action = pages_group->addAction(menu->addAction("Se&asons")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
145 page_to_index_map[action] = 5; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
146 action = pages_group->addAction(menu->addAction("&Torrents")); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
147 page_to_index_map[action] = 6; |
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 connect(pages_group, &QActionGroup::triggered, this, [page_to_index_map, stack](QAction* action) { |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
150 int index = page_to_index_map.at(action); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
151 switch (index) { |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
152 case 0: |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
153 case 1: stack->setCurrentIndex(index); break; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
154 case 3: stack->setCurrentIndex(2); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
155 default: break; |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
156 } |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
157 }); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
158 |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
159 menu = menubar->addMenu("&Help"); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
160 action = menu->addAction("About &Qt", qApp, [this]{ |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
161 qApp->aboutQt(); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
162 }); |
619cbd6e69f9
filesystem: fix CreateDirectories function
Paper <mrpapersonic@gmail.com>
parents:
37
diff
changeset
|
163 action->setMenuRole(QAction::AboutQtRole); |
2 | 164 |
165 setMenuBar(menubar); | |
7 | 166 |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
5
diff
changeset
|
167 QHBoxLayout* layout = new QHBoxLayout(main_widget); |
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
168 layout->addWidget(sidebar); |
7 | 169 layout->addWidget(stack); |
170 setCentralWidget(main_widget); | |
2 | 171 |
172 ThemeChanged(); | |
173 } | |
174 | |
175 void MainWindow::SetStyleSheet(enum Themes theme) { | |
176 switch (theme) { | |
9 | 177 case Themes::DARK: { |
2 | 178 QFile f(":qdarkstyle/dark/darkstyle.qss"); |
179 if (!f.exists()) | |
180 return; // fail | |
181 f.open(QFile::ReadOnly | QFile::Text); | |
182 QTextStream ts(&f); | |
183 setStyleSheet(ts.readAll()); | |
184 break; | |
185 } | |
9 | 186 default: setStyleSheet(""); break; |
2 | 187 } |
188 } | |
189 | |
190 void MainWindow::ThemeChanged() { | |
191 switch (session.config.theme) { | |
9 | 192 case Themes::LIGHT: { |
5 | 193 #if MACOSX |
2 | 194 if (osx::DarkThemeAvailable()) |
195 osx::SetToLightTheme(); | |
196 else | |
197 #else | |
9 | 198 SetStyleSheet(Themes::LIGHT); |
2 | 199 #endif |
9 | 200 break; |
2 | 201 } |
9 | 202 case Themes::DARK: { |
5 | 203 #if MACOSX |
2 | 204 if (osx::DarkThemeAvailable()) |
205 osx::SetToDarkTheme(); | |
206 else | |
207 #else | |
9 | 208 SetStyleSheet(Themes::DARK); |
2 | 209 #endif |
9 | 210 break; |
2 | 211 } |
9 | 212 case Themes::OS: { |
5 | 213 #if MACOSX |
2 | 214 if (osx::DarkThemeAvailable()) |
215 osx::SetToAutoTheme(); | |
216 else | |
217 #elif defined(WIN32) | |
218 if (win32::DarkThemeAvailable()) { | |
219 if (win32::IsInDarkTheme()) { | |
9 | 220 SetStyleSheet(Themes::DARK); |
2 | 221 } else { |
9 | 222 SetStyleSheet(Themes::LIGHT); |
2 | 223 } |
9 | 224 } else |
2 | 225 #endif |
9 | 226 /* Currently OS detection only supports Windows and macOS. |
227 Please don't be shy if you're willing to port it to other OSes | |
228 (or desktop environments, or window managers) */ | |
229 SetStyleSheet(Themes::LIGHT); | |
2 | 230 break; |
231 } | |
232 } | |
233 } | |
234 | |
235 void MainWindow::SetActivePage(QWidget* page) { | |
236 this->setCentralWidget(page); | |
237 } | |
238 | |
239 void MainWindow::closeEvent(QCloseEvent* event) { | |
240 session.config.Save(); | |
241 event->accept(); | |
242 } | |
243 | |
9 | 244 #include "gui/moc_window.cpp" |