~ubuntu-branches/ubuntu/precise/python-chaco/precise

« back to all changes in this revision

Viewing changes to enthought/chaco/plot_label.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-12-29 02:34:05 UTC
  • Revision ID: james.westby@ubuntu.com-20081229023405-x7i4kp9mdxzmdnvu
Tags: upstream-3.0.1
ImportĀ upstreamĀ versionĀ 3.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" Defines the PlotLabel class.
 
2
"""
 
3
from enthought.kiva import font_metrics_provider
 
4
from enthought.traits.api import Delegate, Enum, Instance
 
5
 
 
6
from abstract_overlay import AbstractOverlay
 
7
from label import Label
 
8
 
 
9
 
 
10
class PlotLabel(AbstractOverlay):
 
11
    """ A label used by plots. 
 
12
    
 
13
    This class wraps a simple Label instance, and delegates some traits to it.
 
14
    """
 
15
    
 
16
    # The text of the label.
 
17
    text = Delegate("_label")
 
18
    # The color of the label text.
 
19
    color = Delegate("_label", modify=True)
 
20
    # The font for the label text.
 
21
    font = Delegate("_label")
 
22
    # The angle of rotation of the label.
 
23
    angle = Delegate("_label", "rotate_angle")
 
24
    
 
25
    #------------------------------------------------------------------------
 
26
    # Layout-related traits
 
27
    #------------------------------------------------------------------------
 
28
    
 
29
    # Horizontal justification used if the label has more horizontal space
 
30
    # than it needs.
 
31
    hjustify = Enum("center", "left", "right")
 
32
    
 
33
    # Vertical justification used if the label has more vertical space than it
 
34
    # needs.
 
35
    vjustify = Enum("center", "bottom", "top")
 
36
    
 
37
    # The position of this label relative to the object it is overlaying.
 
38
    overlay_position = Enum("top", "bottom", "left", "right", None)
 
39
    
 
40
    # Should this PlotLabel modify the padding on its underlying component
 
41
    # if there is not enough room to lay out the text?
 
42
    # FIXME: This could cause cycles in layout, so not implemented for now
 
43
    #modify_component = Bool(True)
 
44
 
 
45
    # By default, this acts like a component and will render on the main
 
46
    # "plot" layer unless its **component** attribute gets set.
 
47
    draw_layer = "plot"
 
48
 
 
49
    #------------------------------------------------------------------------
 
50
    # Private traits
 
51
    #------------------------------------------------------------------------
 
52
    
 
53
    # The label has a fixed height and can be resized horizontally. (Overrides
 
54
    # PlotComponent.)
 
55
    resizable = "h"
 
56
 
 
57
    # The Label instance this plot label is wrapping.
 
58
    _label = Instance(Label, args=())
 
59
 
 
60
    
 
61
    def __init__(self, text="", *args, **kw):
 
62
        super(PlotLabel, self).__init__(*args, **kw)
 
63
        self.text = text
 
64
        return
 
65
    
 
66
    def overlay(self, component, gc, view_bounds=None, mode="normal"):
 
67
        """ Draws this label overlaid on another component.
 
68
        
 
69
        Overrides AbstractOverlay.
 
70
        """
 
71
        self._draw_overlay(gc, view_bounds, mode)
 
72
        return
 
73
 
 
74
    def get_preferred_size(self):
 
75
        """ Returns the label's preferred size.
 
76
        
 
77
        Overrides PlotComponent.
 
78
        """
 
79
        dummy_gc = font_metrics_provider()
 
80
        size = self._label.get_bounding_box(dummy_gc)
 
81
        return size
 
82
    
 
83
    def do_layout(self):
 
84
        """ Tells this component to do layout.
 
85
        
 
86
        Overrides PlotComponent.
 
87
        """
 
88
        if self.component is not None:
 
89
            self._layout_as_overlay()
 
90
        else:
 
91
            self._layout_as_component()
 
92
        return
 
93
    
 
94
    def _draw_overlay(self, gc, view_bounds=None, mode="normal"):
 
95
        """ Draws the overlay layer of a component.
 
96
        
 
97
        Overrides PlotComponent.
 
98
        """
 
99
        try:
 
100
            # Perform justification and compute the correct offsets for
 
101
            # the label position
 
102
            width, height = self._label.get_bounding_box(gc)
 
103
            if self.hjustify == "left":
 
104
                x_offset = 0
 
105
            elif self.hjustify == "right":
 
106
                x_offset = self.width - width
 
107
            elif self.hjustify == "center":
 
108
                x_offset = int((self.width - width) / 2)
 
109
            
 
110
            if self.vjustify == "bottom":
 
111
                y_offset = 0
 
112
            elif self.vjustify == "top":
 
113
                y_offset = self.height - height
 
114
            elif self.vjustify == "center":
 
115
                y_offset = int((self.height - height) / 2)
 
116
            
 
117
            gc.save_state()
 
118
            
 
119
            # XXX: Uncomment this after we fix kiva GL backend's clip stack
 
120
            #gc.clip_to_rect(self.x, self.y, self.width, self.height)
 
121
 
 
122
            # We have to translate to our position because the label
 
123
            # tries to draw at (0,0)
 
124
            gc.translate_ctm(self.x + x_offset, self.y + y_offset)
 
125
            self._label.draw(gc)
 
126
        finally:
 
127
            gc.restore_state()
 
128
        return
 
129
    
 
130
    def _draw_plot(self, gc, view_bounds=None, mode="normal"):
 
131
        if self.component is None:
 
132
            # We are not overlaying anything else, so we should render
 
133
            # on this layer
 
134
            self._draw_overlay(gc, view_bounds, mode)
 
135
 
 
136
    def _layout_as_component(self, size=None, force=False):
 
137
        pass
 
138
    
 
139
    def _layout_as_overlay(self, size=None, force=False):
 
140
        """ Lays out the label as an overlay on another component.
 
141
        """
 
142
        if self.component is not None:
 
143
            orientation = self.overlay_position
 
144
            if orientation in ("left", "right"):
 
145
                self.y = self.component.y
 
146
                self.height = self.component.height
 
147
                if orientation == "left":
 
148
                    self.width = self.component.padding_left
 
149
                    self.x = self.component.outer_x
 
150
                elif orientation == "right":
 
151
                    self.width = self.component.padding_right
 
152
                    self.x = self.component.x2 + 1
 
153
            elif orientation in ("bottom", "top"):
 
154
                self.x = self.component.x
 
155
                self.width = self.component.width
 
156
                if orientation == "bottom":
 
157
                    self.height = self.component.padding_bottom
 
158
                    self.y = self.component.outer_y
 
159
                elif orientation == "top":
 
160
                    self.height = self.component.padding_top
 
161
                    self.y = self.component.y2 + 1
 
162
            else:
 
163
                # Leave the position alone
 
164
                pass
 
165
        return
 
166
 
 
167
    def _text_changed(self, old, new):
 
168
        self._label.text = new
 
169
        self.do_layout()
 
170
        return
 
171
 
 
172
    def _font_changed(self, old, new):
 
173
        self._label.font = new
 
174
        self.do_layout()
 
175
        return
 
176
 
 
177
    def _angle_changed(self, old, new):
 
178
        self._label.rotate_angle = new
 
179
        self.do_layout()
 
180
        return
 
181
 
 
182
    def _component_changed(self, old, new):
 
183
        if new:
 
184
            self.draw_layer = "overlay"
 
185
        else:
 
186
            self.draw_layer = "plot"
 
187
        return
 
188
 
 
189
 
 
190
 
 
191
# EOF