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 |
36 |
class SessioinTreeCtrl(wxTreeCtrl): |
29 |
|
|
|
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 |
6 |
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 |
bh |
36 |
EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged) |
45 |
bh |
6 |
|
46 |
|
|
def update_tree(self, *args): |
47 |
|
|
"""Clear and rebuild the tree""" |
48 |
bh |
36 |
self.DeleteAllItems() |
49 |
bh |
6 |
session = self.app.session |
50 |
bh |
36 |
root = self.AddRoot("Session: %s" % session.title) |
51 |
bh |
6 |
for map in session.Maps(): |
52 |
bh |
36 |
mapitem = self.AppendItem(root, "Map: %s" % map.title) |
53 |
|
|
self.SetPyData(mapitem, map) |
54 |
bh |
6 |
if map.projection and len(map.projection.params) > 0: |
55 |
bh |
36 |
projectionitem = self.AppendItem(mapitem, "Projection") |
56 |
bh |
6 |
for param in map.projection.params: |
57 |
bh |
36 |
parameteritem = self.AppendItem(projectionitem, str(param)) |
58 |
|
|
self.Expand(projectionitem) |
59 |
bh |
6 |
|
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 |
bh |
36 |
layeritem = self.AppendItem(mapitem, |
66 |
|
|
"Layer '%s'" % layer.Title(), |
67 |
|
|
data = idata) |
68 |
bh |
6 |
if layer is self.app.interactor.selected_layer: |
69 |
bh |
36 |
self.SelectItem(layeritem) |
70 |
bh |
6 |
if isinstance(layer, Layer): |
71 |
|
|
if layer.Visible(): |
72 |
|
|
text = "Shown" |
73 |
|
|
else: |
74 |
|
|
text = "Hidden" |
75 |
bh |
36 |
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 |
bh |
6 |
|
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 |
bh |
36 |
item = self.GetSelection() |
102 |
bh |
6 |
while item.IsOk(): |
103 |
bh |
36 |
object = self.GetPyData(item) |
104 |
bh |
6 |
if isinstance(object, Layer) or isinstance(object, Map): |
105 |
|
|
break |
106 |
bh |
36 |
item = self.GetItemParent(item) |
107 |
bh |
6 |
|
108 |
bh |
14 |
self.changing_selection = 1 |
109 |
|
|
try: |
110 |
bh |
36 |
self.SelectItem(item) |
111 |
bh |
14 |
finally: |
112 |
|
|
self.changing_selection = 0 |
113 |
bh |
6 |
|
114 |
|
|
def SelectedLayer(self): |
115 |
|
|
"""Return the layer object currently selected in the tree. |
116 |
|
|
Return None if no layer is selected""" |
117 |
bh |
36 |
layer = self.GetPyData(self.GetSelection()) |
118 |
bh |
6 |
if isinstance(layer, Layer): |
119 |
|
|
return layer |
120 |
|
|
return None |
121 |
|
|
|
122 |
|
|
def OnSelChanged(self, event): |
123 |
bh |
14 |
if self.changing_selection: |
124 |
|
|
# we're changing the selection ourselves (probably through |
125 |
|
|
# self.normalize_selection(). ignore the event. |
126 |
|
|
return |
127 |
bh |
6 |
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 |
bh |
36 |
|
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 |
|
|
|