/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/mainwindow.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/mainwindow.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations)
Mon Sep 3 16:25:09 2001 UTC (23 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/mainwindow.py
File MIME type: text/x-python
File size: 16816 byte(s)
* Thuban/UI/mainwindow.py (MainWindow.AddLayer): Fit the map to
the window when the first layer is added to the map.

* setup.py (ThubanInstall.run): Honor the build root (self.root)
when linking thuban.py to <prefix>/bin

1 # Copyright (C) 2001 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]>
4 # Bernhard Herzog <[email protected]>
5 #
6 # This program is free software under the GPL (>=v2)
7 # Read the file COPYING coming with Thuban for details.
8
9 """
10 The main window
11 """
12
13 __version__ = "$Revision$"
14
15 import sys, os
16
17 from wxPython.wx import *
18
19 import Thuban
20 from Thuban.Model.session import Session
21 from Thuban.Model.map import Map
22 from Thuban.Model.layer import Layer
23 from Thuban.Model.color import Color
24 from Thuban.Model.proj import Projection
25
26 import view
27 import tree
28 import proj4dialog
29 import tableview, identifyview
30
31 import main
32 from command import registry, Command
33
34
35 # the directory where the toolbar icons are stored
36 bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps")
37 bitmapext = ".xpm"
38
39
40 class MainWindow(wxFrame):
41
42 def __init__(self, parent, ID):
43 wxFrame.__init__(self, parent, ID, 'Thuban',
44 wxDefaultPosition, wxSize(400, 300))
45
46 self.CreateStatusBar()
47 self.SetStatusText("This is the wxPython-based "
48 "Graphical User Interface for exploring geographic data")
49
50 self.identify_view = None
51
52 self.init_ids()
53
54 menuBar = wxMenuBar()
55
56 menu = wxMenu()
57 menuBar.Append(menu, "&File");
58 for name in ["new_session", "open_session", None,
59 "save_session", "save_session_as", None,
60 "exit"]:
61 self.add_menu_command(menu, name)
62
63 menu = wxMenu()
64 menuBar.Append(menu, "&Map");
65 for name in ["map_projection",
66 None,
67 "map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
68 "map_identify_tool", "map_label_tool",
69 None,
70 "map_full_extent",
71 None,
72 "map_print"]:
73 self.add_menu_command(menu, name)
74
75 menu = wxMenu()
76 menuBar.Append(menu, "&Layer");
77 for name in ["layer_add", "layer_remove",
78 None,
79 "layer_fill_color", "layer_transparent_fill",
80 "layer_ourline_color", "layer_no_outline",
81 None,
82 "layer_raise", "layer_lower",
83 None,
84 "layer_show", "layer_hide",
85 None,
86 "layer_show_table"]:
87 self.add_menu_command(menu, name)
88
89 menu = wxMenu()
90 menuBar.Append(menu, "&Help");
91 self.add_menu_command(menu, "help_about")
92
93 self.SetMenuBar(menuBar)
94
95 # toolbar
96 toolbar = self.CreateToolBar(wxTB_3DBUTTONS)
97 for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
98 "map_identify_tool", "map_label_tool"]:
99 self.add_toolbar_command(toolbar, name)
100 # call Realize to make sure that the tools appear.
101 toolbar.Realize()
102
103 # Create the map canvas
104 canvas = view.MapCanvas(self, -1)
105 self.canvas = canvas
106
107 EVT_CLOSE(self, self.OnClose)
108
109 def init_ids(self):
110 """Initialize the ids"""
111 self.current_id = 6000
112 self.id_to_name = {}
113 self.name_to_id = {}
114
115 def get_id(self, name):
116 """Return the wxWindows id for the command named name.
117
118 Create a new one if there isn't one yet"""
119 ID = self.name_to_id.get(name)
120 if ID is None:
121 ID = self.current_id
122 self.current_id = self.current_id + 1
123 self.name_to_id[name] = ID
124 self.id_to_name[ID] = name
125 return ID
126
127 def add_menu_command(self, menu, name):
128 """Add the command with name name to the menu menu.
129
130 If name is None, add a separator.
131 """
132 if name is None:
133 menu.AppendSeparator()
134 else:
135 command = registry.Command(name)
136 if command is not None:
137 ID = self.get_id(name)
138 menu.Append(ID, command.Title(), command.HelpText(),
139 command.IsCheckCommand())
140 EVT_MENU(self, ID, self.invoke_command)
141 if command.IsDynamic():
142 EVT_UPDATE_UI(self, ID, self.update_command_ui)
143 else:
144 print "Unknown command %s" % name
145
146 def add_toolbar_command(self, toolbar, name):
147 """Add the command with name name to the toolbar toolbar.
148
149 If name is None, add a separator.
150 """
151 # Assume that all toolbar commands are also menu commmands so
152 # that we don't have to add the event handlers here
153 if name is None:
154 toolbar.AddSeparator()
155 else:
156 command = registry.Command(name)
157 if command is not None:
158 ID = self.get_id(name)
159 filename = os.path.join(bitmapdir, command.Icon()) + bitmapext
160 bitmap = wxBitmap(filename, wxBITMAP_TYPE_XPM)
161 toolbar.AddTool(ID, bitmap,
162 shortHelpString = command.HelpText(),
163 isToggle = command.IsCheckCommand())
164 else:
165 print "Unknown command %s" % name
166
167 def invoke_command(self, event):
168 name = self.id_to_name.get(event.GetId())
169 if name is not None:
170 command = registry.Command(name)
171 command.Execute(self)
172 else:
173 print "Unknown command ID %d" % event.GetId()
174
175 def update_command_ui(self, event):
176 #print "update_command_ui", self.id_to_name[event.GetId()]
177 command = registry.Command(self.id_to_name[event.GetId()])
178 if command is not None:
179 event.Enable(command.Sensitive(self))
180 event.SetText(command.DynText(self))
181 if command.IsCheckCommand():
182 event.Check(command.Checked(self))
183
184 def NewSession(self):
185 session = Session("")
186 session.AddMap(Map(""))
187 main.app.SetSession(session)
188
189 def OpenSession(self):
190 dlg = wxFileDialog(self, "Select a session file", ".", "",
191 "*.session", wxOPEN)
192 if dlg.ShowModal() == wxID_OK:
193 main.app.OpenSession(dlg.GetPath())
194 dlg.Destroy()
195
196 def SaveSession(self):
197 main.app.SaveSession()
198
199 def SaveSessionAs(self):
200 dlg = wxFileDialog(self, "Enter a filename for session", ".", "",
201 "*.session", wxOPEN)
202 if dlg.ShowModal() == wxID_OK:
203 main.app.session.SetFilename(dlg.GetPath())
204 main.app.SaveSession()
205 dlg.Destroy()
206
207 def Exit(self):
208 self.Close(false)
209
210 def OnClose(self, event):
211 veto = 0
212 if main.app.session.WasModified():
213 flags = wxYES_NO | wxICON_QUESTION
214 if event.CanVeto():
215 flags = flags | wxCANCEL
216 dlg = wxMessageDialog(self,
217 ("The session has been modified."
218 " Do you want to save it?"),
219 "Exit", flags)
220 result = dlg.ShowModal()
221 dlg.Destroy()
222 if result == wxID_YES:
223 self.SaveSession()
224 elif result == wxID_CANCEL:
225 veto = 1
226
227 if veto:
228 event.Veto()
229 else:
230 self.Destroy()
231
232 def SetMap(self, map):
233 self.canvas.SetMap(map)
234
235 def About(self):
236 dlg = wxMessageDialog(self,
237 ("Thuban is a program for\n"
238 "exploring geographic data.\n"
239 "Copyright (C) 2001 Intevation GmbH.\n"
240 "Thuban is licensed under the GPL"),
241 "About", wxOK | wxICON_INFORMATION)
242 dlg.ShowModal()
243 dlg.Destroy()
244
245 def AddLayer(self):
246 dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*",
247 wxOPEN)
248 if dlg.ShowModal() == wxID_OK:
249 filename = dlg.GetPath()
250 title = os.path.splitext(os.path.basename(filename))[0]
251 layer = Layer(title, filename)
252 map = self.canvas.Map()
253 has_layers = map.HasLayers()
254 map.AddLayer(layer)
255 if not has_layers:
256 # if we're adding a layer to an empty map, for the new
257 # map to the window
258 self.canvas.FitMapToWindow()
259 dlg.Destroy()
260
261 def RemoveLayer(self):
262 layer = self.current_layer()
263 if layer is not None:
264 self.canvas.Map().RemoveLayer(layer)
265 else:
266 dlg = wxMessageDialog(self, "No layer is selected.\n",
267 "Remove layer", wxOK | wxICON_INFORMATION)
268 dlg.ShowModal()
269 dlg.Destroy()
270
271 def RaiseLayer(self):
272 layer = self.current_layer()
273 if layer is not None:
274 self.canvas.Map().RaiseLayer(layer)
275
276 def LowerLayer(self):
277 layer = self.current_layer()
278 if layer is not None:
279 self.canvas.Map().LowerLayer(layer)
280
281 def current_layer(self):
282 """Return the currently selected layer.
283
284 If no layer is selected, return None
285 """
286 tree = main.app.tree.tree
287 layer = tree.GetPyData(tree.GetSelection())
288 if isinstance(layer, Layer):
289 return layer
290 return None
291
292 def has_selected_layer(self):
293 """Return true if a layer is currently selected"""
294 tree = main.app.tree.tree
295 layer = tree.GetPyData(tree.GetSelection())
296 return isinstance(layer, Layer)
297
298 def choose_color(self):
299 """Run the color selection dialog and return the selected color.
300
301 If the user cancels, return None.
302 """
303 dlg = wxColourDialog(self)
304 color = None
305 if dlg.ShowModal() == wxID_OK:
306 data = dlg.GetColourData()
307 wxc = data.GetColour()
308 color = Color(wxc.Red() / 255.0,
309 wxc.Green() / 255.0,
310 wxc.Blue() / 255.0)
311 dlg.Destroy()
312 return color
313
314 def LayerFillColor(self):
315 layer = self.current_layer()
316 if layer is not None:
317 color = self.choose_color()
318 if color is not None:
319 layer.SetFill(color)
320
321 def LayerTransparentFill(self):
322 layer = self.current_layer()
323 if layer is not None:
324 layer.SetFill(None)
325
326 def LayerOutlineColor(self):
327 layer = self.current_layer()
328 if layer is not None:
329 color = self.choose_color()
330 if color is not None:
331 layer.SetStroke(color)
332
333 def LayerNoOutline(self):
334 layer = self.current_layer()
335 if layer is not None:
336 layer.SetStroke(None)
337
338 def HideLayer(self):
339 layer = self.current_layer()
340 if layer is not None:
341 layer.SetVisible(0)
342
343 def ShowLayer(self):
344 layer = self.current_layer()
345 if layer is not None:
346 layer.SetVisible(1)
347
348 def LayerShowTable(self):
349 layer = self.current_layer()
350 if layer is not None:
351 tv = tableview.TableFrame(self, layer.table)
352 tv.Show(true)
353
354 def Projection(self):
355 map = self.canvas.Map()
356 proj = map.projection
357 if proj is None:
358 proj4Dlg = proj4dialog.Proj4Dialog(NULL, None)
359 else:
360 proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params)
361 if proj4Dlg.ShowModal() == wxID_OK:
362 params = proj4Dlg.GetParams()
363 if params is not None:
364 proj = Projection(params)
365 else:
366 proj = None
367 map.SetProjection(proj)
368 proj4Dlg.Destroy()
369
370 def ZoomInTool(self):
371 self.canvas.ZoomInTool()
372
373 def ZoomOutTool(self):
374 self.canvas.ZoomOutTool()
375
376 def PanTool(self):
377 self.canvas.PanTool()
378
379 def IdentifyTool(self):
380 if self.identify_view is None:
381 self.identify_view = identifyview.IdentifyView(self, main.app)
382 self.identify_view.Show(true)
383 self.canvas.IdentifyTool()
384
385 def LabelTool(self):
386 self.canvas.LabelTool()
387
388 def FullExtent(self):
389 self.canvas.FitMapToWindow()
390
391 def PrintMap(self):
392 self.canvas.Print()
393
394
395 #
396 # Define all the commands available in the main window
397 #
398
399
400 # Helper functions to define common command implementations
401 def call_method(context, methodname, *args):
402 """Call the context's method methodname with args *args"""
403 apply(getattr(context, methodname), args)
404
405 def _method_command(name, title, method, helptext = "", sensitive = None):
406 """Add a command implemented by a method of the context object"""
407 registry.Add(Command(name, title, call_method, args=(method,),
408 helptext = helptext, sensitive = sensitive))
409 def _tool_command(name, title, method, toolname, helptext = "",
410 icon = ""):
411 """Add a tool command"""
412 def check_current_tool(context, name=toolname):
413 return context.canvas.CurrentTool() == name
414 registry.Add(Command(name, title, call_method, args=(method,),
415 helptext = helptext, icon = icon,
416 checked = check_current_tool))
417
418 def _has_selected_layer(context):
419 """Return true if a layer is selected in the context"""
420 return context.has_selected_layer()
421
422 # File menu
423 _method_command("new_session", "&New Session", "NewSession")
424 _method_command("open_session", "&Open Session", "OpenSession")
425 _method_command("save_session", "&Save Session", "SaveSession")
426 _method_command("save_session_as", "Save Session &As", "SaveSessionAs")
427 _method_command("exit", "&Exit", "Exit")
428
429 # Help menu
430 _method_command("help_about", "&About", "About")
431
432
433 # Map menu
434 _method_command("map_projection", "Pro&jection", "Projection")
435
436 _tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool",
437 helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in")
438 _tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool",
439 helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out")
440 _tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool",
441 helptext = "Switch to map-mode 'pan'", icon = "pan")
442 _tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool",
443 helptext = "Switch to map-mode 'identify'", icon = "identify")
444 _tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool",
445 helptext = "Add/Remove labels", icon = "label")
446 _method_command("map_full_extent", "&Full extent", "FullExtent")
447 _method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map")
448
449 # Layer menu
450 _method_command("layer_add", "&Add", "AddLayer",
451 helptext = "Add a new layer to active map")
452 _method_command("layer_remove", "&Remove", "RemoveLayer",
453 helptext = "Remove selected layer(s)",
454 sensitive = _has_selected_layer)
455 _method_command("layer_fill_color", "&Fill Color", "LayerFillColor",
456 helptext = "Set the fill color of selected layer(s)",
457 sensitive = _has_selected_layer)
458 _method_command("layer_transparent_fill", "&Transparent Fill",
459 "LayerTransparentFill",
460 helptext = "Do not fill the selected layer(s)",
461 sensitive = _has_selected_layer)
462 _method_command("layer_ourline_color", "&Outline Color", "LayerOutlineColor",
463 helptext = "Set the outline color of selected layer(s)",
464 sensitive = _has_selected_layer)
465 _method_command("layer_no_outline", "&No Outline", "LayerNoOutline",
466 helptext = "Do not draw the outline of the selected layer(s)",
467 sensitive = _has_selected_layer)
468 _method_command("layer_raise", "&Raise", "RaiseLayer",
469 helptext = "Raise selected layer(s)",
470 sensitive = _has_selected_layer)
471 _method_command("layer_lower", "&Lower", "LowerLayer",
472 helptext = "Lower selected layer(s)",
473 sensitive = _has_selected_layer)
474 _method_command("layer_show", "&Show", "ShowLayer",
475 helptext = "Make selected layer(s) visible",
476 sensitive = _has_selected_layer)
477 _method_command("layer_hide", "&Hide", "HideLayer",
478 helptext = "Make selected layer(s) unvisible",
479 sensitive = _has_selected_layer)
480 _method_command("layer_show_table", "Show Ta&ble", "LayerShowTable",
481 helptext = "Show the selected layer's table",
482 sensitive = _has_selected_layer)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26