1 |
# Copyright (C) 2001, 2002 by Intevation GmbH |
# Copyright (C) 2001, 2002, 2003 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 |
|
__ThubanVersion__ = "0.2" #"$THUBAN_0_2$" |
16 |
|
#__BuildDate__ = "$Date$" |
17 |
|
|
18 |
import os |
import os |
19 |
|
|
20 |
from wxPython.wx import * |
from wxPython.wx import * |
30 |
import tree |
import tree |
31 |
import proj4dialog |
import proj4dialog |
32 |
import tableview, identifyview |
import tableview, identifyview |
33 |
import classifier |
from Thuban.UI.classifier import Classifier |
34 |
|
import legend |
35 |
from menu import Menu |
from menu import Menu |
36 |
|
|
37 |
from context import Context |
from context import Context |
38 |
from command import registry, Command, ToolCommand |
from command import registry, Command, ToolCommand |
39 |
from messages import SELECTED_SHAPE, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, DOCKABLE_DOCKED, DOCKABLE_UNDOCKED, DOCKABLE_CLOSED |
40 |
|
|
41 |
|
from Thuban.UI.dock import DockableWindow, DockFrame, DockPanel |
42 |
|
|
43 |
|
|
44 |
# the directory where the toolbar icons are stored |
# the directory where the toolbar icons are stored |
45 |
bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps") |
bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps") |
46 |
bitmapext = ".xpm" |
bitmapext = ".xpm" |
47 |
|
|
48 |
|
ID_WINDOW_LEGEND = 4001 |
49 |
|
ID_WINDOW_CANVAS = 4002 |
50 |
|
|
51 |
|
|
52 |
|
class MainWindow(DockFrame): |
53 |
|
|
54 |
class MainWindow(wxFrame): |
# Some messages that can be subscribed/unsubscribed directly through |
55 |
|
# the MapCanvas come in fact from other objects. This is a map to |
56 |
|
# map those messages to the names of the instance variables they |
57 |
|
# actually come from. This delegation is implemented in the |
58 |
|
# Subscribe and unsubscribed methods |
59 |
|
delegated_messages = {LAYER_SELECTED: "canvas", |
60 |
|
SHAPES_SELECTED: "canvas"} |
61 |
|
|
62 |
|
# Methods delegated to some instance variables. The delegation is |
63 |
|
# implemented in the __getattr__ method. |
64 |
|
delegated_methods = {"SelectLayer": "canvas", |
65 |
|
"SelectShapes": "canvas", |
66 |
|
} |
67 |
|
|
68 |
def __init__(self, parent, ID, title, application, interactor, |
def __init__(self, parent, ID, title, application, interactor, |
69 |
initial_message = None, size = wxSize(-1, -1)): |
initial_message = None, size = wxSize(-1, -1)): |
70 |
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
71 |
|
#wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
72 |
|
|
73 |
self.application = application |
self.application = application |
|
self.interactor = interactor |
|
74 |
|
|
75 |
self.CreateStatusBar() |
self.CreateStatusBar() |
76 |
if initial_message: |
if initial_message: |
88 |
# call Realize to make sure that the tools appear. |
# call Realize to make sure that the tools appear. |
89 |
toolbar.Realize() |
toolbar.Realize() |
90 |
|
|
91 |
|
|
92 |
# Create the map canvas |
# Create the map canvas |
93 |
canvas = view.MapCanvas(self, -1, interactor) |
canvas = view.MapCanvas(self, -1) |
94 |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
95 |
|
canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand) |
96 |
self.canvas = canvas |
self.canvas = canvas |
97 |
|
|
98 |
|
self.SetMainWindow(self.canvas) |
99 |
|
|
100 |
|
self.SetAutoLayout(True) |
101 |
|
|
102 |
self.init_dialogs() |
self.init_dialogs() |
103 |
|
|
104 |
interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand) |
EVT_CLOSE(self, self._OnClose) |
105 |
|
|
106 |
EVT_CLOSE(self, self.OnClose) |
def Subscribe(self, channel, *args): |
107 |
|
"""Subscribe a function to a message channel. |
108 |
|
|
109 |
|
If channel is one of the delegated messages call the appropriate |
110 |
|
object's Subscribe method. Otherwise do nothing. |
111 |
|
""" |
112 |
|
if channel in self.delegated_messages: |
113 |
|
object = getattr(self, self.delegated_messages[channel]) |
114 |
|
object.Subscribe(channel, *args) |
115 |
|
else: |
116 |
|
print "Trying to subscribe to unsupported channel %s" % channel |
117 |
|
|
118 |
|
def Unsubscribe(self, channel, *args): |
119 |
|
"""Unsubscribe a function from a message channel. |
120 |
|
|
121 |
|
If channel is one of the delegated messages call the appropriate |
122 |
|
object's Unsubscribe method. Otherwise do nothing. |
123 |
|
""" |
124 |
|
if channel in self.delegated_messages: |
125 |
|
object = getattr(self, self.delegated_messages[channel]) |
126 |
|
object.Unsubscribe(channel, *args) |
127 |
|
|
128 |
|
def __getattr__(self, attr): |
129 |
|
"""If attr is one of the delegated methods return that method |
130 |
|
|
131 |
|
Otherwise raise AttributeError. |
132 |
|
""" |
133 |
|
if attr in self.delegated_methods: |
134 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
135 |
|
raise AttributeError(attr) |
136 |
|
|
137 |
def init_ids(self): |
def init_ids(self): |
138 |
"""Initialize the ids""" |
"""Initialize the ids""" |
387 |
dlg.Destroy() |
dlg.Destroy() |
388 |
|
|
389 |
def Exit(self): |
def Exit(self): |
390 |
self.Close(false) |
self.Close(False) |
391 |
|
|
392 |
def OnClose(self, event): |
def _OnClose(self, event): |
393 |
result = self.save_modified_session(can_veto = event.CanVeto()) |
result = self.save_modified_session(can_veto = event.CanVeto()) |
394 |
if result == wxID_CANCEL: |
if result == wxID_CANCEL: |
395 |
event.Veto() |
event.Veto() |
398 |
# wx's destroy event, but that isn't implemented for wxGTK |
# wx's destroy event, but that isn't implemented for wxGTK |
399 |
# yet. |
# yet. |
400 |
self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed) |
self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed) |
401 |
|
DockFrame._OnClose(self, event) |
402 |
self.Destroy() |
self.Destroy() |
403 |
|
|
404 |
def SetMap(self, map): |
def SetMap(self, map): |
405 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
406 |
|
self.SetTitle("Thuban - " + map.Title()) |
407 |
|
#self.legendPanel.SetMap(map) |
408 |
|
|
409 |
def Map(self): |
def Map(self): |
410 |
"""Return the map displayed by this mainwindow""" |
"""Return the map displayed by this mainwindow""" |
411 |
|
|
412 |
|
# sanity check |
413 |
|
#assert(self.canvas.Map() is self.legendPanel.GetMap()) |
414 |
|
|
415 |
return self.canvas.Map() |
return self.canvas.Map() |
416 |
|
|
417 |
def ShowSessionTree(self): |
def ToggleSessionTree(self): |
418 |
|
"""If the session tree is shown close it otherwise create a new tree""" |
419 |
name = "session_tree" |
name = "session_tree" |
420 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
421 |
if dialog is None: |
if dialog is None: |
422 |
dialog = tree.SessionTreeView(self, self.application, name) |
dialog = tree.SessionTreeView(self, self.application, name) |
423 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
424 |
dialog.Show(true) |
dialog.Show(True) |
425 |
else: |
else: |
426 |
# FIXME: bring dialog to front here |
dialog.Close() |
427 |
pass |
|
428 |
|
def SessionTreeShown(self): |
429 |
|
"""Return true iff the session tree is currently shown""" |
430 |
|
return self.get_open_dialog("session_tree") is not None |
431 |
|
|
432 |
def About(self): |
def About(self): |
433 |
self.RunMessageBox(_("About"), |
self.RunMessageBox(_("About"), |
434 |
_("Thuban is a program for\n" |
_("Thuban v%s\n" |
435 |
|
#"Build Date: %s\n" |
436 |
|
"\n" |
437 |
|
"Thuban is a program for\n" |
438 |
"exploring geographic data.\n" |
"exploring geographic data.\n" |
439 |
"Copyright (C) 2001-2003 Intevation GmbH.\n" |
"Copyright (C) 2001-2003 Intevation GmbH.\n" |
440 |
"Thuban is licensed under the GPL"), |
"Thuban is licensed under the GNU GPL" |
441 |
|
% __ThubanVersion__), #__BuildDate__)), |
442 |
wxOK | wxICON_INFORMATION) |
wxOK | wxICON_INFORMATION) |
443 |
|
|
444 |
def AddLayer(self): |
def AddLayer(self): |
458 |
_("Can't open the file '%s'.") % filename) |
_("Can't open the file '%s'.") % filename) |
459 |
else: |
else: |
460 |
if not has_layers: |
if not has_layers: |
461 |
# if we're adding a layer to an empty map, for the |
# if we're adding a layer to an empty map, fit the |
462 |
# new map to the window |
# new map to the window |
463 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
464 |
dlg.Destroy() |
dlg.Destroy() |
471 |
def CanRemoveLayer(self): |
def CanRemoveLayer(self): |
472 |
"""Return true if the currently selected layer can be deleted. |
"""Return true if the currently selected layer can be deleted. |
473 |
|
|
474 |
If no layer is selected return false. |
If no layer is selected return False. |
475 |
|
|
476 |
The return value of this method determines whether the remove |
The return value of this method determines whether the remove |
477 |
layer command is sensitive in menu. |
layer command is sensitive in menu. |
479 |
layer = self.current_layer() |
layer = self.current_layer() |
480 |
if layer is not None: |
if layer is not None: |
481 |
return self.canvas.Map().CanRemoveLayer(layer) |
return self.canvas.Map().CanRemoveLayer(layer) |
482 |
return 0 |
return False |
483 |
|
|
484 |
def RaiseLayer(self): |
def RaiseLayer(self): |
485 |
layer = self.current_layer() |
layer = self.current_layer() |
496 |
|
|
497 |
If no layer is selected, return None |
If no layer is selected, return None |
498 |
""" |
""" |
499 |
return self.interactor.SelectedLayer() |
return self.canvas.SelectedLayer() |
500 |
|
|
501 |
def has_selected_layer(self): |
def has_selected_layer(self): |
502 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
503 |
return self.interactor.HasSelectedLayer() |
return self.canvas.HasSelectedLayer() |
504 |
|
|
505 |
def choose_color(self): |
def choose_color(self): |
506 |
"""Run the color selection dialog and return the selected color. |
"""Run the color selection dialog and return the selected color. |
518 |
dlg.Destroy() |
dlg.Destroy() |
519 |
return color |
return color |
520 |
|
|
|
def LayerFillColor(self): |
|
|
layer = self.current_layer() |
|
|
if layer is not None: |
|
|
color = self.choose_color() |
|
|
if color is not None: |
|
|
layer.GetClassification().SetDefaultFill(color) |
|
|
|
|
|
def LayerTransparentFill(self): |
|
|
layer = self.current_layer() |
|
|
if layer is not None: |
|
|
layer.GetClassification().SetDefaultFill(Color.None) |
|
|
|
|
|
def LayerOutlineColor(self): |
|
|
layer = self.current_layer() |
|
|
if layer is not None: |
|
|
color = self.choose_color() |
|
|
if color is not None: |
|
|
layer.GetClassification().SetDefaultLineColor(color) |
|
|
|
|
|
def LayerNoOutline(self): |
|
|
layer = self.current_layer() |
|
|
if layer is not None: |
|
|
layer.GetClassification().SetDefaultLineColor(Color.None) |
|
|
|
|
521 |
def HideLayer(self): |
def HideLayer(self): |
522 |
layer = self.current_layer() |
layer = self.current_layer() |
523 |
if layer is not None: |
if layer is not None: |
535 |
name = "table_view" + str(id(table)) |
name = "table_view" + str(id(table)) |
536 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
537 |
if dialog is None: |
if dialog is None: |
538 |
dialog = tableview.LayerTableFrame(self, self.interactor, name, |
dialog = tableview.LayerTableFrame(self, name, |
539 |
_("Table: %s") % layer.Title(), |
_("Table: %s") % layer.Title(), |
540 |
layer, table) |
layer, table) |
541 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
542 |
dialog.Show(true) |
dialog.Show(true) |
561 |
map.SetProjection(proj) |
map.SetProjection(proj) |
562 |
proj4Dlg.Destroy() |
proj4Dlg.Destroy() |
563 |
|
|
564 |
def Classify(self): |
def LayerEditProperties(self): |
565 |
|
|
566 |
# |
# |
567 |
# the menu option for this should only be available if there |
# the menu option for this should only be available if there |
570 |
# |
# |
571 |
|
|
572 |
layer = self.current_layer() |
layer = self.current_layer() |
573 |
name = "classifier" + str(id(layer)) |
self.OpenLayerProperties(layer) |
574 |
|
|
575 |
|
def OpenLayerProperties(self, layer, group = None): |
576 |
|
name = "layer_properties" + str(id(layer)) |
577 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
578 |
|
|
579 |
if dialog is None: |
if dialog is None: |
580 |
dialog = classifier.Classifier(self, self.interactor, |
dialog = Classifier(self, name, layer, group) |
|
name, self.current_layer()) |
|
581 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
582 |
dialog.Show() |
dialog.Show() |
583 |
|
dialog.Raise() |
584 |
|
|
585 |
|
|
586 |
|
def ShowLegend(self): |
587 |
|
if not self.LegendShown(): |
588 |
|
self.ToggleLegend() |
589 |
|
|
590 |
|
def ToggleLegend(self): |
591 |
|
"""Show the legend if it's not shown otherwise hide it again""" |
592 |
|
name = "legend" |
593 |
|
dialog = self.FindRegisteredDock(name) |
594 |
|
|
595 |
|
if dialog is None: |
596 |
|
dialog = self.CreateDock(name, -1, _("Legend"), wxLAYOUT_LEFT) |
597 |
|
legend.LegendPanel(dialog, None, self) |
598 |
|
dialog.Dock() |
599 |
|
dialog.GetPanel().SetMap(self.Map()) |
600 |
|
dialog.Show() |
601 |
|
else: |
602 |
|
dialog.Show(not dialog.IsShown()) |
603 |
|
|
604 |
|
def LegendShown(self): |
605 |
|
"""Return true iff the legend is currently open""" |
606 |
|
dialog = self.FindRegisteredDock("legend") |
607 |
|
return dialog is not None and dialog.IsShown() |
608 |
|
|
609 |
def ZoomInTool(self): |
def ZoomInTool(self): |
610 |
self.canvas.ZoomInTool() |
self.canvas.ZoomInTool() |
628 |
def PrintMap(self): |
def PrintMap(self): |
629 |
self.canvas.Print() |
self.canvas.Print() |
630 |
|
|
631 |
def identify_view_on_demand(self, layer, shape): |
def identify_view_on_demand(self, layer, shapes): |
632 |
name = "identify_view" |
name = "identify_view" |
633 |
if self.canvas.CurrentTool() == "IdentifyTool": |
if self.canvas.CurrentTool() == "IdentifyTool": |
634 |
if not self.dialog_open(name): |
if not self.dialog_open(name): |
635 |
dialog = identifyview.IdentifyView(self, self.interactor, name) |
dialog = identifyview.IdentifyView(self, name) |
636 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
637 |
dialog.Show(true) |
dialog.Show(True) |
638 |
else: |
else: |
639 |
# FIXME: bring dialog to front? |
# FIXME: bring dialog to front? |
640 |
pass |
pass |
650 |
apply(getattr(context.mainwindow, methodname), args) |
apply(getattr(context.mainwindow, methodname), args) |
651 |
|
|
652 |
def _method_command(name, title, method, helptext = "", |
def _method_command(name, title, method, helptext = "", |
653 |
icon = "", sensitive = None): |
icon = "", sensitive = None, checked = None): |
654 |
"""Add a command implemented by a method of the mainwindow object""" |
"""Add a command implemented by a method of the mainwindow object""" |
655 |
registry.Add(Command(name, title, call_method, args=(method,), |
registry.Add(Command(name, title, call_method, args=(method,), |
656 |
helptext = helptext, icon = icon, |
helptext = helptext, icon = icon, |
657 |
sensitive = sensitive)) |
sensitive = sensitive, checked = checked)) |
658 |
|
|
659 |
def make_check_current_tool(toolname): |
def make_check_current_tool(toolname): |
660 |
"""Return a function that tests if the currently active tool is toolname |
"""Return a function that tests if the currently active tool is toolname |
684 |
|
|
685 |
def _has_tree_window_shown(context): |
def _has_tree_window_shown(context): |
686 |
"""Return true if the tree window is shown""" |
"""Return true if the tree window is shown""" |
687 |
return context.mainwindow.get_open_dialog("session_tree") is None |
return context.mainwindow.SessionTreeShown() |
688 |
|
|
689 |
def _has_visible_map(context): |
def _has_visible_map(context): |
690 |
"""Return true iff theres a visible map in the mainwindow. |
"""Return true iff theres a visible map in the mainwindow. |
697 |
return 1 |
return 1 |
698 |
return 0 |
return 0 |
699 |
|
|
700 |
|
def _has_legend_shown(context): |
701 |
|
"""Return true if the legend window is shown""" |
702 |
|
return context.mainwindow.LegendShown() |
703 |
|
|
704 |
|
|
705 |
# File menu |
# File menu |
706 |
_method_command("new_session", _("&New Session"), "NewSession") |
_method_command("new_session", _("&New Session"), "NewSession") |
707 |
_method_command("open_session", _("&Open Session"), "OpenSession") |
_method_command("open_session", _("&Open Session"), "OpenSession") |
708 |
_method_command("save_session", _("&Save Session"), "SaveSession") |
_method_command("save_session", _("&Save Session"), "SaveSession") |
709 |
_method_command("save_session_as", _("Save Session &As"), "SaveSessionAs") |
_method_command("save_session_as", _("Save Session &As"), "SaveSessionAs") |
710 |
_method_command("show_session_tree", _("Show Session &Tree"), "ShowSessionTree", |
_method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree", |
711 |
sensitive = _has_tree_window_shown) |
checked = _has_tree_window_shown) |
712 |
|
_method_command("toggle_legend", _("Legend"), "ToggleLegend", |
713 |
|
checked = _has_legend_shown) |
714 |
_method_command("exit", _("E&xit"), "Exit") |
_method_command("exit", _("E&xit"), "Exit") |
715 |
|
|
716 |
# Help menu |
# Help menu |
748 |
_method_command("layer_remove", _("&Remove Layer"), "RemoveLayer", |
_method_command("layer_remove", _("&Remove Layer"), "RemoveLayer", |
749 |
helptext = _("Remove selected layer(s)"), |
helptext = _("Remove selected layer(s)"), |
750 |
sensitive = _can_remove_layer) |
sensitive = _can_remove_layer) |
|
_method_command("layer_fill_color", _("&Fill Color"), "LayerFillColor", |
|
|
helptext = _("Set the fill color of selected layer(s)"), |
|
|
sensitive = _has_selected_layer) |
|
|
_method_command("layer_transparent_fill", _("&Transparent Fill"), |
|
|
"LayerTransparentFill", |
|
|
helptext = _("Do not fill the selected layer(s)"), |
|
|
sensitive = _has_selected_layer) |
|
|
_method_command("layer_outline_color", _("&Outline Color"), "LayerOutlineColor", |
|
|
helptext = _("Set the outline color of selected layer(s)"), |
|
|
sensitive = _has_selected_layer) |
|
|
_method_command("layer_no_outline", _("&No Outline"), "LayerNoOutline", |
|
|
helptext= _("Do not draw the outline of the selected layer(s)"), |
|
|
sensitive = _has_selected_layer) |
|
751 |
_method_command("layer_raise", _("&Raise"), "RaiseLayer", |
_method_command("layer_raise", _("&Raise"), "RaiseLayer", |
752 |
helptext = _("Raise selected layer(s)"), |
helptext = _("Raise selected layer(s)"), |
753 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
763 |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
764 |
helptext = _("Show the selected layer's table"), |
helptext = _("Show the selected layer's table"), |
765 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
766 |
|
_method_command("layer_properties", _("Properties"), "LayerEditProperties", |
|
_method_command("layer_classifier", _("Classify"), "Classify", |
|
767 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
768 |
|
|
769 |
# the menu structure |
# the menu structure |
771 |
[Menu("file", _("&File"), |
[Menu("file", _("&File"), |
772 |
["new_session", "open_session", None, |
["new_session", "open_session", None, |
773 |
"save_session", "save_session_as", None, |
"save_session", "save_session_as", None, |
774 |
"show_session_tree", None, |
"toggle_session_tree", None, |
775 |
"exit"]), |
"exit"]), |
776 |
Menu("map", _("&Map"), |
Menu("map", _("&Map"), |
777 |
["layer_add", "layer_remove", |
["layer_add", "layer_remove", |
783 |
None, |
None, |
784 |
"map_full_extent", |
"map_full_extent", |
785 |
None, |
None, |
786 |
|
"toggle_legend", |
787 |
|
None, |
788 |
"map_print"]), |
"map_print"]), |
789 |
Menu("layer", _("&Layer"), |
Menu("layer", _("&Layer"), |
790 |
["layer_fill_color", "layer_transparent_fill", |
["layer_raise", "layer_lower", |
|
"layer_outline_color", "layer_no_outline", |
|
|
None, |
|
|
"layer_raise", "layer_lower", |
|
791 |
None, |
None, |
792 |
"layer_show", "layer_hide", |
"layer_show", "layer_hide", |
793 |
None, |
None, |
794 |
"layer_show_table", |
"layer_show_table", |
795 |
None, |
None, |
796 |
"layer_classifier"]), |
"layer_properties"]), |
797 |
Menu("help", _("&Help"), |
Menu("help", _("&Help"), |
798 |
["help_about"])]) |
["help_about"])]) |
799 |
|
|