~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/pychart/text_box.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
 
3
 
4
# Jockey is free software; you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License as published by the
 
6
# Free Software Foundation; either version 2, or (at your option) any
 
7
# later version.
 
8
#
 
9
# Jockey is distributed in the hope that it will be useful, but WITHOUT
 
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
12
# for more details.
 
13
#
 
14
import canvas
 
15
import line_style
 
16
import pychart_util
 
17
import fill_style
 
18
import font
 
19
import chart_object
 
20
import color
 
21
import arrow
 
22
import text_box_doc
 
23
from pychart_types import *
 
24
from types import *
 
25
 
 
26
class T(chart_object.T):
 
27
    __doc__ = text_box_doc.doc
 
28
    keys = {"text": (StringType, "???", "Text body. <<font>>"),
 
29
            "loc": (TupleType, (0,0),
 
30
                    "The location of the text box."),
 
31
            "line_style": (line_style.T, line_style.default,
 
32
                           """The line style of the surrounding frame."""),
 
33
            "fill_style": (fill_style.T, fill_style.white,
 
34
                           "Specifies the fill style of the text box."),
 
35
            "top_fudge": (UnitType, 0,
 
36
                          "The amount of space (in points) above the first line"),
 
37
            "bottom_fudge": (UnitType, 5,
 
38
                             "The amount of space below the last line"),
 
39
            "left_fudge": (UnitType, 5,
 
40
                           "The amount of space left of the box"),
 
41
            "right_fudge": (UnitType, 5,
 
42
                            "The amount of space right of the box"),
 
43
            "_arrows": (ListType, pychart_util.new_list, "The list of arrows. Not to be touched by the user directly"),
 
44
            "radius": (UnitType, 0,
 
45
                       """Radius of the four corners of the rectangle.
 
46
                       If the value is zero, a sharp-cornered
 
47
                       rectangle is drawn."""),
 
48
            "shadow": (ShadowType, None,
 
49
                       pychart_util.shadow_desc)
 
50
            }
 
51
##AUTOMATICALLY GENERATED
 
52
 
 
53
##END AUTOMATICALLY GENERATED
 
54
    def get_dimension(self):
 
55
        x = self.loc[0] - self.left_fudge
 
56
        y = self.loc[1] - self.bottom_fudge
 
57
        width = font.text_width(self.text) + self.right_fudge + self.left_fudge
 
58
        height = (font.text_height(self.text))[0] + self.top_fudge + self.bottom_fudge
 
59
        return (x, y, width, height)
 
60
    
 
61
    def choose_end_point(self, tipx, tipy):
 
62
        (x, y, width, height) = self.get_dimension()
 
63
        
 
64
        minDist = -1
 
65
        minPoint = None
 
66
        vertices = [(x, y),
 
67
                    (x+width, y),
 
68
                    (x+width, y+height),
 
69
                    (x, y+height)]
 
70
            
 
71
        if tipx >= x and tipx < x+width:
 
72
            vertices.append((tipx, y))
 
73
            vertices.append((tipx, y+height))
 
74
        if tipy >= y and tipy < y+height:
 
75
            vertices.append((x, tipy))
 
76
            vertices.append((x+width, tipy))
 
77
            
 
78
        for startPoint in vertices:
 
79
            dist = ((startPoint[0] - tipx) **2 + (startPoint[1] - tipy) **2)
 
80
            if not minPoint or dist < minDist:
 
81
                minPoint = startPoint
 
82
                minDist = dist
 
83
            
 
84
        return minPoint
 
85
    
 
86
    def add_arrow(self, tipLoc, tail=None, arrow = arrow.default):
 
87
        """This method adds a straight arrow that points to
 
88
        @var{TIPLOC}, which is a tuple of integers. @var{TAIL}
 
89
        specifies the starting point of the arrow. It is either None
 
90
        or a string consisting of the following letters: 'l', 'c',
 
91
        'r', 't', 'm,', and 'b'.  Letters 'l', 'c', or 'r' means to
 
92
        start the arrow from the left, center, or right of the text
 
93
        box, respectively. Letters 't', 'm', or 'b' means to start the
 
94
        arrow from the top, middle or bottom of the text box.  For
 
95
        example, when @samp{tail = 'tc'} then arrow is drawn from
 
96
        top-center point of the text box. ARROW specifies the style of
 
97
        the arrow. <<arrow>>.
 
98
        """
 
99
        self._arrows.append((tipLoc, tail, arrow))
 
100
        
 
101
    def draw(self, can = None):
 
102
        if can == None:
 
103
            can = canvas.default_canvas()
 
104
        x = self.loc[0]
 
105
        y = self.loc[1]
 
106
        text_width = font.text_width(self.text)
 
107
        text_height = font.text_height(self.text)[0]
 
108
        (halign, valign, angle) = font.get_align(self.text)
 
109
        
 
110
        if self.line_style or self.fill_style:
 
111
            width = text_width+self.left_fudge+self.right_fudge
 
112
            height = text_height+self.bottom_fudge+self.top_fudge
 
113
            can.round_rectangle(self.line_style, self.fill_style,
 
114
                                   x-self.left_fudge, y-self.bottom_fudge,
 
115
                                   x-self.left_fudge+width, y-self.bottom_fudge+height,
 
116
                                   self.radius, self.shadow)
 
117
 
 
118
        if halign == 'L':
 
119
            can.show(x, y, self.text)
 
120
        elif halign == 'C':
 
121
            can.show(x+text_width/2.0, y, self.text)
 
122
        elif halign == 'R':
 
123
            can.show(x+text_width, y, self.text)
 
124
        else:
 
125
            raise Exception, "Unsupported alignment (" + halign + ")"
 
126
 
 
127
        # draw arrows
 
128
        for t in self._arrows:
 
129
            (tipLoc, tail, arrow) = t
 
130
            if tail:
 
131
                (x, y, width, height) = self.get_dimension()
 
132
                origin = [x, y]
 
133
                for ch in tail:
 
134
                    if ch == 'l':
 
135
                        origin[0] = x
 
136
                    elif ch == 'c':
 
137
                        origin[0] = x+width/2.0
 
138
                    elif ch == 'r':
 
139
                        origin[0] = x+width
 
140
                    elif ch == 'b':
 
141
                        origin[1] = y
 
142
                    elif ch == 'm':
 
143
                        origin[1] = y+height/2.0
 
144
                    elif ch == 't':
 
145
                        origin[1] = y+height
 
146
                    else:
 
147
                        raise ValueError, tail +  ": unknown tail location spec."
 
148
            else:
 
149
                origin = self.choose_end_point(tipLoc[0], tipLoc[1])
 
150
            arrow.draw((origin, tipLoc), can)
 
151
            
 
152