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