-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.py
More file actions
58 lines (50 loc) · 1.98 KB
/
actions.py
File metadata and controls
58 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from node import Node
from networkx.readwrite import json_graph
import copy
class Action:
def execute(self, editor):
pass # Baseclass for actions
class NoOpAction(Action):
def execute(self, editor):
print("No operation action executed.")
pass # No operation action
class AddNodeAction(Action):
def execute(self, editor):
editor._sync_node_names_to_graph()
editor.undo_stack.push(copy.deepcopy(editor.nx_graph))
center_x = editor.screen.get_width() // 2
center_y = editor.screen.get_height() // 2
world_x = (center_x + editor.panning_state.offset_x * editor.zoom) / editor.zoom
world_y = (center_y + editor.panning_state.offset_y * editor.zoom) / editor.zoom
node = Node(world_x, world_y, editor.next_node_id)
editor.nodes.append(node)
editor.nx_graph.add_node(editor.next_node_id,name=node.node_name, pos=(world_x, world_y))
editor.next_node_id += 1
class DeleteAllAction(Action):
def execute(self, editor):
# Clear the undo stack so clear all cannot be undone
editor.undo_stack.clear()
editor.nodes.clear()
editor.connections.clear()
editor.nx_graph.clear()
editor.next_node_id = 1
editor.selected_node = None
class DumpGraphAction(Action):
def execute(self, editor):
print("=== Current Graph Model ===")
from pprint import pprint
pprint(json_graph.node_link_data(editor.nx_graph, edges="edges"))
print("=== Undo Stack (most recent last) ===")
for i, g in enumerate(editor.undo_stack.stack):
print(f"UndoStack[{i}]: {g}")
pprint(json_graph.node_link_data(g, edges="edges"))
print("======================")
class UndoAction(Action):
def execute(self, editor):
editor.undo()
class SaveGraphAction(Action):
def execute(self, editor):
editor.save_graph()
class LoadGraphAction(Action):
def execute(self, editor):
editor.load_graph()