/[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 6 - (show annotations)
Tue Aug 28 15:41:52 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: 16440 byte(s)
import all the source files

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