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]> |
31 |
import proj4dialog |
import proj4dialog |
32 |
import tableview, identifyview |
import tableview, identifyview |
33 |
import 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 |
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(wxFrame): |
class MainWindow(wxFrame): |
53 |
|
|
54 |
|
# 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) |
wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
71 |
|
|
72 |
self.application = application |
self.application = application |
|
self.interactor = interactor |
|
73 |
|
|
74 |
self.CreateStatusBar() |
self.CreateStatusBar() |
75 |
if initial_message: |
if initial_message: |
87 |
# call Realize to make sure that the tools appear. |
# call Realize to make sure that the tools appear. |
88 |
toolbar.Realize() |
toolbar.Realize() |
89 |
|
|
90 |
|
win = wxSashLayoutWindow(self, ID_WINDOW_LEGEND, |
91 |
|
style=wxNO_BORDER|wxSW_3D) |
92 |
|
win.SetOrientation(wxLAYOUT_VERTICAL) |
93 |
|
win.SetAlignment(wxLAYOUT_LEFT) |
94 |
|
win.SetSashVisible(wxSASH_RIGHT, True) |
95 |
|
win.SetSashBorder(wxSASH_RIGHT, True) |
96 |
|
win.Hide() |
97 |
|
self.sash_legend = win |
98 |
|
|
99 |
|
|
100 |
# Create the map canvas |
# Create the map canvas |
101 |
canvas = view.MapCanvas(self, -1, interactor) |
canvas = view.MapCanvas(self, -1) |
102 |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
103 |
|
canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand) |
104 |
self.canvas = canvas |
self.canvas = canvas |
105 |
|
|
106 |
|
self.SetAutoLayout(True) |
107 |
|
|
108 |
self.init_dialogs() |
self.init_dialogs() |
109 |
|
|
110 |
interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand) |
self.legendPanel = None |
111 |
|
self.legendWindow = None |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
EVT_CLOSE(self, self.OnClose) |
EVT_CLOSE(self, self.OnClose) |
116 |
|
EVT_SASH_DRAGGED_RANGE(self, |
117 |
|
ID_WINDOW_LEGEND, ID_WINDOW_CANVAS, |
118 |
|
self._OnSashDrag) |
119 |
|
EVT_SIZE(self, self._OnSize) |
120 |
|
|
121 |
|
def Subscribe(self, channel, *args): |
122 |
|
"""Subscribe a function to a message channel. |
123 |
|
|
124 |
|
If channel is one of the delegated messages call the appropriate |
125 |
|
object's Subscribe method. Otherwise do nothing. |
126 |
|
""" |
127 |
|
if channel in self.delegated_messages: |
128 |
|
object = getattr(self, self.delegated_messages[channel]) |
129 |
|
object.Subscribe(channel, *args) |
130 |
|
else: |
131 |
|
print "Trying to subscribe to unsupported channel %s" % channel |
132 |
|
|
133 |
|
def Unsubscribe(self, channel, *args): |
134 |
|
"""Unsubscribe a function from a message channel. |
135 |
|
|
136 |
|
If channel is one of the delegated messages call the appropriate |
137 |
|
object's Unsubscribe method. Otherwise do nothing. |
138 |
|
""" |
139 |
|
if channel in self.delegated_messages: |
140 |
|
object = getattr(self, self.delegated_messages[channel]) |
141 |
|
object.Unsubscribe(channel, *args) |
142 |
|
|
143 |
|
def __getattr__(self, attr): |
144 |
|
"""If attr is one of the delegated methods return that method |
145 |
|
|
146 |
|
Otherwise raise AttributeError. |
147 |
|
""" |
148 |
|
if attr in self.delegated_methods: |
149 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
150 |
|
raise AttributeError(attr) |
151 |
|
|
152 |
def init_ids(self): |
def init_ids(self): |
153 |
"""Initialize the ids""" |
"""Initialize the ids""" |
417 |
|
|
418 |
def SetMap(self, map): |
def SetMap(self, map): |
419 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
420 |
|
#self.legendPanel.SetMap(map) |
421 |
|
|
422 |
def Map(self): |
def Map(self): |
423 |
"""Return the map displayed by this mainwindow""" |
"""Return the map displayed by this mainwindow""" |
424 |
|
|
425 |
|
# sanity check |
426 |
|
#assert(self.canvas.Map() is self.legendPanel.GetMap()) |
427 |
|
|
428 |
return self.canvas.Map() |
return self.canvas.Map() |
429 |
|
|
430 |
def ShowSessionTree(self): |
def ShowSessionTree(self): |
468 |
_("Can't open the file '%s'.") % filename) |
_("Can't open the file '%s'.") % filename) |
469 |
else: |
else: |
470 |
if not has_layers: |
if not has_layers: |
471 |
# if we're adding a layer to an empty map, for the |
# if we're adding a layer to an empty map, fit the |
472 |
# new map to the window |
# new map to the window |
473 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
474 |
dlg.Destroy() |
dlg.Destroy() |
506 |
|
|
507 |
If no layer is selected, return None |
If no layer is selected, return None |
508 |
""" |
""" |
509 |
return self.interactor.SelectedLayer() |
return self.canvas.SelectedLayer() |
510 |
|
|
511 |
def has_selected_layer(self): |
def has_selected_layer(self): |
512 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
513 |
return self.interactor.HasSelectedLayer() |
return self.canvas.HasSelectedLayer() |
514 |
|
|
515 |
def choose_color(self): |
def choose_color(self): |
516 |
"""Run the color selection dialog and return the selected color. |
"""Run the color selection dialog and return the selected color. |
569 |
name = "table_view" + str(id(table)) |
name = "table_view" + str(id(table)) |
570 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
571 |
if dialog is None: |
if dialog is None: |
572 |
dialog = tableview.LayerTableFrame(self, self.interactor, name, |
dialog = tableview.LayerTableFrame(self, name, |
573 |
_("Table: %s") % layer.Title(), |
_("Table: %s") % layer.Title(), |
574 |
layer, table) |
layer, table) |
575 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
576 |
dialog.Show(true) |
dialog.Show(true) |
604 |
# |
# |
605 |
|
|
606 |
layer = self.current_layer() |
layer = self.current_layer() |
607 |
|
self.OpenClassifier(layer) |
608 |
|
|
609 |
|
def OpenClassifier(self, layer): |
610 |
name = "classifier" + str(id(layer)) |
name = "classifier" + str(id(layer)) |
611 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
612 |
|
|
613 |
if dialog is None: |
if dialog is None: |
614 |
dialog = classifier.Classifier(self, self.interactor, |
dialog = classifier.Classifier(self, name, layer) |
|
name, self.current_layer()) |
|
615 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
616 |
dialog.Show() |
dialog.Show() |
617 |
|
|
618 |
|
|
619 |
|
def ShowLegend(self, switch = False): |
620 |
|
|
621 |
|
name = "legend" |
622 |
|
dialog = self.get_open_dialog(name) |
623 |
|
|
624 |
|
if dialog is None: |
625 |
|
self.legendPanel = \ |
626 |
|
legend.LegendPanel(self.sash_legend, None, self) |
627 |
|
dialog = DockableWindow(self, -1, name, |
628 |
|
"Legend: %s" % self.Map().Title(), |
629 |
|
self.sash_legend, self.legendPanel) |
630 |
|
|
631 |
|
|
632 |
|
self.add_dialog(name, dialog) |
633 |
|
|
634 |
|
dialog.Subscribe(DOCKABLE_DOCKED, self._OnLegendDock) |
635 |
|
dialog.Subscribe(DOCKABLE_UNDOCKED, self._OnLegendUnDock) |
636 |
|
dialog.Subscribe(DOCKABLE_CLOSED, self._OnLegendClosed) |
637 |
|
|
638 |
|
self.legendWindow = dialog |
639 |
|
self.legendPanel.SetMap(self.Map()) |
640 |
|
|
641 |
|
dialog.Show() |
642 |
|
|
643 |
def ZoomInTool(self): |
def ZoomInTool(self): |
644 |
self.canvas.ZoomInTool() |
self.canvas.ZoomInTool() |
645 |
|
|
662 |
def PrintMap(self): |
def PrintMap(self): |
663 |
self.canvas.Print() |
self.canvas.Print() |
664 |
|
|
665 |
def identify_view_on_demand(self, layer, shape): |
def identify_view_on_demand(self, layer, shapes): |
666 |
name = "identify_view" |
name = "identify_view" |
667 |
if self.canvas.CurrentTool() == "IdentifyTool": |
if self.canvas.CurrentTool() == "IdentifyTool": |
668 |
if not self.dialog_open(name): |
if not self.dialog_open(name): |
669 |
dialog = identifyview.IdentifyView(self, self.interactor, name) |
dialog = identifyview.IdentifyView(self, name) |
670 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
671 |
dialog.Show(true) |
dialog.Show(True) |
672 |
else: |
else: |
673 |
# FIXME: bring dialog to front? |
# FIXME: bring dialog to front? |
674 |
pass |
pass |
675 |
|
|
676 |
|
|
677 |
|
def _OnSashDrag(self, event): |
678 |
|
|
679 |
|
if event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE: |
680 |
|
return |
681 |
|
|
682 |
|
id = event.GetId() |
683 |
|
|
684 |
|
rect = event.GetDragRect() |
685 |
|
|
686 |
|
if id == ID_WINDOW_LEGEND: |
687 |
|
#assert(self.legendPanel.IsDocked()) |
688 |
|
assert(self.legendWindow.IsDocked()) |
689 |
|
#assert(self.legendPanel.GetParent() is self.sash_legend) |
690 |
|
self.__SetLegendDockSize(rect) |
691 |
|
|
692 |
|
wxLayoutAlgorithm().LayoutWindow(self, self.canvas) |
693 |
|
|
694 |
|
def _OnSize(self, event): |
695 |
|
wxLayoutAlgorithm().LayoutWindow(self, self.canvas) |
696 |
|
|
697 |
|
def _OnLegendDock(self, id, win): |
698 |
|
if not self.sash_legend.IsShown(): |
699 |
|
self.sash_legend.Show() |
700 |
|
self.__SetLegendDockSize(None) |
701 |
|
wxLayoutAlgorithm().LayoutWindow(self, self.canvas) |
702 |
|
|
703 |
|
def _OnLegendUnDock(self, id, win): |
704 |
|
if self.sash_legend.IsShown(): |
705 |
|
self.sash_legend.Hide() |
706 |
|
wxLayoutAlgorithm().LayoutWindow(self, self.canvas) |
707 |
|
|
708 |
|
def _OnLegendClosed(self, id, win): |
709 |
|
if self.sash_legend.IsShown(): |
710 |
|
self.sash_legend.Hide() |
711 |
|
wxLayoutAlgorithm().LayoutWindow(self, self.canvas) |
712 |
|
|
713 |
|
def __SetLegendDockSize(self, rect): |
714 |
|
|
715 |
|
w, h = self.legendWindow.GetBestSize() |
716 |
|
#w, h = self.legendPanel.GetBestSize() |
717 |
|
|
718 |
|
if rect is not None: |
719 |
|
rw = rect.width |
720 |
|
rh = rect.height |
721 |
|
if rw < w: rw = w |
722 |
|
else: |
723 |
|
rw = w |
724 |
|
rh = h |
725 |
|
|
726 |
|
rw += 2 # XXX: without this the sash isn't visible!?!?!?! |
727 |
|
|
728 |
|
self.sash_legend.SetDefaultSize(wxSize(rw, 1000)) |
729 |
|
|
730 |
# |
# |
731 |
# Define all the commands available in the main window |
# Define all the commands available in the main window |
732 |
# |
# |
785 |
return 1 |
return 1 |
786 |
return 0 |
return 0 |
787 |
|
|
788 |
|
def _has_legend_shown(context): |
789 |
|
"""Return true if the legend window is shown""" |
790 |
|
return context.mainwindow.get_open_dialog("legend") is None |
791 |
|
|
792 |
|
|
793 |
# File menu |
# File menu |
794 |
_method_command("new_session", _("&New Session"), "NewSession") |
_method_command("new_session", _("&New Session"), "NewSession") |
862 |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
863 |
helptext = _("Show the selected layer's table"), |
helptext = _("Show the selected layer's table"), |
864 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
|
|
|
865 |
_method_command("layer_classifier", _("Classify"), "Classify", |
_method_command("layer_classifier", _("Classify"), "Classify", |
866 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
867 |
|
_method_command("show_legend", _("Legend"), "ShowLegend", |
868 |
|
sensitive = _has_legend_shown) |
869 |
|
|
870 |
# the menu structure |
# the menu structure |
871 |
main_menu = Menu("<main>", "<main>", |
main_menu = Menu("<main>", "<main>", |
873 |
["new_session", "open_session", None, |
["new_session", "open_session", None, |
874 |
"save_session", "save_session_as", None, |
"save_session", "save_session_as", None, |
875 |
"show_session_tree", None, |
"show_session_tree", None, |
876 |
|
"show_legend", None, |
877 |
"exit"]), |
"exit"]), |
878 |
Menu("map", _("&Map"), |
Menu("map", _("&Map"), |
879 |
["layer_add", "layer_remove", |
["layer_add", "layer_remove", |