Better handling of Object names in a separate dictionary.

This commit is contained in:
Adsooi 2022-10-17 20:25:29 +02:00
parent 3dc69cc9ba
commit fad5325501
Signed by: Ad5001
GPG key ID: 7251B1AF90B960F9
13 changed files with 84 additions and 49 deletions

View file

@ -255,6 +255,7 @@ ApplicationWindow {
for(var objData of data['objects'][objType]) { for(var objData of data['objects'][objType]) {
var obj = new Objects.types[objType](...objData) var obj = new Objects.types[objType](...objData)
Objects.currentObjects[objType].push(obj) Objects.currentObjects[objType].push(obj)
Objects.currentObjectsByName[obj.name] = obj
} }
} else { } else {
error += qsTr("Unknown object type: %1.").arg(objType) + "\n"; error += qsTr("Unknown object type: %1.").arg(objType) + "\n";

View file

@ -74,29 +74,39 @@ D.Dialog {
width: objEditor.width - 20 width: objEditor.width - 20
spacing: 10 spacing: 10
D.MessageDialog {
id: invalidNameDialog
title: qsTr("LogarithmPlotter - Invalid object name")
text: ""
function showDialog(objectName) {
text = qsTr("An object with the name '%1' already exists.").arg(objectName)
open()
}
}
Setting.TextSetting { Setting.TextSetting {
id: nameProperty id: nameProperty
height: 30 height: 30
label: qsTr("Name") label: qsTr("Name")
icon: "common/label.svg" icon: "common/label.svg"
min: 1
width: dlgProperties.width width: dlgProperties.width
value: objEditor.obj.name value: objEditor.obj.name
onChanged: function(newValue) { onChanged: function(newValue) {
var newName = Utils.parseName(newValue) let newName = Utils.parseName(newValue)
if(newName != '' && objEditor.obj.name != newName) { if(newName != '' && objEditor.obj.name != newName) {
if(Objects.getObjectByName(newName) != null) { if(newName in Objects.currentObjectsByName) {
newName = ObjectsCommons.getNewName(newName) invalidNameDialog.showDialog(newName)
} } else {
history.addToHistory(new HistoryLib.NameChanged( history.addToHistory(new HistoryLib.NameChanged(
objEditor.obj.name, objEditor.objType, newName objEditor.obj.name, objEditor.objType, newName
)) ))
Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = newName Objects.renameObject(obj.name, newName)
objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex] objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex]
objectListList.update() objectListList.update()
} }
} }
} }
}
Setting.ComboBoxSetting { Setting.ComboBoxSetting {
id: labelContentProperty id: labelContentProperty
@ -213,7 +223,7 @@ D.Dialog {
[]) : []) :
modelData[1].values) modelData[1].values)
: [] : []
// Translated verison of the model. // Translated version of the model.
model: selectObjMode ? baseModel : modelData[1].translatedValues model: selectObjMode ? baseModel : modelData[1].translatedValues
visible: paramTypeIn(modelData[1], ['ObjectType', 'Enum']) visible: paramTypeIn(modelData[1], ['ObjectType', 'Enum'])
currentIndex: baseModel.indexOf(selectObjMode ? objEditor.obj[modelData[0]].name : objEditor.obj[modelData[0]]) currentIndex: baseModel.indexOf(selectObjMode ? objEditor.obj[modelData[0]].name : objEditor.obj[modelData[0]])
@ -222,7 +232,7 @@ D.Dialog {
if(selectObjMode) { if(selectObjMode) {
// This is only done when what we're selecting are Objects. // This is only done when what we're selecting are Objects.
// Setting object property. // Setting object property.
var selectedObj = Objects.getObjectByName(baseModel[newIndex], modelData[1].objType) var selectedObj = Objects.currentObjectsByName[baseModel[newIndex]]
if(newIndex != 0) { if(newIndex != 0) {
// Make sure we don't set the object to null. // Make sure we don't set the object to null.
if(selectedObj == null) { if(selectedObj == null) {

View file

@ -193,8 +193,7 @@ ScrollView {
history.addToHistory(new HistoryLib.DeleteObject( history.addToHistory(new HistoryLib.DeleteObject(
obj.name, objType, obj.export() obj.name, objType, obj.export()
)) ))
Objects.currentObjects[objType][index].delete() Objects.deleteObject(obj.name)
Objects.currentObjects[objType].splice(index, 1)
objectListList.update() objectListList.update()
} }
} }

View file

@ -98,7 +98,7 @@ Item {
'Expression': () => new MathLib.Expression(newValue), 'Expression': () => new MathLib.Expression(newValue),
'number': () => parseFloat(newValue) 'number': () => parseFloat(newValue)
}[Objects.types[objType].properties()[propertyX]]() }[Objects.types[objType].properties()[propertyX]]()
let obj = Objects.getObjectByName(objName, objType) let obj = Objects.currentObjectsByName[objName] // getObjectByName(objName, objType)
history.addToHistory(new HistoryLib.EditedProperty( history.addToHistory(new HistoryLib.EditedProperty(
objName, objType, propertyX, obj[propertyX], newValue objName, objType, propertyX, obj[propertyX], newValue
)) ))
@ -112,7 +112,7 @@ Item {
'Expression': () => new MathLib.Expression(newValue), 'Expression': () => new MathLib.Expression(newValue),
'number': () => parseFloat(newValue) 'number': () => parseFloat(newValue)
}[Objects.types[objType].properties()[propertyY]]() }[Objects.types[objType].properties()[propertyY]]()
let obj = Objects.getObjectByName(objName, objType) let obj = Objects.currentObjectsByName[objName] // Objects.getObjectByName(objName, objType)
history.addToHistory(new HistoryLib.EditedProperty( history.addToHistory(new HistoryLib.EditedProperty(
objName, objType, propertyY, obj[propertyY], newValue objName, objType, propertyY, obj[propertyY], newValue
)) ))

View file

@ -36,9 +36,11 @@ class CreateNewObject extends C.Action {
undo() { undo() {
var targetIndex = Objects.getObjectsName(this.targetType).indexOf(this.targetName) Objects.deleteObject(this.targetName)
Objects.currentObjects[this.targetType][targetIndex].delete() //let targetIndex = Objects.getObjectsName(this.targetType).indexOf(this.targetName)
Objects.currentObjects[this.targetType].splice(targetIndex, 1) //delete Objects.currentObjectsByName[this.targetName]
//Objects.currentObjects[this.targetType][targetIndex].delete()
//Objects.currentObjects[this.targetType].splice(targetIndex, 1)
} }
redo() { redo() {

View file

@ -49,19 +49,19 @@ class EditedProperty extends C.Action {
this.newValue = MathLib.parseDomain(this.newValue); this.newValue = MathLib.parseDomain(this.newValue);
} else { } else {
// Objects // Objects
this.previousValue = Objects.getObjectByName(this.previousValue); this.previousValue = Objects.currentObjectsByName[this.previousValue] // Objects.getObjectByName(this.previousValue);
this.newValue = Objects.getObjectByName(this.newValue); this.newValue = Objects.currentObjectsByName[this.newValue] // Objects.getObjectByName(this.newValue);
} }
} }
this.setReadableValues() this.setReadableValues()
} }
undo() { undo() {
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.previousValue Objects.currentObjectsByName[this.targetName][this.targetProperty] = this.previousValue
} }
redo() { redo() {
Objects.getObjectByName(this.targetName, this.targetType)[this.targetProperty] = this.newValue Objects.currentObjectsByName[this.targetName][this.targetProperty] = this.newValue
} }
export() { export() {

View file

@ -40,11 +40,15 @@ class NameChanged extends EP.EditedProperty {
} }
undo() { undo() {
Objects.getObjectByName(this.newValue, this.targetType)['name'] = this.previousValue Objects.renameObject(this.newValue, this.previousValue)
} }
redo() { redo() {
Objects.getObjectByName(this.previousValue, this.targetType)['name'] = this.newValue Objects.renameObject(this.previousValue, this.newValue)
//let obj = Objects.currentObjectsByName[this.previousValue]
//obj.name = this.newValue
//Objects.currentObjectsByName[this.newValue] = obj
//delete Objects.currentObjectsByName[this.previousValue]
} }
getReadableString() { getReadableString() {

View file

@ -25,30 +25,37 @@
var types = {} var types = {}
var currentObjects = {} var currentObjects = {}
var currentObjectsByName = {}
function getObjectByName(objName, objType = null) { function renameObject(oldName, newName) {
var objectTypes = Object.keys(currentObjects) /**
if(typeof objType == 'string' && objType != "") { * Renames an object from its old name to the new one.
if(objType == "ExecutableObject") { * @param {string} oldName - Current name of the object.
objectTypes = getExecutableTypes() * @param {string} newName - Name to rename the object to.
} else if(currentObjects[objType] != undefined) { */
objectTypes = [objType] let obj = currentObjectsByName[oldName]
} delete currentObjectsByName[oldName]
} currentObjectsByName[newName] = obj
if(Array.isArray(objType)) objectTypes = objType obj.name = newName
var retObj = null }
if(objName != "" && objName != null) {
objectTypes.forEach(function(objType){ function deleteObject(objName) {
if(currentObjects[objType] == undefined) return null /**
currentObjects[objType].forEach(function(obj){ * Deletes an object by its given name.
if(obj.name == objName) retObj = obj * @param {string} objName - Current name of the object.
}) */
}) let obj = currentObjectsByName[objName]
} delete currentObjectsByName[objName]
return retObj currentObjects[obj.type].splice(currentObjects.indexOf(obj),1)
obj.delete()
} }
function getObjectsName(objType) { function getObjectsName(objType) {
/**
* Gets a list of all names of a certain object type.
* @param {string} objType - Type of the object to query. Can be ExecutableObject for all ExecutableObjects.
* @return {array} List of names of the objects.
*/
if(objType == "ExecutableObject") { if(objType == "ExecutableObject") {
var types = getExecutableTypes() var types = getExecutableTypes()
var elementNames = [''] var elementNames = ['']
@ -62,15 +69,26 @@ function getObjectsName(objType) {
} }
function getExecutableTypes() { function getExecutableTypes() {
/**
* Returns a list of all object types which are executable objects.
* @return {array} List of all object types which are executable objects.
*/
return Object.keys(currentObjects).filter(objType => types[objType].executable()) return Object.keys(currentObjects).filter(objType => types[objType].executable())
} }
function createNewRegisteredObject(objType, args=[]) { function createNewRegisteredObject(objType, args=[]) {
/**
* Creates and register an object in the database.
* @param {string} objType - Type of the object to create.
* @param {string} args - List of arguments for the objects (can be empty).
* @return {[objType]} Newly created object.
*/
if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist. if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist.
var newobj = new types[objType](...args) var newobj = new types[objType](...args)
if(Object.keys(currentObjects).indexOf(objType) == -1) { if(Object.keys(currentObjects).indexOf(objType) == -1) {
currentObjects[objType] = [] currentObjects[objType] = []
} }
currentObjects[objType].push(newobj) currentObjects[objType].push(newobj)
currentObjectsByName[newobj.name] = newobj
return newobj return newobj
} }

View file

@ -40,7 +40,7 @@ function getNewName(allowedLetters) {
var num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length) var num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length)
ret = letter + (num > 0 ? Utils.textsub(num-1) : '') ret = letter + (num > 0 ? Utils.textsub(num-1) : '')
newid += 1 newid += 1
} while(Objects.getObjectByName(ret) != null) } while(ret in Objects.currentObjectsByName)
return ret return ret
} }

