From b33e1329dbbfc99a25f1cfeaca8c44514fdde6e8 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Mon, 14 Oct 2024 23:22:57 +0200 Subject: [PATCH] Removing typed config functions in Helper. --- common/src/module/history.mjs | 2 +- common/src/module/interface.mjs | 28 +------- common/src/module/latex.mjs | 2 +- common/src/module/settings.mjs | 18 +---- common/src/preferences/common.mjs | 14 ++-- common/src/preferences/default.mjs | 2 +- common/test/mock/helper.mjs | 69 ++++--------------- .../Setting/ExpressionEditor.qml | 8 +-- .../eu/ad5001/LogarithmPlotter/Settings.qml | 18 ++--- .../LogarithmPlotter/util/helper.py | 31 ++------- runtime-pyside6/tests/test_helper.py | 10 +-- 11 files changed, 53 insertions(+), 149 deletions(-) diff --git a/common/src/module/history.mjs b/common/src/module/history.mjs index be70f87..4b07095 100644 --- a/common/src/module/history.mjs +++ b/common/src/module/history.mjs @@ -138,7 +138,7 @@ class HistoryAPI extends Module { if(action instanceof Action) { console.log("Added new entry to history: " + action.getReadableString()) this.undoStack.push(action) - if(this.#helper.getSettingBool("reset_redo_stack")) + if(this.#helper.getSetting("reset_redo_stack")) this.redoStack = [] this.emit(new AddedEvent(action)) } diff --git a/common/src/module/interface.mjs b/common/src/module/interface.mjs index 27fb7eb..b6d462f 100644 --- a/common/src/module/interface.mjs +++ b/common/src/module/interface.mjs @@ -109,37 +109,13 @@ export class HelperInterface extends Interface { /** * Gets a setting from the config * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {boolean} Value of the setting - */ - getSettingBool = FUNCTION - /** - * Gets a setting from the config - * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {number} Value of the setting - */ - getSettingInt = FUNCTION - /** - * Gets a setting from the config - * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {string} Value of the setting + * @returns {string|number|boolean} Value of the setting */ getSetting = FUNCTION /** * Sets a setting in the config * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {boolean} value - */ - setSettingBool = FUNCTION - /** - * Sets a setting in the config - * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {number} value - */ - setSettingInt = FUNCTION - /** - * Sets a setting in the config - * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {string} value + * @param {string|number|boolean} value */ setSetting = FUNCTION /** diff --git a/common/src/module/latex.mjs b/common/src/module/latex.mjs index 4ff81cb..a6aa216 100644 --- a/common/src/module/latex.mjs +++ b/common/src/module/latex.mjs @@ -81,7 +81,7 @@ class LatexAPI extends Module { initialize({ latex, helper }) { super.initialize({ latex, helper }) this.#latex = latex - this.enabled = helper.getSettingBool("enable_latex") + this.enabled = helper.getSetting("enable_latex") } /** diff --git a/common/src/module/settings.mjs b/common/src/module/settings.mjs index 8ea152d..d630e7a 100644 --- a/common/src/module/settings.mjs +++ b/common/src/module/settings.mjs @@ -81,21 +81,9 @@ class SettingsAPI extends Module { initialize({ helper }) { super.initialize({ helper }) // Initialize default values. - for(const key of this.#properties.keys()) { - if(!this.#nonConfigurable.includes(key)) { - switch(typeof this.#properties.get(key)) { - case "boolean": - this.set(key, helper.getSettingBool("default_graph."+key), false) - break - case "number": - this.set(key, helper.getSettingInt("default_graph."+key), false) - break - case "string": - this.set(key, helper.getSetting("default_graph."+key), false) - break - } - } - } + for(const key of this.#properties.keys()) + if(!this.#nonConfigurable.includes(key)) + this.set(key, helper.getSetting("default_graph."+key), false) } /** diff --git a/common/src/preferences/common.mjs b/common/src/preferences/common.mjs index e456dc2..812263c 100644 --- a/common/src/preferences/common.mjs +++ b/common/src/preferences/common.mjs @@ -53,11 +53,11 @@ export class BoolSetting extends Setting { } value() { - return Helper.getSettingBool(this.nameInConfig) + return Helper.getSetting(this.nameInConfig) } set(value) { - Helper.setSettingBool(this.nameInConfig, value) + Helper.setSetting(this.nameInConfig, value === true) } } @@ -69,11 +69,11 @@ export class NumberSetting extends Setting { } value() { - return Helper.getSettingInt(this.nameInConfig) + return Helper.getSetting(this.nameInConfig) } set(value) { - Helper.setSettingInt(this.nameInConfig, value) + Helper.setSetting(this.nameInConfig, +value) } } @@ -84,11 +84,11 @@ export class EnumIntSetting extends Setting { } value() { - return Helper.getSettingInt(this.nameInConfig) + return Helper.getSetting(this.nameInConfig) } set(value) { - Helper.setSettingInt(this.nameInConfig, value) + Helper.setSetting(this.nameInConfig, +value) } } @@ -131,6 +131,6 @@ export class StringSetting extends Setting { } set(value) { - Helper.setSetting(this.nameInConfig, value) + Helper.setSetting(this.nameInConfig, ""+value) } } \ No newline at end of file diff --git a/common/src/preferences/default.mjs b/common/src/preferences/default.mjs index 20d62e3..1d7e4bd 100644 --- a/common/src/preferences/default.mjs +++ b/common/src/preferences/default.mjs @@ -37,7 +37,7 @@ const XMIN = new NumberSetting( qsTranslate("Settings", "Min X"), "default_graph.xmin", "xmin", - () => Helper.getSettingBool("default_graph.logscalex") ? 1e-100 : -Infinity + () => Helper.getSetting("default_graph.logscalex") ? 1e-100 : -Infinity ) const YMAX = new NumberSetting( diff --git a/common/test/mock/helper.mjs b/common/test/mock/helper.mjs index 9f74b64..c2b024f 100644 --- a/common/test/mock/helper.mjs +++ b/common/test/mock/helper.mjs @@ -53,7 +53,13 @@ export class MockHelper { this.__settings = { ...DEFAULT_SETTINGS } } - __getSetting(settingName) { + + /** + * Gets a setting from the config + * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") + * @returns {string|number|boolean} Value of the setting + */ + getSetting(settingName) { const namespace = settingName.split(".") let data = this.__settings for(const name of namespace) @@ -64,7 +70,12 @@ export class MockHelper { return data } - __setSetting(settingName, value) { + /** + * Sets a setting in the config + * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") + * @param {string|number|boolean} value + */ + setSetting(settingName, value) { const namespace = settingName.split(".") const finalName = namespace.pop() let data = this.__settings @@ -76,60 +87,6 @@ export class MockHelper { data[finalName] = value } - /** - * Gets a setting from the config - * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {boolean} Value of the setting - */ - getSettingBool(settingName) { - return this.__getSetting(settingName) === true - } - - /** - * Gets a setting from the config - * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {number} Value of the setting - */ - getSettingInt(settingName) { - return +(this.__getSetting(settingName)) - } - - /** - * Gets a setting from the config - * @param {string} settingName - Setting (and its dot-separated namespace) to get (e.g. "default_graph.xmin") - * @returns {string} Value of the setting - */ - getSetting(settingName) { - return this.__getSetting(settingName).toString() - } - - /** - * Sets a setting in the config - * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {boolean} value - */ - setSettingBool(settingName, value) { - return this.__setSetting(settingName, value === true) - } - - /** - * Sets a setting in the config - * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {number} value - */ - setSettingInt(settingName, value) { - return this.__setSetting(settingName, +(value)) - } - - /** - * Sets a setting in the config - * @param {string} settingName - Setting (and its dot-separated namespace) to set (e.g. "default_graph.xmin") - * @param {string} value - */ - setSetting(settingName, value) { - return this.__setSetting(settingName, value.toString()) - } - /** * Sends data to be written * @param {string} file diff --git a/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml b/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml index a0c030a..bb99f52 100644 --- a/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml +++ b/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ExpressionEditor.qml @@ -221,9 +221,9 @@ Item { focus: true selectByMouse: true - property bool autocompleteEnabled: Helper.getSettingBool("autocompletion.enabled") - property bool syntaxHighlightingEnabled: Helper.getSettingBool("expression_editor.colorize") - property bool autoClosing: Helper.getSettingBool("expression_editor.autoclose") + property bool autocompleteEnabled: Helper.getSetting("autocompletion.enabled") + property bool syntaxHighlightingEnabled: Helper.getSetting("expression_editor.colorize") + property bool autoClosing: Helper.getSetting("expression_editor.autoclose") property var tokens: autocompleteEnabled || syntaxHighlightingEnabled ? parent.tokens(text) : [] Keys.priority: Keys.BeforeItem // Required for knowing which key the user presses. @@ -600,7 +600,7 @@ Item { */ function colorize(tokenList) { let parsedText = "" - let scheme = colorSchemes[Helper.getSettingInt("expression_editor.color_scheme")] + let scheme = colorSchemes[Helper.getSetting("expression_editor.color_scheme")] for(let token of tokenList) { switch(token.type) { case JS.Parsing.TokenType.VARIABLE: diff --git a/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml b/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml index b440d07..992e9b1 100644 --- a/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml +++ b/runtime-pyside6/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml @@ -44,25 +44,25 @@ ScrollView { Zoom on the x axis of the diagram, provided from settings. \sa Settings */ - property double xzoom: Helper.getSettingInt('default_graph.xzoom') + property double xzoom: Helper.getSetting('default_graph.xzoom') /*! \qmlproperty double Settings::yzoom Zoom on the y axis of the diagram, provided from settings. \sa Settings */ - property double yzoom: Helper.getSettingInt('default_graph.yzoom') + property double yzoom: Helper.getSetting('default_graph.yzoom') /*! \qmlproperty double Settings::xmin Minimum x of the diagram, provided from settings. \sa Settings */ - property double xmin: Helper.getSettingInt('default_graph.xmin') + property double xmin: Helper.getSetting('default_graph.xmin') /*! \qmlproperty double Settings::ymax Maximum y of the diagram, provided from settings. \sa Settings */ - property double ymax: Helper.getSettingInt('default_graph.ymax') + property double ymax: Helper.getSetting('default_graph.ymax') /*! \qmlproperty string Settings::xaxisstep Step of the x axis graduation, provided from settings. @@ -93,34 +93,34 @@ ScrollView { Width of lines that will be drawn into the canvas, provided from settings. \sa Settings */ - property double linewidth: Helper.getSettingInt('default_graph.linewidth') + property double linewidth: Helper.getSetting('default_graph.linewidth') /*! \qmlproperty double Settings::textsize Font size of the text that will be drawn into the canvas, provided from settings. \sa Settings */ - property double textsize: Helper.getSettingInt('default_graph.textsize') + property double textsize: Helper.getSetting('default_graph.textsize') /*! \qmlproperty bool Settings::logscalex true if the canvas should be in logarithmic mode, false otherwise. Provided from settings. \sa Settings */ - property bool logscalex: Helper.getSettingBool('default_graph.logscalex') + property bool logscalex: Helper.getSetting('default_graph.logscalex') /*! \qmlproperty bool Settings::showxgrad true if the x graduation should be shown, false otherwise. Provided from settings. \sa Settings */ - property bool showxgrad: Helper.getSettingBool('default_graph.showxgrad') + property bool showxgrad: Helper.getSetting('default_graph.showxgrad') /*! \qmlproperty bool Settings::showygrad true if the y graduation should be shown, false otherwise. Provided from settings. \sa Settings */ - property bool showygrad: Helper.getSettingBool('default_graph.showygrad') + property bool showygrad: Helper.getSetting('default_graph.showygrad') Column { spacing: 10 diff --git a/runtime-pyside6/LogarithmPlotter/util/helper.py b/runtime-pyside6/LogarithmPlotter/util/helper.py index 6db4570..344e425 100644 --- a/runtime-pyside6/LogarithmPlotter/util/helper.py +++ b/runtime-pyside6/LogarithmPlotter/util/helper.py @@ -15,10 +15,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . """ - from PySide6.QtWidgets import QMessageBox, QApplication from PySide6.QtCore import QRunnable, QThreadPool, QThread, QObject, Signal, Slot, QCoreApplication -from PySide6.QtQml import QQmlApplicationEngine +from PySide6.QtQml import QJSValue from PySide6.QtGui import QImage from PySide6 import __version__ as PySide6_version @@ -135,29 +134,13 @@ class Helper(QObject): def getVersion(self): return __VERSION__ - @Slot(str, result=str) - def getSetting(self, namespace): - return str(config.getSetting(namespace)) + @Slot(str, result=QJSValue) + def getSetting(self, namespace: str) -> QJSValue: + return QJSValue(config.getSetting(namespace)) - @Slot(str, result=float) - def getSettingInt(self, namespace): - return float(config.getSetting(namespace)) - - @Slot(str, result=bool) - def getSettingBool(self, namespace): - return bool(config.getSetting(namespace)) - - @Slot(str, str) - def setSetting(self, namespace, value): - return config.setSetting(namespace, str(value)) - - @Slot(str, bool) - def setSettingBool(self, namespace, value): - return config.setSetting(namespace, bool(value)) - - @Slot(str, float) - def setSettingInt(self, namespace, value): - return config.setSetting(namespace, float(value)) + @Slot(str, QJSValue) + def setSetting(self, namespace: str, value: QJSValue): + return config.setSetting(namespace, value.toPrimitive().toVariant()) @Slot(result=str) def getDebugInfos(self): diff --git a/runtime-pyside6/tests/test_helper.py b/runtime-pyside6/tests/test_helper.py index 412eac3..73dfb23 100644 --- a/runtime-pyside6/tests/test_helper.py +++ b/runtime-pyside6/tests/test_helper.py @@ -158,16 +158,16 @@ class TestHelper: obj = Helper(pwd, tmpfile) assert obj.getVersion() == version assert type(obj.getDebugInfos()) == str - assert type(obj.getSetting("check_for_updates")) == str - assert type(obj.getSettingInt("check_for_updates")) == float - assert type(obj.getSettingBool("check_for_updates")) == bool + assert type(obj.getSetting("last_install_greet").toVariant()) == str + assert type(obj.getSetting("check_for_updates").toVariant()) == bool + assert type(obj.getSetting("default_graph.xzoom").toVariant()) in [float, int] def test_set_config(self, temporary): tmpfile, directory = temporary obj = Helper(pwd, tmpfile) obj.setSetting("last_install_greet", obj.getSetting("last_install_greet")) - obj.setSettingBool("check_for_updates", obj.getSettingBool("check_for_updates")) - obj.setSettingInt("default_graph.xzoom", obj.getSettingInt("default_graph.xzoom")) + obj.setSetting("check_for_updates", obj.getSetting("check_for_updates")) + obj.setSetting("default_graph.xzoom", obj.getSetting("default_graph.xzoom")) def test_fetch_changelog(self, temporary, qtbot): tmpfile, directory = temporary