]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DrawXXXList.py
Removed AlignIn after further thought
[wxWidgets.git] / wxPython / demo / DrawXXXList.py
1
2 from wxPython.wx import *
3 import whrandom, time
4
5 #----------------------------------------------------------------------
6
7 colours = [
8 "BLACK",
9 "BLUE",
10 "BLUE VIOLET",
11 "BROWN",
12 "CYAN",
13 "DARK GREY",
14 "DARK GREEN",
15 "GOLD",
16 "GREY",
17 "GREEN",
18 "MAGENTA",
19 "NAVY",
20 "PINK",
21 "RED",
22 "SKY BLUE",
23 "VIOLET",
24 "YELLOW",
25 ]
26
27 #----------------------------------------------------------------------
28
29 def makeRandomPoints(num, w, h):
30 pnts = []
31 for i in range(num):
32 x = whrandom.randint(0, w)
33 y = whrandom.randint(0, h)
34 pnts.append( (x,y) )
35 return pnts
36
37
38 def makeRandomLines(num, w, h):
39 pnts = []
40 for i in range(num):
41 x1 = whrandom.randint(0, w)
42 y1 = whrandom.randint(0, h)
43 x2 = whrandom.randint(0, w)
44 y2 = whrandom.randint(0, h)
45 pnts.append( (x1,y1, x2,y2) )
46 return pnts
47
48
49 def makeRandomRectangles(num, W, H):
50 rects = []
51 for i in range(num):
52 w = whrandom.randint(10, W/2)
53 h = whrandom.randint(10, H/2)
54 x = whrandom.randint(0, W - w)
55 y = whrandom.randint(0, H - h)
56 rects.append( (x, y, w, h) )
57 return rects
58
59
60 def makeRandomText(num):
61 Np = 8 # number of charcters in text
62 text = []
63 for i in range(num):
64 word = []
65 for i in range(Np):
66 c = chr( whrandom.randint(48, 122) )
67 word.append( c )
68 text.append( "".join(word) )
69 return text
70
71
72 def makeRandomPolygons(num, W, H):
73 Np = 8 # number of points per polygon
74 polys = []
75 for i in range(num):
76 poly = []
77 for i in range(Np):
78 x = whrandom.randint(0, W)
79 y = whrandom.randint(0, H)
80 poly.append( (x,y) )
81 polys.append( poly )
82 return polys
83
84
85
86 def makeRandomPens(num, cache):
87 pens = []
88 for i in range(num):
89 c = whrandom.choice(colours)
90 t = whrandom.randint(1, 4)
91 if not cache.has_key( (c, t) ):
92 cache[(c, t)] = wxPen(c, t)
93 pens.append( cache[(c, t)] )
94 return pens
95
96
97 def makeRandomBrushes(num, cache):
98 brushes = []
99 for i in range(num):
100 c = whrandom.choice(colours)
101 if not cache.has_key(c):
102 cache[c] = wxBrush(c)
103 brushes.append( cache[c] )
104 return brushes
105
106
107 def makeRandomColors(num):
108 colors = []
109 for i in range(num):
110 c = whrandom.choice(colours)
111 colors.append(wxNamedColor(c))
112 return colors
113
114
115
116 pencache = {}
117 brushcache = {}
118 points = None
119 lines = None
120 rectangles = None
121 polygons = None
122 text = None
123 pens = None
124 brushes = None
125 colors1 = None
126 colors2 = None
127
128
129 def Init(w, h, n):
130 global pencache
131 global brushcache
132 global points
133 global lines
134 global rectangles
135 global polygons
136 global text
137 global pens
138 global brushes
139 global colors1
140 global colors2
141
142 # make some lists of random shapes
143 points = makeRandomPoints(n, w, h)
144 try:
145 import Numeric
146 Apoints = Numeric.array(points)
147 except:
148 pass
149 lines = makeRandomLines(n, w, h)
150 rectangles = makeRandomRectangles(n, w, h)
151 polygons = makeRandomPolygons(n, w, h)
152 text = makeRandomText(n)
153
154 # make some random pens and brushes
155 pens = makeRandomPens(n, pencache)
156 brushes = makeRandomBrushes(n, brushcache)
157 # make some random color lists
158 colors1 = makeRandomColors(n)
159 colors2 = makeRandomColors(n)
160
161
162
163 def TestPoints(dc,log):
164 dc.BeginDrawing()
165 start = time.time()
166 dc.SetPen(wxPen("BLACK", 4))
167
168 dc.DrawPointList(points)
169 dc.DrawPointList(points, wxPen("RED", 2))
170 dc.DrawPointList(points, pens)
171
172 dc.EndDrawing()
173 log.write("DrawTime: %s seconds with DrawPointList\n" % (time.time() - start))
174
175
176 def TestArrayPoints(dc,log):
177 try:
178 import Numeric
179
180 dc.BeginDrawing()
181 start = time.time()
182 dc.SetPen(wxPen("BLACK", 1))
183 for i in range(1):
184 dc.DrawPointList(Apoints)
185 #dc.DrawPointList(Apoints, wxPen("RED", 2))
186 #dc.DrawPointList(Apoints, pens)
187 dc.EndDrawing()
188 log.write("DrawTime: %s seconds with DrawPointList an Numpy Array\n" % (time.time() - start))
189 except ImportError:
190 log.write("Couldn't import Numeric")
191 pass
192
193
194 def TestLines(dc,log):
195 dc.BeginDrawing()
196 start = time.time()
197
198 dc.SetPen(wxPen("BLACK", 2))
199 dc.DrawLineList(lines)
200 dc.DrawLineList(lines, wxPen("RED", 2))
201 dc.DrawLineList(lines, pens)
202
203 dc.EndDrawing()
204 log.write("DrawTime: %s seconds with DrawLineList\n" % (time.time() - start))
205
206
207 def TestRectangles(dc,log):
208 dc.BeginDrawing()
209 start = time.time()
210
211 dc.SetPen( wxPen("BLACK",1) )
212 dc.SetBrush( wxBrush("RED") )
213
214 dc.DrawRectangleList(rectangles)
215 dc.DrawRectangleList(rectangles,pens)
216 dc.DrawRectangleList(rectangles,pens[0],brushes)
217 dc.DrawRectangleList(rectangles,pens,brushes[0])
218 dc.DrawRectangleList(rectangles,None,brushes)
219 ## for i in range(10):
220 ## #dc.DrawRectangleList(rectangles,pens,brushes)
221 ## dc.DrawRectangleList(rectangles)
222
223 dc.EndDrawing()
224 log.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time.time() - start))
225
226
227 def TestEllipses(dc,log):
228 dc.BeginDrawing()
229 start = time.time()
230
231 dc.SetPen( wxPen("BLACK",1) )
232 dc.SetBrush( wxBrush("RED") )
233
234 dc.DrawEllipseList(rectangles)
235 dc.DrawEllipseList(rectangles,pens)
236 dc.DrawEllipseList(rectangles,pens[0],brushes)
237 dc.DrawEllipseList(rectangles,pens,brushes[0])
238 dc.DrawEllipseList(rectangles,None,brushes)
239 dc.DrawEllipseList(rectangles,pens,brushes)
240
241 dc.EndDrawing()
242 log.write("DrawTime: %s seconds with DrawEllipsesList\n" % (time.time() - start))
243
244
245 def TestRectanglesArray(dc,log):
246 try:
247 import Numeric
248 Apoints = Numeric.array(rectangles)
249
250 dc.BeginDrawing()
251 start = time.time()
252 dc.SetPen(wxPen("BLACK", 1))
253 dc.DrawRectangleList(rectangles)
254 dc.DrawRectangleList(rectangles,pens)
255 dc.DrawRectangleList(rectangles,pens[0],brushes)
256 dc.DrawRectangleList(rectangles,pens,brushes[0])
257 dc.DrawRectangleList(rectangles,None,brushes)
258 ## for i in range(10):
259 ## #dc.DrawRectangleList(rectangles,pens,brushes)
260 ## dc.DrawRectangleList(rectangles)
261
262 dc.EndDrawing()
263 log.write("DrawTime: %s seconds with DrawRectangleList and Numpy Array\n" % (time.time() - start))
264 except ImportError:
265 log.write("Couldn't import Numeric")
266 pass
267
268
269 def TestRectanglesLoop(dc,log):
270 dc.BeginDrawing()
271
272 start = time.time()
273 dc.DrawRectangleList(rectangles,pens,brushes)
274 log.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time.time() - start))
275
276 start = time.time()
277 for i in range(len(rectangles)):
278 dc.SetPen( pens[i] )
279 dc.SetBrush( brushes[i] )
280 dc.DrawRectangle(rectangles[i][0],rectangles[i][1],rectangles[i][2],rectangles[i][3])
281 dc.EndDrawing()
282 log.write("DrawTime: %s seconds with Python loop\n" % (time.time() - start))
283
284
285 def TestPolygons(dc,log):
286 dc.BeginDrawing()
287
288 start = time.time()
289 dc.SetPen(wxPen("BLACK", 1))
290 dc.DrawPolygonList(polygons)
291 dc.DrawPolygonList(polygons,pens)
292 dc.DrawPolygonList(polygons,pens[0],brushes)
293 dc.DrawPolygonList(polygons,pens,brushes[0])
294 dc.DrawPolygonList(polygons,None,brushes)
295 log.write("DrawTime: %s seconds with DrawPolygonList\n" % (time.time() - start))
296
297 dc.EndDrawing()
298
299
300 def TestText(dc,log):
301 dc.BeginDrawing()
302
303 start = time.time()
304
305 # NOTE: you need to set BackgroundMode for the background colors to be used.
306 dc.SetBackgroundMode(wxSOLID)
307 foreground = colors1
308 background = colors2
309 dc.DrawTextList(text, points, foreground, background)
310
311 log.write("DrawTime: %s seconds with DrawTextList\n" % (time.time() - start))
312
313 dc.EndDrawing()
314
315
316
317 class TestNB(wxNotebook):
318 def __init__(self, parent, id, log):
319 style = wxNB_BOTTOM
320 if wxPlatform == "__WXMAC__":
321 style = 0
322 wxNotebook.__init__(self, parent, id, style=style)
323 self.log = log
324
325 win = DrawPanel(self, TestEllipses, log)
326 self.AddPage(win, 'Ellipses')
327
328 win = DrawPanel(self, TestText, log)
329 self.AddPage(win, 'Text')
330
331 win = DrawPanel(self, TestPolygons, log)
332 self.AddPage(win, 'Polygons')
333
334 win = DrawPanel(self, TestPoints, log)
335 self.AddPage(win, 'Points')
336
337 win = DrawPanel(self, TestLines, log)
338 self.AddPage(win, 'Lines')
339
340 win = DrawPanel(self, TestRectangles, log)
341 self.AddPage(win, 'Rectangles')
342
343
344 class DrawPanel(wxPanel):
345 def __init__(self, parent, drawFun, log):
346 wxPanel.__init__(self, parent, -1)
347 self.SetBackgroundColour(wxWHITE)
348
349 self.log = log
350 self.drawFun = drawFun
351 EVT_PAINT(self, self.OnPaint)
352
353
354 def OnPaint(self, evt):
355 dc = wxPaintDC(self)
356 dc.Clear()
357 self.drawFun(dc,self.log)
358
359
360 #----------------------------------------------------------------------
361
362 def runTest(frame, nb, log):
363 w = nb.GetClientSize().width
364 h = nb.GetClientSize().height
365 if w < 600: w = 600
366 if h < 400: h = 400
367 Init(w, h, 200)
368 win = TestNB(nb, -1, log)
369 return win
370
371 #----------------------------------------------------------------------
372
373
374 overview = """\
375
376 Some methods have been added to wxDC to help with optimization of
377 drawing routines. Currently they are:
378
379 <pre>
380 DrawPointList(sequence, pens=None)
381 </pre>
382 Where sequence is a tuple, list, whatever of 2 element tuples
383 (x, y) and pens is either None, a single pen or a list of pens.
384
385 <pre>
386 DrawLineList(sequence, pens=None)
387 </pre>
388 Where sequence is a tuple, list, whatever of 4 element tuples
389 (x1,y1, x2,y2) andd pens is either None, a single pen or a list
390 of pens.
391
392 <pre>
393 DrawRectangleList(rectangles, pens=None, brushes=None)
394 </pre>
395
396
397 <pre>
398 DrawEllipseList(ellipses, pens=None, brushes=None)
399 </pre>
400
401
402 <pre>
403 DrawPolygonList(polygons, pens=None, brushes=None)
404 </pre>
405
406
407 <pre>
408 DrawTextList(textList, coords, foregrounds = None, backgrounds = None)
409 </pre>
410
411 """
412
413
414 if __name__ == '__main__':
415 import sys,os
416 import run
417 run.main(['', os.path.basename(sys.argv[0])])
418