Skip to content

Commit

Permalink
Added on_change hook, option_labels respond to clicks, updated tests …
Browse files Browse the repository at this point in the history
…and added log files
  • Loading branch information
karandeepSJ committed Aug 1, 2018
1 parent f36ad89 commit 95a4be8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
Binary file added dipy/data/files/test_ui_checkbox.log.gz
Binary file not shown.
Binary file added dipy/data/files/test_ui_checkbox.pkl
Binary file not shown.
Binary file added dipy/data/files/test_ui_radio_button.log.gz
Binary file not shown.
Binary file added dipy/data/files/test_ui_radio_button.pkl
Binary file not shown.
96 changes: 96 additions & 0 deletions dipy/viz/tests/test_ui.py
Expand Up @@ -553,6 +553,10 @@ def test_ui_option(interactive=False):
@npt.dec.skipif(not have_vtk or skip_it)
@xvfb_it
def test_ui_checkbox(interactive=False):
filename = "test_ui_checkbox"
recording_filename = pjoin(DATA_DIR, filename + ".log.gz")
expected_events_counts_filename = pjoin(DATA_DIR, filename + ".pkl")

checkbox_test = ui.Checkbox(labels=["option 1", "option 2\nOption 2",
"option 3", "option 4"],
position=(10, 10))
Expand All @@ -569,7 +573,54 @@ def test_ui_checkbox(interactive=False):
npt.assert_equal(new_positions - old_positions,
90 * np.ones((4, 2)))

# Collect the sequence of options that have been checked in this list.
selected_options = []

def _on_change():
selected_options.append(list(checkbox_test.checked))

# Set up a callback when selection changes
checkbox_test.on_change = _on_change

event_counter = EventCounter()
event_counter.monitor(checkbox_test)

# Create a show manager and record/play events.
show_manager = window.ShowManager(size=(600, 600),
title="DIPY Checkbox")
show_manager.ren.add(checkbox_test)

# Recorded events:
# 1. Click on button of option 1.
# 2. Click on button of option 2.
# 3. Click on button of option 1.
# 4. Click on text of option 3.
# 5. Click on text of option 1.
# 6. Click on button of option 4.
# 7. Click on text of option 1.
# 8. Click on text of option 2.
# 9. Click on text of option 4.
# 10. Click on button of option 3.
show_manager.play_events_from_file(recording_filename)
expected = EventCounter.load(expected_events_counts_filename)
event_counter.check_counts(expected)

# Check if the right options were selected.
expected = [['option 1'], ['option 1', 'option 2\nOption 2'],
['option 2\nOption 2'], ['option 2\nOption 2', 'option 3'],
['option 2\nOption 2', 'option 3', 'option 1'],
['option 2\nOption 2', 'option 3', 'option 1', 'option 4'],
['option 2\nOption 2', 'option 3', 'option 4'],
['option 3', 'option 4'], ['option 3'], []]
assert len(selected_options) == len(expected)
assert_arrays_equal(selected_options, expected)
del show_manager
del checkbox_test

if interactive:
checkbox_test = ui.Checkbox(labels=["option 1", "option 2\nOption 2",
"option 3", "option 4"],
position=(100, 100))
showm = window.ShowManager(size=(600, 600))
showm.ren.add(checkbox_test)
showm.start()
Expand All @@ -578,6 +629,10 @@ def test_ui_checkbox(interactive=False):
@npt.dec.skipif(not have_vtk or skip_it)
@xvfb_it
def test_ui_radio_button(interactive=False):
filename = "test_ui_radio_button"
recording_filename = pjoin(DATA_DIR, filename + ".log.gz")
expected_events_counts_filename = pjoin(DATA_DIR, filename + ".pkl")

radio_button_test = ui.RadioButton(
labels=["option 1", "option 2\nOption 2", "option 3", "option 4"],
position=(10, 10))
Expand All @@ -594,7 +649,48 @@ def test_ui_radio_button(interactive=False):
npt.assert_equal(new_positions - old_positions,
90 * np.ones((4, 2)))

selected_option = []

def _on_change():
selected_option.append(radio_button_test.checked)

# Set up a callback when selection changes
radio_button_test.on_change = _on_change

event_counter = EventCounter()
event_counter.monitor(radio_button_test)

# Create a show manager and record/play events.
show_manager = window.ShowManager(size=(600, 600),
title="DIPY Checkbox")
show_manager.ren.add(radio_button_test)

# Recorded events:
# 1. Click on button of option 1.
# 2. Click on button of option 2.
# 2. Click on button of option 2.
# 2. Click on text of option 2.
# 3. Click on button of option 1.
# 4. Click on text of option 3.
# 6. Click on button of option 4.
# 9. Click on text of option 4.
show_manager.play_events_from_file(recording_filename)
expected = EventCounter.load(expected_events_counts_filename)
event_counter.check_counts(expected)

# Check if the right options were selected.
expected = [['option 1'], ['option 2\nOption 2'], ['option 2\nOption 2'],
['option 2\nOption 2'], ['option 1'], ['option 3'],
['option 4'], ['option 4']]
assert len(selected_option) == len(expected)
assert_arrays_equal(selected_option, expected)
del show_manager
del radio_button_test

if interactive:
radio_button_test = ui.RadioButton(
labels=["option 1", "option 2\nOption 2", "option 3", "option 4"],
position=(100, 100))
showm = window.ShowManager(size=(600, 600))
showm.ren.add(radio_button_test)
showm.start()
Expand Down
23 changes: 15 additions & 8 deletions dipy/viz/ui.py
Expand Up @@ -3072,6 +3072,8 @@ def __init__(self, labels, padding=1, font_size=18,
self._font_size = font_size
self.font_family = font_family
super(Checkbox, self).__init__(position)
self.on_change = lambda: None
self.checked = []

def _setup(self):
""" Setup this UI component.
Expand All @@ -3087,13 +3089,16 @@ def _setup(self):
(label.count('\n') + 1) * (line_spacing + 0.1) + self.padding
option.button.on_left_mouse_button_pressed = self.toggle_check
self.options.append(option)
option.button.add_callback(option.text.actor,
"LeftButtonPressEvent",
self.toggle_check)

def _get_actors(self):
""" Get the actors composing this UI component.
"""
actors = []
for option in self.options:
actors = actors + option.get_actors()
actors = actors + option.actors
return actors

def _add_to_renderer(self, ren):
Expand Down Expand Up @@ -3122,15 +3127,18 @@ def toggle_check(self, i_ren, obj, button):
The picked actor
button : :class:`Button2D`
"""
event = []
button.next_icon()
for option in self.options:
if option.button == button:
option.checked = not option.checked
if option.checked is True:
event.append(option.label)
if option.checked is True:
self.checked.append(option.label)
else:
self.checked.remove(option.label)
break

self.on_change()
i_ren.force_render()
print(event)

def _set_position(self, coords):
""" Position the lower-left corner of this UI component.
Expand Down Expand Up @@ -3212,14 +3220,13 @@ def toggle_check(self, i_ren, obj, button):
if option.checked is not True:
option.checked = True
option.button.next_icon()
event = option.label
self.checked = option.label

elif option.checked is True:
option.checked = False
option.button.next_icon()

self.on_change()
i_ren.force_render()
print(event)


class ListBox2D(UI):
Expand Down

0 comments on commit 95a4be8

Please sign in to comment.