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]) {
var obj = new Objects.types[objType](...objData)
Objects.currentObjects[objType].push(obj)
Objects.currentObjectsByName[obj.name] = obj
}
} else {
error += qsTr("Unknown object type: %1.").arg(objType) + "\n";

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -40,11 +40,15 @@ class NameChanged extends EP.EditedProperty {
}
undo() {
Objects.getObjectByName(this.newValue, this.targetType)['name'] = this.previousValue
Objects.renameObject(this.newValue, this.previousValue)
}
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() {

View file

@ -25,30 +25,37 @@
var types = {}
var currentObjects = {}
var currentObjectsByName = {}
function getObjectByName(objName, objType = null) {
var objectTypes = Object.keys(currentObjects)
if(typeof objType == 'string' && objType != "") {
if(objType == "ExecutableObject") {
objectTypes = getExecutableTypes()
} else if(currentObjects[objType] != undefined) {
objectTypes = [objType]
}
}
if(Array.isArray(objType)) objectTypes = objType
var retObj = null
if(objName != "" && objName != null) {
objectTypes.forEach(function(objType){
if(currentObjects[objType] == undefined) return null
currentObjects[objType].forEach(function(obj){
if(obj.name == objName) retObj = obj
})
})
}
return retObj
function renameObject(oldName, newName) {
/**
* Renames an object from its old name to the new one.
* @param {string} oldName - Current name of the object.
* @param {string} newName - Name to rename the object to.
*/
let obj = currentObjectsByName[oldName]
delete currentObjectsByName[oldName]
currentObjectsByName[newName] = obj
obj.name = newName
}
function deleteObject(objName) {
/**
* Deletes an object by its given name.
* @param {string} objName - Current name of the object.
*/
let obj = currentObjectsByName[objName]
delete currentObjectsByName[objName]
currentObjects[obj.type].splice(currentObjects.indexOf(obj),1)
obj.delete()
}
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") {
var types = getExecutableTypes()
var elementNames = ['']
@ -62,15 +69,26 @@ function getObjectsName(objType) {
}
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())
}
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.
var newobj = new types[objType](...args)
if(Object.keys(currentObjects).indexOf(objType) == -1) {
currentObjects[objType] = []
}
currentObjects[objType].push(newobj)
currentObjectsByName[newobj.name] = newobj
return newobj
}

View file

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

View file

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

View file

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

View file

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

View file

@ -149,6 +149,7 @@ class Helper(QObject):
@Slot()
def fetchChangelog(self):
changelog_cache_path = path.join(path.dirname(path.realpath(__file__)), "CHANGELOG.md")
print(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.
f = open(changelog_cache_path);