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

Replace dynamic on_trait_change with observe #590

Merged
merged 22 commits into from
Apr 2, 2021
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
6 changes: 3 additions & 3 deletions chaco/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,12 @@ def _bounds_items_changed(self, event):

def _mapper_changed(self, old, new):
if old is not None:
old.on_trait_change(self.mapper_updated, "updated", remove=True)
old.observe(self.mapper_updated, "updated", remove=True)
if new is not None:
new.on_trait_change(self.mapper_updated, "updated")
new.observe(self.mapper_updated, "updated")
self._invalidate()

def mapper_updated(self):
def mapper_updated(self, event=None):
"""
Event handler that is bound to this axis's mapper's **updated** event
"""
Expand Down
20 changes: 10 additions & 10 deletions chaco/barplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ def _orientation_changed(self):

def _index_changed(self, old, new):
if old is not None:
old.on_trait_change(self._either_data_changed, "data_changed", remove=True)
old.observe(self._either_data_updated, "data_changed", remove=True)
if new is not None:
new.on_trait_change(self._either_data_changed, "data_changed")
self._either_data_changed()
new.observe(self._either_data_updated, "data_changed")
self._either_data_updated()

def _index_direction_changed(self):
m = self.index_mapper
Expand All @@ -438,17 +438,17 @@ def _value_direction_changed(self):
m.low_pos, m.high_pos = m.high_pos, m.low_pos
self.invalidate_draw()

def _either_data_changed(self):
def _either_data_updated(self, event=None):
self.invalidate_draw()
self._cache_valid = False
self.request_redraw()

def _value_changed(self, old, new):
if old is not None:
old.on_trait_change(self._either_data_changed, "data_changed", remove=True)
old.observe(self._either_data_updated, "data_changed", remove=True)
if new is not None:
new.on_trait_change(self._either_data_changed, "data_changed")
self._either_data_changed()
new.observe(self._either_data_updated, "data_changed")
self._either_data_updated()

def _index_mapper_changed(self, old, new):
return self._either_mapper_changed(old, new)
Expand All @@ -458,12 +458,12 @@ def _value_mapper_changed(self, old, new):

def _either_mapper_changed(self, old, new):
if old is not None:
old.on_trait_change(self._mapper_updated_handler, "updated", remove=True)
old.observe(self._mapper_updated_handler, "updated", remove=True)
if new is not None:
new.on_trait_change(self._mapper_updated_handler, "updated")
new.observe(self._mapper_updated_handler, "updated")
self.invalidate_draw()

def _mapper_updated_handler(self):
def _mapper_updated_handler(self, event):
self._cache_valid = False
self.invalidate_draw()
self.request_redraw()
Expand Down
9 changes: 4 additions & 5 deletions chaco/base_1d_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,17 @@ def _high_pos_changed(self, old, new):

def _range_changed(self, old, new):
if old is not None:
old.on_trait_change(self._range_change_handler, "updated",
remove = True)
old.observe(self._range_change_handler, "updated", remove=True)
if new is not None:
new.on_trait_change(self._range_change_handler, "updated")
new.observe(self._range_change_handler, "updated")

self._cache_valid = False
self.updated = new

def _range_change_handler(self, obj, name, new):
def _range_change_handler(self, event):
"Handles the range changing; dynamically attached to our ranges"
self._cache_valid = False
self.updated = obj
self.updated = event.object

def _get_screen_bounds(self):
return (self.low_pos, self.high_pos)
Expand Down
31 changes: 12 additions & 19 deletions chaco/base_2d_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ def __init__(self, **kwargs):
self.trait_set(**kwargs_tmp)
super(Base2DPlot, self).__init__(**kwargs)
if self.index is not None:
self.index.on_trait_change(self._update_index_data,
"data_changed")
self.index.observe(self._update_index_data, "data_changed")
if self.index_mapper:
self.index_mapper.on_trait_change(self._update_index_mapper,
"updated")
self.index_mapper.observe(self._update_index_mapper, "updated")
if self.value is not None:
self.value.on_trait_change(self._update_value_data,
"data_changed")
self.value.observe(self._update_value_data, "data_changed")
# If we are not resizable, we will not get a bounds update upon layout,
# so we have to manually update our mappers
if self.resizable == "":
Expand Down Expand Up @@ -245,7 +242,7 @@ def _get_y_mapper(self):
# Private methods
#------------------------------------------------------------------------

