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