~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to examples/demo/canvas/transient_plot_overlay.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2014-06-01 17:04:08 UTC
  • mfrom: (7.2.5 sid)
  • Revision ID: package-import@ubuntu.com-20140601170408-m86xvdjd83a4qon0
Tags: 4.4.1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
 - Let the binary-predeb target work on the usr/lib/python* directory
   as we don't have usr/share/pyshared anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
from __future__ import with_statement
3
 
 
4
 
from enable.api import Component
5
 
from traits.api import Enum, Float, Instance, Trait, Tuple
6
 
 
7
 
from chaco.api import AbstractOverlay, BasePlotContainer
8
 
 
9
 
 
10
 
class TransientPlotOverlay(BasePlotContainer, AbstractOverlay):
11
 
    """ Allows an arbitrary plot component to be overlaid on top of another one.
12
 
    """
13
 
 
14
 
    # The PlotComponent to draw as an overlay
15
 
    overlay_component = Instance(Component)
16
 
 
17
 
    # Where this overlay should draw relative to our .component
18
 
    align = Enum("right", "left", "top", "bottom")
19
 
 
20
 
    # The amount of space between the overlaying component and the underlying
21
 
    # one.  This is either horizontal or vertical (depending on the value of
22
 
    # self.align), but is not both.
23
 
    margin = Float(10)
24
 
 
25
 
    # An offset to apply in X and Y
26
 
    offset = Trait(None, None, Tuple)
27
 
 
28
 
    # Override default values of some inherited traits
29
 
    unified_draw = True
30
 
    resizable = ""
31
 
 
32
 
    def _bounds_default(self):
33
 
        return [450, 250]
34
 
 
35
 
    def _clear_bounds(self, gc, view_bounds):
36
 
        if view_bounds is None:
37
 
            view_bounds = (0,0, self.width, self.height)
38
 
        gc.clip_to_rect(*view_bounds)
39
 
        gc.set_fill_color((1.0,1.0,1.0,1.0))
40
 
        gc.begin_path()
41
 
        gc.rect(*view_bounds)
42
 
        gc.fill_path()
43
 
 
44
 
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
45
 
        self._do_layout()
46
 
        with gc:
47
 
            self._clear_bounds(gc, view_bounds)
48
 
            self.overlay_component._draw(gc, view_bounds, mode)
49
 
 
50
 
    # TODO: Implement this more intelligently than the one in BasePlotContainer
51
 
    #def get_preferred_size(self):
52
 
    #    pass
53
 
 
54
 
    def _do_layout(self):
55
 
        component = self.component
56
 
        bounds = self.outer_bounds
57
 
 
58
 
        if self.align in ("right", "left"):
59
 
            y = component.outer_y -(bounds[1] - component.outer_height) / 2
60
 
            if self.align == "right":
61
 
                x = component.outer_x2 + self.margin
62
 
            else:
63
 
                x = component.outer_x - bounds[0] - self.margin
64
 
 
65
 
        else:   # "top", "bottom"
66
 
            x = component.outer_x -(bounds[0] - component.outer_width) / 2
67
 
            if self.align == "top":
68
 
                y = component.outer_y2 + self.margin
69
 
            else:
70
 
                y = component.outer_y - bounds[1] - self.margin
71
 
 
72
 
        if self.offset is not None:
73
 
            x += self.offset[0]
74
 
            y += self.offset[1]
75
 
 
76
 
        overlay_component = self.overlay_component
77
 
        overlay_component.outer_bounds = self.outer_bounds
78
 
        overlay_component.outer_position = [x, y]
79
 
        overlay_component._layout_needed = True
80
 
        overlay_component.do_layout()
81
 
 
82
 
    def dispatch(self, event, suffix):
83
 
        if self.visible and self.overlay_component.is_in(event.x, event.y):
84
 
            return self.overlay_component.dispatch(event, suffix)
85
 
 
86