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