|
16 | 16 | from toolbar import Toolbar |
17 | 17 | from component_sketch import ComponentSketcher |
18 | 18 | from dataCDLT import FREE, USED |
19 | | -from object_model.circuit_object_model import Chip, get_all_available_chips |
| 19 | +from object_model.circuit_object_model import Chip, get_all_available_chips, get_chip_modification_times |
20 | 20 |
|
21 | 21 |
|
22 | 22 | @dataclass |
@@ -62,19 +62,8 @@ def __init__( |
62 | 62 | - canvas: The canvas where the chips are placed. |
63 | 63 | - sketcher: The component sketcher object. |
64 | 64 | """ |
65 | | - self.current_dict_circuit = current_dict_circuit |
66 | | - images = self.load_chip_images(chip_images_path) |
67 | | - self.available_chips_and_imgs: list[Tuple[Chip, tk.PhotoImage | None]] = [ |
68 | | - (chip, images.get(chip.package_name)) for chip in get_all_available_chips().values() |
69 | | - ] |
70 | | - # Sort the chips based on the number after 'HC' in their chip_type |
71 | | - self.available_chips_and_imgs.sort(key=lambda chip_img: int(chip_img[0].chip_type.split("HC")[-1])) |
72 | | - |
73 | | - # Create a reverse lookup dictionary for chip names to their index in the list |
74 | | - self.chip_name_to_index = { |
75 | | - chip.chip_type: index for index, (chip, _) in enumerate(self.available_chips_and_imgs) |
76 | | - } |
77 | | - |
| 65 | + self.initialize_chip_data(current_dict_circuit, chip_images_path) |
| 66 | + self.chip_images_path = chip_images_path |
78 | 67 | self.canvas: tk.Canvas = canvas |
79 | 68 | self.sketcher: ComponentSketcher = sketcher |
80 | 69 | self.toolbar = toolbar |
@@ -102,6 +91,25 @@ def __init__( |
102 | 91 | self.create_chips_area(sidebar_frame) |
103 | 92 | self.create_manage_button(sidebar_frame) |
104 | 93 |
|
| 94 | + self.chip_files_mtimes = get_chip_modification_times() |
| 95 | + |
| 96 | + def initialize_chip_data(self, current_dict_circuit, chip_images_path) -> None: |
| 97 | + """ |
| 98 | + Initializes the chip data for the sidebar. |
| 99 | + """ |
| 100 | + self.current_dict_circuit = current_dict_circuit |
| 101 | + images = self.load_chip_images(chip_images_path) |
| 102 | + self.available_chips_and_imgs: list[Tuple[Chip, tk.PhotoImage | None]] = [ |
| 103 | + (chip, images.get(chip.package_name)) for chip in get_all_available_chips().values() |
| 104 | + ] |
| 105 | + # Sort the chips based on the number after 'HC' in their chip_type |
| 106 | + self.available_chips_and_imgs.sort(key=lambda chip_img: int(chip_img[0].chip_type.split("HC")[-1])) |
| 107 | + |
| 108 | + # Create a reverse lookup dictionary for chip names to their index in the list |
| 109 | + self.chip_name_to_index = { |
| 110 | + chip.chip_type: index for index, (chip, _) in enumerate(self.available_chips_and_imgs) |
| 111 | + } |
| 112 | + |
105 | 113 | def load_chip_images(self, img_path) -> dict[str, tk.PhotoImage]: |
106 | 114 | """ |
107 | 115 | Loads chip images from the specified directory and scales them down. |
@@ -494,3 +502,16 @@ def on_search(self, _): |
494 | 502 | or any(query in func.__class__.__name__.lower() for func in chip_data[0].functions) |
495 | 503 | ] |
496 | 504 | self.display_chips(filtered_chips) |
| 505 | + |
| 506 | + def refresh(self): |
| 507 | + """ |
| 508 | + Refreshes the sidebar with updated chip data. |
| 509 | + """ |
| 510 | + current_mtimes = get_chip_modification_times() |
| 511 | + if current_mtimes != self.chip_files_mtimes: |
| 512 | + self.chip_files_mtimes = current_mtimes |
| 513 | + self.initialize_chip_data(self.current_dict_circuit, self.chip_images_path) |
| 514 | + self.on_search(None) |
| 515 | + print("Sidebar refreshed with updated chips.") |
| 516 | + else: |
| 517 | + print("No changes detected. Sidebar not refreshed.") |
0 commit comments