Skip to content

Commit

Permalink
Merge pull request #308 from mwcraig/fix-autoui-disabled-state
Browse files Browse the repository at this point in the history
Better handle disabling widgets when displaying
  • Loading branch information
mwcraig authored May 1, 2024
2 parents b617348 + 66e376d commit 967dc27
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion stellarphot/settings/custom_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ipywidgets as ipw
import traitlets as tr
from ipyautoui.autoobject import AutoObject
from ipyautoui.custom.iterable import ItemBox

from stellarphot.settings import (
Camera,
Expand Down Expand Up @@ -240,8 +241,16 @@ def _set_disable_state_nested_models(self, top, value):
State that ``disabled`` should be set to.
"""

if hasattr(top, "disabled") or isinstance(top, AutoObject):
if isinstance(top, AutoObject):
top.disabled = value
elif isinstance(top, ItemBox):
if value:
# Disabled, so do not show the add/remove buttons
top.add_remove_controls = "none"
else:
# Enabled, so show the add/remove buttons
top.add_remove_controls = "add_remove"

try:
for child in top.children:
self._set_disable_state_nested_models(child, value)
Expand Down
31 changes: 31 additions & 0 deletions stellarphot/settings/tests/test_custom_widgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ipywidgets as ipw
import pytest
from ipyautoui.custom.iterable import ItemBox, ItemControl

from stellarphot.settings import Camera, Observatory, PassbandMap, SavedSettings
from stellarphot.settings.custom_widgets import ChooseOrMakeNew, Confirm
Expand Down Expand Up @@ -187,6 +188,36 @@ def test_choosing_different_item_updates_display(self, tmp_path):
# The item widget should now have the values of the second observatory
assert Observatory(**choose_or_make_new._item_widget.value) == observatory2

def test_passband_map_buttons_are_disabled_or_enabled(self, tmp_path):
# When an existing PassbandMap is selected the add/remove buttons
# for individual rows should not be displayed.
saved = SavedSettings(_testing_path=tmp_path)
passband_map = PassbandMap(**DEFAULT_PASSBAND_MAP)
saved.add_item(passband_map)

choose_or_make_new = ChooseOrMakeNew("passband_map", _testing_path=tmp_path)

# There is no great way to get to the ItemBox widget that contains and controls
# the add/remove buttons, so we keep going down through widget children until we
# get to an ItemBox and then check that the buttons are disabled.
# Recursion is the easiest way to do that, so recurse we will..
def find_item_box(top_widget):
for kid in top_widget.children:
if isinstance(kid, ItemBox):
return kid
if hasattr(kid, "children"):
result = find_item_box(kid)
if result:
return result

item_box = find_item_box(choose_or_make_new)
assert item_box.add_remove_controls == ItemControl.none

# Next, we will click the "Edit" button and check that the buttons are enabled.
choose_or_make_new._edit_button.click()
item_box = find_item_box(choose_or_make_new)
assert item_box.add_remove_controls == ItemControl.add_remove

def test_make_passband_map(self, tmp_path):
# Make a passband map and save it, then check that it is in the dropdown
saved = SavedSettings(_testing_path=tmp_path)
Expand Down

0 comments on commit 967dc27

Please sign in to comment.