/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/tree.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/tree.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (hide annotations)
Thu Sep 6 18:58:46 2001 UTC (23 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/tree.py
File MIME type: text/x-python
File size: 5929 byte(s)
	* Thuban/UI/tree.py (SessionTreeCtrl, SessionTreeView.__init__):
	Correct the spelling of SessionTreeCtrl. dabbrev is too damn
	convenient :-)
	(SessionTreeCtrl.__init__, SessionTreeCtrl.update_tree): Introduce
	a new instvar layer_to_item to map layers to tree items
	(SessionTreeCtrl.layer_selected): Select the appropriate tree item
	to match the current selection in the interactor

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26