Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle disabling widgets when displaying #308

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -239,8 +240,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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll admit I am trusting your tests because I don't know pyautoui well.

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..
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, an actual use for recursion that isn't factorials or the Fibonacci sequence or binary search...

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
Loading