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

Convert on_trait_change decorators to observe #654

Merged
merged 6 commits into from
Mar 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
11 changes: 5 additions & 6 deletions enable/savage/compliance/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
from enable.api import Component
from enable.component_editor import ComponentEditor
from traits.api import (
Any, Button, Dict, HasTraits, HTML, Instance, List, Property, Str,
on_trait_change
Any, Button, Dict, HasTraits, HTML, Instance, List, Property, Str, observe
)
from traitsui.api import (
EnumEditor, HGroup, HSplit, Item, Tabbed, VGroup, View, VSplit
Expand Down Expand Up @@ -396,10 +395,10 @@ def _get_mouse_coords(self):
else:
return "%1.3g %1.3g" % self.ch_controller.svg_coords

@on_trait_change("profile_this:profile_ended")
def _update_profiling(self, new):
if new is not None:
name, p = new
@observe("profile_this:profile_ended")
def _update_profiling(self, event):
if event.new is not None:
name, p = event.new
stats = pstats.Stats(p)
if name == "Parsing":
self.parsing_sike.stats = stats
Expand Down
6 changes: 3 additions & 3 deletions enable/savage/compliance/crosshair.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

from enable.api import BaseTool, ColorTrait, LineStyle
from traits.api import Bool, Float, HasTraits, List, Tuple, on_trait_change
from traits.api import Bool, Float, HasTraits, List, Tuple, observe


class Crosshair(BaseTool):
Expand Down Expand Up @@ -84,8 +84,8 @@ def normal_mouse_move(self, event):
self.svg_coords = event.x, y
event.handled = True

@on_trait_change("svg_coords,mouse_in")
def ensure_redraw(self):
@observe("svg_coords,mouse_in")
def ensure_redraw(self, event=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method and cell_properties_changed in a later file are never called standalone, but they are not privately named. So I made event a kwarg to protect against breaks from other callers, but this is likely overkill/unneeded. I am happy to change this to just event if a reviewer prefers.

if self.component is not None:
self.component.invalidate_and_redraw()

Expand Down
32 changes: 17 additions & 15 deletions enable/savage/compliance/sike.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from traits.api import (
Any, Bool, Constant, Dict, Event, Float, HasTraits, Instance, Int, List,
Property, Str, on_trait_change
Property, Str, observe
)
from traitsui.api import (
CodeEditor, Group, HGroup, Item, Label, TabularAdapter, TabularEditor,
Expand Down Expand Up @@ -208,12 +208,12 @@ def _adapter_default(self):
total_time=self.total_time,
)

@on_trait_change("total_time,percentages,basenames")
def _adapter_traits_changed(self, object, name, old, new):
setattr(self.adapter, name, new)
@observe("total_time,percentages,basenames")
def _adapter_traits_changed(self, event):
setattr(self.adapter, event.name, event.new)

@on_trait_change("sort_key,sort_ascending")
def _resort(self):
@observe("sort_key,sort_ascending")
def _resort(self, event=None):
self.records = self.sort_records(self.records)

def _column_clicked_changed(self, new):
Expand Down Expand Up @@ -365,17 +365,18 @@ def get_callee_map(self, records):
)
return callees

@on_trait_change("percentages,basenames")
def _adapter_traits_changed(self, object, name, old, new):
@observe("percentages,basenames")
def _adapter_traits_changed(self, event):
for obj in [
self.main_results,
self.callee_results,
self.caller_results,
]:
setattr(obj, name, new)
setattr(obj, event.name, event.new)

@on_trait_change("main_results:selected_record")
def update_sub_results(self, new):
@observe("main_results:selected_record")
def update_sub_results(self, event):
new = event.new
if new is None:
return
self.caller_results.total_time = new.cum_time
Expand Down Expand Up @@ -404,16 +405,17 @@ def update_sub_results(self, new):
else:
self.trait_set(code="", filename="", line=1)

@on_trait_change("caller_results:dclicked," "callee_results:dclicked")
def goto_record(self, new):
@observe("caller_results:dclicked," "callee_results:dclicked")
def goto_record(self, event):
new = event.new
if new is None:
return
if new.item.file_line_name in self.record_map:
record = self.record_map[new.item.file_line_name]
self.main_results.selected_record = record

@on_trait_change("stats")
def _refresh_stats(self):
@observe("stats")
def _refresh_stats(self, event=None):
""" Refresh the records from the stored Stats object.
"""
self.main_results.records = self.main_results.sort_records(
Expand Down
6 changes: 3 additions & 3 deletions enable/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Enthought library imports
from kiva.api import STROKE
from traits.api import (
Any, Bool, Enum, Float, Int, Property, Trait, on_trait_change,
Any, Bool, Enum, Float, Int, Property, Trait, observe,
)
from traitsui.api import EnumEditor

Expand Down Expand Up @@ -521,8 +521,8 @@ def _get_endcap_size(self):
def _get_tick_size(self):
return self._cached_tick_size

@on_trait_change("bounds,bounds_items")
def _update_sizes(self):
@observe("bounds.items")
def _update_sizes(self, event=None):
if self._slider_size_mode == "percent":
if self.orientation == "h":
self._cached_slider_size = int(
Expand Down
6 changes: 3 additions & 3 deletions enable/text_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Enthought library imports
from traits.api import (
Any, Array, Bool, Int, List, Property, Trait, Tuple, on_trait_change,
Any, Array, Bool, Int, List, Property, Trait, Tuple, observe,
)
from kiva.trait_defs.api import KivaFont

Expand Down Expand Up @@ -296,8 +296,8 @@ def _string_array_changed(self, old, new):
self._compute_positions()
self._update_bounds()

@on_trait_change("cell_border_width,cell_padding")
def cell_properties_changed(self):
@observe("cell_border_width,cell_padding")
def cell_properties_changed(self, event=None):
self._compute_positions()
self._update_bounds()

Expand Down
39 changes: 22 additions & 17 deletions enable/viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from enable.tools.viewport_zoom_tool import ViewportZoomTool
from kiva import affine
from traits.api import (
Any, Bool, Delegate, Enum, Float, Instance, on_trait_change,
Any, Bool, Delegate, Enum, Float, Instance, observe,
)

# Local relative imports
Expand Down Expand Up @@ -320,20 +320,25 @@ def _component_changed(self, old, new):
new.viewports.append(self)
self._update_component_view_bounds()

@on_trait_change("component:bounds")
def _component_bounds_updated(self, obj, name, old, new):
if name == "bounds":
delta_x = new[0] - old[0]
delta_y = new[1] - old[1]
elif name == "bounds_items":
delta_x = 0
delta_y = 0
if new.index == 1:
delta_y = new.added[0] - new.removed[0]
else:
delta_x = new.added[0] - new.removed[0]
if len(new.removed) == 2:
delta_y = new.added[1] - new.removed[1]
@observe("component:bounds")
def _updated_component_bounds(self, event):
new = event.new
old = event.old

delta_x = new[0] - old[0]
delta_y = new[1] - old[1]
self._adjust_view_from_component_resize(delta_x, delta_y)

@observe("component:bounds:items")
def _updated_component_bounds_items(self, event):
delta_x = 0
delta_y = 0
if event.index == 1:
delta_y = event.added[0] - event.removed[0]
else:
delta_x = event.added[0] - event.removed[0]
if len(event.removed) == 2:
delta_y = event.added[1] - event.removed[1]
self._adjust_view_from_component_resize(delta_x, delta_y)

def _bounds_changed(self, old, new):
Expand All @@ -348,8 +353,8 @@ def _bounds_changed(self, old, new):
def _bounds_items_changed(self, event):
return self._bounds_changed(None, self.bounds)

@on_trait_change("view_bounds,view_position")
def _handle_view_box_changed(self):
@observe("view_bounds,view_position")
def _handle_view_box_changed(self, event=None):
self._update_component_view_bounds()

def _get_position(self):
Expand Down