def _update_index_mapper(self):
def _update_index_mapper(self, event=None):
""" Updates the index mapper.

Called by various trait change handlers.
Expand Down Expand Up @@ -278,15 +275,15 @@ def _update_index_mapper(self):
self.index_mapper_changed = True
self.invalidate_draw()

def _update_index_data(self):
def _update_index_data(self, event=None):
""" Updates the index data.

Called by various trait change handlers.
"""
self.index_data_changed = True
self.invalidate_draw()

def _update_value_data(self):
def _update_value_data(self, event=None):
""" Updates the value data.

Called by various trait change handlers.
Expand Down Expand Up @@ -315,25 +312,21 @@ def _origin_changed(self):

def _index_changed(self, old, new):
if old is not None:
old.on_trait_change(self._update_index_data,
"data_changed", remove=True)
old.obseve(self._update_index_data, "data_changed", remove=True)
if new is not None:
new.on_trait_change(self._update_index_data, "data_changed")
new.obseve(self._update_index_data, "data_changed")
self._update_index_data()

def _value_changed(self, old, new):
if old is not None:
old.on_trait_change(self._update_value_data,
"data_changed", remove=True)
old.observe(self._update_value_data, "data_changed", remove=True)
if new is not None:
new.on_trait_change(self._update_value_data, "data_changed")
new.observe(self._update_value_data, "data_changed")
self._update_value_data()

def _index_mapper_changed(self, old, new):
if old is not None:
old.on_trait_change(self._update_index_mapper,
"updated", remove=True)
old.observe(self._update_index_mapper, "updated", remove=True)
if new is not None:
new.on_trait_change(self._update_index_mapper, "updated")
new.observe(self._update_index_mapper, "updated")
self._update_index_mapper()

8 changes: 4 additions & 4 deletions chaco/base_contour_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BaseContourPlot(Base2DPlot):
def __init__(self, *args, **kwargs):
super(BaseContourPlot, self).__init__(*args, **kwargs)
if self.color_mapper:
self.color_mapper.on_trait_change(self._update_color_mapper, "updated")
self.color_mapper.observe(self._update_color_mapper, "updated")

def _update_levels(self):
""" Updates the levels cache. """
Expand Down Expand Up @@ -149,7 +149,7 @@ def _index_mapper_changed_fired(self):
# If the index mapper has changed, then we need to redraw
self.invalidate_and_redraw()

def _update_color_mapper(self):
def _update_color_mapper(self, event=None):
# If the color mapper has changed, then we need to recompute the
# levels and cached data associated with that.
self._level_cache_valid = False
Expand Down Expand Up @@ -177,7 +177,7 @@ def _get_color_mapper(self):
def _set_color_mapper(self, color_mapper):
# Remove the dynamic event handler from the old color mapper
if self.colors is not None and isinstance(self.colors, ColorMapper):
self.colors.on_trait_change(self._update_color_mapper, "updated", remove=True)
self.colors.observe(self._update_color_mapper, "updated", remove=True)

# Check to see if we should copy over the range as well
if color_mapper is not None:
Expand All @@ -186,7 +186,7 @@ def _set_color_mapper(self, color_mapper):

# Attach the dynamic event handler to the new color mapper
if color_mapper is not None:
color_mapper.on_trait_change(self._update_color_mapper, "updated")
color_mapper.observe(self._update_color_mapper, "updated")

