/[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 520 - (hide annotations)
Wed Mar 12 10:35:49 2003 UTC (21 years, 11 months ago) by jonathan
Original Path: trunk/thuban/Thuban/UI/tree.py
File MIME type: text/x-python
File size: 8489 byte(s)
Try using a 0x0 image for items that don't need an image so that the
tree looks OK under Windows.

1 bh 6 #! /usr/bin/python
2 bh 151 # Copyright (c) 2001, 2002 by Intevation GmbH
3 bh 6 # 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 jonathan 383 from types import StringType, UnicodeType
13 bh 217
14 bh 6 from wxPython.wx import *
15    
16 jan 374 from Thuban import _
17 jonathan 443 from Thuban.UI.common import *
18 jan 374
19 jonathan 383 from Thuban.Model.color import Color
20    
21 bh 233 from Thuban.Model.messages import CHANGED
22     from Thuban.Model.layer import Layer
23 bh 6 from Thuban.Model.map import Map
24    
25 bh 36 from dialogs import NonModalDialog
26 jonathan 504 from messages import SESSION_REPLACED, SELECTED_LAYER
27 bh 6
28 jonathan 383 BMP_SIZE = 15
29 bh 6
30 bh 41 class SessionTreeCtrl(wxTreeCtrl):
31 bh 36
32 bh 217 """Widget to display a tree view of the session.
33    
34     The tree view is created recursively from the session object. The
35     tree view calls the session's TreeInfo method which should return a
36     pair (<title>, <item>) where <title> ist the title of the session
37     item in the tree view and <items> is a list of objects to use as the
38     children of the session in the tree view.
39    
40     The items list can contain three types of items:
41    
42     1. a string. The string is used as the title for a leaf item in
43     the tree view.
44    
45     2. an object with a TreeInfo method. This method is called and
46     should return a pair just like the session's TreeInfo method.
47    
48     3. a pair (<title>, <item>) which is treated like the return
49     value of TreeInfo.
50     """
51    
52 bh 36 def __init__(self, parent, ID, app):
53 jonathan 383
54 bh 6 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
55 bh 36 wxTreeCtrl.__init__(self, parent, ID)
56    
57 bh 6 self.app = app
58 bh 14 # boolean to indicate that we manipulate the selection ourselves
59     # so that we can ignore the selection events generated
60     self.changing_selection = 0
61    
62 bh 41 # Dictionary mapping layer id's to tree items
63     self.layer_to_item = {}
64    
65 jonathan 504 self.app.Subscribe(SESSION_REPLACED, self.session_changed)
66 bh 6 self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected)
67 bh 151
68     # the session currently displayed in the tree
69     self.session = None
70    
71 jonathan 383
72 bh 6 # pretend the session has changed to build the initial tree
73     self.session_changed()
74    
75 bh 36 EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
76 bh 6
77 bh 151 def unsubscribe_all(self):
78     if self.session is not None:
79 bh 233 self.session.Unsubscribe(CHANGED, self.update_tree)
80 bh 161 self.session = None
81 jonathan 504 self.app.Unsubscribe(SESSION_REPLACED, self.session_changed)
82 bh 151 self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected)
83    
84 bh 6 def update_tree(self, *args):
85     """Clear and rebuild the tree"""
86 bh 36 self.DeleteAllItems()
87 bh 41 self.layer_to_item.clear()
88 jonathan 510 self.image_list = wxImageList(BMP_SIZE, BMP_SIZE, False, 0)
89     self.AssignImageList(self.image_list)
90 jonathan 520 bmp = wxEmptyBitmap(0, 0)
91     self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0))
92 jan 104
93 bh 217 session = self.app.session
94     info = session.TreeInfo()
95 jonathan 520 root = self.AddRoot(info[0], 0, -1, None)
96 bh 217 self.add_items(root, info[1])
97 bh 36 self.Expand(root)
98 bh 217 # select the selected layer
99     selected_layer = self.app.interactor.selected_layer
100     if selected_layer is not None:
101 bh 218 # One would expect that the selected_layer's id is in
102     # layer_to_item at this point as we've just rebuilt that
103     # mapping completely. However, when a new session is loaded
104     # for instance, it can happen that the tree view is updated
105     # before the interactor in which case selected_layer may be
106     # a layer of the old session.
107     item = self.layer_to_item.get(id(selected_layer))
108     if item is not None:
109     self.SelectItem(item)
110 bh 6
111 bh 217 def add_items(self, parent, items):
112 jonathan 383
113     if items is None: return
114    
115 bh 217 for item in items:
116     if hasattr(item, "TreeInfo"):
117     # Supports the TreeInfo protocol
118     info = item.TreeInfo()
119 jonathan 510 #treeitem = self.AppendItem(parent, info[0])
120 jonathan 520 treeitem = self.AppendItem(parent, info[0], 0, -1, None)
121 bh 217 self.SetPyData(treeitem, item)
122     self.add_items(treeitem, info[1])
123     self.Expand(treeitem)
124     if isinstance(item, Layer):
125     self.layer_to_item[id(item)] = treeitem
126 jonathan 383 elif isinstance(item, StringType) or \
127     isinstance(item, UnicodeType):
128 bh 217 # it's a string
129 jonathan 520 treeitem = self.AppendItem(parent, item, 0, -1, None)
130 jonathan 510 #self.SetItemImage(treeitem, -1)
131 bh 217 else:
132     # assume its a sequence (title, items)
133 jonathan 383 if isinstance(item[1], Color):
134    
135 jonathan 417 treeitem = self.AppendItem(parent, "(%s)" % item[0])
136    
137 jonathan 383 bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE)
138 jonathan 443 brush = wxBrush(Color2wxColour(item[1]), wxSOLID)
139 jonathan 383 dc = wxMemoryDC()
140     dc.SelectObject(bmp)
141     dc.SetBrush(brush)
142     dc.Clear()
143     dc.DrawRoundedRectangle(0, 0,
144     bmp.GetWidth(), bmp.GetHeight(),
145     4)
146     dc.SelectObject(wxNullBitmap)
147    
148     i = self.image_list.Add(bmp)
149     self.SetItemImage(treeitem, i)
150     else:
151 jonathan 510 #treeitem = self.AppendItem(parent, item[0])
152 jonathan 520 treeitem = self.AppendItem(parent, item[0], 0, -1, None)
153 jonathan 383 self.add_items(treeitem, item[1])
154 bh 217 self.Expand(treeitem)
155 jan 198
156 bh 6 def session_changed(self, *args):
157 bh 151 new_session = self.app.session
158     # if the session has changed subscribe/unsubscribe
159     if self.session is not new_session:
160     if self.session is not None:
161 bh 233 self.session.Unsubscribe(CHANGED, self.update_tree)
162 bh 151 if new_session is not None:
163 bh 233 new_session.Subscribe(CHANGED, self.update_tree)
164 bh 151 self.session = new_session
165 bh 6 self.update_tree()
166    
167     def normalize_selection(self):
168     """Select the layer or map containing currently selected item"""
169 bh 36 item = self.GetSelection()
170 bh 6 while item.IsOk():
171 bh 36 object = self.GetPyData(item)
172 bh 6 if isinstance(object, Layer) or isinstance(object, Map):
173     break
174 bh 36 item = self.GetItemParent(item)
175 bh 353 else:
176     # No layer or map was found in the chain of parents, so
177     # there's nothing we can do.
178     return
179 bh 6
180 bh 14 self.changing_selection = 1
181     try:
182 bh 36 self.SelectItem(item)
183 bh 14 finally:
184     self.changing_selection = 0
185 bh 6
186     def SelectedLayer(self):
187     """Return the layer object currently selected in the tree.
188     Return None if no layer is selected"""
189 bh 36 layer = self.GetPyData(self.GetSelection())
190 bh 6 if isinstance(layer, Layer):
191     return layer
192     return None
193    
194     def OnSelChanged(self, event):
195 bh 14 if self.changing_selection:
196     # we're changing the selection ourselves (probably through
197     # self.normalize_selection(). ignore the event.
198     return
199 bh 6 self.normalize_selection()
200 bh 61 # SelectedLayer returns None if no layer is selected. Since
201     # passing None to interactor.SelectLayer deselects the layer we
202     # can simply pass the result of SelectedLayer on in all cases
203     self.app.interactor.SelectLayer(self.SelectedLayer())
204 bh 6
205     def layer_selected(self, layer):
206 bh 41 item = self.layer_to_item.get(id(layer))
207     if item is not None and item != self.GetSelection():
208     self.SelectItem(item)
209 bh 36
210    
211     class SessionTreeView(NonModalDialog):
212    
213     """Non modal dialog showing the session as a tree"""
214    
215     def __init__(self, parent, app, name):
216 jan 374 NonModalDialog.__init__(self, parent, app.interactor, name,
217     _("Session"))
218 bh 41 self.tree = SessionTreeCtrl(self, -1, app)
219 bh 36
220 jonathan 504 def OnClose(self, event):
221     NonModalDialog.OnClose(self, event)
222    
223 bh 151 # if there were a way to get notified when the tree control
224     # itself is destroyed we could use that to unsubscribe instead
225     # of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at
226     # all)
227     self.tree.unsubscribe_all()
228 jonathan 488

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26