Skip to content

Commit 78a9554

Browse files
committed
ctrl s
1 parent cfb4dab commit 78a9554

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

menus.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ def __init__(
121121
self.serial_port = SerialPort(None, 115200, 1, None)
122122
"""The serial port configuration."""
123123

124+
self.open_file_path: str | None = None
125+
"""The file path for the current open file."""
126+
124127
# Define menu items and their corresponding dropdown options
125128
menus = {
126-
"Fichier": ["Nouveau", "Ouvrir", "Enregistrer", "Quitter"],
129+
"Fichier": ["Nouveau", "Ouvrir", "Enregistrer", "Enregistrer sous", "Quitter"],
127130
"Microcontrôleur": ["Choisir un microcontrôleur", "Table de correspondance", "Configurer le port série"],
128131
"Exporter": ["Vérifier", "Téléverser"],
129132
"Aide": ["Documentation", "À propos"],
@@ -132,7 +135,8 @@ def __init__(
132135
menu_commands = {
133136
"Nouveau": self.new_file,
134137
"Ouvrir": self.open_file,
135-
"Enregistrer": self.save_file,
138+
"Enregistrer": lambda: self.save_file(False),
139+
"Enregistrer sous": self.save_file,
136140
"Quitter": self.parent.quit,
137141
"Configurer le port série": self.configure_ports,
138142
"Table de correspondance": self.show_correspondence_table,
@@ -158,6 +162,8 @@ def __init__(
158162
self.parent.bind("<Button-1>", self.close_dropdown, add="+")
159163
self.canvas.bind("<Button-1>", self.close_dropdown, add="+")
160164

165+
self.parent.bind("<Control-s>", lambda _: self.save_file(False), add="+")
166+
161167
def select_microcontroller(self):
162168
"""Handler for microcontroller selection."""
163169
# Create a new top-level window for the dialog
@@ -405,6 +411,7 @@ def close_dropdown(self, event):
405411
def new_file(self):
406412
"""Handler for the 'New' menu item."""
407413
# Clear the canvas and reset the circuit
414+
self.open_file_path = None
408415
self.board.sketcher.clear_board()
409416
self.board.fill_matrix_1260_pts()
410417
self.board.draw_blank_board_model()
@@ -456,6 +463,7 @@ def open_file(self):
456463

457464
print(f"Unspecified component: {key}")
458465
messagebox.showinfo("Ouvrir un fichier", f"Circuit chargé depuis {file_path}")
466+
self.open_file_path = file_path
459467
except Exception as e:
460468
print(f"Error loading file: {e}")
461469
messagebox.showerror(
@@ -521,12 +529,15 @@ def load_io(self, io_data):
521529
]
522530
self.board.sketcher.circuit(x_o, y_o, model=model_io)
523531

524-
def save_file(self):
532+
def save_file(self, prompt_for_path: bool = True):
525533
"""Handler for the 'Save' menu item."""
526534
print("Save File")
527-
file_path = filedialog.asksaveasfilename(
528-
defaultextension=".json", filetypes=[("JSON files", "*.json"), ("All files", "*.*")]
529-
)
535+
if prompt_for_path or not self.open_file_path:
536+
file_path = filedialog.asksaveasfilename(
537+
defaultextension=".json", filetypes=[("JSON files", "*.json"), ("All files", "*.*")]
538+
)
539+
else:
540+
file_path = self.open_file_path
530541
if file_path:
531542
try:
532543
circuit_data = deepcopy(self.current_dict_circuit)
@@ -546,6 +557,7 @@ def save_file(self):
546557
json.dump(circuit_data, file, indent=4)
547558
print(f"Circuit saved to {file_path}")
548559
messagebox.showinfo("Sauvegarde réussie", f"Circuit sauvegardé dans {file_path}")
560+
self.open_file_path = file_path
549561
except (TypeError, KeyError) as e:
550562
print(f"Error saving file: {e}")
551563
messagebox.showerror(

0 commit comments

Comments
 (0)