1 |
bh |
6 |
#! /usr/bin/python |
2 |
bh |
151 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
3 |
bh |
6 |
# Authors: |
4 |
|
|
# Jan-Oliver Wagner <[email protected]> |
5 |
|
|
# Bernhard Herzog <[email protected]> |
6 |
|
|
# |
7 |
|
|
# This program is free software under the GPL (>=v2) |
8 |
|
|
# Read the file COPYING coming with Thuban for details. |
9 |
|
|
|
10 |
|
|
__version__ = "$Revision$" |
11 |
|
|
|
12 |
|
|
from wxPython.wx import * |
13 |
|
|
|
14 |
|
|
from Thuban.Model.messages import MAPS_CHANGED, MAP_PROJECTION_CHANGED, \ |
15 |
jan |
198 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
16 |
|
|
EXTENSIONS_CHANGED, EXTENSION_OBJECTS_CHANGED |
17 |
bh |
6 |
from Thuban.Model.layer import Layer, shapetype_names |
18 |
|
|
from Thuban.Model.map import Map |
19 |
|
|
|
20 |
bh |
36 |
from dialogs import NonModalDialog |
21 |
bh |
6 |
from messages import SESSION_CHANGED, SELECTED_LAYER |
22 |
|
|
|
23 |
|
|
def color_string(color): |
24 |
|
|
if color is None: |
25 |
|
|
return "None" |
26 |
|
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
27 |
|
|
|
28 |
|
|
|
29 |
bh |
41 |
class SessionTreeCtrl(wxTreeCtrl): |
30 |
bh |
36 |
|
31 |
jan |
198 |
# the session channels to subscribe to update the tree |
32 |
bh |
151 |
session_channels = (MAPS_CHANGED, MAP_PROJECTION_CHANGED, |
33 |
|
|
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
34 |
jan |
198 |
LAYER_VISIBILITY_CHANGED, EXTENSIONS_CHANGED, |
35 |
|
|
EXTENSION_OBJECTS_CHANGED) |
36 |
bh |
151 |
|
37 |
bh |
36 |
def __init__(self, parent, ID, app): |
38 |
bh |
6 |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
39 |
bh |
36 |
wxTreeCtrl.__init__(self, parent, ID) |
40 |
|
|
|
41 |
bh |
6 |
self.app = app |
42 |
bh |
14 |
# boolean to indicate that we manipulate the selection ourselves |
43 |
|
|
# so that we can ignore the selection events generated |
44 |
|
|
self.changing_selection = 0 |
45 |
|
|
|
46 |
bh |
41 |
# Dictionary mapping layer id's to tree items |
47 |
|
|
self.layer_to_item = {} |
48 |
|
|
|
49 |
bh |
6 |
self.app.Subscribe(SESSION_CHANGED, self.session_changed) |
50 |
|
|
self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected) |
51 |
bh |
151 |
|
52 |
|
|
# the session currently displayed in the tree |
53 |
|
|
self.session = None |
54 |
|
|
|
55 |
bh |
6 |
# pretend the session has changed to build the initial tree |
56 |
|
|
self.session_changed() |
57 |
|
|
|
58 |
bh |
36 |
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged) |
59 |
bh |
6 |
|
60 |
bh |
151 |
def unsubscribe_all(self): |
61 |
|
|
if self.session is not None: |
62 |
|
|
for channel in self.session_channels: |
63 |
|
|
self.session.Unsubscribe(channel, self.update_tree) |
64 |
bh |
161 |
self.session = None |
65 |
bh |
151 |
self.app.Unsubscribe(SESSION_CHANGED, self.session_changed) |
66 |
|
|
self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected) |
67 |
|
|
|
68 |
bh |
6 |
def update_tree(self, *args): |
69 |
|
|
"""Clear and rebuild the tree""" |
70 |
bh |
36 |
self.DeleteAllItems() |
71 |
bh |
6 |
session = self.app.session |
72 |
bh |
36 |
root = self.AddRoot("Session: %s" % session.title) |
73 |
bh |
41 |
self.layer_to_item.clear() |
74 |
jan |
104 |
if session.filename == None: |
75 |
|
|
self.AppendItem(root, "Filename:") |
76 |
|
|
else: |
77 |
|
|
self.AppendItem(root, "Filename: %s" % session.filename) |
78 |
|
|
if session.WasModified(): |
79 |
|
|
self.AppendItem(root, "Modified: yes") |
80 |
|
|
else: |
81 |
|
|
self.AppendItem(root, "Modified: no") |
82 |
|
|
|
83 |
bh |
6 |
for map in session.Maps(): |
84 |
bh |
36 |
mapitem = self.AppendItem(root, "Map: %s" % map.title) |
85 |
|
|
self.SetPyData(mapitem, map) |
86 |
jan |
115 |
if map.BoundingBox() != None: |
87 |
|
|
self.AppendItem(mapitem, ("Extent (lat-lon): (%g, %g, %g, %g)" |
88 |
|
|
% map.BoundingBox())) |
89 |
bh |
6 |
if map.projection and len(map.projection.params) > 0: |
90 |
jan |
111 |
self.AppendItem(mapitem, |
91 |
|
|
("Extent (projected): (%g, %g, %g, %g)" |
92 |
|
|
% map.ProjectedBoundingBox())) |
93 |
bh |
36 |
projectionitem = self.AppendItem(mapitem, "Projection") |
94 |
bh |
6 |
for param in map.projection.params: |
95 |
bh |
36 |
parameteritem = self.AppendItem(projectionitem, str(param)) |
96 |
|
|
self.Expand(projectionitem) |
97 |
bh |
6 |
|
98 |
|
|
layers = map.Layers() |
99 |
|
|
for layer_index in range(len(layers) - 1, -1, -1): |
100 |
|
|
layer = layers[layer_index] |
101 |
|
|
idata = wxTreeItemData() |
102 |
|
|
idata.SetData(layer) |
103 |
bh |
36 |
layeritem = self.AppendItem(mapitem, |
104 |
|
|
"Layer '%s'" % layer.Title(), |
105 |
|
|
data = idata) |
106 |
bh |
41 |
self.layer_to_item[id(layer)] = layeritem |
107 |
bh |
6 |
if layer is self.app.interactor.selected_layer: |
108 |
bh |
36 |
self.SelectItem(layeritem) |
109 |
bh |
6 |
if isinstance(layer, Layer): |
110 |
|
|
if layer.Visible(): |
111 |
|
|
text = "Shown" |
112 |
|
|
else: |
113 |
|
|
text = "Hidden" |
114 |
bh |
36 |
self.AppendItem(layeritem, text) |
115 |
|
|
self.AppendItem(layeritem, "Shapes: %d" %layer.NumShapes()) |
116 |
bh |
177 |
bbox = layer.LatLongBoundingBox() |
117 |
|
|
if bbox is not None: |
118 |
|
|
self.AppendItem(layeritem, |
119 |
|
|
("Extent (lat-lon): (%g, %g, %g, %g)" |
120 |
|
|
% bbox)) |
121 |
|
|
else: |
122 |
|
|
self.AppendItem(layeritem, ("Extent (lat-lon):")) |
123 |
bh |
36 |
self.AppendItem(layeritem, |
124 |
|
|
"Shapetype: %s" |
125 |
|
|
% shapetype_names[layer.ShapeType()]) |
126 |
|
|
self.AppendItem(layeritem, |
127 |
|
|
"Fill: " + color_string(layer.fill)) |
128 |
|
|
self.AppendItem(layeritem, |
129 |
|
|
"Outline: " + color_string(layer.stroke)) |
130 |
|
|
self.Expand(layeritem) |
131 |
|
|
self.Expand(mapitem) |
132 |
|
|
self.Expand(root) |
133 |
bh |
6 |
|
134 |
jan |
198 |
for extension in session.Extensions(): |
135 |
|
|
extensionitem = self.AppendItem(root, "Extension: %s" % |
136 |
|
|
extension.title) |
137 |
|
|
for object in extension.Objects(): |
138 |
|
|
objectitem = self.AppendItem(extensionitem, "%s: %s" % |
139 |
|
|
(object.title, object.name)) |
140 |
|
|
self.Expand(extensionitem) |
141 |
|
|
|
142 |
|
|
|
143 |
bh |
6 |
def session_changed(self, *args): |
144 |
bh |
151 |
new_session = self.app.session |
145 |
|
|
# if the session has changed subscribe/unsubscribe |
146 |
|
|
if self.session is not new_session: |
147 |
|
|
if self.session is not None: |
148 |
|
|
for channel in self.session_channels: |
149 |
|
|
self.session.Unsubscribe(channel, self.update_tree) |
150 |
|
|
if new_session is not None: |
151 |
|
|
for channel in self.session_channels: |
152 |
|
|
new_session.Subscribe(channel, self.update_tree) |
153 |
|
|
self.session = new_session |
154 |
bh |
6 |
self.update_tree() |
155 |
|
|
|
156 |
|
|
def normalize_selection(self): |
157 |
|
|
"""Select the layer or map containing currently selected item""" |
158 |
bh |
36 |
item = self.GetSelection() |
159 |
bh |
6 |
while item.IsOk(): |
160 |
bh |
36 |
object = self.GetPyData(item) |
161 |
bh |
6 |
if isinstance(object, Layer) or isinstance(object, Map): |
162 |
|
|
break |
163 |
bh |
36 |
item = self.GetItemParent(item) |
164 |
bh |
6 |
|
165 |
bh |
14 |
self.changing_selection = 1 |
166 |
|
|
try: |
167 |
bh |
36 |
self.SelectItem(item) |
168 |
bh |
14 |
finally: |
169 |
|
|
self.changing_selection = 0 |
170 |
bh |
6 |
|
171 |
|
|
def SelectedLayer(self): |
172 |
|
|
"""Return the layer object currently selected in the tree. |
173 |
|
|
Return None if no layer is selected""" |
174 |
bh |
36 |
layer = self.GetPyData(self.GetSelection()) |
175 |
bh |
6 |
if isinstance(layer, Layer): |
176 |
|
|
return layer |
177 |
|
|
return None |
178 |
|
|
|
179 |
|
|
def OnSelChanged(self, event): |
180 |
bh |
14 |
if self.changing_selection: |
181 |
|
|
# we're changing the selection ourselves (probably through |
182 |
|
|
# self.normalize_selection(). ignore the event. |
183 |
|
|
return |
184 |
bh |
6 |
self.normalize_selection() |
185 |
bh |
61 |
# SelectedLayer returns None if no layer is selected. Since |
186 |
|
|
# passing None to interactor.SelectLayer deselects the layer we |
187 |
|
|
# can simply pass the result of SelectedLayer on in all cases |
188 |
|
|
self.app.interactor.SelectLayer(self.SelectedLayer()) |
189 |
bh |
6 |
|
190 |
|
|
def layer_selected(self, layer): |
191 |
bh |
41 |
item = self.layer_to_item.get(id(layer)) |
192 |
|
|
if item is not None and item != self.GetSelection(): |
193 |
|
|
self.SelectItem(item) |
194 |
bh |
36 |
|
195 |
|
|
|
196 |
|
|
class SessionTreeView(NonModalDialog): |
197 |
|
|
|
198 |
|
|
"""Non modal dialog showing the session as a tree""" |
199 |
|
|
|
200 |
|
|
def __init__(self, parent, app, name): |
201 |
|
|
NonModalDialog.__init__(self, parent, app.interactor, name, "Session") |
202 |
bh |
41 |
self.tree = SessionTreeCtrl(self, -1, app) |
203 |
bh |
36 |
|
204 |
|
|
def OnClose(self, event): |
205 |
|
|
#self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
206 |
|
|
NonModalDialog.OnClose(self, event) |
207 |
|
|
|
208 |
bh |
151 |
# if there were a way to get notified when the tree control |
209 |
|
|
# itself is destroyed we could use that to unsubscribe instead |
210 |
|
|
# of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at |
211 |
|
|
# all) |
212 |
|
|
self.tree.unsubscribe_all() |