View file

@ -49,7 +49,7 @@ class GainBode extends Common.ExecutableObject {
this.type = 'Gain Bode' this.type = 'Gain Bode'
if(typeof om_0 == "string") { if(typeof om_0 == "string") {
// Point name or create one // Point name or create one
om_0 = Objects.getObjectByName(om_0, 'Point') om_0 = Objects.currentObjectsByName[om_0]
if(om_0 == null) { if(om_0 == null) {
// Create new point // Create new point
om_0 = Objects.createNewRegisteredObject('Point') om_0 = Objects.createNewRegisteredObject('Point')

View file

@ -48,7 +48,7 @@ class PhaseBode extends Common.ExecutableObject {
this.phase = phase this.phase = phase
if(typeof om_0 == "string") { if(typeof om_0 == "string") {
// Point name or create one // Point name or create one
om_0 = Objects.getObjectByName(om_0, 'Point') om_0 = Objects.currentObjectsByName[om_0]
if(om_0 == null) { if(om_0 == null) {
// Create new point // Create new point
om_0 = Objects.createNewRegisteredObject('Point') om_0 = Objects.createNewRegisteredObject('Point')

View file

@ -56,7 +56,7 @@ class XCursor extends Common.DrawableObject {
this.x = x this.x = x
this.targetElement = targetElement this.targetElement = targetElement
if(typeof targetElement == "string") { if(typeof targetElement == "string") {
this.targetElement = Objects.getObjectByName(targetElement, elementTypes) this.targetElement = Objects.currentObjectsByName[targetElement]
} }
this.labelPosition = labelPosition this.labelPosition = labelPosition
this.displayStyle = displayStyle this.displayStyle = displayStyle

View file

@ -149,6 +149,7 @@ class Helper(QObject):
@Slot() @Slot()
def fetchChangelog(self): def fetchChangelog(self):
changelog_cache_path = path.join(path.dirname(path.realpath(__file__)), "CHANGELOG.md") changelog_cache_path = path.join(path.dirname(path.realpath(__file__)), "CHANGELOG.md")
print(changelog_cache_path)
if path.exists(changelog_cache_path): if path.exists(changelog_cache_path):
# We have a cached version of the changelog, for env that don't have access to the internet. # We have a cached version of the changelog, for env that don't have access to the internet.
f = open(changelog_cache_path); f = open(changelog_cache_path);