Skip to content

Commit 1896833

Browse files
committed
Periodic refresh for sidebar
1 parent e100f32 commit 1896833

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

arduino_logique.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ def main():
8383

8484
# Draw the circuit
8585
board.draw_blank_board_model(50, 10)
86-
8786

8887
# Creating the Sidebar instance after canvas, board, sketcher, component_data are defined
89-
_ = Sidebar(
88+
sidebar = Sidebar(
9089
parent=win,
9190
chip_images_path="Assets/chips",
9291
canvas=canvas,
@@ -95,6 +94,13 @@ def main():
9594
toolbar=toolbar,
9695
)
9796

97+
def refresh_sidebar():
98+
sidebar.refresh()
99+
win.after(5000, refresh_sidebar)
100+
101+
# Start the periodic refresh
102+
refresh_sidebar()
103+
98104
# Creating the Menus instance with proper references
99105
menus = Menus(
100106
parent=win,

sidebar.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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,23 @@ def __init__(
10291
self.create_chips_area(sidebar_frame)
10392
self.create_manage_button(sidebar_frame)
10493

94+
def initialize_chip_data(self, current_dict_circuit, chip_images_path) -> None:
95+
"""
96+
Initializes the chip data for the sidebar.
97+
"""
98+
self.current_dict_circuit = current_dict_circuit
99+
images = self.load_chip_images(chip_images_path)
100+
self.available_chips_and_imgs: list[Tuple[Chip, tk.PhotoImage | None]] = [
101+
(chip, images.get(chip.package_name)) for chip in get_all_available_chips().values()
102+
]
103+
# Sort the chips based on the number after 'HC' in their chip_type
104+
self.available_chips_and_imgs.sort(key=lambda chip_img: int(chip_img[0].chip_type.split("HC")[-1]))
105+
106+
# Create a reverse lookup dictionary for chip names to their index in the list
107+
self.chip_name_to_index = {
108+
chip.chip_type: index for index, (chip, _) in enumerate(self.available_chips_and_imgs)
109+
}
110+
105111
def load_chip_images(self, img_path) -> dict[str, tk.PhotoImage]:
106112
"""
107113
Loads chip images from the specified directory and scales them down.
@@ -494,3 +500,11 @@ def on_search(self, _):
494500
or any(query in func.__class__.__name__.lower() for func in chip_data[0].functions)
495501
]
496502
self.display_chips(filtered_chips)
503+
504+
def refresh(self):
505+
"""
506+
Refreshes the sidebar by reloading the chip data and redisplaying the chips.
507+
"""
508+
self.initialize_chip_data(self.current_dict_circuit, self.chip_images_path)
509+
self.on_search(None)
510+
print("Sidebar refreshed.")

0 commit comments

Comments
 (0)