9 |
|
|
10 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
11 |
|
|
12 |
|
from types import StringType |
13 |
|
|
14 |
from wxPython.wx import * |
from wxPython.wx import * |
15 |
|
|
16 |
from Thuban.Model.messages import MAPS_CHANGED, MAP_PROJECTION_CHANGED, \ |
from Thuban.Model.messages import MAPS_CHANGED, MAP_PROJECTION_CHANGED, \ |
17 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
18 |
|
EXTENSIONS_CHANGED, EXTENSION_OBJECTS_CHANGED |
19 |
from Thuban.Model.layer import Layer, shapetype_names |
from Thuban.Model.layer import Layer, shapetype_names |
20 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
21 |
|
|
22 |
from dialogs import NonModalDialog |
from dialogs import NonModalDialog |
23 |
from messages import SESSION_CHANGED, SELECTED_LAYER |
from messages import SESSION_CHANGED, SELECTED_LAYER |
24 |
|
|
|
def color_string(color): |
|
|
if color is None: |
|
|
return "None" |
|
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
|
|
|
|
25 |
|
|
26 |
class SessionTreeCtrl(wxTreeCtrl): |
class SessionTreeCtrl(wxTreeCtrl): |
27 |
|
|
28 |
# the session channels to subscribe to to update the tree |
"""Widget to display a tree view of the session. |
29 |
|
|
30 |
|
The tree view is created recursively from the session object. The |
31 |
|
tree view calls the session's TreeInfo method which should return a |
32 |
|
pair (<title>, <item>) where <title> ist the title of the session |
33 |
|
item in the tree view and <items> is a list of objects to use as the |
34 |
|
children of the session in the tree view. |
35 |
|
|
36 |
|
The items list can contain three types of items: |
37 |
|
|
38 |
|
1. a string. The string is used as the title for a leaf item in |
39 |
|
the tree view. |
40 |
|
|
41 |
|
2. an object with a TreeInfo method. This method is called and |
42 |
|
should return a pair just like the session's TreeInfo method. |
43 |
|
|
44 |
|
3. a pair (<title>, <item>) which is treated like the return |
45 |
|
value of TreeInfo. |
46 |
|
""" |
47 |
|
|
48 |
|
# the session channels to subscribe to update the tree |
49 |
session_channels = (MAPS_CHANGED, MAP_PROJECTION_CHANGED, |
session_channels = (MAPS_CHANGED, MAP_PROJECTION_CHANGED, |
50 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
51 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED, EXTENSIONS_CHANGED, |
52 |
|
EXTENSION_OBJECTS_CHANGED) |
53 |
|
|
54 |
def __init__(self, parent, ID, app): |
def __init__(self, parent, ID, app): |
55 |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
85 |
def update_tree(self, *args): |
def update_tree(self, *args): |
86 |
"""Clear and rebuild the tree""" |
"""Clear and rebuild the tree""" |
87 |
self.DeleteAllItems() |
self.DeleteAllItems() |
|
session = self.app.session |
|
|
root = self.AddRoot("Session: %s" % session.title) |
|
88 |
self.layer_to_item.clear() |
self.layer_to_item.clear() |
89 |
if session.filename == None: |
|
90 |
self.AppendItem(root, "Filename:") |
session = self.app.session |
91 |
else: |
info = session.TreeInfo() |
92 |
self.AppendItem(root, "Filename: %s" % session.filename) |
root = self.AddRoot(info[0]) |
93 |
if session.WasModified(): |
self.add_items(root, info[1]) |
|
self.AppendItem(root, "Modified: yes") |
|
|
else: |
|
|
self.AppendItem(root, "Modified: no") |
|
|
|
|
|
for map in session.Maps(): |
|
|
mapitem = self.AppendItem(root, "Map: %s" % map.title) |
|
|
self.SetPyData(mapitem, map) |
|
|
if map.BoundingBox() != None: |
|
|
self.AppendItem(mapitem, ("Extent (lat-lon): (%g, %g, %g, %g)" |
|
|
% map.BoundingBox())) |
|
|
if map.projection and len(map.projection.params) > 0: |
|
|
self.AppendItem(mapitem, |
|
|
("Extent (projected): (%g, %g, %g, %g)" |
|
|
% map.ProjectedBoundingBox())) |
|
|
projectionitem = self.AppendItem(mapitem, "Projection") |
|
|
for param in map.projection.params: |
|
|
parameteritem = self.AppendItem(projectionitem, str(param)) |
|
|
self.Expand(projectionitem) |
|
|
|
|
|
layers = map.Layers() |
|
|
for layer_index in range(len(layers) - 1, -1, -1): |
|
|
layer = layers[layer_index] |
|
|
idata = wxTreeItemData() |
|
|
idata.SetData(layer) |
|
|
layeritem = self.AppendItem(mapitem, |
|
|
"Layer '%s'" % layer.Title(), |
|
|
data = idata) |
|
|
self.layer_to_item[id(layer)] = layeritem |
|
|
if layer is self.app.interactor.selected_layer: |
|
|
self.SelectItem(layeritem) |
|
|
if isinstance(layer, Layer): |
|
|
if layer.Visible(): |
|
|
text = "Shown" |
|
|
else: |
|
|
text = "Hidden" |
|
|
self.AppendItem(layeritem, text) |
|
|
self.AppendItem(layeritem, "Shapes: %d" %layer.NumShapes()) |
|
|
bbox = layer.LatLongBoundingBox() |
|
|
if bbox is not None: |
|
|
self.AppendItem(layeritem, |
|
|
("Extent (lat-lon): (%g, %g, %g, %g)" |
|
|
% bbox)) |
|
|
else: |
|
|
self.AppendItem(layeritem, ("Extent (lat-lon):")) |
|
|
self.AppendItem(layeritem, |
|
|
"Shapetype: %s" |
|
|
% shapetype_names[layer.ShapeType()]) |
|
|
self.AppendItem(layeritem, |
|
|
"Fill: " + color_string(layer.fill)) |
|
|
self.AppendItem(layeritem, |
|
|
"Outline: " + color_string(layer.stroke)) |
|
|
self.Expand(layeritem) |
|
|
self.Expand(mapitem) |
|
94 |
self.Expand(root) |
self.Expand(root) |
95 |
|
# select the selected layer |
96 |
|
selected_layer = self.app.interactor.selected_layer |
97 |
|
if selected_layer is not None: |
98 |
|
self.SelectItem(self.layer_to_item[id(selected_layer)]) |
99 |
|
|
100 |
|
def add_items(self, parent, items): |
101 |
|
for item in items: |
102 |
|
if hasattr(item, "TreeInfo"): |
103 |
|
# Supports the TreeInfo protocol |
104 |
|
info = item.TreeInfo() |
105 |
|
treeitem = self.AppendItem(parent, info[0]) |
106 |
|
self.SetPyData(treeitem, item) |
107 |
|
self.add_items(treeitem, info[1]) |
108 |
|
self.Expand(treeitem) |
109 |
|
if isinstance(item, Layer): |
110 |
|
self.layer_to_item[id(item)] = treeitem |
111 |
|
elif isinstance(item, StringType): |
112 |
|
# it's a string |
113 |
|
# FIXME: What to do about UNICODE |
114 |
|
self.AppendItem(parent, item) |
115 |
|
else: |
116 |
|
# assume its a sequence (title, items) |
117 |
|
treeitem = self.AppendItem(parent, item[0]) |
118 |
|
self.add_items(treeitem, item[1]) |
119 |
|
self.Expand(treeitem) |
120 |
|
|
121 |
def session_changed(self, *args): |
def session_changed(self, *args): |
122 |
new_session = self.app.session |
new_session = self.app.session |