File: FloatCanvas.py

package info (click to toggle)
wxwindows2.4 2.4.5.1.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 59,920 kB
  • ctags: 97,489
  • sloc: cpp: 610,307; ansic: 111,957; python: 103,357; makefile: 3,676; sh: 3,391; lex: 192; yacc: 128; xml: 95; pascal: 74
file content (443 lines) | stat: -rw-r--r-- 17,971 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443

from wxPython.wx  import *


## Stuff to integrate FloatCanvas into wxPython Demo
try:
    import Numeric
    haveNumeric = True
except ImportError:
    haveNumeric = False


if not haveNumeric:
    errorText = """\
The FloatCanvas requires the Numeric module:
You can get it at:
     http://sourceforge.net/projects/numpy
"""     
    def runTest(frame, nb, log):
        dlg = wxMessageDialog(frame, errorText, 
                              'Sorry', wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    overview = ""

else:
    def runTest(frame, nb, log):
        """
        This method is used by the wxPython Demo Framework for integrating
        this demo with the rest.
        """
        win = DrawFrame(NULL, -1, "FloatCanvas Drawing Window",wxDefaultPosition,wxSize(500,500))
        frame.otherWin = win
        win.Show(True)



    from wxPython.lib import floatcanvas
    import wxPython.lib.colourdb
    
    ID_ABOUT_MENU = wxNewId()          
    ID_EXIT_MENU  = wxNewId() 
    ID_ZOOM_TO_FIT_MENU = wxNewId()
    ID_DRAWTEST_MENU = wxNewId()
    ID_LINETEST_MENU = wxNewId()
    ID_DRAWMAP_MENU = wxNewId()
    ID_DRAWMAP2_MENU = wxNewId()
    ID_CLEAR_MENU = wxNewId()

    wxPython.lib.colourdb.updateColourDB()
    colors = wxPython.lib.colourdb.getColourList()

    LineStyles = floatcanvas.draw_object.LineStyleList.keys()
    
    class DrawFrame(wxFrame):
    
        """
    
        A frame used for the FloatCanvas Demo
        
        """
        
        def __init__(self,parent, id,title,position,size):
            wxFrame.__init__(self,parent, id,title,position, size)
            
            ## Set up the MenuBar
            
            MenuBar = wxMenuBar()
            
            file_menu = wxMenu()
            file_menu.Append(ID_EXIT_MENU, "&Close","Close this frame")
            EVT_MENU(self, ID_EXIT_MENU,       self.OnQuit)
            MenuBar.Append(file_menu, "&File")
            
            draw_menu = wxMenu()
            draw_menu.Append(ID_DRAWTEST_MENU, "&Draw Test","Run a test of drawing random components")
            EVT_MENU(self, ID_DRAWTEST_MENU,self.DrawTest)
            draw_menu.Append(ID_LINETEST_MENU, "&Line Test","Run a test of drawing random lines")
            EVT_MENU(self, ID_LINETEST_MENU,self.LineTest)
            draw_menu.Append(ID_DRAWMAP_MENU, "Draw &Map","Run a test of drawing a map")
            EVT_MENU(self, ID_DRAWMAP_MENU,self.DrawMap)
            draw_menu.Append(ID_CLEAR_MENU, "&Clear","Clear the Canvas")
            EVT_MENU(self, ID_CLEAR_MENU,self.Clear)
            MenuBar.Append(draw_menu, "&Draw")
            
            
            view_menu = wxMenu()
            view_menu.Append(ID_ZOOM_TO_FIT_MENU, "Zoom to &Fit","Zoom to fit the window")
            EVT_MENU(self, ID_ZOOM_TO_FIT_MENU,self.ZoomToFit)
            MenuBar.Append(view_menu, "&View")
            
            help_menu = wxMenu()
            help_menu.Append(ID_ABOUT_MENU, "&About",
                                    "More information About this program")
            EVT_MENU(self, ID_ABOUT_MENU,      self.OnAbout)
            MenuBar.Append(help_menu, "&Help")
            
            self.SetMenuBar(MenuBar)
            
            self.CreateStatusBar()
            self.SetStatusText("")
            
            EVT_CLOSE(self, self.OnCloseWindow)
            
            # Other event handlers:
            EVT_RIGHT_DOWN(self, self.RightButtonEvent)
            
            # Add the Canvas
            self.Canvas = floatcanvas.FloatCanvas(self,-1,(500,500),
                                      ProjectionFun = 'FlatEarth',
                                      Debug = 0,
                                      EnclosingFrame = self,
                                      BackgroundColor = "DARK SLATE BLUE",
                                      UseBackground = 0,
                                      UseToolbar = 1)
            self.Show(True)
            
            self.object_list = []
            
            return None
            
        def RightButtonEvent(self,event):
            print "Right Button has been clicked in DrawFrame"
            print "coords are: %i, %i"%(event.GetX(),event.GetY())
            event.Skip()
            
        def OnAbout(self, event):
            dlg = wxMessageDialog(self, "This is a small program to demonstrate\n"
                                                      "the use of the FloatCanvas\n",
                                                      "About Me", wxOK | wxICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
            
        def SetMode(self,event):
            for id in [ID_ZOOM_IN_BUTTON,ID_ZOOM_OUT_BUTTON,ID_MOVE_MODE_BUTTON]:
                self.ToolBar.ToggleTool(id,0)
            self.ToolBar.ToggleTool(event.GetId(),1)
            if event.GetId() == ID_ZOOM_IN_BUTTON:
                self.Canvas.SetGUIMode("ZoomIn")
            elif event.GetId() == ID_ZOOM_OUT_BUTTON:
                self.Canvas.SetGUIMode("ZoomOut")
            elif event.GetId() == ID_MOVE_MODE_BUTTON:
                self.Canvas.SetGUIMode("Move")
                
        def ZoomToFit(self,event):
            self.Canvas.ZoomToBB()
            
        def Clear(self,event = None):
            self.Canvas.RemoveObjects(self.object_list)
            self.object_list = []
            self.Canvas.Draw()
            
        def OnQuit(self,event):
            self.Close(True)
            
        def OnCloseWindow(self, event):
            self.Destroy()
            
        def DrawTest(self,event):
            wxGetApp().Yield()
            import random
            import RandomArray
            Range = (-10,10)
            
            Canvas = self.Canvas
            object_list = self.object_list
            
            ##		Random tests of everything:
            
            # Rectangles
            for i in range(5):
                x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                lw = random.randint(1,5)
                cf = random.randint(0,len(colors)-1)
                h = random.randint(1,5)
                w = random.randint(1,5)
                object_list.append(Canvas.AddRectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
              
                # Ellipses
            for i in range(5):
                x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                lw = random.randint(1,5)
                cf = random.randint(0,len(colors)-1)
                h = random.randint(1,5)
                w = random.randint(1,5)
                object_list.append(Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
              
                # Dots
            for i in range(5):
                x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                D = random.randint(1,50)
                lw = random.randint(1,5)
                cf = random.randint(0,len(colors)-1)
                cl = random.randint(0,len(colors)-1)
                object_list.append(Canvas.AddDot(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf]))
              
                # Circles
            for i in range(5):
                x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                D = random.randint(1,5)
                lw = random.randint(1,5)
                cf = random.randint(0,len(colors)-1)
                cl = random.randint(0,len(colors)-1)
                object_list.append(Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf]))
                self.object_list.append(self.Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackGround = None,Position = "cc"))
              
                # Lines
            for i in range(5):
                points = []
                for j in range(random.randint(2,10)):
                    point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
                    points.append(point)
                lw = random.randint(1,10)
                cf = random.randint(0,len(colors)-1)
                cl = random.randint(0,len(colors)-1)
                self.object_list.append(self.Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
              
                # Polygons
            for i in range(3):
                points = []
                for j in range(random.randint(2,6)):
                    point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                    points.append(point)
                lw = random.randint(1,6)
                cf = random.randint(0,len(colors)-1)
                cl = random.randint(0,len(colors)-1)
                self.object_list.append(self.Canvas.AddPolygon(points,
                                                               LineWidth = lw,
                                                               LineColor = colors[cl],
                                                               FillColor = colors[cf],
                                                               FillStyle = 'Solid'))
                
                
            ## Pointset
            for i in range(4):
                points = []
                points = RandomArray.uniform(Range[0],Range[1],(100,2))
                cf = random.randint(0,len(colors)-1)
                D = random.randint(1,4)
                self.object_list.append(self.Canvas.AddPointSet(points, Color = colors[cf], Diameter = D))
            
            # Text
            String = "Some text"
            for i in range(10):
                ts = random.randint(10,40)
                cf = random.randint(0,len(colors)-1)
                x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
                self.object_list.append(self.Canvas.AddText(String,x,y,Size = ts,ForeGround = colors[cf],Position = "cc"))
                
            self.Canvas.ZoomToBB()
            
        def DrawMap(self,event = None):
            wxGetApp().Yield()
            import os, time
        ## Test of Actual Map Data
            self.Clear()
            start = time.clock()
            Shorelines = Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
            print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
            start = time.clock()
            for segment in Shorelines:
                self.object_list.append(self.Canvas.AddLine(segment))
            print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
            start = time.clock()
            self.Canvas.ZoomToBB()
            print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
    
    ##    def LineTest(self,event = None):
    ##        wxGetApp().Yield()
    ##        import os, time
    ##        import random
    ##        Range = (-10,10)
    ##    ## Test of drawing lots of lines
    ##        self.Clear()
    ##        start = time.clock()
    ##        linepoints = []
    ##        linecolors = []
    ##        linewidths = []
    ##        linestyles = []
    ##        for i in range(500):
    ##            points = (random.randint(Range[0],Range[1]),
    ##                     random.randint(Range[0],Range[1]))
    ##            linepoints.append(points)
    ##            points = (random.randint(Range[0],Range[1]),
    ##                     random.randint(Range[0],Range[1]))
    ##            linepoints.append(points)
    ##            linewidths.append(random.randint(1,10) )
    ##            linecolors.append(colors[random.randint(0,len(colors)-1) ])
    ##            linestyles.append(LineStyles[random.randint(0, len(LineStyles)-1)])
    
    ##        self.object_list.append(self.Canvas.AddLineSet(linepoints, LineWidths = linewidths, LineColors = linecolors, LineStyles = linestyles))
    ##        print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
    ##        start = time.clock()
    ##        self.Canvas.ZoomToBB()
    ##        print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
    
        def LineTest(self,event = None):
            wxGetApp().Yield()
            import os, time
            import random
            Range = (-10,10)
        ## Test of drawing lots of lines
            self.Clear()
            start = time.clock()
            linepoints = []
            linecolors = []
            linewidths = []
            for i in range(2000):
                points = (random.randint(Range[0],Range[1]),
                         random.randint(Range[0],Range[1]),
                         random.randint(Range[0],Range[1]),
                         random.randint(Range[0],Range[1]))
                linepoints.append(points)
                linewidths.append(random.randint(1,10) )
                linecolors.append(random.randint(0,len(colors)-1) )
            for (points,color,width) in zip(linepoints,linecolors,linewidths):
                self.object_list.append(self.Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color]))
            print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
            start = time.clock()
            self.Canvas.ZoomToBB()
            print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
    
    class DemoApp(wxApp):
        """
        How the demo works:
        
        Under the Draw menu, there are three options:
        
        *Draw Test: will put up a picture of a bunch of randomly generated
        objects, of each kind supported.
        
        *Draw Map: will draw a map of the world. Be patient, it is a big map,
        with a lot of data, and will take a while to load and draw (about 10 sec 
        on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
        performance is not very good for large drawings.
        
        *Clear: Clears the Canvas.
        
        Once you have a picture drawn, you can zoom in and out and move about
        the picture. There is a tool bar with three tools that can be
        selected. 
        
        The magnifying glass with the plus is the zoom in tool. Once selected,
        if you click the image, it will zoom in, centered on where you
        clicked. If you click and drag the mouse, you will get a rubber band
        box, and the image will zoom to fit that box when you release it.
        
        The magnifying glass with the minus is the zoom out tool. Once selected,
        if you click the image, it will zoom out, centered on where you
        clicked. (note that this takes a while when you are looking at the map,
        as it has a LOT of lines to be drawn. The image is double buffered, so
        you don't see the drawing in progress)
        
        The hand is the move tool. Once selected, if you click and drag on the
        image, it will move so that the part you clicked on ends up where you
        release the mouse. Nothing is changed while you are dragging. The
        drawing is too slow for that.
        
        I'd like the cursor to change as you change tools, but the stock
        wxCursors didn't include anything I liked, so I stuck with the
        pointer. Please let me know if you have any nice cursor images for me to
        use.
        
        
        Any bugs, comments, feedback, questions, and especially code are welcome:
        
        -Chris Barker
        
        Chris.Barker@noaa.gov
    
        """
        
        def OnInit(self):
            frame = DrawFrame(NULL, -1, "FloatCanvas Demo App",wxDefaultPosition,wxSize(700,700))
    
            self.SetTopWindow(frame)
    
            return True
                
    def Read_MapGen(filename,stats = 0,AllLines=0):
        """
        This function reads a MapGen Format file, and
        returns a list of NumPy arrays with the line segments in them.
        
        Each NumPy array in the list is an NX2 array of Python Floats.
        
        The demo should have come with a file, "world.dat" that is the
        shorelines of the whole world, in MapGen format.
        
        """
        import string
        from Numeric import array
        file = open(filename,'rt')
        data = file.readlines()
        data = map(string.strip,data)
        
        Shorelines = []
        segment = []
        for line in data:
            if line:
                if line == "# -b": #New segment beginning
                    if segment: Shorelines.append(array(segment))
                    segment = []
                else:
                    segment.append(map(float,string.split(line)))
        if segment: Shorelines.append(array(segment))
        
        if stats:
            NumSegments = len(Shorelines)
            NumPoints = 0
            for segment in Shorelines:
                NumPoints = NumPoints + len(segment)
            AvgPoints = NumPoints / NumSegments
            print "Number of Segments: ", NumSegments
            print "Average Number of Points per segment: ",AvgPoints
        if AllLines:
            Lines = []
            for segment in Shorelines:
                Lines.append(segment[0])
                for point in segment[1:-1]:
                    Lines.append(point)
                    Lines.append(point)
                Lines.append(segment[-1])
            #print Shorelines
            #for point in Lines: print point
            return Lines
        else:
            return Shorelines
    
    ## for the wxPython demo:
    overview = floatcanvas.FloatCanvas.__doc__
      
if __name__ == "__main__":
    if not haveNumeric:
        print errorText
    else:
        app = DemoApp(0)
        app.MainLoop()