File: view_bounds_test.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (80 lines) | stat: -rw-r--r-- 2,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Demonstrates how clipping of objects occurs with the view_bounds parameter to draw().
"""
from enable.example_support import DemoFrame, demo_main

from enable.api import Container, Component, Scrolled, Window
from enable.base import empty_rectangle, intersect_bounds


class Box(Component):
    def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
        with gc:
            gc.set_fill_color((1.0, 0.0, 0.0, 1.0))
            dx, dy = self.bounds
            x, y = self.position
            gc.rect(x, y, dx, dy)
            gc.fill_path()
        return


class VerboseContainer(Container):
    """
    By default a container just doesn't draw contained components if they are
    outside its view_bounds.  This subclass modifies the behavior to report
    what components are not being drawn and what their bounds are.
    """

    fit_window = False

    def _draw_container_mainlayer(self, gc, view_bounds, mode="default"):
        with gc:
            gc.set_fill_color((1.0, 1.0, 1.0, 1.0))
            gc.set_stroke_color((1.0, 1.0, 1.0, 1.0))
            gc.rect(self.x, self.y, self.width, self.height)
            gc.fill_path()

        if view_bounds:
            v = view_bounds
            new_bounds = (v[0]-self.x, v[1]-self.y, v[2], v[3])
        else:
            new_bounds = None

        with gc:
            gc.translate_ctm(*self.position)
            gc.set_stroke_color((0.0, 0.0, 0.0, 1.0))
            for component in self._components:
                # See if the component is visible:
                tmp = intersect_bounds(component.position + component.bounds,
                                       new_bounds)
                if tmp == empty_rectangle:
                    print "skipping component:", component.__class__.__name__,
                    print "\tbounds:", component.position, component.bounds
                    continue

                with gc:
                    component.draw(gc, new_bounds, mode)


class MyFrame(DemoFrame):
    def _create_window(self):

        container = VerboseContainer(auto_size=False, bounds = [800,800])
        a = Box(bounds=[50.0,50.0], position=[50.0,50.0])
        b = Box(bounds=[50.0,50.0], position=[200.0,50.0])
        c = Box(bounds=[50.0,50.0], position=[50.0,200.0])
        d = Box(bounds=[50.0,50.0], position=[200.0,200.0])
        container.add(a)
        container.add(b)
        container.add(c)
        container.add(d)
        scr = Scrolled(container, bounds=[300,300], position=[50,50],
                       fit_window=False)
        return Window(self, -1, component=scr)


if __name__ == "__main__":
    title = "Use the scroll bars to show and hide components"
    # Save demo so that it doesn't get garbage collected when run within
    # existing event loop (i.e. from ipython).
    demo = demo_main(MyFrame, title=title)