/[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 14 - (hide annotations)
Fri Aug 31 15:34:33 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: 5692 byte(s)
	* Thuban/UI/tree.py (myTreeCtrlPanel.__init__): New inst var
	changing_selection to avoid recursive selection events when
	modifying the selection in response to a selection event.
	(myTreeCtrlPanel.normalize_selection): Set the new inst var when
	changing the tree's selection.
	(myTreeCtrlPanel.OnSelChanged): Only normalize the selection when
	we're not being called indirectly from normalize_selection.

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     import view
19    
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     class myTreeCtrlPanel(wxPanel):
28    
29     def __init__(self, parent, app):
30     # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
31     wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
32     self.app = app
33    
34 bh 14 # boolean to indicate that we manipulate the selection ourselves
35     # so that we can ignore the selection events generated
36     self.changing_selection = 0
37    
38 bh 6 EVT_SIZE(self, self.OnSize)
39     self.tree = wxTreeCtrl(self, -1)
40    
41     self.app.Subscribe(SESSION_CHANGED, self.session_changed)
42     self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected)
43     # pretend the session has changed to build the initial tree
44     self.session_changed()
45    
46     EVT_TREE_SEL_CHANGED(self, self.tree.GetId(), self.OnSelChanged)
47    
48     def update_tree(self, *args):
49     """Clear and rebuild the tree"""
50     self.tree.DeleteAllItems()
51     session = self.app.session
52     root = self.tree.AddRoot("Session: %s" % session.title)
53     for map in session.Maps():
54     mapitem = self.tree.AppendItem(root, "Map: %s" % map.title)
55     self.tree.SetPyData(mapitem, map)
56     if map.projection and len(map.projection.params) > 0:
57     projectionitem = self.tree.AppendItem(mapitem, "Projection")
58     for param in map.projection.params:
59     parameteritem = self.tree.AppendItem(projectionitem,
60     str(param))
61     self.tree.Expand(projectionitem)
62    
63     layers = map.Layers()
64     for layer_index in range(len(layers) - 1, -1, -1):
65     layer = layers[layer_index]
66     idata = wxTreeItemData()
67     idata.SetData(layer)
68     layeritem = self.tree.AppendItem(mapitem,
69     "Layer '%s'" % layer.Title(),
70     data = idata)
71     if layer is self.app.interactor.selected_layer:
72     self.tree.SelectItem(layeritem)
73     if isinstance(layer, Layer):
74     if layer.Visible():
75     text = "Shown"
76     else:
77     text = "Hidden"
78     self.tree.AppendItem(layeritem, text)
79     self.tree.AppendItem(layeritem,
80     "Shapes: %d" % layer.NumShapes())
81     self.tree.AppendItem(layeritem,
82     ("Extents: (%g, %g, %g, %g)"
83     % layer.LatLongBoundingBox()))
84     self.tree.AppendItem(layeritem,
85     "Shapetype: %s" %
86     shapetype_names[layer.ShapeType()])
87     self.tree.AppendItem(layeritem,
88     "Fill: " + color_string(layer.fill))
89     self.tree.AppendItem(layeritem,
90     "Outline: " + color_string(layer.stroke))
91     self.tree.Expand(layeritem)
92     self.tree.Expand(mapitem)
93     self.tree.Expand(root)
94    
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 OnSize(self, event):
105     w,h = self.GetClientSizeTuple()
106     self.tree.SetDimensions(0, 0, w, h)
107    
108     def normalize_selection(self):
109     """Select the layer or map containing currently selected item"""
110     tree = self.tree
111     item = self.tree.GetSelection()
112     while item.IsOk():
113     object = tree.GetPyData(item)
114     if isinstance(object, Layer) or isinstance(object, Map):
115     break
116     item = tree.GetItemParent(item)
117    
118 bh 14 self.changing_selection = 1
119     try:
120     self.tree.SelectItem(item)
121     finally:
122     self.changing_selection = 0
123 bh 6
124     def SelectedLayer(self):
125     """Return the layer object currently selected in the tree.
126     Return None if no layer is selected"""
127     tree = self.tree
128     layer = tree.GetPyData(tree.GetSelection())
129     if isinstance(layer, Layer):
130     return layer
131     return None
132    
133     def OnSelChanged(self, event):
134 bh 14 if self.changing_selection:
135     # we're changing the selection ourselves (probably through
136     # self.normalize_selection(). ignore the event.
137     return
138 bh 6 self.normalize_selection()
139     layer = self.SelectedLayer()
140     if layer is not None:
141     self.app.interactor.SelectLayer(layer)
142    
143     def layer_selected(self, layer):
144     pass

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26