Skip to content

Commit 9fed5c5

Browse files
authored
Merge pull request #78 from Team-Arduino-Logique/77-reloader-les-chips-dynamiquement-dans-la-sidebar
77 reloader les chips dynamiquement dans la sidebar
2 parents e100f32 + 9f08633 commit 9fed5c5

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

arduino_logique.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def main():
7070
board = Breadboard(canvas, sketcher)
7171
board.fill_matrix_1260_pts()
7272

73-
74-
7573
# Creating the toolbar instance
7674
toolbar = Toolbar(parent=win, canvas=canvas, sketcher=sketcher, current_dict_circuit=sketcher.current_dict_circuit)
7775
# Placing the secondary top bar in row=1, column=1 (spanning only the canvas area)
@@ -83,10 +81,9 @@ def main():
8381

8482
# Draw the circuit
8583
board.draw_blank_board_model(50, 10)
86-
8784

8885
# Creating the Sidebar instance after canvas, board, sketcher, component_data are defined
89-
_ = Sidebar(
86+
sidebar = Sidebar(
9087
parent=win,
9188
chip_images_path="Assets/chips",
9289
canvas=canvas,
@@ -95,6 +92,13 @@ def main():
9592
toolbar=toolbar,
9693
)
9794

95+
def refresh_sidebar():
96+
sidebar.refresh()
97+
win.after(5000, refresh_sidebar)
98+
99+
# Start the periodic refresh
100+
refresh_sidebar()
101+
98102
# Creating the Menus instance with proper references
99103
menus = Menus(
100104
parent=win,

object_model/circuit_object_model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ def get_all_available_chips() -> dict[str, Chip]:
453453
return all_chips
454454

455455

456+
def get_chip_modification_times() -> dict[str, float]:
457+
"""
458+
Returns a dictionary of chip modification times.
459+
"""
460+
chip_mod_times = {}
461+
chips_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "Components", "Chips")
462+
for root, _, files in os.walk(chips_dir):
463+
for filename in files:
464+
if filename.endswith(".json"):
465+
chip_mod_times[filename] = os.path.getmtime(os.path.join(root, filename))
466+
return chip_mod_times
467+
468+
456469
if __name__ == "__main__":
457470
available_chips = get_all_available_chips()
458471
print("--------------------LOADED CHIPS:--------------------")

sidebar.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from toolbar import Toolbar
1717
from component_sketch import ComponentSketcher
1818
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
2020

2121

2222
@dataclass
@@ -62,19 +62,8 @@ def __init__(
6262
- canvas: The canvas where the chips are placed.
6363
- sketcher: The component sketcher object.
6464
"""
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
7867
self.canvas: tk.Canvas = canvas
7968
self.sketcher: ComponentSketcher = sketcher
8069
self.toolbar = toolbar
@@ -102,6 +91,25 @@ def __init__(
10291
self.create_chips_area(sidebar_frame)
10392
self.create_manage_button(sidebar_frame)
10493

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+
105113
def load_chip_images(self, img_path) -> dict[str, tk.PhotoImage]:
106114
"""
107115
Loads chip images from the specified directory and scales them down.
@@ -494,3 +502,16 @@ def on_search(self, _):
494502
or any(query in func.__class__.__name__.lower() for func in chip_data[0].functions)
495503
]
496504
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

Comments
 (0)