file_operations

 1# file_operations.py
 2
 3import json
 4from PySide6.QtWidgets import QFileDialog
 5from Node import Node
 6from Edge import Edge
 7from NodeData import NodeData
 8
 9def save(scene):
10    filename, _ = QFileDialog.getSaveFileName(None, "Save File", "", "JSON Files (*.json)")
11    if filename:
12        data = {
13            "nodes": [],
14            "node_counter": scene.node_counter
15        }
16
17        for item in scene.items():
18            if isinstance(item, Node):
19                node_dict = item.data.to_dict()
20                data["nodes"].append(node_dict)
21
22        with open(filename, 'w') as file:
23            json.dump(data, file, indent=4)
24
25def load(scene):
26    filename, _ = QFileDialog.getOpenFileName(None, "Load File", "", "JSON Files (*.json)")
27    if filename:
28        with open(filename, 'r') as file:
29            data = json.load(file)
30
31            node_map = {}
32
33            # Load the node counter
34            scene.node_counter = data.get("node_counter", 1)
35
36            # First create all nodes
37            for node_data in data["nodes"]:
38                node_data_obj = NodeData.from_dict(node_data)
39                node = Node(node_data_obj)
40                scene.addItem(node)
41                node_map[node_data_obj.uniq_id] = node
42
43            # Collect edges
44            edge_set = set()  # To track created edges and avoid duplicates
45            for node_data in data["nodes"]:
46                source_id = node_data["uniq_id"]
47                for next_id in node_data["nexts"]:
48                    edge_tuple = (source_id, next_id)
49                    edge_set.add(edge_tuple)
50
51            # Clear nexts and prevs to avoid duplicates
52            for node in node_map.values():
53                node.data.nexts.clear()
54                node.data.prevs.clear()
55
56            # Then create edges based on the collected edge_set
57            for source_id, next_id in edge_set:
58                if source_id in node_map and next_id in node_map:
59                    source_node = node_map[source_id]
60                    destination_node = node_map[next_id]
61                    edge = Edge(source_node.output_port)
62                    edge.set_destination(destination_node.input_port)
63                    scene.addItem(edge)
64                    source_node.output_port.edges.append(edge)
65                    destination_node.input_port.edges.append(edge)
66
67            # Ensure edges are properly connected and update their positions
68            for node in node_map.values():
69                for edge in node.input_port.edges + node.output_port.edges:
70                    edge.update_position()
def save(scene):
10def save(scene):
11    filename, _ = QFileDialog.getSaveFileName(None, "Save File", "", "JSON Files (*.json)")
12    if filename:
13        data = {
14            "nodes": [],
15            "node_counter": scene.node_counter
16        }
17
18        for item in scene.items():
19            if isinstance(item, Node):
20                node_dict = item.data.to_dict()
21                data["nodes"].append(node_dict)
22
23        with open(filename, 'w') as file:
24            json.dump(data, file, indent=4)
def load(scene):
26def load(scene):
27    filename, _ = QFileDialog.getOpenFileName(None, "Load File", "", "JSON Files (*.json)")
28    if filename:
29        with open(filename, 'r') as file:
30            data = json.load(file)
31
32            node_map = {}
33
34            # Load the node counter
35            scene.node_counter = data.get("node_counter", 1)
36
37            # First create all nodes
38            for node_data in data["nodes"]:
39                node_data_obj = NodeData.from_dict(node_data)
40                node = Node(node_data_obj)
41                scene.addItem(node)
42                node_map[node_data_obj.uniq_id] = node
43
44            # Collect edges
45            edge_set = set()  # To track created edges and avoid duplicates
46            for node_data in data["nodes"]:
47                source_id = node_data["uniq_id"]
48                for next_id in node_data["nexts"]:
49                    edge_tuple = (source_id, next_id)
50                    edge_set.add(edge_tuple)
51
52            # Clear nexts and prevs to avoid duplicates
53            for node in node_map.values():
54                node.data.nexts.clear()
55                node.data.prevs.clear()
56
57            # Then create edges based on the collected edge_set
58            for source_id, next_id in edge_set:
59                if source_id in node_map and next_id in node_map:
60                    source_node = node_map[source_id]
61                    destination_node = node_map[next_id]
62                    edge = Edge(source_node.output_port)
63                    edge.set_destination(destination_node.input_port)
64                    scene.addItem(edge)
65                    source_node.output_port.edges.append(edge)
66                    destination_node.input_port.edges.append(edge)
67
68            # Ensure edges are properly connected and update their positions
69            for node in node_map.values():
70                for edge in node.input_port.edges + node.output_port.edges:
71                    edge.update_position()