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