]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
fixed sending 2 events (normal and bogus cancel one) when ending to edit a tree ctrl...
[wxWidgets.git] / wxPython / demo / FloatCanvas.py
1 #!/usr/bin/env python2.3
2 try:
3 import Numeric
4 import RandomArray
5 haveNumeric = True
6 except ImportError:
7 try:
8 import numarray as Numeric
9 import numarray.random_array as RandomArray
10 haveNumeric = True
11 except ImportError:
12 haveNumeric = False
13
14 if not haveNumeric:
15 errorText = """
16 The FloatCanvas requires either the Numeric or Numarray module:
17 You can get them at:
18 http://sourceforge.net/projects/numpy
19
20 NOTE: The Numeric module is substantially faster than numarray for this
21 purpose, if you have lot's of objects
22 """
23
24 def runTest(frame, nb, log):
25 dlg = wx.MessageDialog(frame, errorText, 'Sorry', wx.OK |
26 wx.ICON_INFORMATION)
27 dlg.ShowModal()
28 dlg.Destroy()
29
30 overview = ""
31
32 else:
33 StartUpDemo = "all"
34 if __name__ == "__main__": # parse options if run stand-alone
35 # check options:
36 import sys, getopt
37 optlist, args = getopt.getopt(sys.argv[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props"])
38
39 for opt in optlist:
40 if opt[0] == "--all":
41 StartUpDemo = "all"
42 elif opt[0] == "--text":
43 StartUpDemo = "text"
44 elif opt[0] == "--map":
45 StartUpDemo = "map"
46 elif opt[0] == "--stext":
47 StartUpDemo = "stext"
48 elif opt[0] == "--hit":
49 StartUpDemo = "hit"
50 elif opt[0] == "--hitf":
51 StartUpDemo = "hitf"
52 elif opt[0] == "--animate":
53 StartUpDemo = "animate"
54 elif opt[0] == "--speed":
55 StartUpDemo = "speed"
56 elif opt[0] == "--temp":
57 StartUpDemo = "temp"
58 elif opt[0] == "--props":
59 StartUpDemo = "props"
60 import wx
61 import time, random
62
63 def runTest(frame, nb, log):
64 """
65 This method is used by the wxPython Demo Framework for integrating
66 this demo with the rest.
67 """
68 win = DrawFrame(None, -1, "FloatCanvas Drawing Window",wx.DefaultPosition,(500,500))
69 frame.otherWin = win
70 win.Show(True)
71 win.DrawTest()
72
73 try:
74 from floatcanvas import NavCanvas, FloatCanvas
75 except ImportError: # if it's not there locally, try the wxPython lib.
76 from wx.lib.floatcanvas import NavCanvas, FloatCanvas
77
78 import wxPython.lib.colourdb
79
80 class DrawFrame(wx.Frame):
81
82 """
83 A frame used for the FloatCanvas Demo
84
85 """
86
87
88 def __init__(self,parent, id,title,position,size):
89 wx.Frame.__init__(self,parent, id,title,position, size)
90
91 ## Set up the MenuBar
92 MenuBar = wx.MenuBar()
93
94 file_menu = wx.Menu()
95 item = file_menu.Append(-1, "&Close","Close this frame")
96 self.Bind(wx.EVT_MENU, self.OnQuit, item)
97 MenuBar.Append(file_menu, "&File")
98
99 draw_menu = wx.Menu()
100
101 item = draw_menu.Append(-1, "&Draw Test","Run a test of drawing random components")
102 self.Bind(wx.EVT_MENU, self.DrawTest, item)
103
104 item = draw_menu.Append(-1, "&Line Test","Run a test of drawing random lines")
105 self.Bind(wx.EVT_MENU, self.LineTest, item)
106
107 item = draw_menu.Append(-1, "Draw &Map","Run a test of drawing a map")
108 self.Bind(wx.EVT_MENU, self.DrawMap, item)
109 item = draw_menu.Append(-1, "&Text Test","Run a test of text drawing")
110 self.Bind(wx.EVT_MENU, self.TestText, item)
111 item = draw_menu.Append(-1, "&ScaledText Test","Run a test of text drawing")
112 self.Bind(wx.EVT_MENU, self.TestScaledText, item)
113 item = draw_menu.Append(-1, "&Clear","Clear the Canvas")
114 self.Bind(wx.EVT_MENU, self.Clear, item)
115 item = draw_menu.Append(-1, "&Hit Test","Run a test of the hit test code")
116 self.Bind(wx.EVT_MENU, self.TestHitTest, item)
117 item = draw_menu.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
118 self.Bind(wx.EVT_MENU, self.TestHitTestForeground, item)
119 item = draw_menu.Append(-1, "&Animation","Run a test of Animation")
120 self.Bind(wx.EVT_MENU, self.TestAnimation, item)
121 item = draw_menu.Append(-1, "&Speed","Run a test of Drawing Speed")
122 self.Bind(wx.EVT_MENU, self.SpeedTest, item)
123 item = draw_menu.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
124 self.Bind(wx.EVT_MENU, self.PropertiesChangeTest, item)
125 MenuBar.Append(draw_menu, "&Tests")
126
127 view_menu = wx.Menu()
128 item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
129 self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
130 MenuBar.Append(view_menu, "&View")
131
132 help_menu = wx.Menu()
133 item = help_menu.Append(-1, "&About",
134 "More information About this program")
135 self.Bind(wx.EVT_MENU, self.OnAbout, item)
136 MenuBar.Append(help_menu, "&Help")
137
138 self.SetMenuBar(MenuBar)
139
140 self.CreateStatusBar()
141 # Add the Canvas
142 self.Canvas = NavCanvas.NavCanvas(self,
143 -1,
144 (500,500),
145 Debug = 0,
146 BackgroundColor = "DARK SLATE BLUE")
147
148 wx.EVT_CLOSE(self, self.OnCloseWindow)
149
150 FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove )
151 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
152
153 self.EventsAreBound = False
154
155 ## getting all the colors and linestyles for random objects
156 wxPython.lib.colourdb.updateColourDB()
157 self.colors = wxPython.lib.colourdb.getColourList()
158 #self.LineStyles = FloatCanvas.DrawObject.LineStyleList.keys()
159
160
161 return None
162
163 def BindAllMouseEvents(self):
164 if not self.EventsAreBound:
165 ## Here is how you catch FloatCanvas mouse events
166 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, self.OnLeftDown )
167 FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
168 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, self.OnLeftDouble )
169
170 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, self.OnMiddleDown )
171 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, self.OnMiddleUp )
172 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, self.OnMiddleDouble )
173
174 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, self.OnRightDown )
175 FloatCanvas.EVT_RIGHT_UP(self.Canvas, self.OnRightUp )
176 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, self.OnRightDouble )
177
178 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, self.OnWheel )
179 self.EventsAreBound = True
180
181 def UnBindAllMouseEvents(self):
182 ## Here is how you catch FloatCanvas mouse events
183 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, None )
184 FloatCanvas.EVT_LEFT_UP(self.Canvas, None )
185 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, None)
186
187 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, None )
188 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, None )
189 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, None )
190
191 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, None )
192 FloatCanvas.EVT_RIGHT_UP(self.Canvas, None )
193 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, None )
194
195 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, None )
196
197 self.EventsAreBound = False
198
199 def PrintCoords(self,event):
200 print "coords are: %s"%(event.Coords,)
201 print "pixel coords are: %s\n"%(event.GetPosition(),)
202
203 def OnLeftDown(self, event):
204 print "Left Button has been clicked in DrawFrame"
205 self.PrintCoords(event)
206
207 def OnLeftUp(self, event):
208 print "Left up in DrawFrame"
209 self.PrintCoords(event)
210
211 def OnLeftDouble(self, event):
212 print "Left Double Click in DrawFrame"
213 self.PrintCoords(event)
214
215 def OnMiddleDown(self, event):
216 print "Middle Button clicked in DrawFrame"
217 self.PrintCoords(event)
218
219 def OnMiddleUp(self, event):
220 print "Middle Button Up in DrawFrame"
221 self.PrintCoords(event)
222
223 def OnMiddleDouble(self, event):
224 print "Middle Button Double clicked in DrawFrame"
225 self.PrintCoords(event)
226
227 def OnRightDown(self, event):
228 print "Right Button has been clicked in DrawFrame"
229 self.PrintCoords(event)
230
231 def OnRightUp(self, event):
232 print "Right Button Up in DrawFrame"
233 self.PrintCoords(event)
234
235 def OnRightDouble(self, event):
236 print "Right Button Double clicked in DrawFrame"
237 self.PrintCoords(event)
238
239 def OnWheel(self, event):
240 print "Mouse Wheel Moved in DrawFrame"
241 self.PrintCoords(event)
242
243 def OnMove(self, event):
244 """
245 Updates the staus bar with the world coordinates
246 """
247 self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
248
249 def OnAbout(self, event):
250 print "OnAbout called"
251
252 dlg = wx.MessageDialog(self, "This is a small program to demonstrate\n"
253 "the use of the FloatCanvas\n",
254 "About Me", wx.OK | wx.ICON_INFORMATION)
255 dlg.ShowModal()
256 dlg.Destroy()
257
258 def ZoomToFit(self,event):
259 self.Canvas.ZoomToBB()
260
261 def Clear(self,event = None):
262 self.UnBindAllMouseEvents()
263 self.Canvas.ClearAll()
264 self.Canvas.SetProjectionFun(None)
265 self.Canvas.Draw()
266
267 def OnQuit(self,event):
268 self.Close(True)
269
270 def OnCloseWindow(self, event):
271 self.Destroy()
272
273 def DrawTest(self,event=None):
274 wx.GetApp().Yield()
275 # import random
276 # import RandomArray
277 Range = (-10,10)
278 colors = self.colors
279
280 self.BindAllMouseEvents()
281 Canvas = self.Canvas
282
283 Canvas.ClearAll()
284 Canvas.SetProjectionFun(None)
285
286 ## Random tests of everything:
287
288 # Rectangles
289 for i in range(3):
290 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
291 lw = random.randint(1,5)
292 cf = random.randint(0,len(colors)-1)
293 h = random.randint(1,5)
294 w = random.randint(1,5)
295 Canvas.AddRectangle(x,y,w,h,LineWidth = lw,FillColor = colors[cf])
296
297 # Ellipses
298 for i in range(3):
299 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
300 lw = random.randint(1,5)
301 cf = random.randint(0,len(colors)-1)
302 h = random.randint(1,5)
303 w = random.randint(1,5)
304 Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
305
306 # Points
307 for i in range(5):
308 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
309 D = random.randint(1,50)
310 cf = random.randint(0,len(colors)-1)
311 Canvas.AddPoint((x,y), Color = colors[cf], Diameter = D)
312
313 # Circles
314 for i in range(5):
315 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
316 D = random.randint(1,5)
317 lw = random.randint(1,5)
318 cf = random.randint(0,len(colors)-1)
319 cl = random.randint(0,len(colors)-1)
320 Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
321 Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc")
322
323 # Lines
324 for i in range(5):
325 points = []
326 for j in range(random.randint(2,10)):
327 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
328 points.append(point)
329 lw = random.randint(1,10)
330 cf = random.randint(0,len(colors)-1)
331 cl = random.randint(0,len(colors)-1)
332 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
333
334 # Polygons
335 for i in range(3):
336 points = []
337 for j in range(random.randint(2,6)):
338 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
339 points.append(point)
340 lw = random.randint(1,6)
341 cf = random.randint(0,len(colors)-1)
342 cl = random.randint(0,len(colors)-1)
343 Canvas.AddPolygon(points,
344 LineWidth = lw,
345 LineColor = colors[cl],
346 FillColor = colors[cf],
347 FillStyle = 'Solid')
348
349 ## Pointset
350 for i in range(4):
351 points = []
352 points = RandomArray.uniform(Range[0],Range[1],(100,2))
353 cf = random.randint(0,len(colors)-1)
354 D = random.randint(1,4)
355 Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
356
357 # Text
358 String = "Unscaled text"
359 for i in range(3):
360 ts = random.randint(10,40)
361 cf = random.randint(0,len(colors)-1)
362 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
363 Canvas.AddText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
364
365 # Scaled Text
366 String = "Scaled text"
367 for i in range(3):
368 ts = random.random()*3 + 0.2
369 cf = random.randint(0,len(colors)-1)
370 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
371 Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
372
373 Canvas.ZoomToBB()
374
375 def TestAnimation(self,event=None):
376 """
377
378 In this test, a relatively complex background is drawn, and
379 a simple object placed in the foreground is moved over
380 it. This demonstrates how to use the InForeground attribute
381 to make an object in the foregorund draw fast, without
382 having to re-draw the whole background.
383
384 """
385 wx.GetApp().Yield()
386 Range = (-10,10)
387 self.Range = Range
388
389 self.UnBindAllMouseEvents()
390 Canvas = self.Canvas
391
392 Canvas.ClearAll()
393 Canvas.SetProjectionFun(None)
394
395 ## Random tests of everything:
396 colors = self.colors
397 # Rectangles
398 for i in range(3):
399 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
400 lw = random.randint(1,5)
401 cf = random.randint(0,len(colors)-1)
402 h = random.randint(1,5)
403 w = random.randint(1,5)
404 Canvas.AddRectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
405
406 # Ellipses
407 for i in range(3):
408 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
409 lw = random.randint(1,5)
410 cf = random.randint(0,len(colors)-1)
411 h = random.randint(1,5)
412 w = random.randint(1,5)
413 Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
414
415 # Circles
416 for i in range(5):
417 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
418 D = random.randint(1,5)
419 lw = random.randint(1,5)
420 cf = random.randint(0,len(colors)-1)
421 cl = random.randint(0,len(colors)-1)
422 Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
423 Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackgroundColor = None,Position = "cc")
424
425 # Lines
426 for i in range(5):
427 points = []
428 for j in range(random.randint(2,10)):
429 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
430 points.append(point)
431 lw = random.randint(1,10)
432 cf = random.randint(0,len(colors)-1)
433 cl = random.randint(0,len(colors)-1)
434 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
435
436 # Polygons
437 for i in range(3):
438 points = []
439 for j in range(random.randint(2,6)):
440 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
441 points.append(point)
442 lw = random.randint(1,6)
443 cf = random.randint(0,len(colors)-1)
444 cl = random.randint(0,len(colors)-1)
445 Canvas.AddPolygon(points,
446 LineWidth = lw,
447 LineColor = colors[cl],
448 FillColor = colors[cf],
449 FillStyle = 'Solid')
450
451 # Scaled Text
452 String = "Scaled text"
453 for i in range(3):
454 ts = random.random()*3 + 0.2
455 cf = random.randint(0,len(colors)-1)
456 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
457 Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc")
458
459
460 # Now the Foreground Object:
461 C = Canvas.AddCircle(0,0,7,LineWidth = 2,LineColor = "Black",FillColor = "Red", InForeground = True)
462 T = Canvas.AddScaledText("Click to Move",0,0, Size = 0.6, Position = 'cc', InForeground = True)
463 C.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.MoveMe)
464 C.Text = T
465
466 self.Timer = wx.PyTimer(self.ShowFrame)
467 self.FrameDelay = 50 # milliseconds
468
469 Canvas.ZoomToBB()
470
471 def ShowFrame(self):
472 Object = self.MovingObject
473 Range = self.Range
474 if self.TimeStep < self.NumTimeSteps:
475 x,y = Object.XY
476 if x > Range[1] or x < Range[0]:
477 self.dx = -self.dx
478 if y > Range[1] or y < Range[0]:
479 self.dy = -self.dy
480 Object.Move( (self.dx,self.dy) )
481 Object.Text.Move( (self.dx,self.dy))
482 self.Canvas.Draw()
483 self.TimeStep += 1
484 wx.GetApp().Yield(True)
485 else:
486 self.Timer.Stop()
487
488
489 def MoveMe(self, Object):
490 self.MovingObject = Object
491 Range = self.Range
492 self.dx = random.uniform(Range[0]/4,Range[1]/4)
493 self.dy = random.uniform(Range[0]/4,Range[1]/4)
494 #import time
495 #start = time.time()
496 self.NumTimeSteps = 200
497 self.TimeStep = 1
498 self.Timer.Start(self.FrameDelay)
499 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
500
501 def TestHitTest(self,event=None):
502 wx.GetApp().Yield()
503
504 self.UnBindAllMouseEvents()
505 Canvas = self.Canvas
506
507 Canvas.ClearAll()
508 Canvas.SetProjectionFun(None)
509
510 #Add a HitAble rectangle
511 w, h = 60, 20
512
513 dx = 80
514 dy = 40
515 x,y = 20, 20
516 FontSize = 8
517
518 #Add one that is not HitAble
519 Canvas.AddRectangle(x, y, w, h, LineWidth = 2)
520 Canvas.AddText("Not Hit-able", x, y, Size = FontSize, Position = "bl")
521
522
523 x += dx
524 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2)
525 R.Name = "Line Rectangle"
526 R.HitFill = False
527 R.HitLineWidth = 5 # Makes it a little easier to hit
528 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
529 Canvas.AddText("Left Click Line", x, y, Size = FontSize, Position = "bl")
530 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
531
532 x += dx
533 color = "Red"
534 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
535 R.Name = color + "Rectangle"
536 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
537 Canvas.AddText("Left Click Fill", x, y, Size = FontSize, Position = "bl")
538 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
539
540 x = 20
541 y += dy
542 color = "LightBlue"
543 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
544 R.Name = color + " Rectangle"
545 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
546 Canvas.AddText("Right Click Fill", x, y, Position = "bl")
547 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
548
549 x += dx
550 color = "Grey"
551 R = Canvas.AddEllipse(x, y, w, h,LineWidth = 2,FillColor = color)
552 R.Name = color +" Ellipse"
553 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
554 Canvas.AddText("Right Click Fill", x, y, Size = FontSize, Position = "bl")
555 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
556
557 x += dx
558 color = "Brown"
559 R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2, FillColor = color)
560 R.Name = color + " Circle"
561 R.HitFill = True
562 R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
563 Canvas.AddText("Left D-Click Fill", x, y, Size = FontSize, Position = "bl")
564 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
565
566 x = 20
567 y += dy
568 color = "Pink"
569 R = Canvas.AddCircle(x+dx/2, y+dy/2, dx/4, LineWidth = 2,FillColor = color)
570 R.Name = color + " Circle"
571 R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
572 Canvas.AddText("Left Up Fill", x, y, Size = FontSize, Position = "bl")
573 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
574
575 x += dx
576 color = "White"
577 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
578 R.Name = color + " Rectangle"
579 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
580 Canvas.AddText("Middle Down", x, y, Size = FontSize, Position = "bl")
581 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
582
583 x += dx
584 color = "AQUAMARINE"
585 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
586 R.Name = color + " Rectangle"
587 R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
588 Canvas.AddText("Middle Up", x, y, Size = FontSize, Position = "bl")
589 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
590
591 x = 20
592 y += dy
593 color = "CORAL"
594 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
595 R.Name = color + " Rectangle"
596 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
597 Canvas.AddText("Middle DoubleClick", x, y, Size = FontSize, Position = "bl")
598 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
599
600 x += dx
601 color = "CYAN"
602 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
603 R.Name = color + " Rectangle"
604 R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
605 Canvas.AddText("Right Up", x, y, Size = FontSize, Position = "bl")
606 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
607
608 x += dx
609 color = "LIME GREEN"
610 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
611 R.Name = color + " Rectangle"
612 R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
613 Canvas.AddText("Right Double Click", x, y, Size = FontSize, Position = "bl")
614 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
615
616 x = 20
617 y += dy
618 color = "MEDIUM GOLDENROD"
619 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
620 R.Name = color
621 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
622 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
623 Canvas.AddText("L and R Click", x, y, Size = FontSize, Position = "bl")
624 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
625
626 x += dx
627 color = "SALMON"
628 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
629 R.Name = color + " Rectangle"
630 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
631 Canvas.AddText("Mouse Enter", x, y, Size = FontSize, Position = "bl")
632 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
633
634 x += dx
635 color = "MEDIUM VIOLET RED"
636 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
637 R.Name = color
638 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
639 Canvas.AddText("Mouse Leave", x, y, Size = FontSize, Position = "bl")
640 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
641
642 x = 20
643 y += dy
644 color = "SKY BLUE"
645 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color)
646 R.Name = color
647 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
648 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
649 Canvas.AddText("Enter and Leave", x, y, Size = FontSize, Position = "bl")
650 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
651
652 x += dx
653 color = "WHEAT"
654 R = Canvas.AddRectangle(x, y, w+12, h, LineColor = None, FillColor = color)
655 R.Name = color
656 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
657 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
658 Canvas.AddText("Mouse Enter&Leave", x, y, Size = FontSize, Position = "bl")
659 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
660
661 x += dx
662 color = "KHAKI"
663 R = Canvas.AddRectangle(x-12, y, w+12, h, LineColor = None, FillColor = color)
664 R.Name = color
665 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
666 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
667 Canvas.AddText("Mouse ENter&Leave", x, y, Size = FontSize, Position = "bl")
668 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
669
670 x = 20
671 y += dy
672 L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
673 L.Name = "A Line"
674 L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
675 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "bl")
676 Canvas.AddText(L.Name, x, y+h, Size = FontSize, Position = "tl")
677
678 x += dx
679 color = "SEA GREEN"
680 Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
681 R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
682 R.Name = color + " Polygon"
683 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
684 Canvas.AddText("RIGHT_DOWN", x, y, Size = FontSize, Position = "bl")
685 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
686
687 x += dx
688 color = "Red"
689 Points = Numeric.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), Numeric.Float)
690 R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
691 R.Name = "PointSet"
692 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
693 Canvas.AddText("LEFT_DOWN", x, y, Size = FontSize, Position = "bl")
694 Canvas.AddText(R.Name, x, y+h, Size = FontSize, Position = "tl")
695
696 x = 20
697 y += dy
698 T = Canvas.AddText("Hit-able Text", x, y, Size = 15, Color = "Red", Position = 'tl')
699 T.Name = "Hit-able Text"
700 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
701 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "bl")
702
703 x += dx
704 T = Canvas.AddScaledText("Scaled Text", x, y, Size = 1./2*h, Color = "Pink", Position = 'bl')
705 Canvas.AddPointSet( (x, y), Diameter = 3)
706 T.Name = "Scaled Text"
707 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
708 Canvas.AddText("Left Down", x, y, Size = FontSize, Position = "tl")
709
710 self.Canvas.ZoomToBB()
711
712 def TestHitTestForeground(self,event=None):
713 wx.GetApp().Yield()
714
715 self.UnBindAllMouseEvents()
716 Canvas = self.Canvas
717
718 Canvas.ClearAll()
719 Canvas.SetProjectionFun(None)
720
721 #Add a Hitable rectangle
722 w, h = 60, 20
723
724 dx = 80
725 dy = 40
726 x,y = 20, 20
727
728 color = "Red"
729 R = Canvas.AddRectangle(x, y, w, h, LineWidth = 2, FillColor = color, InForeground = False)
730 R.Name = color + "Rectangle"
731 R.HitFill = True
732 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
733 Canvas.AddText("Left Click Fill", x, y, Position = "bl")
734 Canvas.AddText(R.Name, x, y+h, Position = "tl")
735
736 ## A set of Rectangles that move together
737
738 ## NOTE: In a real app, it might be better to create a new
739 ## custom FloatCanvas DrawObject
740
741 self.MovingRects = []
742 x += dx
743 color = "LightBlue"
744 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
745 R.HitFill = True
746 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
747 L = Canvas.AddText("Left", x + w/4, y + h/4, Position = "cc", InForeground = True)
748 self.MovingRects.extend( (R,L) )
749
750 x += w/2
751 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
752 R.HitFill = True
753 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
754 L = Canvas.AddText("Right", x + w/4, y + h/4, Position = "cc", InForeground = True)
755 self.MovingRects.extend( (R,L) )
756
757 x -= w/2
758 y += h/2
759 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
760 R.HitFill = True
761 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
762 L = Canvas.AddText("Up", x + w/4, y + h/4, Position = "cc", InForeground = True)
763 self.MovingRects.extend( (R,L) )
764
765
766 x += w/2
767 R = Canvas.AddRectangle(x, y, w/2, h/2, LineWidth = 2, FillColor = color, InForeground = True)
768 R.HitFill = True
769 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
770 L = Canvas.AddText("Down", x + w/4, y + h/4, Position = "cc", InForeground = True)
771 self.MovingRects.extend( (R,L) )
772
773 self.Canvas.ZoomToBB()
774
775 def RectMoveLeft(self,Object):
776 self.MoveRects("left")
777
778 def RectMoveRight(self,Object):
779 self.MoveRects("right")
780
781 def RectMoveUp(self,Object):
782 self.MoveRects("up")
783
784 def RectMoveDown(self,Object):
785 self.MoveRects("down")
786
787 def MoveRects(self, Dir):
788 for Object in self.MovingRects:
789 X,Y = Object.XY
790 if Dir == "left": X -= 10
791 elif Dir == "right": X += 10
792 elif Dir == "up": Y += 10
793 elif Dir == "down": Y -= 10
794 Object.SetXY(X,Y)
795 self.Canvas.Draw()
796
797
798 def PointSetGotHit(self, Object):
799 print Object.Name, "Got Hit\n"
800
801 def RectGotHit(self, Object):
802 print Object.Name, "Got Hit\n"
803
804 def RectGotHitRight(self, Object):
805 print Object.Name, "Got Hit With Right\n"
806
807 def RectGotHitLeft(self, Object):
808 print Object.Name, "Got Hit with Left\n"
809
810 def RectMouseOver(self, Object):
811 print "Mouse entered:", Object.Name
812
813 def RectMouseLeave(self, Object):
814 print "Mouse left ", Object.Name
815
816
817 def TestText(self, event= None):
818 wx.GetApp().Yield()
819
820 self.BindAllMouseEvents()
821 Canvas = self.Canvas
822 Canvas.ClearAll()
823 Canvas.SetProjectionFun(None)
824
825 x,y = (0, 0)
826
827 ## Add a non-visible rectangle, just to get a Bounding Box
828 ## Text objects have a zero-size bounding box, because it changes with zoom
829 Canvas.AddRectangle(-10,-10,20,20,LineWidth = 1, LineColor = None)
830
831 # Text
832 String = "Some text"
833 self.Canvas.AddText("Top Left",x,y,Size = 14,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
834 self.Canvas.AddText("Bottom Left",x,y,Size = 14,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
835 self.Canvas.AddText("Top Right",x,y,Size = 14,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
836 self.Canvas.AddText("Bottom Right",x,y,Size = 14,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
837 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
838
839 x,y = (0, 2)
840
841 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
842 self.Canvas.AddText("Top Center",x,y,Size = 14,Color = "Black",Position = "tc")
843 self.Canvas.AddText("Bottom Center",x,y,Size = 14,Color = "White",Position = "bc")
844
845 x,y = (0, 4)
846
847 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
848 self.Canvas.AddText("Center Right",x,y,Size = 14,Color = "Black",Position = "cr")
849 self.Canvas.AddText("Center Left",x,y,Size = 14,Color = "Black",Position = "cl")
850
851 x,y = (0, -2)
852
853 Canvas.AddPointSet((x,y), Color = "White", Diameter = 2)
854 self.Canvas.AddText("Center Center",x,y,Size = 14,Color = "Black",Position = "cc")
855
856 self.Canvas.AddText("40 Pixels",-10,8,Size = 40)
857 self.Canvas.AddText("20 Pixels",-10,5,Size = 20)
858 self.Canvas.AddText("10 Pixels",-10,3,Size = 10)
859
860 self.Canvas.AddText("MODERN Font", -10, 0, Family = wx.MODERN)
861 self.Canvas.AddText("DECORATIVE Font", -10, -1, Family = wx.DECORATIVE)
862 self.Canvas.AddText("ROMAN Font", -10, -2, Family = wx.ROMAN)
863 self.Canvas.AddText("SCRIPT Font", -10, -3, Family = wx.SCRIPT)
864 self.Canvas.AddText("ROMAN BOLD Font", -10, -4, Family = wx.ROMAN, Weight=wx.BOLD)
865 self.Canvas.AddText("ROMAN ITALIC BOLD Font", -10, -5, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
866
867 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
868 Font = wx.Font(20, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
869 self.Canvas.AddText("zapf chancery Font", -10, -6, Font = Font)
870
871 self.Canvas.ZoomToBB()
872
873 def TestScaledText(self, event= None):
874 wx.GetApp().Yield()
875
876 self.BindAllMouseEvents()
877 Canvas = self.Canvas
878 Canvas.ClearAll()
879 Canvas.SetProjectionFun(None)
880
881 x,y = (0, 0)
882
883 T = Canvas.AddScaledText("Top Left",x,y,Size = 5,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
884 T = Canvas.AddScaledText("Bottom Left",x,y,Size = 5,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
885 T = Canvas.AddScaledText("Top Right",x,y,Size = 5,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
886 T = Canvas.AddScaledText("Bottom Right",x,y,Size = 5,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
887 Canvas.AddPointSet((x,y), Color = "Red", Diameter = 4)
888
889
890 x,y = (0, 20)
891
892 Canvas.AddScaledText("Top Center",x,y,Size = 7,Color = "Black",Position = "tc")
893 Canvas.AddScaledText("Bottom Center",x,y,Size = 7,Color = "White",Position = "bc")
894 Canvas.AddPointSet((x,y), Color = "White", Diameter = 4)
895
896 x,y = (0, -20)
897
898 Canvas.AddScaledText("Center Right",x,y,Size = 9,Color = "Black",Position = "cr")
899 Canvas.AddScaledText("Center Left",x,y,Size = 9,Color = "Black",Position = "cl")
900 Canvas.AddPointSet((x,y), Color = "White", Diameter = 4)
901
902 x = -200
903
904 self.Canvas.AddScaledText("MODERN Font", x, 0, Size = 7, Family = wx.MODERN, Color = (0,0,0))
905 self.Canvas.AddScaledText("DECORATIVE Font", x, -10, Size = 7, Family = wx.DECORATIVE, Color = (0,0,1))
906 self.Canvas.AddScaledText("ROMAN Font", x, -20, Size = 7, Family = wx.ROMAN)
907 self.Canvas.AddScaledText("SCRIPT Font", x, -30, Size = 7, Family = wx.SCRIPT)
908 self.Canvas.AddScaledText("ROMAN BOLD Font", x, -40, Size = 7, Family = wx.ROMAN, Weight=wx.BOLD)
909 self.Canvas.AddScaledText("ROMAN ITALIC BOLD Font", x, -50, Size = 7, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
910 Canvas.AddPointSet((x,0), Color = "White", Diameter = 4)
911
912
913 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
914 x,y = (-100, 50)
915 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
916 T = self.Canvas.AddScaledText("zapf chancery Font", x, y, Size = 20, Font = Font, Position = 'bc')
917
918 x,y = (-50, -50)
919 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "bookman")
920 T = self.Canvas.AddScaledText("Bookman Font", x, y, Size = 8, Font = Font)
921
922 self.Canvas.ZoomToBB()
923
924 def DrawMap(self,event = None):
925 wx.GetApp().Yield()
926 import os, time
927 self.BindAllMouseEvents()
928
929 ## Test of Actual Map Data
930 self.Canvas.ClearAll()
931 self.Canvas.SetProjectionFun("FlatEarth")
932 #start = time.clock()
933 Shorelines = Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
934 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
935 #start = time.clock()
936 for segment in Shorelines:
937 self.Canvas.AddLine(segment)
938 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
939 #start = time.clock()
940 self.Canvas.ZoomToBB()
941 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
942
943
944 def LineTest(self,event = None):
945 wx.GetApp().Yield()
946 import os, time
947 # import random
948 colors = self.colors
949 Range = (-10,10)
950 ## Test of drawing lots of lines
951 Canvas = self.Canvas
952 Canvas.ClearAll()
953 Canvas.SetProjectionFun(None)
954 #start = time.clock()
955 linepoints = []
956 linecolors = []
957 linewidths = []
958 for i in range(2000):
959 points = (random.randint(Range[0],Range[1]),
960 random.randint(Range[0],Range[1]),
961 random.randint(Range[0],Range[1]),
962 random.randint(Range[0],Range[1]))
963 linepoints.append(points)
964 linewidths.append(random.randint(1,10) )
965 linecolors.append(random.randint(0,len(colors)-1) )
966 for (points,color,width) in zip(linepoints,linecolors,linewidths):
967 Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color])
968 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
969 #start = time.clock()
970 Canvas.ZoomToBB()
971 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
972
973 def SpeedTest(self,event=None):
974 wx.GetApp().Yield()
975 BigRange = (-1000,1000)
976 colors = self.colors
977
978 self.UnBindAllMouseEvents()
979 Canvas = self.Canvas
980
981 Canvas.ClearAll()
982 Canvas.SetProjectionFun(None)
983
984 # Pointset
985 coords = []
986 for i in range(1000):
987 x,y = (random.uniform(BigRange[0],BigRange[1]),random.uniform(BigRange[0],BigRange[1]))
988 coords.append( (x,y) )
989 print "Drawing the Points"
990 start = time.clock()
991 for Point in coords:
992 Canvas.AddPoint(Point, Diameter = 4)
993 print "It took %s seconds to add the points"%(time.clock() - start)
994 Canvas.ZoomToBB()
995
996 def PropertiesChangeTest(self,event=None):
997 wx.GetApp().Yield()
998
999 Range = (-10,10)
1000 colors = self.colors
1001
1002 self.UnBindAllMouseEvents()
1003 Canvas = self.Canvas
1004
1005 Canvas.ClearAll()
1006 Canvas.SetProjectionFun(None)
1007
1008 self.ColorObjectsAll = []
1009 self.ColorObjectsLine = []
1010 self.ColorObjectsColor = []
1011 self.ColorObjectsText = []
1012 ##One of each object:
1013 # Rectangle
1014 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1015 lw = random.randint(1,5)
1016 cf = random.randint(0,len(colors)-1)
1017 h = random.randint(1,5)
1018 w = random.randint(1,5)
1019 self.Rectangle = Canvas.AddRectangle(x,y,w,h,LineWidth = lw,FillColor = colors[cf])
1020 self.ColorObjectsAll.append(self.Rectangle)
1021
1022 # Ellipse
1023 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1024 lw = random.randint(1,5)
1025 cf = random.randint(0,len(colors)-1)
1026 h = random.randint(1,5)
1027 w = random.randint(1,5)
1028 self.Ellipse = Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf])
1029 self.ColorObjectsAll.append(self.Ellipse)
1030
1031 # Point
1032 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1033 D = random.randint(1,50)
1034 lw = random.randint(1,5)
1035 cf = random.randint(0,len(colors)-1)
1036 cl = random.randint(0,len(colors)-1)
1037 self.ColorObjectsColor.append(Canvas.AddPoint(xy, colors[cf], D))
1038
1039 # Circle
1040 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1041 D = random.randint(1,5)
1042 lw = random.randint(1,5)
1043 cf = random.randint(0,len(colors)-1)
1044 cl = random.randint(0,len(colors)-1)
1045 self.Circle = Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf])
1046 self.ColorObjectsAll.append(self.Circle)
1047
1048 # Line
1049 points = []
1050 for j in range(random.randint(2,10)):
1051 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
1052 points.append(point)
1053 lw = random.randint(1,10)
1054 cf = random.randint(0,len(colors)-1)
1055 cl = random.randint(0,len(colors)-1)
1056 self.ColorObjectsLine.append(Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
1057
1058 # Polygon
1059 ## points = []
1060 ## for j in range(random.randint(2,6)):
1061 ## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1062 ## points.append(point)
1063 points = RandomArray.uniform(Range[0],Range[1],(6,2))
1064 lw = random.randint(1,6)
1065 cf = random.randint(0,len(colors)-1)
1066 cl = random.randint(0,len(colors)-1)
1067 self.ColorObjectsAll.append(Canvas.AddPolygon(points,
1068 LineWidth = lw,
1069 LineColor = colors[cl],
1070 FillColor = colors[cf],
1071 FillStyle = 'Solid'))
1072
1073 ## Pointset
1074 points = RandomArray.uniform(Range[0],Range[1],(100,2))
1075 cf = random.randint(0,len(colors)-1)
1076 D = random.randint(1,4)
1077 self.PointSet = Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
1078 self.ColorObjectsColor.append(self.PointSet)
1079
1080 ## Point
1081 point = RandomArray.uniform(Range[0],Range[1],(2,))
1082 cf = random.randint(0,len(colors)-1)
1083 D = random.randint(1,4)
1084 self.Point = Canvas.AddPoint(point, Color = colors[cf], Diameter = D)
1085 self.ColorObjectsColor.append(self.Point)
1086
1087 # Text
1088 String = "Unscaled text"
1089 ts = random.randint(10,40)
1090 cf = random.randint(0,len(colors)-1)
1091 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1092 self.ColorObjectsText.append(Canvas.AddText(String, x, y, Size = ts, Color = colors[cf], Position = "cc"))
1093
1094 # Scaled Text
1095 String = "Scaled text"
1096 ts = random.random()*3 + 0.2
1097 cf = random.randint(0,len(colors)-1)
1098 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1099 self.ColorObjectsText.append(Canvas.AddScaledText(String, x, y, Size = ts, Color = colors[cf], Position = "cc"))
1100
1101 # A "Button"
1102 Button = Canvas.AddRectangle(-10, -12, 20, 3, LineStyle = None, FillColor = "Red")
1103 Canvas.AddScaledText("Click Here To Change Properties",
1104 0, -10.5,
1105 Size = 0.7,
1106 Color = "Black",
1107 Position = "cc")
1108
1109 Button.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ChangeProperties)
1110
1111 Canvas.ZoomToBB()
1112
1113 def ChangeProperties(self, Object = None):
1114 colors = self.colors
1115 Range = (-10,10)
1116
1117 for Object in self.ColorObjectsAll:
1118 pass
1119 Object.SetFillColor(colors[random.randint(0,len(colors)-1)])
1120 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1121 Object.SetLineWidth(random.randint(1,7))
1122 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1123 for Object in self.ColorObjectsLine:
1124 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1125 Object.SetLineWidth(random.randint(1,7))
1126 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1127 for Object in self.ColorObjectsColor:
1128 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1129 for Object in self.ColorObjectsText:
1130 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1131 Object.SetBackgroundColor(colors[random.randint(0,len(colors)-1)])
1132 self.Circle.SetDiameter(random.randint(1,10))
1133 self.PointSet.SetDiameter(random.randint(1,8))
1134 self.Point.SetDiameter(random.randint(1,8))
1135 for Object in (self.Rectangle, self.Ellipse):
1136 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1137 w,h = random.randint(1,5), random.randint(1,5)
1138 Object.SetShape(x,y,w,h)
1139
1140 self.Canvas.Draw(Force = True)
1141
1142 def TempTest(self, event= None):
1143 wx.GetApp().Yield()
1144
1145 self.UnBindAllMouseEvents()
1146 Canvas = self.Canvas
1147 Canvas.ClearAll()
1148 Canvas.SetProjectionFun(None)
1149
1150 Range = (-10,10)
1151
1152 # Create a random Polygon
1153 points = []
1154 for j in range(6):
1155 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1156 points.append(point)
1157 Poly = Canvas.AddPolygon(points,
1158 LineWidth = 2,
1159 LineColor = "Black",
1160 FillColor = "LightBlue",
1161 FillStyle = 'Solid')
1162
1163 Poly.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPoly)
1164
1165 self.SelectedPoly = None
1166 self.SelectPoints = []
1167 self.SelectedPoint = None
1168
1169 Canvas.ZoomToBB()
1170
1171 def SelectPoly(self, Object):
1172 print "In SelectPoly"
1173 Canvas = self.Canvas
1174 if Object is self.SelectedPoly:
1175 pass
1176 else:
1177 #fixme: Do something to unselect the old one
1178 self.SelectedPoly = Object
1179 Canvas.RemoveObjects(self.SelectPoints)
1180 self.SelectPoints = []
1181 # Draw points on the Vertices of the Selected Poly:
1182 for i, point in enumerate(Object.Points):
1183 P = Canvas.AddPointSet(point, Diameter = 6, Color = "Red")
1184 P.VerticeNum = i
1185 P.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPointHit)
1186 self.SelectPoints.append(P)
1187 #Canvas.ZoomToBB()
1188 Canvas.Draw()
1189
1190 def SelectPointHit(self, Point):
1191 print "Point Num: %i Hit"%Point.VerticeNum
1192 self.SelectedPoint = Point
1193
1194
1195
1196 class DemoApp(wx.App):
1197 """
1198 How the demo works:
1199
1200 Under the Draw menu, there are three options:
1201
1202 *Draw Test: will put up a picture of a bunch of randomly generated
1203 objects, of each kind supported.
1204
1205 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1206 with a lot of data, and will take a while to load and draw (about 10 sec
1207 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1208 performance is not very good for large drawings.
1209
1210 *Clear: Clears the Canvas.
1211
1212 Once you have a picture drawn, you can zoom in and out and move about
1213 the picture. There is a tool bar with three tools that can be
1214 selected.
1215
1216 The magnifying glass with the plus is the zoom in tool. Once selected,
1217 if you click the image, it will zoom in, centered on where you
1218 clicked. If you click and drag the mouse, you will get a rubber band
1219 box, and the image will zoom to fit that box when you release it.
1220
1221 The magnifying glass with the minus is the zoom out tool. Once selected,
1222 if you click the image, it will zoom out, centered on where you
1223 clicked. (note that this takes a while when you are looking at the map,
1224 as it has a LOT of lines to be drawn. The image is double buffered, so
1225 you don't see the drawing in progress)
1226
1227 The hand is the move tool. Once selected, if you click and drag on the
1228 image, it will move so that the part you clicked on ends up where you
1229 release the mouse. Nothing is changed while you are dragging. The
1230 drawing is too slow for that.
1231
1232 I'd like the cursor to change as you change tools, but the stock
1233 wxCursors didn't include anything I liked, so I stuck with the
1234 pointer. Please let me know if you have any nice cursor images for me to
1235 use.
1236
1237
1238 Any bugs, comments, feedback, questions, and especially code are welcome:
1239
1240 -Chris Barker
1241
1242 Chris.Barker@noaa.gov
1243
1244 """
1245
1246 def __init__(self, *args, **kwargs):
1247 wx.App.__init__(self, *args, **kwargs)
1248
1249 def OnInit(self):
1250 wx.InitAllImageHandlers()
1251 frame = DrawFrame(None, -1, "FloatCanvas Demo App",wx.DefaultPosition,(700,700))
1252
1253 self.SetTopWindow(frame)
1254 frame.Show()
1255
1256 ## check to see if the demo is set to start in a particular mode.
1257 if StartUpDemo == "text":
1258 frame.TestText()
1259 if StartUpDemo == "stext":
1260 frame.TestScaledText()
1261 elif StartUpDemo == "all":
1262 frame.DrawTest()
1263 elif StartUpDemo == "map":
1264 frame.DrawMap()
1265 elif StartUpDemo == "hit":
1266 frame.TestHitTest()
1267 elif StartUpDemo == "hitf":
1268 "starting TestHitTestForeground"
1269 frame.TestHitTestForeground()
1270 elif StartUpDemo == "animate":
1271 "starting TestAnimation"
1272 frame.TestAnimation()
1273 elif StartUpDemo == "speed":
1274 "starting SpeedTest"
1275 frame.SpeedTest()
1276 elif StartUpDemo == "temp":
1277 "starting temp Test"
1278 frame.TempTest()
1279 elif StartUpDemo == "props":
1280 "starting PropertiesChange Test"
1281 frame.PropertiesChangeTest()
1282
1283 return True
1284
1285 def Read_MapGen(filename,stats = 0,AllLines=0):
1286 """
1287 This function reads a MapGen Format file, and
1288 returns a list of NumPy arrays with the line segments in them.
1289
1290 Each NumPy array in the list is an NX2 array of Python Floats.
1291
1292 The demo should have come with a file, "world.dat" that is the
1293 shorelines of the whole world, in MapGen format.
1294
1295 """
1296 import string
1297 file = open(filename,'rt')
1298 data = file.readlines()
1299 data = map(string.strip,data)
1300
1301 Shorelines = []
1302 segment = []
1303 for line in data:
1304 if line:
1305 if line == "# -b": #New segment beginning
1306 if segment: Shorelines.append(Numeric.array(segment))
1307 segment = []
1308 else:
1309 segment.append(map(float,string.split(line)))
1310 if segment: Shorelines.append(Numeric.array(segment))
1311
1312 if stats:
1313 NumSegments = len(Shorelines)
1314 NumPoints = 0
1315 for segment in Shorelines:
1316 NumPoints = NumPoints + len(segment)
1317 AvgPoints = NumPoints / NumSegments
1318 print "Number of Segments: ", NumSegments
1319 print "Average Number of Points per segment: ",AvgPoints
1320 if AllLines:
1321 Lines = []
1322 for segment in Shorelines:
1323 Lines.append(segment[0])
1324 for point in segment[1:-1]:
1325 Lines.append(point)
1326 Lines.append(point)
1327 Lines.append(segment[-1])
1328 return Lines
1329 else:
1330 return Shorelines
1331
1332 ## for the wxPython demo:
1333 try:
1334 import floatcanvas
1335 except ImportError: # if it's not there locally, try the wxPython lib.
1336 from wx.lib import floatcanvas
1337
1338 overview = floatcanvas.__doc__
1339
1340 if __name__ == "__main__":
1341 if not haveNumeric:
1342 print errorText
1343 else:
1344 app = DemoApp(False)# put in True if you want output to go to it's own window.
1345 app.MainLoop()
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367