Preliminary settings work

This commit is contained in:
Simone
2024-01-23 11:44:46 +01:00
parent f5a4cb1fd3
commit e3cc520fa5
10 changed files with 159 additions and 7 deletions

View File

@@ -59,7 +59,12 @@ add_executable(kaizen-qt
EmuThread.cpp EmuThread.cpp
mainwindow.ui mainwindow.ui
MainWindow.hpp MainWindow.hpp
MainWindow.cpp) MainWindow.cpp
SettingsWindow.hpp
SettingsWindow.cpp
CPUSettings.hpp
CPUSettings.cpp
JSONUtils.hpp)
target_link_libraries(kaizen-qt PUBLIC Qt6::Core Qt6::Gui Qt6::Widgets fmt mio nlohmann_json nfd parallel-rdp backend) target_link_libraries(kaizen-qt PUBLIC Qt6::Core Qt6::Gui Qt6::Widgets fmt mio nlohmann_json nfd parallel-rdp backend)

View File

@@ -0,0 +1,35 @@
#include <CPUSettings.hpp>
#include <QVBoxLayout>
#include <QLabel>
#include <JSONUtils.hpp>
#include <log.hpp>
CPUSettings::CPUSettings(nlohmann::json& settings) : settings(settings), QWidget(nullptr) {
cpuTypes->addItems({ "Interpreter", "Dynamic Recompiler" });
if (JSONGetField<std::string>(settings, "cpu", "type") == "jit") {
cpuTypes->setCurrentIndex(1);
} else {
cpuTypes->setCurrentIndex(0);
}
connect(cpuTypes, &QComboBox::currentIndexChanged, this, [&]() {
if (cpuTypes->currentIndex() == 0) {
JSONSetField(settings, "cpu", "type", "interpreter");
} else if (cpuTypes->currentIndex() == 1) {
JSONSetField(settings, "cpu", "type", "jit");
} else {
Util::panic("Impossible CPU type!");
}
emit modified();
});
QLabel* label = new QLabel("CPU type:");
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(label);
mainLayout->addWidget(cpuTypes);
mainLayout->addStretch();
setLayout(mainLayout);
}

View File

@@ -0,0 +1,14 @@
#pragma once
#include <QWidget>
#include <QComboBox>
#include <JSONUtils.hpp>
class CPUSettings : public QWidget {
QComboBox* cpuTypes = new QComboBox;
Q_OBJECT
public:
CPUSettings(nlohmann::json&);
nlohmann::json& settings;
Q_SIGNALS:
void modified();
};

View File

@@ -0,0 +1,37 @@
#pragma once
#include <nlohmann/json.hpp>
#include <filesystem>
#include <fstream>
namespace fs = std::filesystem;
static inline nlohmann::json JSONOpenOrCreate(const std::string& path) {
auto fileExists = fs::exists(path);
if (fileExists) {
auto file = std::fstream(path, std::fstream::in | std::fstream::out);
auto json = nlohmann::json::parse(file);
file.close();
return json;
} else {
auto file = std::fstream(path, std::fstream::in | std::fstream::out | std::fstream::trunc);
nlohmann::json json;
json["audio"]["volume"] = 0.5;
json["cpu"]["type"] = std::string("interpreter");
file << json;
file.close();
return json;
}
}
template <typename T>
static inline void JSONSetField(nlohmann::json& json, const std::string& field1, const std::string& field2, const T& value) {
json[field1][field2] = value;
}
template <typename T>
static inline const T& JSONGetField(nlohmann::json& json, const std::string& field1, const std::string& field2) {
return json[field1][field2];
}

View File

