From 18968334e5d9ff3f63bc849c46a10021a73d00ec Mon Sep 17 00:00:00 2001 From: Charles-Olivier Trudel Date: Thu, 14 Nov 2024 21:24:31 -0500 Subject: [PATCH 1/4] Periodic refresh for sidebar --- arduino_logique.py | 10 ++++++++-- sidebar.py | 40 +++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/arduino_logique.py b/arduino_logique.py index 05d4c60..bf1a8d0 100644 --- a/arduino_logique.py +++ b/arduino_logique.py @@ -83,10 +83,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 +94,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/sidebar.py b/sidebar.py index 4de2a2f..28fec33 100644 --- a/sidebar.py +++ b/sidebar.py @@ -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,23 @@ def __init__( self.create_chips_area(sidebar_frame) self.create_manage_button(sidebar_frame) + 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 +500,11 @@ 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 by reloading the chip data and redisplaying the chips. + """ + self.initialize_chip_data(self.current_dict_circuit, self.chip_images_path) + self.on_search(None) + print("Sidebar refreshed.") From 6042da8db29b0200df4dd5d0c03801db947c7972 Mon Sep 17 00:00:00 2001 From: Charles-Olivier Trudel Date: Thu, 14 Nov 2024 21:25:19 -0500 Subject: [PATCH 2/4] Formatting --- arduino_logique.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/arduino_logique.py b/arduino_logique.py index bf1a8d0..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) From 99701ecb5c9de5e5d706c62463dc958f0ecfb8dc Mon Sep 17 00:00:00 2001 From: abbou169 Date: Fri, 15 Nov 2024 12:36:18 -0500 Subject: [PATCH 3/4] improvement to the logic --- sidebar.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/sidebar.py b/sidebar.py index 28fec33..c6d2962 100644 --- a/sidebar.py +++ b/sidebar.py @@ -91,6 +91,31 @@ def __init__( self.create_chips_area(sidebar_frame) self.create_manage_button(sidebar_frame) + self.chips_images_path = chip_images_path + self.chip_files_mtimes = self.get_chips_files_mtimes() + + def get_chips_files_mtimes(self): + """ + Gets the modification times of the chip JSON files. + """ + chip_files_mtimes = {} + # Construct the correct base path relative to the sidebar.py location + chips_dir = os.path.join(os.path.dirname(__file__), "Components", "Chips") + + # Subdirectories containing JSON files + subdirs = ["Sequential", "Logical"] + + for subdir in subdirs: + folder_path = os.path.join(chips_dir, subdir) + if not os.path.isdir(folder_path): + print(f"Warning: Subdirectory '{folder_path}' does not exist.") + continue + for filename in os.listdir(folder_path): + filepath = os.path.join(folder_path, filename) + if os.path.isfile(filepath) and filename.lower().endswith(".json"): + chip_files_mtimes[filepath] = os.path.getmtime(filepath) + return chip_files_mtimes + def initialize_chip_data(self, current_dict_circuit, chip_images_path) -> None: """ Initializes the chip data for the sidebar. @@ -503,8 +528,14 @@ def on_search(self, _): def refresh(self): """ - Refreshes the sidebar by reloading the chip data and redisplaying the chips. + Refreshes the sidebar with updated chip data. """ - self.initialize_chip_data(self.current_dict_circuit, self.chip_images_path) - self.on_search(None) - print("Sidebar refreshed.") + current_mtimes = self.get_chips_files_mtimes() + if current_mtimes != self.chip_files_mtimes: + self.chip_files_mtimes = current_mtimes + self.initialize_chip_data(self.current_dict_circuit, self.chips_images_path) + self.on_search(None) + print("Sidebar refreshed with updated chips.") + else: + print("No changes detected. Sidebar not refreshed.") + From 9f086335f978aab9d41d0f38ba4566786ac5e161 Mon Sep 17 00:00:00 2001 From: Charles-Olivier Trudel Date: Fri, 15 Nov 2024 13:50:09 -0500 Subject: [PATCH 4/4] Moved get_modification_times to more logical file --- object_model/circuit_object_model.py | 13 +++++++++++ sidebar.py | 32 ++++------------------------ 2 files changed, 17 insertions(+), 28 deletions(-) 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 c6d2962..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 @@ -91,30 +91,7 @@ def __init__( self.create_chips_area(sidebar_frame) self.create_manage_button(sidebar_frame) - self.chips_images_path = chip_images_path - self.chip_files_mtimes = self.get_chips_files_mtimes() - - def get_chips_files_mtimes(self): - """ - Gets the modification times of the chip JSON files. - """ - chip_files_mtimes = {} - # Construct the correct base path relative to the sidebar.py location - chips_dir = os.path.join(os.path.dirname(__file__), "Components", "Chips") - - # Subdirectories containing JSON files - subdirs = ["Sequential", "Logical"] - - for subdir in subdirs: - folder_path = os.path.join(chips_dir, subdir) - if not os.path.isdir(folder_path): - print(f"Warning: Subdirectory '{folder_path}' does not exist.") - continue - for filename in os.listdir(folder_path): - filepath = os.path.join(folder_path, filename) - if os.path.isfile(filepath) and filename.lower().endswith(".json"): - chip_files_mtimes[filepath] = os.path.getmtime(filepath) - return chip_files_mtimes + self.chip_files_mtimes = get_chip_modification_times() def initialize_chip_data(self, current_dict_circuit, chip_images_path) -> None: """ @@ -530,12 +507,11 @@ def refresh(self): """ Refreshes the sidebar with updated chip data. """ - current_mtimes = self.get_chips_files_mtimes() + 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.chips_images_path) + 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.") -