1 |
# Copyright (C) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (C) 2001, 2002, 2003, 2004 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]> |
35 |
import view |
import view |
36 |
import tree |
import tree |
37 |
import tableview, identifyview |
import tableview, identifyview |
|
from Thuban.UI.classifier import Classifier |
|
38 |
import legend |
import legend |
39 |
from menu import Menu |
from menu import Menu |
40 |
|
|
52 |
|
|
53 |
import projdialog |
import projdialog |
54 |
|
|
55 |
|
from Thuban.Lib.classmapper import ClassMapper |
56 |
|
|
57 |
|
layer_properties_dialogs = ClassMapper() |
58 |
|
|
59 |
class MainWindow(DockFrame): |
class MainWindow(DockFrame): |
60 |
|
|
136 |
""" |
""" |
137 |
if channel in self.delegated_messages: |
if channel in self.delegated_messages: |
138 |
object = getattr(self, self.delegated_messages[channel]) |
object = getattr(self, self.delegated_messages[channel]) |
139 |
object.Unsubscribe(channel, *args) |
try: |
140 |
|
object.Unsubscribe(channel, *args) |
141 |
|
except wxPyDeadObjectError: |
142 |
|
# The object was a wxObject and has already been |
143 |
|
# destroyed. Hopefully it has unsubscribed all its |
144 |
|
# subscribers already so that it's OK if we do nothing |
145 |
|
# here |
146 |
|
pass |
147 |
|
|
148 |
def __getattr__(self, attr): |
def __getattr__(self, attr): |
149 |
"""If attr is one of the delegated methods return that method |
"""If attr is one of the delegated methods return that method |
351 |
""" |
""" |
352 |
self.SetStatusText(text) |
self.SetStatusText(text) |
353 |
|
|
354 |
|
def OpenOrRaiseDialog(self, name, dialog_class, *args, **kw): |
355 |
|
""" |
356 |
|
Open or raise a dialog. |
357 |
|
|
358 |
|
If a dialog with the denoted name does already exist it is |
359 |
|
raised. Otherwise a new dialog, an instance of dialog_class, |
360 |
|
is created, inserted into the main list and displayed. |
361 |
|
""" |
362 |
|
dialog = self.get_open_dialog(name) |
363 |
|
|
364 |
|
if dialog is None: |
365 |
|
dialog = dialog_class(self, name, *args, **kw) |
366 |
|
self.add_dialog(name, dialog) |
367 |
|
dialog.Show(True) |
368 |
|
else: |
369 |
|
dialog.Raise() |
370 |
|
|
371 |
def save_modified_session(self, can_veto = 1): |
def save_modified_session(self, can_veto = 1): |
372 |
"""If the current session has been modified, ask the user |
"""If the current session has been modified, ask the user |
373 |
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 |
397 |
|
|
398 |
def OpenSession(self): |
def OpenSession(self): |
399 |
if self.save_modified_session() != wxID_CANCEL: |
if self.save_modified_session() != wxID_CANCEL: |
400 |
dlg = wxFileDialog(self, _("Open Session"), ".", "", |
dlg = wxFileDialog(self, _("Open Session"), |
401 |
|
self.application.Path("data"), "", |
402 |
"Thuban Session File (*.thuban)|*.thuban", |
"Thuban Session File (*.thuban)|*.thuban", |
403 |
wxOPEN) |
wxOPEN) |
404 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
405 |
self.application.OpenSession(dlg.GetPath(), |
self.application.OpenSession(dlg.GetPath(), |
406 |
self.run_db_param_dialog) |
self.run_db_param_dialog) |
407 |
|
self.application.SetPath("data", dlg.GetPath()) |
408 |
dlg.Destroy() |
dlg.Destroy() |
409 |
|
|
410 |
def run_db_param_dialog(self, parameters, message): |
def run_db_param_dialog(self, parameters, message): |
419 |
self.application.SaveSession() |
self.application.SaveSession() |
420 |
|
|
421 |
def SaveSessionAs(self): |
def SaveSessionAs(self): |
422 |
dlg = wxFileDialog(self, _("Save Session As"), ".", "", |
dlg = wxFileDialog(self, _("Save Session As"), |
423 |
|
self.application.Path("data"), "", |
424 |
"Thuban Session File (*.thuban)|*.thuban", |
"Thuban Session File (*.thuban)|*.thuban", |
425 |
wxSAVE|wxOVERWRITE_PROMPT) |
wxSAVE|wxOVERWRITE_PROMPT) |
426 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
427 |
self.application.session.SetFilename(dlg.GetPath()) |
self.application.session.SetFilename(dlg.GetPath()) |
428 |
self.application.SaveSession() |
self.application.SaveSession() |
429 |
|
self.application.SetPath("data",dlg.GetPath()) |
430 |
dlg.Destroy() |
dlg.Destroy() |
431 |
|
|
432 |
def Exit(self): |
def Exit(self): |
491 |
dialog.Raise() |
dialog.Raise() |
492 |
|
|
493 |
def AddLayer(self): |
def AddLayer(self): |
494 |
dlg = wxFileDialog(self, _("Select one or more data files"), ".", "", |
dlg = wxFileDialog(self, _("Select one or more data files"), |
495 |
_("Shapefiles (*.shp)") + "|*.shp|" + |
self.application.Path("data"), "", |
496 |
_("All Files (*.*)") + "|*.*", |
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
497 |
|
_("All Files (*.*)") + "|*.*", |
498 |
wxOPEN | wxMULTIPLE) |
wxOPEN | wxMULTIPLE) |
499 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
500 |
filenames = dlg.GetPaths() |
filenames = dlg.GetPaths() |
515 |
# if we're adding a layer to an empty map, fit the |
# if we're adding a layer to an empty map, fit the |
516 |
# new map to the window |
# new map to the window |
517 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
518 |
|
self.application.SetPath("data",filename) |
519 |
dlg.Destroy() |
dlg.Destroy() |
520 |
|
|
521 |
def AddRasterLayer(self): |
def AddRasterLayer(self): |
522 |
dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*", |
dlg = wxFileDialog(self, _("Select an image file"), |
523 |
|
self.application.Path("data"), "", "*.*", |
524 |
wxOPEN) |
wxOPEN) |
525 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
526 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
539 |
# if we're adding a layer to an empty map, fit the |
# if we're adding a layer to an empty map, fit the |
540 |
# new map to the window |
# new map to the window |
541 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
542 |
|
self.application.SetPath("data", filename) |
543 |
dlg.Destroy() |
dlg.Destroy() |
544 |
|
|
545 |
def AddDBLayer(self): |
def AddDBLayer(self): |
548 |
dlg = ChooseDBTableDialog(self, self.application.Session()) |
dlg = ChooseDBTableDialog(self, self.application.Session()) |
549 |
|
|
550 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
551 |
dbconn, dbtable = dlg.GetTable() |
dbconn, dbtable, id_column, geo_column = dlg.GetTable() |
552 |
try: |
try: |
553 |
title = str(dbtable) |
title = str(dbtable) |
554 |
|
|
555 |
# Chose the correct Interface for the database type |
# Chose the correct Interface for the database type |
556 |
store = PostGISShapeStore(dbconn, dbtable) |
store = session.OpenDBShapeStore(dbconn, dbtable, |
557 |
session.AddShapeStore(store) |
id_column = id_column, |
558 |
|
geometry_column = geo_column) |
559 |
layer = Layer(title, store) |
layer = Layer(title, store) |
560 |
except: |
except: |
561 |
# Some error occured while initializing the layer |
# Some error occured while initializing the layer |
562 |
self.RunMessageBox(_("Add Layer from database"), |
self.RunMessageBox(_("Add Layer from database"), |
563 |
_("Can't open the database table '%s'") |
_("Can't open the database table '%s'") |
564 |
% dbtable) |
% dbtable) |
565 |
|
return |
566 |
|
|
567 |
map = self.canvas.Map() |
map = self.canvas.Map() |
568 |
|
|
591 |
return self.canvas.Map().CanRemoveLayer(layer) |
return self.canvas.Map().CanRemoveLayer(layer) |
592 |
return False |
return False |
593 |
|
|
594 |
|
def LayerToTop(self): |
595 |
|
layer = self.current_layer() |
596 |
|
if layer is not None: |
597 |
|
self.canvas.Map().MoveLayerToTop(layer) |
598 |
|
|
599 |
def RaiseLayer(self): |
def RaiseLayer(self): |
600 |
layer = self.current_layer() |
layer = self.current_layer() |
601 |
if layer is not None: |
if layer is not None: |
606 |
if layer is not None: |
if layer is not None: |
607 |
self.canvas.Map().LowerLayer(layer) |
self.canvas.Map().LowerLayer(layer) |
608 |
|
|
609 |
|
def LayerToBottom(self): |
610 |
|
layer = self.current_layer() |
611 |
|
if layer is not None: |
612 |
|
self.canvas.Map().MoveLayerToBottom(layer) |
613 |
|
|
614 |
def current_layer(self): |
def current_layer(self): |
615 |
"""Return the currently selected layer. |
"""Return the currently selected layer. |
616 |
|
|
622 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
623 |
return self.canvas.HasSelectedLayer() |
return self.canvas.HasSelectedLayer() |
624 |
|
|
625 |
|
def has_selected_shape_layer(self): |
626 |
|
"""Return true if a shape layer is currently selected""" |
627 |
|
return isinstance(self.current_layer(), Layer) |
628 |
|
|
629 |
def has_selected_shapes(self): |
def has_selected_shapes(self): |
630 |
"""Return true if a shape is currently selected""" |
"""Return true if a shape is currently selected""" |
631 |
return self.canvas.HasSelectedShapes() |
return self.canvas.HasSelectedShapes() |
633 |
def HideLayer(self): |
def HideLayer(self): |
634 |
layer = self.current_layer() |
layer = self.current_layer() |
635 |
if layer is not None: |
if layer is not None: |
636 |
layer.SetVisible(0) |
layer.SetVisible(False) |
637 |
|
|
638 |
def ShowLayer(self): |
def ShowLayer(self): |
639 |
layer = self.current_layer() |
layer = self.current_layer() |
640 |
if layer is not None: |
if layer is not None: |
641 |
layer.SetVisible(1) |
layer.SetVisible(True) |
642 |
|
|
643 |
|
def ToggleLayerVisibility(self): |
644 |
|
layer = self.current_layer() |
645 |
|
layer.SetVisible(not layer.Visible()) |
646 |
|
|
647 |
def DuplicateLayer(self): |
def DuplicateLayer(self): |
648 |
"""Ceate a new layer above the selected layer with the same shapestore |
"""Ceate a new layer above the selected layer with the same shapestore |
662 |
return layer is not None and hasattr(layer, "ShapeStore") |
return layer is not None and hasattr(layer, "ShapeStore") |
663 |
|
|
664 |
def LayerShowTable(self): |
def LayerShowTable(self): |
665 |
|
""" |
666 |
|
Present a TableView Window for the current layer. |
667 |
|
In case the window is already open, bring it to the front. |
668 |
|
In case, there is no active layer, do nothing. |
669 |
|
In case, the layer has no ShapeStore, do nothing. |
670 |
|
""" |
671 |
layer = self.current_layer() |
layer = self.current_layer() |
672 |
if layer is not None: |
if layer is not None: |
673 |
|
if not hasattr(layer, "ShapeStore"): |
674 |
|
return |
675 |
table = layer.ShapeStore().Table() |
table = layer.ShapeStore().Table() |
676 |
name = "table_view" + str(id(table)) |
name = "table_view" + str(id(table)) |
677 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
682 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
683 |
dialog.Show(True) |
dialog.Show(True) |
684 |
else: |
else: |
685 |
# FIXME: bring dialog to front here |
dialog.Raise() |
|
pass |
|
686 |
|
|
687 |
def MapProjection(self): |
def MapProjection(self): |
688 |
|
|
724 |
self.OpenLayerProperties(layer) |
self.OpenLayerProperties(layer) |
725 |
|
|
726 |
def OpenLayerProperties(self, layer, group = None): |
def OpenLayerProperties(self, layer, group = None): |
727 |
name = "layer_properties" + str(id(layer)) |
""" |
728 |
dialog = self.get_open_dialog(name) |
Open or raise the properties dialog. |
729 |
|
|
730 |
if dialog is None: |
This method opens or raises the properties dialog for the |
731 |
dialog = Classifier(self, name, self.Map(), layer, group) |
currently selected layer if one is defined for this layer |
732 |
self.add_dialog(name, dialog) |
type. |
733 |
dialog.Show() |
""" |
734 |
dialog.Raise() |
dialog_class = layer_properties_dialogs.get(layer) |
735 |
|
|
736 |
|
if dialog_class is not None: |
737 |
|
name = "layer_properties" + str(id(layer)) |
738 |
|
self.OpenOrRaiseDialog(name, dialog_class, layer, group = group) |
739 |
|
|
740 |
def LayerJoinTable(self): |
def LayerJoinTable(self): |
741 |
layer = self.canvas.SelectedLayer() |
layer = self.canvas.SelectedLayer() |
776 |
return dialog is not None and dialog.IsShown() |
return dialog is not None and dialog.IsShown() |
777 |
|
|
778 |
def TableOpen(self): |
def TableOpen(self): |
779 |
dlg = wxFileDialog(self, _("Open Table"), ".", "", |
dlg = wxFileDialog(self, _("Open Table"), |
780 |
|
self.application.Path("data"), "", |
781 |
_("DBF Files (*.dbf)") + "|*.dbf|" + |
_("DBF Files (*.dbf)") + "|*.dbf|" + |
782 |
#_("CSV Files (*.csv)") + "|*.csv|" + |
#_("CSV Files (*.csv)") + "|*.csv|" + |
783 |
_("All Files (*.*)") + "|*.*", |
_("All Files (*.*)") + "|*.*", |
793 |
_("Can't open the file '%s'.") % filename) |
_("Can't open the file '%s'.") % filename) |
794 |
else: |
else: |
795 |
self.ShowTableView(table) |
self.ShowTableView(table) |
796 |
|
self.application.SetPath("data",filename) |
797 |
|
|
798 |
def TableClose(self): |
def TableClose(self): |
799 |
tables = self.application.session.UnreferencedTables() |
tables = self.application.session.UnreferencedTables() |
870 |
|
|
871 |
# Second, let the user rename the layers |
# Second, let the user rename the layers |
872 |
for table in to_rename: |
for table in to_rename: |
873 |
dlg = wxTextEntryDialog(self, "Table Title: ", "Rename Table", |
dlg = wxTextEntryDialog(self, _("Table Title:"), _("Rename Table"), |
874 |
table.Title()) |
table.Title()) |
875 |
try: |
try: |
876 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
919 |
self.canvas.Print() |
self.canvas.Print() |
920 |
|
|
921 |
def RenameMap(self): |
def RenameMap(self): |
922 |
dlg = wxTextEntryDialog(self, "Map Title: ", "Rename Map", |
dlg = wxTextEntryDialog(self, _("Map Title:"), _("Rename Map"), |
923 |
self.Map().Title()) |
self.Map().Title()) |
924 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
925 |
title = dlg.GetValue() |
title = dlg.GetValue() |
932 |
"""Let the user rename the currently selected layer""" |
"""Let the user rename the currently selected layer""" |
933 |
layer = self.current_layer() |
layer = self.current_layer() |
934 |
if layer is not None: |
if layer is not None: |
935 |
dlg = wxTextEntryDialog(self, "Layer Title: ", "Rename Layer", |
dlg = wxTextEntryDialog(self, _("Layer Title:"), _("Rename Layer"), |
936 |
layer.Title()) |
layer.Title()) |
937 |
try: |
try: |
938 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
1031 |
"""Return true if a layer is selected in the context""" |
"""Return true if a layer is selected in the context""" |
1032 |
return context.mainwindow.has_selected_layer() |
return context.mainwindow.has_selected_layer() |
1033 |
|
|
1034 |
|
def _has_selected_layer_visible(context): |
1035 |
|
"""Return true if a layer is selected in the context which is |
1036 |
|
visible.""" |
1037 |
|
if context.mainwindow.has_selected_layer(): |
1038 |
|
layer = context.mainwindow.current_layer() |
1039 |
|
if layer.Visible(): return True |
1040 |
|
return False |
1041 |
|
|
1042 |
|
def _has_selected_shape_layer(context): |
1043 |
|
"""Return true if a shape layer is selected in the context""" |
1044 |
|
return context.mainwindow.has_selected_shape_layer() |
1045 |
|
|
1046 |
def _has_selected_shapes(context): |
def _has_selected_shapes(context): |
1047 |
"""Return true if a layer is selected in the context""" |
"""Return true if a layer is selected in the context""" |
1048 |
return context.mainwindow.has_selected_shapes() |
return context.mainwindow.has_selected_shapes() |
1062 |
if map is not None: |
if map is not None: |
1063 |
for layer in map.Layers(): |
for layer in map.Layers(): |
1064 |
if layer.Visible(): |
if layer.Visible(): |
1065 |
return 1 |
return True |
1066 |
return 0 |
return False |
1067 |
|
|
1068 |
def _has_legend_shown(context): |
def _has_legend_shown(context): |
1069 |
"""Return true if the legend window is shown""" |
"""Return true if the legend window is shown""" |
1179 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1180 |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
1181 |
helptext = _("Show the selected layer's table"), |
helptext = _("Show the selected layer's table"), |
1182 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_shape_layer) |
1183 |
_method_command("layer_properties", _("&Properties..."), "LayerEditProperties", |
_method_command("layer_properties", _("&Properties..."), "LayerEditProperties", |
1184 |
sensitive = _has_selected_layer, |
sensitive = _has_selected_layer, |
1185 |
helptext = _("Edit the properties of the selected layer")) |
helptext = _("Edit the properties of the selected layer")) |
1186 |
_method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable", |
_method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable", |
1187 |
sensitive = _has_selected_layer, |
sensitive = _has_selected_shape_layer, |
1188 |
helptext = _("Join and attach a table to the selected layer")) |
helptext = _("Join and attach a table to the selected layer")) |
1189 |
|
|
1190 |
|
# further layer methods: |
1191 |
|
_method_command("layer_to_top", _("&Top"), "LayerToTop", |
1192 |
|
helptext = _("Put selected layer to the top"), |
1193 |
|
sensitive = _has_selected_layer) |
1194 |
|
_method_command("layer_to_bottom", _("&Bottom"), "LayerToBottom", |
1195 |
|
helptext = _("Put selected layer to the bottom"), |
1196 |
|
sensitive = _has_selected_layer) |
1197 |
|
_method_command("layer_visibility", _("&Visible"), "ToggleLayerVisibility", |
1198 |
|
checked = _has_selected_layer_visible, |
1199 |
|
helptext = _("Toggle visibility of selected layer"), |
1200 |
|
sensitive = _has_selected_layer) |
1201 |
|
|
1202 |
def _can_unjoin(context): |
def _can_unjoin(context): |
1203 |
"""Return whether the Layer/Unjoin command can be executed. |
"""Return whether the Layer/Unjoin command can be executed. |
1204 |
|
|