/GUI/Generic/GCanvases.py
Python | 277 lines | 198 code | 62 blank | 17 comment | 4 complexity | 3f06368373c62ba34ba5ddcaf79f6674 MD5 | raw file
1#
2# Python GUI - Drawing - Generic
3#
4
5from StdColors import black, white
6from StdFonts import application_font
7from Properties import Properties, overridable_property
8
9class Canvas(Properties):
10
11 _default_forecolor = black
12 _default_backcolor = white
13
14 pencolor = overridable_property('pencolor', "Current color for stroking paths.")
15 fillcolor = overridable_property('fillcolor', "Current color for filling paths.")
16 textcolor = overridable_property('textcolor', "Current color for drawint text.")
17 forecolor = overridable_property('forecolor', "Sets pen, fill and text colors to the same color.")
18 backcolor = overridable_property('backcolor', "Current color for erasing regions.")
19 pensize = overridable_property('pensize', "Width of pen for framing and stroking.")
20 font = overridable_property('font', "Font for drawing text.")
21 current_point = overridable_property('current_point', "The current point, or None.")
22
23 #forecolor = overridable_property('forecolor', "Sets both pencolor and fillcolor.")
24
25 def __init__(self):
26 self.newpath()
27
28 def initgraphics(self):
29 self.set_forecolor(self._default_forecolor)
30 self.set_backcolor(self._default_backcolor)
31 self.set_pensize(1)
32 self.set_font(application_font)
33
34 def set_forecolor(self, c):
35 self.pencolor = c
36 self.fillcolor = c
37 self.textcolor = c
38
39 def rmoveto(self, dx, dy):
40 x0, y0 = self._current_point()
41 self.moveto(x0 + dx, y0 + dy)
42
43 def rlineto(self, dx, dy):
44 x0, y0 = self.current_point
45 self.lineto(x0 + dx, y0 + dy)
46
47 def curve(self, sp, cp1, cp2, ep):
48 self.moveto(sp)
49 self.curveto(cp1, cp2, ep)
50
51 def rcurveto(self, cp1, cp2, ep):
52 x0, y0 = self.current_point
53 x1, y1 = cp1
54 x2, y2 = cp2
55 x3, y3 = ep
56 self.curveto(
57 (x0 + x1, y0 + y1),
58 (x0 + x2, y0 + y2),
59 (x0 + x3, y0 + y3))
60
61 def fill_stroke(self):
62 self.fill()
63 self.stroke()
64
65 # Rectangles
66
67 def _pen_inset_rect(self, rect):
68 l, t, r, b = rect
69 p = 0.5 * self.pensize
70 return (l + p, t + p, r - p, b - p)
71
72 def rect(self, rect):
73 l, t, r, b = rect
74 self.moveto(l, t)
75 self.lineto(r, t)
76 self.lineto(r, b)
77 self.lineto(l, b)
78 self.closepath()
79
80 def rect_frame(self, rect):
81 self.rect(self._pen_inset_rect(rect))
82
83 def fill_rect(self, rect):
84 self.newpath()
85 self.rect(rect)
86 self.fill()
87
88 def stroke_rect(self, rect):
89 self.newpath()
90 self.rect(rect)
91 self.stroke()
92
93 def frame_rect(self, rect):
94 self.newpath()
95 self.rect_frame(rect)
96 self.stroke()
97
98 def fill_stroke_rect(self, rect):
99 self.rect_path(rect)
100 self.fill_stroke()
101
102 def fill_frame_rect(self, rect):
103 self.fill_rect(rect)
104 self.frame_rect(rect)
105
106 def erase_rect(self, rect):
107 self.newpath()
108 self.rect(rect)
109 self.erase()
110
111 # Ovals
112
113 def oval_frame(self, rect):
114 self.oval(self._pen_inset_rect(rect))
115
116 def fill_oval(self, rect):
117 self.newpath()
118 self.oval_frame(rect)
119 self.fill()
120
121 def stroke_oval(self, rect):
122 self.newpath()
123 self.oval(rect)
124 self.stroke()
125
126 def frame_oval(self, rect):
127 self.newpath()
128 self.oval_frame(rect)
129 self.stroke()
130
131 def fill_stroke_oval(self, rect):
132 self.newpath()
133 self.oval(rect)
134 self.fill_stroke()
135
136 def fill_frame_oval(self, rect):
137 self.fill_oval(rect)
138 self.frame_oval()
139
140 def erase_oval(self, rect):
141 self.newpath()
142 self.oval(rect)
143 self.erase()
144
145 # Arcs
146
147 def _arc_path(self, c, r, a0, a1):
148# x, y = c
149# a0r = a0 * deg
150# x0 = x + r * cos(a0r)
151# y0 = y + r * sin(a0r)
152 self.newpath()
153# self.moveto(x0, y0)
154 self.arc(c, r, a0, a1)
155
156 def _arc_frame_path(self, c, r, a0, a1):
157 self._arc_path(c, r - 0.5 * self.pensize, a0, a1)
158
159 def stroke_arc(self, c, r, a0, a1):
160 self._arc_path(c, r, a0, a1)
161 self.stroke()
162
163 def frame_arc(self, c, r, a0, a1):
164 self._arc_frame_path(c, r, a0, a1)
165 self.stroke()
166
167 # Wedges
168
169 def wedge(self, c, r, a0, a1):
170 self.moveto(*c)
171 self.arc(c, r, a0, a1)
172 self.closepath()
173
174 def fill_wedge(self, c, r, a0, a1):
175 self.newpath()
176 self.wedge(c, r, a0, a1)
177 self.fill()
178
179 def stroke_wedge(self, c, r, a0, a1):
180 self.newpath()
181 self.wedge(c, r, a0, a1)
182 self.stroke()
183
184 def fill_stroke_wedge(self, c, r, a0, a1):
185 self.newpath()
186 self.wedge(c, r, a0, a1)
187 self.fill_stroke()
188
189 def erase_wedge(self, c, r, a0, a1):
190 self.newpath()
191 self.wedge(c, r, a0, a1)
192 self.erase()
193
194 # Polylines
195
196 def lines(self, points):
197 point_iter = iter(points)
198 self.moveto(*point_iter.next())
199 for p in point_iter:
200 self.lineto(*p)
201
202 def linesto(self, points):
203 for p in points:
204 self.lineto(*p)
205
206 def stroke_lines(self, points):
207 self.newpath()
208 self.lines(points)
209 self.stroke()
210
211 # Polycurves
212
213 def curves(self, points):
214 self.moveto(*points[0])
215 for i in xrange(1, len(points), 3):
216 self.curveto(*points[i:i+3])
217
218 def curvesto(self, points):
219 for i in xrange(0, len(points), 3):
220 self.curveto(*points[i:i+3])
221
222 def stroke_curves(self, points):
223 self.newpath()
224 self.curves(points)
225 self.stroke()
226
227 # Polygons
228
229 def poly(self, points):
230 self.lines(points)
231 self.closepath()
232
233 def fill_poly(self, points):
234 self.newpath()
235 self.poly(points)
236 self.fill()
237
238 def stroke_poly(self, points):
239 self.newpath()
240 self.poly(points)
241 self.stroke()
242
243 def fill_stroke_poly(self, points):
244 self.newpath()
245 self.poly(points)
246 self.fill_stroke()
247
248 def erase_poly(self, points):
249 self.newpath()
250 self.poly(points)
251 self.erase()
252
253 # Loops
254
255 def loop(self, points):
256 self.curves(points)
257 self.closepath()
258
259 def fill_loop(self, points):
260 self.newpath()
261 self.loop(points)
262 self.fill()
263
264 def stroke_loop(self, points):
265 self.newpath()
266 self.loop(points)
267 self.stroke()
268
269 def fill_stroke_loop(self, points):
270 self.newpath()
271 self.loop(points)
272 self.fill_stroke()
273
274 def erase_loop(self, points):
275 self.newpath()
276 self.loop(points)
277 self.erase()