diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogarithmPlotter.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogarithmPlotter.qml index 3a53be3..9b69b47 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogarithmPlotter.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogarithmPlotter.qml @@ -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"; diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/EditorDialog.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/EditorDialog.qml index e4721ed..9cf4a12 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/EditorDialog.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/EditorDialog.qml @@ -73,27 +73,37 @@ D.Dialog { anchors.top: dlgTitle.bottom 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.renameObject(obj.name, newName) + objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex] + objectListList.update() } - history.addToHistory(new HistoryLib.NameChanged( - objEditor.obj.name, objEditor.objType, newName - )) - Objects.currentObjects[objEditor.objType][objEditor.objIndex].name = newName - objEditor.obj = Objects.currentObjects[objEditor.objType][objEditor.objIndex] - objectListList.update() } } } @@ -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) { diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectLists.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectLists.qml index d19ddaa..945c200 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectLists.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectLists.qml @@ -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() } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml index 6c4ddec..8ef80d6 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml @@ -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 )) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/create.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/create.js index 2c3fe18..00af298 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/create.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/create.js @@ -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() { diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.js index 31c21df..140a54b 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.js @@ -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() { diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/name.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/name.js index 015895f..0ec790d 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/name.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/name.js @@ -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() { diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.js index e27562d..b2a5b15 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objects.js @@ -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 } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.js index 3864b2f..7af97f7 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/common.js @@ -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 } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.js index ff8236c..a95f53e 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/gainbode.js @@ -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') diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.js index e3d7be0..63b01a6 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/phasebode.js @@ -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') diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.js b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.js index 52c989f..0199b6a 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.js +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/objs/xcursor.js @@ -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 diff --git a/LogarithmPlotter/util/helper.py b/LogarithmPlotter/util/helper.py index 4f2a7da..47e3992 100644 --- a/LogarithmPlotter/util/helper.py +++ b/LogarithmPlotter/util/helper.py @@ -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);