diff --git a/arduino_logique.py b/arduino_logique.py index 05d4c60..78260f9 100644 --- a/arduino_logique.py +++ b/arduino_logique.py @@ -70,8 +70,6 @@ def main(): board = Breadboard(canvas, sketcher) board.fill_matrix_1260_pts() - - # Creating the toolbar instance toolbar = Toolbar(parent=win, canvas=canvas, sketcher=sketcher, current_dict_circuit=sketcher.current_dict_circuit) # Placing the secondary top bar in row=1, column=1 (spanning only the canvas area) @@ -83,10 +81,9 @@ def main(): # Draw the circuit board.draw_blank_board_model(50, 10) - # Creating the Sidebar instance after canvas, board, sketcher, component_data are defined - _ = Sidebar( + sidebar = Sidebar( parent=win, chip_images_path="Assets/chips", canvas=canvas, @@ -95,6 +92,13 @@ def main(): toolbar=toolbar, ) + def refresh_sidebar(): + sidebar.refresh() + win.after(5000, refresh_sidebar) + + # Start the periodic refresh + refresh_sidebar() + # Creating the Menus instance with proper references menus = Menus( parent=win, diff --git a/object_model/circuit_object_model.py b/object_model/circuit_object_model.py index 42978d4..29a099e 100644 --- a/object_model/circuit_object_model.py +++ b/object_model/circuit_object_model.py @@ -453,6 +453,19 @@ def get_all_available_chips() -> dict[str, Chip]: return all_chips +def get_chip_modification_times() -> dict[str, float]: + """ + Returns a dictionary of chip modification times. + """ + chip_mod_times = {} + chips_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "Components", "Chips") + for root, _, files in os.walk(chips_dir): + for filename in files: + if filename.endswith(".json"): + chip_mod_times[filename] = os.path.getmtime(os.path.join(root, filename)) + return chip_mod_times + + if __name__ == "__main__": available_chips = get_all_available_chips() print("--------------------LOADED CHIPS:--------------------") diff --git a/sidebar.py b/sidebar.py index 4de2a2f..8af9978 100644 --- a/sidebar.py +++ b/sidebar.py @@ -16,7 +16,7 @@ from toolbar import Toolbar from component_sketch import ComponentSketcher from dataCDLT import FREE, USED -from object_model.circuit_object_model import Chip, get_all_available_chips +from object_model.circuit_object_model import Chip, get_all_available_chips, get_chip_modification_times @dataclass @@ -62,19 +62,8 @@ def __init__( - canvas: The canvas where the chips are placed. - sketcher: The component sketcher object. """ - self.current_dict_circuit = current_dict_circuit - images = self.load_chip_images(chip_images_path) - self.available_chips_and_imgs: list[Tuple[Chip, tk.PhotoImage | None]] = [ - (chip, images.get(chip.package_name)) for chip in get_all_available_chips().values() - ] - # Sort the chips based on the number after 'HC' in their chip_type - self.available_chips_and_imgs.sort(key=lambda chip_img: int(chip_img[0].chip_type.split("HC")[-1])) - - # Create a reverse lookup dictionary for chip names to their index in the list - self.chip_name_to_index = { - chip.chip_type: index for index, (chip, _) in enumerate(self.available_chips_and_imgs) - } - + self.initialize_chip_data(current_dict_circuit, chip_images_path) + self.chip_images_path = chip_images_path self.canvas: tk.Canvas = canvas self.sketcher: ComponentSketcher = sketcher self.toolbar = toolbar @@ -102,6 +91,25 @@ def __init__( self.create_chips_area(sidebar_frame) self.create_manage_button(sidebar_frame) + self.chip_files_mtimes = get_chip_modification_times() + + def initialize_chip_data(self, current_dict_circuit, chip_images_path) -> None: + """ + Initializes the chip data for the sidebar. + """ + self.current_dict_circuit = current_dict_circuit + images = self.load_chip_images(chip_images_path) + self.available_chips_and_imgs: list[Tuple[Chip, tk.PhotoImage | None]] = [ + (chip, images.get(chip.package_name)) for chip in get_all_available_chips().values() + ] + # Sort the chips based on the number after 'HC' in their chip_type + self.available_chips_and_imgs.sort(key=lambda chip_img: int(chip_img[0].chip_type.split("HC")[-1])) + + # Create a reverse lookup dictionary for chip names to their index in the list + self.chip_name_to_index = { + chip.chip_type: index for index, (chip, _) in enumerate(self.available_chips_and_imgs) + } + def load_chip_images(self, img_path) -> dict[str, tk.PhotoImage]: """ Loads chip images from the specified directory and scales them down. @@ -494,3 +502,16 @@ def on_search(self, _): or any(query in func.__class__.__name__.lower() for func in chip_data[0].functions) ] self.display_chips(filtered_chips) + + def refresh(self): + """ + Refreshes the sidebar with updated chip data. + """ + current_mtimes = get_chip_modification_times() + if current_mtimes != self.chip_files_mtimes: + self.chip_files_mtimes = current_mtimes + self.initialize_chip_data(self.current_dict_circuit, self.chip_images_path) + self.on_search(None) + print("Sidebar refreshed with updated chips.") + else: + print("No changes detected. Sidebar not refreshed.")