]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/FloatCanvas.py
reSWIGged
[wxWidgets.git] / wxPython / demo / FloatCanvas.py
CommitLineData
8fa876ca
RD
1# 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for V2.5
4#
8b9a4190 5
8fa876ca 6import wx
8b9a4190 7
8fa876ca 8# Stuff to integrate FloatCanvas into wxPython Demo
8b9a4190
RD
9try:
10 import Numeric
11 haveNumeric = True
12except ImportError:
13 haveNumeric = False
14
15
16if not haveNumeric:
17 errorText = """\
18The FloatCanvas requires the Numeric module:
19You can get it at:
20 http://sourceforge.net/projects/numpy
21"""
22 def runTest(frame, nb, log):
8fa876ca
RD
23 dlg = wx.MessageDialog(
24 frame, errorText, 'Sorry', wx.OK | wx.ICON_INFORMATION
25 )
26
8b9a4190
RD
27 dlg.ShowModal()
28 dlg.Destroy()
29
30 overview = ""
31
32else:
8b9a4190
RD
33 from wxPython.lib import floatcanvas
34 import wxPython.lib.colourdb
35
8fa876ca
RD
36 ID_ABOUT_MENU = wx.NewId()
37 ID_EXIT_MENU = wx.NewId()
38 ID_ZOOM_TO_FIT_MENU = wx.NewId()
39 ID_DRAWTEST_MENU = wx.NewId()
40 ID_LINETEST_MENU = wx.NewId()
41 ID_DRAWMAP_MENU = wx.NewId()
42 ID_DRAWMAP2_MENU = wx.NewId()
43 ID_CLEAR_MENU = wx.NewId()
44
8b9a4190 45
fbd5dd1d 46 colors = []
8b9a4190 47 LineStyles = floatcanvas.draw_object.LineStyleList.keys()
fbd5dd1d
RD
48
49
8b9a4190 50
8fa876ca 51 class DrawFrame(wx.Frame):
8b9a4190
RD
52
53 """
54
55 A frame used for the FloatCanvas Demo
56
57 """
58
8fa876ca
RD
59 def __init__(self, parent, id, title, position, size):
60 wx.Frame.__init__(self,parent, id,title,position, size)
8b9a4190 61
8fa876ca 62 # Set up the MenuBar
8b9a4190 63
8fa876ca 64 MenuBar = wx.MenuBar()
8b9a4190 65
8fa876ca 66 file_menu = wx.Menu()
8b9a4190 67 file_menu.Append(ID_EXIT_MENU, "&Close","Close this frame")
8fa876ca 68 self.Bind(wx.EVT_MENU, self.OnQuit, id=ID_EXIT_MENU)
8b9a4190
RD
69 MenuBar.Append(file_menu, "&File")
70
8fa876ca 71 draw_menu = wx.Menu()
8b9a4190 72 draw_menu.Append(ID_DRAWTEST_MENU, "&Draw Test","Run a test of drawing random components")
8fa876ca 73 self.Bind(wx.EVT_MENU, self.DrawTest, id=ID_DRAWTEST_MENU)
8b9a4190 74 draw_menu.Append(ID_LINETEST_MENU, "&Line Test","Run a test of drawing random lines")
8fa876ca 75 self.Bind(wx.EVT_MENU, self.LineTest, id=ID_LINETEST_MENU)
8b9a4190 76 draw_menu.Append(ID_DRAWMAP_MENU, "Draw &Map","Run a test of drawing a map")
8fa876ca 77 self.Bind(wx.EVT_MENU, self.DrawMap, id=ID_DRAWMAP_MENU)
8b9a4190 78 draw_menu.Append(ID_CLEAR_MENU, "&Clear","Clear the Canvas")
8fa876ca 79 self.Bind(wx.EVT_MENU, self.Clear, id=ID_CLEAR_MENU)
8b9a4190 80 MenuBar.Append(draw_menu, "&Draw")
8fa876ca
RD
81
82 view_menu = wx.Menu()
8b9a4190 83 view_menu.Append(ID_ZOOM_TO_FIT_MENU, "Zoom to &Fit","Zoom to fit the window")
8fa876ca 84 self.Bind(wx.EVT_MENU, self.ZoomToFit, id=ID_ZOOM_TO_FIT_MENU)
8b9a4190
RD
85 MenuBar.Append(view_menu, "&View")
86
8fa876ca 87 help_menu = wx.Menu()
8b9a4190
RD
88 help_menu.Append(ID_ABOUT_MENU, "&About",
89 "More information About this program")
8fa876ca 90 self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT_MENU)
8b9a4190
RD
91 MenuBar.Append(help_menu, "&Help")
92
93 self.SetMenuBar(MenuBar)
94
95 self.CreateStatusBar()
96 self.SetStatusText("")
97
8fa876ca 98 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
8b9a4190
RD
99
100 # Other event handlers:
8fa876ca 101 self.Bind(wx.EVT_RIGHT_DOWN, self.RightButtonEvent)
8b9a4190
RD
102
103 # Add the Canvas
104 self.Canvas = floatcanvas.FloatCanvas(self,-1,(500,500),
105 ProjectionFun = 'FlatEarth',
118f4724 106 Debug = 0,
8b9a4190
RD
107 EnclosingFrame = self,
108 BackgroundColor = "DARK SLATE BLUE",
109 UseBackground = 0,
110 UseToolbar = 1)
111 self.Show(True)
112
113 self.object_list = []
114
115 return None
116
117 def RightButtonEvent(self,event):
118 print "Right Button has been clicked in DrawFrame"
119 print "coords are: %i, %i"%(event.GetX(),event.GetY())
120 event.Skip()
121
122 def OnAbout(self, event):
8fa876ca 123 dlg = wx.MessageDialog(self, "This is a small program to demonstrate\n"
8b9a4190 124 "the use of the FloatCanvas\n",
8fa876ca 125 "About Me", wx.OK | wx.ICON_INFORMATION)
8b9a4190
RD
126 dlg.ShowModal()
127 dlg.Destroy()
128
129 def SetMode(self,event):
130 for id in [ID_ZOOM_IN_BUTTON,ID_ZOOM_OUT_BUTTON,ID_MOVE_MODE_BUTTON]:
131 self.ToolBar.ToggleTool(id,0)
8fa876ca 132
8b9a4190 133 self.ToolBar.ToggleTool(event.GetId(),1)
8fa876ca 134
8b9a4190
RD
135 if event.GetId() == ID_ZOOM_IN_BUTTON:
136 self.Canvas.SetGUIMode("ZoomIn")
8fa876ca 137
8b9a4190
RD
138 elif event.GetId() == ID_ZOOM_OUT_BUTTON:
139 self.Canvas.SetGUIMode("ZoomOut")
8fa876ca 140
8b9a4190
RD
141 elif event.GetId() == ID_MOVE_MODE_BUTTON:
142 self.Canvas.SetGUIMode("Move")
143
144 def ZoomToFit(self,event):
145 self.Canvas.ZoomToBB()
146
147 def Clear(self,event = None):
148 self.Canvas.RemoveObjects(self.object_list)
149 self.object_list = []
150 self.Canvas.Draw()
151
152 def OnQuit(self,event):
153 self.Close(True)
154
155 def OnCloseWindow(self, event):
156 self.Destroy()
157
158 def DrawTest(self,event):
8fa876ca
RD
159 wx.GetApp().Yield()
160
8b9a4190
RD
161 import random
162 import RandomArray
8fa876ca 163
8b9a4190
RD
164 Range = (-10,10)
165
166 Canvas = self.Canvas
167 object_list = self.object_list
168
8fa876ca 169 # Random tests of everything:
8b9a4190
RD
170
171 # Rectangles
172 for i in range(5):
173 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
174 lw = random.randint(1,5)
175 cf = random.randint(0,len(colors)-1)
176 h = random.randint(1,5)
177 w = random.randint(1,5)
178 object_list.append(Canvas.AddRectangle(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
179
180 # Ellipses
181 for i in range(5):
182 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
183 lw = random.randint(1,5)
184 cf = random.randint(0,len(colors)-1)
185 h = random.randint(1,5)
186 w = random.randint(1,5)
187 object_list.append(Canvas.AddEllipse(x,y,h,w,LineWidth = lw,FillColor = colors[cf]))
188
189 # Dots
190 for i in range(5):
191 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
192 D = random.randint(1,50)
193 lw = random.randint(1,5)
194 cf = random.randint(0,len(colors)-1)
195 cl = random.randint(0,len(colors)-1)
196 object_list.append(Canvas.AddDot(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf]))
197
198 # Circles
199 for i in range(5):
200 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
201 D = random.randint(1,5)
202 lw = random.randint(1,5)
203 cf = random.randint(0,len(colors)-1)
204 cl = random.randint(0,len(colors)-1)
205 object_list.append(Canvas.AddCircle(x,y,D,LineWidth = lw,LineColor = colors[cl],FillColor = colors[cf]))
206 self.object_list.append(self.Canvas.AddText("Circle # %i"%(i),x,y,Size = 12,BackGround = None,Position = "cc"))
207
208 # Lines
209 for i in range(5):
210 points = []
211 for j in range(random.randint(2,10)):
212 point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
213 points.append(point)
214 lw = random.randint(1,10)
215 cf = random.randint(0,len(colors)-1)
216 cl = random.randint(0,len(colors)-1)
217 self.object_list.append(self.Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
218
219 # Polygons
220 for i in range(3):
221 points = []
222 for j in range(random.randint(2,6)):
223 point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
224 points.append(point)
225 lw = random.randint(1,6)
226 cf = random.randint(0,len(colors)-1)
227 cl = random.randint(0,len(colors)-1)
228 self.object_list.append(self.Canvas.AddPolygon(points,
229 LineWidth = lw,
230 LineColor = colors[cl],
231 FillColor = colors[cf],
232 FillStyle = 'Solid'))
233
234
235 ## Pointset
236 for i in range(4):
237 points = []
238 points = RandomArray.uniform(Range[0],Range[1],(100,2))
239 cf = random.randint(0,len(colors)-1)
240 D = random.randint(1,4)
241 self.object_list.append(self.Canvas.AddPointSet(points, Color = colors[cf], Diameter = D))
242
243 # Text
244 String = "Some text"
245 for i in range(10):
246 ts = random.randint(10,40)
247 cf = random.randint(0,len(colors)-1)
248 x,y = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
249 self.object_list.append(self.Canvas.AddText(String,x,y,Size = ts,ForeGround = colors[cf],Position = "cc"))
250
251 self.Canvas.ZoomToBB()
252
253 def DrawMap(self,event = None):
8fa876ca 254 wx.GetApp().Yield()
8b9a4190
RD
255 import os, time
256 ## Test of Actual Map Data
257 self.Clear()
258 start = time.clock()
259 Shorelines = Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
260 print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
261 start = time.clock()
262 for segment in Shorelines:
263 self.object_list.append(self.Canvas.AddLine(segment))
264 print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
265 start = time.clock()
266 self.Canvas.ZoomToBB()
267 print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
268
269 ## def LineTest(self,event = None):
270 ## wxGetApp().Yield()
271 ## import os, time
272 ## import random
273 ## Range = (-10,10)
274 ## ## Test of drawing lots of lines
275 ## self.Clear()
276 ## start = time.clock()
277 ## linepoints = []
278 ## linecolors = []
279 ## linewidths = []
280 ## linestyles = []
281 ## for i in range(500):
282 ## points = (random.randint(Range[0],Range[1]),
283 ## random.randint(Range[0],Range[1]))
284 ## linepoints.append(points)
285 ## points = (random.randint(Range[0],Range[1]),
286 ## random.randint(Range[0],Range[1]))
287 ## linepoints.append(points)
288 ## linewidths.append(random.randint(1,10) )
289 ## linecolors.append(colors[random.randint(0,len(colors)-1) ])
290 ## linestyles.append(LineStyles[random.randint(0, len(LineStyles)-1)])
291
292 ## self.object_list.append(self.Canvas.AddLineSet(linepoints, LineWidths = linewidths, LineColors = linecolors, LineStyles = linestyles))
293 ## print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
294 ## start = time.clock()
295 ## self.Canvas.ZoomToBB()
296 ## print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
297
298 def LineTest(self,event = None):
8fa876ca 299 wx.GetApp().Yield()
8b9a4190
RD
300 import os, time
301 import random
302 Range = (-10,10)
303 ## Test of drawing lots of lines
304 self.Clear()
305 start = time.clock()
306 linepoints = []
307 linecolors = []
308 linewidths = []
309 for i in range(2000):
310 points = (random.randint(Range[0],Range[1]),
311 random.randint(Range[0],Range[1]),
312 random.randint(Range[0],Range[1]),
313 random.randint(Range[0],Range[1]))
314 linepoints.append(points)
315 linewidths.append(random.randint(1,10) )
316 linecolors.append(random.randint(0,len(colors)-1) )
317 for (points,color,width) in zip(linepoints,linecolors,linewidths):
318 self.object_list.append(self.Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color]))
319 print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
320 start = time.clock()
321 self.Canvas.ZoomToBB()
322 print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
323
8fa876ca 324 class DemoApp(wx.App):
8b9a4190
RD
325 """
326 How the demo works:
327
328 Under the Draw menu, there are three options:
329
330 *Draw Test: will put up a picture of a bunch of randomly generated
331 objects, of each kind supported.
332
333 *Draw Map: will draw a map of the world. Be patient, it is a big map,
334 with a lot of data, and will take a while to load and draw (about 10 sec
335 on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
336 performance is not very good for large drawings.
337
338 *Clear: Clears the Canvas.
339
340 Once you have a picture drawn, you can zoom in and out and move about
341 the picture. There is a tool bar with three tools that can be
342 selected.
343
344 The magnifying glass with the plus is the zoom in tool. Once selected,
345 if you click the image, it will zoom in, centered on where you
346 clicked. If you click and drag the mouse, you will get a rubber band
347 box, and the image will zoom to fit that box when you release it.
348
349 The magnifying glass with the minus is the zoom out tool. Once selected,
350 if you click the image, it will zoom out, centered on where you
351 clicked. (note that this takes a while when you are looking at the map,
352 as it has a LOT of lines to be drawn. The image is double buffered, so
353 you don't see the drawing in progress)
354
355 The hand is the move tool. Once selected, if you click and drag on the
356 image, it will move so that the part you clicked on ends up where you
357 release the mouse. Nothing is changed while you are dragging. The
358 drawing is too slow for that.
359
360 I'd like the cursor to change as you change tools, but the stock
361 wxCursors didn't include anything I liked, so I stuck with the
362 pointer. Please let me know if you have any nice cursor images for me to
363 use.
364
365
366 Any bugs, comments, feedback, questions, and especially code are welcome:
367
368 -Chris Barker
369
370 Chris.Barker@noaa.gov
371
372 """
373
374 def OnInit(self):
fbd5dd1d
RD
375 global colors
376 wxPython.lib.colourdb.updateColourDB()
377 colors = wxPython.lib.colourdb.getColourList()
378
e7325293
RD
379 frame = DrawFrame(None, -1, "FloatCanvas Demo App",
380 wx.DefaultPosition, (700,700))
8b9a4190
RD
381
382 self.SetTopWindow(frame)
383
384 return True
385
386 def Read_MapGen(filename,stats = 0,AllLines=0):
387 """
388 This function reads a MapGen Format file, and
389 returns a list of NumPy arrays with the line segments in them.
390
391 Each NumPy array in the list is an NX2 array of Python Floats.
392
393 The demo should have come with a file, "world.dat" that is the
394 shorelines of the whole world, in MapGen format.
395
396 """
397 import string
398 from Numeric import array
399 file = open(filename,'rt')
400 data = file.readlines()
401 data = map(string.strip,data)
402
403 Shorelines = []
404 segment = []
405 for line in data:
406 if line:
407 if line == "# -b": #New segment beginning
408 if segment: Shorelines.append(array(segment))
409 segment = []
410 else:
411 segment.append(map(float,string.split(line)))
412 if segment: Shorelines.append(array(segment))
413
414 if stats:
415 NumSegments = len(Shorelines)
416 NumPoints = 0
417 for segment in Shorelines:
418 NumPoints = NumPoints + len(segment)
419 AvgPoints = NumPoints / NumSegments
420 print "Number of Segments: ", NumSegments
421 print "Average Number of Points per segment: ",AvgPoints
422 if AllLines:
423 Lines = []
424 for segment in Shorelines:
425 Lines.append(segment[0])
426 for point in segment[1:-1]:
427 Lines.append(point)
428 Lines.append(point)
429 Lines.append(segment[-1])
430 #print Shorelines
431 #for point in Lines: print point
432 return Lines
433 else:
434 return Shorelines
fbd5dd1d
RD
435
436
437 #----------------------------------------------------------------------
438
439 def runTest(frame, nb, log):
440 """
441 This method is used by the wxPython Demo Framework for integrating
442 this demo with the rest.
443 """
444 global colors
445 wxPython.lib.colourdb.updateColourDB()
446 colors = wxPython.lib.colourdb.getColourList()
447
707fbe5d
RD
448 win = DrawFrame(None, -1, "FloatCanvas Drawing Window",
449 wx.DefaultPosition, (500,500))
fbd5dd1d
RD
450 frame.otherWin = win
451 win.Show(True)
452
453
454
8b9a4190
RD
455
456 ## for the wxPython demo:
457 overview = floatcanvas.FloatCanvas.__doc__
fbd5dd1d
RD
458
459
8b9a4190
RD
460
461if __name__ == "__main__":
462 if not haveNumeric:
463 print errorText
464 else:
465 app = DemoApp(0)
8fa876ca
RD
466
467 import wx.lib.colourdb
468 wx.lib.colourdb.updateColourDB()
469 colors = wx.lib.colourdb.getColourList()
470
8b9a4190
RD
471 app.MainLoop()
472
473
474
475