Unique names!

This commit is contained in:
Adsooi 2020-12-24 00:28:06 +01:00
parent 47a4ac67a9
commit efd258ce05
4 changed files with 43 additions and 35 deletions

View file

@ -43,7 +43,6 @@ Canvas {
property double yaxisstep1: yaxisstepExpr.execute(1) property double yaxisstep1: yaxisstepExpr.execute(1)
property int drawMaxY: Math.ceil(Math.max(Math.abs(ymax), Math.abs(px2y(canvasSize.height)))/yaxisstep1) property int drawMaxY: Math.ceil(Math.max(Math.abs(ymax), Math.abs(px2y(canvasSize.height)))/yaxisstep1)
Component.onCompleted: console.log(yaxisstepExpr.toEditableString())
onPaint: { onPaint: {
//console.log('Redrawing') //console.log('Redrawing')

View file

@ -62,6 +62,7 @@ ListView {
CheckBox { CheckBox {
id: visibilityCheckBox id: visibilityCheckBox
checked: Objects.currentObjects[objType][index].visible checked: Objects.currentObjects[objType][index].visible
anchors.verticalCenter: parent.verticalCenter
onClicked: { onClicked: {
Objects.currentObjects[objType][index].visible = !Objects.currentObjects[objType][index].visible Objects.currentObjects[objType][index].visible = !Objects.currentObjects[objType][index].visible
objectListList.changed() objectListList.changed()
@ -158,7 +159,10 @@ ListView {
defValue: objEditor.obj.name defValue: objEditor.obj.name
onChanged: function(newValue) { onChanged: function(newValue) {
if(Utils.parseName(newValue) != '') { if(Utils.parseName(newValue) != '') {
Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = Utils.parseName(newValue) var newName = Utils.parseName(newValue)
if(Objects.getObjectByName(newName) != null)
newName = Objects.getNewName(newName)
Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = newName
// TODO Resolve dependencies // TODO Resolve dependencies
objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex] objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
//objEditor.editingRow.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex] //objEditor.editingRow.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
@ -237,7 +241,7 @@ ListView {
onActivated: function(newIndex) { onActivated: function(newIndex) {
// Setting object property. // Setting object property.
if(selectObjMode) { if(selectObjMode) {
var selectedObj = Objects.getObjectByName(model[newIndex]) var selectedObj = Objects.getObjectByName(model[newIndex], modelData[1])
if(selectedObj == null) { if(selectedObj == null) {
selectedObj = Objects.createNewRegisteredObject(modelData[1]) selectedObj = Objects.createNewRegisteredObject(modelData[1])
model = Objects.getObjectsName(modelData[1]).concat(['+ Create new ' + modelData[1]]) model = Objects.getObjectsName(modelData[1]).concat(['+ Create new ' + modelData[1]])

View file

@ -23,12 +23,17 @@
function getNewName(allowedLetters, category) { function getNewName(allowedLetters) {
if(Object.keys(currentObjects).indexOf(category) == -1) return allowedLetters[0] var newid = 0
var newid = currentObjects[category].length var ret
do {
var letter = allowedLetters[newid % allowedLetters.length] var letter = allowedLetters[newid % allowedLetters.length]
var num = Math.round((newid - (newid % allowedLetters.length)) / allowedLetters.length) var num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length)
return letter + (num > 0 ? Utils.textsub(num) : '') ret = letter + (num > 0 ? Utils.textsub(num) : '')
newid += 1
console.log
} while(getObjectByName(ret) != null)
return ret
} }
class DrawableObject { class DrawableObject {
@ -105,7 +110,7 @@ class Point extends DrawableObject {
constructor(name = null, visible = true, color = null, labelContent = 'name + value', constructor(name = null, visible = true, color = null, labelContent = 'name + value',
x = 1, y = 0, labelPos = 'top', pointStyle = 'dot') { x = 1, y = 0, labelPos = 'top', pointStyle = 'dot') {
if(name == null) name = getNewName('ABCDEFJKLMNOPQRSTUVW', 'Point') if(name == null) name = getNewName('ABCDEFJKLMNOPQRSTUVW')
super(name, visible, color, labelContent) super(name, visible, color, labelContent)
this.type = 'Point' this.type = 'Point'
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString()) if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
@ -185,7 +190,7 @@ class Function extends ExecutableObject {
constructor(name = null, visible = true, color = null, labelContent = 'name + value', constructor(name = null, visible = true, color = null, labelContent = 'name + value',
expression = 'x', inDomain = 'RPE', outDomain = 'R', displayMode = 'application', labelPos = 'above', labelX = 1) { expression = 'x', inDomain = 'RPE', outDomain = 'R', displayMode = 'application', labelPos = 'above', labelX = 1) {
if(name == null) name = getNewName('fghjqlmnopqrstuvwabcde', 'Function') if(name == null) name = getNewName('fghjqlmnopqrstuvwabcde')
super(name, visible, color, labelContent) super(name, visible, color, labelContent)
if(typeof expression == 'number' || typeof expression == 'string') expression = new MathLib.Expression(expression.toString()) if(typeof expression == 'number' || typeof expression == 'string') expression = new MathLib.Expression(expression.toString())
this.expression = expression this.expression = expression
@ -280,16 +285,16 @@ class GainBode extends ExecutableObject {
constructor(name = null, visible = true, color = null, labelContent = 'name + value', constructor(name = null, visible = true, color = null, labelContent = 'name + value',
ω_0 = '', pass = 'high', gain = '20', labelPos = 'above', labelX = 1) { ω_0 = '', pass = 'high', gain = '20', labelPos = 'above', labelX = 1) {
if(name == null) name = getNewName('G', 'Gain Bode') if(name == null) name = getNewName('G')
if(name == 'G') name = 'G₀' // G is reserved for sum of BODE magnitues (Somme gains Bode). if(name == 'G') name = 'G₀' // G is reserved for sum of BODE magnitues (Somme gains Bode).
super(name, visible, color, labelContent) super(name, visible, color, labelContent)
if(typeof ω_0 == "string") { if(typeof ω_0 == "string") {
// Point name or create one // Point name or create one
ω_0 = getObjectByName('Point', ω_0) ω_0 = getObjectByName(ω_0, 'Point')
if(ω_0 == null) { if(ω_0 == null) {
// Create new point // Create new point
ω_0 = createNewRegisteredObject('Point') ω_0 = createNewRegisteredObject('Point')
ω_0.name = getNewName('ω', 'Gain Bode') ω_0.name = getNewName('ω')
ω_0.color = this.color ω_0.color = this.color
labelPos = 'below' labelPos = 'below'
} }
@ -510,7 +515,7 @@ class CursorX extends DrawableObject {
static type(){return 'CursorX'} static type(){return 'CursorX'}
static typeMultiple(){return 'CursorX'} static typeMultiple(){return 'CursorX'}
static properties() { static properties() {
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
var elementNames = [] var elementNames = []
elementTypes.forEach(function(elemType){ elementTypes.forEach(function(elemType){
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name)) elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
@ -525,27 +530,18 @@ class CursorX extends DrawableObject {
constructor(name = null, visible = true, color = null, labelContent = 'name + value', constructor(name = null, visible = true, color = null, labelContent = 'name + value',
x = 1, element = null, labelPos = 'left') { x = 1, element = null, labelPos = 'left') {
if(name == null) name = getNewName('ABCDEFJKLMNOPQRSTUVW', 'Point') if(name == null) name = getNewName('ABCDEFJKLMNOPQRSTUVW')
super(name, visible, color, labelContent) super(name, visible, color, labelContent)
this.type = 'CursorX' this.type = 'CursorX'
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString()) if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
this.getElement() var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
this.labelPost = labelPos this.element = getObjectByName(this.element, elementTypes)
this.labelPos = labelPos
} }
update() { update() {
if(typeof this.element == 'string') this.getElement() if(typeof this.element == 'string')
console.log(this.element) this.element = getObjectByName(this.element, elementTypes)
}
getElement(){
var element = this.element
var elem = null
Object.keys(currentObjects).forEach(function(objType){
var ele = getObjectByName(objType, element)
if(ele != null) elem = ele
})
this.element = elem
} }
} }
@ -559,15 +555,25 @@ const types = {
var currentObjects = {} var currentObjects = {}
function getObjectByName(objType, objName) { function getObjectByName(objName, objType = null) {
var objectTypes = Object.keys(currentObjects)
if(typeof objType == 'string') {
if(currentObjects[objType] == undefined) return null if(currentObjects[objType] == undefined) return null
objectTypes = [objType]
}
if(Array.isArray(objType)) objectTypes = objType
var retObj = null var retObj = null
objectTypes.forEach(function(objType){
if(currentObjects[objType] == undefined) return null
currentObjects[objType].forEach(function(obj){ currentObjects[objType].forEach(function(obj){
if(obj.name == objName) retObj = obj if(obj.name == objName) retObj = obj
}) })
})
return retObj return retObj
} }
function getObjectsName(objType) { function getObjectsName(objType) {
if(currentObjects[objType] == undefined) return [] if(currentObjects[objType] == undefined) return []
return currentObjects[objType].map(function(obj) {return obj.name}) return currentObjects[objType].map(function(obj) {return obj.name})

View file

@ -188,7 +188,6 @@ function simplifyExpression(str) {
[/ ($|\))/g, '$1'], [/ ($|\))/g, '$1'],
] ]
console.log(str)
// Replacements // Replacements
replacements.forEach(function(replacement){ replacements.forEach(function(replacement){
while(replacement[0].test(str)) while(replacement[0].test(str))