Converting actual settings to new Settings module.

This commit is contained in:
Adsooi 2024-10-10 23:28:25 +02:00
parent d1ac70a946
commit 52f859349a
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
8 changed files with 176 additions and 75 deletions

View file

@ -53,7 +53,7 @@ export class BaseEventEmitter {
* @param {string} eventType - Name of the event to listen to. Throws an error if this object does not emit this kind of event. * @param {string} eventType - Name of the event to listen to. Throws an error if this object does not emit this kind of event.
* @param {function(BaseEvent)} eventListener - The function to be called back when the event is emitted. * @param {function(BaseEvent)} eventListener - The function to be called back when the event is emitted.
*/ */
addEventListener(eventType, eventListener) { on(eventType, eventListener) {
if(!this.constructor.emits.includes(eventType)) { if(!this.constructor.emits.includes(eventType)) {
const className = this.constructor.name const className = this.constructor.name
const eventTypes = this.constructor.emits.join(", ") const eventTypes = this.constructor.emits.join(", ")
@ -70,14 +70,13 @@ export class BaseEventEmitter {
* @param {function(BaseEvent)} eventListener - The function previously registered as a listener. * @param {function(BaseEvent)} eventListener - The function previously registered as a listener.
* @returns {boolean} True if the listener was removed, false if it was not found. * @returns {boolean} True if the listener was removed, false if it was not found.
*/ */
removeEventListener(eventType, eventListener) { off(eventType, eventListener) {
if(!this.constructor.emits.includes(eventType)) { if(!this.constructor.emits.includes(eventType)) {
const className = this.constructor.name const className = this.constructor.name
const eventTypes = this.constructor.emits.join(", ") const eventTypes = this.constructor.emits.join(", ")
throw new Error(`Cannot listen to unknown event ${eventType} in class ${className}. ${className} only emits: ${eventTypes}`) throw new Error(`Cannot listen to unknown event ${eventType} in class ${className}. ${className} only emits: ${eventTypes}`)
} }
return this.#listeners[eventType].delete(eventListener) return this.#listeners[eventType].delete(eventListener)
} }
/** /**

View file

@ -62,7 +62,7 @@ class IOAPI extends Module {
// Add extension if necessary // Add extension if necessary
if(["lpf"].indexOf(filename.split(".")[filename.split(".").length - 1]) === -1) if(["lpf"].indexOf(filename.split(".")[filename.split(".").length - 1]) === -1)
filename += ".lpf" filename += ".lpf"
this.saveFilename = filename Settings.set("saveFilename", filename, false)
let objs = {} let objs = {}
for(let objType in Objects.currentObjects) { for(let objType in Objects.currentObjects) {
objs[objType] = [] objs[objType] = []

View file

@ -48,6 +48,8 @@ class ChangedEvent extends BaseEvent {
class SettingsAPI extends Module { class SettingsAPI extends Module {
static emits = ["changed"] static emits = ["changed"]
#nonConfigurable = ["saveFilename"]
#properties = new Map([ #properties = new Map([
["saveFilename", ""], ["saveFilename", ""],
["xzoom", 100], ["xzoom", 100],
@ -75,16 +77,18 @@ class SettingsAPI extends Module {
super.initialize({ helper }) super.initialize({ helper })
// Initialize default values. // Initialize default values.
for(const key of this.#properties.keys()) { for(const key of this.#properties.keys()) {
switch(typeof this.#properties.get(key)) { if(!this.#nonConfigurable.includes(key)) {
case "boolean": switch(typeof this.#properties.get(key)) {
this.set(key, helper.getSettingBool("default_graph."+key), false) case "boolean":
break this.set(key, helper.getSettingBool("default_graph."+key), false)
case "number": break
this.set(key, helper.getSettingInt("default_graph."+key), false) case "number":
break this.set(key, helper.getSettingInt("default_graph."+key), false)
case "string": break
this.set(key, helper.getSetting("default_graph."+key), false) case "string":
break this.set(key, helper.getSetting("default_graph."+key), false)
break
}
} }
} }
} }
@ -101,8 +105,9 @@ class SettingsAPI extends Module {
throw new Error(`Property ${property} is not a setting.`) throw new Error(`Property ${property} is not a setting.`)
} }
const oldValue = this.#properties.get(property) const oldValue = this.#properties.get(property)
console.debug("Setting", property, "from", oldValue, "to", value, `(${typeof value}, ${byUser})`)
const propType = typeof oldValue const propType = typeof oldValue
if(byUser)
console.debug("Setting", property, "from", oldValue, "to", value, `(${typeof value}, ${byUser})`)
if(propType !== typeof value) if(propType !== typeof value)
throw new Error(`Value of ${property} must be a ${propType} (${typeof value} provided).`) throw new Error(`Value of ${property} must be a ${propType} (${typeof value} provided).`)
this.#properties.set(property, value) this.#properties.set(property, value)

View file

@ -28,7 +28,7 @@ const XZOOM = new NumberSetting(
const YZOOM = new NumberSetting( const YZOOM = new NumberSetting(
qsTranslate("Settings", "Y Zoom"), qsTranslate("Settings", "Y Zoom"),
"default_graph.xzoom", "default_graph.yzoom",
"yzoom", "yzoom",
0.1 0.1
) )

View file

@ -42,7 +42,7 @@ ApplicationWindow {
width: 1000 width: 1000
height: 500 height: 500
color: sysPalette.window color: sysPalette.window
title: "LogarithmPlotter " + (settings.saveFilename != "" ? " - " + settings.saveFilename.split('/').pop() : "") + (history.saved ? "" : "*") title: "LogarithmPlotter"
SystemPalette { id: sysPalette; colorGroup: SystemPalette.Active } SystemPalette { id: sysPalette; colorGroup: SystemPalette.Active }
SystemPalette { id: sysPaletteIn; colorGroup: SystemPalette.Disabled } SystemPalette { id: sysPaletteIn; colorGroup: SystemPalette.Disabled }
@ -250,5 +250,12 @@ ApplicationWindow {
Component.onCompleted: { Component.onCompleted: {
Modules.IO.initialize({ root, settings, alert }) Modules.IO.initialize({ root, settings, alert })
Modules.Latex.initialize({ latex: Latex, helper: Helper }) Modules.Latex.initialize({ latex: Latex, helper: Helper })
Modules.Settings.on("changed", (evt) => {
if(evt.property === "saveFilename") {
const fileName = evt.newValue.split('/').pop().split('\\').pop()
if(fileName !== "")
title = `${fileName}`
}
})
} }
} }

View file

@ -285,7 +285,7 @@ Item {
const axisX = Modules.Canvas.axesSteps.x.value const axisX = Modules.Canvas.axesSteps.x.value
const xpos = Modules.Canvas.px2x(picker.mouseX) const xpos = Modules.Canvas.px2x(picker.mouseX)
if(snapToGridCheckbox.checked) { if(snapToGridCheckbox.checked) {
if(canvas.logscalex) { if(Modules.Settings.logscalex) {
// Calculate the logged power // Calculate the logged power
let pow = Math.pow(10, Math.floor(Math.log10(xpos))) let pow = Math.pow(10, Math.floor(Math.log10(xpos)))
return pow*Math.round(xpos/pow) return pow*Math.round(xpos/pow)

View file

@ -37,7 +37,7 @@ Item {
Emitted when the value of the text has been changed. Emitted when the value of the text has been changed.
The corresponding handler is \c onChanged. The corresponding handler is \c onChanged.
*/ */
signal changed(string newValue) signal changed(var newValue)
/*! /*!
\qmlproperty bool TextSetting::isInt \qmlproperty bool TextSetting::isInt

View file

@ -121,11 +121,6 @@ ScrollView {
\sa Settings \sa Settings
*/ */
property bool showygrad: Helper.getSettingBool('default_graph.showygrad') property bool showygrad: Helper.getSettingBool('default_graph.showygrad')
/*!
\qmlproperty bool Settings::saveFilename
Path of the currently opened file. Empty if no file is opened.
*/
property string saveFilename: ""
Column { Column {
spacing: 10 spacing: 10
@ -136,15 +131,18 @@ ScrollView {
id: fdiag id: fdiag
onAccepted: { onAccepted: {
var filePath = fdiag.currentFile.toString().substr(7) var filePath = fdiag.currentFile.toString().substr(7)
settings.saveFilename = filePath Modules.Settings.set("saveFilename", filePath)
if(exportMode) { if(exportMode) {
Modules.IO.saveDiagram(filePath) Modules.IO.saveDiagram(filePath)
} else { } else {
Modules.IO.loadDiagram(filePath) Modules.IO.loadDiagram(filePath)
if(xAxisLabel.find(settings.xlabel) == -1) xAxisLabel.model.append({text: settings.xlabel}) // Adding labels.
xAxisLabel.editText = settings.xlabel if(xAxisLabel.find(Modules.Settings.xlabel) === -1)
if(yAxisLabel.find(settings.ylabel) == -1) yAxisLabel.model.append({text: settings.ylabel}) xAxisLabel.model.append({text: Modules.Settings.xlabel})
yAxisLabel.editText = settings.ylabel xAxisLabel.editText = Modules.Settings.xlabel
if(yAxisLabel.find(Modules.Settings.ylabel) === -1)
yAxisLabel.model.append({text: Modules.Settings.ylabel})
yAxisLabel.editText = Modules.Settings.ylabel
} }
} }
} }
@ -158,11 +156,16 @@ ScrollView {
min: 0.1 min: 0.1
icon: "settings/xzoom.svg" icon: "settings/xzoom.svg"
width: settings.settingWidth width: settings.settingWidth
value: settings.xzoom.toFixed(2)
onChanged: function(newValue) { onChanged: function(newValue) {
settings.xzoom = newValue Modules.Settings.set("xzoom", newValue, true)
settings.changed() settings.changed()
} }
function update(newValue) {
value = Modules.Settings.xzoom.toFixed(2)
maxX.update()
}
} }
Setting.TextSetting { Setting.TextSetting {
@ -173,11 +176,16 @@ ScrollView {
label: qsTr("Y Zoom") label: qsTr("Y Zoom")
icon: "settings/yzoom.svg" icon: "settings/yzoom.svg"
width: settings.settingWidth width: settings.settingWidth
value: settings.yzoom.toFixed(2)
onChanged: function(newValue) { onChanged: function(newValue) {
settings.yzoom = newValue Modules.Settings.set("yzoom", newValue, true)
settings.changed() settings.changed()
} }
function update(newValue) {
value = Modules.Settings.yzoom.toFixed(2)
minY.update()
}
} }
// Positioning the graph // Positioning the graph
@ -189,14 +197,18 @@ ScrollView {
label: qsTr("Min X") label: qsTr("Min X")
icon: "settings/xmin.svg" icon: "settings/xmin.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.xmin
onChanged: function(newValue) { onChanged: function(newValue) {
if(parseFloat(maxX.value) > newValue) { Modules.Settings.set("xmin", newValue, true)
settings.xmin = newValue settings.changed()
settings.changed() }
} else {
alert.show("Minimum x value must be inferior to maximum.") function update(newValue) {
} let newVal = Modules.Settings.xmin
if(newVal > 1e-5)
newVal = newVal.toDecimalPrecision(8)
value = newVal
maxX.update()
} }
} }
@ -208,11 +220,16 @@ ScrollView {
label: qsTr("Max Y") label: qsTr("Max Y")
icon: "settings/ymax.svg" icon: "settings/ymax.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.ymax
onChanged: function(newValue) { onChanged: function(newValue) {
settings.ymax = newValue Modules.Settings.set("ymax", newValue, true)
settings.changed() settings.changed()
} }
function update() {
value = Modules.Settings.ymax
minY.update()
}
} }
Setting.TextSetting { Setting.TextSetting {
@ -223,15 +240,24 @@ ScrollView {
label: qsTr("Max X") label: qsTr("Max X")
icon: "settings/xmax.svg" icon: "settings/xmax.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: Modules.Canvas.px2x(canvas.width).toFixed(2)
onChanged: function(xvaluemax) { onChanged: function(xvaluemax) {
if(xvaluemax > settings.xmin) { if(xvaluemax > Modules.Settings.xmin) {
settings.xzoom = settings.xzoom * canvas.width/(Modules.Canvas.x2px(xvaluemax)) // Adjusting zoom to fit. = (end)/(px of current point) const newXZoom = Modules.Settings.xzoom * canvas.width/(Modules.Canvas.x2px(xvaluemax)) // Adjusting zoom to fit. = (end)/(px of current point)
Modules.Settings.set("xzoom", newXZoom, true)
zoomX.update()
settings.changed() settings.changed()
} else { } else {
alert.show("Maximum x value must be superior to minimum.") alert.show("Maximum x value must be superior to minimum.")
} }
} }
function update() {
let newVal = Modules.Canvas.px2x(canvas.width)
if(newVal > 1e-5)
newVal = newVal.toDecimalPrecision(8)
value = newVal
}
} }
Setting.TextSetting { Setting.TextSetting {
@ -242,15 +268,21 @@ ScrollView {
label: qsTr("Min Y") label: qsTr("Min Y")
icon: "settings/ymin.svg" icon: "settings/ymin.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: Modules.Canvas.px2y(canvas.height).toFixed(2)
onChanged: function(yvaluemin) { onChanged: function(yvaluemin) {
if(yvaluemin < settings.ymax) { if(yvaluemin < settings.ymax) {
settings.yzoom = settings.yzoom * canvas.height/(Modules.Canvas.y2px(yvaluemin)) // Adjusting zoom to fit. = (end)/(px of current point) const newYZoom = Modules.Settings.yzoom * canvas.height/(Modules.Canvas.y2px(yvaluemin)) // Adjusting zoom to fit. = (end)/(px of current point)
Modules.Settings.set("yzoom", newYZoom, true)
zoomY.update()
settings.changed() settings.changed()
} else { } else {
alert.show("Minimum y value must be inferior to maximum.") alert.show("Minimum y value must be inferior to maximum.")
} }
} }
function update() {
value = Modules.Canvas.px2y(canvas.height).toDecimalPrecision(8)
}
} }
Setting.TextSetting { Setting.TextSetting {
@ -260,12 +292,16 @@ ScrollView {
label: qsTr("X Axis Step") label: qsTr("X Axis Step")
icon: "settings/xaxisstep.svg" icon: "settings/xaxisstep.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.xaxisstep
visible: !settings.logscalex
onChanged: function(newValue) { onChanged: function(newValue) {
settings.xaxisstep = newValue Modules.Settings.set("xaxisstep", newValue, true)
settings.changed() settings.changed()
} }
function update() {
value = Modules.Settings.xaxisstep
visible = !Modules.Settings.logscalex
}
} }
Setting.TextSetting { Setting.TextSetting {
@ -275,11 +311,13 @@ ScrollView {
label: qsTr("Y Axis Step") label: qsTr("Y Axis Step")
icon: "settings/yaxisstep.svg" icon: "settings/yaxisstep.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.yaxisstep
onChanged: function(newValue) { onChanged: function(newValue) {
settings.yaxisstep = newValue Modules.Settings.set("yaxisstep", newValue, true)
settings.changed() settings.changed()
} }
function update() { value = Modules.Settings.yaxisstep }
} }
Setting.TextSetting { Setting.TextSetting {
@ -290,11 +328,13 @@ ScrollView {
min: 1 min: 1
icon: "settings/linewidth.svg" icon: "settings/linewidth.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.linewidth
onChanged: function(newValue) { onChanged: function(newValue) {
settings.linewidth = newValue Modules.Settings.set("linewidth", newValue, true)
settings.changed() settings.changed()
} }
function update() { value = Modules.Settings.linewidth }
} }
Setting.TextSetting { Setting.TextSetting {
@ -305,11 +345,13 @@ ScrollView {
min: 1 min: 1
icon: "settings/textsize.svg" icon: "settings/textsize.svg"
width: settings.settingWidth width: settings.settingWidth
defValue: settings.textsize
onChanged: function(newValue) { onChanged: function(newValue) {
settings.textsize = newValue Modules.Settings.set("textsize", newValue, true)
settings.changed() settings.changed()
} }
function update() { value = Modules.Settings.textsize }
} }
Setting.ComboBoxSetting { Setting.ComboBoxSetting {
@ -318,24 +360,31 @@ ScrollView {
width: settings.settingWidth width: settings.settingWidth
label: qsTr('X Label') label: qsTr('X Label')
icon: "settings/xlabel.svg" icon: "settings/xlabel.svg"
editable: true
model: ListModel { model: ListModel {
ListElement { text: "" } ListElement { text: "" }
ListElement { text: "x" } ListElement { text: "x" }
ListElement { text: "ω (rad/s)" } ListElement { text: "ω (rad/s)" }
} }
currentIndex: find(settings.xlabel)
editable: true
onAccepted: function(){ onAccepted: function(){
editText = JS.Utils.parseName(editText, false) editText = JS.Utils.parseName(editText, false)
if (find(editText) === -1) model.append({text: editText}) if(find(editText) === -1) model.append({text: editText})
settings.xlabel = editText currentIndex = find(editText)
Modules.Settings.set("xlabel", editText, true)
settings.changed() settings.changed()
} }
onActivated: function(selectedId) { onActivated: function(selectedId) {
settings.xlabel = model.get(selectedId).text Modules.Settings.set("xlabel", model.get(selectedId).text, true)
settings.changed() settings.changed()
} }
Component.onCompleted: editText = settings.xlabel
function update() {
editText = Modules.Settings.xlabel
if(find(editText) === -1) model.append({text: editText})
currentIndex = find(editText)
}
} }
Setting.ComboBoxSetting { Setting.ComboBoxSetting {
@ -344,6 +393,7 @@ ScrollView {
width: settings.settingWidth width: settings.settingWidth
label: qsTr('Y Label') label: qsTr('Y Label')
icon: "settings/ylabel.svg" icon: "settings/ylabel.svg"
editable: true
model: ListModel { model: ListModel {
ListElement { text: "" } ListElement { text: "" }
ListElement { text: "y" } ListElement { text: "y" }
@ -352,39 +402,52 @@ ScrollView {
ListElement { text: "φ (deg)" } ListElement { text: "φ (deg)" }
ListElement { text: "φ (rad)" } ListElement { text: "φ (rad)" }
} }
currentIndex: find(settings.ylabel)
editable: true
onAccepted: function(){ onAccepted: function(){
editText = JS.Utils.parseName(editText, false) editText = JS.Utils.parseName(editText, false)
if (find(editText) === -1) model.append({text: editText, yaxisstep: root.yaxisstep}) if(find(editText) === -1) model.append({text: editText})
settings.ylabel = editText currentIndex = find(editText)
Modules.Settings.set("ylabel", editText, true)
settings.changed() settings.changed()
} }
onActivated: function(selectedId) { onActivated: function(selectedId) {
settings.ylabel = model.get(selectedId).text Modules.Settings.set("ylabel", model.get(selectedId).text, true)
settings.changed() settings.changed()
} }
Component.onCompleted: editText = settings.ylabel
function update() {
editText = Modules.Settings.ylabel
if(find(editText) === -1) model.append({text: editText})
currentIndex = find(editText)
}
} }
CheckBox { CheckBox {
id: logScaleX id: logScaleX
checked: settings.logscalex
text: qsTr('X Log scale') text: qsTr('X Log scale')
onClicked: { onClicked: {
settings.logscalex = checked Modules.Settings.set("logscalex", checked, true)
if(Modules.Settings.xmin <= 0) // Reset xmin to prevent crash.
Modules.Settings.set("xmin", .5)
settings.changed() settings.changed()
} }
function update() {
checked = Modules.Settings.logscalex
xAxisStep.update()
}
} }
CheckBox { CheckBox {
id: showXGrad id: showXGrad
checked: settings.showxgrad
text: qsTr('Show X graduation') text: qsTr('Show X graduation')
onClicked: { onClicked: {
settings.showxgrad = checked Modules.Settings.set("showxgrad", checked, true)
settings.changed() settings.changed()
} }
function update() { checked = Modules.Settings.showxgrad }
} }
CheckBox { CheckBox {
@ -392,9 +455,10 @@ ScrollView {
checked: settings.showygrad checked: settings.showygrad
text: qsTr('Show Y graduation') text: qsTr('Show Y graduation')
onClicked: { onClicked: {
settings.showygrad = checked Modules.Settings.set("showygrad", checked, true)
settings.changed() settings.changed()
} }
function update() { checked = Modules.Settings.showygrad }
} }
Button { Button {
@ -440,10 +504,10 @@ ScrollView {
Saves the current canvas in the opened file. If no file is currently opened, prompts to pick a save location. Saves the current canvas in the opened file. If no file is currently opened, prompts to pick a save location.
*/ */
function save() { function save() {
if(settings.saveFilename == "") { if(Modules.Settings.saveFilename == "") {
saveAs() saveAs()
} else { } else {
Modules.IO.saveDiagram(settings.saveFilename) Modules.IO.saveDiagram(Modules.Settings.saveFilename)
} }
} }
@ -464,4 +528,30 @@ ScrollView {
fdiag.exportMode = false fdiag.exportMode = false
fdiag.open() fdiag.open()
} }
/**
* Initializing the settings
*/
Component.onCompleted: function() {
const matchedElements = new Map([
["xzoom", zoomX],
["yzoom", zoomY],
["xmin", minX],
["ymax", maxY],
["xaxisstep", xAxisStep],
["yaxisstep", yAxisStep],
["xlabel", xAxisLabel],
["ylabel", yAxisLabel],
["linewidth", lineWidth],
["textsize", textSize],
["logscalex", logScaleX],
["showxgrad", showXGrad],
["showygrad", showYGrad]
])
Modules.Settings.on("changed", (evt) => {
if(matchedElements.has(evt.property))
matchedElements.get(evt.property).update()
})
Modules.Settings.initialize({ helper: Helper })
}
} }