]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/DrawXXXList.py
1 # 11/5/2003 - Jeff Grimmett (grimmtooth@softhome.net)
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.
7 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
17 #----------------------------------------------------------------------
39 #----------------------------------------------------------------------
41 def makeRandomPoints(num
, w
, h
):
45 x
= random
.randint(0, w
)
46 y
= random
.randint(0, h
)
52 def makeRandomLines(num
, w
, h
):
56 x1
= random
.randint(0, w
)
57 y1
= random
.randint(0, h
)
58 x2
= random
.randint(0, w
)
59 y2
= random
.randint(0, h
)
60 pnts
.append( (x1
,y1
, x2
,y2
) )
65 def makeRandomRectangles(num
, W
, H
):
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
)
73 rects
.append( (x
, y
, w
, h
) )
78 def makeRandomText(num
):
79 Np
= 8 # number of characters in text
86 c
= chr( random
.randint(48, 122) )
89 text
.append( "".join(word
) )
94 def makeRandomPolygons(num
, W
, H
):
95 Np
= 8 # number of points per polygon
102 x
= random
.randint(0, W
)
103 y
= random
.randint(0, H
)
111 def makeRandomPens(num
, cache
):
115 c
= random
.choice(colours
)
116 t
= random
.randint(1, 4)
118 if not cache
.has_key( (c
, t
) ):
119 cache
[(c
, t
)] = wx
.Pen(c
, t
)
121 pens
.append( cache
[(c
, t
)] )
126 def makeRandomBrushes(num
, cache
):
130 c
= random
.choice(colours
)
132 if not cache
.has_key(c
):
133 cache
[c
] = wx
.Brush(c
)
135 brushes
.append( cache
[c
] )
140 def makeRandomColors(num
):
144 c
= random
.choice(colours
)
145 colors
.append(wx
.NamedColour(c
))
176 # make some lists of random shapes
177 points
= makeRandomPoints(n
, w
, h
)
181 Apoints
= Numeric
.array(points
)
185 lines
= makeRandomLines(n
, w
, h
)
186 rectangles
= makeRandomRectangles(n
, w
, h
)
187 polygons
= makeRandomPolygons(n
, w
, h
)
188 text
= makeRandomText(n
)
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
)
199 def TestPoints(dc
,log
):
202 dc
.SetPen(wx
.Pen("BLACK", 4))
204 dc
.DrawPointList(points
)
205 dc
.DrawPointList(points
, wx
.Pen("RED", 2))
206 dc
.DrawPointList(points
, pens
)
209 log
.write("DrawTime: %s seconds with DrawPointList\n" % (time
.time() - start
))
212 def TestArrayPoints(dc
,log
):
218 dc
.SetPen(wx
.Pen("BLACK", 1))
221 dc
.DrawPointList(Apoints
)
223 #dc.DrawPointList(Apoints, wx.Pen("RED", 2))
224 #dc.DrawPointList(Apoints, pens)
226 log
.write("DrawTime: %s seconds with DrawPointList an Numpy Array\n" % (time
.time() - start
))
228 log
.write("Couldn't import Numeric")
232 def TestLines(dc
,log
):
236 dc
.SetPen(wx
.Pen("BLACK", 2))
237 dc
.DrawLineList(lines
)
238 dc
.DrawLineList(lines
, wx
.Pen("RED", 2))
239 dc
.DrawLineList(lines
, pens
)
242 log
.write("DrawTime: %s seconds with DrawLineList\n" % (time
.time() - start
))
245 def TestRectangles(dc
,log
):
249 dc
.SetPen( wx
.Pen("BLACK",1) )
250 dc
.SetBrush( wx
.Brush("RED") )
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)
262 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
265 def TestEllipses(dc
,log
):
269 dc
.SetPen( wx
.Pen("BLACK",1) )
270 dc
.SetBrush( wx
.Brush("RED") )
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
)
280 log
.write("DrawTime: %s seconds with DrawEllipsesList\n" % (time
.time() - start
))
283 def TestRectanglesArray(dc
,log
):
286 Apoints
= Numeric
.array(rectangles
)
290 dc
.SetPen(wx
.Pen("BLACK", 1))
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)
301 log
.write("DrawTime: %s seconds with DrawRectangleList and Numpy Array\n" % (time
.time() - start
))
303 log
.write("Couldn't import Numeric")
307 def TestRectanglesLoop(dc
,log
):
311 dc
.DrawRectangleList(rectangles
,pens
,brushes
)
312 log
.write("DrawTime: %s seconds with DrawRectanglesList\n" % (time
.time() - start
))
316 for i
in range(len(rectangles
)):
318 dc
.SetBrush( brushes
[i
] )
319 dc
.DrawRectangle(rectangles
[i
][0],rectangles
[i
][1],rectangles
[i
][2],rectangles
[i
][3])
322 log
.write("DrawTime: %s seconds with Python loop\n" % (time
.time() - start
))
325 def TestPolygons(dc
,log
):
329 dc
.SetPen(wx
.Pen("BLACK", 1))
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
))
340 def TestText(dc
,log
):
345 # NOTE: you need to set BackgroundMode for the background colors to be used.
346 dc
.SetBackgroundMode(wx
.SOLID
)
349 dc
.DrawTextList(text
, points
, foreground
, background
)
351 log
.write("DrawTime: %s seconds with DrawTextList\n" % (time
.time() - start
))
357 class TestNB(wx
.Notebook
):
358 def __init__(self
, parent
, id, log
):
361 if wx
.Platform
== "__WXMAC__":
364 wx
.Notebook
.__init
__(self
, parent
, id, style
=style
)
367 # Initialize our various samples and add them to the notebook.
368 win
= DrawPanel(self
, TestEllipses
, log
)
369 self
.AddPage(win
, 'Ellipses')
371 win
= DrawPanel(self
, TestText
, log
)
372 self
.AddPage(win
, 'Text')
374 win
= DrawPanel(self
, TestPolygons
, log
)
375 self
.AddPage(win
, 'Polygons')
377 win
= DrawPanel(self
, TestPoints
, log
)
378 self
.AddPage(win
, 'Points')
380 win
= DrawPanel(self
, TestLines
, log
)
381 self
.AddPage(win
, 'Lines')
383 win
= DrawPanel(self
, TestRectangles
, log
)
384 self
.AddPage(win
, 'Rectangles')
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.
389 class DrawPanel(wx
.Panel
):
390 def __init__(self
, parent
, drawFun
, log
):
391 wx
.Panel
.__init
__(self
, parent
, -1)
392 self
.SetBackgroundColour(wx
.WHITE
)
395 self
.drawFun
= drawFun
396 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
399 def OnPaint(self
, evt
):
400 dc
= wx
.PaintDC(self
)
402 self
.drawFun(dc
,self
.log
)
405 #----------------------------------------------------------------------
407 def runTest(frame
, nb
, log
):
408 w
= nb
.GetClientSize().width
409 h
= nb
.GetClientSize().height
413 win
= TestNB(nb
, -1, log
)
416 #----------------------------------------------------------------------
421 Some methods have been added to wx.DC to help with optimization of
422 drawing routines. Currently they are:
425 DrawPointList(sequence, pens=None)
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.
431 DrawLineList(sequence, pens=None)
433 Where sequence is a tuple, list, whatever of 4 element tuples
434 (x1,y1, x2,y2) and pens is either None, a single pen or a list
438 DrawRectangleList(rectangles, pens=None, brushes=None)
443 DrawEllipseList(ellipses, pens=None, brushes=None)
448 DrawPolygonList(polygons, pens=None, brushes=None)
453 DrawTextList(textList, coords, foregrounds = None, backgrounds = None)
459 if __name__
== '__main__':
462 run
.main(['', os
.path
.basename(sys
.argv
[0])])