view src/main.cpp @ 6:1d82f6e04d7d

Update: add first parts to the settings dialog
author Paper <mrpapersonic@gmail.com>
date Wed, 16 Aug 2023 00:49:17 -0400
parents 51ae25154b70
children 07a9095eaeed
line wrap: on
line source

#include "window.h"
#include "config.h"
#include "anime.h"
#include "sidebar.h"
#include "ui_utils.h"
#include "settings.h"
#if MACOSX
#include "sys/osx/dark_theme.h"
#elif WIN32
#include "sys/win32/dark_theme.h"
#endif

Session session = {
	.config = Config()
};

/* note that this code was originally created for use in 
   wxWidgets, but I thought the API was a little meh, so
   I switched to Qt. */

MainWindow::MainWindow(QWidget* parent) :
           QMainWindow(parent) {
	main_widget = new QWidget(parent);
	/* Menu Bar */
	QAction* action;
	QMenuBar* menubar = new QMenuBar(parent);
	QMenu* menu = menubar->addMenu("&File");
	QMenu* submenu = menu->addMenu("&Library folders");
	action = new QAction("&Add new folder...");
	submenu->addAction(action);
	action = new QAction("&Scan available episodes");
	menu->addAction(action);

	menu->addSeparator();

	action = menu->addAction("Play &next episode");
	action = menu->addAction("Play &random episode");
	menu->addSeparator();
	action = menu->addAction("E&xit", qApp, &QApplication::quit);

	menu = menubar->addMenu("&Services");
	action = new QAction("Synchronize &list");

	menu->addSeparator();

	submenu = menu->addMenu("&AniList");
	action = submenu->addAction("Go to my &profile");
	action = submenu->addAction("Go to my &stats");

	submenu = menu->addMenu("&Kitsu");
	action = submenu->addAction("Go to my &feed");
	action = submenu->addAction("Go to my &library");
	action = submenu->addAction("Go to my &profile");

	submenu = menu->addMenu("&MyAnimeList");
	action = submenu->addAction("Go to my p&anel");
	action = submenu->addAction("Go to my &profile");
	action = submenu->addAction("Go to my &history");

	menu = menubar->addMenu("&Tools");
	submenu = menu->addMenu("&Export anime list");
	action = submenu->addAction("Export as &Markdown...");
	action = submenu->addAction("Export as MyAnimeList &XML...");

	menu->addSeparator();

	action = menu->addAction("Enable anime &recognition");
	action->setCheckable(true);
	action = menu->addAction("Enable auto &sharing");
	action->setCheckable(true);
	action = menu->addAction("Enable &auto synchronization");
	action->setCheckable(true);

	menu->addSeparator();

	action = menu->addAction("&Settings", [this]{
		SettingsDialog dialog(this);
		dialog.exec();
	});

	setMenuBar(menubar);
	
	/* Side toolbar */
	SideBar* sidebar = new SideBar(main_widget);
	sidebar->AddItem("Now Playing", UiUtils::CreateSideBarIcon(":/icons/16x16/film.png"));
	sidebar->AddSeparator();
	sidebar->AddItem("Anime List", UiUtils::CreateSideBarIcon(":/icons/16x16/document-list.png"));
	sidebar->AddItem("History", UiUtils::CreateSideBarIcon(":/icons/16x16/clock-history-frame.png"));
	sidebar->AddItem("Statistics", UiUtils::CreateSideBarIcon(":/icons/16x16/chart.png"));
	sidebar->AddSeparator();
	sidebar->AddItem("Search", UiUtils::CreateSideBarIcon(":/icons/16x16/magnifier.png"));
	sidebar->AddItem("Seasons", UiUtils::CreateSideBarIcon(":/icons/16x16/calendar.png"));
	sidebar->AddItem("Torrents", UiUtils::CreateSideBarIcon(":/icons/16x16/feed.png"));
	sidebar->setFixedWidth(128);
	sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);

	anime_list_page = new AnimeListPage(parent);

	QHBoxLayout* layout = new QHBoxLayout(main_widget);
	layout->addWidget(sidebar, 0, Qt::AlignLeft | Qt::AlignTop);
	layout->addWidget(anime_list_page);
	SetActivePage(main_widget);
/*
	QToolBar* toolbar = new QToolBar(parent);
	QActionGroup* tb_action_group = new QActionGroup(toolbar);

	action = toolbar->addAction("Now Playing");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->addSeparator();

	action = toolbar->addAction("Anime List", [this]() {
		setCentralWidget(anime_list_page);
	});
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action->setChecked(true);
	anime_list_page = new AnimeListPage(parent);
	SetActivePage(anime_list_page);
	action = toolbar->addAction("History");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Statistics");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->addSeparator();

	action = toolbar->addAction("Search");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Seasons");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);
	action = toolbar->addAction("Torrents");
	action->setActionGroup(tb_action_group);
	action->setCheckable(true);

	toolbar->setMovable(false);
	toolbar->setFloatable(false);
	toolbar->setMinimumSize(QSize(140, 0));
	toolbar->setObjectName("sidebar");
	toolbar->setStyleSheet("QToolBar#sidebar{margin: 6px;}");
	//toolbar->setFrameShape(QFrame::NoFrame);
	toolbar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Maximum);

	addToolBar(Qt::LeftToolBarArea, toolbar);
*/
	ThemeChanged();
}

void MainWindow::SetStyleSheet(enum Themes theme) {
	switch (theme) {
		case DARK: {
			QFile f(":qdarkstyle/dark/darkstyle.qss");
			if (!f.exists())
				return; // fail
			f.open(QFile::ReadOnly | QFile::Text);
			QTextStream ts(&f);
			setStyleSheet(ts.readAll());
			break;
		}
		default:
			setStyleSheet("");
			break;
	}
}

void MainWindow::ThemeChanged() {
	switch (session.config.theme) {
		case LIGHT: {
#if MACOSX
			if (osx::DarkThemeAvailable())
				osx::SetToLightTheme();
			else
				SetStyleSheet(LIGHT);
#else
			SetStyleSheet(LIGHT);
#endif
			break;
		}
		case DARK: {
#if MACOSX
			if (osx::DarkThemeAvailable())
				osx::SetToDarkTheme();
			else
				SetStyleSheet(DARK);
#else
			SetStyleSheet(DARK);
#endif
			break;
		}
		case OS: {
#if MACOSX
			if (osx::DarkThemeAvailable())
				osx::SetToAutoTheme();
			else
				SetStyleSheet(LIGHT);
#elif defined(WIN32)
			if (win32::DarkThemeAvailable()) {
				if (win32::IsInDarkTheme()) {
					SetStyleSheet(DARK);
				} else {
					SetStyleSheet(LIGHT);
				}
			}
#else
			/* Currently OS detection only supports Windows and macOS.
			   Please don't be shy if you're willing to port it to other OSes
			   (or desktop environments, or window managers) */
			SetStyleSheet(LIGHT);
#endif
			break;
		}
	}
}

void MainWindow::SetActivePage(QWidget* page) {
	this->setCentralWidget(page);
}

void MainWindow::closeEvent(QCloseEvent* event) {
	session.config.Save();
	event->accept();
}

int main(int argc, char** argv) {
	QApplication app(argc, argv);

	session.config.Load();

	MainWindow window;

	window.resize(941, 750);
	window.setWindowTitle("Weeaboo");
	window.show();

	return app.exec();
}