]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
   4 # Stuff to integrate FloatCanvas into wxPython Demo 
  14 The FloatCanvas requires the Numeric module: 
  16      http://sourceforge.net/projects/numpy 
  18     def runTest(frame
, nb
, log
): 
  19         dlg 
= wx
.MessageDialog( 
  20                 frame
, errorText
, 'Sorry', wx
.OK | wx
.ICON_INFORMATION
 
  29     from wxPython
.lib 
import floatcanvas
 
  30     import wxPython
.lib
.colourdb
 
  32     ID_ABOUT_MENU 
= wx
.NewId()           
  33     ID_EXIT_MENU  
= wx
.NewId()  
  34     ID_ZOOM_TO_FIT_MENU 
= wx
.NewId() 
  35     ID_DRAWTEST_MENU 
= wx
.NewId() 
  36     ID_LINETEST_MENU 
= wx
.NewId() 
  37     ID_DRAWMAP_MENU 
= wx
.NewId() 
  38     ID_DRAWMAP2_MENU 
= wx
.NewId() 
  39     ID_CLEAR_MENU 
= wx
.NewId() 
  43     LineStyles 
= floatcanvas
.draw_object
.LineStyleList
.keys() 
  47     class DrawFrame(wx
.Frame
): 
  51         A frame used for the FloatCanvas Demo 
  55         def __init__(self
, parent
, id, title
, position
, size
): 
  56             wx
.Frame
.__init
__(self
,parent
, id,title
,position
, size
) 
  60             MenuBar 
= wx
.MenuBar() 
  63             file_menu
.Append(ID_EXIT_MENU
, "&Close","Close this frame") 
  64             self
.Bind(wx
.EVT_MENU
, self
.OnQuit
, id=ID_EXIT_MENU
) 
  65             MenuBar
.Append(file_menu
, "&File") 
  68             draw_menu
.Append(ID_DRAWTEST_MENU
, "&Draw Test","Run a test of drawing random components") 
  69             self
.Bind(wx
.EVT_MENU
, self
.DrawTest
, id=ID_DRAWTEST_MENU
) 
  70             draw_menu
.Append(ID_LINETEST_MENU
, "&Line Test","Run a test of drawing random lines") 
  71             self
.Bind(wx
.EVT_MENU
, self
.LineTest
, id=ID_LINETEST_MENU
) 
  72             draw_menu
.Append(ID_DRAWMAP_MENU
, "Draw &Map","Run a test of drawing a map") 
  73             self
.Bind(wx
.EVT_MENU
, self
.DrawMap
, id=ID_DRAWMAP_MENU
) 
  74             draw_menu
.Append(ID_CLEAR_MENU
, "&Clear","Clear the Canvas") 
  75             self
.Bind(wx
.EVT_MENU
, self
.Clear
, id=ID_CLEAR_MENU
) 
  76             MenuBar
.Append(draw_menu
, "&Draw") 
  79             view_menu
.Append(ID_ZOOM_TO_FIT_MENU
, "Zoom to &Fit","Zoom to fit the window") 
  80             self
.Bind(wx
.EVT_MENU
, self
.ZoomToFit
, id=ID_ZOOM_TO_FIT_MENU
) 
  81             MenuBar
.Append(view_menu
, "&View") 
  84             help_menu
.Append(ID_ABOUT_MENU
, "&About", 
  85                                     "More information About this program") 
  86             self
.Bind(wx
.EVT_MENU
, self
.OnAbout
, id=ID_ABOUT_MENU
) 
  87             MenuBar
.Append(help_menu
, "&Help") 
  89             self
.SetMenuBar(MenuBar
) 
  91             self
.CreateStatusBar() 
  92             self
.SetStatusText("") 
  94             self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
) 
  96             # Other event handlers: 
  97             self
.Bind(wx
.EVT_RIGHT_DOWN
, self
.RightButtonEvent
) 
 100             self
