Skip to content

Commit f23dc8b

Browse files
committed
fix(component editor): fix saving data to data_model
1 parent 9d196fc commit f23dc8b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

ardupilot_methodic_configurator/frontend_tkinter_component_editor.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def add_entry_or_combobox(
188188
cb.bind("<FocusOut>", lambda event, path=path: self._validate_combobox(event, path)) # type: ignore[misc]
189189
cb.bind("<KeyRelease>", lambda event, path=path: self._validate_combobox(event, path)) # type: ignore[misc]
190190
cb.bind("<Return>", lambda event, path=path: self._validate_combobox(event, path)) # type: ignore[misc]
191+
cb.bind("<ButtonRelease>", lambda event, path=path: self._validate_combobox(event, path)) # type: ignore[misc]
191192

192193
# Prevent mouse wheel from changing value when dropdown is not open
193194
def handle_mousewheel(_event: tk.Event, widget: tk.Widget = cb) -> Optional[str]:
@@ -257,15 +258,27 @@ def _validate_combobox(self, event: tk.Event, path: ComponentPath) -> bool:
257258
value = combobox.get() # Get the current value of the combobox
258259
allowed_values = combobox.cget("values") # Get the list of allowed values
259260

261+
# Events that should trigger data model update (when value is valid)
262+
should_update_data_model = event.type in {
263+
tk.EventType.FocusOut,
264+
tk.EventType.KeyPress, # Return key
265+
tk.EventType.KeyRelease, # Key release events
266+
tk.EventType.ButtonRelease, # Mouse click on dropdown item
267+
}
268+
269+
if should_update_data_model and value in allowed_values:
270+
self.data_model.set_component_value(path, value)
271+
260272
if value not in allowed_values:
261-
if (
262-
(event.type == "10" and getattr(combobox, "dropdown_is_open", False)) # FocusOut events
263-
or event.type == "2" # Return KeyPress event
273+
if ( # this is complicated because we only want to issue error messages in particular cases
274+
(event.type == tk.EventType.FocusOut and getattr(combobox, "dropdown_is_open", False)) # FocusOut events
275+
or event.type == tk.EventType.KeyPress # KeyPress event (Return key)
264276
):
265277
paths_str = ">".join(list(path))
266278
allowed_str = ", ".join(allowed_values)
267279
error_msg = _("Invalid value '{value}' for {paths_str}\nAllowed values are: {allowed_str}")
268280
show_error_message(_("Error"), error_msg.format(value=value, paths_str=paths_str, allowed_str=allowed_str))
281+
269282
combobox.configure(style="comb_input_invalid.TCombobox")
270283
return False
271284

ardupilot_methodic_configurator/frontend_tkinter_component_editor_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ def _refresh_component_display(self) -> None:
222222
for widget in self.scroll_frame.view_port.winfo_children():
223223
widget.destroy()
224224

225+
# Clear the entry_widgets dictionary since all widgets have been destroyed
226+
self.entry_widgets.clear()
227+
225228
# Repopulate the frame with widgets according to the new complexity setting
226229
self.populate_frames()
227230

tests/test_frontend_tkinter_component_editor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def test_validate_data_with_errors(self, editor_with_mocked_root: ComponentEdito
577577
editor_with_mocked_root.data_model.get_combobox_values_for_path = MagicMock(return_value=("PWM", "SBUS"))
578578
editor_with_mocked_root.data_model.validate_entry_limits = MagicMock(return_value=("Value too high", None))
579579

580-
with patch("ardupilot_methodic_configurator.frontend_tkinter_component_editor.show_error_message") as mock_error:
580+
with patch("ardupilot_methodic_configurator.frontend_tkinter_component_editor_base.show_error_message") as mock_error:
581581
result = editor_with_mocked_root.validate_data_and_highlight_errors_in_red()
582582

583583
# Should configure widgets with invalid styles
@@ -607,7 +607,7 @@ def test_validate_data_combobox_valid_after_correction(self, editor_with_mocked_
607607
editor_with_mocked_root.data_model.validate_all_data = MagicMock(return_value=(False, ["Some error"]))
608608
editor_with_mocked_root.data_model.get_combobox_values_for_path = MagicMock(return_value=("PWM", "SBUS"))
609609

610-
with patch("ardupilot_methodic_configurator.frontend_tkinter_component_editor.show_error_message"):
610+
with patch("ardupilot_methodic_configurator.frontend_tkinter_component_editor_base.show_error_message"):
611611
editor_with_mocked_root.validate_data_and_highlight_errors_in_red()
612612

613613
# Should configure combobox as valid since value is in allowed values

0 commit comments

Comments
 (0)