1313import os
1414
1515
16-
1716from dataCDLT import (
1817 HORIZONTAL ,
1918 RIGHT ,
@@ -72,7 +71,6 @@ def __init__(self, canvas) -> None:
7271 self .id_origins = {"xyOrigin" : (0 , 0 )}
7372 self .battery_wire_drag_data : dict [str , Any ] = {}
7473
75-
7674 def circuit (self , x_distance = 0 , y_distance = 0 , scale = 1 , width = - 1 , direction = VERTICAL , ** kwargs ):
7775 """
7876 Generates a circuit layout on the canvas based on the provided parameters and model.
@@ -345,7 +343,7 @@ def on_wire_body_leave(self, *_):
345343 """
346344 Event handler for when the mouse leaves the wire body.
347345 """
348- if not self .drag_selector and not self .wire_drag_data ["creating_wire" ]:
346+ if not self .drag_selector and not self .wire_drag_data ["creating_wire" ] and not self . delete_mode_active :
349347 self .canvas .config (cursor = "arrow" )
350348
351349 def on_wire_body_click (self , event , wire_id ) -> None :
@@ -2666,8 +2664,6 @@ def draw_pin_io(self, x_distance, y_distance, scale=1, width=-1, direction=HORIZ
26662664 matrix [f"{ coord [0 ][0 ]} ,{ coord [0 ][1 ]} " ]["state" ] = USED
26672665
26682666 return x_distance , y_distance
2669-
2670-
26712667
26722668 def clear_board (self ):
26732669 """Clear the board of all drawn components."""
@@ -2681,13 +2677,20 @@ def clear_board(self):
26812677 self .current_dict_circuit .clear ()
26822678 # TODO Khalid update the Circuit instance
26832679
2684-
2685-
2686-
2687- def draw_battery (self , x_distance , y_distance , scale = 1 , width = - 1 , direction = 'HORIZONTAL' , pos_wire_end = None , neg_wire_end = None , ** kwargs ):
2680+ def draw_battery (
2681+ self ,
2682+ x_distance ,
2683+ y_distance ,
2684+ scale = 1 ,
2685+ width = - 1 ,
2686+ direction = "HORIZONTAL" ,
2687+ pos_wire_end = None ,
2688+ neg_wire_end = None ,
2689+ ** kwargs ,
2690+ ):
26882691 """
26892692 Draws a battery image at the given coordinates with two hanging wires on the left side.
2690-
2693+
26912694 Parameters:
26922695 - x_distance (int): The x-coordinate where the battery will be drawn.
26932696 - y_distance (int): The y-coordinate where the battery will be drawn.
@@ -2697,11 +2700,11 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR
26972700 - pos_wire_end (tuple): Coordinates where the positive wire should end.
26982701 - neg_wire_end (tuple): Coordinates where the negative wire should end.
26992702 - kwargs: Additional keyword arguments.
2700-
2703+
27012704 Returns:
27022705 - Tuple of (x_distance, y_distance)
27032706 """
2704- battery_id = ' _battery'
2707+ battery_id = " _battery"
27052708
27062709 # Check if battery already exists
27072710 if battery_id in self .current_dict_circuit :
@@ -2748,35 +2751,28 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR
27482751 return x_distance , y_distance
27492752
27502753 battery_obj = self .canvas .create_image (
2751- x_distance - 10 ,
2752- y_distance ,
2753- anchor = 'nw' ,
2754- image = battery_photo ,
2755- tags = (battery_id ,)
2754+ x_distance - 10 , y_distance , anchor = "nw" , image = battery_photo , tags = (battery_id ,)
27562755 )
27572756
2758- if not hasattr (self , ' image_references' ):
2757+ if not hasattr (self , " image_references" ):
27592758 self .image_references = []
27602759 self .image_references .append (battery_photo )
27612760
27622761 neg_wire_offset_x = 0 # Left edge
27632762 neg_wire_offset_y = new_height * 0.2 # 20% from the top
27642763
27652764 pos_wire_offset_x = 0 # Left edge
2766- pos_wire_offset_y = new_height * 0.8
2765+ pos_wire_offset_y = new_height * 0.8
27672766
27682767 neg_wire_start_x = x_distance + neg_wire_offset_x
27692768 neg_wire_start_y = y_distance + neg_wire_offset_y
27702769
27712770 pos_wire_start_x = x_distance + pos_wire_offset_x
27722771 pos_wire_start_y = y_distance + pos_wire_offset_y
27732772
2774- self .current_dict_circuit [battery_id ] = {
2775- 'id' : battery_id ,
2776- 'tags' : [battery_id ]
2777- }
2773+ self .current_dict_circuit [battery_id ] = {"id" : battery_id , "tags" : [battery_id ]}
27782774
2779- neg_wire_id = ' _battery_neg_wire'
2775+ neg_wire_id = " _battery_neg_wire"
27802776 if neg_wire_end :
27812777 neg_wire_end_x , neg_wire_end_y = neg_wire_end
27822778 else :
@@ -2790,10 +2786,10 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR
27902786 end_x = neg_wire_end_x + 3 ,
27912787 end_y = neg_wire_end_y + 3 ,
27922788 color = (0 , 0 , 0 ),
2793- terminal_type = ' neg'
2789+ terminal_type = " neg" ,
27942790 )
27952791
2796- pos_wire_id = ' _battery_pos_wire'
2792+ pos_wire_id = " _battery_pos_wire"
27972793 if pos_wire_end :
27982794 pos_wire_end_x , pos_wire_end_y = pos_wire_end
27992795 else :
@@ -2807,13 +2803,13 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR
28072803 end_x = pos_wire_end_x + 3 ,
28082804 end_y = pos_wire_end_y + 3 ,
28092805 color = (255 , 0 , 0 ),
2810- terminal_type = ' pos'
2806+ terminal_type = " pos" ,
28112807 )
28122808
28132809 self .canvas .tag_raise (battery_id )
28142810
2815- self .current_dict_circuit [battery_id ][' tags' ].extend (
2816- self .current_dict_circuit [pos_wire_id ][' tags' ] + self .current_dict_circuit [neg_wire_id ][' tags' ]
2811+ self .current_dict_circuit [battery_id ][" tags" ].extend (
2812+ self .current_dict_circuit [pos_wire_id ][" tags" ] + self .current_dict_circuit [neg_wire_id ][" tags" ]
28172813 )
28182814
28192815 return x_distance , y_distance
@@ -2833,23 +2829,11 @@ def draw_battery_wire(self, wire_id, start_x, start_y, end_x, end_y, color, term
28332829
28342830 # shadow line
28352831 self .canvas .create_line (
2836- start_x ,
2837- start_y ,
2838- end_x ,
2839- end_y ,
2840- fill = contour ,
2841- width = 8 * thickness ,
2842- tags = (wire_id , wire_body_shadow_tag )
2832+ start_x , start_y , end_x , end_y , fill = contour , width = 8 * thickness , tags = (wire_id , wire_body_shadow_tag )
28432833 )
28442834 # main line
28452835 self .canvas .create_line (
2846- start_x ,
2847- start_y ,
2848- end_x ,
2849- end_y ,
2850- fill = encre ,
2851- width = 4 * thickness ,
2852- tags = (wire_id , wire_body_tag )
2836+ start_x , start_y , end_x , end_y , fill = encre , width = 4 * thickness , tags = (wire_id , wire_body_tag )
28532837 )
28542838
28552839 radius = 2 * self .scale_factor
@@ -2858,37 +2842,37 @@ def draw_battery_wire(self, wire_id, start_x, start_y, end_x, end_y, color, term
28582842 end_y - radius ,
28592843 end_x + radius ,
28602844 end_y + radius ,
2861- fill = ' green' if terminal_type == ' pos' else ' black' ,
2862- outline = '' ,
2863- tags = (endpoint_tag ,)
2845+ fill = " green" if terminal_type == " pos" else " black" ,
2846+ outline = "" ,
2847+ tags = (endpoint_tag ,),
28642848 )
28652849
28662850 self .current_dict_circuit [wire_id ] = {
2867- 'id' : wire_id ,
2868- ' tags' : [wire_id , wire_body_tag , wire_body_shadow_tag , endpoint_tag ],
2869- ' start' : (start_x , start_y ),
2870- ' end' : (end_x , end_y ),
2871- ' color' : color ,
2872- ' terminal_type' : terminal_type ,
2873- ' endpoint_tag' : endpoint_tag ,
2851+ "id" : wire_id ,
2852+ " tags" : [wire_id , wire_body_tag , wire_body_shadow_tag , endpoint_tag ],
2853+ " start" : (start_x , start_y ),
2854+ " end" : (end_x , end_y ),
2855+ " color" : color ,
2856+ " terminal_type" : terminal_type ,
2857+ " endpoint_tag" : endpoint_tag ,
28742858 }
28752859
28762860 self .canvas .tag_bind (
28772861 endpoint_tag ,
28782862 "<Button-1>" ,
2879- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_click (event , wire_id )
2863+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_click (event , wire_id ),
28802864 )
28812865 self .canvas .tag_bind (
28822866 endpoint_tag ,
28832867 "<B1-Motion>" ,
2884- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_drag (event , wire_id )
2868+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_drag (event , wire_id ),
28852869 )
28862870 self .canvas .tag_bind (
28872871 endpoint_tag ,
28882872 "<ButtonRelease-1>" ,
2889- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_release (event , wire_id )
2873+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_release (event , wire_id ),
28902874 )
2891-
2875+
28922876 def create_battery_wire_endpoint (self , x , y , wire_id , terminal_type ):
28932877 """
28942878 Creates an interactive endpoint for a battery wire.
@@ -2901,28 +2885,28 @@ def create_battery_wire_endpoint(self, x, y, wire_id, terminal_type):
29012885 y - radius ,
29022886 x + radius ,
29032887 y + radius ,
2904- fill = ' green' if terminal_type == ' pos' else ' black' ,
2905- outline = '' ,
2906- tags = (endpoint_tag ,)
2888+ fill = " green" if terminal_type == " pos" else " black" ,
2889+ outline = "" ,
2890+ tags = (endpoint_tag ,),
29072891 )
29082892
29092893 self .canvas .tag_bind (
29102894 endpoint_tag ,
29112895 "<Button-1>" ,
2912- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_click (event , wire_id )
2896+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_click (event , wire_id ),
29132897 )
29142898 self .canvas .tag_bind (
29152899 endpoint_tag ,
29162900 "<B1-Motion>" ,
2917- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_drag (event , wire_id )
2901+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_drag (event , wire_id ),
29182902 )
29192903 self .canvas .tag_bind (
29202904 endpoint_tag ,
29212905 "<ButtonRelease-1>" ,
2922- lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_release (event , wire_id )
2906+ lambda event , wire_id = wire_id : self .on_battery_wire_endpoint_release (event , wire_id ),
29232907 )
29242908
2925- self .current_dict_circuit [wire_id ][' endpoint_tag' ] = endpoint_tag
2909+ self .current_dict_circuit [wire_id ][" endpoint_tag" ] = endpoint_tag
29262910
29272911 def on_battery_wire_endpoint_click (self , event , wire_id ):
29282912 """
@@ -2932,39 +2916,33 @@ def on_battery_wire_endpoint_click(self, event, wire_id):
29322916 x , y = event .x , event .y
29332917 _ , nearest_point_coord = self .find_nearest_grid_point (x , y )
29342918 old_col , old_line = nearest_point_coord
2935- self .matrix [f' { old_col } ,{ old_line } ' ][ ' state' ] = FREE
2919+ self .matrix [f" { old_col } ,{ old_line } " ][ " state" ] = FREE
29362920 self .battery_wire_drag_data = {
2937- ' wire_id' : wire_id ,
2921+ " wire_id" : wire_id ,
29382922 }
29392923
29402924 def on_battery_wire_endpoint_drag (self , event , wire_id ):
29412925 """
29422926 Handler for dragging a battery wire endpoint.
29432927 Updates the wire's end position as it's being dragged.
29442928 """
2945- if self .battery_wire_drag_data [' wire_id' ] != wire_id :
2929+ if self .battery_wire_drag_data [" wire_id" ] != wire_id :
29462930 return
29472931
29482932 wire_data = self .current_dict_circuit [wire_id ]
2949- start_x , start_y = wire_data [' start' ]
2933+ start_x , start_y = wire_data [" start" ]
29502934
29512935 wire_body_tag = f"{ wire_id } _body"
29522936 wire_body_shadow_tag = f"{ wire_id } _body_shadow"
29532937
29542938 self .canvas .coords (wire_body_shadow_tag , start_x , start_y , event .x , event .y )
29552939 self .canvas .coords (wire_body_tag , start_x , start_y , event .x , event .y )
29562940
2957- endpoint_tag = wire_data [' endpoint_tag' ]
2941+ endpoint_tag = wire_data [" endpoint_tag" ]
29582942 radius = 5 * self .scale_factor
2959- self .canvas .coords (
2960- endpoint_tag ,
2961- event .x - radius ,
2962- event .y - radius ,
2963- event .x + radius ,
2964- event .y + radius
2965- )
2943+ self .canvas .coords (endpoint_tag , event .x - radius , event .y - radius , event .x + radius , event .y + radius )
29662944
2967- wire_data [' end' ] = (event .x , event .y )
2945+ wire_data [" end" ] = (event .x , event .y )
29682946
29692947 def on_battery_wire_endpoint_release (self , event , wire_id ):
29702948 """
@@ -2979,7 +2957,7 @@ def on_battery_wire_endpoint_release(self, event, wire_id):
29792957 return
29802958
29812959 wire_data = self .current_dict_circuit [wire_id ]
2982- start_x , start_y = wire_data [' start' ]
2960+ start_x , start_y = wire_data [" start" ]
29832961 new_end_x , new_end_y = nearest_point
29842962
29852963 wire_body_tag = f"{ wire_id } _body"
@@ -2988,20 +2966,16 @@ def on_battery_wire_endpoint_release(self, event, wire_id):
29882966 self .canvas .coords (wire_body_shadow_tag , start_x , start_y , new_end_x + 3 , new_end_y + 3 )
29892967 self .canvas .coords (wire_body_tag , start_x , start_y , new_end_x + 3 , new_end_y + 3 )
29902968
2991- endpoint_tag = wire_data [' endpoint_tag' ]
2969+ endpoint_tag = wire_data [" endpoint_tag" ]
29922970 radius = 2 * self .scale_factor
29932971 self .canvas .coords (
2994- endpoint_tag ,
2995- new_end_x - radius + 3 ,
2996- new_end_y - radius + 3 ,
2997- new_end_x + radius + 3 ,
2998- new_end_y + radius + 3
2972+ endpoint_tag , new_end_x - radius + 3 , new_end_y - radius + 3 , new_end_x + radius + 3 , new_end_y + radius + 3
29992973 )
30002974
3001- wire_data [' end' ] = (new_end_x , new_end_y )
2975+ wire_data [" end" ] = (new_end_x , new_end_y )
30022976
30032977 col , line = nearest_point_coord
3004- self .matrix [f' { col } ,{ line } ' ][ ' state' ] = USED
2978+ self .matrix [f" { col } ,{ line } " ][ " state" ] = USED
30052979
30062980 self .battery_wire_drag_data = {}
30072981
@@ -3010,14 +2984,14 @@ def get_power_line_last_pins(self):
30102984 Returns a list of allowed positions (x, y, col, line) for the battery wires.
30112985 """
30122986 allowed_positions = []
3013- last_col = 61
2987+ last_col = 61
30142988 power_lines = [1 , 2 , 13 , 14 , 15 , 16 , 27 , 28 ]
30152989
30162990 for line in power_lines :
30172991 col = last_col
30182992 key = f"{ col } ,{ line } "
30192993 if key in self .matrix :
3020- x , y = self .matrix [key ]['xy' ]
2994+ x , y = self .matrix [key ]["xy" ]
30212995 x += self .id_origins ["xyOrigin" ][0 ]
30222996 y += self .id_origins ["xyOrigin" ][1 ]
30232997 allowed_positions .append ((x , y , col , line ))
@@ -3028,17 +3002,17 @@ def find_nearest_allowed_grid_point(self, x, y, allowed_positions):
30283002 Find the nearest grid point among the allowed positions to the given x, y coordinates.
30293003 Skips positions that are already USED.
30303004 """
3031- min_distance = float (' inf' )
3005+ min_distance = float (" inf" )
30323006 nearest_point = (x , y )
30333007 nearest_point_coord = None
30343008 for grid_x , grid_y , col , line in allowed_positions :
30353009 # Check if the hole is FREE
3036- hole_state = self .matrix .get (f' { col } ,{ line } ' , {}).get (' state' , None )
3010+ hole_state = self .matrix .get (f" { col } ,{ line } " , {}).get (" state" , None )
30373011 if hole_state != FREE :
30383012 continue
30393013 distance = math .hypot (x - grid_x , y - grid_y )
30403014 if distance < min_distance :
30413015 min_distance = distance
30423016 nearest_point = (grid_x , grid_y )
30433017 nearest_point_coord = (col , line )
3044- return nearest_point , nearest_point_coord
3018+ return nearest_point , nearest_point_coord
0 commit comments