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]> |
12 |
|
|
13 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
14 |
|
|
15 |
import sys, os |
import os |
16 |
|
|
17 |
from wxPython.wx import * |
from wxPython.wx import * |
18 |
|
|
19 |
import Thuban |
import Thuban |
20 |
from Thuban.Model.session import Session, create_empty_session |
from Thuban.Model.session import create_empty_session |
|
from Thuban.Model.map import Map |
|
21 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
22 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
23 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
26 |
import tree |
import tree |
27 |
import proj4dialog |
import proj4dialog |
28 |
import tableview, identifyview |
import tableview, identifyview |
29 |
|
from menu import Menu |
30 |
|
|
31 |
import main |
import main |
32 |
from command import registry, Command |
from command import registry, Command |
33 |
from messages import SELECTED_SHAPE |
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 |
54 |
|
|
55 |
self.init_ids() |
self.init_ids() |
56 |
|
|
57 |
menuBar = wxMenuBar() |
# creat the menubar from the main_menu description |
58 |
|
self.SetMenuBar(self.build_menu_bar(main_menu)) |
59 |
|
|
60 |
menu = wxMenu() |
# Similarly, create the toolbar from main_toolbar |
61 |
menuBar.Append(menu, "&File"); |
toolbar = self.build_toolbar(main_toolbar) |
|
for name in ["new_session", "open_session", None, |
|
|
"save_session", "save_session_as", None, |
|
|
"exit"]: |
|
|
self.add_menu_command(menu, name) |
|
|
|
|
|
menu = wxMenu() |
|
|
menuBar.Append(menu, "&Map"); |
|
|
for name in ["layer_add", "layer_remove", |
|
|
None, |
|
|
"map_projection", |
|
|
None, |
|
|
"map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
|
|
"map_identify_tool", "map_label_tool", |
|
|
None, |
|
|
"map_full_extent", |
|
|
None, |
|
|
"map_print"]: |
|
|
self.add_menu_command(menu, name) |
|
|
|
|
|
menu = wxMenu() |
|
|
menuBar.Append(menu, "&Layer"); |
|
|
for name in ["layer_fill_color", "layer_transparent_fill", |
|
|
"layer_ourline_color", "layer_no_outline", |
|
|
None, |
|
|
"layer_raise", "layer_lower", |
|
|
None, |
|
|
"layer_show", "layer_hide", |
|
|
None, |
|
|
"layer_show_table"]: |
|
|
self.add_menu_command(menu, name) |
|
|
|
|
|
menu = wxMenu() |
|
|
menuBar.Append(menu, "&Help"); |
|
|
self.add_menu_command(menu, "help_about") |
|
|
|
|
|
self.SetMenuBar(menuBar) |
|
|
|
|
|
# toolbar |
|
|
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
|
|
|
|
|
# set the size of the tools' bitmaps. Not needed on wxGTK, but |
|
|
# on Windows. We probably shouldn't hardwire the bitmap size |
|
|
# here |
|
|
toolbar.SetToolBitmapSize(wxSize(24, 24)) |
|
|
|
|
|
for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
|
|
"map_identify_tool", "map_label_tool"]: |
|
|
self.add_toolbar_command(toolbar, name) |
|
62 |
# call Realize to make sure that the tools appear. |
# call Realize to make sure that the tools appear. |
63 |
toolbar.Realize() |
toolbar.Realize() |
64 |
|
|
65 |
# Create the map canvas |
# Create the map canvas |
66 |
canvas = view.MapCanvas(self, -1, interactor) |
canvas = view.MapCanvas(self, -1, interactor) |
67 |
|
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
68 |
self.canvas = canvas |
self.canvas = canvas |
69 |
|
|
70 |
self.init_dialogs() |
self.init_dialogs() |
78 |
self.current_id = 6000 |
self.current_id = 6000 |
79 |
self.id_to_name = {} |
self.id_to_name = {} |
80 |
self.name_to_id = {} |
self.name_to_id = {} |
81 |
|
self.events_bound = {} |
82 |
|
|
83 |
def get_id(self, name): |
def get_id(self, name): |
84 |
"""Return the wxWindows id for the command named name. |
"""Return the wxWindows id for the command named name. |
91 |
self.name_to_id[name] = ID |
self.name_to_id[name] = ID |
92 |
self.id_to_name[ID] = name |
self.id_to_name[ID] = name |
93 |
return ID |
return ID |
94 |
|
|
95 |
|
def bind_command_events(self, command, ID): |
96 |
|
"""Bind the necessary events for the given command and ID""" |
97 |
|
if not self.events_bound.has_key(ID): |
98 |
|
# the events haven't been bound yet |
99 |
|
EVT_MENU(self, ID, self.invoke_command) |
100 |
|
if command.IsDynamic(): |
101 |
|
EVT_UPDATE_UI(self, ID, self.update_command_ui) |
102 |
|
|
103 |
|
def build_menu_bar(self, menudesc): |
104 |
|
"""Build and return the menu bar from the menu description""" |
105 |
|
menu_bar = wxMenuBar() |
106 |
|
|
107 |
|
for item in menudesc.items: |
108 |
|
# here the items must all be Menu instances themselves |
109 |
|
menu_bar.Append(self.build_menu(item), item.title) |
110 |
|
|
111 |
|
return menu_bar |
112 |
|
|
113 |
|
def build_menu(self, menudesc): |
114 |
|
"""Build and return a wxMenu from a menudescription""" |
115 |
|
wxmenu = wxMenu() |
116 |
|
last = None |
117 |
|
for item in menudesc.items: |
118 |
|
# here the items must all be Menu instances themselves |
119 |
|
if item is None: |
120 |
|
# a separator. Only add one if the last item was not a |
121 |
|
# separator |
122 |
|
if last is not None: |
123 |
|
wxmenu.AppendSeparator() |
124 |
|
elif isinstance(item, Menu): |
125 |
|
# a submenu |
126 |
|
wxmenu.AppendMenu(wxNewId(), item.title, self.build_menu(item)) |
127 |
|
else: |
128 |
|
# must the name the name of a command |
129 |
|
self.add_menu_command(wxmenu, item) |
130 |
|
last = item |
131 |
|
return wxmenu |
132 |
|
|
133 |
|
def build_toolbar(self, toolbardesc): |
134 |
|
"""Build and return the main toolbar window from a toolbar description |
135 |
|
|
136 |
|
The parameter should be an instance of the Menu class but it |
137 |
|
should not contain submenus. |
138 |
|
""" |
139 |
|
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
140 |
|
|
141 |
|
# set the size of the tools' bitmaps. Not needed on wxGTK, but |
142 |
|
# on Windows, although it doesn't work very well there. It seems |
143 |
|
# that only 16x16 icons are really supported on windows. |
144 |
|
# We probably shouldn't hardwire the bitmap size here. |
145 |
|
toolbar.SetToolBitmapSize(wxSize(24, 24)) |
146 |
|
|
147 |
|
for item in toolbardesc.items: |
148 |
|
if item is None: |
149 |
|
toolbar.AddSeparator() |
150 |
|
else: |
151 |
|
# assume it's a string. |
152 |
|
self.add_toolbar_command(toolbar, item) |
153 |
|
|
154 |
|
return toolbar |
155 |
|
|
156 |
def add_menu_command(self, menu, name): |
def add_menu_command(self, menu, name): |
157 |
"""Add the command with name name to the menu menu. |
"""Add the command with name name to the menu menu. |
158 |
|
|
166 |
ID = self.get_id(name) |
ID = self.get_id(name) |
167 |
menu.Append(ID, command.Title(), command.HelpText(), |
menu.Append(ID, command.Title(), command.HelpText(), |
168 |
command.IsCheckCommand()) |
command.IsCheckCommand()) |
169 |
EVT_MENU(self, ID, self.invoke_command) |
self.bind_command_events(command, ID) |
|
if command.IsDynamic(): |
|
|
EVT_UPDATE_UI(self, ID, self.update_command_ui) |
|
170 |
else: |
else: |
171 |
print "Unknown command %s" % name |
print "Unknown command %s" % name |
172 |
|
|
188 |
toolbar.AddTool(ID, bitmap, |
toolbar.AddTool(ID, bitmap, |
189 |
shortHelpString = command.HelpText(), |
shortHelpString = command.HelpText(), |
190 |
isToggle = command.IsCheckCommand()) |
isToggle = command.IsCheckCommand()) |
191 |
|
self.bind_command_events(command, ID) |
192 |
else: |
else: |
193 |
print "Unknown command %s" % name |
print "Unknown command %s" % name |
194 |
|
|
210 |
event.Check(command.Checked(self)) |
event.Check(command.Checked(self)) |
211 |
|
|
212 |
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
213 |
"""Run a modla message box with the given text, title and flags |
"""Run a modal message box with the given text, title and flags |
214 |
and return the result""" |
and return the result""" |
215 |
dlg = wxMessageDialog(self, text, title, flags) |
dlg = wxMessageDialog(self, text, title, flags) |
216 |
result = dlg.ShowModal() |
result = dlg.ShowModal() |
238 |
def get_open_dialog(self, name): |
def get_open_dialog(self, name): |
239 |
return self.dialogs.get(name) |
return self.dialogs.get(name) |
240 |
|
|
241 |
|
def view_position_changed(self): |
242 |
|
pos = self.canvas.CurrentPosition() |
243 |
|
if pos is not None: |
244 |
|
text = "(%10.10g, %10.10g)" % pos |
245 |
|
else: |
246 |
|
text = "" |
247 |
|
self.SetStatusText(text) |
248 |
|
|
249 |
def save_modified_session(self, can_veto = 1): |
def save_modified_session(self, can_veto = 1): |
250 |
"""If the current session has been modified, ask the user |
"""If the current session has been modified, ask the user |
251 |
whether to save it and do so if requested. Return the outcome of |
whether to save it and do so if requested. Return the outcome of |
276 |
def OpenSession(self): |
def OpenSession(self): |
277 |
self.save_modified_session() |
self.save_modified_session() |
278 |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
279 |
"*.session", wxOPEN) |
"*.thuban", wxOPEN) |
280 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
281 |
main.app.OpenSession(dlg.GetPath()) |
main.app.OpenSession(dlg.GetPath()) |
282 |
dlg.Destroy() |
dlg.Destroy() |
288 |
|
|
289 |
def SaveSessionAs(self): |
def SaveSessionAs(self): |
290 |
dlg = wxFileDialog(self, "Enter a filename for session", ".", "", |
dlg = wxFileDialog(self, "Enter a filename for session", ".", "", |
291 |
"*.session", wxOPEN) |
"*.thuban", wxOPEN) |
292 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
293 |
main.app.session.SetFilename(dlg.GetPath()) |
main.app.session.SetFilename(dlg.GetPath()) |
294 |
main.app.SaveSession() |
main.app.SaveSession() |
327 |
wxOK | wxICON_INFORMATION) |
wxOK | wxICON_INFORMATION) |
328 |
|
|
329 |
def AddLayer(self): |
def AddLayer(self): |
330 |
dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*", |
dlg = wxFileDialog(self, "Select a data file", ".", "", "*.*", |
331 |
wxOPEN) |
wxOPEN) |
332 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
333 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
444 |
map = self.canvas.Map() |
map = self.canvas.Map() |
445 |
proj = map.projection |
proj = map.projection |
446 |
if proj is None: |
if proj is None: |
447 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None) |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None, map.BoundingBox()) |
448 |
else: |
else: |
449 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params) |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params, |
450 |
|
map.BoundingBox()) |
451 |
if proj4Dlg.ShowModal() == wxID_OK: |
if proj4Dlg.ShowModal() == wxID_OK: |
452 |
params = proj4Dlg.GetParams() |
params = proj4Dlg.GetParams() |
453 |
if params is not None: |
if params is not None: |
500 |
"""Call the context's method methodname with args *args""" |
"""Call the context's method methodname with args *args""" |
501 |
apply(getattr(context, methodname), args) |
apply(getattr(context, methodname), args) |
502 |
|
|
503 |
def _method_command(name, title, method, helptext = "", sensitive = None): |
def _method_command(name, title, method, helptext = "", |
504 |
|
icon = "", sensitive = None): |
505 |
"""Add a command implemented by a method of the context object""" |
"""Add a command implemented by a method of the context object""" |
506 |
registry.Add(Command(name, title, call_method, args=(method,), |
registry.Add(Command(name, title, call_method, args=(method,), |
507 |
helptext = helptext, sensitive = sensitive)) |
helptext = helptext, icon = icon, |
508 |
|
sensitive = sensitive)) |
509 |
|
|
510 |
def _tool_command(name, title, method, toolname, helptext = "", |
def _tool_command(name, title, method, toolname, helptext = "", |
511 |
icon = ""): |
icon = ""): |
512 |
"""Add a tool command""" |
"""Add a tool command""" |
525 |
_method_command("open_session", "&Open Session", "OpenSession") |
_method_command("open_session", "&Open Session", "OpenSession") |
526 |
_method_command("save_session", "&Save Session", "SaveSession") |
_method_command("save_session", "&Save Session", "SaveSession") |
527 |
_method_command("save_session_as", "Save Session &As", "SaveSessionAs") |
_method_command("save_session_as", "Save Session &As", "SaveSessionAs") |
528 |
|
_method_command("show_session_tree", "Show Session &Tree", "ShowSessionTree") |
529 |
_method_command("exit", "&Exit", "Exit") |
_method_command("exit", "&Exit", "Exit") |
530 |
|
|
531 |
# Help menu |
# Help menu |
545 |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
546 |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
547 |
helptext = "Add/Remove labels", icon = "label") |
helptext = "Add/Remove labels", icon = "label") |
548 |
_method_command("map_full_extent", "&Full extent", "FullExtent") |
_method_command("map_full_extent", "&Full extent", "FullExtent", |
549 |
|
helptext = "Full Extent", icon = "fullextent") |
550 |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
551 |
|
|
552 |
# Layer menu |
# Layer menu |
562 |
"LayerTransparentFill", |
"LayerTransparentFill", |
563 |
helptext = "Do not fill the selected layer(s)", |
helptext = "Do not fill the selected layer(s)", |
564 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
565 |
_method_command("layer_ourline_color", "&Outline Color", "LayerOutlineColor", |
_method_command("layer_outline_color", "&Outline Color", "LayerOutlineColor", |
566 |
helptext = "Set the outline color of selected layer(s)", |
helptext = "Set the outline color of selected layer(s)", |
567 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
568 |
_method_command("layer_no_outline", "&No Outline", "LayerNoOutline", |
_method_command("layer_no_outline", "&No Outline", "LayerNoOutline", |
583 |
_method_command("layer_show_table", "Show Ta&ble", "LayerShowTable", |
_method_command("layer_show_table", "Show Ta&ble", "LayerShowTable", |
584 |
helptext = "Show the selected layer's table", |
helptext = "Show the selected layer's table", |
585 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
586 |
|
|
587 |
|
|
588 |
|
# the menu structure |
589 |
|
main_menu = Menu("<main>", "<main>", |
590 |
|
[Menu("file", "&File", |
591 |
|
["new_session", "open_session", None, |
592 |
|
"save_session", "save_session_as", None, |
593 |
|
"show_session_tree", None, |
594 |
|
"exit"]), |
595 |
|
Menu("map", "&Map", |
596 |
|
["layer_add", "layer_remove", |
597 |
|
None, |
598 |
|
"map_projection", |
599 |
|
None, |
600 |
|
"map_zoom_in_tool", "map_zoom_out_tool", |
601 |
|
"map_pan_tool", "map_identify_tool", "map_label_tool", |
602 |
|
None, |
603 |
|
"map_full_extent", |
604 |
|
None, |
605 |
|
"map_print"]), |
606 |
|
Menu("layer", "&Layer", |
607 |
|
["layer_fill_color", "layer_transparent_fill", |
608 |
|
"layer_outline_color", "layer_no_outline", |
609 |
|
None, |
610 |
|
"layer_raise", "layer_lower", |
611 |
|
None, |
612 |
|
"layer_show", "layer_hide", |
613 |
|
None, |
614 |
|
"layer_show_table"]), |
615 |
|
Menu("help", "&Help", |
616 |
|
["help_about"])]) |
617 |
|
|
618 |
|
# the main toolbar |
619 |
|
|
620 |
|
main_toolbar = Menu("<toolbar>", "<toolbar>", |
621 |
|
["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
622 |
|
"map_identify_tool", "map_label_tool", "map_full_extent"]) |