mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
Network: Add undo command for node move.
This commit is contained in:
1 parent
a7945eea9a
commit
eb6d218b8e
2 files changed
+33
-3
No files matched your search
@@ -669,14 +669,15 @@ class GraphWidget(QGraphicsView):
|
||||
if self._state == "move":
|
||||
self._selected_new_edge_src_node = None
|
||||
|
||||
self.mouse_origin_x = pos.x()
|
||||
self.mouse_origin_y = pos.y()
|
||||
|
||||
items = self.items(event.pos())
|
||||
if items and type(items[0]) == EdgeItem:
|
||||
edge = items[0]
|
||||
if 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
|
||||
elif self._state == "add":
|
||||
@@ -712,6 +713,17 @@ class GraphWidget(QGraphicsView):
|
||||
def mouseReleaseEvent(self, event):
|
||||
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()
|
||||
super(GraphWidget, self).mouseReleaseEvent(event)
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
from copy import deepcopy
|
||||
from tools import trace, timer
|
||||
|
||||
from PyQt5.QtCore import (
|
||||
QPointF,
|
||||
)
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QMessageBox, QUndoCommand, QUndoStack,
|
||||
)
|
||||
@@ -24,6 +28,20 @@ class AddNodeCommand(QUndoCommand):
|
||||
def redo(self):
|
||||
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):
|
||||
def __init__(self, graph, node):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
Reference in new issue
Block a user