.Canvas 
= floatcanvas
.FloatCanvas(self
,-1,(500,500), 
 101                                       ProjectionFun 
= 'FlatEarth', 
 103                                       EnclosingFrame 
= self
, 
 104                                       BackgroundColor 
= "DARK SLATE BLUE", 
 109             self
.object_list 
= [] 
 113         def RightButtonEvent(self
,event
): 
 114             print "Right Button has been clicked in DrawFrame" 
 115             print "coords are: %i, %i"%(event
.GetX(),event
.GetY()) 
 118         def OnAbout(self
, event
): 
 119             dlg 
= wx
.MessageDialog(self
, "This is a small program to demonstrate\n" 
 120                                                       "the use of the FloatCanvas\n", 
 121                                                       "About Me", wx
.OK | wx
.ICON_INFORMATION
) 
 125         def SetMode(self
,event
): 
 126             for id in [ID_ZOOM_IN_BUTTON
,ID_ZOOM_OUT_BUTTON
,ID_MOVE_MODE_BUTTON
]: 
 127                 self
.ToolBar
.ToggleTool(id,0) 
 129             self
.ToolBar
.ToggleTool(event
.GetId(),1) 
 131             if event
.GetId() == ID_ZOOM_IN_BUTTON
: 
 132                 self
.Canvas
.SetGUIMode("ZoomIn") 
 134             elif event
.GetId() == ID_ZOOM_OUT_BUTTON
: 
 135                 self
.Canvas
.SetGUIMode("ZoomOut") 
 137             elif event
.GetId() == ID_MOVE_MODE_BUTTON
: 
 138                 self
.Canvas
.SetGUIMode("Move") 
 140         def ZoomToFit(self
,event
): 
 141             self
.Canvas
.ZoomToBB() 
 143         def Clear(self
,event 
= None): 
 144             self
.Canvas
.RemoveObjects(self
.object_list
) 
 145             self
.object_list 
= [] 
 148         def OnQuit(self
,event
): 
 151         def OnCloseWindow(self
, event
): 
 154         def DrawTest(self
,event
): 
 163             object_list 
= self
.object_list
 
 165             #           Random tests of everything: 
 169                 x
,y 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 170                 lw 
= random
.randint(1,5) 
 171                 cf 
= random
.randint(0,len(colors
)-1) 
 172                 h 
= random
.randint(1,5) 
 173                 w 
= random
.randint(1,5) 
 174                 object_list
.append(Canvas
.AddRectangle(x
,y
,h
,w
,LineWidth 
= lw
,FillColor 
= colors
[cf
])) 
 178                 x
,y 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 179                 lw 
= random
.randint(1,5) 
 180                 cf 
= random
.randint(0,len(colors
)-1) 
 181                 h 
= random
.randint(1,5) 
 182                 w 
= random
.randint(1,5) 
 183                 object_list
.append(Canvas
.AddEllipse(x
,y
,h
,w
,LineWidth 
= lw
,FillColor 
= colors
[cf
])) 
 187                 x
,y 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 188                 D 
= random
.randint(1,50) 
 189                 lw 
= random
.randint(1,5) 
 190                 cf 
= random
.randint(0,len(colors
)-1) 
 191                 cl 
= random
.randint(0,len(colors
)-1) 
 192                 object_list
.append(Canvas
.AddDot(x
,y
,D
,LineWidth 
= lw
,LineColor 
= colors
[cl
],FillColor 
= colors
[cf
])) 
 196                 x
,y 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 197                 D 
= random
.randint(1,5) 
 198                 lw 
= random
.randint(1,5) 
 199                 cf 
= random
.randint(0,len(colors
)-1) 
 200                 cl 
= random
.randint(0,len(colors
)-1) 
 201                 object_list
.append(Canvas
.AddCircle(x
,y
,D
,LineWidth 
= lw
,LineColor 
= colors
[cl
],FillColor 
= colors
[cf
])) 
 202                 self
.object_list
.append(self
.Canvas
.AddText("Circle # %i"%(i),x
,y
,Size 
= 12,BackGround 
= None,Position 
= "cc")) 
 207                 for j 
in range(random
.randint(2,10)): 
 208                     point 
= (random
.randint(Range
[0],Range
[1]),random
.randint(Range
[0],Range
[1])) 
 210                 lw 
= random
.randint(1,10) 
 211                 cf 
= random
.randint(0,len(colors
)-1) 
 212                 cl 
= random
.randint(0,len(colors
)-1) 
 213                 self
.object_list
.append(self
.Canvas
.AddLine(points
, LineWidth 
= lw
, LineColor 
= colors
[cl
])) 
 218                 for j 
in range(random
.randint(2,6)): 
 219                     point 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 221                 lw 
= random
.randint(1,6) 
 222                 cf 
= random
.randint(0,len(colors
)-1) 
 223                 cl 
= random
.randint(0,len(colors
)-1) 
 224                 self
.object_list
.append(self
.Canvas
.AddPolygon(points
, 
 226                                                                LineColor 
= colors
[cl
], 
 227                                                                FillColor 
= colors
[cf
], 
 228                                                                FillStyle 
= 'Solid')) 
 234                 points 
= RandomArray
.uniform(Range
[0],Range
[1],(100,2)) 
 235                 cf 
= random
.randint(0,len(colors
)-1) 
 236                 D 
= random
.randint(1,4) 
 237                 self
.object_list
.append(self
.Canvas
.AddPointSet(points
, Color 
= colors
[cf
], Diameter 
= D
)) 
 242                 ts 
= random
.randint(10,40) 
 243                 cf 
= random
.randint(0,len(colors
)-1) 
 244                 x
,y 
= (random
.uniform(Range
[0],Range
[1]),random
.uniform(Range
[0],Range
[1])) 
 245                 self
.object_list
.append(self
.Canvas
.AddText(String
,x
,y
,Size 
= ts
,ForeGround 
= colors
[cf
],Position 
= "cc")) 
 247             self
.Canvas
.ZoomToBB() 
 249         def DrawMap(self
,event 
= None): 
 252         ## Test of Actual Map Data 
 255             Shorelines 
= Read_MapGen(os
.path
.join("data",'world.dat'),stats 
= 0) 
 256             print "It took %f seconds to load %i shorelines"%(time
.clock() - start
,len(Shorelines
) ) 
 258             for segment 
in Shorelines
: 
 259                 self
.object_list
.append(self
.Canvas
.AddLine(segment
)) 
 260             print "It took %f seconds to add %i shorelines"%(time
.clock() - start
,len(Shorelines
) ) 
 262             self
.Canvas
.ZoomToBB() 
 263             print "It took %f seconds to draw %i shorelines"%(time
.clock() - start
,len(Shorelines
) ) 
 265     ##    def LineTest(self,event = None): 
 266     ##        wxGetApp().Yield() 
 270     ##    ## Test of drawing lots of lines 
 272     ##        start = time.clock() 
 277     ##        for i in range(500): 
 278     ##            points = (random.randint(Range[0],Range[1]), 
 279     ##                     random.randint(Range[0],Range[1])) 
 280     ##            linepoints.append(points) 
 281     ##            points = (random.randint(Range[0],Range[1]), 
 282     ##                     random.randint(Range[0],Range[1])) 
 283     ##            linepoints.append(points) 
 284     ##            linewidths.append(random.randint(1,10) ) 
 285     ##            linecolors.append(colors[random.randint(0,len(colors)-1) ]) 
 286     ##            linestyles.append(LineStyles[random.randint(0, len(LineStyles)-1)]) 
 288     ##        self.object_list.append(self.Canvas.AddLineSet(linepoints, LineWidths = linewidths, LineColors = linecolors, LineStyles = linestyles)) 
 289     ##        print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) ) 
 290     ##        start = time.clock() 
 291     ##        self.Canvas.ZoomToBB() 
 292     ##        print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) ) 
 294         def LineTest(self
,event 
= None): 
 299         ## Test of drawing lots of lines 
 305             for i 
in range(2000): 
 306                 points 
= (random
.randint(Range
[0],Range
[1]), 
 307                          random
.randint(Range
[0],Range
[1]), 
 308                          random
.randint(Range
[0],Range
[1]), 
 309                          random
.randint(Range
[0],Range
[1])) 
 310                 linepoints
.append(points
) 
 311                 linewidths
.append(random
.randint(1,10) ) 
 312                 linecolors
.append(random
.randint(0,len(colors
)-1) ) 
 313             for (points
,color
,width
) in zip(linepoints
,linecolors
,linewidths
): 
 314                 self
.object_list
.append(self
.Canvas
.AddLine((points
[0:2],points
[2:4]), LineWidth 
= width
, LineColor 
= colors
[color
])) 
 315             print "It took %f seconds to add %i lines"%(time
.clock() - start
,len(linepoints
) ) 
 317             self
.Canvas
.ZoomToBB() 
 318             print "It took %f seconds to draw %i lines"%(time
.clock() - start
,len(linepoints
) ) 
 320     class DemoApp(wx
.App
): 
 324         Under the Draw menu, there are three options: 
 326         *Draw Test: will put up a picture of a bunch of randomly generated 
 327         objects, of each kind supported. 
 329         *Draw Map: will draw a map of the world. Be patient, it is a big map, 
 330         with a lot of data, and will take a while to load and draw (about 10 sec  
 331         on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the 
 332         performance is not very good for large drawings. 
 334         *Clear: Clears the Canvas. 
 336         Once you have a picture drawn, you can zoom in and out and move about 
 337         the picture. There is a tool bar with three tools that can be 
 340         The magnifying glass with the plus is the zoom in tool. Once selected, 
 341         if you click the image, it will zoom in, centered on where you 
 342         clicked. If you click and drag the mouse, you will get a rubber band 
 343         box, and the image will zoom to fit that box when you release it. 
 345         The magnifying glass with the minus is the zoom out tool. Once selected, 
 346         if you click the image, it will zoom out, centered on where you 
 347         clicked. (note that this takes a while when you are looking at the map, 
 348         as it has a LOT of lines to be drawn. The image is double buffered, so 
 349         you don't see the drawing in progress) 
 351         The hand is the move tool. Once selected, if you click and drag on the 
 352         image, it will move so that the part you clicked on ends up where you 
 353         release the mouse. Nothing is changed while you are dragging. The 
 354         drawing is too slow for that. 
 356         I'd like the cursor to change as you change tools, but the stock 
 357         wxCursors didn't include anything I liked, so I stuck with the 
 358         pointer. Please let me know if you have any nice cursor images for me to 
 362         Any bugs, comments, feedback, questions, and especially code are welcome: 
 366         Chris.Barker@noaa.gov 
 372             wxPython
.lib
.colourdb
.updateColourDB() 
 373             colors 
= wxPython
.lib
.colourdb
.getColourList() 
 375             frame 
= DrawFrame(None, -1, "FloatCanvas Demo App", 
 376                               wx
.DefaultPosition
, (700,700)) 
 378             self
.SetTopWindow(frame
) 
 382     def Read_MapGen(filename
,stats 
= 0,AllLines
=0): 
 384         This function reads a MapGen Format file, and 
 385         returns a list of NumPy arrays with the line segments in them. 
 387         Each NumPy array in the list is an NX2 array of Python Floats. 
 389         The demo should have come with a file, "world.dat" that is the 
 390         shorelines of the whole world, in MapGen format. 
 394         from Numeric 
import array
 
 395         file = open(filename
,'rt') 
 396         data 
= file.readlines() 
 397         data 
= map(string
.strip
,data
) 
 403                 if line 
== "# -b": #New segment beginning 
 404                     if segment
: Shorelines
.append(array(segment
)) 
 407                     segment
.append(map(float,string
.split(line
))) 
 408         if segment
: Shorelines
.append(array(segment
)) 
 411             NumSegments 
= len(Shorelines
) 
 413             for segment 
in Shorelines
: 
 414                 NumPoints 
= NumPoints 
+ len(segment
) 
 415             AvgPoints 
= NumPoints 
/ NumSegments
 
 416             print "Number of Segments: ", NumSegments
 
 417             print "Average Number of Points per segment: ",AvgPoints
 
 420             for segment 
in Shorelines
: 
 421                 Lines
.append(segment
[0]) 
 422                 for point 
in segment
[1:-1]: 
 425                 Lines
.append(segment
[-1]) 
 427             #for point in Lines: print point 
 433     #---------------------------------------------------------------------- 
 435     def runTest(frame
, nb
, log
): 
 437         This method is used by the wxPython Demo Framework for integrating 
 438         this demo with the rest. 
 441         wxPython
.lib
.colourdb
.updateColourDB() 
 442         colors 
= wxPython
.lib
.colourdb
.getColourList() 
 444         win 
= DrawFrame(None, -1, "FloatCanvas Drawing Window", 
 445                         wx
.DefaultPosition
, (500,500)) 
 452     ## for the wxPython demo: 
 453     overview 
= floatcanvas
.FloatCanvas
.__doc
__ 
 457 if __name__ 
== "__main__": 
 463         import  wx
.lib
.colourdb
 
 464         wx
.lib
.colourdb
.updateColourDB() 
 465         colors 
= wx
.lib
.colourdb
.getColourList()