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 SessioinTreeCtrl(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 |
self.app.Subscribe(SESSION_CHANGED, self.session_changed) |
40 |
self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected) |
41 |
# pretend the session has changed to build the initial tree |
42 |
self.session_changed() |
43 |
|
44 |
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged) |
45 |
|
46 |
def update_tree(self, *args): |
47 |
"""Clear and rebuild the tree""" |
48 |
self.DeleteAllItems() |
49 |
session = self.app.session |
50 |
root = self.AddRoot("Session: %s" % session.title) |
51 |
for map in session.Maps(): |
52 |
mapitem = self.AppendItem(root, "Map: %s" % map.title) |
53 |
self.SetPyData(mapitem, map) |
54 |
if map.projection and len(map.projection.params) > 0: |
55 |
projectionitem = self.AppendItem(mapitem, "Projection") |
56 |
for param in map.projection.params: |
57 |
parameteritem = self.AppendItem(projectionitem, str(param)) |
58 |
self.Expand(projectionitem) |
59 |
|
60 |
layers = map.Layers() |
61 |
for layer_index in range(len(layers) - 1, -1, -1): |
62 |
layer = layers[layer_index] |
63 |
idata = wxTreeItemData() |
64 |
idata.SetData(layer) |
65 |
layeritem = self.AppendItem(mapitem, |
66 |
"Layer '%s'" % layer.Title(), |
67 |
data = idata) |
68 |
if layer is self.app.interactor.selected_layer: |
69 |
self.SelectItem(layeritem) |
70 |
if isinstance(layer, Layer): |
71 |
if layer.Visible(): |
72 |
text = "Shown" |
73 |
else: |
74 |
text = "Hidden" |
75 |
self.AppendItem(layeritem, text) |
76 |
self.AppendItem(layeritem, "Shapes: %d" %layer.NumShapes()) |
77 |
self.AppendItem(layeritem, ("Extents: (%g, %g, %g, %g)" |
78 |
% layer.LatLongBoundingBox())) |
79 |
self.AppendItem(layeritem, |
80 |
"Shapetype: %s" |
81 |
% shapetype_names[layer.ShapeType()]) |
82 |
self.AppendItem(layeritem, |
83 |
"Fill: " + color_string(layer.fill)) |
84 |
self.AppendItem(layeritem, |
85 |
"Outline: " + color_string(layer.stroke)) |
86 |
self.Expand(layeritem) |
87 |
self.Expand(mapitem) |
88 |
self.Expand(root) |
89 |
|
90 |
def session_changed(self, *args): |
91 |
for channel in (MAPS_CHANGED, |
92 |
MAP_PROJECTION_CHANGED, |
93 |
LAYERS_CHANGED, |
94 |
LAYER_LEGEND_CHANGED, |
95 |
LAYER_VISIBILITY_CHANGED): |
96 |
self.app.session.Subscribe(channel, self.update_tree) |
97 |
self.update_tree() |
98 |
|
99 |
def normalize_selection(self): |
100 |
"""Select the layer or map containing currently selected item""" |
101 |
item = self.GetSelection() |
102 |
while item.IsOk(): |
103 |
object = self.GetPyData(item) |
104 |
if isinstance(object, Layer) or isinstance(object, Map): |
105 |
break |
106 |
item = self.GetItemParent(item) |
107 |
|
108 |
self.changing_selection = 1 |
109 |
try: |
110 |
self.SelectItem(item) |
111 |
finally: |
112 |
self.changing_selection = 0 |
113 |
|
114 |
def SelectedLayer(self): |
115 |
"""Return the layer object currently selected in the tree. |
116 |
Return None if no layer is selected""" |
117 |
layer = self.GetPyData(self.GetSelection()) |
118 |
if isinstance(layer, Layer): |
119 |
return layer |
120 |
return None |
121 |
|
122 |
def OnSelChanged(self, event): |
123 |
if self.changing_selection: |
124 |
# we're changing the selection ourselves (probably through |
125 |
# self.normalize_selection(). ignore the event. |
126 |
return |
127 |
self.normalize_selection() |
128 |
layer = self.SelectedLayer() |
129 |
if layer is not None: |
130 |
self.app.interactor.SelectLayer(layer) |
131 |
|
132 |
def layer_selected(self, layer): |
133 |
pass |
134 |
|
135 |
|
136 |
class SessionTreeView(NonModalDialog): |
137 |
|
138 |
"""Non modal dialog showing the session as a tree""" |
139 |
|
140 |
def __init__(self, parent, app, name): |
141 |
NonModalDialog.__init__(self, parent, app.interactor, name, "Session") |
142 |
self.tree = SessioinTreeCtrl(self, -1, app) |
143 |
|
144 |
def OnClose(self, event): |
145 |
#self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
146 |
NonModalDialog.OnClose(self, event) |
147 |
|