]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/DrawXXXList.py
newevent helpers
[wxWidgets.git] / wxPython / demo / DrawXXXList.py
CommitLineData
9d37f964
RD
1
2from wxPython.wx import *
3import whrandom, time
4
5#----------------------------------------------------------------------
6
7colours = [
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
29def 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
38def 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
1e4a197e
RD
49def 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
60def 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
72def 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
9d37f964
RD
86def 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
1e4a197e
RD
97def 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
9d37f964 105
9d37f964 106
1e4a197e
RD
107def makeRandomColors(num):
108 colors = []
109 for i in range(num):
110 c = whrandom.choice(colours)
111 colors.append(wxNamedColor(c))
112 return colors
9d37f964 113
9d37f964 114
9d37f964 115
1e4a197e
RD
116pencache = {}
117brushcache = {}
118points = None
119lines = None
120rectangles = None
121polygons = None
122text = None
123pens = None
124brushes = None
125colors1 = None
126colors2 = None
127
128
129def 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
163def 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
176def TestArrayPoints(dc,log):
177 try:
178 import Numeric
9d37f964 179
9d37f964
RD
180 dc.BeginDrawing()
181 start = time.time()
9d37f964 182 dc.SetPen(wxPen("BLACK", 1))
1e4a197e
RD
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
194def 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
207def TestRectangles(dc,log):
208 dc.BeginDrawing()
209 start = time.time()
210
211 dc.SetPen( wxPen("BLACK",1) )
212 dc.SetBrush( wxBrush("RED") )
9d37f964 213
1e4a197e
RD
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
227def 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
245def TestRectanglesArray(dc,log):
246 try:
247 import Numeric
248 Apoints = Numeric.array(rectangles)
249
250 dc.BeginDrawing()
251 start = time.time()
9d37f964 252 dc.SetPen(wxPen("BLACK", 1))
1e4a197e
RD
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)
9d37f964
RD
261
262 dc.EndDrawing()
1e4a197e
RD
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
269def 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
285def 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
300def 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
317class 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
344class 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
9d37f964
RD
359
360#----------------------------------------------------------------------
361
362def runTest(frame, nb, log):
363 w = nb.GetClientSize().width
364 h = nb.GetClientSize().height
1e4a197e
RD
365 if w < 600: w = 600
366 if h < 400: h = 400
367 Init(w, h, 200)
368 win = TestNB(nb, -1, log)
9d37f964
RD
369 return win
370
371#----------------------------------------------------------------------
372
373
374overview = """\
375
376Some methods have been added to wxDC to help with optimization of
377drawing 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
1e4a197e
RD
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
9d37f964 411"""
1e4a197e
RD
412
413
414if __name__ == '__main__':
415 import sys,os
416 import run
417 run.main(['', os.path.basename(sys.argv[0])])
418