view src/main.cpp @ 8:b1f73678ef61

update text paragraphs are now their own objects, as they should be
author Paper <mrpapersonic@gmail.com>
date Sat, 26 Aug 2023 03:39:34 -0400
parents 07a9095eaeed
children 5c0397762b53
line wrap: on
line source

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QPlainTextEdit>
#include <QStackedWidget>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include "window.h"
#include "config.h"
#include "anime_list.h"
#include "now_playing.h"
#include "statistics.h"
#include "sidebar.h"
#include "ui_utils.h"
#include "settings.h"
#include "session.h"
#if MACOSX
#include "sys/osx/dark_theme.h"
#elif WIN32
#include "sys/win32/dark_theme.h"
#endif

Session session;

/* 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);

	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);

	QStackedWidget* stack = new QStackedWidget(main_widget);
	stack->addWidget(new NowPlayingWidget(parent));
	AnimeListWidget* list_widget = new AnimeListWidget(parent);
	list_widget->SyncAnimeList();
	stack->addWidget(list_widget);
	stack->addWidget(new StatisticsWidget(list_widget, parent));

	connect(sidebar, &SideBar::CurrentItemChanged, stack, [stack](int index){
		switch (index) {
			case 0:
			case 1:
				stack->setCurrentIndex(index);
				break;
			case 3:
				stack->setCurrentIndex(2);
				break;
			default:
				break;
		}
	});
	sidebar->setCurrentRow(2);

	QHBoxLayout* layout = new QHBoxLayout(main_widget);
	layout->addWidget(sidebar, 0, Qt::AlignLeft | Qt::AlignTop);
	layout->addWidget(stack);
	setCentralWidget(main_widget);

	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();
}