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 |
self.canvas.Map().AddLayer(layer) |
253 |
dlg.Destroy() |
254 |
|
255 |
def RemoveLayer(self): |
256 |
layer = self.current_layer() |
257 |
if layer is not None: |
258 |
self.canvas.Map().RemoveLayer(layer) |
259 |
else: |
260 |
dlg = wxMessageDialog(self, "No layer is selected.\n", |
261 |
"Remove layer", wxOK | wxICON_INFORMATION) |
262 |
dlg.ShowModal() |
263 |
dlg.Destroy() |
264 |
|
265 |
def RaiseLayer(self): |
266 |
layer = self.current_layer() |
267 |
if layer is not None: |
268 |
self.canvas.Map().RaiseLayer(layer) |
269 |
|
270 |
def LowerLayer(self): |
271 |
layer = self.current_layer() |
272 |
if layer is not None: |
273 |
self.canvas.Map().LowerLayer(layer) |
274 |
|
275 |
def current_layer(self): |
276 |
"""Return the currently selected layer. |
277 |
|
278 |
If no layer is selected, return None |
279 |
""" |
280 |
tree = main.app.tree.tree |
281 |
layer = tree.GetPyData(tree.GetSelection()) |
282 |
if isinstance(layer, Layer): |
283 |
return layer |
284 |
return None |
285 |
|
286 |
def has_selected_layer(self): |
287 |
"""Return true if a layer is currently selected""" |
288 |
tree = main.app.tree.tree |
289 |
layer = tree.GetPyData(tree.GetSelection()) |
290 |
return isinstance(layer, Layer) |
291 |
|
292 |
def choose_color(self): |
293 |
"""Run the color selection dialog and return the selected color. |
294 |
|
295 |
If the user cancels, return None. |
296 |
""" |
297 |
dlg = wxColourDialog(self) |
298 |
color = None |
299 |
if dlg.ShowModal() == wxID_OK: |
300 |
data = dlg.GetColourData() |
301 |
wxc = data.GetColour() |
302 |
color = Color(wxc.Red() / 255.0, |
303 |
wxc.Green() / 255.0, |
304 |
wxc.Blue() / 255.0) |
305 |
dlg.Destroy() |
306 |
return color |
307 |
|
308 |
def LayerFillColor(self): |
309 |
layer = self.current_layer() |
310 |
if layer is not None: |
311 |
color = self.choose_color() |
312 |
if color is not None: |
313 |
layer.SetFill(color) |
314 |
|
315 |
def LayerTransparentFill(self): |
316 |
layer = self.current_layer() |
317 |
if layer is not None: |
318 |
layer.SetFill(None) |
319 |
|
320 |
def LayerOutlineColor(self): |
321 |
layer = self.current_layer() |
322 |
if layer is not None: |
323 |
color = self.choose_color() |
324 |
if color is not None: |
325 |
layer.SetStroke(color) |
326 |
|
327 |
def LayerNoOutline(self): |
328 |
layer = self.current_layer() |
329 |
if layer is not None: |
330 |
layer.SetStroke(None) |
331 |
|
332 |
def HideLayer(self): |
333 |
layer = self.current_layer() |
334 |
if layer is not None: |
335 |
layer.SetVisible(0) |
336 |
|
337 |
def ShowLayer(self): |
338 |
layer = self.current_layer() |
339 |
if layer is not None: |
340 |
layer.SetVisible(1) |
341 |
|
342 |
def LayerShowTable(self): |
343 |
layer = self.current_layer() |
344 |
if layer is not None: |
345 |
tv = tableview.TableFrame(self, layer.table) |
346 |
tv.Show(true) |
347 |
|
348 |
def Projection(self): |
349 |
map = self.canvas.Map() |
350 |
proj = map.projection |
351 |
if proj is None: |
352 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None) |
353 |
else: |
354 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params) |
355 |
if proj4Dlg.ShowModal() == wxID_OK: |
356 |
params = proj4Dlg.GetParams() |
357 |
if params is not None: |
358 |
proj = Projection(params) |
359 |
else: |
360 |
proj = None |
361 |
map.SetProjection(proj) |
362 |
proj4Dlg.Destroy() |
363 |
|
364 |
def ZoomInTool(self): |
365 |
self.canvas.ZoomInTool() |
366 |
|
367 |
def ZoomOutTool(self): |
368 |
self.canvas.ZoomOutTool() |
369 |
|
370 |
def PanTool(self): |
371 |
self.canvas.PanTool() |
372 |
|
373 |
def IdentifyTool(self): |
374 |
if self.identify_view is None: |
375 |
self.identify_view = identifyview.IdentifyView(self, main.app) |
376 |
self.identify_view.Show(true) |
377 |
self.canvas.IdentifyTool() |
378 |
|
379 |
def LabelTool(self): |
380 |
self.canvas.LabelTool() |
381 |
|
382 |
def FullExtent(self): |
383 |
self.canvas.FitMapToWindow() |
384 |
|
385 |
def PrintMap(self): |
386 |
self.canvas.Print() |
387 |
|
388 |
|
389 |
# |
390 |
# Define all the commands available in the main window |
391 |
# |
392 |
|
393 |
|
394 |
# Helper functions to define common command implementations |
395 |
def call_method(context, methodname, *args): |
396 |
"""Call the context's method methodname with args *args""" |
397 |
apply(getattr(context, methodname), args) |
398 |
|
399 |
def _method_command(name, title, method, helptext = "", sensitive = None): |
400 |
"""Add a command implemented by a method of the context object""" |
401 |
registry.Add(Command(name, title, call_method, args=(method,), |
402 |
helptext = helptext, sensitive = sensitive)) |
403 |
def _tool_command(name, title, method, toolname, helptext = "", |
404 |
icon = ""): |
405 |
"""Add a tool command""" |
406 |
def check_current_tool(context, name=toolname): |
407 |
return context.canvas.CurrentTool() == name |
408 |
registry.Add(Command(name, title, call_method, args=(method,), |
409 |
helptext = helptext, icon = icon, |
410 |
checked = check_current_tool)) |
411 |
|
412 |
def _has_selected_layer(context): |
413 |
"""Return true if a layer is selected in the context""" |
414 |
return context.has_selected_layer() |
415 |
|
416 |
# File menu |
417 |
_method_command("new_session", "&New Session", "NewSession") |
418 |
_method_command("open_session", "&Open Session", "OpenSession") |
419 |
_method_command("save_session", "&Save Session", "SaveSession") |
420 |
_method_command("save_session_as", "Save Session &As", "SaveSessionAs") |
421 |
_method_command("exit", "&Exit", "Exit") |
422 |
|
423 |
# Help menu |
424 |
_method_command("help_about", "&About", "About") |
425 |
|
426 |
|
427 |
# Map menu |
428 |
_method_command("map_projection", "Pro&jection", "Projection") |
429 |
|
430 |
_tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool", |
431 |
helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in") |
432 |
_tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool", |
433 |
helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out") |
434 |
_tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool", |
435 |
helptext = "Switch to map-mode 'pan'", icon = "pan") |
436 |
_tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool", |
437 |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
438 |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
439 |
helptext = "Add/Remove labels", icon = "label") |
440 |
_method_command("map_full_extent", "&Full extent", "FullExtent") |
441 |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
442 |
|
443 |
# Layer menu |
444 |
_method_command("layer_add", "&Add", "AddLayer", |
445 |
helptext = "Add a new layer to active map") |
446 |
_method_command("layer_remove", "&Remove", "RemoveLayer", |
447 |
helptext = "Remove selected layer(s)", |
448 |
sensitive = _has_selected_layer) |
449 |
_method_command("layer_fill_color", "&Fill Color", "LayerFillColor", |
450 |
helptext = "Set the fill color of selected layer(s)", |
451 |
sensitive = _has_selected_layer) |
452 |
_method_command("layer_transparent_fill", "&Transparent Fill", |
453 |
"LayerTransparentFill", |
454 |
helptext = "Do not fill the selected layer(s)", |
455 |
sensitive = _has_selected_layer) |
456 |
_method_command("layer_ourline_color", "&Outline Color", "LayerOutlineColor", |
457 |
helptext = "Set the outline color of selected layer(s)", |
458 |
sensitive = _has_selected_layer) |
459 |
_method_command("layer_no_outline", "&No Outline", "LayerNoOutline", |
460 |
helptext = "Do not draw the outline of the selected layer(s)", |
461 |
sensitive = _has_selected_layer) |
462 |
_method_command("layer_raise", "&Raise", "RaiseLayer", |
463 |
helptext = "Raise selected layer(s)", |
464 |
sensitive = _has_selected_layer) |
465 |
_method_command("layer_lower", "&Lower", "LowerLayer", |
466 |
helptext = "Lower selected layer(s)", |
467 |
sensitive = _has_selected_layer) |
468 |
_method_command("layer_show", "&Show", "ShowLayer", |
469 |
helptext = "Make selected layer(s) visible", |
470 |
sensitive = _has_selected_layer) |
471 |
_method_command("layer_hide", "&Hide", "HideLayer", |
472 |
helptext = "Make selected layer(s) unvisible", |
473 |
sensitive = _has_selected_layer) |
474 |
_method_command("layer_show_table", "Show Ta&ble", "LayerShowTable", |
475 |
helptext = "Show the selected layer's table", |
476 |
sensitive = _has_selected_layer) |