Fix sketcher mode handling to prevent unintended line creation during drag operations
Major changes: - Fixed right-click handler to directly set mode to NONE instead of relying on main app signal handling - Added safety checks in left-click handler to prevent drawing when no draggable point is found in NONE mode - Enhanced mode compatibility by treating Python None as SketchMode.NONE in set_mode() method - Added comprehensive debug logging for mode changes and interaction state tracking - Resolved integration issue where persistent constraint modes were prematurely reset by main app - Ensured point dragging is only enabled in NONE mode, preventing accidental polyline creation This fixes the reported issue where deactivating the line tool would still create lines when dragging, and ensures proper mode transitions between drawing tools and selection/drag mode.
This commit is contained in:
180
test_dragging_specific.py
Normal file
180
test_dragging_specific.py
Normal file
@@ -0,0 +1,180 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Specific test for dragging functionality
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.path.append('/Volumes/Data_drive/Programming/fluency')
|
||||
|
||||
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QHBoxLayout
|
||||
from PySide6.QtCore import Qt, QPoint
|
||||
from PySide6.QtGui import QMouseEvent
|
||||
from drawing_modules.improved_sketcher import ImprovedSketchWidget, SketchMode, Point2D
|
||||
import logging
|
||||
|
||||
# Set up more detailed logging
|
||||
logging.basicConfig(level=logging.DEBUG, format='%(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DragTestWindow(QMainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.setWindowTitle("Dragging Test")
|
||||
self.resize(1000, 700)
|
||||
|
||||
# Create central widget
|
||||
central_widget = QWidget()
|
||||
self.setCentralWidget(central_widget)
|
||||
layout = QVBoxLayout(central_widget)
|
||||
|
||||
# Create button layout
|
||||
button_layout = QHBoxLayout()
|
||||
|
||||
# Add test points button
|
||||
add_points_btn = QPushButton("Add Test Points")
|
||||
add_points_btn.clicked.connect(self.add_test_points)
|
||||
button_layout.addWidget(add_points_btn)
|
||||
|
||||
# Test manual drag
|
||||
test_drag_btn = QPushButton("Test Manual Drag")
|
||||
test_drag_btn.clicked.connect(self.test_manual_drag)
|
||||
button_layout.addWidget(test_drag_btn)
|
||||
|
||||
# Check state
|
||||
check_state_btn = QPushButton("Check State")
|
||||
check_state_btn.clicked.connect(self.check_state)
|
||||
button_layout.addWidget(check_state_btn)
|
||||
|
||||
layout.addLayout(button_layout)
|
||||
|
||||
# Create the sketcher widget
|
||||
self.sketcher = ImprovedSketchWidget()
|
||||
self.sketcher.set_mode(SketchMode.NONE)
|
||||
layout.addWidget(self.sketcher)
|
||||
|
||||
# Override mouse events to add detailed logging
|
||||
self.override_mouse_events()
|
||||
|
||||
def override_mouse_events(self):
|
||||
"""Override mouse events with logging"""
|
||||
self.original_mouse_press = self.sketcher.mousePressEvent
|
||||
self.original_mouse_move = self.sketcher.mouseMoveEvent
|
||||
self.original_mouse_release = self.sketcher.mouseReleaseEvent
|
||||
|
||||
self.sketcher.mousePressEvent = self.logged_mouse_press
|
||||
self.sketcher.mouseMoveEvent = self.logged_mouse_move
|
||||
self.sketcher.mouseReleaseEvent = self.logged_mouse_release
|
||||
|
||||
def logged_mouse_press(self, event):
|
||||
"""Logged mouse press"""
|
||||
viewport_pos = event.pos()
|
||||
local_pos = self.sketcher._viewport_to_local(viewport_pos)
|
||||
print(f"\n=== MOUSE PRESS ===")
|
||||
print(f"Button: {event.button()}")
|
||||
print(f"Viewport: {viewport_pos}, Local: {local_pos}")
|
||||
print(f"Current mode: {self.sketcher.current_mode}")
|
||||
print(f"Dragging point before: {self.sketcher.dragging_point}")
|
||||
|
||||
# Call original method
|
||||
result = self.original_mouse_press(event)
|
||||
|
||||
print(f"Dragging point after: {self.sketcher.dragging_point}")
|
||||
print(f"Drag start pos: {self.sketcher.drag_start_pos}")
|
||||
|
||||
return result
|
||||
|
||||
def logged_mouse_move(self, event):
|
||||
"""Logged mouse move"""
|
||||
if self.sketcher.dragging_point:
|
||||
viewport_pos = event.pos()
|
||||
local_pos = self.sketcher._viewport_to_local(viewport_pos)
|
||||
print(f"DRAG MOVE - Viewport: {viewport_pos}, Local: {local_pos}")
|
||||
|
||||
return self.original_mouse_move(event)
|
||||
|
||||
def logged_mouse_release(self, event):
|
||||
"""Logged mouse release"""
|
||||
if self.sketcher.dragging_point:
|
||||
print(f"DRAG END - Button: {event.button()}")
|
||||
|
||||
return self.original_mouse_release(event)
|
||||
|
||||
def add_test_points(self):
|
||||
"""Add test points"""
|
||||
print("\\n--- Adding test points ---")
|
||||
|
||||
# Clear existing points
|
||||
self.sketcher.sketch.points.clear()
|
||||
|
||||
# Add a few test points
|
||||
test_points = [
|
||||
(0, 0),
|
||||
(100, 100),
|
||||
(-100, -100)
|
||||
]
|
||||
|
||||
for x, y in test_points:
|
||||
point = Point2D(x, y)
|
||||
self.sketcher.sketch.add_point(point)
|
||||
print(f"Added point at ({x}, {y})")
|
||||
|
||||
self.sketcher.update()
|
||||
print(f"Total points: {len(self.sketcher.sketch.points)}")
|
||||
|
||||
def test_manual_drag(self):
|
||||
"""Test manual drag simulation"""
|
||||
print("\\n--- Testing manual drag ---")
|
||||
|
||||
if not self.sketcher.sketch.points:
|
||||
print("No points to drag! Add test points first.")
|
||||
return
|
||||
|
||||
# Get the first point
|
||||
point = self.sketcher.sketch.points[0]
|
||||
print(f"Testing drag of point: {point}")
|
||||
|
||||
# Simulate starting a drag
|
||||
start_pos = QPoint(int(point.x), int(point.y))
|
||||
print(f"Starting drag at local position: {start_pos}")
|
||||
|
||||
self.sketcher._start_point_drag(point, start_pos)
|
||||
print(f"Dragging point is now: {self.sketcher.dragging_point}")
|
||||
|
||||
# Simulate moving the point
|
||||
new_pos = QPoint(int(point.x + 50), int(point.y + 30))
|
||||
print(f"Moving to local position: {new_pos}")
|
||||
|
||||
self.sketcher._handle_point_drag(new_pos)
|
||||
print(f"Point position after drag: ({point.x}, {point.y})")
|
||||
|
||||
# End the drag
|
||||
self.sketcher._end_point_drag()
|
||||
print(f"Point position after drag end: ({point.x}, {point.y})")
|
||||
print(f"Dragging point after end: {self.sketcher.dragging_point}")
|
||||
|
||||
def check_state(self):
|
||||
"""Check current state"""
|
||||
print("\\n--- Current State ---")
|
||||
print(f"Mode: {self.sketcher.current_mode}")
|
||||
print(f"Dragging point: {self.sketcher.dragging_point}")
|
||||
print(f"Drag start pos: {self.sketcher.drag_start_pos}")
|
||||
print(f"Number of points: {len(self.sketcher.sketch.points)}")
|
||||
|
||||
for i, point in enumerate(self.sketcher.sketch.points):
|
||||
print(f" Point {i}: ({point.x}, {point.y}) at UI: {point.ui_point}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
window = DragTestWindow()
|
||||
window.show()
|
||||
|
||||
print("\\n" + "="*60)
|
||||
print("DRAGGING FUNCTIONALITY TEST")
|
||||
print("1. Click 'Add Test Points' to create points")
|
||||
print("2. Click 'Test Manual Drag' to simulate dragging programmatically")
|
||||
print("3. Click 'Check State' to see current state")
|
||||
print("4. Try manual dragging with mouse (watch console output)")
|
||||
print("="*60 + "\\n")
|
||||
|
||||
sys.exit(app.exec())
|
||||
Reference in New Issue
Block a user