]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
1 #!/usr/bin/env python2.3
5 ##First, make sure Numeric or numarray can be imported.
11 # Numeric isn't there, let's try numarray
13 import numarray
as Numeric
14 import numarray
.random_array
as RandomArray
17 # numarray isn't there either
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"
27 #---------------------------------------------------------------------------
29 def BuildDrawFrame(): # this gets called when needed, rather than on import
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
35 import wx
.lib
.colourdb
38 class DrawFrame(wx
.Frame
):
41 A frame used for the FloatCanvas Demo
46 def __init__(self
,parent
, id,title
,position
,size
):
47 wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
)
50 MenuBar
= wx
.MenuBar()
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")
59 item
= draw_menu
.Append(-1, "&Draw Test","Run a test of drawing random components")
60 self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, item
)
62 item
= draw_menu
.Append(-1, "&Line Test","Run a test of drawing random lines")
63 self
.Bind(wx
.EVT_MENU
, self
.LineTest
, item
)
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
)
86 MenuBar
.Append(draw_menu
, "&Tests")
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")
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")
99 self
.SetMenuBar(MenuBar
)
101 self
.CreateStatusBar()
103 self
.Canvas
= NavCanvas
.NavCanvas(self
,
107 BackgroundColor
= "DARK SLATE BLUE")
109 wx
.EVT_CLOSE(self
, self
.OnCloseWindow
)
111 FloatCanvas
.EVT_MOTION(self
.Canvas
, self
.OnMove
)
112 #FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
114 self
.EventsAreBound
= False
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()
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
)
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
)
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
)
139 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, self
.OnWheel
)
140 self
.EventsAreBound
= True
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)
148 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
149 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
150 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
152 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
153 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
154 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
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)
161 FloatCanvas
.EVT_MIDDLE_DOWN(self
.Canvas
, None )
162 FloatCanvas
.EVT_MIDDLE_UP(self
.Canvas
, None )
163 FloatCanvas
.EVT_MIDDLE_DCLICK(self
.Canvas
, None )
165 FloatCanvas
.EVT_RIGHT_DOWN(self
.Canvas
, None )
166 FloatCanvas
.EVT_RIGHT_UP(self
.Canvas
, None )
167 FloatCanvas
.EVT_RIGHT_DCLICK(self
.Canvas
, None )
169 FloatCanvas
.EVT_MOUSEWHEEL(self
.Canvas
, None )
171 self
.EventsAreBound
= False
173 def PrintCoords(self
,event
):
174 print "coords are: %s"%(event
.Coords
,)
175 print "pixel coords are: %s\n"%(event
.GetPosition(),)
177 def OnLeftDown(self
, event
):
178 print "Left Button has been clicked in DrawFrame"
179 self
.PrintCoords(event
)
181 def OnLeftUp(self
, event
):
182 print "Left up in DrawFrame"
183 self
.PrintCoords(event
)
185 def OnLeftDouble(self
, event
):
186 print "Left Double Click in DrawFrame"
187 self
.PrintCoords(event
)
189 def OnMiddleDown(self
, event
):
190 print "Middle Button clicked in DrawFrame"
191 self
.PrintCoords(event
)
193 def OnMiddleUp(self
, event
):
194 print "Middle Button Up in DrawFrame"
195 self
.PrintCoords(event
)
197 def OnMiddleDouble(self
, event
):
198 print "Middle Button Double clicked in DrawFrame"
199 self
.PrintCoords(event
)
201 def OnRightDown(self
, event
):
202 print "Right Button has been clicked in DrawFrame"
203 self
.PrintCoords(event
)
205 def OnRightUp(self
, event
):
206 print "Right Button Up in DrawFrame"
207 self
.PrintCoords(event
)
209 def OnRightDouble(self
, event
):
210 print "Right Button Double clicked in DrawFrame"
211 self
.PrintCoords(event
)
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" )
223 self
.Canvas
.MoveImage( (0, Rot
), "Panel" )
225 def OnMove(self
, event
):
227 Updates the status bar with the world coordinates
229 self
.SetStatusText("%.2f, %.2f"%tuple(event
.Coords
))
231 def OnAbout(self
, event
):
232 print "OnAbout called"
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
)
240 def ZoomToFit(self
,event
):
241 self
.Canvas
.ZoomToBB()
243 def Clear(self
,event
= None):
244 self
.UnBindAllMouseEvents()
245 self
.Canvas
.ClearAll()
246 self
.Canvas
.SetProjectionFun(None)
249 def OnQuit(self
,event
):
252 def OnCloseWindow(self
, event
):
255 def DrawTest(self
,event
=None):
261 self
.BindAllMouseEvents()
265 Canvas
.SetProjectionFun(None)
267 ## Random tests of everything:
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
])
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
])
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
)
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")
305 for j
in range(random
.randint(2,10)):
306 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
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
])
315 for j
in range(random
.randint(2,6)):
316 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
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
,
323 LineColor
= colors
[cl
],
324 FillColor
= colors
[cf
],
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
)
336 String
= "Unscaled text"
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")
344 String
= "Scaled text"
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")
353 Points
= RandomArray
.uniform(Range
[0], Range
[1], (N
,2) )
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))
364 def TestAnimation(self
,event
=None):
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.
378 self
.UnBindAllMouseEvents()
382 Canvas
.SetProjectionFun(None)
384 ## Random tests of everything:
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
])
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
])
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")
417 for j
in range(random
.randint(2,10)):
418 point
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1]))
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
])
428 for j
in range(random
.randint(2,6)):
429 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
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
,
436 LineColor
= colors
[cl
],
437 FillColor
= colors
[cf
],
441 String
= "Scaled text"
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")
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
)
455 self
.Timer
= wx
.PyTimer(self
.ShowFrame
)
456 self
.FrameDelay
= 50 # milliseconds
461 Object
= self
.MovingObject
463 if self
.TimeStep
< self
.NumTimeSteps
:
465 if x
> Range
[1] or x
< Range
[0]:
467 if y
> Range
[1] or y
< Range
[0]:
469 Object
.Move( (self
.dx
,self
.dy
) )
470 Object
.Text
.Move( (self
.dx
,self
.dy
))
473 wx
.GetApp().Yield(True)
478 def MoveMe(self
, Object
):
479 self
.MovingObject
= Object
481 self
.dx
= random
.uniform(Range
[0]/4,Range
[1]/4)
482 self
.dy
= random
.uniform(Range
[0]/4,Range
[1]/4)
485 self
.NumTimeSteps
= 200
487 self
.Timer
.Start(self
.FrameDelay
)
488 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
490 def TestHitTest(self
,event
=None):
493 self
.UnBindAllMouseEvents()
497 Canvas
.SetProjectionFun(None)
499 #Add a Hit-able rectangle
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")
513 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2)
514 R
.Name
= "Line Rectangle"
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")
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")
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")
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")
548 R
= Canvas
.AddCircle(x
+dx
/2, y
+dy
/2, dx
/4, LineWidth
= 2, FillColor
= color
)
549 R
.Name
= color
+ " Circle"
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")
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")
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")
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")
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")
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")
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")
607 color
= "MEDIUM GOLDENROD"
608 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= 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")
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")
624 color
= "MEDIUM VIOLET RED"
625 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= 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")
634 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= 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")
643 R
= Canvas
.AddRectangle(x
, y
, w
+12, h
, LineColor
= None, FillColor
= 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")
652 R
= Canvas
.AddRectangle(x
-12, y
, w
+12, h
, LineColor
= None, FillColor
= 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")
661 L
= Canvas
.AddLine(( (x
, y
), (x
+10, y
+10), (x
+w
, y
+h
) ), LineWidth
= 2, LineColor
= "Red")
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")
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")
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
)
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")
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")
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")
699 self
.Canvas
.ZoomToBB()
701 def TestHitTestForeground(self
,event
=None):
704 self
.UnBindAllMouseEvents()
708 Canvas
.SetProjectionFun(None)
710 #Add a Hitable rectangle
718 R
= Canvas
.AddRectangle(x
, y
, w
, h
, LineWidth
= 2, FillColor
= color
, InForeground
= False)
719 R
.Name
= color
+ "Rectangle"
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")
725 ## A set of Rectangles that move together
727 ## NOTE: In a real app, it might be better to create a new
728 ## custom FloatCanvas DrawObject
730 self
.MovingRects
= []
733 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= 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
) )
740 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= 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
) )
748 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= 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
) )
756 R
= Canvas
.AddRectangle(x
, y
, w
/2, h
/2, LineWidth
= 2, FillColor
= color
, InForeground
= 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
) )
762 self
.Canvas
.ZoomToBB()
764 def RectMoveLeft(self
,Object
):
765 self
.MoveRects("left")
767 def RectMoveRight(self
,Object
):
768 self
.MoveRects("right")
770 def RectMoveUp(self
,Object
):
773 def RectMoveDown(self
,Object
):
774 self
.MoveRects("down")
776 def MoveRects(self
, Dir
):
777 for Object
in self
.MovingRects
:
779 if Dir
== "left": X
-= 10
780 elif Dir
== "right": X
+= 10
781 elif Dir
== "up": Y
+= 10
782 elif Dir
== "down": Y
-= 10
787 def PointSetGotHit(self
, Object
):
788 print Object
.Name
, "Got Hit\n"
790 def RectGotHit(self
, Object
):
791 print Object
.Name
, "Got Hit\n"
793 def RectGotHitRight(self
, Object
):
794 print Object
.Name
, "Got Hit With Right\n"
796 def RectGotHitLeft(self
, Object
):
797 print Object
.Name
, "Got Hit with Left\n"
799 def RectMouseOver(self
, Object
):
800 print "Mouse entered:", Object
.Name
802 def RectMouseLeave(self
, Object
):
803 print "Mouse left ", Object
.Name
806 def TestText(self
, event
= None):
809 self
.BindAllMouseEvents()
812 Canvas
.SetProjectionFun(None)
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)
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)
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")
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")
842 Canvas
.AddPointSet((x
,y
), Color
= "White", Diameter
= 2)
843 self
.Canvas
.AddText("Center Center",x
,y
,Size
= 14,Color
= "Black",Position
= "cc")
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)
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
)
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
)
860 self
.Canvas
.ZoomToBB()
862 def TestScaledText(self
, event
= None):
865 self
.BindAllMouseEvents()
868 Canvas
.SetProjectionFun(None)
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)
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)
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)
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)
902 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
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')
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
)
911 self
.Canvas
.ZoomToBB()
913 def DrawMap(self
,event
= None):
916 self
.BindAllMouseEvents()
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) )
933 def LineTest(self
,event
= None):
939 ## Test of drawing lots of lines
942 Canvas
.SetProjectionFun(None)
943 #start = time.clock()
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()
960 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
962 def SpeedTest(self
,event
=None):
964 BigRange
= (-1000,1000)
967 self
.UnBindAllMouseEvents()
971 Canvas
.SetProjectionFun(None)
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"
981 Canvas
.AddPoint(Point
, Diameter
= 4)
982 print "It took %s seconds to add the points"%(time
.clock() - start
)
985 def PropertiesChangeTest(self
,event
=None):
991 self
.UnBindAllMouseEvents()
995 Canvas
.SetProjectionFun(None)
997 self
.ColorObjectsAll
= []
998 self
.ColorObjectsLine
= []
999 self
.ColorObjectsColor
= []
1000 self
.ColorObjectsText
= []
1001 ##One of each object:
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
)
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
)
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
))
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
)
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
]))
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
,
1058 LineColor
= colors
[cl
],
1059 FillColor
= colors
[cf
],
1060 FillStyle
= 'Solid'))
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
)
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
)
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"))
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"))
1091 Button
= Canvas
.AddRectangle(-10, -12, 20, 3, LineStyle
= None, FillColor
= "Red")
1092 Canvas
.AddScaledText("Click Here To Change Properties",
1098 Button
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.ChangeProperties
)
1102 def ChangeProperties(self
, Object
= None):
1103 colors
= self
.colors
1106 for Object
in self
.ColorObjectsAll
:
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)
1130 def ArrowTest(self
,event
=None):
1132 self
.UnBindAllMouseEvents()
1133 Canvas
= self
.Canvas
1136 Canvas
.SetProjectionFun(None)
1138 # put in a rectangle to get a bounding box
1139 Canvas
.AddRectangle(0,0,20,20,LineColor
= None)
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 )
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
)
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
)
1163 def ArrowClicked(self
,event
):
1164 print "The Arrow was Clicked"
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) )
1175 self
.Canvas
.Draw(Force
= True)
1177 def TempTest(self
, event
= None):
1180 self
.UnBindAllMouseEvents()
1181 Canvas
= self
.Canvas
1183 Canvas
.SetProjectionFun(None)
1187 # Create a random Polygon
1190 point
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1]))
1191 points
.append(point
)
1192 Poly
= Canvas
.AddPolygon(points
,
1194 LineColor
= "Black",
1195 FillColor
= "LightBlue",
1196 FillStyle
= 'Solid')
1198 Poly
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPoly
)
1200 self
.SelectedPoly
= None
1201 self
.SelectPoints
= []
1202 self
.SelectedPoint
= None
1206 def SelectPoly(self
, Object
):
1207 print "In SelectPoly"
1208 Canvas
= self
.Canvas
1209 if Object
is self
.SelectedPoly
:
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")
1220 P
.Bind(FloatCanvas
.EVT_FC_LEFT_DOWN
, self
.SelectPointHit
)
1221 self
.SelectPoints
.append(P
)
1225 def SelectPointHit(self
, Point
):
1226 print "Point Num: %i Hit"%Point
.VerticeNum
1227 self
.SelectedPoint
= Point
1229 def Read_MapGen(self
, filename
, stats
= 0,AllLines
=0):
1231 This function reads a MapGen Format file, and
1232 returns a list of NumPy arrays with the line segments in them.
1234 Each NumPy array in the list is an NX2 array of Python Floats.
1236 The demo should have come with a file, "world.dat" that is the
1237 shorelines of the whole world, in MapGen format.
1241 file = open(filename
,'rt')
1242 data
= file.readlines()
1243 data
= map(string
.strip
,data
)
1249 if line
== "# -b": #New segment beginning
1250 if segment
: Shorelines
.append(Numeric
.array(segment
))
1253 segment
.append(map(float,string
.split(line
)))
1254 if segment
: Shorelines
.append(Numeric
.array(segment
))
1257 NumSegments
= len(Shorelines
)
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
1266 for segment
in Shorelines
:
1267 Lines
.append(segment
[0])
1268 for point
in segment
[1:-1]:
1271 Lines
.append(segment
[-1])
1277 #---------------------------------------------------------------------------
1279 if __name__
== "__main__":
1282 optlist
, args
= getopt
.getopt(sys
.argv
[1:],'l',["local","all","text","map","stext","hit","hitf","animate","speed","temp","props","arrow"])
1285 raise ImportError(errorText
)
1286 StartUpDemo
= "all" # the default
1288 if opt
[0] == "--all":
1290 elif opt
[0] == "--text":
1291 StartUpDemo
= "text"
1292 elif opt
[0] == "--map":
1294 elif opt
[0] == "--stext":
1295 StartUpDemo
= "stext"
1296 elif opt
[0] == "--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"
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")
1319 class DemoApp(wx
.App
):
1323 Under the Draw menu, there are three options:
1325 *Draw Test: will put up a picture of a bunch of randomly generated
1326 objects, of each kind supported.
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.
1333 *Clear: Clears the Canvas.
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
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.
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)
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.
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
1361 Any bugs, comments, feedback, questions, and especially code are welcome:
1365 Chris.Barker@noaa.gov
1369 def __init__(self
, *args
, **kwargs
):
1370 wx
.App
.__init
__(self
, *args
, **kwargs
)
1373 wx
.InitAllImageHandlers()
1374 DrawFrame
= BuildDrawFrame()
1375 frame
= DrawFrame(None, -1, "FloatCanvas Demo App",wx
.DefaultPosition
,(700,700))
1377 self
.SetTopWindow(frame
)
1380 ## check to see if the demo is set to start in a particular mode.
1381 if StartUpDemo
== "text":
1383 if StartUpDemo
== "stext":
1384 frame
.TestScaledText()
1385 elif StartUpDemo
== "all":
1387 elif StartUpDemo
== "map":
1389 elif StartUpDemo
== "hit":
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"
1400 elif StartUpDemo
== "temp":
1401 "starting temp Test"
1403 elif StartUpDemo
== "props":
1404 "starting PropertiesChange Test"
1405 frame
.PropertiesChangeTest()
1406 elif StartUpDemo
== "arrow":
1407 "starting arrow Test"
1412 app
= DemoApp(False)# put in True if you want output to go to it's own window.
1416 # It's not running stand-alone, set up for wxPython demo.
1418 ## TestPanel and runTest used for integration into wxPython Demo
1419 class TestPanel(wx
.Panel
):
1420 def __init__(self
, parent
, log
):
1422 wx
.Panel
.__init
__(self
, parent
, -1)
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
)
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
)
1438 ## TestPanel and runTest used for integration into wxPython Demo
1439 class TestPanel(wx
.Panel
):
1440 def __init__(self
, parent
, 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
)
1448 S
= wx
.BoxSizer(wx
.VERTICAL
)
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)
1457 def OnButton(self
, evt
):
1458 DrawFrame
= BuildDrawFrame()
1459 frame
= DrawFrame(None, -1, "FloatCanvas Drawing Window",wx
.DefaultPosition
,(500,500))
1461 #win = wx.lib.plot.TestFrame(self, -1, "PlotCanvas Demo")
1465 def runTest(frame
, nb
, log
):
1466 win
= TestPanel(nb
, log
)
1469 # import to get the doc
1470 from wx
.lib
import floatcanvas
1471 overview
= floatcanvas
.__doc
__