[ui] return command result after pushing it to the undo stack

this allows to directly retrieve, for instance, the node created by UIGraph.addNode method
This commit is contained in:
Yann Lanthony 2018-01-10 14:12:48 +01:00
parent 22c54bd1c5
commit 4ea793be74
2 changed files with 19 additions and 8 deletions

View file

@ -57,7 +57,7 @@ class UndoStack(QUndoStack):
except Exception as e: except Exception as e:
logging.error("Error while trying command '{}': \n{}".format(command.text(), traceback.format_exc())) logging.error("Error while trying command '{}': \n{}".format(command.text(), traceback.format_exc()))
res = False res = False
if res: if res is not False:
command.setEnabled(False) command.setEnabled(False)
self.push(command) # takes ownership self.push(command) # takes ownership
command.setEnabled(True) command.setEnabled(True)
@ -84,15 +84,17 @@ class GraphCommand(UndoCommand):
class AddNodeCommand(GraphCommand): class AddNodeCommand(GraphCommand):
def __init__(self, graph, nodeType, parent=None): def __init__(self, graph, nodeType, parent=None, **kwargs):
super(AddNodeCommand, self).__init__(graph, parent) super(AddNodeCommand, self).__init__(graph, parent)
self.nodeType = nodeType self.nodeType = nodeType
self.nodeName = None self.nodeName = None
self.kwargs = kwargs
def redoImpl(self): def redoImpl(self):
self.nodeName = self.graph.addNewNode(self.nodeType).name node = self.graph.addNewNode(self.nodeType, **self.kwargs)
self.nodeName = node.name
self.setText("Add Node {}".format(self.nodeName)) self.setText("Add Node {}".format(self.nodeName))
return True return node
def undoImpl(self): def undoImpl(self):
self.graph.removeNode(self.nodeName) self.graph.removeNode(self.nodeName)

View file

@ -210,7 +210,7 @@ class UIGraph(QObject):
Args: Args:
command (commands.UndoCommand): the command to push command (commands.UndoCommand): the command to push
""" """
self._undoStack.tryAndPush(command) return self._undoStack.tryAndPush(command)
def groupedGraphModification(self, title): def groupedGraphModification(self, title):
""" Get a GroupedGraphModification for this Reconstruction. """ Get a GroupedGraphModification for this Reconstruction.
@ -223,9 +223,18 @@ class UIGraph(QObject):
""" """
return commands.GroupedGraphModification(self._graph, self._undoStack, title) return commands.GroupedGraphModification(self._graph, self._undoStack, title)
@Slot(str) @Slot(str, result=QObject)
def addNode(self, nodeType): def addNode(self, nodeType, **kwargs):
self.push(commands.AddNodeCommand(self._graph, nodeType)) """ [Undoable]
Create a new Node of type 'nodeType' and returns it.
Args:
nodeType (str): the type of the Node to create.
**kwargs: optional node attributes values
Returns:
Node: the created node
"""
return self.push(commands.AddNodeCommand(self._graph, nodeType, **kwargs))
@Slot(graph.Node) @Slot(graph.Node)
def removeNode(self, node): def removeNode(self, node):