root/gaphor/trunk/gaphor/diagram/simpleitem.py

Revision 2337, 4.2 kB (checked in by arj..@yirdis.nl, 4 months ago)

Added backupp service.

It doesn't really work yet, though, since instancemethods can not be stored, and they're used all over the place.

Replacing them with objects is an option, but an ugly one (to java-ish)

Line 
1 """
2 Trivial drawing aids (box, line, ellipse).
3 """
4
5
6 from gaphas.item import Line as _Line
7 from gaphas.item import Element, NW
8 from gaphas.util import path_ellipse
9 from style import Style
10
11 class Line(_Line):
12
13     __style__ = {
14         'line-width': 2,
15         'line-color': (0, 0, 0, 1),
16     }
17
18     def __init__(self, id=None):
19         super(Line, self).__init__()
20         self.style = Style(Line.__style__)
21         self._id = id
22         self.fuzziness = 2
23         self._handles[0].connectable = False
24         self._handles[-1].connectable = False
25
26     id = property(lambda self: self._id, doc='Id')
27
28     def save (self, save_func):
29         save_func('matrix', tuple(self.matrix))
30         for prop in ('orthogonal', 'horizontal'):
31             save_func(prop, getattr(self, prop))
32         points = [ ]
33         for h in self.handles():
34             points.append(tuple(map(float, h.pos)))
35         save_func('points', points)
36
37     def load (self, name, value):
38         if name == 'matrix':
39             self.matrix = eval(value)
40         elif name == 'points':
41             points = eval(value)
42             for x in xrange(len(points) - 2):
43                 self.split_segment(0)
44             for i, p in enumerate(points):
45                 self.handles()[i].pos = p
46         elif name == 'horizontal':
47             self.horizontal = eval(value)
48         elif name == 'orthogonal':
49             self._load_orthogonal = eval(value)
50
51     def postload(self):
52         if hasattr(self, '_load_orthogonal'):
53             self.orthogonal = self._load_orthogonal
54             del self._load_orthogonal
55
56     def draw(self, context):
57         cr = context.cairo
58         style = self.style
59         cr.set_line_width(style.line_width)
60         cr.set_source_rgba(*style.line_color)
61         super(Line, self).draw(context)
62
63
64 class Box(Element):
65     """
66     A Box has 4 handles (for a start)::
67     
68     NW +---+ NE
69     SW +---+ SE
70     """
71
72     __style__ = {
73         'border-width': 2,
74         'border-color': (0, 0, 0, 1),
75         'fill-color': (1, 1, 1, 0),
76     }
77
78     def __init__(self, id=None):
79         super(Box, self).__init__(10, 10)
80         self.style = Style(Box.__style__)
81         self._id = id
82
83     id = property(lambda self: self._id, doc='Id')
84
85     def save(self, save_func):
86         save_func('matrix', tuple(self.matrix))
87         save_func('width', self.width)
88         save_func('height', self.height)
89
90     def load(self, name, value):
91         if name == 'matrix':
92             self.matrix = eval(value)
93         elif name == 'width':
94             self.width = eval(value)
95         elif name == 'height':
96             self.height = eval(value)
97
98     def postload(self):
99         pass
100
101     def draw(self, context):
102         cr = context.cairo
103         nw = self._handles[NW]
104         style = self.style
105         cr.rectangle(nw.x, nw.y, self.width, self.height)
106         cr.set_source_rgba(*style.fill_color)
107         cr.fill_preserve()
108         cr.set_source_rgba(*style.border_color)
109         cr.set_line_width(style.border_width)
110         cr.stroke()
111
112
113 class Ellipse(Element):
114     """
115     """
116
117     __style__ = {
118         'border-width': 2,
119         'border-color': (0, 0, 0, 1),
120         'fill-color': (1, 1, 1, 0),
121     }
122
123     def __init__(self, id=None):
124         super(Ellipse, self).__init__()
125         self.style = Style(Ellipse.__style__)
126         self._id = id
127
128     id = property(lambda self: self._id, doc='Id')
129
130     def save(self, save_func):
131         save_func('matrix', tuple(self.matrix))
132         save_func('width', self.width)
133         save_func('height', self.height)
134
135     def load(self, name, value):
136         if name == 'matrix':
137             self.matrix = eval(value)
138         elif name == 'width':
139             self.width = eval(value)
140         elif name == 'height':
141             self.height = eval(value)
142
143     def postload(self):
144         pass
145
146     def draw(self, context):
147         cr = context.cairo
148         nw = self._handles[NW]
149         style = self.style
150
151         rx = self.width / 2.
152         ry = self.height / 2.
153
154         cr.move_to(self.width, ry)
155         path_ellipse(cr, rx, ry, self.width, self.height)
156         cr.set_source_rgba(*style.fill_color)
157         cr.fill_preserve()
158         cr.set_source_rgba(*style.border_color)
159         cr.set_line_width(style.border_width)
160         cr.stroke()
161
162
163 # vim:sw=4:et:ai
Note: See TracBrowser for help on using the browser.