From 781a0f4aae7e5a0ea10d12476a4f99cab1833d8d Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Tue, 2 Apr 2024 00:11:56 +0200 Subject: [PATCH] Enforcing type safety at export --- .../qml/eu/ad5001/LogarithmPlotter/js/io.mjs | 22 +++++++++---------- .../LogarithmPlotter/js/objs/common.mjs | 4 +--- .../LogarithmPlotter/js/objs/function.mjs | 6 ----- .../LogarithmPlotter/js/objs/gainbode.mjs | 5 ----- .../LogarithmPlotter/js/objs/phasebode.mjs | 5 ----- .../ad5001/LogarithmPlotter/js/objs/point.mjs | 4 ---- .../LogarithmPlotter/js/objs/repartition.mjs | 13 +++++------ .../LogarithmPlotter/js/objs/sequence.mjs | 6 ----- .../js/objs/sommegainsbode.mjs | 4 ---- .../js/objs/sommephasesbode.mjs | 4 ---- .../ad5001/LogarithmPlotter/js/objs/text.mjs | 4 ---- .../LogarithmPlotter/js/objs/xcursor.mjs | 6 ----- .../ad5001/LogarithmPlotter/js/parameters.mjs | 6 +++-- 13 files changed, 21 insertions(+), 68 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/io.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/io.mjs index f9fde47..396ada8 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/io.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/io.mjs @@ -98,15 +98,15 @@ class IOAPI extends Module { Modules.History.clear() // Importing settings this.settings.saveFilename = filename - this.settings.xzoom = data["xzoom"] - this.settings.yzoom = data["yzoom"] - this.settings.xmin = data["xmin"] - this.settings.ymax = data["ymax"] - this.settings.xaxisstep = data["xaxisstep"] - this.settings.yaxisstep = data["yaxisstep"] - this.settings.xlabel = data["xaxislabel"] - this.settings.ylabel = data["yaxislabel"] - this.settings.logscalex = data["logscalex"] + this.settings.xzoom = parseFloat(data["xzoom"]) || 100 + this.settings.yzoom = parseFloat(data["yzoom"]) || 10 + this.settings.xmin = parseFloat(data["xmin"]) || 5/10 + this.settings.ymax = parseFloat(data["ymax"]) || 24 + this.settings.xaxisstep = data["xaxisstep"] || "4" + this.settings.yaxisstep = data["yaxisstep"] || "4" + this.settings.xlabel = data["xaxislabel"] || "" + this.settings.ylabel = data["yaxislabel"] || "" + this.settings.logscalex = data["logscalex"] === true if("showxgrad" in data) this.settings.showxgrad = data["showxgrad"] if("showygrad" in data) @@ -115,8 +115,8 @@ class IOAPI extends Module { this.settings.linewidth = data["linewidth"] if("textsize" in data) this.settings.textsize = data["textsize"] - this.rootElement.height = data["height"] - this.rootElement.width = data["width"] + this.rootElement.height = parseFloat(data["height"]) || 500 + this.rootElement.width = parseFloat(data["width"]) || 1000 // Importing objects Modules.Objects.currentObjects = {} diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs index c38b5c1..173aef4 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.mjs @@ -139,11 +139,9 @@ export class DrawableObject { */ static import(name, visible, color, labelContent, ...args) { let importedArgs = [name.toString(), visible === true, color.toString(), labelContent] - console.log('---') - console.log(this, name, args) + console.log('Importing', this.type(), name, args) for(let [name, propType] of Object.entries(this.properties())) if(!name.startsWith('comment')) { - console.log(name, propType, importedArgs.length-4, args[importedArgs.length-4]) importedArgs.push(ensureTypeSafety(propType, args[importedArgs.length-4])) } return new this(...importedArgs) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs index 2386a96..fb9e93c 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/function.mjs @@ -82,12 +82,6 @@ export default class Function extends ExecutableObject { } } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.expression.toEditableString(), this.definitionDomain.toString(), this.destinationDomain.toString(), - this.displayMode, this.labelPosition, this.labelX, this.drawPoints, this.drawDashedLines] - } - execute(x = 1) { if(this.definitionDomain.includes(x)) return this.expression.execute(x) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs index fb0e9cd..a4a6416 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.mjs @@ -81,11 +81,6 @@ export default class GainBode extends ExecutableObject { \\end{array}` } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.om_0.name, this.pass.toString(), this.gain.toEditableString(), this.labelPosition, this.labelX, this.omGraduation] - } - execute(x=1) { if(typeof x == 'string') x = executeExpression(x) if((this.pass === 'high' && x < this.om_0.x) || (this.pass === 'low' && x > this.om_0.x)) { diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs index 5b32ec5..bee6ee6 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.mjs @@ -64,11 +64,6 @@ export default class PhaseBode extends ExecutableObject { this.labelX = labelX } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.om_0.name, this.phase.toEditableString(), this.unit, this.labelPosition, this.labelX] - } - getReadableString() { return `${this.name}: ${this.phase.toString(true)}${this.unit} at ${this.om_0.name} = ${this.om_0.x}` } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs index 03de816..f53eb72 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/point.mjs @@ -54,10 +54,6 @@ export default class Point extends DrawableObject { return `${Latex.variable(this.name)} = \\left(${this.x.latexMarkup}, ${this.y.latexMarkup}\\right)` } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, this.x.toEditableString(), this.y.toEditableString(), this.labelPosition, this.pointStyle] - } - draw(canvas) { let [canvasX, canvasY] = [canvas.x2px(this.x.execute()), canvas.y2px(this.y.execute())] let pointSize = 8+(canvas.linewidth*2) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs index 6c5245d..2dd632d 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/repartition.mjs @@ -35,9 +35,12 @@ export default class RepartitionFunction extends ExecutableObject { [QT_TRANSLATE_NOOP('prop','labelX')]: 'number', }} static import(name, visible, color, labelContent, ...args) { - if(args.length === 5) + console.log(args, args.length) + if(args.length === 5) { // Two legacy values no longer included. - args.shift(); args.shift() + args.shift() + args.shift() + } return super.import(name, visible, color, labelContent, ...args) } @@ -51,12 +54,6 @@ export default class RepartitionFunction extends ExecutableObject { this.update() } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.probabilities, this.labelPosition, this.labelX] - } - - getReadableString() { let keys = Object.keys(this.probabilities).sort((a,b) => a-b); let varname = this.name.substring(this.name.indexOf("_")+1) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs index a2e8673..10cb154 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sequence.mjs @@ -55,12 +55,6 @@ export default class Sequence extends ExecutableObject { this.labelX = labelX this.update() } - - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.drawPoints, this.drawDashedLines, this.defaultExpression, this.baseValues, - this.labelPosition, this.labelX] - } update() { console.log('Updating sequence', this.sequence) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommegainsbode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommegainsbode.mjs index 947c99f..236c3d4 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommegainsbode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommegainsbode.mjs @@ -44,10 +44,6 @@ export default class SommeGainsBode extends ExecutableObject { this.recalculateCache() } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX] - } - getReadableString() { return `${this.name} = ${Objects.getObjectsName('Gain Bode').join(' + ')}` } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommephasesbode.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommephasesbode.mjs index 30ad4b0..1483788 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommephasesbode.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/sommephasesbode.mjs @@ -42,10 +42,6 @@ export default class SommePhasesBode extends ExecutableObject { this.recalculateCache() } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX] - } - getReadableString() { return `${this.name} = ${Objects.getObjectsName('Phase Bode').join(' + ')}` } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs index ed62b81..dfd0d27 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/text.mjs @@ -78,10 +78,6 @@ export default class Text extends DrawableObject { return `${Latex.variable(this.name)} = "\\textsf{${this.latexMarkupText()}}"` } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, this.x.toEditableString(), this.y.toEditableString(), this.labelPosition, this.text, this.disableLatex] - } - getLabel() { return this.text } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs index 45727dd..2e3c6e0 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.mjs @@ -60,12 +60,6 @@ export default class XCursor extends DrawableObject { this.targetValuePosition = targetValuePosition } - export() { - return [this.name, this.visible, this.color.toString(), this.labelContent, - this.x.toEditableString(), this.targetElement == null ? null : this.targetElement.name, this.labelPosition, - this.approximate, this.rounding, this.displayStyle, this.targetValuePosition] - } - getReadableString() { if(this.targetElement == null) return `${this.name} = ${this.x.toString()}` return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}` diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parameters.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parameters.mjs index 0427531..72f9dee 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parameters.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/parameters.mjs @@ -144,8 +144,10 @@ export class ObjectType extends PropertyType { } export(value) { - if(value.type === this.objType || (this.objType === 'ExecutableObject' && value.execute)) - return value + if(value == null && this.allowNull) + return null + else if(value.type === this.objType || (this.objType === 'ExecutableObject' && value.execute)) + return value.name else throw new TypeError(`Exportation error: ${value} not a ${this.objType}.`) }