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:
bklronin
2025-08-16 22:30:18 +02:00
parent 54261bb8fd
commit 11d053fda4
886 changed files with 168708 additions and 51 deletions

137
test_coordinates.py Normal file
View File

@@ -0,0 +1,137 @@
#!/usr/bin/env python3
"""
Simple test to verify coordinate systems and point detection
"""
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 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 CoordinateTestWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Coordinate System 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 Points at (0,0), (100,100), (-100,-100)")
add_points_btn.clicked.connect(self.add_test_points)
button_layout.addWidget(add_points_btn)
# Test coordinate conversion
test_coords_btn = QPushButton("Test Coordinate Conversion")
test_coords_btn.clicked.connect(self.test_coordinate_conversion)
button_layout.addWidget(test_coords_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 logging
self.original_mouse_press = self.sketcher.mousePressEvent
self.sketcher.mousePressEvent = self.logged_mouse_press
def logged_mouse_press(self, event):
"""Logged mouse press to see coordinates"""
viewport_pos = event.pos()
local_pos = self.sketcher._viewport_to_local(viewport_pos)
print(f"Mouse press at viewport: {viewport_pos}, local: {local_pos}")
# Check if we can find a point near this location
point = self.sketcher.sketch.get_point_near(local_pos)
print(f"Point near click: {point}")
# Call original method
return self.original_mouse_press(event)
def add_test_points(self):
"""Add specific test points"""
print("\n--- Adding test points ---")
# Clear existing points
self.sketcher.sketch.points.clear()
# Add points at specific locations
test_points = [
(0, 0, "Origin"),
(100, 100, "Positive quadrant"),
(-100, -100, "Negative quadrant"),
(150, -50, "Mixed quadrant")
]
for x, y, label in test_points:
print(f"Creating {label} point at ({x}, {y})")
point = Point2D(x, y)
# Add to sketch - this will add to solver too
added_point = self.sketcher.sketch.add_point(point)
print(f" Before solver: Point at ({added_point.x}, {added_point.y})")
print(f" UI point: {added_point.ui_point}")
# Run solver to see if it changes
result = self.sketcher.sketch.solve_system()
print(f" After solver ({result}): Point at ({added_point.x}, {added_point.y})")
print(f" UI point after: {added_point.ui_point}")
print()
self.sketcher.update()
print(f"Total points in sketch: {len(self.sketcher.sketch.points)}")
def test_coordinate_conversion(self):
"""Test coordinate conversion functions"""
print("\n--- Testing coordinate conversion ---")
# Test some viewport positions
test_viewport_positions = [
QPoint(500, 350), # Center of widget (approximately)
QPoint(600, 250), # Right and up from center
QPoint(400, 450), # Left and down from center
]
for vp_pos in test_viewport_positions:
local_pos = self.sketcher._viewport_to_local(vp_pos)
back_to_viewport = self.sketcher._local_to_viewport(local_pos)
print(f"Viewport {vp_pos} -> Local {local_pos} -> Viewport {back_to_viewport}")
# Check if conversion is accurate
diff_x = abs(vp_pos.x() - back_to_viewport.x())
diff_y = abs(vp_pos.y() - back_to_viewport.y())
print(f" Difference: ({diff_x}, {diff_y})")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = CoordinateTestWindow()
window.show()
print("\n" + "="*60)
print("COORDINATE SYSTEM TEST")
print("1. Click 'Add Points...' to create test points")
print("2. Click 'Test Coordinate Conversion' to test conversions")
print("3. Try clicking near the points to see if they're detected")
print("4. Try dragging points (should work if mode is NONE)")
print("="*60 + "\n")
sys.exit(app.exec())