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