Copies selected nodes, their connections, and groups to the clipboard.
(self)
| 215 | self.execute_command(command) |
| 216 | |
| 217 | def copy_selected(self): |
| 218 | """Copies selected nodes, their connections, and groups to the clipboard.""" |
| 219 | selected_nodes = [item for item in self.selectedItems() if isinstance(item, (Node, RerouteNode))] |
| 220 | selected_groups = [item for item in self.selectedItems() if type(item).__name__ == 'Group'] |
| 221 | |
| 222 | if not selected_nodes and not selected_groups: |
| 223 | return {"requirements": [], "nodes": [], "groups": [], "connections": []} |
| 224 | |
| 225 | nodes_data = [node.serialize() for node in selected_nodes] |
| 226 | groups_data = [group.serialize() for group in selected_groups] |
| 227 | connections_data = [] |
| 228 | selected_node_uuids = {node.uuid for node in selected_nodes} |
| 229 | |
| 230 | for conn in self.connections: |
| 231 | if hasattr(conn.start_pin.node, "uuid") and hasattr(conn.end_pin.node, "uuid") and conn.start_pin.node.uuid in selected_node_uuids and conn.end_pin.node.uuid in selected_node_uuids: |
| 232 | connections_data.append(conn.serialize()) |
| 233 | |
| 234 | # Get requirements from main window if available |
| 235 | requirements = [] |
| 236 | views = self.views() |
| 237 | if views: |
| 238 | main_window = views[0].window() |
| 239 | requirements = main_window.current_requirements if hasattr(main_window, "current_requirements") else [] |
| 240 | |
| 241 | clipboard_data = { |
| 242 | "requirements": requirements, |
| 243 | "nodes": nodes_data, |
| 244 | "groups": groups_data, |
| 245 | "connections": connections_data |
| 246 | } |
| 247 | |
| 248 | # Convert to markdown format for clipboard |
| 249 | try: |
| 250 | from data.flow_format import FlowFormatHandler |
| 251 | handler = FlowFormatHandler() |
| 252 | clipboard_markdown = handler.data_to_markdown(clipboard_data, "Clipboard Content", "Copied nodes and groups from PyFlowGraph") |
| 253 | QApplication.clipboard().setText(clipboard_markdown) |
| 254 | except ImportError: |
| 255 | # Fallback to JSON format if FlowFormatHandler is not available (e.g., during testing) |
| 256 | import json |
| 257 | QApplication.clipboard().setText(json.dumps(clipboard_data, indent=2)) |
| 258 | |
| 259 | total_items = len(nodes_data) + len(groups_data) |
| 260 | print(f"Copied {len(nodes_data)} nodes and {len(groups_data)} groups ({total_items} items total) to clipboard as markdown.") |
| 261 | |
| 262 | return clipboard_data |
| 263 | |
| 264 | def paste(self, mouse_position=None): |
| 265 | """Pastes nodes and connections from clipboard at mouse position or viewport center.""" |