Network: Add undo command for node move.

mesh
Pierre-Antoine Rouby 2023-06-09 15:45:16 +02:00
parent a7945eea9a
commit eb6d218b8e
2 changed files with 33 additions and 3 deletions

View File

@ -669,14 +669,15 @@ class GraphWidget(QGraphicsView):
if self._state == "move": if self._state == "move":
self._selected_new_edge_src_node = None self._selected_new_edge_src_node = None
self.mouse_origin_x = pos.x()
self.mouse_origin_y = pos.y()
items = self.items(event.pos()) items = self.items(event.pos())
if items and type(items[0]) == EdgeItem: if items and type(items[0]) == EdgeItem:
edge = items[0] edge = items[0]
if edge: if edge:
self.set_current_edge(edge) self.set_current_edge(edge)
elif items and type(items[0]) == NodeItem:
self._mouse_origin_x = pos.x()
self._mouse_origin_y = pos.y()
self._current_moved_node = items[0]
# Add nodes and edges # Add nodes and edges
elif self._state == "add": elif self._state == "add":
@ -712,6 +713,17 @@ class GraphWidget(QGraphicsView):
def mouseReleaseEvent(self, event): def mouseReleaseEvent(self, event):
self.clicked = False self.clicked = False
if self._state == "move":
pos = self.mapToScene(event.pos())
self._undo.push(
SetNodePosCommand(
self._current_moved_node,
(pos.x(), pos.y()),
(self._mouse_origin_x,
self._mouse_origin_y)
)
)
self.update() self.update()
super(GraphWidget, self).mouseReleaseEvent(event) super(GraphWidget, self).mouseReleaseEvent(event)

View File

@ -3,6 +3,10 @@
from copy import deepcopy from copy import deepcopy
from tools import trace, timer from tools import trace, timer
from PyQt5.QtCore import (
QPointF,
)
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QMessageBox, QUndoCommand, QUndoStack, QMessageBox, QUndoCommand, QUndoStack,
) )
@ -24,6 +28,20 @@ class AddNodeCommand(QUndoCommand):
def redo(self): def redo(self):
self._graph.insert_node(self._node) self._graph.insert_node(self._node)
class SetNodePosCommand(QUndoCommand):
def __init__(self, node, new, old):
QUndoCommand.__init__(self)
self._node = node
self._new = new
self._old = old
def undo(self):
self._node.node.setPos(self._old[0], self._old[1])
def redo(self):
self._node.node.setPos(self._new[0], self._new[1])
class DelNodeCommand(QUndoCommand): class DelNodeCommand(QUndoCommand):
def __init__(self, graph, node): def __init__(self, graph, node):
QUndoCommand.__init__(self) QUndoCommand.__init__(self)