Command for moving nodes with position tracking.
| 614 | |
| 615 | |
| 616 | class MoveNodeCommand(CommandBase): |
| 617 | """Command for moving nodes with position tracking.""" |
| 618 | |
| 619 | def __init__(self, node_graph, node, old_position: QPointF, new_position: QPointF): |
| 620 | """ |
| 621 | Initialize move node command. |
| 622 | |
| 623 | Args: |
| 624 | node_graph: The NodeGraph instance |
| 625 | node: Node to move |
| 626 | old_position: Original position |
| 627 | new_position: New position |
| 628 | """ |
| 629 | super().__init__(f"Move '{node.title}' node") |
| 630 | self.node_graph = node_graph |
| 631 | self.node = node |
| 632 | self.old_position = old_position |
| 633 | self.new_position = new_position |
| 634 | |
| 635 | def execute(self) -> bool: |
| 636 | """Move node to new position.""" |
| 637 | try: |
| 638 | self.node.setPos(self.new_position) |
| 639 | self._mark_executed() |
| 640 | return True |
| 641 | except Exception as e: |
| 642 | print(f"Failed to move node: {e}") |
| 643 | return False |
| 644 | |
| 645 | def undo(self) -> bool: |
| 646 | """Move node back to original position.""" |
| 647 | try: |
| 648 | self.node.setPos(self.old_position) |
| 649 | self._mark_undone() |
| 650 | return True |
| 651 | except Exception as e: |
| 652 | print(f"Failed to undo node move: {e}") |
| 653 | return False |
| 654 | |
| 655 | def can_merge_with(self, other: CommandBase) -> bool: |
| 656 | """Check if this move can be merged with another move.""" |
| 657 | return (isinstance(other, MoveNodeCommand) and |
| 658 | other.node == self.node and |
| 659 | abs(other.timestamp - self.timestamp) < 1.0) # Within 1 second |
| 660 | |
| 661 | def merge_with(self, other: CommandBase) -> Optional[CommandBase]: |
| 662 | """Merge with another move command.""" |
| 663 | if not self.can_merge_with(other): |
| 664 | return None |
| 665 | |
| 666 | # Create merged command using original start position and latest end position |
| 667 | return MoveNodeCommand( |
| 668 | self.node_graph, |
| 669 | self.node, |
| 670 | self.old_position, |
| 671 | other.new_position |
| 672 | ) |
no outgoing calls