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

« back to all changes in this revision

Viewing changes to chaco/text_box_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:
5
5
# Enthought library imports
6
6
from enable.api import ColorTrait
7
7
from kiva.trait_defs.kiva_font_trait import KivaFont
8
 
from traits.api import Any, Enum, Int, Str, Float, Trait
 
8
from traits.api import Any, Enum, Int, Str, Float, Trait, Bool
9
9
 
10
10
# Local, relative imports
11
11
from abstract_overlay import AbstractOverlay
16
16
    """ Draws a box with text in it.
17
17
    """
18
18
 
19
 
    #### Configuration traits ##################################################
 
19
    #### Configuration traits #################################################
20
20
 
21
21
    # The text to display in the box.
22
22
    text = Str
33
33
    # The color of the outside box.
34
34
    border_color = ColorTrait("dodgerblue")
35
35
 
 
36
    # The color of the text.
 
37
    text_color = ColorTrait("black")
 
38
 
36
39
    # The thickness of box border.
37
40
    border_size = Int(1)
38
41
 
 
42
    # The border visibility. Defaults to true to duplicate previous behavior.
 
43
    border_visible = Bool(True)
 
44
 
39
45
    # Number of pixels of padding around the text within the box.
40
46
    padding = Int(5)
41
47
 
 
48
    # The maximum width of the displayed text. This affects the width of the
 
49
    # text only, not the text box, which includes margins around the text and
 
50
    # `padding`.
 
51
    # A `max_text_width` of 0.0 means that the width will not be restricted.
 
52
    max_text_width = Float(0.0)
 
53
 
42
54
    # Alignment of the text in the box:
43
55
    #
44
56
    # * "ur": upper right
51
63
    # of the text box.  Must be a sequence of length 2.
52
64
    alternate_position = Any
53
65
 
54
 
    #### Public 'AbstractOverlay' interface ####################################
 
66
    #### Public 'AbstractOverlay' interface ###################################
55
67
 
56
68
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
57
69
        """ Draws the box overlaid on another component.
66
78
        # different shapes and put the text inside it without the label
67
79
        # filling a rectangle on top of it
68
80
        label = Label(text=self.text, font=self.font, bgcolor="transparent",
 
81
                      color=self.text_color, max_width=self.max_text_width,
69
82
                      margin=5)
70
83
        width, height = label.get_width_height(gc)
71
84
 
94
107
                x = component.x + self.padding
95
108
 
96
109
        # attempt to get the box entirely within the component
97
 
        if x + width > component.width:
98
 
            x = max(0, component.width-width)
99
 
        if y + height > component.height:
100
 
            y = max(0, component.height - height)
101
 
        elif y < 0:
102
 
            y = 0
 
110
        x_min, y_min, x_max, y_max = (component.x,
 
111
                                      component.y,
 
112
                                      component.x + component.width,
 
113
                                      component.y + component.height)
 
114
        if x + width > x_max:
 
115
            x = max(x_min, x_max - width)
 
116
        if y + height > y_max:
 
117
            y = max(y_min, y_max - height)
 
118
        elif y < y_min:
 
119
            y = y_min
103
120
 
104
121
        # apply the alpha channel
105
122
        color = self.bgcolor_
118
135
            gc.set_stroke_color(self.border_color_)
119
136
            gc.set_fill_color(color)
120
137
 
121
 
            # draw a rounded rectangle
122
 
            x = y = 0
123
 
            end_radius = 8.0
124
 
            gc.begin_path()
125
 
            gc.move_to(x + end_radius, y)
126
 
            gc.arc_to(x + width, y,
127
 
                    x + width,
128
 
                    y + end_radius, end_radius)
129
 
            gc.arc_to(x + width,
130
 
                    y + height,
131
 
                    x + width - end_radius,
132
 
                    y + height, end_radius)
133
 
            gc.arc_to(x, y + height,
134
 
                    x, y,
135
 
                    end_radius)
136
 
            gc.arc_to(x, y,
137
 
                    x + width + end_radius,
138
 
                    y, end_radius)
139
 
            gc.draw_path()
 
138
            if self.border_visible:
 
139
                # draw a rounded rectangle.
 
140
                x = y = 0
 
141
                end_radius = 8.0
 
142
                gc.begin_path()
 
143
                gc.move_to(x + end_radius, y)
 
144
                gc.arc_to(x + width, y,
 
145
                          x + width,
 
146
                          y + end_radius, end_radius)
 
147
                gc.arc_to(x + width,
 
148
                          y + height,
 
149
                          x + width - end_radius,
 
150
                          y + height, end_radius)
 
151
                gc.arc_to(x, y + height,
 
152
                          x, y,
 
153
                          end_radius)
 
154
                gc.arc_to(x, y,
 
155
                          x + width + end_radius,
 
156
                          y, end_radius)
 
157
                gc.draw_path()
140
158
 
141
159
            label.draw(gc)