| 62 | super().keyPressEvent(event) |
| 63 | |
| 64 | def show_context_menu(self, event: QContextMenuEvent): |
| 65 | scene_pos = self.mapToScene(event.pos()) |
| 66 | item_at_pos = self.scene().itemAt(scene_pos, self.transform()) |
| 67 | |
| 68 | # Find the top-level item if we clicked on a child item (using duck typing) |
| 69 | node = None |
| 70 | group = None |
| 71 | if item_at_pos: |
| 72 | current_item = item_at_pos |
| 73 | while current_item: |
| 74 | if type(current_item).__name__ in ['Node', 'RerouteNode']: |
| 75 | node = current_item |
| 76 | break |
| 77 | elif type(current_item).__name__ == 'Group': |
| 78 | group = current_item |
| 79 | break |
| 80 | current_item = current_item.parentItem() |
| 81 | |
| 82 | menu = QMenu(self) |
| 83 | |
| 84 | # Get selected items for group operations (using duck typing) |
| 85 | selected_items = [item for item in self.scene().selectedItems() if type(item).__name__ in ['Node', 'RerouteNode']] |
| 86 | |
| 87 | if group: |
| 88 | # Context menu for a group |
| 89 | properties_action = menu.addAction("Group Properties") |
| 90 | |
| 91 | action = menu.exec(event.globalPos()) |
| 92 | if action == properties_action: |
| 93 | group.show_properties_dialog() |
| 94 | |
| 95 | elif node: |
| 96 | # Context menu for a node |
| 97 | properties_action = menu.addAction("Properties") |
| 98 | |
| 99 | # Add group option if multiple nodes are selected |
| 100 | group_action = None |
| 101 | if len(selected_items) >= 2: |
| 102 | group_action = menu.addAction("Group Selected") |
| 103 | # Basic validation - ensure we have valid nodes |
| 104 | if not self._can_group_nodes(selected_items): |
| 105 | group_action.setEnabled(False) |
| 106 | |
| 107 | action = menu.exec(event.globalPos()) |
| 108 | if action == properties_action: |
| 109 | node.show_properties_dialog() |
| 110 | elif action == group_action and group_action: |
| 111 | self._create_group_from_selection(selected_items) |
| 112 | else: |
| 113 | # Context menu for empty space |
| 114 | add_node_action = menu.addAction("Add Node") |
| 115 | |
| 116 | # Add group option if multiple nodes are selected |
| 117 | group_action = None |
| 118 | if len(selected_items) >= 2: |
| 119 | group_action = menu.addAction("Group Selected") |
| 120 | # Basic validation - ensure we have valid nodes |
| 121 | if not self._can_group_nodes(selected_items): |