self.colors = color_mapper
self._update_color_mapper()
50 changes: 25 additions & 25 deletions chaco/base_xy_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ def __init__(self, **kwtraits):
self.trait_set(**kwargs_tmp)
AbstractPlotRenderer.__init__(self, **kwtraits)
if self.index is not None:
self.index.on_trait_change(self._either_data_changed, "data_changed")
self.index.on_trait_change(self._either_metadata_changed, "metadata_changed")
self.index.observe(self._either_data_updated, "data_changed")
self.index.observe(self._either_metadata_updated, "metadata_changed")
if self.index_mapper:
self.index_mapper.on_trait_change(self._mapper_updated_handler, "updated")
self.index_mapper.observe(self._mapper_updated_handler, "updated")
if self.value is not None:
self.value.on_trait_change(self._either_data_changed, "data_changed")
self.value.on_trait_change(self._either_metadata_changed, "metadata_changed")
self.value.observe(self._either_data_updated, "data_changed")
self.value.observe(self._either_metadata_updated, "metadata_changed")
if self.value_mapper:
self.value_mapper.on_trait_change(self._mapper_updated_handler, "updated")
self.value_mapper.observe(self._mapper_updated_handler, "updated")

# If we are not resizable, we will not get a bounds update upon layout,
# so we have to manually update our mappers
Expand Down Expand Up @@ -614,33 +614,33 @@ def _orientation_changed(self):

def _index_changed(self, old, new):
if old is not None:
old.on_trait_change(self._either_data_changed, "data_changed", remove=True)
old.on_trait_change(self._either_metadata_changed, "metadata_changed",
remove=True)
old.observe(self._either_data_updated, "data_changed", remove=True)
old.observe(self._either_metadata_updated, "metadata_changed",
remove=True)
if new is not None:
new.on_trait_change(self._either_data_changed, "data_changed")
new.on_trait_change(self._either_metadata_changed, "metadata_changed")
self._either_data_changed()
new.observe(self._either_data_updated, "data_changed")
new.observe(self._either_metadata_updated, "metadata_changed")
self._either_data_updated()

def _either_data_changed(self):
def _either_data_updated(self, event=None):
self.invalidate_draw()
self._cache_valid = False
self._screen_cache_valid = False
self.request_redraw()

def _either_metadata_changed(self):
def _either_metadata_updated(self, event):
# By default, don't respond to metadata change events.
pass

def _value_changed(self, old, new):
if old is not None:
old.on_trait_change(self._either_data_changed, "data_changed", remove=True)
old.on_trait_change(self._either_metadata_changed, "metadata_changed",
remove=True)
old.observe(self._either_data_updated, "data_changed", remove=True)
old.observe(self._either_metadata_updated, "metadata_changed",
remove=True)
if new is not None:
new.on_trait_change(self._either_data_changed, "data_changed")
new.on_trait_change(self._either_metadata_changed, "metadata_changed")
self._either_data_changed()
new.observe(self._either_data_updated, "data_changed")
new.observe(self._either_metadata_updated, "metadata_changed")
self._either_data_updated()

def _origin_changed(self, old, new):
# origin switch from left to right or vice versa?
Expand Down Expand Up @@ -671,13 +671,13 @@ def _value_mapper_changed(self, old, new):

def _either_mapper_changed(self, obj, name, old, new):
if old is not None:
old.on_trait_change(self._mapper_updated_handler, "updated", remove=True)
old.observe(self._mapper_updated_handler, "updated", remove=True)
if new is not None:
new.on_trait_change(self._mapper_updated_handler, "updated")
new.observe(self._mapper_updated_handler, "updated")
self.invalidate_draw()
self._screen_cache_valid = False

def _mapper_updated_handler(self):
def _mapper_updated_handler(self, event):
self._cache_valid = False
self._screen_cache_valid = False
self.invalidate_draw()
Expand Down Expand Up @@ -710,9 +710,9 @@ def __getstate__(self):
def __setstate__(self, state):
super(BaseXYPlot, self).__setstate__(state)
if self.index is not None:
self.index.on_trait_change(self._either_data_changed, "data_changed")
self.index.observe(self._either_data_updated, "data_changed")
if self.value is not None:
self.value.on_trait_change(self._either_data_changed, "data_changed")
self.value.observe(self._either_data_updated, "data_changed")

