[commands] new AddEdge/RemoveEdge commands

This commit is contained in:
Yann Lanthony 2017-10-13 10:53:49 +02:00
parent e4ccef2187
commit d4509ec20e
2 changed files with 60 additions and 11 deletions

View file

@ -132,3 +132,38 @@ class SetAttributeCommand(GraphCommand):
def undoImpl(self):
self.graph.node(self.nodeName).attribute(self.attrName).value = self.oldValue
class AddEdgeCommand(GraphCommand):
def __init__(self, graph, src, dst, parent=None):
super(AddEdgeCommand, self).__init__(graph, parent)
self.srcNode, self.srcAttr = src.fullName().split(".")
self.dstNode, self.dstAttr = dst.fullName().split(".")
self.setText("Connect '{}'->'{}'".format(src.fullName(), dst.fullName()))
def redoImpl(self):
try:
self.graph.addEdge(self.graph.node(self.srcNode).attribute(self.srcAttr),
self.graph.node(self.dstNode).attribute(self.dstAttr))
except RuntimeError:
return False
return True
def undoImpl(self):
self.graph.removeEdge(self.graph.node(self.dstNode).attribute(self.dstAttr))
class RemoveEdgeCommand(GraphCommand):
def __init__(self, graph, edge, parent=None):
super(RemoveEdgeCommand, self).__init__(graph, parent)
self.srcNode, self.srcAttr = edge.src.fullName().split(".")
self.dstNode, self.dstAttr = edge.dst.fullName().split(".")
self.setText("Disconnect '{}'->'{}'".format(edge.src.fullName(), edge.dst.fullName()))
def redoImpl(self):
self.graph.removeEdge(self.graph.node(self.dstNode).attribute(self.dstAttr))
return True
def undoImpl(self):
self.graph.addEdge(self.graph.node(self.srcNode).attribute(self.srcAttr),
self.graph.node(self.dstNode).attribute(self.dstAttr))