@@ -18,12 +18,16 @@ KaizenQt::KaizenQt() noexcept : QWidget(nullptr) {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
setFocus(); setFocus();
grabKeyboard(); grabKeyboard();
mainWindow->show(); mainWindow->show();
settingsWindow = new SettingsWindow;
settingsWindow->hide();
emuThread->core = new n64::Core(); emuThread->core = new n64::Core();
} }
void KaizenQt::ConnectMainWindowSignalsToSlots() noexcept { void KaizenQt::ConnectMainWindowSignalsToSlots() noexcept {
connect(mainWindow, &MainWindowController::OpenSettings, this, [this]() {
settingsWindow->show();
});
connect(mainWindow, &MainWindowController::OpenROM, this, &KaizenQt::LoadROM); connect(mainWindow, &MainWindowController::OpenROM, this, &KaizenQt::LoadROM);
connect(mainWindow, &MainWindowController::Exit, this, []() { connect(mainWindow, &MainWindowController::Exit, this, []() {
QApplication::quit(); QApplication::quit();

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <EmuThread.hpp> #include <EmuThread.hpp>
#include <MainWindow.hpp> #include <MainWindow.hpp>
#include <SettingsWindow.hpp>
enum class CompositorCategory { enum class CompositorCategory {
Windows, MacOS, XCB, Wayland Windows, MacOS, XCB, Wayland
@@ -34,5 +35,6 @@ public:
private: private:
void ConnectMainWindowSignalsToSlots() noexcept; void ConnectMainWindowSignalsToSlots() noexcept;
MainWindowController* mainWindow; MainWindowController* mainWindow;
SettingsWindow* settingsWindow;
EmuThread* emuThread; EmuThread* emuThread;
}; };

View File

@@ -58,10 +58,6 @@ void MainWindowController::ConnectSignalsToSlots() noexcept {
}); });
connect(view.actionSettings, &QAction::triggered, this, [this]() { connect(view.actionSettings, &QAction::triggered, this, [this]() {
auto layout = new QVBoxLayout(this); emit OpenSettings();
layout->addWidget(new QSlider(Qt::Horizontal));
auto settings = new QWidget;
settings->setLayout(layout);
settings->show();
}); });
} }

View File

@@ -18,6 +18,7 @@ private:
bool textPauseToggle = false; bool textPauseToggle = false;
Q_SIGNALS: Q_SIGNALS:
void OpenSettings();
void OpenROM(const QString& rom_file); void OpenROM(const QString& rom_file);
void Exit(); void Exit();
void Reset(); void Reset();

View File

@@ -0,0 +1,43 @@
#include <SettingsWindow.hpp>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QButtonGroup>
SettingsWindow::SettingsWindow() : QWidget(nullptr) {
settings = JSONOpenOrCreate("resources/settings.json");
if (objectName().isEmpty())
setObjectName("Settings");
resize(500, 400);
setWindowTitle("Settings");
cpuSettings = new CPUSettings(settings);
auto* tabs = new QTabWidget;
tabs->addTab(cpuSettings, tr("CPU"));
//tabs->addTab(new PermissionsTab, tr("Audio"));
apply->setEnabled(false);
connect(cpuSettings, &CPUSettings::modified, this, [&]() {
apply->setEnabled(true);
});
connect(apply, &QPushButton::pressed, this, [&]() {
apply->setEnabled(false);
std::ofstream file("resources/settings.json");
file << settings;
file.close();
});
connect(cancel, &QPushButton::pressed, this, &QWidget::hide);
QVBoxLayout* mainLayout = new QVBoxLayout;
QHBoxLayout* buttonsLayout = new QHBoxLayout;
buttonsLayout->addWidget(apply);
buttonsLayout->addWidget(cancel);
mainLayout->addWidget(tabs);
mainLayout->addLayout(buttonsLayout);
setLayout(mainLayout);
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <QWidget>
#include <QTabWidget>
#include <QPushButton>
#include <CPUSettings.hpp>
class SettingsWindow : public QWidget {
QPushButton* cancel = new QPushButton("Cancel");
QPushButton* apply = new QPushButton("Apply");
Q_OBJECT
public:
SettingsWindow();
nlohmann::json settings;
CPUSettings* cpuSettings;
};