1 |
# Copyright (C) 2001, 2002, 2003, 2004 by Intevation GmbH |
# Copyright (C) 2001, 2002, 2003, 2004, 2005 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]> |
23 |
import Thuban |
import Thuban |
24 |
|
|
25 |
from Thuban import _ |
from Thuban import _ |
26 |
from Thuban.Model.messages import TITLE_CHANGED |
from Thuban.Model.messages import TITLE_CHANGED, LAYER_PROJECTION_CHANGED, \ |
27 |
|
MAP_PROJECTION_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED |
28 |
|
|
29 |
from Thuban.Model.session import create_empty_session |
from Thuban.Model.session import create_empty_session |
30 |
from Thuban.Model.layer import Layer, RasterLayer |
from Thuban.Model.layer import Layer, RasterLayer |
31 |
from Thuban.Model.postgisdb import PostGISShapeStore, has_postgis_support |
from Thuban.Model.postgisdb import PostGISShapeStore, has_postgis_support |
37 |
import view |
import view |
38 |
import tree |
import tree |
39 |
import tableview, identifyview |
import tableview, identifyview |
|
from Thuban.UI.classifier import Classifier |
|
40 |
import legend |
import legend |
41 |
from menu import Menu |
from menu import Menu |
42 |
|
|
54 |
|
|
55 |
import projdialog |
import projdialog |
56 |
|
|
57 |
|
from Thuban.Lib.classmapper import ClassMapper |
58 |
|
|
59 |
|
layer_properties_dialogs = ClassMapper() |
60 |
|
|
61 |
class MainWindow(DockFrame): |
class MainWindow(DockFrame): |
62 |
|
|
77 |
"SelectedShapes": "canvas", |
"SelectedShapes": "canvas", |
78 |
} |
} |
79 |
|
|
80 |
|
# Messages from the canvas that may require a status bar update. |
81 |
|
# The update_status_bar method will be subscribed to these messages. |
82 |
|
update_status_bar_messages = (VIEW_POSITION, LAYER_PROJECTION_CHANGED, |
83 |
|
MAP_PROJECTION_CHANGED, MAP_LAYERS_ADDED, |
84 |
|
MAP_LAYERS_REMOVED) |
85 |
|
|
86 |
def __init__(self, parent, ID, title, application, interactor, |
def __init__(self, parent, ID, title, application, interactor, |
87 |
initial_message = None, size = wxSize(-1, -1)): |
initial_message = None, size = wxSize(-1, -1)): |
88 |
DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
109 |
|
|
110 |
# Create the map canvas |
# Create the map canvas |
111 |
canvas = view.MapCanvas(self, -1) |
canvas = view.MapCanvas(self, -1) |
|
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
|
112 |
canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand) |
canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand) |
113 |
self.canvas = canvas |
self.canvas = canvas |
114 |
self.canvas.Subscribe(TITLE_CHANGED, self.title_changed) |
self.canvas.Subscribe(TITLE_CHANGED, self.title_changed) |
115 |
|
|
116 |
|
for channel in self.update_status_bar_messages: |
117 |
|
self.canvas.Subscribe(channel, self.update_status_bar) |
118 |
|
|
119 |
self.SetMainWindow(self.canvas) |
self.SetMainWindow(self.canvas) |
120 |
|
|
121 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
344 |
def get_open_dialog(self, name): |
def get_open_dialog(self, name): |
345 |
return self.dialogs.get(name) |
return self.dialogs.get(name) |
346 |
|
|
347 |
def view_position_changed(self): |
def update_status_bar(self, *args): |
348 |
|
"""Handler for a bunch of messages that may require a status bar update |
349 |
|
|
350 |
|
Currently this handles the canvas' VIEW_POSITION_CHANGED |
351 |
|
messages as well as several messages about changes in the map |
352 |
|
which may affect whether and how projections are used. |
353 |
|
|
354 |
|
These messages affect the text in the status bar in the following way: |
355 |
|
|
356 |
|
When VIEW_POSITION_CHANGED messages are sent and the mouse is |
357 |
|
actually in the canvas window, display the current mouse |
358 |
|
coordinates as defined by the canvas' CurrentPosition method. |
359 |
|
|
360 |
|
If there is no current position to show, check whether there is |
361 |
|
a potential problem with the map and layer projections and |
362 |
|
display a message about it. Otherwise the status bar will |
363 |
|
become empty. |
364 |
|
|
365 |
|
The text is displayed in the status bar using the |
366 |
|
set_position_text method. |
367 |
|
""" |
368 |
|
# Implementation note: We do not really have to know which |
369 |
|
# message was sent. We can simply call the canvas' |
370 |
|
# CurrentPosition method and if that returns a tuple, it was a |
371 |
|
# VIEW_POSITION_CHANGED message and we have to display it. |
372 |
|
# Otherwise it was a VIEW_POSITION_CHANGED message where the |
373 |
|
# mouse has left the canvas or it was a message about a change |
374 |
|
# to the map, in which case we check the projections. |
375 |
|
# |
376 |
|
# When changing this method, keep in mind that the |
377 |
|
# VIEW_POSITION_CHANGED message are sent for every mouse move in |
378 |
|
# the canvas window, that is they happen very often, so the path |
379 |
|
# taken in that case has to be fast. |
380 |
|
text = "" |
381 |
pos = self.canvas.CurrentPosition() |
pos = self.canvas.CurrentPosition() |
382 |
if pos is not None: |
if pos is not None: |
383 |
text = "(%10.10g, %10.10g)" % pos |
text = "(%10.10g, %10.10g)" % pos |
384 |
else: |
else: |
385 |
text = "" |
for layer in self.canvas.Map().Layers(): |
386 |
|
bbox = layer.LatLongBoundingBox() |
387 |
|
if bbox: |
388 |
|
left, bottom, right, top = bbox |
389 |
|
if not (-180 <= left <= 180 and |
390 |
|
-180 <= right <= 180 and |
391 |
|
-90 <= top <= 90 and |
392 |
|
-90 <= bottom <= 90): |
393 |
|
text = _("Select layer '%s' and pick a projection " |
394 |
|
"using Layer/Projection...") % layer.title |
395 |
|
break |
396 |
|
|
397 |
self.set_position_text(text) |
self.set_position_text(text) |
398 |
|
|
399 |
def set_position_text(self, text): |
def set_position_text(self, text): |
400 |
"""Set the statusbar text showing the current position. |
"""Set the statusbar text to that created by update_status_bar |
401 |
|
|
402 |
By default the text is shown in field 0 of the status bar. |
By default the text is shown in field 0 of the status bar. |
403 |
Override this method in derived classes to put it into a |
Override this method in derived classes to put it into a |
404 |
different field of the statusbar. |
different field of the statusbar. |
405 |
|
|
406 |
|
For historical reasons this method is called set_position_text |
407 |
|
because at first the text was always either the current position |
408 |
|
or the empty string. Now it can contain other messages as well. |
409 |
|
The method will be renamed at one point. |
410 |
""" |
""" |
411 |
|
# Note: If this method is renamed we should perhaps think about |
412 |
|
# some backwards compatibility measures for projects like |
413 |
|
# GREAT-ER which override this method. |
414 |
self.SetStatusText(text) |
self.SetStatusText(text) |
415 |
|
|
416 |
|
def OpenOrRaiseDialog(self, name, dialog_class, *args, **kw): |
417 |
|
""" |
418 |
|
Open or raise a dialog. |
419 |
|
|
420 |
|
If a dialog with the denoted name does already exist it is |
421 |
|
raised. Otherwise a new dialog, an instance of dialog_class, |
422 |
|
is created, inserted into the main list and displayed. |
423 |
|
""" |
424 |
|
dialog = self.get_open_dialog(name) |
425 |
|
|
426 |
|
if dialog is None: |
427 |
|
dialog = dialog_class(self, name, *args, **kw) |
428 |
|
self.add_dialog(name, dialog) |
429 |
|
dialog.Show(True) |
430 |
|
else: |
431 |
|
dialog.Raise() |
432 |
|
|
433 |
def save_modified_session(self, can_veto = 1): |
def save_modified_session(self, can_veto = 1): |
434 |
"""If the current session has been modified, ask the user |
"""If the current session has been modified, ask the user |
435 |
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 |
502 |
# FIXME: it would be better to tie the unsubscription to |
# FIXME: it would be better to tie the unsubscription to |
503 |
# wx's destroy event, but that isn't implemented for wxGTK |
# wx's destroy event, but that isn't implemented for wxGTK |
504 |
# yet. |
# yet. |
505 |
self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed) |
for channel in self.update_status_bar_messages: |
506 |
|
self.canvas.Unsubscribe(channel, self.update_status_bar) |
507 |
|
|
508 |
DockFrame.OnClose(self, event) |
DockFrame.OnClose(self, event) |
509 |
for dlg in self.dialogs.values(): |
for dlg in self.dialogs.values(): |
510 |
dlg.Destroy() |
dlg.Destroy() |
655 |
return self.canvas.Map().CanRemoveLayer(layer) |
return self.canvas.Map().CanRemoveLayer(layer) |
656 |
return False |
return False |
657 |
|
|
658 |
|
def LayerToTop(self): |
659 |
|
layer = self.current_layer() |
660 |
|
if layer is not None: |
661 |
|
self.canvas.Map().MoveLayerToTop(layer) |
662 |
|
|
663 |
def RaiseLayer(self): |
def RaiseLayer(self): |
664 |
layer = self.current_layer() |
layer = self.current_layer() |
665 |
if layer is not None: |
if layer is not None: |
670 |
if layer is not None: |
if layer is not None: |
671 |
self.canvas.Map().LowerLayer(layer) |
self.canvas.Map().LowerLayer(layer) |
672 |
|
|
673 |
|
def LayerToBottom(self): |
674 |
|
layer = self.current_layer() |
675 |
|
if layer is not None: |
676 |
|
self.canvas.Map().MoveLayerToBottom(layer) |
677 |
|
|
678 |
def current_layer(self): |
def current_layer(self): |
679 |
"""Return the currently selected layer. |
"""Return the currently selected layer. |
680 |
|
|
697 |
def HideLayer(self): |
def HideLayer(self): |
698 |
layer = self.current_layer() |
layer = self.current_layer() |
699 |
if layer is not None: |
if layer is not None: |
700 |
layer.SetVisible(0) |
layer.SetVisible(False) |
701 |
|
|
702 |
def ShowLayer(self): |
def ShowLayer(self): |
703 |
layer = self.current_layer() |
layer = self.current_layer() |
704 |
if layer is not None: |
if layer is not None: |
705 |
layer.SetVisible(1) |
layer.SetVisible(True) |
706 |
|
|
707 |
|
def ToggleLayerVisibility(self): |
708 |
|
layer = self.current_layer() |
709 |
|
layer.SetVisible(not layer.Visible()) |
710 |
|
|
711 |
def DuplicateLayer(self): |
def DuplicateLayer(self): |
712 |
"""Ceate a new layer above the selected layer with the same shapestore |
"""Ceate a new layer above the selected layer with the same shapestore |
717 |
layer.ShapeStore(), |
layer.ShapeStore(), |
718 |
projection = layer.GetProjection()) |
projection = layer.GetProjection()) |
719 |
new_classification = copy.deepcopy(layer.GetClassification()) |
new_classification = copy.deepcopy(layer.GetClassification()) |
720 |
|
new_layer.SetClassificationColumn( |
721 |
|
layer.GetClassificationColumn()) |
722 |
new_layer.SetClassification(new_classification) |
new_layer.SetClassification(new_classification) |
723 |
self.Map().AddLayer(new_layer) |
self.Map().AddLayer(new_layer) |
724 |
|
|
728 |
return layer is not None and hasattr(layer, "ShapeStore") |
return layer is not None and hasattr(layer, "ShapeStore") |
729 |
|
|
730 |
def LayerShowTable(self): |
def LayerShowTable(self): |
731 |
|
""" |
732 |
|
Present a TableView Window for the current layer. |
733 |
|
In case the window is already open, bring it to the front. |
734 |
|
In case, there is no active layer, do nothing. |
735 |
|
In case, the layer has no ShapeStore, do nothing. |
736 |
|
""" |
737 |
layer = self.current_layer() |
layer = self.current_layer() |
738 |
if layer is not None: |
if layer is not None: |
739 |
|
if not hasattr(layer, "ShapeStore"): |
740 |
|
return |
741 |
table = layer.ShapeStore().Table() |
table = layer.ShapeStore().Table() |
742 |
name = "table_view" + str(id(table)) |
name = "table_view" + str(id(table)) |
743 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
748 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
749 |
dialog.Show(True) |
dialog.Show(True) |
750 |
else: |
else: |
751 |
# FIXME: bring dialog to front here |
dialog.Raise() |
|
pass |
|
752 |
|
|
753 |
def MapProjection(self): |
def MapProjection(self): |
754 |
|
|
790 |
self.OpenLayerProperties(layer) |
self.OpenLayerProperties(layer) |
791 |
|
|
792 |
def OpenLayerProperties(self, layer, group = None): |
def OpenLayerProperties(self, layer, group = None): |
793 |
name = "layer_properties" + str(id(layer)) |
""" |
794 |
dialog = self.get_open_dialog(name) |
Open or raise the properties dialog. |
795 |
|
|
796 |
if dialog is None: |
This method opens or raises the properties dialog for the |
797 |
dialog = Classifier(self, name, self.Map(), layer, group) |
currently selected layer if one is defined for this layer |
798 |
self.add_dialog(name, dialog) |
type. |
799 |
dialog.Show() |
""" |
800 |
dialog.Raise() |
dialog_class = layer_properties_dialogs.get(layer) |
801 |
|
|
802 |
|
if dialog_class is not None: |
803 |
|
name = "layer_properties" + str(id(layer)) |
804 |
|
self.OpenOrRaiseDialog(name, dialog_class, layer, group = group) |
805 |
|
|
806 |
def LayerJoinTable(self): |
def LayerJoinTable(self): |
807 |
layer = self.canvas.SelectedLayer() |
layer = self.canvas.SelectedLayer() |
1097 |
"""Return true if a layer is selected in the context""" |
"""Return true if a layer is selected in the context""" |
1098 |
return context.mainwindow.has_selected_layer() |
return context.mainwindow.has_selected_layer() |
1099 |
|
|
1100 |
|
def _has_selected_layer_visible(context): |
1101 |
|
"""Return true if a layer is selected in the context which is |
1102 |
|
visible.""" |
1103 |
|
if context.mainwindow.has_selected_layer(): |
1104 |
|
layer = context.mainwindow.current_layer() |
1105 |
|
if layer.Visible(): return True |
1106 |
|
return False |
1107 |
|
|
1108 |
def _has_selected_shape_layer(context): |
def _has_selected_shape_layer(context): |
1109 |
"""Return true if a shape layer is selected in the context""" |
"""Return true if a shape layer is selected in the context""" |
1110 |
return context.mainwindow.has_selected_shape_layer() |
return context.mainwindow.has_selected_shape_layer() |
1128 |
if map is not None: |
if map is not None: |
1129 |
for layer in map.Layers(): |
for layer in map.Layers(): |
1130 |
if layer.Visible(): |
if layer.Visible(): |
1131 |
return 1 |
return True |
1132 |
return 0 |
return False |
1133 |
|
|
1134 |
def _has_legend_shown(context): |
def _has_legend_shown(context): |
1135 |
"""Return true if the legend window is shown""" |
"""Return true if the legend window is shown""" |
1253 |
sensitive = _has_selected_shape_layer, |
sensitive = _has_selected_shape_layer, |
1254 |
helptext = _("Join and attach a table to the selected layer")) |
helptext = _("Join and attach a table to the selected layer")) |
1255 |
|
|
1256 |
|
# further layer methods: |
1257 |
|
_method_command("layer_to_top", _("&Top"), "LayerToTop", |
1258 |
|
helptext = _("Put selected layer to the top"), |
1259 |
|
sensitive = _has_selected_layer) |
1260 |
|
_method_command("layer_to_bottom", _("&Bottom"), "LayerToBottom", |
1261 |
|
helptext = _("Put selected layer to the bottom"), |
1262 |
|
sensitive = _has_selected_layer) |
1263 |
|
_method_command("layer_visibility", _("&Visible"), "ToggleLayerVisibility", |
1264 |
|
checked = _has_selected_layer_visible, |
1265 |
|
helptext = _("Toggle visibility of selected layer"), |
1266 |
|
sensitive = _has_selected_layer) |
1267 |
|
|
1268 |
def _can_unjoin(context): |
def _can_unjoin(context): |
1269 |
"""Return whether the Layer/Unjoin command can be executed. |
"""Return whether the Layer/Unjoin command can be executed. |
1270 |
|
|