Commit 520d8f73 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

Network: Add delete node undo command.

Showing with 34 additions and 13 deletions
+34 -13
......@@ -101,7 +101,7 @@ class Graph(object):
return self._add_node(node)
def insert_node(self, node):
self._add_node(node)
return self._add_node(node)
def create_node(self, x:float = 0.0, y:float = 0.0):
node = self._create_node(x, y)
......@@ -126,6 +126,9 @@ class Graph(object):
status = self._status)
return self._add_edge(edge)
def insert_edge(self, edge):
return self._add_edge(edge)
def remove_node(self, node_name:str):
self._nodes = list(
filter(
......
......@@ -400,21 +400,12 @@ class GraphWidget(QGraphicsView):
Nothing
"""
node = item.node
edges = list(
filter(
lambda ie: (ie.edge.node1 == node or
ie.edge.node2 == node),
self.edge_items
self._undo.push(
DelNodeCommand(
self.graph, node
)
)
self.set_selected_item(None)
for edge in edges:
self.graph.remove_edge(edge.edge.name)
self.graph.remove_node(node.name)
self.scene().clear()
self.create_items()
self.changeNode.emit(self.sender())
......
......@@ -24,6 +24,33 @@ class AddNodeCommand(QUndoCommand):
def redo(self):
self._graph.insert_node(self._node)
class DelNodeCommand(QUndoCommand):
def __init__(self, graph, node):
QUndoCommand.__init__(self)
self._graph = graph
self._node = node
self._edges = []
def undo(self):
for edge in self._edges:
self._graph.insert_edge(edge)
self._graph.insert_node(self._node)
def redo(self):
self._edges = list(
filter(
lambda e: (e.node1 == self._node or
e.node2 == self._node),
self._graph.edges()
)
)
for edge in self._edges:
self._graph.remove_edge(edge.name)
self._graph.remove_node(self._node.name)
class SetCommand(QUndoCommand):
def __init__(self, element, column, new_value):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment