1 |
bh |
6 |
#! /usr/bin/python |
2 |
|
|
# Copyright (c) 2001 by Intevation GmbH |
3 |
|
|
# 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 |
|
|
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED |
16 |
|
|
from Thuban.Model.layer import Layer, shapetype_names |
17 |
|
|
from Thuban.Model.map import Map |
18 |
|
|
|
19 |
bh |
36 |
from dialogs import NonModalDialog |
20 |
bh |
6 |
from messages import SESSION_CHANGED, SELECTED_LAYER |
21 |
|
|
|
22 |
|
|
def color_string(color): |
23 |
|
|
if color is None: |
24 |
|
|
return "None" |
25 |
|
|
return "(%.3f, %.3f, %.3f)" % (color.red, color.green, color.blue) |
26 |
|
|
|
27 |
|
|
|
28 |
bh |
41 |
class SessionTreeCtrl(wxTreeCtrl): |
29 |
bh |
36 |
|
30 |
|
|
def __init__(self, parent, ID, app): |
31 |
bh |
6 |
# Use the WANTS_CHARS style so the panel doesn't eat the Return key. |
32 |
bh |
36 |
wxTreeCtrl.__init__(self, parent, ID) |
33 |
|
|
|
34 |
bh |
6 |
self.app = app |
35 |
bh |
14 |
# boolean to indicate that we manipulate the selection ourselves |
36 |
|
|
# so that we can ignore the selection events generated |
37 |
|
|
self.changing_selection = 0 |
38 |
|
|
|
39 |
bh |
41 |
# Dictionary mapping layer id's to tree items |
40 |
|
|
self.layer_to_item = {} |
41 |
|
|
|
42 |
bh |
6 |
self.app.Subscribe(SESSION_CHANGED, self.session_changed) |
43 |
|
|
self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected) |
44 |
|
|
# pretend the session has changed to build the initial tree |
45 |
|
|
self.session_changed() |
46 |
|
|
|
47 |
bh |
36 |
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged) |
48 |
bh |
6 |
|
49 |
|
|
def update_tree(self, *args): |
50 |
|
|
"""Clear and rebuild the tree""" |
51 |
bh |
36 |
self.DeleteAllItems() |
52 |
bh |
6 |
session = self.app.session |
53 |
bh |
36 |
root = self.AddRoot("Session: %s" % session.title) |
54 |
bh |
41 |
self.layer_to_item.clear() |
55 |
jan |
104 |
if session.filename == None: |
56 |
|
|
self.AppendItem(root, "Filename:") |
57 |
|
|
else: |
58 |
|
|
self.AppendItem(root, "Filename: %s" % session.filename) |
59 |
|
|
if session.WasModified(): |
60 |
|
|
self.AppendItem(root, "Modified: yes") |
61 |
|
|
else: |
62 |
|
|
self.AppendItem(root, "Modified: no") |
63 |
|
|
|
64 |
bh |
6 |
for map in session.Maps(): |
65 |
bh |
36 |
mapitem = self.AppendItem(root, "Map: %s" % map.title) |
66 |
|
|
self.SetPyData(mapitem, map) |
67 |
jan |
115 |
if map.BoundingBox() != None: |
68 |
|
|
self.AppendItem(mapitem, ("Extent (lat-lon): (%g, %g, %g, %g)" |
69 |
|
|
% map.BoundingBox())) |
70 |
bh |
6 |
if map.projection and len(map.projection.params) > 0: |
71 |
jan |
111 |
self.AppendItem(mapitem, |
72 |
|
|
("Extent (projected): (%g, %g, %g, %g)" |
73 |
|
|
% map.ProjectedBoundingBox())) |
74 |
bh |
36 |
projectionitem = self.AppendItem(mapitem, "Projection") |
75 |
bh |
6 |
for param in map.projection.params: |
76 |
bh |
36 |
parameteritem = self.AppendItem(projectionitem, str(param)) |
77 |
|
|
self.Expand(projectionitem) |
78 |
bh |
6 |
|
79 |
|
|
layers = map.Layers() |
80 |
|
|
for layer_index in range(len(layers) - 1, -1, -1): |
81 |
|
|
layer = layers[layer_index] |
82 |
|
|
idata = wxTreeItemData() |
83 |
|
|
idata.SetData(layer) |
84 |
bh |
36 |
layeritem = self.AppendItem(mapitem, |
85 |
|
|
"Layer '%s'" % layer.Title(), |
86 |
|
|
data = idata) |
87 |
bh |
41 |
self.layer_to_item[id(layer)] = layeritem |
88 |
bh |
6 |
if layer is self.app.interactor.selected_layer: |
89 |
bh |
36 |
self.SelectItem(layeritem) |
90 |
bh |
6 |
if isinstance(layer, Layer): |
91 |
|
|
if layer.Visible(): |
92 |
|
|
text = "Shown" |
93 |
|
|
else: |
94 |
|
|
text = "Hidden" |
95 |
bh |
36 |
self.AppendItem(layeritem, text) |
96 |
|
|
self.AppendItem(layeritem, "Shapes: %d" %layer.NumShapes()) |
97 |
|
|
self.AppendItem(layeritem, |
98 |
jan |
111 |
("Extent (lat-lon): (%g, %g, %g, %g)" |
99 |
|
|
% layer.LatLongBoundingBox())) |
100 |
|
|
self.AppendItem(layeritem, |
101 |
bh |
36 |
"Shapetype: %s" |
102 |
|
|
% shapetype_names[layer.ShapeType()]) |
103 |
|
|
self.AppendItem(layeritem, |
104 |
|
|
"Fill: " + color_string(layer.fill)) |
105 |
|
|
self.AppendItem(layeritem, |
106 |
|
|
"Outline: " + color_string(layer.stroke)) |
107 |
|
|
self.Expand(layeritem) |
108 |
|
|
self.Expand(mapitem) |
109 |
|
|
self.Expand(root) |
110 |
bh |
6 |
|
111 |
|
|
def session_changed(self, *args): |
112 |
|
|
for channel in (MAPS_CHANGED, |
113 |
|
|
MAP_PROJECTION_CHANGED, |
114 |
|
|
LAYERS_CHANGED, |
115 |
|
|
LAYER_LEGEND_CHANGED, |
116 |
|
|
LAYER_VISIBILITY_CHANGED): |
117 |
|
|
self.app.session.Subscribe(channel, self.update_tree) |
118 |
|
|
self.update_tree() |
119 |
|
|
|
120 |
|
|
def normalize_selection(self): |
121 |
|
|
"""Select the layer or map containing currently selected item""" |
122 |
bh |
36 |
item = self.GetSelection() |
123 |
bh |
6 |
while item.IsOk(): |
124 |
bh |
36 |
object = self.GetPyData(item) |
125 |
bh |
6 |
if isinstance(object, Layer) or isinstance(object, Map): |
126 |
|
|
break |
127 |
bh |
36 |
item = self.GetItemParent(item) |
128 |
bh |
6 |
|
129 |
bh |
14 |
self.changing_selection = 1 |
130 |
|
|
try: |
131 |
bh |
36 |
self.SelectItem(item) |
132 |
bh |
14 |
finally: |
133 |
|
|
self.changing_selection = 0 |
134 |
bh |
6 |
|
135 |
|
|
def SelectedLayer(self): |
136 |
|
|
"""Return the layer object currently selected in the tree. |
137 |
|
|
Return None if no layer is selected""" |
138 |
bh |
36 |
layer = self.GetPyData(self.GetSelection()) |
139 |
bh |
6 |
if isinstance(layer, Layer): |
140 |
|
|
return layer |
141 |
|
|
return None |
142 |
|
|
|
143 |
|
|
def OnSelChanged(self, event): |
144 |
bh |
14 |
if self.changing_selection: |
145 |
|
|
# we're changing the selection ourselves (probably through |
146 |
|
|
# self.normalize_selection(). ignore the event. |
147 |
|
|
return |
148 |
bh |
6 |
self.normalize_selection() |
149 |
bh |
61 |
# SelectedLayer returns None if no layer is selected. Since |
150 |
|
|
# passing None to interactor.SelectLayer deselects the layer we |
151 |
|
|
# can simply pass the result of SelectedLayer on in all cases |
152 |
|
|
self.app.interactor.SelectLayer(self.SelectedLayer()) |
153 |
bh |
6 |
|
154 |
|
|
def layer_selected(self, layer): |
155 |
bh |
41 |
item = self.layer_to_item.get(id(layer)) |
156 |
|
|
if item is not None and item != self.GetSelection(): |
157 |
|
|
self.SelectItem(item) |
158 |
bh |
36 |
|
159 |
|
|
|
160 |
|
|
class SessionTreeView(NonModalDialog): |
161 |
|
|
|
162 |
|
|
"""Non modal dialog showing the session as a tree""" |
163 |
|
|
|
164 |
|
|
def __init__(self, parent, app, name): |
165 |
|
|
NonModalDialog.__init__(self, parent, app.interactor, name, "Session") |
166 |
bh |
41 |
self.tree = SessionTreeCtrl(self, -1, app) |
167 |
bh |
36 |
|
168 |
|
|
def OnClose(self, event): |
169 |
|
|
#self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
170 |
|
|
NonModalDialog.OnClose(self, event) |
171 |
|
|
|