self.invalidate_draw()
self._cache_valid = False
Expand Down
14 changes: 7 additions & 7 deletions chaco/chaco_plot_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ def init ( self, parent ):
object = self.object
if USE_DATA_UPDATE == 1:
for name in (plotitem.index, plotitem.value):
object.on_trait_change( self._update_data, name)
object.observe( self._update_data, name)
for name in (plotitem.x_label_trait, plotitem.y_label_trait):
object.on_trait_change(lambda s: self._update_axis_grids(), name)
object.observe(lambda s: self._update_axis_grids(), name)
if plotitem.type_trait not in ("", None):
object.on_trait_change(self.update_editor, plotitem.type_trait)
object.observe(self.update_editor, plotitem.type_trait)

#---------------------------------------------------------------------------
# Disposes of the contents of an editor:
Expand All @@ -261,9 +261,9 @@ def dispose(self):

if USE_DATA_UPDATE == 1:
for name in (plotitem.index, plotitem.value):
object.on_trait_change( self._update_data, name, remove = True )
object.observe( self._update_data, name, remove = True )
for name in (plotitem.type_trait,):
object.on_trait_change( self.update_editor, name, remove = True )
object.observe( self.update_editor, name, remove = True )
self._destroy_plot()
super(ChacoPlotEditor, self).dispose()

Expand All @@ -283,7 +283,7 @@ def _destroy_plot(self):
# Updates the editor when the object trait changes externally to the editor:
#---------------------------------------------------------------------------

def update_editor(self):
def update_editor(self, event=None):
""" Updates the editor when the object trait changes externally to the
editor.
"""
Expand Down Expand Up @@ -340,7 +340,7 @@ def update_editor(self):
self._container.add(plot)
self._container.request_redraw()

def _update_data(self):
def _update_data(self, event):
""" Updates the editor when the object trait changes externally to the
editor.
"""
Expand Down
16 changes: 6 additions & 10 deletions chaco/cmap_image_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ class CMapImagePlot(ImagePlot):
def __init__(self, **kwargs):
super(CMapImagePlot, self).__init__(**kwargs)
if self.value_mapper:
self.value_mapper.on_trait_change(self._update_value_mapper,
"updated")
self.value_mapper.observe(self._update_value_mapper, "updated")
if self.value:
self.value.on_trait_change(self._update_selections,
"metadata_changed")
self.value.observe(self._update_selections, "metadata_changed")

def set_value_selection(self, val):
""" Sets a range of values in the value data source as selected.
Expand Down Expand Up @@ -139,12 +137,12 @@ def _compute_cached_image(self, selection_masks=None):
ImagePlot._compute_cached_image(self, self.value.data, mapper=lambda data:
self._cmap_values(data))

def _update_value_mapper(self):
def _update_value_mapper(self, event=None):
self._mapped_image_cache_valid = False
self._image_cache_valid = False
self.invalidate_and_redraw()

def _update_selections(self):
def _update_selections(self, event=None):
self._mapped_image_cache_valid = False
self._image_cache_valid = False
self.invalidate_and_redraw()
Expand All @@ -171,10 +169,9 @@ def _set_color_mapper(self, val):

def _value_mapper_changed(self, old, new):
if old is not None:
old.on_trait_change(self._update_value_mapper,
"updated", remove=True)
old.observe(self._update_value_mapper, "updated", remove=True)
if new is not None:
new.on_trait_change(self._update_value_mapper, "updated")
new.observe(self._update_value_mapper, "updated")

if old and new:
if new.range is None and old.range is not None:
Expand All @@ -192,4 +189,3 @@ def _index_data_changed_fired(self):
def _cache_full_map_changed(self):
self._mapped_image_cache_valid = False


Loading