]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/DrawXXXList.py
The great wxVScrolledWindow refactoring: allow using it both horizontal and
[wxWidgets.git] / wxPython / demo / DrawXXXList.py
CommitLineData
8fa876ca
RD
1
2import random
3import time
4
5import wx
9d37f964
RD
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 = []
8fa876ca 33
9d37f964 34 for i in range(num):
8fa876ca
RD
35 x = random.randint(0, w)
36 y = random.randint(0, h)
9d37f964 37 pnts.append( (x,y) )
8fa876ca 38
9d37f964
RD
39 return pnts
40
41
42def makeRandomLines(num, w, h):
43 pnts = []
8fa876ca 44
9d37f964 45 for i in range(num):
8fa876ca
RD
46 x1 = random.randint(0, w)
47 y1 = random.randint(0, h)
48 x2 = random.randint(0, w)
49 y2 = random.randint(0, h)
9d37f964 50 pnts.append( (x1,y1, x2,y2) )
8fa876ca 51
9d37f964
RD
52 return pnts
53
54
1e4a197e
RD
55def makeRandomRectangles(num, W, H):
56 rects = []
8fa876ca 57
1e4a197e 58 for i in range(num):
8fa876ca
RD
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)
1e4a197e 63 rects.append( (x, y, w, h) )
8fa876ca 64
1e4a197e
RD
65 return rects
66
67
68def makeRandomText(num):
8b9a4190 69 Np = 8 # number of characters in text
1e4a197e 70 text = []
8fa876ca 71
1e4a197e
RD
72 for i in range(num):
73 word = []
8fa876ca 74
1e4a197e 75 for i in range(Np):
8fa876ca 76 c = chr( random.randint(48, 122) )
1e4a197e 77 word.append( c )
8fa876ca 78
1e4a197e 79 text.append( "".join(word) )
8fa876ca 80
1e4a197e
RD
81 return text
82
83
84def makeRandomPolygons(num, W, H):
85 Np = 8 # number of points per polygon
86 polys = []
8fa876ca 87
1e4a197e
RD
88 for i in range(num):
89 poly = []
8fa876ca 90
1e4a197e 91 for i in range(Np):
8fa876ca
RD
92 x = random.randint(0, W)
93 y = random.randint(0, H)
1e4a197e 94 poly.append( (x,y) )
8fa876ca 95
1e4a197e 96 polys.append( poly )
1e4a197e 97
8fa876ca 98 return polys
1e4a197e
RD
99
100
9d37f964
RD
101def makeRandomPens(num, cache):
102 pens = []
8fa876ca 103
9d37f964 104 for i in range(num):
8fa876ca
RD
105 c = random.choice(colours)
106 t = random.randint(1, 4)
107
9d37f964 108 if not cache.has_key( (c, t) ):
8fa876ca
RD
109 cache[(c, t)] = wx.Pen(c, t)
110
9d37f964 111 pens.append( cache[(c, t)] )
8fa876ca 112
9d37f964
RD
113 return pens
114
115
1e4a197e
RD
116def makeRandomBrushes(num, cache):
117 brushes = []
8fa876ca 118
1e4a197e 119 for i in range(num):
8fa876ca
RD
120 c = random.choice(colours)
121
1e4a197e 122 if not cache.has_key(c):
8fa876ca
RD
123 cache[c] = wx.Brush(c)
124
1e4a197e 125 brushes.append( cache[c] )
8fa876ca 126
1e4a197e 127 return brushes
9d37f964 128
9d37f964 129
1e4a197e
RD
130def makeRandomColors(num):
131 colors = []
8fa876ca 132
1e4a197e 133 for i in range(num):
372bde9b
RD
134 c = random.choice(colours)
135 colors.append(wx.NamedColour(c))
1e4a197e 136 return colors
9d37f964 137
9d37f964 138
9d37f964 139
1e4a197e
RD
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)
8fa876ca 168
1e4a197e
RD
169 try:
170 import Numeric
171 Apoints = Numeric.array(points)
172 except:
173 pass
8fa876ca 174
1e4a197e
RD
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()
8fa876ca 192 dc.SetPen(wx.Pen("BLACK", 4))
1e4a197e
RD
193
194 dc.DrawPointList(points)
8fa876ca 195 dc.DrawPointList(points, wx.Pen("RED", 2))
1e4a197e
RD
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
9d37f964 205
9d37f964
RD
206 dc.BeginDrawing()
207 start = time.time()
8fa876ca
RD
208 dc.SetPen(wx.Pen("BLACK", 1))
209
1e4a197e
RD
210 for i in range(1):
211 dc.DrawPointList(Apoints)
8fa876ca
RD
212
213 #dc.DrawPointList(Apoints, wx.Pen("RED", 2))
1e4a197e
RD
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
8fa876ca 226 dc.SetPen(wx.Pen("BLACK", 2))
1e4a197e 227 dc.DrawLineList(lines)
8fa876ca 228 dc.DrawLineList(lines, wx.Pen("RED", 2))
1e4a197e
RD
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
8fa876ca
RD
239 dc.SetPen( wx.Pen("BLACK",1) )
240 dc.SetBrush( wx.Brush("RED") )
9d37f964 241
1e4a197e
RD
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
8fa876ca
RD
259 dc.SetPen( wx.Pen("BLACK",1) )
260 dc.SetBrush( wx.Brush("RED") )
1e4a197e
RD
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()
8fa876ca 280 dc.SetPen(wx.Pen("BLACK", 1))
1e4a197e
RD
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)
9d37f964
RD
289
290 dc.EndDrawing()
1e4a197e
RD
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()
8fa876ca 305
1e4a197e
RD
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])
8fa876ca 310
1e4a197e
RD
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()
8fa876ca 319 dc.SetPen(wx.Pen("BLACK", 1))
1e4a197e
RD
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.
8fa876ca 336 dc.SetBackgroundMode(wx.SOLID)
1e4a197e
RD
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
8fa876ca 347class TestNB(wx.Notebook):
1e4a197e 348 def __init__(self, parent, id, log):
8fa876ca
RD
349 style = wx.NB_BOTTOM
350
351 if wx.Platform == "__WXMAC__":
1e4a197e 352 style = 0
8fa876ca
RD
353
354 wx.Notebook.__init__(self, parent, id, style=style)
1e4a197e
RD
355 self.log = log
356
8fa876ca 357 # Initialize our various samples and add them to the notebook.
1e4a197e
RD
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
8fa876ca
RD
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):
1e4a197e 380 def __init__(self, parent, drawFun, log):
8fa876ca
RD
381 wx.Panel.__init__(self, parent, -1)
382 self.SetBackgroundColour(wx.WHITE)
1e4a197e
RD
383
384 self.log = log
385 self.drawFun = drawFun
8fa876ca 386 self.Bind(wx.EVT_PAINT, self.OnPaint)
1e4a197e
RD
387
388
389 def OnPaint(self, evt):
8fa876ca 390 dc = wx.PaintDC(self)
1e4a197e
RD
391 dc.Clear()
392 self.drawFun(dc,self.log)
393
9d37f964
RD
394
395#----------------------------------------------------------------------
396
397def runTest(frame, nb, log):
398 w = nb.GetClientSize().width
399 h = nb.GetClientSize().height
1e4a197e
RD
400 if w < 600: w = 600
401 if h < 400: h = 400
402 Init(w, h, 200)
403 win = TestNB(nb, -1, log)
9d37f964
RD
404 return win
405
406#----------------------------------------------------------------------
407
408
409overview = """\
410
8fa876ca 411Some methods have been added to wx.DC to help with optimization of
9d37f964
RD
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
8b9a4190 424 (x1,y1, x2,y2) and pens is either None, a single pen or a list
9d37f964
RD
425 of pens.
426
1e4a197e
RD
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
9d37f964 446"""
1e4a197e
RD
447
448
449if __name__ == '__main__':
450 import sys,os
451 import run
8eca4fef 452 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
1e4a197e 453