@@ -936,13 +936,32 @@ def addNewNode(self, nodeType, position=None, **kwargs):
936936 return self .push (commands .AddNodeCommand (self ._graph , nodeType , position = position , ** kwargs ))
937937
938938 @Slot (Node , str , result = str )
939- def renameNode (self , node , newName ):
939+ def renameNode (self , node : Node , newName : str ):
940+ """ Triggers the node renaming.
941+
942+ The name we send here doesn't need to be unique here because we will ensure this later in
943+ the RenameNodeCommand. In this function the last `_N` index is removed, then all special characters
944+ (everything except letters and numbers) are removed. Then a `_N` index will be added to
945+ ensure the node name unicity.
946+
947+ As we remove all special characters, only one underscore is allowed in the node name.
948+ Label can be used if multiple underscores are needed.
949+
950+ Args:
951+ node (Node): Node to rename.
952+ newName (str): New name to set.
953+
954+ Returns:
955+ str: The final name of the node.
956+ """
940957 newName = "_" .join (newName .split ("_" )[:- 1 ]) if "_" in newName else newName
941958 # Eliminate all characters except digits and letters
942959 newName = re .sub (r"[^0-9a-zA-Z]" , "" , newName )
943- if not newName :
960+ # Create unique name
961+ uniqueName = self ._graph ._createUniqueNodeName (newName , {n ._name for n in self ._graph ._nodes if n != node })
962+ if not newName or uniqueName == node ._name :
944963 return ""
945- return self .push (commands .RenameNodeCommand (self ._graph , node , newName ))
964+ return self .push (commands .RenameNodeCommand (self ._graph , node , uniqueName ))
946965
947966 def moveNode (self , node : Node , position : Position ):
948967 """
0 commit comments