From bc209f78f6a0e96c60b8263831c7106927576702 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr> Date: Thu, 8 Jun 2023 16:39:08 +0200 Subject: [PATCH] Network: Add add node command. --- src/Model/Network/Graph.py | 26 ++++++++++++++++++-------- src/View/Network/GraphWidget.py | 7 ++++++- src/View/Network/UndoCommand.py | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/Model/Network/Graph.py b/src/Model/Network/Graph.py index f6871abd..9ad25fe8 100644 --- a/src/Model/Network/Graph.py +++ b/src/Model/Network/Graph.py @@ -81,22 +81,32 @@ class Graph(object): return edge[0] - def _add_node(self, node): - self._nodes.append(node) - self._nodes_ids += 1 - - self._status.modified() - return node - - def add_node(self, x:float = 0.0, y:float = 0.0): + def _create_node(self, x:float, y:float): node = self._node_ctor( self._nodes_ids, f"Node {self._nodes_ids}", x = x, y = y, status = self._status ) + self._nodes_ids += 1 + return node + + def _add_node(self, node): + self._nodes.append(node) + self._status.modified() + return node + + def add_node(self, x:float = 0.0, y:float = 0.0): + node = self._create_node(x, y) return self._add_node(node) + def insert_node(self, node): + self._add_node(node) + + def create_node(self, x:float = 0.0, y:float = 0.0): + node = self._create_node(x, y) + return node + def _add_edge(self, edge): # This edge already exists ? if any(filter(lambda e: (e.node1 == edge.node1 and diff --git a/src/View/Network/GraphWidget.py b/src/View/Network/GraphWidget.py index 8cdcff55..573ac3d7 100644 --- a/src/View/Network/GraphWidget.py +++ b/src/View/Network/GraphWidget.py @@ -376,7 +376,12 @@ class GraphWidget(QGraphicsView): Returns: Nothing """ - node = self.graph.add_node(pos.x(), pos.y()) + node = self.graph.create_node(pos.x(), pos.y()) + self._undo.push( + AddNodeCommand( + self.graph, node + ) + ) inode = NodeItem(node, self) self.scene().addItem(inode) self.node_items.append(inode) diff --git a/src/View/Network/UndoCommand.py b/src/View/Network/UndoCommand.py index f845ed81..3266c1cf 100644 --- a/src/View/Network/UndoCommand.py +++ b/src/View/Network/UndoCommand.py @@ -11,6 +11,20 @@ from Model.Network.Node import Node from Model.Network.Edge import Edge from Model.Network.Graph import Graph +class AddNodeCommand(QUndoCommand): + def __init__(self, graph, node): + QUndoCommand.__init__(self) + + self._graph = graph + self._node = node + + def undo(self): + self._graph.remove_node(self._node.name) + + def redo(self): + self._graph.insert_node(self._node) + + class SetCommand(QUndoCommand): def __init__(self, element, column, new_value): QUndoCommand.__init__(self) -- GitLab