diff --git a/Makefile b/Makefile index 9a8cd68..c0b5d47 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,8 @@ cserver_setup: @rm -Rf tmp doc: - doxygen Doxyfile + @echo -e "=> \e[1mBuilding the documentation\e[0m" + @doxygen Doxyfile clean: @echo -e "=> \e[1mCleaning $(BIN_FOLDER)/\e[0m" diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index f4f5395..323bdac 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -3,23 +3,22 @@ #include #include -#include -#include #include #include +#include #include #include #include +#include +#include #include +#include #include #include #include #include -#include -#include -MainWindow::MainWindow() : - settings("aging.conf", QSettings::NativeFormat) { +MainWindow::MainWindow() : settings("aging.conf", QSettings::NativeFormat) { isSystemTheme = settings.value("isSystemTheme", true).toBool(); if (isSystemTheme) { isDarkTheme = window()->palette().window().color().lightnessF() < 0.5; @@ -27,6 +26,7 @@ MainWindow::MainWindow() : isDarkTheme = settings.value("isDarkTheme", false).toBool(); } + resources = ResourceManager(isDarkTheme); resize(720, 720); setFocusPolicy(Qt::FocusPolicy::ClickFocus); @@ -35,7 +35,7 @@ MainWindow::MainWindow() : univ_type = UniverseType::Hashlife; for (int i = 64; i < 192; ++i) { for (int j = 64; j < 192; ++j) { - universe->set(Vec2(i, j), ( (i + j) % 12 == 0 || (i - j) % 11 == 0) ); + universe->set(Vec2(i, j), ((i + j) % 12 == 0 || (i - j) % 11 == 0)); } } @@ -44,12 +44,10 @@ MainWindow::MainWindow() : MainWindow::~MainWindow() {} - ///////////////////////////////////////////////////// //// Creation de l'interface //// ///////////////////////////////////////////////////// - void MainWindow::createUI() { createCentralWidget(); createToolBar(); @@ -57,9 +55,7 @@ void MainWindow::createUI() { } void MainWindow::createCentralWidget() { - ctxt.universe_scene = new UniverseScene(this, - universe, - univ_type); + ctxt.universe_scene = new UniverseScene(this, universe, univ_type); setCentralWidget(ctxt.universe_scene); QString color_theme = settings.value("colorTheme", "#8bb158").toString(); @@ -84,295 +80,271 @@ void MainWindow::createToolBar() { ctxt.toolbar = controlToolbar; addToolBar(Qt::LeftToolBarArea, controlToolbar); - // chargement des icones - QString icon_dir("../res/icons/light/"); - if (isDarkTheme) - icon_dir = "../res/icons/dark/"; - - // new file - connect(controlToolbar->addAction(QIcon(icon_dir + "file.svg"), - "New File"), - &QAction::triggered, this, &MainWindow::action_newFile); + connect(controlToolbar->addAction(resources.getIcon("file"), "New File"), + &QAction::triggered, this, &MainWindow::action_newFile); // open file - connect(controlToolbar->addAction(QIcon(icon_dir + "folder-open.svg"), - "Open File"), - &QAction::triggered, this, &MainWindow::action_openFile); + connect( + controlToolbar->addAction(resources.getIcon("folder-open"), "Open File"), + &QAction::triggered, this, &MainWindow::action_openFile); // save file - connect(controlToolbar->addAction(QIcon(icon_dir + "floppy.svg"), - "Save File"), - &QAction::triggered, this, &MainWindow::action_saveFile); - + connect(controlToolbar->addAction(resources.getIcon("floppy"), "Save File"), + &QAction::triggered, this, &MainWindow::action_saveFile); controlToolbar->addSeparator(); // Button Play/pause - ctxt.playIcon = new QIcon(icon_dir + "play.svg"); - ctxt.pauseIcon = new QIcon(icon_dir + "pause.svg"); - ctxt.playPauseAction = controlToolbar->addAction(*(ctxt.playIcon), - "Play/Pause"); + ctxt.playIcon = new QIcon(resources.getIconPath("play")); + ctxt.pauseIcon = new QIcon(resources.getIconPath("pause")); + ctxt.playPauseAction = + controlToolbar->addAction(*(ctxt.playIcon), "Play/Pause"); ctxt.playPauseAction->setShortcut(QKeySequence(Qt::Key_Space)); connect(ctxt.playPauseAction, &QAction::triggered, this, - &MainWindow::action_playPause); + &MainWindow::action_playPause); // Button One Step - QAction * act_step = controlToolbar->addAction(QIcon(icon_dir + "step.svg"), - "Advance one step"); - act_step->setShortcut(QKeySequence(Qt::Key_P)); - connect(act_step, - &QAction::triggered, this, &MainWindow::action_step); + QAction *act_step = controlToolbar->addAction(resources.getIcon("step"), + "Advance one step"); + act_step->setShortcut(QKeySequence(Qt::Key_P)); + connect(act_step, &QAction::triggered, this, &MainWindow::action_step); // Button Increase Speed - QAction * act_inc_speed = controlToolbar->addAction( - QIcon(icon_dir + "fast-forward.svg"), "Increase Speed"); - act_inc_speed->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus)); - connect(act_inc_speed, - &QAction::triggered, this, &MainWindow::action_incSpeed); + QAction *act_inc_speed = controlToolbar->addAction( + resources.getIcon("fast-forward"), "Increase Speed"); + act_inc_speed->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Plus)); + connect(act_inc_speed, &QAction::triggered, this, + &MainWindow::action_incSpeed); // Button Decrease Speed - QAction * act_dec_speed = controlToolbar->addAction( - QIcon(icon_dir + "rewind.svg"), "Decrease Speed"); - act_dec_speed->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus)); - connect(act_dec_speed, - &QAction::triggered, this, &MainWindow::action_decSpeed); - + QAction *act_dec_speed = controlToolbar->addAction( + resources.getIcon("rewind"), "Decrease Speed"); + act_dec_speed->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Minus)); + connect(act_dec_speed, &QAction::triggered, this, + &MainWindow::action_decSpeed); controlToolbar->addSeparator(); // Bouton Fit Pattern - QAction * act_fit = controlToolbar->addAction( - QIcon(icon_dir + "bullseye.svg"), "Fit Pattern"); - act_fit->setShortcut(QKeySequence(Qt::Key_F)); - connect(act_fit, - &QAction::triggered, this, &MainWindow::action_fitPattern); + QAction *act_fit = controlToolbar->addAction(resources.getIcon("bullseye"), + "Fit Pattern"); + act_fit->setShortcut(QKeySequence(Qt::Key_F)); + connect(act_fit, &QAction::triggered, this, &MainWindow::action_fitPattern); controlToolbar->addSeparator(); // MouseMode QActionGroup *checkableGroup = new QActionGroup(controlToolbar); - checkableGroup->setExclusive(true); - - QAction *pencilAction = controlToolbar->addAction( - QIcon(icon_dir + "pencil.svg"), "Edit"); - connect(pencilAction, - &QAction::triggered, this, &MainWindow::action_modeEdit); - pencilAction->setCheckable(true); - pencilAction->setActionGroup(checkableGroup); - - QAction *selectAction = controlToolbar->addAction( - QIcon(icon_dir + "table.svg"), "Select"); - connect(selectAction, - &QAction::triggered, this, &MainWindow::action_modeSelect); - selectAction->setCheckable(true); - selectAction->setActionGroup(checkableGroup); - - QAction *moveAction = controlToolbar->addAction( - QIcon(icon_dir + "cursor-move.svg"), "Move"); - connect(moveAction, - &QAction::triggered, this, &MainWindow::action_modeMove); - moveAction->setCheckable(true); - moveAction->setActionGroup(checkableGroup); + checkableGroup->setExclusive(true); + + QAction *pencilAction = + controlToolbar->addAction(resources.getIcon("pencil"), "Edit"); + connect(pencilAction, &QAction::triggered, this, + &MainWindow::action_modeEdit); + pencilAction->setCheckable(true); + pencilAction->setActionGroup(checkableGroup); + + QAction *selectAction = + controlToolbar->addAction(resources.getIcon("table"), "Select"); + connect(selectAction, &QAction::triggered, this, + &MainWindow::action_modeSelect); + selectAction->setCheckable(true); + selectAction->setActionGroup(checkableGroup); + + QAction *moveAction = + controlToolbar->addAction(resources.getIcon("cursor-move"), "Move"); + connect(moveAction, &QAction::triggered, this, &MainWindow::action_modeMove); + moveAction->setCheckable(true); + moveAction->setActionGroup(checkableGroup); controlToolbar->addSeparator(); // bouton d'un zoom centré - QAction * act_zoomIn = controlToolbar->addAction( - QIcon(icon_dir + "zoom-in.svg"), "Zoom In"); - act_zoomIn->setShortcut(QKeySequence(Qt::Key_Plus)); - connect(act_zoomIn, - &QAction::triggered, this, &MainWindow::action_zoomIn); + QAction *act_zoomIn = + controlToolbar->addAction(resources.getIcon("zoom-in"), "Zoom In"); + act_zoomIn->setShortcut(QKeySequence(Qt::Key_Plus)); + connect(act_zoomIn, &QAction::triggered, this, &MainWindow::action_zoomIn); // bouton d'un dezoom centré - QAction * act_zoomOut = controlToolbar->addAction( - QIcon(icon_dir + "zoom-out.svg"), "Zoomt Out"); - act_zoomOut->setShortcut(QKeySequence(Qt::Key_Minus)); - connect(act_zoomOut, - &QAction::triggered, this, &MainWindow::action_zoomOut); + QAction *act_zoomOut = + controlToolbar->addAction(resources.getIcon("zoom-out"), "Zoomt Out"); + act_zoomOut->setShortcut(QKeySequence(Qt::Key_Minus)); + connect(act_zoomOut, &QAction::triggered, this, &MainWindow::action_zoomOut); controlToolbar->addSeparator(); // bouton exit - connect(controlToolbar->addAction(QIcon(icon_dir + "logout.svg"), - "Quit"), - &QAction::triggered, this, &MainWindow::close); + connect(controlToolbar->addAction(resources.getIcon("logout"), "Quit"), + &QAction::triggered, this, &MainWindow::close); } - void MainWindow::createMenuBar() { // File QMenu *fileMenu = new QMenu("File"); - // new file - QAction* act_new_file = fileMenu->addAction("New File"); - act_new_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); - connect(act_new_file, - &QAction::triggered, this, &MainWindow::action_newFile); - - // open file - QAction* act_open_file = fileMenu->addAction("Open File"); - act_open_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); - connect(act_open_file, - &QAction::triggered, this, &MainWindow::action_openFile); + // new file + QAction *act_new_file = fileMenu->addAction("New File"); + act_new_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N)); + connect(act_new_file, &QAction::triggered, this, &MainWindow::action_newFile); - // save file - QAction * act_save_file = fileMenu->addAction("Save File"); - act_save_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); - connect(act_save_file, - &QAction::triggered, this, &MainWindow::action_saveFile); + // open file + QAction *act_open_file = fileMenu->addAction("Open File"); + act_open_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O)); + connect(act_open_file, &QAction::triggered, this, + &MainWindow::action_openFile); - fileMenu->addSeparator(); + // save file + QAction *act_save_file = fileMenu->addAction("Save File"); + act_save_file->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + connect(act_save_file, &QAction::triggered, this, + &MainWindow::action_saveFile); - // exit - QAction* act_quit = fileMenu->addAction("Quit"); - act_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); - connect(act_quit, - &QAction::triggered, this, &MainWindow::close); - menuBar()->addMenu(fileMenu); + fileMenu->addSeparator(); + + // exit + QAction *act_quit = fileMenu->addAction("Quit"); + act_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); + connect(act_quit, &QAction::triggered, this, &MainWindow::close); + menuBar()->addMenu(fileMenu); // Option QMenu *optMenu = new QMenu("Options"); - QMenu *algoMenu = new QMenu("Set Algorithm"); - - QActionGroup* groupAlgo = new QActionGroup(algoMenu); - - QAction* algo1 = algoMenu->addAction("Hashlife"); - QAction* algo2 = algoMenu->addAction("Basic"); - algo1->setCheckable(true); - algo2->setCheckable(true); - algo1->setActionGroup(groupAlgo); - algo2->setActionGroup(groupAlgo); - switch (univ_type) { - case UniverseType::Hashlife: - algo1->setChecked(true); - break; - case UniverseType::Life: - algo2->setChecked(true); - break; - } - connect(algo1, - &QAction::triggered, this, &MainWindow::action_newUnivTypeHashlife); - connect(algo2, - &QAction::triggered, this, &MainWindow::action_newUnivTypeNaive); - optMenu->addMenu(algoMenu); + QMenu *algoMenu = new QMenu("Set Algorithm"); - optMenu->addSeparator(); + QActionGroup *groupAlgo = new QActionGroup(algoMenu); - QAction* expansive = optMenu->addAction("Force expansive"); - expansive->setCheckable(true); - connect(expansive, - &QAction::toggled, this, &MainWindow::action_forceExpanse); + QAction *algo1 = algoMenu->addAction("Hashlife"); + QAction *algo2 = algoMenu->addAction("Basic"); + algo1->setCheckable(true); + algo2->setCheckable(true); + algo1->setActionGroup(groupAlgo); + algo2->setActionGroup(groupAlgo); + switch (univ_type) { + case UniverseType::Hashlife: + algo1->setChecked(true); + break; + case UniverseType::Life: + algo2->setChecked(true); + break; + } + connect(algo1, &QAction::triggered, this, + &MainWindow::action_newUnivTypeHashlife); + connect(algo2, &QAction::triggered, this, + &MainWindow::action_newUnivTypeNaive); + optMenu->addMenu(algoMenu); + + optMenu->addSeparator(); - menuBar()->addMenu(optMenu); + QAction *expansive = optMenu->addAction("Force expansive"); + expansive->setCheckable(true); + connect(expansive, &QAction::toggled, this, &MainWindow::action_forceExpanse); + + menuBar()->addMenu(optMenu); // Preferences QMenu *prefMenu = new QMenu("Preferences"); - // set color - QMenu *prefColor = new QMenu("Set Color"); - connect(prefColor->addAction("Background"), - &QAction::triggered, this, &MainWindow::action_setColorBg); - - connect(prefColor->addAction("Cell"), - &QAction::triggered, this, &MainWindow::action_setColorFg); - - connect(prefColor->addAction("Grid"), - &QAction::triggered, this, &MainWindow::action_setColorGrid); - prefMenu->addMenu(prefColor); - - // Toggle Bord - /* - QAction* toggle_bord = prefMenu->addAction("Infinite Grid"); - toggle_bord->setCheckable(true); - connect(toggle_bord, - &QAction::toggled, this, &MainWindow::action_setInfiniteGrid); - */ - connect(prefMenu->addAction("Set Rank Grid"), - &QAction::triggered, this, &MainWindow::action_setRankGrid); - - prefMenu->addSeparator(); - - QAction* sysThem = prefMenu->addAction("System Theme"); - sysThem->setCheckable(true); - sysThem->setChecked(isSystemTheme); - connect(sysThem, - &QAction::triggered, this, &MainWindow::action_systemeTheme); - - QAction* darkThem = prefMenu->addAction("Dark Theme"); - darkThem->setCheckable(true); - darkThem->setChecked(isDarkTheme); - connect(darkThem, - &QAction::triggered, this, &MainWindow::action_darkTheme); - - QAction* colThem = prefMenu->addAction("Color Theme"); - connect(colThem, - &QAction::triggered, this, &MainWindow::action_colorTheme); - - if (isSystemTheme) { - darkThem->setEnabled(false); - colThem->setEnabled(false); - } + // set color + QMenu *prefColor = new QMenu("Set Color"); + connect(prefColor->addAction("Background"), &QAction::triggered, this, + &MainWindow::action_setColorBg); + + connect(prefColor->addAction("Cell"), &QAction::triggered, this, + &MainWindow::action_setColorFg); + + connect(prefColor->addAction("Grid"), &QAction::triggered, this, + &MainWindow::action_setColorGrid); + prefMenu->addMenu(prefColor); + + // Toggle Bord + /* + QAction* toggle_bord = prefMenu->addAction("Infinite Grid"); + toggle_bord->setCheckable(true); + connect(toggle_bord, + &QAction::toggled, this, &MainWindow::action_setInfiniteGrid); + */ + connect(prefMenu->addAction("Set Rank Grid"), &QAction::triggered, this, + &MainWindow::action_setRankGrid); + + prefMenu->addSeparator(); + + QAction *sysThem = prefMenu->addAction("System Theme"); + sysThem->setCheckable(true); + sysThem->setChecked(isSystemTheme); + connect(sysThem, &QAction::triggered, this, &MainWindow::action_systemeTheme); + + QAction *darkThem = prefMenu->addAction("Dark Theme"); + darkThem->setCheckable(true); + darkThem->setChecked(isDarkTheme); + connect(darkThem, &QAction::triggered, this, &MainWindow::action_darkTheme); + + QAction *colThem = prefMenu->addAction("Color Theme"); + connect(colThem, &QAction::triggered, this, &MainWindow::action_colorTheme); - menuBar()->addMenu(prefMenu); + if (isSystemTheme) { + darkThem->setEnabled(false); + colThem->setEnabled(false); + } + + menuBar()->addMenu(prefMenu); // Help QMenu *helpMenu = new QMenu("Help"); - connect(helpMenu->addAction("Help"), - &QAction::triggered, this, &MainWindow::action_help); + connect(helpMenu->addAction("Help"), &QAction::triggered, this, + &MainWindow::action_help); - connect(helpMenu->addAction("Documentation"), - &QAction::triggered, this, &MainWindow::action_documentation); + connect(helpMenu->addAction("Documentation"), &QAction::triggered, this, + &MainWindow::action_documentation); - helpMenu->addSeparator(); + helpMenu->addSeparator(); - connect(helpMenu->addAction("Licence"), - &QAction::triggered, this, &MainWindow::action_licence); + connect(helpMenu->addAction("Licence"), &QAction::triggered, this, + &MainWindow::action_licence); - connect(helpMenu->addAction("About"), - &QAction::triggered, this, &MainWindow::action_about); + connect(helpMenu->addAction("About"), &QAction::triggered, this, + &MainWindow::action_about); - menuBar()->addMenu(helpMenu); + menuBar()->addMenu(helpMenu); } - ///////////////////////////////////////////////////// //// Actions //// ///////////////////////////////////////////////////// void MainWindow::action_newFile() { switch (univ_type) { - case UniverseType::Hashlife: - action_newUnivTypeHashlife(); - break; - case UniverseType::Life: - action_newUnivTypeNaive(); - break; + case UniverseType::Hashlife: + action_newUnivTypeHashlife(); + break; + case UniverseType::Life: + action_newUnivTypeNaive(); + break; } } void MainWindow::action_openFile() { QString fileName, acceptedFormats; switch (univ_type) { - case UniverseType::Hashlife: - acceptedFormats = "Pattern files (*.rle *.mc)"; - break; - case UniverseType::Life: - acceptedFormats = "Pattern files (*.rle)"; - break; + case UniverseType::Hashlife: + acceptedFormats = "Pattern files (*.rle *.mc)"; + break; + case UniverseType::Life: + acceptedFormats = "Pattern files (*.rle)"; + break; } - fileName = QFileDialog::getOpenFileName(this, "Open File", - "", acceptedFormats); + fileName = + QFileDialog::getOpenFileName(this, "Open File", "", acceptedFormats); if (!fileName.isNull()) { delete universe; delete ctxt.universe_scene; switch (univ_type) { - case UniverseType::Hashlife : - universe = new HashUniverse(fileName); - break; - case UniverseType::Life : - universe = new LifeUniverse(fileName); - break; + case UniverseType::Hashlife: + universe = new HashUniverse(fileName); + break; + case UniverseType::Life: + universe = new LifeUniverse(fileName); + break; } createCentralWidget(); } @@ -389,9 +361,7 @@ void MainWindow::action_playPause() { ctxt.playPauseAction->setIcon(*(ctxt.playIcon)); } } -void MainWindow::action_step() { - ctxt.universe_scene->step(); -} +void MainWindow::action_step() { ctxt.universe_scene->step(); } void MainWindow::action_incSpeed() { ctxt.universe_scene->increase_speed(); ctxt.universe_scene->get_speed(); @@ -401,51 +371,35 @@ void MainWindow::action_decSpeed() { ctxt.universe_scene->get_speed(); } -void MainWindow::action_fitPattern() { - ctxt.universe_scene->fit_pattern(); -} +void MainWindow::action_fitPattern() { ctxt.universe_scene->fit_pattern(); } +void MainWindow::action_modeEdit() { ctxt.universe_scene->set_mode(EDIT); } +void MainWindow::action_modeSelect() { ctxt.universe_scene->set_mode(SELECT); } +void MainWindow::action_modeMove() { ctxt.universe_scene->set_mode(MOVE); } -void MainWindow::action_modeEdit() { - ctxt.universe_scene->set_mode(EDIT); -} -void MainWindow::action_modeSelect() { - ctxt.universe_scene->set_mode(SELECT); -} -void MainWindow::action_modeMove() { - ctxt.universe_scene->set_mode(MOVE); -} - -void MainWindow::action_zoomIn() { - ctxt.universe_scene->zoom_in(); -} -void MainWindow::action_zoomOut() { - ctxt.universe_scene->zoom_out(); -} +void MainWindow::action_zoomIn() { ctxt.universe_scene->zoom_in(); } +void MainWindow::action_zoomOut() { ctxt.universe_scene->zoom_out(); } void MainWindow::action_forceExpanse() { printf("Toggle hyperspeed mode not Implemented\n"); } void MainWindow::action_setColorBg() { - QColor color = - QColorDialog::getColor(ctxt.universe_scene->get_cell_color(0), - this, "Choose Background Color"); + QColor color = QColorDialog::getColor(ctxt.universe_scene->get_cell_color(0), + this, "Choose Background Color"); settings.setValue("colorBg", color.name()); ctxt.universe_scene->set_cell_color(0, color); } void MainWindow::action_setColorFg() { - QColor color = - QColorDialog::getColor(ctxt.universe_scene->get_cell_color(1), - this, "Choose Cell Color"); + QColor color = QColorDialog::getColor(ctxt.universe_scene->get_cell_color(1), + this, "Choose Cell Color"); settings.setValue("colorFg", color.name()); ctxt.universe_scene->set_cell_color(1, color); } void MainWindow::action_setColorGrid() { - QColor color = - QColorDialog::getColor(ctxt.universe_scene->get_grid_color(), - this, "Choose Grid Color"); + QColor color = QColorDialog::getColor(ctxt.universe_scene->get_grid_color(), + this, "Choose Grid Color"); ctxt.universe_scene->set_grid_color(color); settings.setValue("colorGrid", color.name()); } @@ -456,39 +410,36 @@ void MainWindow::action_setInfiniteGrid() { void MainWindow::action_setRankGrid() { bool ok; std::string s; - int rank = QInputDialog::getInt(this, "Set Rang Grid", s.c_str(), - ctxt.universe_scene->get_rank_grid(), 0, 50, 1, &ok); + int rank = + QInputDialog::getInt(this, "Set Rang Grid", s.c_str(), + ctxt.universe_scene->get_rank_grid(), 0, 50, 1, &ok); if (ok) { ctxt.universe_scene->set_rank_grid(rank); } } - - void MainWindow::action_colorTheme() { QMessageBox msgBox; msgBox.setText("Changing the theme will take effect after " - "restarting the application.\nContinue ?"); + "restarting the application.\nContinue ?"); msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Yes); msgBox.setDefaultButton(QMessageBox::Cancel); int ret = msgBox.exec(); if (ret == QMessageBox::Yes) { - QColor color = - QColorDialog::getColor( - QColor(settings.value("colorTheme", "#8bb158").toString()), - this, "Choose Cell Color"); + QColor color = QColorDialog::getColor( + QColor(settings.value("colorTheme", "#8bb158").toString()), this, + "Choose Cell Color"); settings.setValue("colorTheme", color.name()); close(); } } - void MainWindow::action_systemeTheme() { QMessageBox msgBox; msgBox.setText("Changing the theme will take effect after " - "restarting the application."); + "restarting the application."); msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); msgBox.setDefaultButton(QMessageBox::Cancel); int ret = msgBox.exec(); @@ -503,7 +454,7 @@ void MainWindow::action_systemeTheme() { void MainWindow::action_darkTheme() { QMessageBox msgBox; msgBox.setText("Changing the theme will take effect after " - "restarting the application."); + "restarting the application."); msgBox.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); msgBox.setDefaultButton(QMessageBox::Cancel); int ret = msgBox.exec(); @@ -517,10 +468,14 @@ void MainWindow::action_darkTheme() { void MainWindow::action_help() { QFile file("../res/html/help.html"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return; + } QString text; QTextStream in(&file); - while (!in.atEnd()) { text += in.readLine(); } + while (!in.atEnd()) { + text += in.readLine(); + } QMessageBox msgBox; msgBox.setWindowTitle("Help"); msgBox.setTextFormat(Qt::RichText); @@ -529,10 +484,14 @@ void MainWindow::action_help() { } void MainWindow::action_documentation() { QFile file("../res/html/doc.html"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return; + } QString text; QTextStream in(&file); - while (!in.atEnd()) { text += in.readLine(); } + while (!in.atEnd()) { + text += in.readLine(); + } QMessageBox msgBox; msgBox.setWindowTitle("Documentation"); msgBox.setTextFormat(Qt::RichText); @@ -541,10 +500,14 @@ void MainWindow::action_documentation() { } void MainWindow::action_licence() { QFile file("../res/html/licence.html"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return; + } QString text; QTextStream in(&file); - while (!in.atEnd()) { text += in.readLine(); } + while (!in.atEnd()) { + text += in.readLine(); + } QMessageBox msgBox; msgBox.setWindowTitle("Licence"); msgBox.setTextFormat(Qt::RichText); @@ -553,10 +516,14 @@ void MainWindow::action_licence() { } void MainWindow::action_about() { QFile file("../res/html/about.html"); - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return; } + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return; + } QString text; QTextStream in(&file); - while (!in.atEnd()) { text += in.readLine(); } + while (!in.atEnd()) { + text += in.readLine(); + } QMessageBox msgBox; msgBox.setWindowTitle("About"); msgBox.setTextFormat(Qt::RichText); @@ -564,7 +531,6 @@ void MainWindow::action_about() { msgBox.exec(); } - void MainWindow::action_newUnivTypeHashlife() { delete universe; delete ctxt.universe_scene; @@ -584,31 +550,30 @@ void MainWindow::action_newUnivTypeNaive() { createCentralWidget(); } - ///////////////////////////////////////////////////// //// GESTION DES EVENT //// ///////////////////////////////////////////////////// void MainWindow::keyPressEvent(QKeyEvent *event) { switch (event->key()) { - case Qt::Key_Z: - case Qt::Key_Up: - ctxt.universe_scene->up(); - break; - case Qt::Key_S: - case Qt::Key_Down: - ctxt.universe_scene->down(); - break; - case Qt::Key_Q: - case Qt::Key_Left: - ctxt.universe_scene->left(); - break; - case Qt::Key_D: - case Qt::Key_Right: - ctxt.universe_scene->right(); - break; - default: - break; + case Qt::Key_Z: + case Qt::Key_Up: + ctxt.universe_scene->up(); + break; + case Qt::Key_S: + case Qt::Key_Down: + ctxt.universe_scene->down(); + break; + case Qt::Key_Q: + case Qt::Key_Left: + ctxt.universe_scene->left(); + break; + case Qt::Key_D: + case Qt::Key_Right: + ctxt.universe_scene->right(); + break; + default: + break; } update(); } @@ -625,19 +590,19 @@ void MainWindow::mousePressEvent(QMouseEvent *event) { ctxt.buffer_coord = ctxt.universe_scene->map_coords(rpos); ctxt.pressed_button = event->button(); switch (ctxt.universe_scene->get_mode()) { - case SceneMode::MOVE: - if (ctxt.pressed_button = Qt::LeftButton) { - ctxt.drag_position = rpos; - } - break; - case SceneMode::EDIT: - ctxt.universe_scene->set_cell(ctxt.buffer_coord, - ctxt.pressed_button == Qt::LeftButton); - break; - case SceneMode::SELECT: - break; - default: - break; + case SceneMode::MOVE: + if (ctxt.pressed_button = Qt::LeftButton) { + ctxt.drag_position = rpos; + } + break; + case SceneMode::EDIT: + ctxt.universe_scene->set_cell(ctxt.buffer_coord, + ctxt.pressed_button == Qt::LeftButton); + break; + case SceneMode::SELECT: + break; + default: + break; } } @@ -645,27 +610,27 @@ void MainWindow::mouseMoveEvent(QMouseEvent *event) { QPoint rpos = ctxt.universe_scene->mapFromParent(event->pos()); Vec2 coord = ctxt.universe_scene->map_coords(rpos); switch (ctxt.universe_scene->get_mode()) { - case SceneMode::MOVE: - if (ctxt.pressed_button == Qt::LeftButton) { - QPointF diff = rpos - ctxt.drag_position; - if (diff.manhattanLength() > 42) { - ctxt.drag_position = rpos; - ctxt.universe_scene-> - move_camera({-diff.x() / ctxt.universe_scene->width(), - -diff.y() / ctxt.universe_scene->height()}); - } - } - break; - case SceneMode::EDIT: - if (!(ctxt.buffer_coord == coord)) { - ctxt.buffer_coord = coord; - ctxt.universe_scene->set_cell(ctxt.buffer_coord, - ctxt.pressed_button == Qt::LeftButton); + case SceneMode::MOVE: + if (ctxt.pressed_button == Qt::LeftButton) { + QPointF diff = rpos - ctxt.drag_position; + if (diff.manhattanLength() > 42) { + ctxt.drag_position = rpos; + ctxt.universe_scene->move_camera( + {-diff.x() / ctxt.universe_scene->width(), + -diff.y() / ctxt.universe_scene->height()}); } - break; - case SceneMode::SELECT: - break; - default: - break; + } + break; + case SceneMode::EDIT: + if (!(ctxt.buffer_coord == coord)) { + ctxt.buffer_coord = coord; + ctxt.universe_scene->set_cell(ctxt.buffer_coord, + ctxt.pressed_button == Qt::LeftButton); + } + break; + case SceneMode::SELECT: + break; + default: + break; } } diff --git a/src/ui/MainWindow.h b/src/ui/MainWindow.h index d8731e6..c6c3fd1 100644 --- a/src/ui/MainWindow.h +++ b/src/ui/MainWindow.h @@ -7,6 +7,7 @@ #include #include #include +#include typedef struct SwidgetsCtxt { UniverseScene *universe_scene; @@ -90,6 +91,8 @@ class MainWindow : public QMainWindow { void createToolBar(); void createMenuBar(); void createCentralWidget(); + + ResourceManager resources; }; #endif /* UI_MAINWINDOW_H_ */ diff --git a/src/ui/ResourceManager.cpp b/src/ui/ResourceManager.cpp new file mode 100644 index 0000000..1a79a1f --- /dev/null +++ b/src/ui/ResourceManager.cpp @@ -0,0 +1,37 @@ +#include + +ResourceManager::ResourceManager() : isDarkTheme(false) {} + +ResourceManager::ResourceManager(bool isDarkTheme) { + this->isDarkTheme = isDarkTheme; + + QString relative_path("../res"); + + QString data_path = + QStandardPaths::writableLocation(QStandardPaths::DataLocation); + QDir absolute_dir(data_path); + + if (!absolute_dir.exists()) { + QDir relative_dir(relative_path); + if (relative_dir.exists()) + data_path = relative_path; + else + qDebug("Icon folder not available."); + } + + icon_dir = QDir(data_path); + icon_dir.cd("icons"); + + if (isDarkTheme) + icon_dir.cd("dark"); + else + icon_dir.cd("light"); +} + +QIcon ResourceManager::getIcon(QString name) { + return QIcon(icon_dir.absoluteFilePath(name + ".svg")); +} + +QString ResourceManager::getIconPath(QString name) { + return icon_dir.absoluteFilePath(name + ".svg"); +} diff --git a/src/ui/ResourceManager.h b/src/ui/ResourceManager.h new file mode 100644 index 0000000..1850eec --- /dev/null +++ b/src/ui/ResourceManager.h @@ -0,0 +1,73 @@ +#ifndef UI_RESOURCEMANAGER_H_ +#define UI_RESOURCEMANAGER_H_ + +/*! + * \file ResourceManager.h + * \brief A class that manages resources such as icons + * \author aging-team + */ + +#include +#include +#include + +class ResourceManager { + /*! \class ResourceManager + * \brief A class that manages resources such as icons + * + * The goal of this class is to make resource management easier for the + * rest of the program: it searches for the resource directory and pick the + * one that fits best (`/usr/share` if it's an installed program, `../res` + * if it's a debug build). + */ +public: + /*! + * \brief Constructor + * + * Base constructeur of ResourceManager + */ + ResourceManager(); + /*! + * \brief Constructor + * + * Constructeur of ResourceManager + * + * \param isDarkTheme : boolean that describe the set of resources we use + */ + ResourceManager(bool isDarkTheme); + /*! + * \brief QIcon getter + * + * Returns the QIcon of the corresponding icon + * + * Example usage: + * @code + * ResourceManager resources(true); + * QIcon playIcon = resources.getIcon("play"); + * @endcode + * + * \param name : name of the icon + */ + QIcon getIcon(QString name); + /*! + * \brief Icon's path getter + * + * Returns the path of the corresponding icon + * + * Example usage: + * @code + * ResourceManager resources(true); + * QString playIconPath = resources.getIconPath("play"); + * // playIconPath can be equal to ../res/icons/dark/play.svg + * @endcode + * + * \param name : name of the icon + */ + QString getIconPath(QString name); + +private: + bool isDarkTheme; + QDir icon_dir; +}; + +#endif // UI_RESOURCEMANAGER_H_