1 |
# Copyright (C) 2001, 2002, 2003 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]> |
12 |
""" |
""" |
13 |
|
|
14 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
15 |
|
# $Source$ |
16 |
__ThubanVersion__ = "0.8" #"$THUBAN_0_2$" |
# $Id$ |
|
#__BuildDate__ = "$Date$" |
|
17 |
|
|
18 |
import os |
import os |
19 |
import copy |
import copy |
20 |
|
|
21 |
from wxPython.wx import * |
from wxPython.wx import * |
|
from wxPython.wx import __version__ as wxPython_version |
|
22 |
|
|
23 |
import Thuban |
import Thuban |
|
import Thuban.version |
|
24 |
|
|
25 |
from Thuban import _ |
from Thuban import _ |
26 |
|
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 |
32 |
# XXX: replace this by |
# XXX: replace this by |
33 |
# from wxPython.lib.dialogs import wxMultipleChoiceDialog |
# from wxPython.lib.dialogs import wxMultipleChoiceDialog |
34 |
# when Thuban does not support wxPython 2.4.0 any more. |
# when Thuban does not support wxPython 2.4.0 any more. |
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 |
|
|
43 |
from context import Context |
from context import Context |
44 |
from command import registry, Command, ToolCommand |
from command import registry, Command, ToolCommand |
45 |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
46 |
|
MAP_REPLACED |
47 |
from about import About |
from about import About |
48 |
|
|
49 |
from Thuban.UI.dock import DockFrame |
from Thuban.UI.dock import DockFrame |
50 |
from Thuban.UI.join import JoinDialog |
from Thuban.UI.join import JoinDialog |
51 |
|
from Thuban.UI.dbdialog import DBFrame, DBDialog, ChooseDBTableDialog |
52 |
import resource |
import resource |
53 |
import Thuban.Model.resource |
import Thuban.Model.resource |
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 |
|
|
66 |
# actually come from. This delegation is implemented in the |
# actually come from. This delegation is implemented in the |
67 |
# Subscribe and unsubscribed methods |
# Subscribe and unsubscribed methods |
68 |
delegated_messages = {LAYER_SELECTED: "canvas", |
delegated_messages = {LAYER_SELECTED: "canvas", |
69 |
SHAPES_SELECTED: "canvas"} |
SHAPES_SELECTED: "canvas", |
70 |
|
MAP_REPLACED: "canvas"} |
71 |
|
|
72 |
# Methods delegated to some instance variables. The delegation is |
# Methods delegated to some instance variables. The delegation is |
73 |
# implemented in the __getattr__ method. |
# implemented in the __getattr__ method. |
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) |
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 |
|
|
146 |
""" |
""" |
147 |
if channel in self.delegated_messages: |
if channel in self.delegated_messages: |
148 |
object = getattr(self, self.delegated_messages[channel]) |
object = getattr(self, self.delegated_messages[channel]) |
149 |
object.Unsubscribe(channel, *args) |
try: |
150 |
|
object.Unsubscribe(channel, *args) |
151 |
|
except wxPyDeadObjectError: |
152 |
|
# The object was a wxObject and has already been |
153 |
|
# destroyed. Hopefully it has unsubscribed all its |
154 |
|
# subscribers already so that it's OK if we do nothing |
155 |
|
# here |
156 |
|
pass |
157 |
|
|
158 |
def __getattr__(self, attr): |
def __getattr__(self, attr): |
159 |
"""If attr is one of the delegated methods return that method |
"""If attr is one of the delegated methods return that method |
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 |
453 |
result = wxID_NO |
result = wxID_NO |
454 |
return result |
return result |
455 |
|
|
|
def prepare_new_session(self): |
|
|
for d in self.dialogs.values(): |
|
|
if not isinstance(d, tree.SessionTreeView): |
|
|
d.Close() |
|
|
|
|
456 |
def NewSession(self): |
def NewSession(self): |
457 |
if self.save_modified_session() != wxID_CANCEL: |
if self.save_modified_session() != wxID_CANCEL: |
|
self.prepare_new_session() |
|
458 |
self.application.SetSession(create_empty_session()) |
self.application.SetSession(create_empty_session()) |
459 |
|
|
460 |
def OpenSession(self): |
def OpenSession(self): |
461 |
if self.save_modified_session() != wxID_CANCEL: |
if self.save_modified_session() != wxID_CANCEL: |
462 |
dlg = wxFileDialog(self, _("Open Session"), ".", "", |
dlg = wxFileDialog(self, _("Open Session"), |
463 |
|
self.application.Path("data"), "", |
464 |
"Thuban Session File (*.thuban)|*.thuban", |
"Thuban Session File (*.thuban)|*.thuban", |
465 |
wxOPEN) |
wxOPEN) |
466 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
467 |
self.prepare_new_session() |
self.application.OpenSession(dlg.GetPath(), |
468 |
self.application.OpenSession(dlg.GetPath()) |
self.run_db_param_dialog) |
469 |
|
self.application.SetPath("data", dlg.GetPath()) |
470 |
dlg.Destroy() |
dlg.Destroy() |
471 |
|
|
472 |
|
def run_db_param_dialog(self, parameters, message): |
473 |
|
dlg = DBDialog(self, _("DB Connection Parameters"), parameters, |
474 |
|
message) |
475 |
|
return dlg.RunDialog() |
476 |
|
|
477 |
def SaveSession(self): |
def SaveSession(self): |
478 |
if self.application.session.filename == None: |
if self.application.session.filename == None: |
479 |
self.SaveSessionAs() |
self.SaveSessionAs() |
481 |
self.application.SaveSession() |
self.application.SaveSession() |
482 |
|
|
483 |
def SaveSessionAs(self): |
def SaveSessionAs(self): |
484 |
dlg = wxFileDialog(self, _("Save Session As"), ".", "", |
dlg = wxFileDialog(self, _("Save Session As"), |
485 |
|
self.application.Path("data"), "", |
486 |
"Thuban Session File (*.thuban)|*.thuban", |
"Thuban Session File (*.thuban)|*.thuban", |
487 |
wxSAVE|wxOVERWRITE_PROMPT) |
wxSAVE|wxOVERWRITE_PROMPT) |
488 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
489 |
self.application.session.SetFilename(dlg.GetPath()) |
self.application.session.SetFilename(dlg.GetPath()) |
490 |
self.application.SaveSession() |
self.application.SaveSession() |
491 |
|
self.application.SetPath("data",dlg.GetPath()) |
492 |
dlg.Destroy() |
dlg.Destroy() |
493 |
|
|
494 |
def Exit(self): |
def Exit(self): |
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() |
513 |
|
|
514 |
def SetMap(self, map): |
def SetMap(self, map): |
515 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
516 |
self.__SetTitle(map.Title()) |
self.update_title() |
517 |
|
|
518 |
dialog = self.FindRegisteredDock("legend") |
dialog = self.FindRegisteredDock("legend") |
519 |
if dialog is not None: |
if dialog is not None: |
544 |
dlg.ShowModal() |
dlg.ShowModal() |
545 |
dlg.Destroy() |
dlg.Destroy() |
546 |
|
|
547 |
|
def DatabaseManagement(self): |
548 |
|
name = "dbmanagement" |
549 |
|
dialog = self.get_open_dialog(name) |
550 |
|
if dialog is None: |
551 |
|
map = self.canvas.Map() |
552 |
|
dialog = DBFrame(self, name, self.application.Session()) |
553 |
|
self.add_dialog(name, dialog) |
554 |
|
dialog.Show() |
555 |
|
dialog.Raise() |
556 |
|
|
557 |
def AddLayer(self): |
def AddLayer(self): |
558 |
dlg = wxFileDialog(self, _("Select a data file"), ".", "", "*.*", |
dlg = wxFileDialog(self, _("Select one or more data files"), |
559 |
wxOPEN) |
self.application.Path("data"), "", |
560 |
|
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
561 |
|
_("All Files (*.*)") + "|*.*", |
562 |
|
wxOPEN | wxMULTIPLE) |
563 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
564 |
filename = dlg.GetPath() |
filenames = dlg.GetPaths() |
565 |
title = os.path.splitext(os.path.basename(filename))[0] |
for filename in filenames: |
566 |
map = self.canvas.Map() |
title = os.path.splitext(os.path.basename(filename))[0] |
567 |
has_layers = map.HasLayers() |
map = self.canvas.Map() |
568 |
try: |
has_layers = map.HasLayers() |
569 |
store = self.application.Session().OpenShapefile(filename) |
try: |
570 |
except IOError: |
store = self.application.Session().OpenShapefile(filename) |
571 |
# the layer couldn't be opened |
except IOError: |
572 |
self.RunMessageBox(_("Add Layer"), |
# the layer couldn't be opened |
573 |
_("Can't open the file '%s'.") % filename) |
self.RunMessageBox(_("Add Layer"), |
574 |
else: |
_("Can't open the file '%s'.")%filename) |
575 |
layer = Layer(title, store) |
else: |
576 |
map.AddLayer(layer) |
layer = Layer(title, store) |
577 |
if not has_layers: |
map.AddLayer(layer) |
578 |
# if we're adding a layer to an empty map, fit the |
if not has_layers: |
579 |
# new map to the window |
# if we're adding a layer to an empty map, fit the |
580 |
self.canvas.FitMapToWindow() |
# new map to the window |
581 |
|
self.canvas.FitMapToWindow() |
582 |
|
self.application.SetPath("data",filename) |
583 |
dlg.Destroy() |
dlg.Destroy() |
584 |
|
|
585 |
def AddRasterLayer(self): |
def AddRasterLayer(self): |
586 |
dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*", |
dlg = wxFileDialog(self, _("Select an image file"), |
587 |
|
self.application.Path("data"), "", "*.*", |
588 |
wxOPEN) |
wxOPEN) |
589 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
590 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
603 |
# if we're adding a layer to an empty map, fit the |
# if we're adding a layer to an empty map, fit the |
604 |
# new map to the window |
# new map to the window |
605 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
606 |
|
self.application.SetPath("data", filename) |
607 |
|
dlg.Destroy() |
608 |
|
|
609 |
|
def AddDBLayer(self): |
610 |
|
"""Add a layer read from a database""" |
611 |
|
session = self.application.Session() |
612 |
|
dlg = ChooseDBTableDialog(self, self.application.Session()) |
613 |
|
|
614 |
|
if dlg.ShowModal() == wxID_OK: |
615 |
|
dbconn, dbtable, id_column, geo_column = dlg.GetTable() |
616 |
|
try: |
617 |
|
title = str(dbtable) |
618 |
|
|
619 |
|
# Chose the correct Interface for the database type |
620 |
|
store = session.OpenDBShapeStore(dbconn, dbtable, |
621 |
|
id_column = id_column, |
622 |
|
geometry_column = geo_column) |
623 |
|
layer = Layer(title, store) |
624 |
|
except: |
625 |
|
# Some error occured while initializing the layer |
626 |
|
self.RunMessageBox(_("Add Layer from database"), |
627 |
|
_("Can't open the database table '%s'") |
628 |
|
% dbtable) |
629 |
|
return |
630 |
|
|
631 |
|
map = self.canvas.Map() |
632 |
|
|
633 |
|
has_layers = map.HasLayers() |
634 |
|
map.AddLayer(layer) |
635 |
|
if not has_layers: |
636 |
|
self.canvas.FitMapToWindow() |
637 |
|
|
638 |
dlg.Destroy() |
dlg.Destroy() |
639 |
|
|
640 |
def RemoveLayer(self): |
def RemoveLayer(self): |
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 |
|
|
686 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
687 |
return self.canvas.HasSelectedLayer() |
return self.canvas.HasSelectedLayer() |
688 |
|
|
689 |
|
def has_selected_shape_layer(self): |
690 |
|
"""Return true if a shape layer is currently selected""" |
691 |
|
return isinstance(self.current_layer(), Layer) |
692 |
|
|
693 |
def has_selected_shapes(self): |
def has_selected_shapes(self): |
694 |
"""Return true if a shape is currently selected""" |
"""Return true if a shape is currently selected""" |
695 |
return self.canvas.HasSelectedShapes() |
return self.canvas.HasSelectedShapes() |
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() |
836 |
else: |
else: |
837 |
dialog.Show(not dialog.IsShown()) |
dialog.Show(not dialog.IsShown()) |
838 |
|
|
|
self.canvas.FitMapToWindow() |
|
|
|
|
839 |
def LegendShown(self): |
def LegendShown(self): |
840 |
"""Return true iff the legend is currently open""" |
"""Return true iff the legend is currently open""" |
841 |
dialog = self.FindRegisteredDock("legend") |
dialog = self.FindRegisteredDock("legend") |
842 |
return dialog is not None and dialog.IsShown() |
return dialog is not None and dialog.IsShown() |
843 |
|
|
844 |
def TableOpen(self): |
def TableOpen(self): |
845 |
dlg = wxFileDialog(self, _("Open Table"), ".", "", |
dlg = wxFileDialog(self, _("Open Table"), |
846 |
|
self.application.Path("data"), "", |
847 |
_("DBF Files (*.dbf)") + "|*.dbf|" + |
_("DBF Files (*.dbf)") + "|*.dbf|" + |
848 |
#_("CSV Files (*.csv)") + "|*.csv|" + |
#_("CSV Files (*.csv)") + "|*.csv|" + |
849 |
_("All Files (*.*)") + "|*.*", |
_("All Files (*.*)") + "|*.*", |
859 |
_("Can't open the file '%s'.") % filename) |
_("Can't open the file '%s'.") % filename) |
860 |
else: |
else: |
861 |
self.ShowTableView(table) |
self.ShowTableView(table) |
862 |
|
self.application.SetPath("data",filename) |
863 |
|
|
864 |
def TableClose(self): |
def TableClose(self): |
865 |
tables = self.application.session.UnreferencedTables() |
tables = self.application.session.UnreferencedTables() |
913 |
table) |
table) |
914 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
915 |
dialog.Show(True) |
dialog.Show(True) |
916 |
# FIXME: else bring dialog to front |
dialog.Raise() |
917 |
|
|
918 |
def TableRename(self): |
def TableRename(self): |
919 |
"""Let the user rename a table""" |
"""Let the user rename a table""" |
936 |
|
|
937 |
# Second, let the user rename the layers |
# Second, let the user rename the layers |
938 |
for table in to_rename: |
for table in to_rename: |
939 |
dlg = wxTextEntryDialog(self, "Table Title: ", "Rename Table", |
dlg = wxTextEntryDialog(self, _("Table Title:"), _("Rename Table"), |
940 |
table.Title()) |
table.Title()) |
941 |
try: |
try: |
942 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
985 |
self.canvas.Print() |
self.canvas.Print() |
986 |
|
|
987 |
def RenameMap(self): |
def RenameMap(self): |
988 |
dlg = wxTextEntryDialog(self, "Map Title: ", "Rename Map", |
dlg = wxTextEntryDialog(self, _("Map Title:"), _("Rename Map"), |
989 |
self.Map().Title()) |
self.Map().Title()) |
990 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
991 |
title = dlg.GetValue() |
title = dlg.GetValue() |
992 |
if title != "": |
if title != "": |
993 |
self.Map().SetTitle(title) |
self.Map().SetTitle(title) |
|
self.__SetTitle(title) |
|
994 |
|
|
995 |
dlg.Destroy() |
dlg.Destroy() |
996 |
|
|
998 |
"""Let the user rename the currently selected layer""" |
"""Let the user rename the currently selected layer""" |
999 |
layer = self.current_layer() |
layer = self.current_layer() |
1000 |
if layer is not None: |
if layer is not None: |
1001 |
dlg = wxTextEntryDialog(self, "Layer Title: ", "Rename Layer", |
dlg = wxTextEntryDialog(self, _("Layer Title:"), _("Rename Layer"), |
1002 |
layer.Title()) |
layer.Title()) |
1003 |
try: |
try: |
1004 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
1032 |
# FIXME: bring dialog to front? |
# FIXME: bring dialog to front? |
1033 |
pass |
pass |
1034 |
|
|
1035 |
def __SetTitle(self, title): |
def title_changed(self, map): |
1036 |
self.SetTitle("Thuban - " + title) |
"""Subscribed to the canvas' TITLE_CHANGED messages""" |
1037 |
|
self.update_title() |
1038 |
|
|
1039 |
|
def update_title(self): |
1040 |
|
"""Update the window's title according to it's current state. |
1041 |
|
|
1042 |
|
In this default implementation the title is 'Thuban - ' followed |
1043 |
|
by the map's title or simply 'Thuban' if there is not map. |
1044 |
|
Derived classes should override this method to get different |
1045 |
|
titles. |
1046 |
|
|
1047 |
|
This method is called automatically by other methods when the |
1048 |
|
title may have to change. For the methods implemented in this |
1049 |
|
class this usually only means that a different map has been set |
1050 |
|
or the current map's title has changed. |
1051 |
|
""" |
1052 |
|
map = self.Map() |
1053 |
|
if map is not None: |
1054 |
|
title = _("Thuban - %s") % (map.Title(),) |
1055 |
|
else: |
1056 |
|
title = _("Thuban") |
1057 |
|
self.SetTitle(title) |
1058 |
|
|
1059 |
|
|
1060 |
# |
# |
1061 |
# Define all the commands available in the main window |
# Define all the commands available in the main window |
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): |
1109 |
|
"""Return true if a shape layer is selected in the context""" |
1110 |
|
return context.mainwindow.has_selected_shape_layer() |
1111 |
|
|
1112 |
def _has_selected_shapes(context): |
def _has_selected_shapes(context): |
1113 |
"""Return true if a layer is selected in the context""" |
"""Return true if a layer is selected in the context""" |
1114 |
return context.mainwindow.has_selected_shapes() |
return context.mainwindow.has_selected_shapes() |
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""" |
1139 |
"""Return True if the GDAL is available""" |
"""Return True if the GDAL is available""" |
1140 |
return Thuban.Model.resource.has_gdal_support() |
return Thuban.Model.resource.has_gdal_support() |
1141 |
|
|
1142 |
|
def _has_dbconnections(context): |
1143 |
|
"""Return whether the the session has database connections""" |
1144 |
|
return context.session.HasDBConnections() |
1145 |
|
|
1146 |
|
def _has_postgis_support(context): |
1147 |
|
return has_postgis_support() |
1148 |
|
|
1149 |
|
|
1150 |
# File menu |
# File menu |
1151 |
_method_command("new_session", _("&New Session"), "NewSession", |
_method_command("new_session", _("&New Session"), "NewSession", |
1152 |
helptext = _("Start a new session")) |
helptext = _("Start a new session")) |
1162 |
_method_command("toggle_legend", _("Legend"), "ToggleLegend", |
_method_command("toggle_legend", _("Legend"), "ToggleLegend", |
1163 |
checked = _has_legend_shown, |
checked = _has_legend_shown, |
1164 |
helptext = _("Toggle Legend on/off")) |
helptext = _("Toggle Legend on/off")) |
1165 |
|
_method_command("database_management", _("&Database Connections..."), |
1166 |
|
"DatabaseManagement", |
1167 |
|
sensitive = _has_postgis_support) |
1168 |
_method_command("exit", _("E&xit"), "Exit", |
_method_command("exit", _("E&xit"), "Exit", |
1169 |
helptext = _("Finish working with Thuban")) |
helptext = _("Finish working with Thuban")) |
1170 |
|
|
1214 |
_method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer", |
_method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer", |
1215 |
helptext = _("Add a new image layer to the map"), |
helptext = _("Add a new image layer to the map"), |
1216 |
sensitive = _has_gdal_support) |
sensitive = _has_gdal_support) |
1217 |
|
_method_command("layer_add_db", _("Add &Database Layer..."), "AddDBLayer", |
1218 |
|
helptext = _("Add a new database layer to active map"), |
1219 |
|
sensitive = _has_dbconnections) |
1220 |
_method_command("layer_remove", _("&Remove Layer"), "RemoveLayer", |
_method_command("layer_remove", _("&Remove Layer"), "RemoveLayer", |
1221 |
helptext = _("Remove selected layer"), |
helptext = _("Remove selected layer"), |
1222 |
sensitive = _can_remove_layer) |
sensitive = _can_remove_layer) |
1245 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1246 |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
1247 |
helptext = _("Show the selected layer's table"), |
helptext = _("Show the selected layer's table"), |
1248 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_shape_layer) |
1249 |
_method_command("layer_properties", _("&Properties..."), "LayerEditProperties", |
_method_command("layer_properties", _("&Properties..."), "LayerEditProperties", |
1250 |
sensitive = _has_selected_layer, |
sensitive = _has_selected_layer, |
1251 |
helptext = _("Edit the properties of the selected layer")) |
helptext = _("Edit the properties of the selected layer")) |
1252 |
_method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable", |
_method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable", |
1253 |
sensitive = _has_selected_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 |
|
|
1304 |
helptext = _("Join two tables creating a new one")) |
helptext = _("Join two tables creating a new one")) |
1305 |
|
|
1306 |
# Export only under Windows ... |
# Export only under Windows ... |
1307 |
map_menu = ["layer_add", "rasterlayer_add", "layer_remove", |
map_menu = ["layer_add", "layer_add_db", "rasterlayer_add", "layer_remove", |
1308 |
None, |
None, |
1309 |
"map_rename", |
"map_rename", |
1310 |
"map_projection", |
"map_projection", |
1311 |
None, |
None, |
1312 |
"map_zoom_in_tool", "map_zoom_out_tool", |
"map_zoom_in_tool", "map_zoom_out_tool", |
1313 |
"map_pan_tool", |
"map_pan_tool", |
1314 |
"map_full_extent", |
"map_full_extent", |
1315 |
"layer_full_extent", |
"layer_full_extent", |
1316 |
"selected_full_extent", |
"selected_full_extent", |
1317 |
None, |
None, |
1328 |
[Menu("file", _("&File"), |
[Menu("file", _("&File"), |
1329 |
["new_session", "open_session", None, |
["new_session", "open_session", None, |
1330 |
"save_session", "save_session_as", None, |
"save_session", "save_session_as", None, |
1331 |
|
"database_management", None, |
1332 |
"toggle_session_tree", None, |
"toggle_session_tree", None, |
1333 |
"exit"]), |
"exit"]), |
1334 |
Menu("map", _("&Map"), map_menu), |
Menu("map", _("&Map"), map_menu), |