1 |
# Copyright (C) 2001 by Intevation GmbH |
# Copyright (C) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
17 |
from wxPython.wx import * |
from wxPython.wx import * |
18 |
|
|
19 |
import Thuban |
import Thuban |
20 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session, create_empty_session |
21 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
22 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
23 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
30 |
|
|
31 |
import main |
import main |
32 |
from command import registry, Command |
from command import registry, Command |
33 |
|
from messages import SELECTED_SHAPE, VIEW_POSITION |
34 |
|
|
35 |
|
|
36 |
# the directory where the toolbar icons are stored |
# the directory where the toolbar icons are stored |
44 |
wxFrame.__init__(self, parent, ID, 'Thuban', |
wxFrame.__init__(self, parent, ID, 'Thuban', |
45 |
wxDefaultPosition, wxSize(400, 300)) |
wxDefaultPosition, wxSize(400, 300)) |
46 |
|
|
47 |
|
self.interactor = interactor |
48 |
|
|
49 |
self.CreateStatusBar() |
self.CreateStatusBar() |
50 |
self.SetStatusText("This is the wxPython-based " |
self.SetStatusText("This is the wxPython-based " |
51 |
"Graphical User Interface for exploring geographic data") |
"Graphical User Interface for exploring geographic data") |
65 |
|
|
66 |
menu = wxMenu() |
menu = wxMenu() |
67 |
menuBar.Append(menu, "&Map"); |
menuBar.Append(menu, "&Map"); |
68 |
for name in ["map_projection", |
for name in ["layer_add", "layer_remove", |
69 |
|
None, |
70 |
|
"map_projection", |
71 |
None, |
None, |
72 |
"map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
"map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
73 |
"map_identify_tool", "map_label_tool", |
"map_identify_tool", "map_label_tool", |
79 |
|
|
80 |
menu = wxMenu() |
menu = wxMenu() |
81 |
menuBar.Append(menu, "&Layer"); |
menuBar.Append(menu, "&Layer"); |
82 |
for name in ["layer_add", "layer_remove", |
for name in ["layer_fill_color", "layer_transparent_fill", |
|
None, |
|
|
"layer_fill_color", "layer_transparent_fill", |
|
83 |
"layer_ourline_color", "layer_no_outline", |
"layer_ourline_color", "layer_no_outline", |
84 |
None, |
None, |
85 |
"layer_raise", "layer_lower", |
"layer_raise", "layer_lower", |
97 |
|
|
98 |
# toolbar |
# toolbar |
99 |
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
100 |
|
|
101 |
|
# set the size of the tools' bitmaps. Not needed on wxGTK, but |
102 |
|
# on Windows. We probably shouldn't hardwire the bitmap size |
103 |
|
# here |
104 |
|
toolbar.SetToolBitmapSize(wxSize(24, 24)) |
105 |
|
|
106 |
for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
107 |
"map_identify_tool", "map_label_tool"]: |
"map_identify_tool", "map_label_tool", "map_full_extent"]: |
108 |
self.add_toolbar_command(toolbar, name) |
self.add_toolbar_command(toolbar, name) |
109 |
# call Realize to make sure that the tools appear. |
# call Realize to make sure that the tools appear. |
110 |
toolbar.Realize() |
toolbar.Realize() |
111 |
|
|
112 |
# Create the map canvas |
# Create the map canvas |
113 |
canvas = view.MapCanvas(self, -1, interactor) |
canvas = view.MapCanvas(self, -1, interactor) |
114 |
|
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
115 |
self.canvas = canvas |
self.canvas = canvas |
116 |
|
|
117 |
|
self.init_dialogs() |
118 |
|
|
119 |
|
interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand) |
120 |
|
|
121 |
EVT_CLOSE(self, self.OnClose) |
EVT_CLOSE(self, self.OnClose) |
122 |
|
|
123 |
def init_ids(self): |
def init_ids(self): |
203 |
dlg.Destroy() |
dlg.Destroy() |
204 |
return result |
return result |
205 |
|
|
206 |
|
def init_dialogs(self): |
207 |
|
"""Initialize the dialog handling""" |
208 |
|
# The mainwindow maintains a dict mapping names to open |
209 |
|
# non-modal dialogs. The dialogs are put into this dict when |
210 |
|
# they're created and removed when they're closed |
211 |
|
self.dialogs = {} |
212 |
|
|
213 |
|
def add_dialog(self, name, dialog): |
214 |
|
if self.dialogs.has_key(name): |
215 |
|
raise RuntimeError("The Dialog named %s is already open" % name) |
216 |
|
self.dialogs[name] = dialog |
217 |
|
|
218 |
|
def dialog_open(self, name): |
219 |
|
return self.dialogs.has_key(name) |
220 |
|
|
221 |
|
def remove_dialog(self, name): |
222 |
|
del self.dialogs[name] |
223 |
|
|
224 |
|
def get_open_dialog(self, name): |
225 |
|
return self.dialogs.get(name) |
226 |
|
|
227 |
|
def view_position_changed(self): |
228 |
|
pos = self.canvas.CurrentPosition() |
229 |
|
if pos is not None: |
230 |
|
text = "(%10.10g, %10.10g)" % pos |
231 |
|
else: |
232 |
|
text = "" |
233 |
|
self.SetStatusText(text) |
234 |
|
|
235 |
|
def save_modified_session(self, can_veto = 1): |
236 |
|
"""If the current session has been modified, ask the user |
237 |
|
whether to save it and do so if requested. Return the outcome of |
238 |
|
the dialog (either wxID_OK, wxID_CANCEL or wxID_NO). If the |
239 |
|
dialog wasn't run return wxID_NO. |
240 |
|
|
241 |
|
If the can_veto parameter is true (default) the dialog includes |
242 |
|
a cancel button, otherwise not. |
243 |
|
""" |
244 |
|
if main.app.session.WasModified(): |
245 |
|
flags = wxYES_NO | wxICON_QUESTION |
246 |
|
if can_veto: |
247 |
|
flags = flags | wxCANCEL |
248 |
|
result = self.RunMessageBox("Exit", |
249 |
|
("The session has been modified." |
250 |
|
" Do you want to save it?"), |
251 |
|
flags) |
252 |
|
if result == wxID_YES: |
253 |
|
self.SaveSession() |
254 |
|
else: |
255 |
|
result = wxID_NO |
256 |
|
return result |
257 |
|
|
258 |
def NewSession(self): |
def NewSession(self): |
259 |
session = Session("") |
self.save_modified_session() |
260 |
session.AddMap(Map("")) |
main.app.SetSession(create_empty_session()) |
|
main.app.SetSession(session) |
|
261 |
|
|
262 |
def OpenSession(self): |
def OpenSession(self): |
263 |
|
self.save_modified_session() |
264 |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
265 |
"*.session", wxOPEN) |
"*.session", wxOPEN) |
266 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
268 |
dlg.Destroy() |
dlg.Destroy() |
269 |
|
|
270 |
def SaveSession(self): |
def SaveSession(self): |
271 |
|
if main.app.session.filename == None: |
272 |
|
self.SaveSessionAs() |
273 |
main.app.SaveSession() |
main.app.SaveSession() |
274 |
|
|
275 |
def SaveSessionAs(self): |
def SaveSessionAs(self): |
284 |
self.Close(false) |
self.Close(false) |
285 |
|
|
286 |
def OnClose(self, event): |
def OnClose(self, event): |
287 |
veto = 0 |
result = self.save_modified_session(can_veto = event.CanVeto()) |
288 |
if main.app.session.WasModified(): |
if result == wxID_CANCEL: |
|
flags = wxYES_NO | wxICON_QUESTION |
|
|
if event.CanVeto(): |
|
|
flags = flags | wxCANCEL |
|
|
result = self.RunMessageBox("Exit", |
|
|
("The session has been modified." |
|
|
" Do you want to save it?"), |
|
|
flags) |
|
|
if result == wxID_YES: |
|
|
self.SaveSession() |
|
|
elif result == wxID_CANCEL: |
|
|
veto = 1 |
|
|
|
|
|
if veto: |
|
289 |
event.Veto() |
event.Veto() |
290 |
else: |
else: |
291 |
self.Destroy() |
self.Destroy() |
293 |
def SetMap(self, map): |
def SetMap(self, map): |
294 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
295 |
|
|
296 |
|
def ShowSessionTree(self): |
297 |
|
name = "session_tree" |
298 |
|
dialog = self.get_open_dialog(name) |
299 |
|
if dialog is None: |
300 |
|
dialog = tree.SessionTreeView(self, main.app, name) |
301 |
|
self.add_dialog(name, dialog) |
302 |
|
dialog.Show(true) |
303 |
|
else: |
304 |
|
# FIXME: bring dialog to front here |
305 |
|
pass |
306 |
|
|
307 |
def About(self): |
def About(self): |
308 |
self.RunMessageBox("About", |
self.RunMessageBox("About", |
309 |
("Thuban is a program for\n" |
("Thuban is a program for\n" |
313 |
wxOK | wxICON_INFORMATION) |
wxOK | wxICON_INFORMATION) |
314 |
|
|
315 |
def AddLayer(self): |
def AddLayer(self): |
316 |
dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*", |
dlg = wxFileDialog(self, "Select a data file", ".", "", "*.*", |
317 |
wxOPEN) |
wxOPEN) |
318 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
319 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
354 |
|
|
355 |
If no layer is selected, return None |
If no layer is selected, return None |
356 |
""" |
""" |
357 |
tree = main.app.tree.tree |
return self.interactor.SelectedLayer() |
|
layer = tree.GetPyData(tree.GetSelection()) |
|
|
if isinstance(layer, Layer): |
|
|
return layer |
|
|
return None |
|
358 |
|
|
359 |
def has_selected_layer(self): |
def has_selected_layer(self): |
360 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
361 |
tree = main.app.tree.tree |
return self.interactor.HasSelectedLayer() |
|
layer = tree.GetPyData(tree.GetSelection()) |
|
|
return isinstance(layer, Layer) |
|
362 |
|
|
363 |
def choose_color(self): |
def choose_color(self): |
364 |
"""Run the color selection dialog and return the selected color. |
"""Run the color selection dialog and return the selected color. |
413 |
def LayerShowTable(self): |
def LayerShowTable(self): |
414 |
layer = self.current_layer() |
layer = self.current_layer() |
415 |
if layer is not None: |
if layer is not None: |
416 |
tv = tableview.TableFrame(self, layer.table) |
table = layer.table |
417 |
tv.Show(true) |
name = "table_view" + str(id(table)) |
418 |
|
dialog = self.get_open_dialog(name) |
419 |
|
if dialog is None: |
420 |
|
dialog = tableview.TableFrame(self, self.interactor, name, |
421 |
|
"Table: %s" % layer.Title(), |
422 |
|
layer, table) |
423 |
|
self.add_dialog(name, dialog) |
424 |
|
dialog.Show(true) |
425 |
|
else: |
426 |
|
# FIXME: bring dialog to front here |
427 |
|
pass |
428 |
|
|
429 |
def Projection(self): |
def Projection(self): |
430 |
map = self.canvas.Map() |
map = self.canvas.Map() |
431 |
proj = map.projection |
proj = map.projection |
432 |
if proj is None: |
if proj is None: |
433 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None) |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None, map.BoundingBox()) |
434 |
else: |
else: |
435 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params) |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params, |
436 |
|
map.BoundingBox()) |
437 |
if proj4Dlg.ShowModal() == wxID_OK: |
if proj4Dlg.ShowModal() == wxID_OK: |
438 |
params = proj4Dlg.GetParams() |
params = proj4Dlg.GetParams() |
439 |
if params is not None: |
if params is not None: |
453 |
self.canvas.PanTool() |
self.canvas.PanTool() |
454 |
|
|
455 |
def IdentifyTool(self): |
def IdentifyTool(self): |
|
if self.identify_view is None: |
|
|
self.identify_view = identifyview.IdentifyView(self, main.app) |
|
|
self.identify_view.Show(true) |
|
456 |
self.canvas.IdentifyTool() |
self.canvas.IdentifyTool() |
457 |
|
self.identify_view_on_demand(None, None) |
458 |
|
|
459 |
def LabelTool(self): |
def LabelTool(self): |
460 |
self.canvas.LabelTool() |
self.canvas.LabelTool() |
465 |
def PrintMap(self): |
def PrintMap(self): |
466 |
self.canvas.Print() |
self.canvas.Print() |
467 |
|
|
468 |
|
def identify_view_on_demand(self, layer, shape): |
469 |
|
name = "identify_view" |
470 |
|
if self.canvas.CurrentTool() == "IdentifyTool": |
471 |
|
if not self.dialog_open(name): |
472 |
|
dialog = identifyview.IdentifyView(self, self.interactor, name) |
473 |
|
self.add_dialog(name, dialog) |
474 |
|
dialog.Show(true) |
475 |
|
else: |
476 |
|
# FIXME: bring dialog to front? |
477 |
|
pass |
478 |
|
|
479 |
# |
# |
480 |
# Define all the commands available in the main window |
# Define all the commands available in the main window |
486 |
"""Call the context's method methodname with args *args""" |
"""Call the context's method methodname with args *args""" |
487 |
apply(getattr(context, methodname), args) |
apply(getattr(context, methodname), args) |
488 |
|
|
489 |
def _method_command(name, title, method, helptext = "", sensitive = None): |
def _method_command(name, title, method, helptext = "", |
490 |
|
icon = "", sensitive = None): |
491 |
"""Add a command implemented by a method of the context object""" |
"""Add a command implemented by a method of the context object""" |
492 |
registry.Add(Command(name, title, call_method, args=(method,), |
registry.Add(Command(name, title, call_method, args=(method,), |
493 |
helptext = helptext, sensitive = sensitive)) |
helptext = helptext, icon = icon, |
494 |
|
sensitive = sensitive)) |
495 |
|
|
496 |
def _tool_command(name, title, method, toolname, helptext = "", |
def _tool_command(name, title, method, toolname, helptext = "", |
497 |
icon = ""): |
icon = ""): |
498 |
"""Add a tool command""" |
"""Add a tool command""" |
530 |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
531 |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
532 |
helptext = "Add/Remove labels", icon = "label") |
helptext = "Add/Remove labels", icon = "label") |
533 |
_method_command("map_full_extent", "&Full extent", "FullExtent") |
_method_command("map_full_extent", "&Full extent", "FullExtent", |
534 |
|
helptext = "Full Extent", icon = "fullextent") |
535 |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
536 |
|
|
537 |
# Layer menu |
# Layer menu |
538 |
_method_command("layer_add", "&Add", "AddLayer", |
_method_command("layer_add", "&Add Layer", "AddLayer", |
539 |
helptext = "Add a new layer to active map") |
helptext = "Add a new layer to active map") |
540 |
_method_command("layer_remove", "&Remove", "RemoveLayer", |
_method_command("layer_remove", "&Remove Layer", "RemoveLayer", |
541 |
helptext = "Remove selected layer(s)", |
helptext = "Remove selected layer(s)", |
542 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
543 |
_method_command("layer_fill_color", "&Fill Color", "LayerFillColor", |
_method_command("layer_fill_color", "&Fill Color", "LayerFillColor", |