File: graphics_context.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 (98 lines) | stat: -rw-r--r-- 3,714 bytes parent folder | download | duplicates (3)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

from __future__ import with_statement

from kiva.constants import FILL

# Relative imports
from abstract_window import AbstractWindow
from base import bounding_coordinates, coordinates_to_bounds
from kiva_graphics_context import GraphicsContext


class EnableGCMixin(object):
    """
    Subclass of Kiva GraphicsContext that provides a few more utility methods.
    Most importantly, it provides a pointer back to the window that this
    GC is being drawn to.

    This will eventually be deprecated as the follow methods are folded into
    Kiva or their use is discontinuted in Enable.
    """

    # The window that this GraphicsContext is being drawn to.  It is OK to leave
    # this as None if the graphics context is used as a backbuffer; however, in
    # such cases, it is more appropriate to use a GC from Kiva directly as opposed
    # to using the Enable one, as some draw methods may need to parent controls
    # or dialogs from the Window.
    window = None  #Instance(AbstractWindow)

    def __init__(self, *args, **kwargs):
        if kwargs.has_key("window"):
            self.window = kwargs.pop("window")
        super(EnableGCMixin, self).__init__(*args, **kwargs)
        return

    def clip_to_rect(self, x, y, width, height):
        if getattr(self, "corner_pixel_origin", True):
            super(EnableGCMixin, self).clip_to_rect(x-0.5, y-0.5, width+1, height+1)
        else:
            super(EnableGCMixin, self).clip_to_rect(x, y, width, height)

    def clear_clip(self, color, coordinates):
        "Clip and clear a Kiva graphics context to a specified area and color"
        bounds = coordinates_to_bounds(coordinates)
        self.clip_to_rect(*bounds)
        self.set_fill_color(color)
        self.draw_rect(bounds, FILL)
        return

    def clear_clip_region(self, color, update_region):
        "Clip and clear a Kiva graphics context to a specified region and color"
        bounds = coordinates_to_bounds(bounding_coordinates(update_region))
        self.clip_to_rect(*bounds)
        self.set_fill_color(color)
        for coordinates in update_region:
            bounds = coordinates_to_bounds(coordinates)
            self.begin_path()
            self.rect(*bounds)
        self.fill_path()
        return

    def alpha(self, alpha):
        raise NotImplementedError, \
            "The alpha() method is not compatible with DisplayPDF; use clear() instead."

    def stretch_draw(self, image, x, y, dx, dy):
        "Draws an image 'stretched' to fit a specified area"
        idx  = image.width()
        idy  = image.height()
        with self:
            self.clip_to_rect(x, y, dx, dy)
            cx, cy, cdx, cdy = x, y, dx, dy
            yt = cy + cdy
            xr = cx + cdx
            x += (int(cx - x) / idx) * idx
            y += (int(cy - y) / idy) * idy
            while y < yt:
                x0 = x
                while x0 < xr:
                    self.draw_image(image,(x0, y, idx, idy))
                    x0 += idx
                y += idy
        return

# Define a GraphicsContextEnable that subclasses whatever the Kiva backend's
# GraphicsContext is.
class GraphicsContextEnable(EnableGCMixin, GraphicsContext):
    pass

# Define an ImageGraphicsContextEnable that is guaranteed to be a subclass of
# an ImageGraphicsContext, regardless of the actual Kiva backend.  If the kiva
# backend is already the GraphicsContextImage, then just create an alias.
from kiva.image import GraphicsContext as GraphicsContextImage
if isinstance(GraphicsContext, GraphicsContextImage):
    ImageGraphicsContextEnable = GraphicsContextEnable
else:
    class ImageGraphicsContextEnable(EnableGCMixin, GraphicsContextImage):
        pass