Qt: Move configuration loading to its own class
Jeffrey Pfau jeffrey@endrift.com
Wed, 05 Nov 2014 00:22:11 -0800
7 files changed,
127 insertions(+),
35 deletions(-)
M
src/platform/qt/CMakeLists.txt
→
src/platform/qt/CMakeLists.txt
@@ -31,6 +31,7 @@
set(SOURCE_FILES AudioDevice.cpp AudioProcessor.cpp + ConfigController.cpp Display.cpp GBAApp.cpp GameController.cpp
A
src/platform/qt/ConfigController.cpp
@@ -0,0 +1,53 @@
+#include "ConfigController.h" + +#include "GameController.h" + +extern "C" { +#include "platform/commandline.h" +} + +using namespace QGBA; + +ConfigController::ConfigController(QObject* parent) + : QObject(parent) + , m_opts() +{ + GBAConfigInit(&m_config, PORT); + + m_opts.audioSync = GameController::AUDIO_SYNC; + m_opts.videoSync = GameController::VIDEO_SYNC; + GBAConfigLoadDefaults(&m_config, &m_opts); + GBAConfigLoad(&m_config); + GBAConfigMap(&m_config, &m_opts); +} + +ConfigController::~ConfigController() { + write(); + + GBAConfigDeinit(&m_config); + GBAConfigFreeOpts(&m_opts); +} + +bool ConfigController::parseArguments(GBAArguments* args, int argc, char* argv[]) { + return ::parseArguments(args, &m_config, argc, argv, 0); +} + +void ConfigController::setOption(const char* key, bool value) { + setOption(key, (int) value); +} + +void ConfigController::setOption(const char* key, int value) { + GBAConfigSetIntValue(&m_config, key, value); +} + +void ConfigController::setOption(const char* key, unsigned value) { + GBAConfigSetUIntValue(&m_config, key, value); +} + +void ConfigController::setOption(const char* key, const char* value) { + GBAConfigSetValue(&m_config, key, value); +} + +void ConfigController::write() { + GBAConfigSave(&m_config); +}
A
src/platform/qt/ConfigController.h
@@ -0,0 +1,41 @@
+#ifndef QGBA_CONFIG_CONTROLLER +#define QGBA_CONFIG_CONTROLLER + +#include <QObject> + +extern "C" { +#include "gba-config.h" +#include "util/configuration.h" +} + +struct GBAArguments; + +namespace QGBA { +class ConfigController : public QObject { +Q_OBJECT + +public: + constexpr static const char* const PORT = "qt"; + + ConfigController(QObject* parent = nullptr); + ~ConfigController(); + + const GBAOptions* options() const { return &m_opts; } + bool parseArguments(GBAArguments* args, int argc, char* argv[]); + +public slots: + void setOption(const char* key, bool value); + void setOption(const char* key, int value); + void setOption(const char* key, unsigned value); + void setOption(const char* key, const char* value); + + void write(); + +private: + GBAConfig m_config; + GBAOptions m_opts; +}; + +} + +#endif
M
src/platform/qt/GBAApp.cpp
→
src/platform/qt/GBAApp.cpp
@@ -4,40 +4,28 @@ #include "GameController.h"
#include <QFileOpenEvent> -#define PORT "qt" +extern "C" { +#include "platform/commandline.h" +} using namespace QGBA; GBAApp::GBAApp(int& argc, char* argv[]) : QApplication(argc, argv) - , m_args() - , m_opts() { QApplication::setApplicationName(PROJECT_NAME); QApplication::setApplicationVersion(PROJECT_VERSION); - GBAConfigInit(&m_config, PORT); - GBAConfigLoad(&m_config); - - m_opts.audioSync = GameController::AUDIO_SYNC; - m_opts.videoSync = GameController::VIDEO_SYNC; - GBAConfigLoadDefaults(&m_config, &m_opts); - - bool parsed = parseArguments(&m_args, &m_config, argc, argv, 0); - GBAConfigMap(&m_config, &m_opts); - m_window.setOptions(&m_opts); - - if (parsed) { - m_window.argumentsPassed(&m_args); - } + GBAArguments args = {}; + m_window.setConfig(&m_configController); + if (m_configController.parseArguments(&args, argc, argv)) { + m_window.argumentsPassed(&args); + } else { + m_window.loadConfig(); + } + freeArguments(&args); m_window.show(); -} - -GBAApp::~GBAApp() { - freeArguments(&m_args); - GBAConfigFreeOpts(&m_opts); - GBAConfigDeinit(&m_config); } bool GBAApp::event(QEvent* event) {
M
src/platform/qt/GBAApp.h
→
src/platform/qt/GBAApp.h
@@ -3,12 +3,8 @@ #define QGBA_APP_H
#include <QApplication> +#include "ConfigController.h" #include "Window.h" - -extern "C" { -#include "platform/commandline.h" -#include "util/configuration.h" -} namespace QGBA {@@ -19,17 +15,13 @@ Q_OBJECT
public: GBAApp(int& argc, char* argv[]); - virtual ~GBAApp(); protected: bool event(QEvent*); private: Window m_window; - - GBAArguments m_args; - GBAOptions m_opts; - GBAConfig m_config; + ConfigController m_configController; }; }
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -6,6 +6,7 @@ #include <QKeySequence>
#include <QMenuBar> #include <QStackedLayout> +#include "ConfigController.h" #include "GameController.h" #include "GDBController.h" #include "GDBWindow.h"@@ -102,6 +103,8 @@ }
} void Window::argumentsPassed(GBAArguments* args) { + loadConfig(); + if (args->patch) { m_controller->loadPatch(args->patch); }@@ -111,7 +114,13 @@ m_controller->loadGame(args->fname, args->dirmode);
} } -void Window::setOptions(GBAOptions* opts) { +void Window::setConfig(ConfigController* config) { + m_config = config; +} + +void Window::loadConfig() { + const GBAOptions* opts = m_config->options(); + m_logView->setLevels(opts->logLevel); // TODO: Have these show up as modified in the menu m_controller->setFrameskip(opts->frameskip);@@ -133,6 +142,10 @@
if (opts->width && opts->height) { m_screenWidget->setSizeHint(QSize(opts->width, opts->height)); } +} + +void Window::saveConfig() { + m_config->write(); } void Window::selectROM() {
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -17,6 +17,7 @@ struct GBAArguments;
namespace QGBA { +class ConfigController; class GameController; class LogView; class VideoView;@@ -33,7 +34,7 @@ GameController* controller() { return m_controller; }
static GBAKey mapKey(int qtKey); - void setOptions(GBAOptions*); + void setConfig(ConfigController*); void argumentsPassed(GBAArguments*); signals:@@ -47,6 +48,8 @@ void selectROM();
void selectBIOS(); void selectPatch(); void toggleFullScreen(); + void loadConfig(); + void saveConfig(); #ifdef USE_FFMPEG void openVideoWindow();@@ -81,6 +84,7 @@ LogView* m_logView;
LoadSaveState* m_stateWindow; WindowBackground* m_screenWidget; QPixmap m_logo; + ConfigController* m_config; #ifdef USE_FFMPEG VideoView* m_videoView;