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