From 54e6bf610bf69d6444d6025e73ee3951ec683be4 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Fri, 23 Sep 2022 12:48:13 +0200 Subject: [PATCH] GlosSIConfig: Refactor reading/writing target configs --- GlosSIConfig/UIModel.cpp | 79 ++++------------ GlosSIConfig/UIModel.h | 32 +++---- GlosSIConfig/qml/ShortcutProps.qml | 142 ++++++++++++++--------------- 3 files changed, 103 insertions(+), 150 deletions(-) diff --git a/GlosSIConfig/UIModel.cpp b/GlosSIConfig/UIModel.cpp index b0bce26..f75ac62 100644 --- a/GlosSIConfig/UIModel.cpp +++ b/GlosSIConfig/UIModel.cpp @@ -39,16 +39,16 @@ UIModel::UIModel() : QObject(nullptr) std::filesystem::create_directories(path); config_path_ = path; - config_dir_name_ = QString::fromStdWString((path /= "Targets").wstring().data()); + config_dir_name_ = QString::fromStdWString((path /= "Targets").wstring()); if (!std::filesystem::exists(path)) std::filesystem::create_directories(path); parseShortcutVDF(); - readConfigs(); + readTargetConfigs(); } -void UIModel::readConfigs() +void UIModel::readTargetConfigs() { QDir dir(config_dir_name_); auto entries = dir.entryList(QDir::Files, QDir::SortFlag::Name); @@ -68,29 +68,13 @@ void UIModel::readConfigs() const auto data = file.readAll(); file.close(); const auto jsondoc = QJsonDocument::fromJson(data); - const auto filejson = jsondoc.object(); - - QJsonObject json; - json["version"] = filejson["version"]; - json["icon"] = filejson["icon"]; - json["launch"] = filejson["launch"]["launch"]; - json["launchPath"] = filejson["launch"]["launchPath"]; - json["launchAppArgs"] = filejson["launch"]["launchAppArgs"]; - json["closeOnExit"] = filejson["launch"]["closeOnExit"]; - json["waitForChildProcs"] = filejson["launch"]["waitForChildProcs"]; - json["hideDevices"] = filejson["devices"]["hideDevices"]; - json["realDeviceIds"] = filejson["devices"]["realDeviceIds"]; - json["windowMode"] = filejson["window"]["windowMode"]; - json["maxFps"] = filejson["window"]["maxFps"]; - json["scale"] = filejson["window"]["scale"]; - json["disableOverlay"] = filejson["window"]["disableOverlay"]; - json["maxControllers"] = filejson["controller"]["maxControllers"]; - json["allowDesktopConfig"] = filejson["controller"]["allowDesktopConfig"]; - json["emulateDS4"] = filejson["controller"]["emulateDS4"]; - - json["name"] = filejson.contains("name") ? filejson["name"] : QString(name).replace(QRegularExpression("\\.json"), ""); - - targets_.append(json.toVariantMap()); + auto filejson = jsondoc.object(); + + filejson["name"] = filejson.contains("name") + ? filejson["name"].toString() + : QString(name).replace(QRegularExpression("\\.json"), ""); + + targets_.append(filejson.toVariantMap()); }); emit targetListChanged(); @@ -359,49 +343,22 @@ void UIModel::setAcrylicEffect(bool has_acrylic_affect) emit acrylicChanged(); } -void UIModel::writeTarget(const QJsonObject& json, const QString& name) +void UIModel::writeTarget(const QJsonObject& json, const QString& name) const { auto path = config_path_; path /= config_dir_name_.toStdWString(); path /= (QString(name).replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json").toStdWString(); QFile file(path); if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) { - // meh + qDebug() << "Couldn't open file for writing: " << path; return; } - QJsonObject fileJson; - fileJson["version"] = json["version"]; - fileJson["icon"] = json["icon"]; - fileJson["name"] = json["name"]; - - QJsonObject launchObject; - launchObject["launch"] = json["launch"]; - launchObject["launchPath"] = json["launchPath"]; - launchObject["launchAppArgs"] = json["launchAppArgs"]; - launchObject["closeOnExit"] = json["closeOnExit"]; - launchObject["waitForChildProcs"] = json["waitForChildProcs"]; - fileJson["launch"] = launchObject; - - QJsonObject devicesObject; - devicesObject["hideDevices"] = json["hideDevices"]; - devicesObject["realDeviceIds"] = json["realDeviceIds"]; - fileJson["devices"] = devicesObject; - - QJsonObject windowObject; - windowObject["windowMode"] = json["windowMode"]; - windowObject["maxFps"] = json["maxFps"]; - windowObject["scale"] = json["scale"]; - windowObject["disableOverlay"] = json["disableOverlay"]; - fileJson["window"] = windowObject; - - QJsonObject controllerObject; - controllerObject["maxControllers"] = json["maxControllers"]; - controllerObject["allowDesktopConfig"] = json["allowDesktopConfig"]; - controllerObject["emulateDS4"] = json["emulateDS4"]; - fileJson["controller"] = controllerObject; - - auto wtf = QString(QJsonDocument(fileJson).toJson(QJsonDocument::Indented)).toStdString(); - file.write(wtf.data()); + + file.write( + QString(QJsonDocument(json).toJson(QJsonDocument::Indented)) + .toStdString() + .data() + ); file.close(); } diff --git a/GlosSIConfig/UIModel.h b/GlosSIConfig/UIModel.h index 38b6007..4ec241a 100644 --- a/GlosSIConfig/UIModel.h +++ b/GlosSIConfig/UIModel.h @@ -33,7 +33,7 @@ class UIModel : public QObject { public: UIModel(); - Q_INVOKABLE void readConfigs(); + Q_INVOKABLE void readTargetConfigs(); Q_INVOKABLE QVariantList getTargetList() const; Q_INVOKABLE void addTarget(QVariant shortcut); Q_INVOKABLE void updateTarget(int index, QVariant shortcut); @@ -64,17 +64,15 @@ class UIModel : public QObject { void targetListChanged(); private: +#ifdef _WIN32 + bool is_windows_ = true; +#else + bool is_windows_ = false; +#endif + bool has_acrylic_affect_ = false; + std::filesystem::path config_path_; QString config_dir_name_; - - void writeTarget(const QJsonObject& json, const QString& name); - - std::filesystem::path getSteamPath() const; - std::wstring getSteamUserId() const; - bool foundSteam() const; - void parseShortcutVDF(); - - bool isSteamInputXboxSupportEnabled() const; QString shortcutsfile_ = "/config/shortcuts.vdf"; QString user_config_file_ = "/config/localconfig.vdf"; @@ -83,11 +81,13 @@ class UIModel : public QObject { QVariantList targets_; std::vector shortcuts_vdf_; + + void writeTarget(const QJsonObject& json, const QString& name) const; -#ifdef _WIN32 - bool is_windows_ = true; -#else - bool is_windows_ = false; -#endif - bool has_acrylic_affect_ = false; + std::filesystem::path getSteamPath() const; + std::wstring getSteamUserId() const; + bool foundSteam() const; + void parseShortcutVDF(); + + bool isSteamInputXboxSupportEnabled() const; }; diff --git a/GlosSIConfig/qml/ShortcutProps.qml b/GlosSIConfig/qml/ShortcutProps.qml index 889bc99..e6f0029 100644 --- a/GlosSIConfig/qml/ShortcutProps.qml +++ b/GlosSIConfig/qml/ShortcutProps.qml @@ -29,62 +29,58 @@ Item { signal cancel() signal done(var shortcut) - property var shortcutInfo: ({ - version: 1, - name: null, - launch: false, - launchPath: null, - launchAppArgs: null, - closeOnExit: true, - waitForChildProcs: true, - hideDevices: true, - windowMode: false, - maxFps: null, - scale: null, - icon: null, - maxControllers: 1, - disableOverlay: false, - realDeviceIds: false, - allowDesktopConfig: false, - emulateDS4: false, - }) + property var shortcutInfo: ({}) function resetInfo() { shortcutInfo = ({ - version: 1, - name: null, - launch: false, - launchPath: null, - launchAppArgs: null, - closeOnExit: true, - waitForChildProcs: true, - hideDevices: true, - windowMode: false, - maxFps: null, - scale: null, - icon: null, - maxControllers: 1, - disableOverlay: false, - realDeviceIds: false, - allowDesktopConfig: false, - emulateDS4: false, + "controller": { + "maxControllers": 1, + "emulateDS4": false, + "allowDesktopConfig": false + }, + "devices": { + "hideDevices": true, + "realDeviceIds": false + }, + "icon": null, + "launch": { + "closeOnExit": true, + "launch": false, + "launchAppArgs": null, + "launchPath": null, + "waitForChildProcs": true + }, + "name": null, + "version": 1, + "window": { + "disableOverlay": false, + "maxFps": null, + "scale": null, + "windowMode": false + }, + "extendedLogging": false }) } + + Component.onCompleted: function() { + resetInfo() + } onShortcutInfoChanged: function() { nameInput.text = shortcutInfo.name || "" - launchApp.checked = shortcutInfo.launch || false - pathInput.text = shortcutInfo.launchPath || "" - argsInput.text = shortcutInfo.launchAppArgs || "" - closeOnExit.checked = shortcutInfo.closeOnExit || false - waitForChildren.checked = shortcutInfo.waitForChildProcs - hideDevices.checked = shortcutInfo.hideDevices || false - windowMode.checked = shortcutInfo.windowMode || false - maxControllersSpinBox.value = shortcutInfo.maxControllers - disableOverlayCheckbox.checked = shortcutInfo.disableOverlay || false - realDeviceIds.checked = shortcutInfo.realDeviceIds || false - allowDesktopConfig.checked = shortcutInfo.allowDesktopConfig || false - emulateDS4.checked = shortcutInfo.emulateDS4 || false + extendedLogging.checked = shortcutInfo.extendedLogging + launchApp.checked = shortcutInfo.launch.launch + pathInput.text = shortcutInfo.launch.launchPath || "" + argsInput.text = shortcutInfo.launch.launchAppArgs || "" + closeOnExit.checked = shortcutInfo.launch.closeOnExit + waitForChildren.checked = shortcutInfo.launch.waitForChildProcs + hideDevices.checked = shortcutInfo.devices.hideDevices + realDeviceIds.checked = shortcutInfo.devices.realDeviceIds + windowMode.checked = shortcutInfo.window.windowMode + disableOverlayCheckbox.checked = shortcutInfo.window.disableOverlay + maxControllersSpinBox.value = shortcutInfo.controller.maxControllers + allowDesktopConfig.checked = shortcutInfo.controller.allowDesktopConfig + emulateDS4.checked = shortcutInfo.controller.emulateDS4 } Flickable { @@ -159,9 +155,9 @@ Item { CheckBox { id: launchApp text: qsTr("Launch app") - checked: shortcutInfo.launch + checked: shortcutInfo.launch.launch onCheckedChanged: function() { - shortcutInfo.launch = checked + shortcutInfo.launch.launch = checked if (checked) { closeOnExit.enabled = true; if (closeOnExit.checked) { @@ -181,9 +177,9 @@ Item { CheckBox { id: closeOnExit text: qsTr("Close when launched app quits") - checked: shortcutInfo.closeOnExit + checked: shortcutInfo.launch.closeOnExit onCheckedChanged: function() { - shortcutInfo.closeOnExit = checked + shortcutInfo.launch.closeOnExit = checked if (checked) { waitForChildren.enabled = true; } else { @@ -201,9 +197,9 @@ Item { CheckBox { id: waitForChildren text: qsTr("Wait for child processes") - checked: shortcutInfo.waitForChildProcs + checked: shortcutInfo.launch.waitForChildProcs onCheckedChanged: function(){ - shortcutInfo.waitForChildProcs = checked + shortcutInfo.launch.waitForChildProcs = checked } } } @@ -212,9 +208,9 @@ Item { CheckBox { id: allowDesktopConfig text: qsTr("Allow desktop-config") - checked: shortcutInfo.allowDesktopConfig + checked: shortcutInfo.controller.allowDesktopConfig onCheckedChanged: function(){ - shortcutInfo.allowDesktopConfig = checked + shortcutInfo.controller.allowDesktopConfig = checked } } Label { @@ -238,7 +234,7 @@ Item { ? shortcutInfo.icon.endsWith(".exe") ? "image://exe/" + shortcutInfo.icon : "file:///" + shortcutInfo.icon - : null + : '' Layout.preferredWidth: 48 Layout.preferredHeight: 48 visible: shortcutInfo.icon @@ -267,8 +263,8 @@ Item { id: pathInput placeholderText: qsTr("...") enabled: launchApp.checked - text: shortcutInfo.launchPath || "" - onTextChanged: shortcutInfo.launchPath = text + text: shortcutInfo.launch.launchPath || "" + onTextChanged: shortcutInfo.launch.launchPath = text } } Button { @@ -304,8 +300,8 @@ Item { anchors.topMargin: 4 id: argsInput enabled: launchApp.checked - text: shortcutInfo.launchAppArgs - onTextChanged: shortcutInfo.launchAppArgs = text + text: shortcutInfo.launch.launchAppArgs + onTextChanged: shortcutInfo.launch.launchAppArgs = text } } } @@ -333,8 +329,8 @@ Item { CheckBox { id: hideDevices text: qsTr("Hide (Real) Controllers") - checked: shortcutInfo.hideDevices - onCheckedChanged: shortcutInfo.hideDevices = checked + checked: shortcutInfo.devices.hideDevices + onCheckedChanged: shortcutInfo.devices.hideDevices = checked } RoundButton { onClicked: () => { @@ -366,8 +362,8 @@ Item { CheckBox { id: realDeviceIds text: qsTr("Use real device (USB)-IDs") - checked: shortcutInfo.realDeviceIds - onCheckedChanged: shortcutInfo.realDeviceIds = checked + checked: shortcutInfo.devices.realDeviceIds + onCheckedChanged: shortcutInfo.devices.realDeviceIds = checked } RoundButton { onClicked: () => { @@ -399,8 +395,8 @@ Item { CheckBox { id: emulateDS4 text: qsTr("Emulate DS4") - checked: shortcutInfo.emulateDS4 - onCheckedChanged: shortcutInfo.emulateDS4 = checked + checked: shortcutInfo.controller.emulateDS4 || false + onCheckedChanged: shortcutInfo.controller.emulateDS4 = checked } RoundButton { onClicked: () => { @@ -438,10 +434,10 @@ Item { SpinBox { id: maxControllersSpinBox width: 128 - value: 4 + value: shortcutInfo.controller.maxControllers from: 0 to: 4 - onValueChanged: shortcutInfo.maxControllers = value + onValueChanged: shortcutInfo.controller.maxControllers = value } RoundButton { onClicked: () => { @@ -479,8 +475,8 @@ Item { CheckBox { id: windowMode text: qsTr("Steam/GlosSI overlay as separate window") - checked: shortcutInfo.windowMode - onCheckedChanged: shortcutInfo.windowMode = checked + checked: shortcutInfo.window.windowMode + onCheckedChanged: shortcutInfo.window.windowMode = checked } RoundButton { onClicked: () => { @@ -513,8 +509,8 @@ Item { CheckBox { id: disableOverlayCheckbox text: qsTr("Disable Steam/GlosSI overlay") - checked: shortcutInfo.disableOverlay - onCheckedChanged: shortcutInfo.disableOverlay = checked + checked: shortcutInfo.window.disableOverlay + onCheckedChanged: shortcutInfo.window.disableOverlay = checked } RoundButton { onClicked: () => {