Skip to content

Commit 591c04e

Browse files
committed
[core/ui] node rename : Add documentation + improve node rename & fix small ui issue
1 parent 56d8033 commit 591c04e

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

meshroom/common/core.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,18 @@ def add(self, obj):
5151
assert key not in self._objects
5252
self._objects[key] = obj
5353

54-
def rename(self, oldKey, newKey):
54+
def rename(self, oldKey: str, newKey: str):
55+
""" Rename an element in the dict model
56+
57+
Args:
58+
oldKey (str): Previous key name of the element to replace.
59+
newKey (str): New key name to insert in the model.
60+
61+
Raises:
62+
KeyError: if the new name is already used.
63+
"""
64+
if newKey in self._objects.keys():
65+
raise KeyError(f"Key {newKey} is already in use in {self}")
5566
obj = self._objects[oldKey]
5667
self._objects[newKey] = obj
5768
del self._objects[oldKey]

meshroom/common/qt.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,18 @@ def replace(self, i, obj):
167167
self._objects[i] = obj
168168
self.dataChanged.emit(self.index(i), self.index(i), [])
169169

170-
def rename(self, oldKey, newKey):
170+
def rename(self, oldKey: str, newKey: str):
171+
""" Rename an element in the model
172+
173+
Args:
174+
oldKey (str): Previous key name of the element to replace.
175+
newKey (str): New key name to insert in the model.
176+
177+
Raises:
178+
KeyError: if the new name is already used.
179+
"""
180+
if newKey in self._objectByKey.keys():
181+
raise KeyError(f"Key {newKey} is already in use in {self}")
171182
obj = self._objectByKey[oldKey]
172183
index = self.indexOf(obj)
173184
self._objectByKey[newKey] = obj

meshroom/core/graph.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def addNode(self, node, uniqueName=None):
514514
return node
515515

516516
def renameNode(self, node: Node, newName: str):
517+
""" Rename a node in the Node Graph
518+
519+
Args:
520+
node (Node): Node to rename.
521+
newName (str): New name of the node. Must be unique in the graph.
522+
"""
517523
self._nodes.rename(node._name, newName)
518524
# Finally rename
519525
node._name = newName

meshroom/ui/commands.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,20 @@ def undoImpl(self):
153153

154154
class RenameNodeCommand(GraphCommand):
155155
def __init__(self, graph, node, name, parent=None):
156+
""" Command to rename a node. The new name should not be used yet.
157+
"""
156158
super().__init__(graph, parent)
157159
self.node = node
158160
self.oldName = node._name
159161
self.name = name
160162

161163
def redoImpl(self):
162-
newName = self.graph._createUniqueNodeName(self.name, [n._name for n in self.graph._nodes])
164+
newName = self.name
165+
usedNames = {n._name for n in self.graph._nodes}
166+
if newName in usedNames:
167+
newName = self.graph._createUniqueNodeName(self.name, usedNames)
163168
self.setText(f"Rename Node {self.oldName} to {newName}")
164-
self.graph.renameNode(self.node, newName)
169+
self.graph.renameNode(self.node, self.name)
165170
return newName
166171

167172
def undoImpl(self):

meshroom/ui/graph.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"""

meshroom/ui/qml/GraphEditor/NodeEditor.qml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Panel {
8585
function validateNodeNameChange(name) {
8686
if (root.node && name.trim() !== "") {
8787
const newNodeName = _reconstruction.renameNode(_reconstruction.selectedNode, name.trim())
88-
if (newNodeName == "") {
88+
if (newNodeName === "") {
8989
root.displayNodeName = root.nodeName
9090
root.validatedNodeName = root.nodeName
9191
} else {
@@ -95,6 +95,7 @@ Panel {
9595
}
9696
}
9797
function cancelNodeNameChange() {
98+
// HACK: Set to an empty string to force the text to be set to the previous value.
9899
root.displayNodeName = ""
99100
root.displayNodeName = root.validatedNodeName
100101
}
@@ -201,7 +202,7 @@ Panel {
201202
// Show node type if the node name does not start with "nodeType_"
202203
Label {
203204
text: "(" + root.displayNodeType + ")"
204-
visible: root.displayNodeType !== ""
205+
visible: root.displayNodeType !== "" && _reconstruction.selectedNode
205206
topPadding: 4
206207
bottomPadding: 4
207208
}

0 commit comments

Comments
 (0)