comparison src/gui/window.cpp @ 9:5c0397762b53

INCOMPLETE: megacommit :)
author Paper <mrpapersonic@gmail.com>
date Sun, 10 Sep 2023 03:59:16 -0400
parents src/main.cpp@b1f73678ef61
children 4b198a111713
comparison
equal deleted inserted replaced
8:b1f73678ef61 9:5c0397762b53
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"
10 #include <QApplication>
11 #include <QFile>
12 #include <QMainWindow>
13 #include <QMenuBar>
14 #include <QPlainTextEdit>
15 #include <QStackedWidget>
16 #include <QTextStream>
17 #if MACOSX
18 #include "sys/osx/dark_theme.h"
19 #elif WIN32
20 #include "sys/win32/dark_theme.h"
21 #endif
22
23 /* note that this code was originally created for use in
24 wxWidgets, but I thought the API was a little meh, so
25 I switched to Qt. */
26
27 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
28 main_widget = new QWidget(parent);
29 /* Menu Bar */
30 QAction* action;
31 QMenuBar* menubar = new QMenuBar(parent);
32 QMenu* menu = menubar->addMenu("&File");
33 QMenu* submenu = menu->addMenu("&Library folders");
34 action = new QAction("&Add new folder...");
35 submenu->addAction(action);
36 action = new QAction("&Scan available episodes");
37 menu->addAction(action);
38
39 menu->addSeparator();
40
41 action = menu->addAction("Play &next episode");
42 action = menu->addAction("Play &random episode");
43 menu->addSeparator();
44 action = menu->addAction("E&xit", qApp, &QApplication::quit);
45
46 menu = menubar->addMenu("&Services");
47 action = new QAction("Synchronize &list");
48
49 menu->addSeparator();
50
51 submenu = menu->addMenu("&AniList");
52 action = submenu->addAction("Go to my &profile");
53 action = submenu->addAction("Go to my &stats");
54
55 submenu = menu->addMenu("&Kitsu");
56 action = submenu->addAction("Go to my &feed");
57 action = submenu->addAction("Go to my &library");
58 action = submenu->addAction("Go to my &profile");
59
60 submenu = menu->addMenu("&MyAnimeList");
61 action = submenu->addAction("Go to my p&anel");
62 action = submenu->addAction("Go to my &profile");
63 action = submenu->addAction("Go to my &history");
64
65 menu = menubar->addMenu("&Tools");
66 submenu = menu->addMenu("&Export anime list");
67 action = submenu->addAction("Export as &Markdown...");
68 action = submenu->addAction("Export as MyAnimeList &XML...");
69
70 menu->addSeparator();
71
72 action = menu->addAction("Enable anime &recognition");
73 action->setCheckable(true);
74 action = menu->addAction("Enable auto &sharing");
75 action->setCheckable(true);
76 action = menu->addAction("Enable &auto synchronization");
77 action->setCheckable(true);
78
79 menu->addSeparator();
80
81 action = menu->addAction("&Settings", [this] {
82 SettingsDialog dialog(this);
83 dialog.exec();
84 });
85
86 setMenuBar(menubar);
87
88 SideBar* sidebar = new SideBar(main_widget);
89 sidebar->AddItem("Now Playing", SideBar::CreateIcon(":/icons/16x16/film.png"));
90 sidebar->AddSeparator();
91 sidebar->AddItem("Anime List", SideBar::CreateIcon(":/icons/16x16/document-list.png"));
92 sidebar->AddItem("History", SideBar::CreateIcon(":/icons/16x16/clock-history-frame.png"));
93 sidebar->AddItem("Statistics", SideBar::CreateIcon(":/icons/16x16/chart.png"));
94 sidebar->AddSeparator();
95 sidebar->AddItem("Search", SideBar::CreateIcon(":/icons/16x16/magnifier.png"));
96 sidebar->AddItem("Seasons", SideBar::CreateIcon(":/icons/16x16/calendar.png"));
97 sidebar->AddItem("Torrents", SideBar::CreateIcon(":/icons/16x16/feed.png"));
98 sidebar->setFixedWidth(128);
99 sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
100
101 QStackedWidget* stack = new QStackedWidget(main_widget);
102 stack->addWidget(new NowPlayingWidget(parent));
103 stack->addWidget(new AnimeListWidget(parent));
104 stack->addWidget(new StatisticsWidget(parent));
105
106 connect(sidebar, &SideBar::CurrentItemChanged, stack, [stack](int index) {
107 switch (index) {
108 case 0:
109 case 1: stack->setCurrentIndex(index); break;
110 case 3: stack->setCurrentIndex(2); break;
111 default: break;
112 }
113 });
114 sidebar->setCurrentRow(2);
115
116 QHBoxLayout* layout = new QHBoxLayout(main_widget);
117 layout->addWidget(sidebar, 0, Qt::AlignLeft | Qt::AlignTop);
118 layout->addWidget(stack);
119 setCentralWidget(main_widget);
120
121 ThemeChanged();
122 }
123
124 void MainWindow::SetStyleSheet(enum Themes theme) {
125 switch (theme) {
126 case Themes::DARK: {
127 QFile f(":qdarkstyle/dark/darkstyle.qss");
128 if (!f.exists())
129 return; // fail
130 f.open(QFile::ReadOnly | QFile::Text);
131 QTextStream ts(&f);
132 setStyleSheet(ts.readAll());
133 break;
134 }
135 default: setStyleSheet(""); break;
136 }
137 }
138
139 void MainWindow::ThemeChanged() {
140 switch (session.config.theme) {
141 case Themes::LIGHT: {
142 #if MACOSX
143 if (osx::DarkThemeAvailable())
144 osx::SetToLightTheme();
145 else
146 #else
147 SetStyleSheet(Themes::LIGHT);
148 #endif
149 break;
150 }
151 case Themes::DARK: {
152 #if MACOSX
153 if (osx::DarkThemeAvailable())
154 osx::SetToDarkTheme();
155 else
156 #else
157 SetStyleSheet(Themes::DARK);
158 #endif
159 break;
160 }
161 case Themes::OS: {
162 #if MACOSX
163 if (osx::DarkThemeAvailable())
164 osx::SetToAutoTheme();
165 else
166 #elif defined(WIN32)
167 if (win32::DarkThemeAvailable()) {
168 if (win32::IsInDarkTheme()) {
169 SetStyleSheet(Themes::DARK);
170 } else {
171 SetStyleSheet(Themes::LIGHT);
172 }
173 } else
174 #endif
175 /* Currently OS detection only supports Windows and macOS.
176 Please don't be shy if you're willing to port it to other OSes
177 (or desktop environments, or window managers) */
178 SetStyleSheet(Themes::LIGHT);
179 break;
180 }
181 }
182 }
183
184 void MainWindow::SetActivePage(QWidget* page) {
185 this->setCentralWidget(page);
186 }
187
188 void MainWindow::closeEvent(QCloseEvent* event) {
189 session.config.Save();
190 event->accept();
191 }
192
193 #include "gui/moc_window.cpp"