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