]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FloatCanvas.py
Another package name fix
[wxWidgets.git] / wxPython / demo / FloatCanvas.py
CommitLineData
2a0495c9 1
2a0495c9 2
095315e2 3#print "running:", wx.__version__
2a0495c9 4##First, make sure Numeric or numarray can be imported.
8b9a4190
RD
5try:
6 import Numeric
42463de2 7 import RandomArray
8b9a4190
RD
8 haveNumeric = True
9except ImportError:
2a0495c9 10 # Numeric isn't there, let's try numarray
42463de2
RD
11 try:
12 import numarray as Numeric
13 import numarray.random_array as RandomArray
14 haveNumeric = True
15 except ImportError:
2a0495c9 16 # numarray isn't there either
42463de2 17 haveNumeric = False
2a0495c9
RD
18 errorText = (
19 "The FloatCanvas requires either the Numeric or numarray module\n\n"
20 "You can get them at:\n"
21 "http://sourceforge.net/projects/numpy\n\n"
22 "NOTE: The Numeric module is substantially faster than numarray for this\n"
23 "purpose, if you have lots of objects\n"
24 )
25
2e839e96 26#---------------------------------------------------------------------------
34a544a6 27
095315e2 28
2a0495c9 29def BuildDrawFrame(): # this gets called when needed, rather than on import
42463de2 30 try:
095315e2 31 from floatcanvas import NavCanvas, FloatCanvas, Resources
42463de2 32 except ImportError: # if it's not there locally, try the wxPython lib.
095315e2 33 from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
2a0495c9
RD
34 import wx.lib.colourdb
35 import time, random
2e839e96 36
8fa876ca 37 class DrawFrame(wx.Frame):
2e839e96 38
8b9a4190 39 """
8b9a4190 40 A frame used for the FloatCanvas Demo
2e839e96 41
8b9a4190 42 """
2e839e96
RD
43
44
42463de2 45 def __init__(self,parent, id,title,position,size):
8fa876ca 46 wx.Frame.__init__(self,parent, id,title,position, size)
2e839e96 47
42463de2 48 ## Set up the MenuBar
8fa876ca 49 MenuBar = wx.MenuBar()
2e839e96 50
8fa876ca 51 file_menu = wx.Menu()
42463de2
RD
52 item = file_menu.Append(-1, "&Close","Close this frame")
53 self.Bind(wx.EVT_MENU, self.OnQuit, item)
095315e2
RD
54
55 item = file_menu.Append(-1, "&SavePNG","Save the current image as a PNG")
56 self.Bind(wx.EVT_MENU, self.OnSavePNG, item)
8b9a4190 57 MenuBar.Append(file_menu, "&File")
2e839e96 58
8fa876ca 59 draw_menu = wx.Menu()
8fa876ca 60
095315e2
RD
61 item = draw_menu.Append(-1, "&Clear","Clear the Canvas")
62 self.Bind(wx.EVT_MENU, self.Clear, item)
63
42463de2
RD
64 item = draw_menu.Append(-1, "&Draw Test","Run a test of drawing random components")
65 self.Bind(wx.EVT_MENU, self.DrawTest, item)
66
67 item = draw_menu.Append(-1, "&Line Test","Run a test of drawing random lines")
68 self.Bind(wx.EVT_MENU, self.LineTest, item)
2e839e96 69
42463de2
RD
70 item = draw_menu.Append(-1, "Draw &Map","Run a test of drawing a map")
71 self.Bind(wx.EVT_MENU, self.DrawMap, item)
095315e2 72
42463de2
RD
73 item = draw_menu.Append(-1, "&Text Test","Run a test of text drawing")
74 self.Bind(wx.EVT_MENU, self.TestText, item)
095315e2 75
42463de2
RD
76 item = draw_menu.Append(-1, "&ScaledText Test","Run a test of text drawing")
77 self.Bind(wx.EVT_MENU, self.TestScaledText, item)
095315e2
RD
78
79 item = draw_menu.Append(-1, "&ScaledTextBox Test","Run a test of the Scaled Text Box")
80 self.Bind(wx.EVT_MENU, self.TestScaledTextBox, item)
81
82 item = draw_menu.Append(-1, "&Bitmap Test","Run a test of the Bitmap Object")
83 self.Bind(wx.EVT_MENU, self.TestBitmap, item)
84
42463de2
RD
85 item = draw_menu.Append(-1, "&Hit Test","Run a test of the hit test code")
86 self.Bind(wx.EVT_MENU, self.TestHitTest, item)
095315e2 87
5e1796ef 88 item = draw_menu.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
42463de2 89 self.Bind(wx.EVT_MENU, self.TestHitTestForeground, item)
095315e2 90
42463de2
RD
91 item = draw_menu.Append(-1, "&Animation","Run a test of Animation")
92 self.Bind(wx.EVT_MENU, self.TestAnimation, item)
095315e2
RD
93
94 #item = draw_menu.Append(-1, "&Speed","Run a test of Drawing Speed")
95 #self.Bind(wx.EVT_MENU, self.SpeedTest, item)
96
5e1796ef
RD
97 item = draw_menu.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
98 self.Bind(wx.EVT_MENU, self.PropertiesChangeTest, item)
095315e2 99
2a0495c9
RD
100 item = draw_menu.Append(-1, "&Arrows","Run a test of Arrows")
101 self.Bind(wx.EVT_MENU, self.ArrowTest, item)
102
095315e2
RD
103 item = draw_menu.Append(-1, "&Hide","Run a test of the Show() Hide() Show() and methods")
104 self.Bind(wx.EVT_MENU, self.HideTest, item)
105
42463de2 106 MenuBar.Append(draw_menu, "&Tests")
2e839e96 107
8fa876ca 108 view_menu = wx.Menu()
42463de2
RD
109 item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
110 self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
8b9a4190 111 MenuBar.Append(view_menu, "&View")
2e839e96 112
8fa876ca 113 help_menu = wx.Menu()
42463de2 114 item = help_menu.Append(-1, "&About",
8b9a4190 115 "More information About this program")
42463de2 116 self.Bind(wx.EVT_MENU, self.OnAbout, item)
8b9a4190 117 MenuBar.Append(help_menu, "&Help")
2e839e96 118
8b9a4190 119 self.SetMenuBar(MenuBar)
2e839e96 120
095315e2
RD
121 self.CreateStatusBar()
122
123
8b9a4190 124 # Add the Canvas
42463de2 125 self.Canvas = NavCanvas.NavCanvas(self,
5e1796ef 126 Debug = 0,
42463de2
RD
127 BackgroundColor = "DARK SLATE BLUE")
128
095315e2
RD
129 self.MsgWindow = wx.TextCtrl(self, wx.ID_ANY,
130 "Look Here for output from events\n",
131 style = (wx.TE_MULTILINE |
132 wx.TE_READONLY |
133 wx.SUNKEN_BORDER)
134 )
135
136 ##Create a sizer to manage the Canvas and message window
137 MainSizer = wx.BoxSizer(wx.VERTICAL)
138 MainSizer.Add(self.Canvas, 4, wx.EXPAND)
139 MainSizer.Add(self.MsgWindow, 1, wx.EXPAND | wx.ALL, 5)
140
141 self.SetSizer(MainSizer)
42463de2
RD
142 wx.EVT_CLOSE(self, self.OnCloseWindow)
143
144 FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove )
42463de2
RD
145
146 self.EventsAreBound = False
147
095315e2 148 ## getting all the colors for random objects
2a0495c9
RD
149 wx.lib.colourdb.updateColourDB()
150 self.colors = wx.lib.colourdb.getColourList()
42463de2
RD
151
152
8b9a4190 153 return None
42463de2 154
095315e2
RD
155 def Log(self, text):
156 self.MsgWindow.AppendText(text)
157 if not text[-1] == "\n":
158 self.MsgWindow.AppendText("\n")
159
160
42463de2
RD
161 def BindAllMouseEvents(self):
162 if not self.EventsAreBound:
163 ## Here is how you catch FloatCanvas mouse events
164 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, self.OnLeftDown )
165 FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
166 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, self.OnLeftDouble )
167
168 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, self.OnMiddleDown )
169 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, self.OnMiddleUp )
170 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, self.OnMiddleDouble )
171
172 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, self.OnRightDown )
173 FloatCanvas.EVT_RIGHT_UP(self.Canvas, self.OnRightUp )
174 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, self.OnRightDouble )
175
176 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, self.OnWheel )
177 self.EventsAreBound = True
178
179 def UnBindAllMouseEvents(self):
2a0495c9
RD
180 ## Here is how you unbind FloatCanvas mouse events
181 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, None )
182 FloatCanvas.EVT_LEFT_UP(self.Canvas, None )
183 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, None)
184
185 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, None )
186 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, None )
187 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, None )
188
189 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, None )
190 FloatCanvas.EVT_RIGHT_UP(self.Canvas, None )
191 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, None )
192
193 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, None )
42463de2
RD
194 FloatCanvas.EVT_LEFT_DOWN(self.Canvas, None )
195 FloatCanvas.EVT_LEFT_UP(self.Canvas, None )
196 FloatCanvas.EVT_LEFT_DCLICK(self.Canvas, None)
197
198 FloatCanvas.EVT_MIDDLE_DOWN(self.Canvas, None )
199 FloatCanvas.EVT_MIDDLE_UP(self.Canvas, None )
200 FloatCanvas.EVT_MIDDLE_DCLICK(self.Canvas, None )
201
202 FloatCanvas.EVT_RIGHT_DOWN(self.Canvas, None )
203 FloatCanvas.EVT_RIGHT_UP(self.Canvas, None )
204 FloatCanvas.EVT_RIGHT_DCLICK(self.Canvas, None )
205
206 FloatCanvas.EVT_MOUSEWHEEL(self.Canvas, None )
207
208 self.EventsAreBound = False
209
42463de2 210
095315e2
RD
211 def PrintCoords(self,event):
212 #print "coords are: %s"%(event.Coords,)
213 #print "pixel coords are: %s\n"%(event.GetPosition(),)
214 self.Log("coords are: %s"%(event.Coords,))
215 self.Log("pixel coords are: %s\n"%(event.GetPosition(),))
216
217 def OnSavePNG(self, event=None):
218 import os
219 dlg = wx.FileDialog(
220 self, message="Save file as ...", defaultDir=os.getcwd(),
221 defaultFile="", wildcard="*.png", style=wx.SAVE
222 )
223 if dlg.ShowModal() == wx.ID_OK:
224 path = dlg.GetPath()
225 if not(path[-4:].lower() == ".png"):
226 path = path+".png"
227 self.Canvas.SaveAsImage(path)
228
229
42463de2 230 def OnLeftDown(self, event):
095315e2 231 self.Log("LeftDown")
42463de2
RD
232 self.PrintCoords(event)
233
234 def OnLeftUp(self, event):
095315e2 235 self.Log("LeftUp")
42463de2
RD
236 self.PrintCoords(event)
237
238 def OnLeftDouble(self, event):
095315e2 239 self.Log("LeftDouble")
42463de2
RD
240 self.PrintCoords(event)
241
242 def OnMiddleDown(self, event):
095315e2 243 self.Log("MiddleDown")
42463de2
RD
244 self.PrintCoords(event)
245
246 def OnMiddleUp(self, event):
095315e2 247 self.Log("MiddleUp")
42463de2
RD
248 self.PrintCoords(event)
249
250 def OnMiddleDouble(self, event):
095315e2 251 self.Log("MiddleDouble")
42463de2
RD
252 self.PrintCoords(event)
253
254 def OnRightDown(self, event):
095315e2 255 self.Log("RightDown")
42463de2
RD
256 self.PrintCoords(event)
257
258 def OnRightUp(self, event):
095315e2 259 self.Log("RightDown")
42463de2
RD
260 self.PrintCoords(event)
261
262 def OnRightDouble(self, event):
095315e2 263 self.Log("RightDouble")
42463de2
RD
264 self.PrintCoords(event)
265
266 def OnWheel(self, event):
095315e2 267 self.Log("Mouse Wheel")
42463de2 268 self.PrintCoords(event)
2a0495c9 269 Rot = event.GetWheelRotation()
2a0495c9
RD
270 Rot = Rot / abs(Rot) * 0.1
271 if event.ControlDown(): # move left-right
272 self.Canvas.MoveImage( (Rot, 0), "Panel" )
273 else: # move up-down
274 self.Canvas.MoveImage( (0, Rot), "Panel" )
275
42463de2
RD
276 def OnMove(self, event):
277 """
2a0495c9 278 Updates the status bar with the world coordinates
42463de2
RD
279 """
280 self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
281
8b9a4190 282 def OnAbout(self, event):
095315e2
RD
283 dlg = wx.MessageDialog(self,
284 "This is a small program to demonstrate\n"
285 "the use of the FloatCanvas\n",
286 "About Me",
287 wx.OK | wx.ICON_INFORMATION)
8b9a4190
RD
288 dlg.ShowModal()
289 dlg.Destroy()
2e839e96 290
8b9a4190
RD
291 def ZoomToFit(self,event):
292 self.Canvas.ZoomToBB()
2e839e96 293
8b9a4190 294 def Clear(self,event = None):
42463de2
RD
295 self.UnBindAllMouseEvents()
296 self.Canvas.ClearAll()
297 self.Canvas.SetProjectionFun(None)
8b9a4190 298 self.Canvas.Draw()
2e839e96 299
8b9a4190
RD
300 def OnQuit(self,event):
301 self.Close(True)
2e839e96 302
8b9a4190
RD
303 def OnCloseWindow(self, event):
304 self.Destroy()
2e839e96 305
42463de2 306 def DrawTest(self,event=None):
5e1796ef 307 wx.GetApp().Yield()
2a0495c9 308
8b9a4190 309 Range = (-10,10)
42463de2
RD
310 colors = self.colors
311
312 self.BindAllMouseEvents()
8b9a4190 313 Canvas = self.Canvas
42463de2
RD
314
315 Canvas.ClearAll()
316 Canvas.SetProjectionFun(None)
317
095315e2 318 ############# Random tests of everything ##############
2e839e96 319
8b9a4190 320 # Rectangles
42463de2 321 for i in range(3):
095315e2 322 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
8b9a4190
RD
323 lw = random.randint(1,5)
324 cf = random.randint(0,len(colors)-1)
095315e2
RD
325 wh = (random.randint(1,5), random.randint(1,5))
326 Canvas.AddRectangle(xy, wh, LineWidth = lw, FillColor = colors[cf])
2e839e96 327
42463de2
RD
328 # Ellipses
329 for i in range(3):
095315e2 330 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
8b9a4190
RD
331 lw = random.randint(1,5)
332 cf = random.randint(0,len(colors)-1)
333 h = random.randint(1,5)
334 w = random.randint(1,5)
095315e2 335 Canvas.AddEllipse(xy, (h,w), LineWidth = lw,FillColor = colors[cf])
2e839e96 336
5e1796ef
RD
337 # Points
338 for i in range(5):
095315e2 339 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
5e1796ef
RD
340 D = random.randint(1,50)
341 cf = random.randint(0,len(colors)-1)
095315e2
RD
342 Canvas.AddPoint(xy, Color = colors[cf], Diameter = D)
343
344 # SquarePoints
345 for i in range(500):
346 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
347 S = random.randint(1, 50)
348 cf = random.randint(0,len(colors)-1)
349 Canvas.AddSquarePoint(xy, Color = colors[cf], Size = S)
350
2a0495c9 351 # Circles
8b9a4190 352 for i in range(5):
095315e2 353 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
8b9a4190
RD
354 D = random.randint(1,5)
355 lw = random.randint(1,5)
356 cf = random.randint(0,len(colors)-1)
357 cl = random.randint(0,len(colors)-1)
095315e2
RD
358 Canvas.AddCircle(xy, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
359 Canvas.AddText("Circle # %i"%(i), xy, Size = 12, BackgroundColor = None, Position = "cc")
2a0495c9 360 # Lines
8b9a4190
RD
361 for i in range(5):
362 points = []
363 for j in range(random.randint(2,10)):
364 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
365 points.append(point)
366 lw = random.randint(1,10)
367 cf = random.randint(0,len(colors)-1)
368 cl = random.randint(0,len(colors)-1)
42463de2 369 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
2a0495c9 370 # Polygons
8b9a4190
RD
371 for i in range(3):
372 points = []
373 for j in range(random.randint(2,6)):
374 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
375 points.append(point)
376 lw = random.randint(1,6)
377 cf = random.randint(0,len(colors)-1)
378 cl = random.randint(0,len(colors)-1)
42463de2
RD
379 Canvas.AddPolygon(points,
380 LineWidth = lw,
381 LineColor = colors[cl],
382 FillColor = colors[cf],
383 FillStyle = 'Solid')
2e839e96 384
8b9a4190
RD
385 ## Pointset
386 for i in range(4):
387 points = []
388 points = RandomArray.uniform(Range[0],Range[1],(100,2))
389 cf = random.randint(0,len(colors)-1)
390 D = random.randint(1,4)
42463de2 391 Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
2e839e96 392
8b9a4190 393 # Text
42463de2
RD
394 String = "Unscaled text"
395 for i in range(3):
8b9a4190
RD
396 ts = random.randint(10,40)
397 cf = random.randint(0,len(colors)-1)
095315e2
RD
398 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
399 Canvas.AddText(String, xy, Size = ts, Color = colors[cf], Position = "cc")
42463de2
RD
400
401 # Scaled Text
402 String = "Scaled text"
403 for i in range(3):
404 ts = random.random()*3 + 0.2
405 cf = random.randint(0,len(colors)-1)
095315e2
RD
406 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
407 Canvas.AddScaledText(String, Point, Size = ts, Color = colors[cf], Position = "cc")
42463de2 408
2a0495c9
RD
409 # Arrows
410 N = 5
411 Points = RandomArray.uniform(Range[0], Range[1], (N,2) )
412 for i in range(N):
413 Canvas.AddArrow(Points[i],
414 random.uniform(20,100),
415 Direction = random.uniform(0,360),
416 LineWidth = random.uniform(1,5),
417 LineColor = colors[random.randint(0,len(colors)-1)],
418 ArrowHeadAngle = random.uniform(20,90))
419
42463de2
RD
420 Canvas.ZoomToBB()
421
422 def TestAnimation(self,event=None):
423 """
424
425 In this test, a relatively complex background is drawn, and
426 a simple object placed in the foreground is moved over
427 it. This demonstrates how to use the InForeground attribute
428 to make an object in the foregorund draw fast, without
429 having to re-draw the whole background.
430
431 """
5e1796ef 432 wx.GetApp().Yield()
42463de2
RD
433 Range = (-10,10)
434 self.Range = Range
435
436 self.UnBindAllMouseEvents()
437 Canvas = self.Canvas
438
439 Canvas.ClearAll()
440 Canvas.SetProjectionFun(None)
441
095315e2 442 ## Random tests of everything:
42463de2
RD
443 colors = self.colors
444 # Rectangles
445 for i in range(3):
095315e2 446 xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
42463de2
RD
447 lw = random.randint(1,5)
448 cf = random.randint(0,len(colors)-1)
095315e2
RD
449 wh = (random.randint(1,5), random.randint(1,5) )
450 Canvas.AddRectangle(xy, wh, LineWidth = lw, FillColor = colors[cf])
2e839e96 451
42463de2
RD
452 # Ellipses
453 for i in range(3):
095315e2 454 xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
42463de2
RD
455 lw = random.randint(1,5)
456 cf = random.randint(0,len(colors)-1)
095315e2
RD
457 wh = (random.randint(1,5), random.randint(1,5) )
458 Canvas.AddEllipse(xy, wh, LineWidth = lw, FillColor = colors[cf])
2e839e96 459
42463de2
RD
460 # Circles
461 for i in range(5):
095315e2 462 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
42463de2
RD
463 D = random.randint(1,5)
464 lw = random.randint(1,5)
465 cf = random.randint(0,len(colors)-1)
466 cl = random.randint(0,len(colors)-1)
095315e2
RD
467 Canvas.AddCircle(xy, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
468 Canvas.AddText("Circle # %i"%(i), xy, Size = 12, BackgroundColor = None, Position = "cc")
2e839e96 469
42463de2
RD
470 # Lines
471 for i in range(5):
472 points = []
473 for j in range(random.randint(2,10)):
474 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
475 points.append(point)
476 lw = random.randint(1,10)
477 cf = random.randint(0,len(colors)-1)
478 cl = random.randint(0,len(colors)-1)
479 Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
2e839e96 480
42463de2
RD
481 # Polygons
482 for i in range(3):
483 points = []
484 for j in range(random.randint(2,6)):
485 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
486 points.append(point)
487 lw = random.randint(1,6)
488 cf = random.randint(0,len(colors)-1)
489 cl = random.randint(0,len(colors)-1)
490 Canvas.AddPolygon(points,
491 LineWidth = lw,
492 LineColor = colors[cl],
493 FillColor = colors[cf],
494 FillStyle = 'Solid')
2e839e96 495
42463de2
RD
496 # Scaled Text
497 String = "Scaled text"
498 for i in range(3):
499 ts = random.random()*3 + 0.2
500 cf = random.randint(0,len(colors)-1)
095315e2
RD
501 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
502 Canvas.AddScaledText(String, xy, Size = ts, Color = colors[cf], Position = "cc")
42463de2
RD
503
504
505 # Now the Foreground Object:
095315e2
RD
506 C = Canvas.AddCircle((0,0), 7, LineWidth = 2,LineColor = "Black",FillColor = "Red", InForeground = True)
507 T = Canvas.AddScaledText("Click to Move", (0,0), Size = 0.6, Position = 'cc', InForeground = True)
42463de2
RD
508 C.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.MoveMe)
509 C.Text = T
510
511 self.Timer = wx.PyTimer(self.ShowFrame)
512 self.FrameDelay = 50 # milliseconds
2e839e96 513
42463de2
RD
514 Canvas.ZoomToBB()
515
516 def ShowFrame(self):
517 Object = self.MovingObject
518 Range = self.Range
519 if self.TimeStep < self.NumTimeSteps:
520 x,y = Object.XY
521 if x > Range[1] or x < Range[0]:
522 self.dx = -self.dx
523 if y > Range[1] or y < Range[0]:
524 self.dy = -self.dy
525 Object.Move( (self.dx,self.dy) )
526 Object.Text.Move( (self.dx,self.dy))
527 self.Canvas.Draw()
528 self.TimeStep += 1
5e1796ef 529 wx.GetApp().Yield(True)
42463de2
RD
530 else:
531 self.Timer.Stop()
2e839e96 532
42463de2
RD
533
534 def MoveMe(self, Object):
535 self.MovingObject = Object
536 Range = self.Range
537 self.dx = random.uniform(Range[0]/4,Range[1]/4)
538 self.dy = random.uniform(Range[0]/4,Range[1]/4)
539 #import time
540 #start = time.time()
5e1796ef 541 self.NumTimeSteps = 200
42463de2
RD
542 self.TimeStep = 1
543 self.Timer.Start(self.FrameDelay)
544 #print "Did %i frames in %f seconds"%(N, (time.time() - start) )
2e839e96 545
42463de2 546 def TestHitTest(self,event=None):
5e1796ef 547 wx.GetApp().Yield()
42463de2
RD
548
549 self.UnBindAllMouseEvents()
550 Canvas = self.Canvas
551
552 Canvas.ClearAll()
553 Canvas.SetProjectionFun(None)
554
2a0495c9 555 #Add a Hit-able rectangle
42463de2
RD
556 w, h = 60, 20
557
558 dx = 80
559 dy = 40
095315e2 560 x, y = 20, 20
5e1796ef 561 FontSize = 8
2e839e96 562
42463de2 563 #Add one that is not HitAble
095315e2
RD
564 Canvas.AddRectangle((x,y), (w, h), LineWidth = 2)
565 Canvas.AddText("Not Hit-able", (x,y), Size = FontSize, Position = "bl")
2e839e96 566
42463de2
RD
567
568 x += dx
095315e2 569 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2)
42463de2
RD
570 R.Name = "Line Rectangle"
571 R.HitFill = False
5e1796ef 572 R.HitLineWidth = 5 # Makes it a little easier to hit
42463de2 573 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
095315e2
RD
574 Canvas.AddText("Left Click Line", (x,y), Size = FontSize, Position = "bl")
575 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
576
577 x += dx
578 color = "Red"
095315e2 579 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
580 R.Name = color + "Rectangle"
581 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
095315e2
RD
582 Canvas.AddText("Left Click Fill", (x, y), Size = FontSize, Position = "bl")
583 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
584
585 x = 20
586 y += dy
587 color = "LightBlue"
095315e2 588 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
589 R.Name = color + " Rectangle"
590 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
095315e2
RD
591 Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
592 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
593
594 x += dx
595 color = "Grey"
095315e2 596 R = Canvas.AddEllipse((x, y), (w, h),LineWidth = 2,FillColor = color)
42463de2
RD
597 R.Name = color +" Ellipse"
598 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
095315e2
RD
599 Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
600 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
601
602 x += dx
603 color = "Brown"
095315e2 604 R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2, FillColor = color)
42463de2
RD
605 R.Name = color + " Circle"
606 R.HitFill = True
607 R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
095315e2
RD
608 Canvas.AddText("Left D-Click Fill", (x, y), Size = FontSize, Position = "bl")
609 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
610
611 x = 20
612 y += dy
613 color = "Pink"
095315e2 614 R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2,FillColor = color)
42463de2
RD
615 R.Name = color + " Circle"
616 R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
095315e2
RD
617 Canvas.AddText("Left Up Fill", (x, y), Size = FontSize, Position = "bl")
618 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
619
620 x += dx
621 color = "White"
095315e2 622 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
623 R.Name = color + " Rectangle"
624 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
095315e2
RD
625 Canvas.AddText("Middle Down", (x, y), Size = FontSize, Position = "bl")
626 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
627
628 x += dx
629 color = "AQUAMARINE"
095315e2 630 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
631 R.Name = color + " Rectangle"
632 R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
095315e2
RD
633 Canvas.AddText("Middle Up", (x, y), Size = FontSize, Position = "bl")
634 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
635
636 x = 20
637 y += dy
638 color = "CORAL"
095315e2 639 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
640 R.Name = color + " Rectangle"
641 R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
095315e2
RD
642 Canvas.AddText("Middle DoubleClick", (x, y), Size = FontSize, Position = "bl")
643 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
644
645 x += dx
646 color = "CYAN"
095315e2 647 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
648 R.Name = color + " Rectangle"
649 R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
095315e2
RD
650 Canvas.AddText("Right Up", (x, y), Size = FontSize, Position = "bl")
651 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
652
653 x += dx
654 color = "LIME GREEN"
095315e2 655 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
656 R.Name = color + " Rectangle"
657 R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
095315e2
RD
658 Canvas.AddText("Right Double Click", (x, y), Size = FontSize, Position = "bl")
659 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
660
661 x = 20
662 y += dy
663 color = "MEDIUM GOLDENROD"
095315e2 664 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
665 R.Name = color
666 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
667 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
095315e2
RD
668 Canvas.AddText("L and R Click", (x, y), Size = FontSize, Position = "bl")
669 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
670
671 x += dx
672 color = "SALMON"
095315e2 673 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
674 R.Name = color + " Rectangle"
675 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
095315e2
RD
676 Canvas.AddText("Mouse Enter", (x, y), Size = FontSize, Position = "bl")
677 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
678
679 x += dx
680 color = "MEDIUM VIOLET RED"
095315e2 681 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
682 R.Name = color
683 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
095315e2
RD
684 Canvas.AddText("Mouse Leave", (x, y), Size = FontSize, Position = "bl")
685 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
686
687 x = 20
688 y += dy
689 color = "SKY BLUE"
095315e2 690 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
42463de2
RD
691 R.Name = color
692 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
693 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
095315e2
RD
694 Canvas.AddText("Enter and Leave", (x, y), Size = FontSize, Position = "bl")
695 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
696
697 x += dx
698 color = "WHEAT"
095315e2 699 R = Canvas.AddRectangle((x, y), (w+12, h), LineColor = None, FillColor = color)
42463de2
RD
700 R.Name = color
701 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
702 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
095315e2
RD
703 Canvas.AddText("Mouse Enter&Leave", (x, y), Size = FontSize, Position = "bl")
704 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
705
706 x += dx
707 color = "KHAKI"
095315e2 708 R = Canvas.AddRectangle((x-12, y), (w+12, h), LineColor = None, FillColor = color)
42463de2
RD
709 R.Name = color
710 R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
711 R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
095315e2
RD
712 Canvas.AddText("Mouse ENter&Leave", (x, y), Size = FontSize, Position = "bl")
713 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
714
715 x = 20
716 y += dy
717 L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
718 L.Name = "A Line"
719 L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
095315e2
RD
720 Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
721 Canvas.AddText(L.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
722
723 x += dx
724 color = "SEA GREEN"
725 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)
726 R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
727 R.Name = color + " Polygon"
728 R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
095315e2
RD
729 Canvas.AddText("RIGHT_DOWN", (x, y), Size = FontSize, Position = "bl")
730 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
731
732 x += dx
733 color = "Red"
734 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)
735 R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
736 R.Name = "PointSet"
737 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
095315e2
RD
738 Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
739 Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
42463de2
RD
740
741 x = 20
742 y += dy
095315e2 743 T = Canvas.AddText("Hit-able Text", (x, y), Size = 15, Color = "Red", Position = 'tl')
42463de2
RD
744 T.Name = "Hit-able Text"
745 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
095315e2 746 Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
42463de2
RD
747
748 x += dx
095315e2 749 T = Canvas.AddScaledText("Scaled Text", (x, y), Size = 1./2*h, Color = "Pink", Position = 'bl')
42463de2
RD
750 Canvas.AddPointSet( (x, y), Diameter = 3)
751 T.Name = "Scaled Text"
752 T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
095315e2
RD
753 Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "tl")
754
755 x += dx
756 color = "Cyan"
757 Point = (x + w/2, y)
758 #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)
759 R = Canvas.AddSquarePoint(Point, Size = 8, Color = color)
760 R.Name = "SquarePoint"
761 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
762 Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
763 Canvas.AddText(R.Name, (x, y), Size = FontSize, Position = "tl")
764
42463de2
RD
765
766 self.Canvas.ZoomToBB()
767
768 def TestHitTestForeground(self,event=None):
5e1796ef 769 wx.GetApp().Yield()
42463de2
RD
770
771 self.UnBindAllMouseEvents()
772 Canvas = self.Canvas
773
774 Canvas.ClearAll()
775 Canvas.SetProjectionFun(None)
776
777 #Add a Hitable rectangle
778 w, h = 60, 20
779
780 dx = 80
781 dy = 40
782 x,y = 20, 20
2e839e96 783
42463de2 784 color = "Red"
095315e2 785 R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color, InForeground = False)
42463de2
RD
786 R.Name = color + "Rectangle"
787 R.HitFill = True
788 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
095315e2
RD
789 Canvas.AddText("Left Click Fill", (x, y), Position = "bl")
790 Canvas.AddText(R.Name, (x, y+h), Position = "tl")
42463de2
RD
791
792 ## A set of Rectangles that move together
2e839e96 793
42463de2
RD
794 ## NOTE: In a real app, it might be better to create a new
795 ## custom FloatCanvas DrawObject
2e839e96 796
42463de2 797 self.MovingRects = []
095315e2 798 WH = (w/2, h/2)
42463de2
RD
799 x += dx
800 color = "LightBlue"
095315e2 801 R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
42463de2
RD
802 R.HitFill = True
803 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
095315e2 804 L = Canvas.AddText("Left", (x + w/4, y + h/4), Position = "cc", InForeground = True)
42463de2
RD
805 self.MovingRects.extend( (R,L) )
806
807 x += w/2
095315e2 808 R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
42463de2
RD
809 R.HitFill = True
810 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
095315e2 811 L = Canvas.AddText("Right", (x + w/4, y + h/4), Position = "cc", InForeground = True)
42463de2
RD
812 self.MovingRects.extend( (R,L) )
813
814 x -= w/2
815 y += h/2
095315e2 816 R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
42463de2
RD
817 R.HitFill = True
818 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
095315e2 819 L = Canvas.AddText("Up", (x + w/4, y + h/4), Position = "cc", InForeground = True)
42463de2
RD
820 self.MovingRects.extend( (R,L) )
821
822
823 x += w/2
095315e2 824 R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
42463de2
RD
825 R.HitFill = True
826 R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
095315e2 827 L = Canvas.AddText("Down", (x + w/4, y + h/4), Position = "cc", InForeground = True)
42463de2
RD
828 self.MovingRects.extend( (R,L) )
829
830 self.Canvas.ZoomToBB()
831
832 def RectMoveLeft(self,Object):
833 self.MoveRects("left")
2e839e96 834
42463de2
RD
835 def RectMoveRight(self,Object):
836 self.MoveRects("right")
2e839e96 837
42463de2
RD
838 def RectMoveUp(self,Object):
839 self.MoveRects("up")
2e839e96 840
42463de2
RD
841 def RectMoveDown(self,Object):
842 self.MoveRects("down")
2e839e96 843
42463de2
RD
844 def MoveRects(self, Dir):
845 for Object in self.MovingRects:
846 X,Y = Object.XY
847 if Dir == "left": X -= 10
848 elif Dir == "right": X += 10
849 elif Dir == "up": Y += 10
850 elif Dir == "down": Y -= 10
095315e2 851 Object.SetPoint((X,Y))
42463de2 852 self.Canvas.Draw()
2e839e96 853
42463de2 854 def PointSetGotHit(self, Object):
095315e2 855 self.Log(Object.Name + "Got Hit\n")
42463de2
RD
856
857 def RectGotHit(self, Object):
095315e2 858 self.Log(Object.Name + "Got Hit\n")
42463de2
RD
859
860 def RectGotHitRight(self, Object):
095315e2 861 self.Log(Object.Name + "Got Hit With Right\n")
42463de2
RD
862
863 def RectGotHitLeft(self, Object):
095315e2 864 self.Log(Object.Name + "Got Hit with Left\n")
42463de2
RD
865
866 def RectMouseOver(self, Object):
095315e2 867 self.Log("Mouse entered:" + Object.Name)
42463de2
RD
868
869 def RectMouseLeave(self, Object):
095315e2 870 self.Log("Mouse left " + Object.Name)
42463de2
RD
871
872
873 def TestText(self, event= None):
5e1796ef 874 wx.GetApp().Yield()
42463de2
RD
875
876 self.BindAllMouseEvents()
877 Canvas = self.Canvas
878 Canvas.ClearAll()
879 Canvas.SetProjectionFun(None)
880
095315e2 881 Point = (3, 0)
42463de2
RD
882
883 ## Add a non-visible rectangle, just to get a Bounding Box
884 ## Text objects have a zero-size bounding box, because it changes with zoom
095315e2
RD
885 Canvas.AddRectangle((-10,-10),
886 (20,20),
887 LineWidth = 1,
888 LineColor = None)
42463de2
RD
889
890 # Text
891 String = "Some text"
095315e2
RD
892 self.Canvas.AddText("Top Left",Point,Size = 14,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
893 self.Canvas.AddText("Bottom Left",Point,Size = 14,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
894 self.Canvas.AddText("Top Right",Point,Size = 14,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
895 self.Canvas.AddText("Bottom Right",Point,Size = 14,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
896 Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
42463de2 897
095315e2 898 Point = (3, 2)
2e839e96 899
095315e2
RD
900 Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
901 self.Canvas.AddText("Top Center",Point,Size = 14,Color = "Black",Position = "tc")
902 self.Canvas.AddText("Bottom Center",Point,Size = 14,Color = "White",Position = "bc")
42463de2 903
095315e2 904 Point = (3, 4)
2e839e96 905
095315e2
RD
906 Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
907 self.Canvas.AddText("Center Right",Point,Size = 14,Color = "Black",Position = "cr")
908 self.Canvas.AddText("Center Left",Point,Size = 14,Color = "Black",Position = "cl")
42463de2 909
095315e2 910 Point = (3, -2)
2e839e96 911
095315e2
RD
912 Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
913 self.Canvas.AddText("Center Center",
914 Point, Size = 14,
915 Color = "Black",
916 Position = "cc")
42463de2 917
095315e2
RD
918 self.Canvas.AddText("40 Pixels", (-10,8), Size = 40)
919 self.Canvas.AddText("20 Pixels", (-10,5), Size = 20)
920 self.Canvas.AddText("10 Pixels", (-10,3), Size = 10)
42463de2 921
095315e2
RD
922 self.Canvas.AddText("MODERN Font", (-10, 0), Family = wx.MODERN)
923 self.Canvas.AddText("DECORATIVE Font", (-10, -1), Family = wx.DECORATIVE)
924 self.Canvas.AddText("ROMAN Font", (-10, -2), Family = wx.ROMAN)
925 self.Canvas.AddText("SCRIPT Font", (-10, -3), Family = wx.SCRIPT)
926 self.Canvas.AddText("ROMAN BOLD Font", (-10, -4), Family = wx.ROMAN, Weight=wx.BOLD)
927 self.Canvas.AddText("ROMAN ITALIC BOLD Font", (-10, -5), Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
42463de2
RD
928
929 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
930 Font = wx.Font(20, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
095315e2 931 self.Canvas.AddText("zapf chancery Font", (-10, -6), Font = Font)
42463de2
RD
932
933 self.Canvas.ZoomToBB()
934
935 def TestScaledText(self, event= None):
5e1796ef 936 wx.GetApp().Yield()
42463de2
RD
937
938 self.BindAllMouseEvents()
939 Canvas = self.Canvas
940 Canvas.ClearAll()
941 Canvas.SetProjectionFun(None)
942
095315e2 943 Point = (0, 0)
42463de2 944
095315e2
RD
945 T = Canvas.AddScaledText("Top Left",
946 Point,
947 Size = 5,
948 Color = "Yellow",
949 BackgroundColor = "Blue",
950 Position = "tl")
951 T = Canvas.AddScaledText("Bottom Left",Point,Size = 5,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
952 T = Canvas.AddScaledText("Top Right",Point,Size = 5,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
953 T = Canvas.AddScaledText("Bottom Right",Point,Size = 5,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
954 Canvas.AddPointSet((Point), Color = "Red", Diameter = 4)
42463de2
RD
955
956
095315e2 957 Point = (0, 20)
2e839e96 958
095315e2
RD
959 Canvas.AddScaledText("Top Center",Point,Size = 7,Color = "Black",Position = "tc")
960 Canvas.AddScaledText("Bottom Center",Point,Size = 7,Color = "White",Position = "bc")
961 Canvas.AddPointSet((Point), Color = "White", Diameter = 4)
42463de2 962
095315e2 963 Point = (0, -20)
2e839e96 964
095315e2
RD
965 Canvas.AddScaledText("Center Right",Point,Size = 9,Color = "Black",Position = "cr")
966 Canvas.AddScaledText("Center Left",Point,Size = 9,Color = "Black",Position = "cl")
967 Canvas.AddPointSet((Point), Color = "White", Diameter = 4)
42463de2
RD
968
969 x = -200
970
095315e2
RD
971 self.Canvas.AddScaledText("MODERN Font", (x, 0), Size = 7, Family = wx.MODERN, Color = (0,0,0))
972 self.Canvas.AddScaledText("DECORATIVE Font", (x, -10), Size = 7, Family = wx.DECORATIVE, Color = (0,0,1))
973 self.Canvas.AddScaledText("ROMAN Font", (x, -20), Size = 7, Family = wx.ROMAN)
974 self.Canvas.AddScaledText("SCRIPT Font", (x, -30), Size = 7, Family = wx.SCRIPT)
975 self.Canvas.AddScaledText("ROMAN BOLD Font", (x, -40), Size = 7, Family = wx.ROMAN, Weight=wx.BOLD)
976 self.Canvas.AddScaledText("ROMAN ITALIC BOLD Font", (x, -50), Size = 7, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
42463de2
RD
977 Canvas.AddPointSet((x,0), Color = "White", Diameter = 4)
978
979
980 # NOTE: this font exists on my Linux box..who knows were else you'll find it!
095315e2 981 Point = (-100, 50)
42463de2 982 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "zapf chancery")
095315e2 983 T = self.Canvas.AddScaledText("zapf chancery Font", Point, Size = 20, Font = Font, Position = 'bc')
42463de2 984
095315e2 985 Point = (-50, -50)
42463de2 986 Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "bookman")
095315e2
RD
987 T = self.Canvas.AddScaledText("Bookman Font", Point, Size = 8, Font = Font)
988
989 self.Canvas.ZoomToBB()
990
991 def TestScaledTextBox(self, event= None):
992 wx.GetApp().Yield()
993
994 self.UnBindAllMouseEvents()
995 Canvas = self.Canvas
996 Canvas.ClearAll()
997 Canvas.SetProjectionFun(None)
998
999 Point = (45,40)
1000 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1001 Point,
1002 2,
1003 Color = "Black",
1004 BackgroundColor = None,
1005 LineColor = "Red",
1006 LineStyle = "Solid",
1007 LineWidth = 1,
1008 Width = None,
1009 PadSize = 5,
1010 Family = wx.ROMAN,
1011 Style = wx.NORMAL,
1012 Weight = wx.NORMAL,
1013 Underline = False,
1014 Position = 'br',
1015 Alignment = "left",
1016 InForeground = False)
1017
1018 # All defaults
1019 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1020 Point,
1021 2)
1022
1023 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1024 Point,
1025 2,
1026 BackgroundColor = "Yellow",
1027 LineColor = "Red",
1028 LineStyle = "Solid",
1029 PadSize = 5,
1030 Family = wx.TELETYPE,
1031 Position = 'bl')
1032
1033 Box = Canvas.AddScaledTextBox("A String\nThis box is clickable",
1034 Point,
1035 2,
1036 BackgroundColor = "Yellow",
1037 LineColor = "Red",
1038 LineStyle = "Solid",
1039 PadSize = 5,
1040 Family = wx.TELETYPE,
1041 Position = 'tr')
1042
1043 Box.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.binding2)
1044
1045 Canvas.AddPoint(Point, Diameter = 4)
1046
1047 Point = (45,15)
1048 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1049 Point,
1050 2,
1051 Color = "Black",
1052 BackgroundColor = 'Red',
1053 LineColor = "Blue",
1054 LineStyle = "LongDash",
1055 LineWidth = 2,
1056 Width = None,
1057 PadSize = 5,
1058 Family = wx.TELETYPE,
1059 Style = wx.NORMAL,
1060 Weight = wx.NORMAL,
1061 Underline = False,
1062 Position = 'cr',
1063 Alignment = "left",
1064 InForeground = False)
1065
1066 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1067 Point,
1068 1.5,
1069 Color = "Black",
1070 BackgroundColor = 'Red',
1071 LineColor = "Blue",
1072 LineStyle = "LongDash",
1073 LineWidth = 2,
1074 Width = None,
1075 PadSize = 5,
1076 Family = wx.TELETYPE,
1077 Style = wx.NORMAL,
1078 Weight = wx.NORMAL,
1079 Underline = False,
1080 Position = 'cl',
1081 Alignment = "left",
1082 InForeground = False)
1083
1084 Canvas.AddPoint(Point, Diameter = 4)
1085
1086 Point = (45,-10)
1087 Box = Canvas.AddScaledTextBox("A Two Line\nString",
1088 Point,
1089 2,
1090 Color = "Black",
1091 BackgroundColor = 'Red',
1092 LineColor = "Blue",
1093 LineStyle = "LongDash",
1094 LineWidth = 2,
1095 Width = None,
1096 PadSize = 3,
1097 Family = wx.TELETYPE,
1098 Style = wx.NORMAL,
1099 Weight = wx.NORMAL,
1100 Underline = False,
1101 Position = 'tc',
1102 Alignment = "left",
1103 InForeground = False)
1104
1105 Box = Canvas.AddScaledTextBox("A three\nLine\nString",
1106 Point,
1107 1.5,
1108 Color = "Black",
1109 BackgroundColor = 'Red',
1110 LineColor = "Blue",
1111 LineStyle = "LongDash",
1112 LineWidth = 2,
1113 Width = None,
1114 PadSize = 0.5,
1115 Family = wx.TELETYPE,
1116 Style = wx.NORMAL,
1117 Weight = wx.NORMAL,
1118 Underline = False,
1119 Position = 'bc',
1120 Alignment = "left",
1121 InForeground = False)
1122
1123
1124 Canvas.AddPoint(Point, Diameter = 4)
1125
1126 Box = Canvas.AddScaledTextBox("Some Auto Wrapped Text. There is enough to do.",
1127 (80,40),
1128 2,
1129 BackgroundColor = 'White',
1130 LineWidth = 2,
1131 Width = 20,
1132 PadSize = 0.5,
1133 Family = wx.TELETYPE,
1134 )
1135
1136 Box = Canvas.AddScaledTextBox("Some more auto wrapped text. Wrapped to a different width.\n\nThis is another paragraph.",
1137 (80,20),
1138 2,
1139 BackgroundColor = 'White',
1140 LineWidth = 2,
1141 Width = 40,
1142 PadSize = 0.5,
1143 Family = wx.ROMAN,
1144 Alignment = "right"
1145 )
1146 Point = Numeric.array((100, -20), Numeric.Float)
1147 Box = Canvas.AddScaledTextBox("Here is even more auto wrapped text. This time the line spacing is set to 0.8. \n\nThe Padding is set to 0.",
1148 Point,
1149 Size = 3,
1150 BackgroundColor = 'White',
1151 LineWidth = 1,
1152 Width = 40,
1153 PadSize = 0.0,
1154 Family = wx.ROMAN,
1155 Position = "cc",
1156 LineSpacing = 0.8
1157 )
1158 Canvas.AddPoint(Point, "Red", 2)
1159
1160 Point = Numeric.array((0, -40), Numeric.Float)
1161 # Point = Numeric.array((0, 0), Numeric.Float)
1162 for Position in ["tl", "bl", "tr", "br"]:
1163 # for Position in ["br"]:
1164 Box = Canvas.AddScaledTextBox("Here is a\nfour liner\nanother line\nPosition=%s"%Position,
1165 Point,
1166 Size = 4,
1167 Color = "Red",
1168 BackgroundColor = None,#'LightBlue',
1169 LineWidth = 1,
1170 LineColor = "White",
1171 Width = None,
1172 PadSize = 2,
1173 Family = wx.ROMAN,
1174 Position = Position,
1175 LineSpacing = 0.8
1176 )
1177 Canvas.AddPoint(Point, "Red", 4)
1178
1179 Point = Numeric.array((-20, 60), Numeric.Float)
1180 Box = Canvas.AddScaledTextBox("Here is some\ncentered\ntext",
1181 Point,
1182 Size = 4,
1183 Color = "Red",
1184 BackgroundColor = 'LightBlue',
1185 LineWidth = 1,
1186 LineColor = "White",
1187 Width = None,
1188 PadSize = 2,
1189 Family = wx.ROMAN,
1190 Position = "tl",
1191 Alignment = "center",
1192 LineSpacing = 0.8
1193 )
1194
1195 Point = Numeric.array((-20, 20), Numeric.Float)
1196 Box = Canvas.AddScaledTextBox("Here is some\nright aligned\ntext",
1197 Point,
1198 Size = 4,
1199 Color = "Red",
1200 BackgroundColor = 'LightBlue',
1201 LineColor = None,
1202 Width = None,
1203 PadSize = 2,
1204 Family = wx.ROMAN,
1205 Position = "tl",
1206 Alignment = "right",
1207 LineSpacing = 0.8
1208 )
1209
1210 Point = Numeric.array((100, -60), Numeric.Float)
1211 Box = Canvas.AddScaledTextBox("Here is some auto wrapped text. This time it is centered, rather than right aligned.\n\nThe Padding is set to 2.",
1212 Point,
1213 Size = 3,
1214 BackgroundColor = 'White',
1215 LineWidth = 1,
1216 Width = 40,
1217 PadSize = 2.0,
1218 Family = wx.ROMAN,
1219 Position = "cc",
1220 LineSpacing = 0.8,
1221 Alignment = 'center',
1222 )
1223
1224
1225
1226
1227 self.Canvas.ZoomToBB()
1228
1229 def binding2(self, event):
1230 self.Log("I'm the TextBox")
1231
1232 def TestBitmap(self, event= None):
1233 wx.GetApp().Yield()
1234
1235 self.UnBindAllMouseEvents()
1236 Canvas = self.Canvas
1237 Canvas.ClearAll()
1238 Canvas.SetProjectionFun(None)
1239
1240 Canvas.AddRectangle((10, 20),
1241 (400, 100),
1242 LineWidth = 3,
1243 LineColor = "Blue",
1244 FillColor = "Red")
1245
1246 bmp = Resources.getMagPlusBitmap()
1247
1248 Canvas.AddText("These are Unscaled Bitmaps:", (140, 90))
1249
1250 Point = (150, 50)
1251 BitMap = Canvas.AddBitmap(bmp, Point, Position = "cc" )
1252 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1253
1254 Point = (200, 50)
1255 BitMap = Canvas.AddBitmap(bmp, Point, Position = "br" )
1256 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1257
1258 Point = (200, 50)
1259 BitMap = Canvas.AddBitmap(bmp, Point, Position = "bl" )
1260 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1261
1262 Point = (200, 50)
1263 BitMap = Canvas.AddBitmap(bmp, Point, Position = "tr" )
1264 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1265
1266 Point = (200, 50)
1267 BitMap = Canvas.AddBitmap(bmp, Point, Position = "tl" )
1268 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1269
1270 Point = (250, 50)
1271 BitMap = Canvas.AddBitmap(bmp, Point, Position = "cr" )
1272 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1273
1274 Point = (250, 50)
1275 BitMap = Canvas.AddBitmap(bmp, Point, Position = "cl" )
1276 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1277
1278 Point = (300, 50)
1279 BitMap = Canvas.AddBitmap(bmp, Point, Position = "tc" )
1280 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1281
1282 Point = (300, 50)
1283 BitMap = Canvas.AddBitmap(bmp, Point, Position = "bc" )
1284 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1285
1286 Canvas.AddScaledText("These are Scaled Bitmaps:", (220, -60), Size = 10, Position = "tr")
1287 Point = (250, -100)
1288 BitMap = Canvas.AddScaledBitmap(bmp, Point, Height = 50, Position = "bc" )
1289 BitMap = Canvas.AddScaledBitmap(bmp, Point, Height = 50, Position = "tc" )
1290 Canvas.AddPoint(Point, Diameter=4, Color="Green")
1291
1292 Point = (300, -100)
1293 BitMap = Canvas.AddScaledBitmap(Resources.getMondrianImage(), Point, Height = 50)
42463de2 1294
8b9a4190 1295 self.Canvas.ZoomToBB()
2e839e96 1296
8b9a4190 1297 def DrawMap(self,event = None):
5e1796ef 1298 wx.GetApp().Yield()
8b9a4190 1299 import os, time
42463de2 1300 self.BindAllMouseEvents()
2e839e96 1301
8b9a4190 1302 ## Test of Actual Map Data
42463de2
RD
1303 self.Canvas.ClearAll()
1304 self.Canvas.SetProjectionFun("FlatEarth")
1305 #start = time.clock()
095315e2
RD
1306 self.Log("Loading Map from a File")
1307 wx.GetApp().Yield() # so log text will get displayed now.
2a0495c9 1308 Shorelines = self.Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
42463de2
RD
1309 #print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
1310 #start = time.clock()
8b9a4190 1311 for segment in Shorelines:
42463de2
RD
1312 self.Canvas.AddLine(segment)
1313 #print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
1314 #start = time.clock()
8b9a4190 1315 self.Canvas.ZoomToBB()
42463de2 1316 #print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
2e839e96
RD
1317
1318
8b9a4190 1319 def LineTest(self,event = None):
5e1796ef 1320 wx.GetApp().Yield()
8b9a4190 1321 import os, time
42463de2
RD
1322# import random
1323 colors = self.colors
8b9a4190
RD
1324 Range = (-10,10)
1325 ## Test of drawing lots of lines
42463de2
RD
1326 Canvas = self.Canvas
1327 Canvas.ClearAll()
1328 Canvas.SetProjectionFun(None)
1329 #start = time.clock()
8b9a4190
RD
1330 linepoints = []
1331 linecolors = []
1332 linewidths = []
1333 for i in range(2000):
1334 points = (random.randint(Range[0],Range[1]),
1335 random.randint(Range[0],Range[1]),
1336 random.randint(Range[0],Range[1]),
1337 random.randint(Range[0],Range[1]))
1338 linepoints.append(points)
1339 linewidths.append(random.randint(1,10) )
1340 linecolors.append(random.randint(0,len(colors)-1) )
1341 for (points,color,width) in zip(linepoints,linecolors,linewidths):
42463de2
RD
1342 Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color])
1343 #print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
1344 #start = time.clock()
1345 Canvas.ZoomToBB()
1346 #print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
1347
1348 def SpeedTest(self,event=None):
5e1796ef 1349 wx.GetApp().Yield()
42463de2
RD
1350 BigRange = (-1000,1000)
1351 colors = self.colors
1352
1353 self.UnBindAllMouseEvents()
1354 Canvas = self.Canvas
1355
1356 Canvas.ClearAll()
1357 Canvas.SetProjectionFun(None)
1358
5e1796ef 1359 # Pointset
42463de2 1360 coords = []
5e1796ef 1361 for i in range(1000):
095315e2
RD
1362 Point = (random.uniform(BigRange[0],BigRange[1]),random.uniform(BigRange[0],BigRange[1]))
1363 coords.append( (Point) )
5e1796ef 1364 print "Drawing the Points"
8b9a4190 1365 start = time.clock()
5e1796ef
RD
1366 for Point in coords:
1367 Canvas.AddPoint(Point, Diameter = 4)
1368 print "It took %s seconds to add the points"%(time.clock() - start)
1369 Canvas.ZoomToBB()
1370
1371 def PropertiesChangeTest(self,event=None):
1372 wx.GetApp().Yield()
1373
1374 Range = (-10,10)
1375 colors = self.colors
1376
1377 self.UnBindAllMouseEvents()
1378 Canvas = self.Canvas
1379
1380 Canvas.ClearAll()
1381 Canvas.SetProjectionFun(None)
1382
1383 self.ColorObjectsAll = []
1384 self.ColorObjectsLine = []
1385 self.ColorObjectsColor = []
1386 self.ColorObjectsText = []
1387 ##One of each object:
1388 # Rectangle
095315e2 1389 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
5e1796ef
RD
1390 lw = random.randint(1,5)
1391 cf = random.randint(0,len(colors)-1)
095315e2
RD
1392 wh = ( random.randint(1,5), random.randint(1,5) )
1393 self.Rectangle = Canvas.AddRectangle(Point, wh, LineWidth = lw, FillColor = colors[cf])
5e1796ef 1394 self.ColorObjectsAll.append(self.Rectangle)
2e839e96 1395
5e1796ef 1396 # Ellipse
095315e2 1397 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
5e1796ef
RD
1398 lw = random.randint(1,5)
1399 cf = random.randint(0,len(colors)-1)
095315e2
RD
1400 wh = ( random.randint(1,5), random.randint(1,5) )
1401 self.Ellipse = Canvas.AddEllipse(Point, wh, LineWidth = lw, FillColor = colors[cf])
5e1796ef 1402 self.ColorObjectsAll.append(self.Ellipse)
2e839e96 1403
5e1796ef
RD
1404 # Point
1405 xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1406 D = random.randint(1,50)
1407 lw = random.randint(1,5)
1408 cf = random.randint(0,len(colors)-1)
1409 cl = random.randint(0,len(colors)-1)
1410 self.ColorObjectsColor.append(Canvas.AddPoint(xy, colors[cf], D))
2e839e96 1411
5e1796ef 1412 # Circle
095315e2 1413 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
5e1796ef
RD
1414 D = random.randint(1,5)
1415 lw = random.randint(1,5)
1416 cf = random.randint(0,len(colors)-1)
1417 cl = random.randint(0,len(colors)-1)
095315e2 1418 self.Circle = Canvas.AddCircle(Point, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
5e1796ef
RD
1419 self.ColorObjectsAll.append(self.Circle)
1420
1421 # Line
1422 points = []
1423 for j in range(random.randint(2,10)):
1424 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
1425 points.append(point)
1426 lw = random.randint(1,10)
1427 cf = random.randint(0,len(colors)-1)
1428 cl = random.randint(0,len(colors)-1)
1429 self.ColorObjectsLine.append(Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
2e839e96 1430
5e1796ef
RD
1431 # Polygon
1432## points = []
1433## for j in range(random.randint(2,6)):
1434## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1435## points.append(point)
1436 points = RandomArray.uniform(Range[0],Range[1],(6,2))
1437 lw = random.randint(1,6)
1438 cf = random.randint(0,len(colors)-1)
1439 cl = random.randint(0,len(colors)-1)
1440 self.ColorObjectsAll.append(Canvas.AddPolygon(points,
1441 LineWidth = lw,
1442 LineColor = colors[cl],
1443 FillColor = colors[cf],
1444 FillStyle = 'Solid'))
2e839e96 1445
5e1796ef
RD
1446 ## Pointset
1447 points = RandomArray.uniform(Range[0],Range[1],(100,2))
1448 cf = random.randint(0,len(colors)-1)
1449 D = random.randint(1,4)
1450 self.PointSet = Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
1451 self.ColorObjectsColor.append(self.PointSet)
2e839e96 1452
5e1796ef
RD
1453 ## Point
1454 point = RandomArray.uniform(Range[0],Range[1],(2,))
1455 cf = random.randint(0,len(colors)-1)
1456 D = random.randint(1,4)
1457 self.Point = Canvas.AddPoint(point, Color = colors[cf], Diameter = D)
1458 self.ColorObjectsColor.append(self.Point)
2e839e96 1459
5e1796ef
RD
1460 # Text
1461 String = "Unscaled text"
1462 ts = random.randint(10,40)
1463 cf = random.randint(0,len(colors)-1)
095315e2
RD
1464 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1465 self.ColorObjectsText.append(Canvas.AddText(String, Point, Size = ts, Color = colors[cf], Position = "cc"))
5e1796ef
RD
1466
1467 # Scaled Text
1468 String = "Scaled text"
1469 ts = random.random()*3 + 0.2
1470 cf = random.randint(0,len(colors)-1)
095315e2
RD
1471 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1472 self.ColorObjectsText.append(Canvas.AddScaledText(String, Point, Size = ts, Color = colors[cf], Position = "cc"))
42463de2 1473
5e1796ef 1474 # A "Button"
095315e2 1475 Button = Canvas.AddRectangle((-10, -12), (20, 3), LineStyle = None, FillColor = "Red")
5e1796ef 1476 Canvas.AddScaledText("Click Here To Change Properties",
095315e2 1477 (0, -10.5),
5e1796ef
RD
1478 Size = 0.7,
1479 Color = "Black",
1480 Position = "cc")
1481
1482 Button.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ChangeProperties)
42463de2
RD
1483
1484 Canvas.ZoomToBB()
1485
5e1796ef
RD
1486 def ChangeProperties(self, Object = None):
1487 colors = self.colors
1488 Range = (-10,10)
1489
1490 for Object in self.ColorObjectsAll:
1491 pass
1492 Object.SetFillColor(colors[random.randint(0,len(colors)-1)])
1493 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1494 Object.SetLineWidth(random.randint(1,7))
1495 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1496 for Object in self.ColorObjectsLine:
1497 Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
1498 Object.SetLineWidth(random.randint(1,7))
1499 Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
1500 for Object in self.ColorObjectsColor:
1501 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1502 for Object in self.ColorObjectsText:
1503 Object.SetColor(colors[random.randint(0,len(colors)-1)])
1504 Object.SetBackgroundColor(colors[random.randint(0,len(colors)-1)])
1505 self.Circle.SetDiameter(random.randint(1,10))
1506 self.PointSet.SetDiameter(random.randint(1,8))
1507 self.Point.SetDiameter(random.randint(1,8))
1508 for Object in (self.Rectangle, self.Ellipse):
095315e2
RD
1509 Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1510 wh = ( random.randint(1,5), random.randint(1,5) )
1511 Object.SetShape(Point, wh)
2a0495c9
RD
1512 self.Canvas.Draw(Force = True)
1513
1514 def ArrowTest(self,event=None):
1515 wx.GetApp().Yield()
1516 self.UnBindAllMouseEvents()
1517 Canvas = self.Canvas
1518
1519 Canvas.ClearAll()
1520 Canvas.SetProjectionFun(None)
1521
1522 # put in a rectangle to get a bounding box
095315e2 1523 Canvas.AddRectangle((0,0), (20,20), LineColor = None)
2a0495c9
RD
1524
1525 # Draw some Arrows
1526 Canvas.AddArrow((10,10),Length = 40, Direction = 0)
1527 Canvas.AddArrow((10,10),Length = 50, Direction = 45 ,LineWidth = 2, LineColor = "Black", ArrowHeadAngle = 20)
1528 Canvas.AddArrow((10,10),Length = 60, Direction = 90 ,LineWidth = 3, LineColor = "Red", ArrowHeadAngle = 30)
1529 Canvas.AddArrow((10,10),Length = 70, Direction = 135,LineWidth = 4, LineColor = "Red", ArrowHeadAngle = 40)
1530 Canvas.AddArrow((10,10),Length = 80, Direction = 180,LineWidth = 5, LineColor = "Blue", ArrowHeadAngle = 50)
1531 Canvas.AddArrow((10,10),Length = 90, Direction = 225,LineWidth = 4, LineColor = "Blue", ArrowHeadAngle = 60)
1532 Canvas.AddArrow((10,10),Length = 100,Direction = 270,LineWidth = 3, LineColor = "Green", ArrowHeadAngle = 70)
1533 Canvas.AddArrow((10,10),Length = 110,Direction = 315,LineWidth = 2, LineColor = "Green", ArrowHeadAngle = 90 )
1534
095315e2 1535 Canvas.AddText("Clickable Arrow", (4,18), Position = "bc")
2a0495c9
RD
1536 Arrow = Canvas.AddArrow((4,18), 80, Direction = 90 ,LineWidth = 3, LineColor = "Red", ArrowHeadAngle = 30)
1537 Arrow.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ArrowClicked)
1538
095315e2 1539 Canvas.AddText("Changable Arrow", (16,4), Position = "cc")
2a0495c9
RD
1540 self.RotArrow = Canvas.AddArrow((16,4), 80, Direction = 0 ,LineWidth = 3, LineColor = "Green", ArrowHeadAngle = 30)
1541 self.RotArrow.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RotateArrow)
1542
2a0495c9
RD
1543 Canvas.ZoomToBB()
1544
1545 def ArrowClicked(self,event):
1546 print "The Arrow was Clicked"
1547
1548 def RotateArrow(self,event):
095315e2 1549 ##print "The Changeable Arrow was Clicked"
2a0495c9
RD
1550 ## You can do them either one at a time, or both at once
1551 ## Doing them both at once prevents the arrow points from being calculated twice
1552 #self.RotArrow.SetDirection(self.RotArrow.Direction + random.uniform(-90,90))
1553 #self.RotArrow.SetLength(self.RotArrow.Length + random.randint(-20,20))
1554 self.RotArrow.SetLengthDirection(self.RotArrow.Length + random.randint(-20,20),
1555 self.RotArrow.Direction + random.uniform(-90,90) )
2e839e96 1556
5e1796ef 1557 self.Canvas.Draw(Force = True)
42463de2 1558
095315e2
RD
1559 def HideTest(self, event=None):
1560 wx.GetApp().Yield()
1561
1562 self.UnBindAllMouseEvents()
1563 Canvas = self.Canvas
1564 Canvas.ClearAll()
1565 Canvas.SetProjectionFun(None)
1566
1567 Range = (-10,10)
1568
1569 # Create a couple random Polygons
1570 points = []
1571 for j in range(6):
1572 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1573 points.append(point)
1574 Poly = Canvas.AddPolygon(points,
1575 LineWidth = 2,
1576 LineColor = "Black",
1577 FillColor = "LightBlue",
1578 FillStyle = 'Solid')
1579
1580 points = []
1581 for j in range(6):
1582 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1583 points.append(point)
1584 Poly2 = Canvas.AddPolygon(points,
1585 LineWidth = 2,
1586 LineColor = "Black",
1587 FillColor = "Purple",
1588 FillStyle = 'Solid',
1589 InForeground = True)
1590
1591 HideButton = Canvas.AddScaledTextBox("Click To Hide\nBackground Polygon",
1592 (-10, 0),
1593 .5,
1594 BackgroundColor="Red",
1595 PadSize = 0.5,
1596 Position = 'tr',
1597 Alignment="center",
1598 )
1599 HideButton.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.HidePoly)
1600 HideButton.HidePoly = Poly
1601
1602 HideButton2 = Canvas.AddScaledTextBox("Click To Hide\nForeground Polygon",
1603 (-10, 5),
1604 .5,
1605 BackgroundColor="Red",
1606 PadSize = 0.5,
1607 Position = 'tr',
1608 Alignment="center",
1609 )
1610 # Put a reference to the Polygon in the Button object
1611 HideButton2.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.HidePoly)
1612 HideButton2.HidePoly = Poly2
1613
1614
1615 Canvas.ZoomToBB()
1616
1617 def HidePoly(self, Button):
1618 Poly = Button.HidePoly
1619 if Poly.Visible:
1620 Poly.Hide()
1621 Button.SetText(Button.String.replace("Hide","Show"))
1622 else:
1623 Poly.Show()
1624 Button.SetText(Button.String.replace("Show", "Hide"))
1625 self.Canvas.Draw(True)
1626
42463de2 1627 def TempTest(self, event= None):
5e1796ef 1628 wx.GetApp().Yield()
42463de2
RD
1629
1630 self.UnBindAllMouseEvents()
1631 Canvas = self.Canvas
1632 Canvas.ClearAll()
1633 Canvas.SetProjectionFun(None)
1634
42463de2
RD
1635 Range = (-10,10)
1636
1637 # Create a random Polygon
1638 points = []
1639 for j in range(6):
1640 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
1641 points.append(point)
1642 Poly = Canvas.AddPolygon(points,
1643 LineWidth = 2,
1644 LineColor = "Black",
1645 FillColor = "LightBlue",
1646 FillStyle = 'Solid')
1647
1648 Poly.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPoly)
1649
1650 self.SelectedPoly = None
1651 self.SelectPoints = []
1652 self.SelectedPoint = None
2e839e96 1653
42463de2
RD
1654 Canvas.ZoomToBB()
1655
1656 def SelectPoly(self, Object):
1657 print "In SelectPoly"
1658 Canvas = self.Canvas
1659 if Object is self.SelectedPoly:
1660 pass
1661 else:
1662 #fixme: Do something to unselect the old one
1663 self.SelectedPoly = Object
1664 Canvas.RemoveObjects(self.SelectPoints)
1665 self.SelectPoints = []
1666 # Draw points on the Vertices of the Selected Poly:
1667 for i, point in enumerate(Object.Points):
1668 P = Canvas.AddPointSet(point, Diameter = 6, Color = "Red")
1669 P.VerticeNum = i
1670 P.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPointHit)
1671 self.SelectPoints.append(P)
1672 #Canvas.ZoomToBB()
1673 Canvas.Draw()
1674
1675 def SelectPointHit(self, Point):
1676 print "Point Num: %i Hit"%Point.VerticeNum
1677 self.SelectedPoint = Point
1678
2a0495c9
RD
1679 def Read_MapGen(self, filename, stats = 0,AllLines=0):
1680 """
1681 This function reads a MapGen Format file, and
1682 returns a list of NumPy arrays with the line segments in them.
1683
1684 Each NumPy array in the list is an NX2 array of Python Floats.
1685
1686 The demo should have come with a file, "world.dat" that is the
1687 shorelines of the whole world, in MapGen format.
2e839e96 1688
2a0495c9
RD
1689 """
1690 import string
1691 file = open(filename,'rt')
1692 data = file.readlines()
1693 data = map(string.strip,data)
1694
1695 Shorelines = []
1696 segment = []
1697 for line in data:
1698 if line:
1699 if line == "# -b": #New segment beginning
1700 if segment: Shorelines.append(Numeric.array(segment))
1701 segment = []
1702 else:
1703 segment.append(map(float,string.split(line)))
1704 if segment: Shorelines.append(Numeric.array(segment))
1705
1706 if stats:
1707 NumSegments = len(Shorelines)
1708 NumPoints = 0
1709 for segment in Shorelines:
1710 NumPoints = NumPoints + len(segment)
1711 AvgPoints = NumPoints / NumSegments
1712 print "Number of Segments: ", NumSegments
1713 print "Average Number of Points per segment: ",AvgPoints
1714 if AllLines:
1715 Lines = []
1716 for segment in Shorelines:
1717 Lines.append(segment[0])
1718 for point in segment[1:-1]:
1719 Lines.append(point)
1720 Lines.append(point)
1721 Lines.append(segment[-1])
1722 return Lines
1723 else:
1724 return Shorelines
1725 return DrawFrame
1726
1727#---------------------------------------------------------------------------
1728
1729if __name__ == "__main__":
095315e2
RD
1730
1731 import wx
1732
1733
2a0495c9
RD
1734 # check options:
1735 import sys, getopt
095315e2
RD
1736 optlist, args = getopt.getopt(sys.argv[1:],'l',["local",
1737 "all",
1738 "text",
1739 "map",
1740 "stext",
1741 "stextbox",
1742 "bitmap",
1743 "hit",
1744 "hitf",
1745 "animate",
1746 "speed",
1747 "temp",
1748 "props",
1749 "arrow",
1750 "hide"])
2a0495c9
RD
1751
1752 if not haveNumeric:
1753 raise ImportError(errorText)
1754 StartUpDemo = "all" # the default
1755 for opt in optlist:
1756 if opt[0] == "--all":
1757 StartUpDemo = "all"
1758 elif opt[0] == "--text":
1759 StartUpDemo = "text"
1760 elif opt[0] == "--map":
1761 StartUpDemo = "map"
1762 elif opt[0] == "--stext":
1763 StartUpDemo = "stext"
095315e2
RD
1764 elif opt[0] == "--stextbox":
1765 StartUpDemo = "stextbox"
1766 elif opt[0] == "--bitmap":
1767 StartUpDemo = "bitmap"
2a0495c9
RD
1768 elif opt[0] == "--hit":
1769 StartUpDemo = "hit"
1770 elif opt[0] == "--hitf":
1771 StartUpDemo = "hitf"
1772 elif opt[0] == "--animate":
1773 StartUpDemo = "animate"
1774 elif opt[0] == "--speed":
1775 StartUpDemo = "speed"
1776 elif opt[0] == "--temp":
1777 StartUpDemo = "temp"
1778 elif opt[0] == "--props":
1779 StartUpDemo = "props"
1780 elif opt[0] == "--arrow":
1781 StartUpDemo = "arrow"
095315e2
RD
1782 elif opt[0] == "--hide":
1783 StartUpDemo = "hide"
2e839e96 1784
8fa876ca 1785 class DemoApp(wx.App):
8b9a4190
RD
1786 """
1787 How the demo works:
2e839e96 1788
8b9a4190 1789 Under the Draw menu, there are three options:
2e839e96 1790
8b9a4190
RD
1791 *Draw Test: will put up a picture of a bunch of randomly generated
1792 objects, of each kind supported.
2e839e96 1793
8b9a4190
RD
1794 *Draw Map: will draw a map of the world. Be patient, it is a big map,
1795 with a lot of data, and will take a while to load and draw (about 10 sec
1796 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
1797 performance is not very good for large drawings.
2e839e96 1798
8b9a4190 1799 *Clear: Clears the Canvas.
2e839e96 1800
8b9a4190
RD
1801 Once you have a picture drawn, you can zoom in and out and move about
1802 the picture. There is a tool bar with three tools that can be
1803 selected.
2e839e96 1804
8b9a4190
RD
1805 The magnifying glass with the plus is the zoom in tool. Once selected,
1806 if you click the image, it will zoom in, centered on where you
1807 clicked. If you click and drag the mouse, you will get a rubber band
1808 box, and the image will zoom to fit that box when you release it.
2e839e96 1809
8b9a4190
RD
1810 The magnifying glass with the minus is the zoom out tool. Once selected,
1811 if you click the image, it will zoom out, centered on where you
1812 clicked. (note that this takes a while when you are looking at the map,
1813 as it has a LOT of lines to be drawn. The image is double buffered, so
1814 you don't see the drawing in progress)
2e839e96 1815
8b9a4190
RD
1816 The hand is the move tool. Once selected, if you click and drag on the
1817 image, it will move so that the part you clicked on ends up where you
1818 release the mouse. Nothing is changed while you are dragging. The
1819 drawing is too slow for that.
2e839e96 1820
8b9a4190
RD
1821 I'd like the cursor to change as you change tools, but the stock
1822 wxCursors didn't include anything I liked, so I stuck with the
1823 pointer. Please let me know if you have any nice cursor images for me to
1824 use.
2e839e96
RD
1825
1826
8b9a4190 1827 Any bugs, comments, feedback, questions, and especially code are welcome:
2e839e96 1828
8b9a4190 1829 -Chris Barker
2e839e96 1830
8b9a4190 1831 Chris.Barker@noaa.gov
2e839e96 1832
8b9a4190 1833 """
42463de2
RD
1834
1835 def __init__(self, *args, **kwargs):
1836 wx.App.__init__(self, *args, **kwargs)
2e839e96 1837
8b9a4190 1838 def OnInit(self):
42463de2 1839 wx.InitAllImageHandlers()
2a0495c9 1840 DrawFrame = BuildDrawFrame()
42463de2 1841 frame = DrawFrame(None, -1, "FloatCanvas Demo App",wx.DefaultPosition,(700,700))
2e839e96 1842
8b9a4190 1843 self.SetTopWindow(frame)
42463de2
RD
1844 frame.Show()
1845
1846 ## check to see if the demo is set to start in a particular mode.
1847 if StartUpDemo == "text":
1848 frame.TestText()
095315e2 1849 elif StartUpDemo == "stext":
42463de2 1850 frame.TestScaledText()
095315e2
RD
1851 elif StartUpDemo == "stextbox":
1852 frame.TestScaledTextBox()
1853 elif StartUpDemo == "bitmap":
1854 frame.TestBitmap()
42463de2
RD
1855 elif StartUpDemo == "all":
1856 frame.DrawTest()
1857 elif StartUpDemo == "map":
1858 frame.DrawMap()
1859 elif StartUpDemo == "hit":
1860 frame.TestHitTest()
1861 elif StartUpDemo == "hitf":
1862 "starting TestHitTestForeground"
1863 frame.TestHitTestForeground()
1864 elif StartUpDemo == "animate":
1865 "starting TestAnimation"
1866 frame.TestAnimation()
1867 elif StartUpDemo == "speed":
1868 "starting SpeedTest"
1869 frame.SpeedTest()
1870 elif StartUpDemo == "temp":
1871 "starting temp Test"
1872 frame.TempTest()
5e1796ef
RD
1873 elif StartUpDemo == "props":
1874 "starting PropertiesChange Test"
1875 frame.PropertiesChangeTest()
2a0495c9
RD
1876 elif StartUpDemo == "arrow":
1877 "starting arrow Test"
1878 frame.ArrowTest()
095315e2
RD
1879 elif StartUpDemo == "hide":
1880 "starting Hide Test"
1881 frame.HideTest()
2e839e96 1882
8b9a4190 1883 return True
2e839e96 1884
2a0495c9
RD
1885 app = DemoApp(False)# put in True if you want output to go to it's own window.
1886 app.MainLoop()
2e839e96
RD
1887
1888else:
2a0495c9 1889 # It's not running stand-alone, set up for wxPython demo.
095315e2
RD
1890 # don't neeed wxversion here.
1891 import wx
8b9a4190 1892 if not haveNumeric:
2a0495c9
RD
1893 ## TestPanel and runTest used for integration into wxPython Demo
1894 class TestPanel(wx.Panel):
1895 def __init__(self, parent, log):
1896 self.log = log
1897 wx.Panel.__init__(self, parent, -1)
1898
1899 import images
1900
1901 note1 = wx.StaticText(self, -1, errorText)
1902 note2 = wx.StaticText(self, -1, "This is what the FloatCanvas can look like:")
1903 S = wx.BoxSizer(wx.VERTICAL)
1904 S.Add((10, 10), 1)
1905 S.Add(note1, 0, wx.ALIGN_CENTER)
1906 S.Add(note2, 0, wx.ALIGN_CENTER | wx.BOTTOM, 4)
1907 S.Add(wx.StaticBitmap(self,-1,images.getFloatCanvasBitmap()),0,wx.ALIGN_CENTER)
1908 S.Add((10, 10), 1)
1909 self.SetSizer(S)
1910 self.Layout()
42463de2 1911
2a0495c9
RD
1912 else:
1913 ## TestPanel and runTest used for integration into wxPython Demo
1914 class TestPanel(wx.Panel):
1915 def __init__(self, parent, log):
1916 self.log = log
1917 wx.Panel.__init__(self, parent, -1)
1918 note1 = wx.StaticText(self, -1, "The FloatCanvas Demo needs")
1919 note2 = wx.StaticText(self, -1, "a separate frame")
1920 b = wx.Button(self, -1, "Open Demo Frame Now")
1921 b.Bind(wx.EVT_BUTTON, self.OnButton)
1922
1923 S = wx.BoxSizer(wx.VERTICAL)
1924 S.Add((10, 10), 1)
1925 S.Add(note1, 0, wx.ALIGN_CENTER)
1926 S.Add(note2, 0, wx.ALIGN_CENTER | wx.BOTTOM, 5)
1927 S.Add(b, 0, wx.ALIGN_CENTER | wx.ALL, 5)
1928 S.Add((10, 10), 1)
1929 self.SetSizer(S)
1930 self.Layout()
1931
1932 def OnButton(self, evt):
1933 DrawFrame = BuildDrawFrame()
1934 frame = DrawFrame(None, -1, "FloatCanvas Drawing Window",wx.DefaultPosition,(500,500))
1935
1936 #win = wx.lib.plot.TestFrame(self, -1, "PlotCanvas Demo")
1937 frame.Show()
1938 frame.DrawTest()
42463de2 1939
2a0495c9
RD
1940 def runTest(frame, nb, log):
1941 win = TestPanel(nb, log)
1942 return win
42463de2 1943
2a0495c9
RD
1944 # import to get the doc
1945 from wx.lib import floatcanvas
1946 overview = floatcanvas.__doc__
42463de2
RD
1947
1948
1949
1950
1951
1952
1953
1954