]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/FloatCanvas.py
Applied patch [ 827011 ] Event-based processing of item tooltips in wxTreeCtrl
[wxWidgets.git] / wxPython / demo / FloatCanvas.py
1 # 11/25/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for V2.5
4 #
5
6 import wx
7
8 # Stuff to integrate FloatCanvas into wxPython Demo
9 try:
10 import Numeric
11 haveNumeric = True
12 except ImportError:
13 haveNumeric = False
14
15
16 if not haveNumeric:
17 errorText = """\
18 The FloatCanvas requires the Numeric module:
19 You can get it at:
20 http://sourceforge.net/projects/numpy
21 """
22 def runTest(frame, nb, log):
23 dlg = wx.MessageDialog(
24 frame, errorText, 'Sorry', wx.OK | wx.ICON_INFORMATION
25 )
26
27 dlg.ShowModal()
28 dlg.Destroy()
29
30 overview = ""
31
32 else:
33 from wxPython.lib import floatcanvas
34 import wxPython.lib.colourdb
35
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
45
46 colors = []
47 LineStyles = floatcanvas.draw_object.LineStyleList.keys()
48
49
50
51 class DrawFrame(wx.Frame):
52
53 """
54
55 A frame used for the FloatCanvas Demo
56
57 """
58
59 def __init__(self, parent, id, title, position, size):
60 wx.Frame.__init__(self,parent, id,title,position, size)
61
62 # Set up the MenuBar
63
64 MenuBar = wx.MenuBar()
65
66 file_menu = wx.Menu()
67 file_menu.Append(ID_EXIT_MENU, "&Close","Close this frame")
68 self.Bind(wx.EVT_MENU, self.OnQuit, id=ID_EXIT_MENU)
69 MenuBar.Append(file_menu, "&File")
70
71 draw_menu = wx.Menu()
72 draw_menu.Append(ID_DRAWTEST_MENU, "&Draw Test","Run a test of drawing random components")
73 self.Bind(wx.EVT_MENU, self.DrawTest, id=ID_DRAWTEST_MENU)
74 draw_menu.Append(ID_LINETEST_MENU, "&Line Test","Run a test of drawing random lines")
75 self.Bind(wx.EVT_MENU, self.LineTest, id=ID_LINETEST_MENU)
76 draw_menu.Append(ID_DRAWMAP_MENU, "Draw &Map","Run a test of drawing a map")
77 self.Bind(wx.EVT_MENU, self.DrawMap, id=ID_DRAWMAP_MENU)
78 draw_menu.Append(ID_CLEAR_MENU, "&Clear","Clear the Canvas")
79 self.Bind(wx.EVT_MENU, self.Clear, id=ID_CLEAR_MENU)
80 MenuBar.Append(draw_menu, "&Draw")
81
82 view_menu = wx.Menu()
83 view_menu.Append(ID_ZOOM_TO_FIT_MENU, "Zoom to &Fit","Zoom to fit the window")
84 self.Bind(wx.EVT_MENU, self.ZoomToFit, id=ID_ZOOM_TO_FIT_MENU)
85 MenuBar.Append(view_menu, "&View")
86
87 help_menu = wx.Menu()
88 help_menu.Append(ID_ABOUT_MENU, "&About",
89 "More information About this program")
90 self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT_MENU)
91 MenuBar.Append(help_menu, "&Help")
92
93 self.SetMenuBar(MenuBar)
94
95 self.CreateStatusBar()
96 self.SetStatusText("")
97
98 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
99
100 # Other event handlers:
101 self.Bind(wx.EVT_RIGHT_DOWN, self.RightButtonEvent)
102
103 # Add the Canvas
104 self.Canvas = floatcanvas.FloatCanvas(self,-1,(500,500),
105 ProjectionFun = 'FlatEarth',
106 Debug = 0,
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):
123 dlg = wx.MessageDialog(self, "This is a small program to demonstrate\n"
124 "the use of the FloatCanvas\n",
125 "About Me", wx.OK | wx.ICON_INFORMATION)
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)
132
133 self.ToolBar.ToggleTool(event.GetId(),1)
134
135 if event.GetId() == ID_ZOOM_IN_BUTTON:
136 self.Canvas.SetGUIMode("ZoomIn")
137
138 elif event.GetId() == ID_ZOOM_OUT_BUTTON:
139 self.Canvas.SetGUIMode("ZoomOut")
140
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):
159 wx.GetApp().Yield()
160
161 import random
162 import RandomArray
163
164 Range = (-10,10)
165
166 Canvas = self.Canvas
167 object_list = self.object_list
168
169 # Random tests of everything:
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):
254 wx.GetApp().Yield()
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):
299 wx.GetApp().Yield()
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
324 class DemoApp(wx.App):
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):
375 global colors
376 wxPython.lib.colourdb.updateColourDB()
377 colors = wxPython.lib.colourdb.getColourList()
378
379 frame = DrawFrame(None, -1, "FloatCanvas Demo App",wxDefaultPosition,wxSize(700,700))
380
381 self.SetTopWindow(frame)
382
383 return True
384
385 def Read_MapGen(filename,stats = 0,AllLines=0):
386 """
387 This function reads a MapGen Format file, and
388 returns a list of NumPy arrays with the line segments in them.
389
390 Each NumPy array in the list is an NX2 array of Python Floats.
391
392 The demo should have come with a file, "world.dat" that is the
393 shorelines of the whole world, in MapGen format.
394
395 """
396 import string
397 from Numeric import array
398 file = open(filename,'rt')
399 data = file.readlines()
400 data = map(string.strip,data)
401
402 Shorelines = []
403 segment = []
404 for line in data:
405 if line:
406 if line == "# -b": #New segment beginning
407 if segment: Shorelines.append(array(segment))
408 segment = []
409 else:
410 segment.append(map(float,string.split(line)))
411 if segment: Shorelines.append(array(segment))
412
413 if stats:
414 NumSegments = len(Shorelines)
415 NumPoints = 0
416 for segment in Shorelines:
417 NumPoints = NumPoints + len(segment)
418 AvgPoints = NumPoints / NumSegments
419 print "Number of Segments: ", NumSegments
420 print "Average Number of Points per segment: ",AvgPoints
421 if AllLines:
422 Lines = []
423 for segment in Shorelines:
424 Lines.append(segment[0])
425 for point in segment[1:-1]:
426 Lines.append(point)
427 Lines.append(point)
428 Lines.append(segment[-1])
429 #print Shorelines
430 #for point in Lines: print point
431 return Lines
432 else:
433 return Shorelines
434
435
436 #----------------------------------------------------------------------
437
438 def runTest(frame, nb, log):
439 """
440 This method is used by the wxPython Demo Framework for integrating
441 this demo with the rest.
442 """
443 global colors
444 wxPython.lib.colourdb.updateColourDB()
445 colors = wxPython.lib.colourdb.getColourList()
446
447 win = DrawFrame(None, -1, "FloatCanvas Drawing Window",wxDefaultPosition,wxSize(500,500))
448 frame.otherWin = win
449 win.Show(True)
450
451
452
453
454 ## for the wxPython demo:
455 overview = floatcanvas.FloatCanvas.__doc__
456
457
458
459 if __name__ == "__main__":
460 if not haveNumeric:
461 print errorText
462 else:
463 app = DemoApp(0)
464
465 import wx.lib.colourdb
466 wx.lib.colourdb.updateColourDB()
467 colors = wx.lib.colourdb.getColourList()
468
469 app.MainLoop()
470
471
472
473