diff --git a/chaco/axis.py b/chaco/axis.py index 0272a4ab9..e75774f99 100644 --- a/chaco/axis.py +++ b/chaco/axis.py @@ -255,31 +255,43 @@ def overlay(self, component, gc, view_bounds=None, mode="normal"): """ if not self.visible: return - self._draw_component(gc, view_bounds, mode, component) + + if not self._cache_valid: + if component is not None: + self._calculate_geometry_overlay(component) + else: + self._calculate_geometry() + self._compute_tick_positions(gc, component) + self._compute_labels(gc) + + with gc: + # slight optimization: if we set the font correctly on the + # base gc before handing it in to our title and tick labels, + # their set_font() won't have to do any work. + gc.set_font(self.tick_label_font) + + if self.axis_line_visible: + self._draw_axis_line( + gc, self._origin_point, self._end_axis_point + ) + if self.title: + self._draw_title(gc) + + self._draw_ticks(gc) + self._draw_labels(gc) + + self._cache_valid = True def _draw_overlay(self, gc, view_bounds=None, mode="normal"): """Draws the overlay layer of a component. Overrides PlotComponent. """ - self._draw_component(gc, view_bounds, mode) - - def _draw_component( - self, gc, view_bounds=None, mode="normal", component=None - ): - """Draws the component. - - This method is preserved for backwards compatibility. Overrides - PlotComponent. - """ if not self.visible: return if not self._cache_valid: - if component is not None: - self._calculate_geometry_overlay(component) - else: - self._calculate_geometry() + self._calculate_geometry() self._compute_tick_positions(gc, component) self._compute_labels(gc) diff --git a/chaco/base_xy_plot.py b/chaco/base_xy_plot.py index 102e263e4..21510c63e 100644 --- a/chaco/base_xy_plot.py +++ b/chaco/base_xy_plot.py @@ -484,12 +484,6 @@ def get_screen_points(self): def _draw_plot(self, gc, view_bounds=None, mode="normal"): """Draws the 'plot' layer.""" - self._draw_component(gc, view_bounds, mode) - - def _draw_component(self, gc, view_bounds=None, mode="normal"): - # This method should be folded into self._draw_plot(), but is here for - # backwards compatibilty with non-draw-order stuff. - pts = self.get_screen_points() self._render(gc, pts) diff --git a/chaco/grid.py b/chaco/grid.py index 77b8b10b8..efb2927a2 100644 --- a/chaco/grid.py +++ b/chaco/grid.py @@ -343,31 +343,59 @@ def _draw_overlay(self, gc, view_bounds=None, mode="normal"): Overrides PlotComponent. """ - self._draw_component(gc, view_bounds, mode) + # What we're really trying to do with a grid is plot contour lines in + # the space of the plot. In a rectangular plot, these will always be + # straight lines. + if not self.visible: + return - def overlay(self, other_component, gc, view_bounds=None, mode="normal"): - """Draws this component overlaid on another component. + if not self._cache_valid: + self._compute_ticks() - Overrides AbstractOverlay. - """ - if not self.visible: + if len(self._tick_positions) == 0: return - self._compute_ticks(other_component) - self._draw_component(gc, view_bounds, mode) - self._cache_valid = False - def _draw_component(self, gc, view_bounds=None, mode="normal"): - """Draws the component. + with gc: + gc.set_line_width(self.line_weight) + gc.set_line_dash(self.line_style_) + gc.set_stroke_color(self.line_color_) + gc.set_antialias(False) - This method is preserved for backwards compatibility. Overrides - PlotComponent. + if self.component is not None: + gc.clip_to_rect( + *(self.component.position + self.component.bounds) + ) + else: + gc.clip_to_rect(*(self.position + self.bounds)) + + gc.begin_path() + if self.orientation == "horizontal": + starts = self._tick_positions.copy() + starts[:, 0] = self._tick_extents[:, 0] + ends = self._tick_positions.copy() + ends[:, 0] = self._tick_extents[:, 1] + else: + starts = self._tick_positions.copy() + starts[:, 1] = self._tick_extents[:, 0] + ends = self._tick_positions.copy() + ends[:, 1] = self._tick_extents[:, 1] + if self.flip_axis: + starts, ends = ends, starts + gc.line_set(starts, ends) + gc.stroke_path() + + def overlay(self, other_component, gc, view_bounds=None, mode="normal"): + """Draws this component overlaid on another component. + + Overrides AbstractOverlay. """ # What we're really trying to do with a grid is plot contour lines in # the space of the plot. In a rectangular plot, these will always be # straight lines. if not self.visible: return - + self._compute_ticks(other_component) + if not self._cache_valid: self._compute_ticks() @@ -403,6 +431,8 @@ def _draw_component(self, gc, view_bounds=None, mode="normal"): gc.line_set(starts, ends) gc.stroke_path() + self._cache_valid = False + def _mapper_changed(self, old, new): if old is not None: old.observe(self.mapper_updated, "updated", remove=True) diff --git a/chaco/overlays/lasso_overlay.py b/chaco/overlays/lasso_overlay.py index 822e577d7..fb3bb4e9e 100644 --- a/chaco/overlays/lasso_overlay.py +++ b/chaco/overlays/lasso_overlay.py @@ -58,7 +58,21 @@ def overlay(self, other_component, gc, view_bounds=None, mode="normal"): with gc: c = other_component gc.clip_to_rect(c.x, c.y, c.width, c.height) - self._draw_component(gc, view_bounds, mode) + with gc: + # We may need to make map_screen more flexible in the number of + # dimensions it accepts for ths to work well. + for selection in self.lasso_selection.disjoint_selections: + points = self.component.map_screen(selection) + if len(points) == 0: + return + points = concatenate((points, points[0, newaxis]), axis=0) + gc.set_line_width(self.selection_border_width) + gc.set_line_dash(self.selection_border_dash_) + gc.set_fill_color(self.selection_fill_color_) + gc.set_stroke_color(self.selection_border_color_) + gc.set_alpha(self.selection_alpha) + gc.lines(points) + gc.draw_path() def _updated_changed_for_lasso_selection(self): self.component.invalidate_draw() @@ -68,25 +82,3 @@ def _event_state_fired_for_lasso_selection(self, val): self._draw_selection = val == "selecting" self.component.invalidate_draw() self.component.request_redraw() - - def _draw_component(self, gc, view_bounds=None, mode="normal"): - """Draws the component. - - This method is preserved for backwards compatibility with _old_draw(). - Overrides PlotComponent. - """ - with gc: - # We may need to make map_screen more flexible in the number of - # dimensions it accepts for ths to work well. - for selection in self.lasso_selection.disjoint_selections: - points = self.component.map_screen(selection) - if len(points) == 0: - return - points = concatenate((points, points[0, newaxis]), axis=0) - gc.set_line_width(self.selection_border_width) - gc.set_line_dash(self.selection_border_dash_) - gc.set_fill_color(self.selection_fill_color_) - gc.set_stroke_color(self.selection_border_color_) - gc.set_alpha(self.selection_alpha) - gc.lines(points) - gc.draw_path() diff --git a/chaco/plots/colormapped_scatterplot.py b/chaco/plots/colormapped_scatterplot.py index 79aedb0da..26f0c8114 100644 --- a/chaco/plots/colormapped_scatterplot.py +++ b/chaco/plots/colormapped_scatterplot.py @@ -131,7 +131,7 @@ def _draw_plot(self, gc, view_bounds=None, mode="normal"): # Take into account fill_alpha even if we are rendering with only two values old_color = self.color self.color = tuple(self.fill_alpha * array(self.color_)) - super()._draw_component(gc, view_bounds, mode) + super()._draw_plot(gc, view_bounds, mode) self.color = old_color else: colors = self._cached_data_pts[:, 2] diff --git a/chaco/plots/polar_line_renderer.py b/chaco/plots/polar_line_renderer.py index 3dd3fcd6c..911ffed5b 100644 --- a/chaco/plots/polar_line_renderer.py +++ b/chaco/plots/polar_line_renderer.py @@ -132,11 +132,6 @@ def _downsample(self): def _draw_plot(self, *args, **kw): """Draws the 'plot' layer.""" - # Simple compatibility with new-style rendering loop - return self._draw_component(*args, **kw) - - def _draw_component(self, gc, view_bounds=None, mode="normal"): - """Renders the component.""" self._gather_points() self._render(gc, self._cached_data_pts) diff --git a/chaco/tools/regression_lasso.py b/chaco/tools/regression_lasso.py index 48586b3da..18d060d48 100644 --- a/chaco/tools/regression_lasso.py +++ b/chaco/tools/regression_lasso.py @@ -70,8 +70,8 @@ class RegressionOverlay(LassoOverlay): ), ) - def _draw_component(self, gc, view_bounds=None, mode="normal"): - super()._draw_component(gc, view_bounds, mode) + def overlay(self, other_component, gc, view_bounds=None, mode="normal"): + super().overlay(other_component, gc, view_bounds, mode) selection = self.lasso_selection if selection.fit_params is not None: