-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_node_system.py
More file actions
1832 lines (1464 loc) · 78.4 KB
/
debug_node_system.py
File metadata and controls
1832 lines (1464 loc) · 78.4 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Comprehensive debug application for VysualPy node system.
Features:
- Right-click context menus for creating nodes
- Full support for Blueprint, Execution, and Buildable nodes
- Multi-selection and group operations
- Node editing and property management
- Visual feedback and state management
"""
import sys
import threading
import uuid
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel,
QMenu, QAction, QInputDialog, QMessageBox, QTextEdit,
QDialog, QFormLayout, QLineEdit, QPushButton, QHBoxLayout
)
from PyQt5.QtCore import Qt, QPointF, QTimer, QObject, pyqtSignal
from PyQt5.QtGui import QBrush, QColor, QFont
# Import the new node classes and blueprint system
from vpy_node_types import BlueprintNode, ExecutionNode, BuildableNode
from vpy_blueprints import BlueprintScene, BlueprintView
from PyQt5.QtWidgets import QGraphicsLineItem, QGraphicsPathItem
from PyQt5.QtGui import QPen, QPainterPath
# Import RPC server components
try:
from vpy_rpc_server import VysualPyRPCServer, VysualPyAPIServer
RPC_AVAILABLE = True
except ImportError as e:
print(f"Warning: RPC server not available: {e}")
RPC_AVAILABLE = False
class NodeCreationDialog(QDialog):
"""Dialog for creating new nodes with custom properties."""
def __init__(self, node_type, parent=None):
super().__init__(parent)
self.node_type = node_type
self.setWindowTitle(f"Create {node_type} Node")
self.setModal(True)
self.resize(400, 300)
layout = QVBoxLayout(self)
# Form layout for node properties
form_layout = QFormLayout()
# Name field
self.name_edit = QLineEdit()
self.name_edit.setText(f"New {node_type}")
form_layout.addRow("Name:", self.name_edit)
# Content field (for blueprint and buildable nodes)
if node_type in ["Blueprint", "Buildable"]:
self.content_edit = QTextEdit()
self.content_edit.setPlainText(self.get_default_content(node_type))
self.content_edit.setFont(QFont("Courier New", 10))
form_layout.addRow("Content:", self.content_edit)
else:
self.content_edit = None
# Original name field (for execution nodes)
if node_type == "Execution":
self.original_name_edit = QLineEdit()
self.original_name_edit.setText(f"Original {node_type}")
form_layout.addRow("Original Name:", self.original_name_edit)
else:
self.original_name_edit = None
layout.addLayout(form_layout)
# Buttons
button_layout = QHBoxLayout()
create_button = QPushButton("Create")
create_button.clicked.connect(self.accept)
create_button.setDefault(True)
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.reject)
button_layout.addWidget(create_button)
button_layout.addWidget(cancel_button)
layout.addLayout(button_layout)
def get_default_content(self, node_type):
"""Get default content for different node types."""
if node_type == "Blueprint":
return "def new_function():\n \"\"\"A new function.\"\"\"\n pass"
elif node_type == "Buildable":
return "# Buildable code block\nprint('Hello from buildable node')"
return ""
def get_node_data(self):
"""Get the data needed to create the node."""
data = {
'name': self.name_edit.text().strip() or f"New {self.node_type}"
}
if self.content_edit:
data['content'] = self.content_edit.toPlainText()
if self.original_name_edit:
data['original_name'] = self.original_name_edit.text().strip()
return data
class SimpleConnection(QGraphicsPathItem):
"""Curved connection line for blueprint nodes (like Unreal Engine)."""
def __init__(self, start_port, start_pos, scene):
super().__init__()
self.start_port = start_port
self.end_port = None # Will be set when connection is completed
self.end_pos = start_pos
self.scene = scene
# Animation and hover state
self.is_hovered = False
self.is_completed = False
self.current_width = 1.0 # Start thin
self.target_width = 1.0
self.animation_speed = 0.2 # Animation interpolation factor
# Set initial visual style - thin line
self.normal_pen = QPen(QColor(255, 255, 255, 150), 1) # Thin, semi-transparent white
self.normal_pen.setCapStyle(Qt.RoundCap)
self.normal_pen.setJoinStyle(Qt.RoundJoin)
self.hover_pen = QPen(QColor(255, 255, 255, 220), 4) # Thicker on hover
self.hover_pen.setCapStyle(Qt.RoundCap)
self.hover_pen.setJoinStyle(Qt.RoundJoin)
# Completed connection style (green)
self.completed_pen = QPen(QColor(0, 255, 100, 180), 1) # Thin green
self.completed_pen.setCapStyle(Qt.RoundCap)
self.completed_pen.setJoinStyle(Qt.RoundJoin)
self.completed_hover_pen = QPen(QColor(0, 255, 100, 220), 4) # Thick green on hover
self.completed_hover_pen.setCapStyle(Qt.RoundCap)
self.completed_hover_pen.setJoinStyle(Qt.RoundJoin)
# Set initial pen
self.setPen(self.normal_pen)
# Enable hover events
self.setAcceptHoverEvents(True)
# Enable drag functionality for connection lines - IMPORTANT: Make sure we can receive events
self.setFlag(QGraphicsPathItem.ItemIsMovable, False) # We'll handle movement manually
self.setFlag(QGraphicsPathItem.ItemIsSelectable, True) # Need to be selectable to receive events
self.setFlag(QGraphicsPathItem.ItemIsFocusable, False) # Don't take focus
self.setAcceptedMouseButtons(Qt.LeftButton | Qt.RightButton) # Only handle left and right clicks
# Set a higher z-value to ensure connection lines are on top of nodes for event handling
self.setZValue(100) # Higher than nodes so we get mouse events
# Dragging state for connection lines
self.is_dragging_connection = False
self.drag_start_scene_pos = QPointF()
self.drag_threshold = 5.0 # Minimum pixels to move before starting drag
# Store initial positions for consistent dragging
self.initial_start_node_pos = QPointF()
self.initial_end_node_pos = QPointF()
self.initial_drag_pos = QPointF()
# Enhanced tooltip with connection information
self.update_tooltip()
# Add to scene
scene.addItem(self)
self.updatePath()
# Track this connection in the scene for updates when nodes move
if hasattr(scene, 'connections'):
scene.connections.append(self)
def updatePath(self):
"""Update the curved path like Unreal Engine blueprints."""
# Get start position in scene coordinates
start_pos = self.start_port.node.mapToScene(self.start_port.position)
# Convert to local coordinates
local_start = self.mapFromScene(start_pos)
local_end = self.mapFromScene(self.end_pos)
# Create bezier curve path
path = QPainterPath()
path.moveTo(local_start)
# Calculate direction and distance
dx = local_end.x() - local_start.x()
dy = local_end.y() - local_start.y()
# Dynamic control point calculation based on direction
# Base offset that scales with distance
base_offset = max(abs(dx) * 0.4, 60)
# Adjust curve based on the relative positions
if dx >= 0:
# Normal left-to-right connection
control_offset_x = base_offset
else:
# Backwards connection - make tighter curves
control_offset_x = min(base_offset, abs(dx) * 0.8)
# Add vertical adjustment for better curves when going up/down
vertical_influence = min(abs(dy) * 0.2, 50)
# Determine port orientation (output goes right, input comes from left)
if hasattr(self.start_port, 'type') and self.start_port.type.value == 'output':
# Output port - curve extends to the right
control1 = QPointF(local_start.x() + control_offset_x, local_start.y())
else:
# Input port - curve extends to the left
control1 = QPointF(local_start.x() - control_offset_x, local_start.y())
# End control point - always opposite direction from the start
if dx >= 0:
# Normal flow - end control point extends left from end
control2 = QPointF(local_end.x() - control_offset_x, local_end.y())
else:
# Backwards flow - create an S-curve that looks natural
control2 = QPointF(local_end.x() + control_offset_x * 0.5, local_end.y())
# Add some vertical curve influence for smoother transitions
if abs(dy) > 20: # Only add vertical influence for significant height differences
if dy > 0: # Going down
control1 = QPointF(control1.x(), control1.y() + vertical_influence)
control2 = QPointF(control2.x(), control2.y() - vertical_influence)
else: # Going up
control1 = QPointF(control1.x(), control1.y() - vertical_influence)
control2 = QPointF(control2.x(), control2.y() + vertical_influence)
# Create smooth cubic bezier curve
path.cubicTo(control1, control2, local_end)
self.setPath(path)
def paint(self, painter, option, widget):
"""Custom paint with smooth width animation."""
# Update animation if needed
if abs(self.current_width - self.target_width) > 0.1:
self.current_width += (self.target_width - self.current_width) * self.animation_speed
self.update() # Trigger repaint for smooth animation
else:
self.current_width = self.target_width
# Create animated pen based on current state
if self.is_completed:
if self.is_hovered:
color = QColor(0, 255, 100, 220)
else:
color = QColor(0, 255, 100, 180)
else:
if self.is_hovered:
color = QColor(255, 255, 255, 220)
else:
color = QColor(255, 255, 255, 150)
# Create pen with current animated width
animated_pen = QPen(color, max(1, self.current_width))
animated_pen.setCapStyle(Qt.RoundCap)
animated_pen.setJoinStyle(Qt.RoundJoin)
# Draw the connection line
painter.setPen(animated_pen)
painter.drawPath(self.path())
def setEndPoint(self, end_port):
"""Complete the connection to an end port."""
# Store the end port for dynamic updates
self.end_port = end_port
# Update the end position to the target port
end_pos = end_port.node.mapToScene(end_port.position)
self.end_pos = end_pos
# Update the curve
self.updatePath()
# Mark as completed for styling
self.is_completed = True
# Update pen based on current hover state
self.update_pen_style()
# Update tooltip with completed connection information
self.update_tooltip()
print(f"Connection completed from {self.start_port.node.name}:{self.start_port.id} to {end_port.node.name}:{end_port.id}")
def updateForNodeMovement(self):
"""Update connection when connected nodes have moved."""
if self.end_port:
# Both ends are connected to ports - update both positions
start_pos = self.start_port.node.mapToScene(self.start_port.position)
end_pos = self.end_port.node.mapToScene(self.end_port.position)
self.end_pos = end_pos
self.updatePath()
else:
# Only start is connected, end is free-floating
self.updatePath()
def hoverEnterEvent(self, event):
"""Handle mouse hover enter - start thickening animation."""
self.is_hovered = True
self.target_width = 4.0 # Target thickness on hover
self.update_pen_style()
super().hoverEnterEvent(event)
def hoverLeaveEvent(self, event):
"""Handle mouse hover leave - start thinning animation."""
self.is_hovered = False
self.target_width = 1.0 # Target thickness when not hovered
self.update_pen_style()
super().hoverLeaveEvent(event)
def update_pen_style(self):
"""Update pen style based on current state."""
if self.is_completed:
if self.is_hovered:
self.setPen(self.completed_hover_pen)
else:
self.setPen(self.completed_pen)
else:
if self.is_hovered:
self.setPen(self.hover_pen)
else:
self.setPen(self.normal_pen)
# Trigger repaint to show changes
self.update()
def mousePressEvent(self, event):
"""Handle mouse press on connection line for dragging."""
if event.button() == Qt.LeftButton and self.is_completed and self.end_port:
# Only allow dragging of completed connections with both ports
self.drag_start_scene_pos = event.scenePos()
# Store initial positions of both nodes to maintain relative distances
self.initial_start_node_pos = self.start_port.node.pos()
self.initial_end_node_pos = self.end_port.node.pos()
self.initial_drag_pos = event.scenePos()
# Don't set dragging flag yet - wait for mouse move to exceed threshold
event.accept()
return
elif event.button() == Qt.RightButton:
# Handle right-click for context menu
event.accept()
return
# For other cases, don't handle the event
event.ignore()
def mouseMoveEvent(self, event):
"""Handle dragging the connection line to move both nodes."""
if self.is_completed and self.end_port and self.drag_start_scene_pos is not None:
current_scene_pos = event.scenePos()
# Check if we should start dragging (threshold exceeded)
if not self.is_dragging_connection:
dx = current_scene_pos.x() - self.initial_drag_pos.x()
dy = current_scene_pos.y() - self.initial_drag_pos.y()
distance = (dx * dx + dy * dy) ** 0.5
if distance > self.drag_threshold:
self.is_dragging_connection = True
# Change cursor to indicate dragging
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
QApplication.setOverrideCursor(Qt.ClosedHandCursor)
else:
return # Don't start dragging yet
if self.is_dragging_connection:
# Calculate movement delta from initial drag position (maintains relative distances)
delta = current_scene_pos - self.initial_drag_pos
# Get both connected nodes
start_node = self.start_port.node
end_node = self.end_port.node
# Calculate new positions based on initial positions + delta
start_new_pos = self.initial_start_node_pos + delta
end_new_pos = self.initial_end_node_pos + delta
# Apply new positions
start_node.setPos(start_new_pos)
end_node.setPos(end_new_pos)
# Update connections (but avoid recursion by checking if we're already updating)
if hasattr(self.scene, '_updating_connections') and not self.scene._updating_connections:
self.scene._updating_connections = True
self.scene.update_all_connections()
self.scene._updating_connections = False
event.accept()
return
# Don't handle other move events
event.ignore()
def mouseReleaseEvent(self, event):
"""Handle mouse release to stop dragging."""
if event.button() == Qt.LeftButton:
if self.is_dragging_connection:
# Stop dragging and restore cursor
self.is_dragging_connection = False
from PyQt5.QtWidgets import QApplication
QApplication.restoreOverrideCursor()
# Reset drag positions
self.drag_start_scene_pos = QPointF()
self.initial_start_node_pos = QPointF()
self.initial_end_node_pos = QPointF()
self.initial_drag_pos = QPointF()
event.accept()
return
# Don't handle other release events
event.ignore()
def contextMenuEvent(self, event):
"""Handle right-click context menu on connection."""
menu = QMenu()
# Only show break option if this is a completed connection
if self.is_completed and self.end_port:
break_action = menu.addAction("🔗 Break Connection")
break_action.triggered.connect(self.break_connection)
# Show connection info as disabled action (for context)
info_text = f"Connection: {self.start_port.node.name}:{self.start_port.id} → {self.end_port.node.name}:{self.end_port.id}"
info_action = menu.addAction(info_text)
info_action.setEnabled(False) # Make it non-clickable, just informational
else:
# For incomplete connections, offer cancel option
cancel_action = menu.addAction("❌ Cancel Connection")
cancel_action.triggered.connect(self.cancel_connection)
# Convert event position to global coordinates
if hasattr(event, 'screenPos'):
global_pos = event.screenPos()
else:
# Fallback: use current mouse position
from PyQt5.QtGui import QCursor
global_pos = QCursor.pos()
# Show the menu
menu.exec_(global_pos)
def break_connection(self):
"""Break this connection and remove it from the scene."""
if self.scene and hasattr(self.scene, 'connections'):
# Remove from scene's connection tracking
if self in self.scene.connections:
self.scene.connections.remove(self)
# Remove from scene graphics
if self.scene:
self.scene.removeItem(self)
# Clean up port connections if they have connection tracking
if hasattr(self.start_port, 'remove_connection'):
self.start_port.remove_connection(self)
if self.end_port and hasattr(self.end_port, 'remove_connection'):
self.end_port.remove_connection(self)
print(f"Broke connection: {self.start_port.node.name}:{self.start_port.id} → {self.end_port.node.name if self.end_port else 'None'}:{self.end_port.id if self.end_port else 'None'}")
def cancel_connection(self):
"""Cancel an in-progress connection."""
if self.scene and hasattr(self.scene, 'connections'):
# Remove from scene's connection tracking
if self in self.scene.connections:
self.scene.connections.remove(self)
# Remove from scene graphics
if self.scene:
self.scene.removeItem(self)
# Clean up any connection state in the scene
if hasattr(self.scene, 'connection_in_progress') and self.scene.connection_in_progress == self:
self.scene.connection_in_progress = None
if hasattr(self.scene, 'active_connection_point'):
self.scene.active_connection_point = None
print("Cancelled connection creation")
def update_tooltip(self):
"""Update tooltip with enhanced connection information."""
if self.is_completed and self.end_port:
# Complete connection - show detailed info
start_node = self.start_port.node
end_node = self.end_port.node
# Determine node types
start_type = "Unknown"
end_type = "Unknown"
if hasattr(start_node, 'node_type'):
start_type = start_node.node_type.value.capitalize()
if hasattr(end_node, 'node_type'):
end_type = end_node.node_type.value.capitalize()
# Create detailed tooltip
tooltip_parts = [
f"🔗 {start_type} Connection",
f"Source: {start_node.name} [{self.start_port.id}]",
f"Target: {end_node.name} [{self.end_port.id}]",
"",
"Connection Details:"
]
# Add node type specific information
if start_type == "Blueprint" and end_type == "Blueprint":
tooltip_parts.append("📦 Structural relationship between code components")
tooltip_parts.append("• Shows function call or class dependency")
tooltip_parts.append("• Bidirectional logical connection")
elif start_type == "Execution" and end_type == "Execution":
tooltip_parts.append("⚡ Runtime execution flow")
tooltip_parts.append("• Shows actual function call sequence")
tooltip_parts.append("• Directional execution path")
elif start_type == "Buildable" and end_type == "Buildable":
tooltip_parts.append("🔧 Code reference dependency")
tooltip_parts.append("• Automatically detected from code analysis")
tooltip_parts.append("• Shows variable/function references")
# Add interaction hints
tooltip_parts.extend([
"",
"Interaction:",
"• Drag to move both connected nodes",
"• Right-click to break connection",
"• Hover for highlight and thickness change"
])
tooltip_text = "\n".join(tooltip_parts)
elif self.start_port:
# Incomplete connection - show creation info
start_node = self.start_port.node
start_type = "Unknown"
if hasattr(start_node, 'node_type'):
start_type = start_node.node_type.value.capitalize()
tooltip_text = f"🚧 Creating {start_type} Connection\n\nSource: {start_node.name} [{self.start_port.id}]\n\nDrag to a compatible input port to complete the connection."
else:
# Fallback for incomplete state
tooltip_text = "🔗 Connection Line\n\nHover for details when connection is complete."
self.setToolTip(tooltip_text)
class EnhancedBlueprintScene(BlueprintScene):
"""Enhanced scene with comprehensive node creation capabilities."""
def __init__(self):
super().__init__()
self.node_counter = {'Blueprint': 0, 'Execution': 0, 'Buildable': 0}
# Use simple connection system for blueprint nodes
self.connections = []
# Prevent recursion in connection updates
self._updating_connections = False
def showContextMenu(self, event):
"""Enhanced context menu with node creation options."""
menu = QMenu()
# Node creation submenu
create_menu = menu.addMenu("Create Node")
# Blueprint node action
blueprint_action = create_menu.addAction("📦 Blueprint Node")
blueprint_action.triggered.connect(lambda: self.create_node_at_position("Blueprint", event.scenePos()))
# Execution node action
execution_action = create_menu.addAction("⚡ Execution Node")
execution_action.triggered.connect(lambda: self.create_node_at_position("Execution", event.scenePos()))
# Buildable node action
buildable_action = create_menu.addAction("🔧 Buildable Node")
buildable_action.triggered.connect(lambda: self.create_node_at_position("Buildable", event.scenePos()))
menu.addSeparator()
# Scene operations
clear_action = menu.addAction("Clear All Nodes")
clear_action.triggered.connect(self.clear_all_nodes)
# Selection operations if there are selected items
selected_items = self.selectedItems()
if selected_items:
menu.addSeparator()
delete_selected_action = menu.addAction(f"Delete Selected ({len(selected_items)})")
delete_selected_action.triggered.connect(self.delete_selected_nodes)
if len(selected_items) > 1:
align_menu = menu.addMenu("Align Selected")
align_left_action = align_menu.addAction("Align Left")
align_left_action.triggered.connect(lambda: self.align_selected_nodes("left"))
align_right_action = align_menu.addAction("Align Right")
align_right_action.triggered.connect(lambda: self.align_selected_nodes("right"))
align_top_action = align_menu.addAction("Align Top")
align_top_action.triggered.connect(lambda: self.align_selected_nodes("top"))
align_bottom_action = align_menu.addAction("Align Bottom")
align_bottom_action.triggered.connect(lambda: self.align_selected_nodes("bottom"))
distribute_menu = menu.addMenu("Distribute Selected")
distribute_h_action = distribute_menu.addAction("Distribute Horizontally")
distribute_h_action.triggered.connect(lambda: self.distribute_selected_nodes("horizontal"))
distribute_v_action = distribute_menu.addAction("Distribute Vertically")
distribute_v_action.triggered.connect(lambda: self.distribute_selected_nodes("vertical"))
# Legacy comment box support
menu.addSeparator()
addCommentAction = menu.addAction("Add Comment Box")
# Convert scene position to global position for the menu
view = self.views()[0] if self.views() else None
if view:
global_pos = view.mapToGlobal(view.mapFromScene(event.scenePos()))
else:
global_pos = event.screenPos()
# Store scene position for later use
scene_pos = event.scenePos()
# Show menu and handle action
action = menu.exec_(global_pos)
if action == addCommentAction:
name, ok = QInputDialog.getText(None, "New Comment Box",
"Enter comment box name:")
if ok and name:
from vpy_graph import CommentBox
comment_box = CommentBox(name, scene_pos.x(), scene_pos.y())
self.addItem(comment_box)
def create_node_at_position(self, node_type, position):
"""Create a new node at the specified position."""
# Show creation dialog
dialog = NodeCreationDialog(node_type)
if dialog.exec_() != QDialog.Accepted:
return
# Get node data from dialog
data = dialog.get_node_data()
# Create the appropriate node type
try:
if node_type == "Blueprint":
node = BlueprintNode(
name=data['name'],
content=data.get('content', '')
)
# Process content change to trigger resizing
if data.get('content', '').strip():
node.process_content_change("", data.get('content', ''))
elif node_type == "Execution":
node = ExecutionNode(
name=data['name'],
original_name=data.get('original_name', data['name'])
)
elif node_type == "Buildable":
node = BuildableNode(
name=data['name'],
content=data.get('content', '')
)
else:
QMessageBox.warning(None, "Error", f"Unknown node type: {node_type}")
return
# Position the node
node.setPos(position)
# Add to scene
self.addItem(node)
# Update counter
self.node_counter[node_type] += 1
# If this is a BuildableNode and we have a connected global editor, add it
if (node_type == "Buildable" and hasattr(self, 'global_editor') and
self.global_editor is not None):
self.global_editor.add_tracked_node(node)
print(f"Added new BuildableNode '{data['name']}' to global editor")
print(f"Created {node_type} node '{data['name']}' at {position}")
except Exception as e:
QMessageBox.critical(None, "Error Creating Node",
f"Failed to create {node_type} node:\n{str(e)}")
def clear_all_nodes(self):
"""Clear all nodes from the scene."""
reply = QMessageBox.question(
None, "Clear All Nodes",
"Are you sure you want to remove all nodes from the scene?",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
# Remove all node items
items_to_remove = []
for item in self.items():
if hasattr(item, 'node_type'): # Our custom nodes
items_to_remove.append(item)
for item in items_to_remove:
self.removeItem(item)
# Reset counters
self.node_counter = {'Blueprint': 0, 'Execution': 0, 'Buildable': 0}
print("Cleared all nodes from scene")
def delete_selected_nodes(self):
"""Delete all selected nodes."""
selected_items = self.selectedItems()
node_items = [item for item in selected_items if hasattr(item, 'node_type')]
if not node_items:
return
reply = QMessageBox.question(
None, "Delete Selected Nodes",
f"Are you sure you want to delete {len(node_items)} selected node(s)?",
QMessageBox.Yes | QMessageBox.No
)
if reply == QMessageBox.Yes:
for item in node_items:
# Disconnect all connections
if hasattr(item, 'disconnect_all'):
item.disconnect_all()
self.removeItem(item)
print(f"Deleted {len(node_items)} selected nodes")
def align_selected_nodes(self, alignment):
"""Align selected nodes."""
selected_items = [item for item in self.selectedItems() if hasattr(item, 'node_type')]
if len(selected_items) < 2:
return
# Get reference position (first selected node)
ref_item = selected_items[0]
ref_pos = ref_item.pos()
for item in selected_items[1:]:
current_pos = item.pos()
if alignment == "left":
item.setPos(ref_pos.x(), current_pos.y())
elif alignment == "right":
item.setPos(ref_pos.x() + ref_item.rect().width() - item.rect().width(), current_pos.y())
elif alignment == "top":
item.setPos(current_pos.x(), ref_pos.y())
elif alignment == "bottom":
item.setPos(current_pos.x(), ref_pos.y() + ref_item.rect().height() - item.rect().height())
print(f"Aligned {len(selected_items)} nodes to {alignment}")
def distribute_selected_nodes(self, direction):
"""Distribute selected nodes evenly."""
selected_items = [item for item in self.selectedItems() if hasattr(item, 'node_type')]
if len(selected_items) < 3:
QMessageBox.information(None, "Info", "Need at least 3 nodes to distribute")
return
# Sort by position
if direction == "horizontal":
selected_items.sort(key=lambda item: item.pos().x())
else:
selected_items.sort(key=lambda item: item.pos().y())
# Calculate spacing
first_item = selected_items[0]
last_item = selected_items[-1]
if direction == "horizontal":
total_distance = last_item.pos().x() - first_item.pos().x()
spacing = total_distance / (len(selected_items) - 1)
for i, item in enumerate(selected_items[1:-1], 1):
new_x = first_item.pos().x() + (spacing * i)
item.setPos(new_x, item.pos().y())
else:
total_distance = last_item.pos().y() - first_item.pos().y()
spacing = total_distance / (len(selected_items) - 1)
for i, item in enumerate(selected_items[1:-1], 1):
new_y = first_item.pos().y() + (spacing * i)
item.setPos(item.pos().x(), new_y)
print(f"Distributed {len(selected_items)} nodes {direction}ly")
def mousePressEvent(self, event):
"""Enhanced mouse press handling for connection creation."""
if event.button() == Qt.LeftButton:
from PyQt5.QtWidgets import QApplication
from vpy_node_base import BaseNode
modifiers = QApplication.keyboardModifiers()
# First, check ALL nodes for port hits - including ports that extend outside node bounds
scene_pos = event.scenePos()
port_found = False
# Check all nodes in the scene for port proximity
for item in self.items():
if isinstance(item, BaseNode):
# Check if we're close to any ports on this node
found_port, clicked_port = self.check_port_proximity(item, scene_pos)
if found_port:
# Start connection from this port
if not self.connection_in_progress:
self.connection_in_progress = SimpleConnection(
clicked_port, event.scenePos(), self
)
self.active_connection_point = clicked_port
port_type = "output" if clicked_port.type.value == 'output' else "input"
print(f"Started connection from {item.name}:{clicked_port.id} ({port_type})")
event.accept() # Consume the event to prevent rubber band selection
port_found = True
return
# If no port was found, handle regular scene interactions
if not port_found:
# Check if we're clicking on a node (but not a port)
items = self.items(event.scenePos())
if not items and not (modifiers & Qt.ControlModifier):
# Clicking on empty space without Ctrl - clear all selections
for item in self.selectedItems():
item.setSelected(False)
if hasattr(item, 'set_state'):
from vpy_node_base import NodeState
item.set_state(NodeState.NORMAL)
elif event.button() == Qt.RightButton:
# Only show context menu if clicking on empty space
items = self.items(event.scenePos())
if not items: # If no items under cursor
self.showContextMenu(event)
# Only call super if we didn't handle connection creation
if not (hasattr(self, 'connection_in_progress') and self.connection_in_progress):
super().mousePressEvent(event)
def check_port_proximity(self, node, scene_pos):
"""Check if scene position is close to any port on the given node.
Returns (bool, port) where bool indicates if a port was found,
and port is the ConnectionPort object if found, None otherwise.
"""
# Port hit detection radius
port_radius = 30 # Generous hitbox for ports that extend outside nodes
# Check output ports first
for port_id, port in node.output_ports.items():
# Get port position in scene coordinates
port_scene_pos = node.mapToScene(port.position)
# Calculate distance from click to port center
dx = scene_pos.x() - port_scene_pos.x()
dy = scene_pos.y() - port_scene_pos.y()
distance = (dx * dx + dy * dy) ** 0.5
if distance <= port_radius:
return True, port
# Check input ports
for port_id, port in node.input_ports.items():
# Get port position in scene coordinates
port_scene_pos = node.mapToScene(port.position)
# Calculate distance from click to port center
dx = scene_pos.x() - port_scene_pos.x()
dy = scene_pos.y() - port_scene_pos.y()
distance = (dx * dx + dy * dy) ** 0.5
if distance <= port_radius:
return True, port
return False, None
def mouseReleaseEvent(self, event):
"""Handle connection completion."""
if self.connection_in_progress:
valid_connection = False
from vpy_node_base import BaseNode
scene_pos = event.scenePos()
# Check all nodes for target port proximity (not just those under cursor)
for item in self.items():
if isinstance(item, BaseNode) and item != self.active_connection_point.node:
# Check if we're close to a compatible port
found_port, target_port = self.check_port_proximity_for_connection(
item, scene_pos, self.active_connection_point
)
if found_port and target_port:
# STRICT node type compatibility - only same types can connect
start_node = self.active_connection_point.node
end_node = target_port.node
# Check if both nodes have node_type attribute
if not (hasattr(start_node, 'node_type') and hasattr(end_node, 'node_type')):
print(f"Connection failed: One or both nodes missing node_type attribute")
continue
# Only allow connections between nodes of the exact same type
nodes_compatible = (start_node.node_type == end_node.node_type)
if nodes_compatible:
# For Blueprint nodes only: Check if this connection already exists
if (hasattr(start_node, 'node_type') and
start_node.node_type.value == 'blueprint'):
# Check if a connection already exists between these two nodes
connection_exists = self.check_blueprint_connection_exists(
start_node, end_node
)
if connection_exists:
print(f"❌ Blueprint connection already exists between '{start_node.name}' and '{end_node.name}' - duplicate connections not allowed")
continue # Skip this connection and continue checking other targets
# Connection is valid - create it
self.connection_in_progress.setEndPoint(target_port)
valid_connection = True
node_type_name = start_node.node_type.value.capitalize()
print(f"✅ Created {node_type_name} connection: {start_node.name}:{self.active_connection_point.id} → {end_node.name}:{target_port.id}")
break
else:
start_type = start_node.node_type.value.capitalize()
end_type = end_node.node_type.value.capitalize()
print(f"❌ Cannot connect {start_type} node '{start_node.name}' to {end_type} node '{end_node.name}' - incompatible node types")
# Don't break here - continue checking other potential targets
if not valid_connection:
# Remove invalid connection
self.removeItem(self.connection_in_progress)
if self.active_connection_point and hasattr(self.active_connection_point, 'remove_connection'):
self.active_connection_point.remove_connection(self.connection_in_progress)
# Also remove from connections list
if hasattr(self, 'connections') and self.connection_in_progress in self.connections:
self.connections.remove(self.connection_in_progress)
print("❌ Connection cancelled - no valid target found")
self.connection_in_progress = None
self.active_connection_point = None
super().mouseReleaseEvent(event)
def check_port_proximity_for_connection(self, node, scene_pos, source_port):
"""Check if scene position is close to a compatible port for connection.
Only returns ports that can connect to the source port AND are on compatible node types.
"""
port_radius = 30 # Match the radius from check_port_proximity
# FIRST: Check node type compatibility before even looking at ports
source_node = source_port.node
target_node = node
# Strict type checking - both nodes must have node_type and they must match
if not (hasattr(source_node, 'node_type') and hasattr(target_node, 'node_type')):
return False, None
# DISABLE MANUAL CONNECTIONS FOR BUILDABLE NODES - they are code-driven
if (hasattr(source_node, 'node_type') and source_node.node_type.value == 'buildable') or \
(hasattr(target_node, 'node_type') and target_node.node_type.value == 'buildable'):
print(f"❌ Manual connections disabled for BuildableNodes - use code references instead")
return False, None
if source_node.node_type != target_node.node_type:
return False, None # Incompatible node types - don't even check ports
# Node types are compatible, now check port proximity and direction
# Determine which ports to check based on source port type
if source_port.type.value == 'output':
# Source is output, look for input ports
ports_to_check = node.input_ports