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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 217 by bh, Wed Jul 17 10:50:40 2002 UTC revision 520 by jonathan, Wed Mar 12 10:35:49 2003 UTC
# Line 9  Line 9 
9    
10  __version__ = "$Revision$"  __version__ = "$Revision$"
11    
12  from types import StringType  from types import StringType, UnicodeType
13    
14  from wxPython.wx import *  from wxPython.wx import *
15    
16  from Thuban.Model.messages import MAPS_CHANGED, MAP_PROJECTION_CHANGED, \  from Thuban import _
17       LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED, \  from Thuban.UI.common import *
18       EXTENSIONS_CHANGED, EXTENSION_OBJECTS_CHANGED  
19  from Thuban.Model.layer import Layer, shapetype_names  from Thuban.Model.color import Color
20    
21    from Thuban.Model.messages import CHANGED
22    from Thuban.Model.layer import Layer
23  from Thuban.Model.map import Map  from Thuban.Model.map import Map
24    
25  from dialogs import NonModalDialog  from dialogs import NonModalDialog
26  from messages import SESSION_CHANGED, SELECTED_LAYER  from messages import SESSION_REPLACED, SELECTED_LAYER
27    
28    BMP_SIZE = 15
29    
30  class SessionTreeCtrl(wxTreeCtrl):  class SessionTreeCtrl(wxTreeCtrl):
31    
# Line 45  class SessionTreeCtrl(wxTreeCtrl): Line 49  class SessionTreeCtrl(wxTreeCtrl):
49            value of TreeInfo.            value of TreeInfo.
50      """      """
51    
     # the session channels to subscribe to update the tree  
     session_channels = (MAPS_CHANGED, MAP_PROJECTION_CHANGED,  
                         LAYERS_CHANGED, LAYER_LEGEND_CHANGED,  
                         LAYER_VISIBILITY_CHANGED, EXTENSIONS_CHANGED,  
                         EXTENSION_OBJECTS_CHANGED)  
   
52      def __init__(self, parent, ID, app):      def __init__(self, parent, ID, app):
53    
54          # Use the WANTS_CHARS style so the panel doesn't eat the Return key.          # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
55          wxTreeCtrl.__init__(self, parent, ID)          wxTreeCtrl.__init__(self, parent, ID)
56    
# Line 63  class SessionTreeCtrl(wxTreeCtrl): Line 62  class SessionTreeCtrl(wxTreeCtrl):
62          # Dictionary mapping layer id's to tree items          # Dictionary mapping layer id's to tree items
63          self.layer_to_item = {}          self.layer_to_item = {}
64    
65          self.app.Subscribe(SESSION_CHANGED, self.session_changed)          self.app.Subscribe(SESSION_REPLACED, self.session_changed)
66          self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected)          self.app.interactor.Subscribe(SELECTED_LAYER, self.layer_selected)
67    
68          # the session currently displayed in the tree          # the session currently displayed in the tree
69          self.session = None          self.session = None
70    
71    
72          # pretend the session has changed to build the initial tree          # pretend the session has changed to build the initial tree
73          self.session_changed()          self.session_changed()
74    
# Line 76  class SessionTreeCtrl(wxTreeCtrl): Line 76  class SessionTreeCtrl(wxTreeCtrl):
76    
77      def unsubscribe_all(self):      def unsubscribe_all(self):
78          if self.session is not None:          if self.session is not None:
79              for channel in self.session_channels:              self.session.Unsubscribe(CHANGED, self.update_tree)
                 self.session.Unsubscribe(channel, self.update_tree)  
80              self.session = None              self.session = None
81          self.app.Unsubscribe(SESSION_CHANGED, self.session_changed)          self.app.Unsubscribe(SESSION_REPLACED, self.session_changed)
82          self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected)          self.app.interactor.Unsubscribe(SELECTED_LAYER, self.layer_selected)
83    
84      def update_tree(self, *args):      def update_tree(self, *args):
85          """Clear and rebuild the tree"""          """Clear and rebuild the tree"""
86          self.DeleteAllItems()          self.DeleteAllItems()
87          self.layer_to_item.clear()          self.layer_to_item.clear()
88            self.image_list = wxImageList(BMP_SIZE, BMP_SIZE, False, 0)
89            self.AssignImageList(self.image_list)
90            bmp = wxEmptyBitmap(0, 0)
91            self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0))
92    
93          session = self.app.session          session = self.app.session
94          info = session.TreeInfo()          info = session.TreeInfo()
95          root = self.AddRoot(info[0])          root = self.AddRoot(info[0], 0, -1, None)
96          self.add_items(root, info[1])          self.add_items(root, info[1])
97          self.Expand(root)          self.Expand(root)
98          # select the selected layer          # select the selected layer
99          selected_layer = self.app.interactor.selected_layer          selected_layer = self.app.interactor.selected_layer
100          if selected_layer is not None:          if selected_layer is not None:
101              self.SelectItem(self.layer_to_item[id(selected_layer)])              # 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    
111      def add_items(self, parent, items):      def add_items(self, parent, items):
112    
113            if items is None: return
114    
115          for item in items:          for item in items:
116              if hasattr(item, "TreeInfo"):              if hasattr(item, "TreeInfo"):
117                  # Supports the TreeInfo protocol                  # Supports the TreeInfo protocol
118                  info = item.TreeInfo()                  info = item.TreeInfo()
119                  treeitem = self.AppendItem(parent, info[0])                  #treeitem = self.AppendItem(parent, info[0])
120                    treeitem = self.AppendItem(parent, info[0], 0, -1, None)
121                  self.SetPyData(treeitem, item)                  self.SetPyData(treeitem, item)
122                  self.add_items(treeitem, info[1])                  self.add_items(treeitem, info[1])
123                  self.Expand(treeitem)                  self.Expand(treeitem)
124                  if isinstance(item, Layer):                  if isinstance(item, Layer):
125                      self.layer_to_item[id(item)] = treeitem                      self.layer_to_item[id(item)] = treeitem
126              elif isinstance(item, StringType):              elif isinstance(item, StringType) or \
127                     isinstance(item, UnicodeType):
128                  # it's a string                  # it's a string
129                  # FIXME: What to do about UNICODE                  treeitem = self.AppendItem(parent, item, 0, -1, None)
130                  self.AppendItem(parent, item)                  #self.SetItemImage(treeitem, -1)
131              else:              else:
132                  # assume its a sequence (title, items)                  # assume its a sequence (title, items)
133                  treeitem = self.AppendItem(parent, item[0])                  if isinstance(item[1], Color):
134                  self.add_items(treeitem, item[1])  
135                        treeitem = self.AppendItem(parent, "(%s)" % item[0])
136    
137                        bmp = wxEmptyBitmap(BMP_SIZE, BMP_SIZE)
138                        brush = wxBrush(Color2wxColour(item[1]), wxSOLID)
139                        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                        #treeitem = self.AppendItem(parent, item[0])
152                        treeitem = self.AppendItem(parent, item[0], 0, -1, None)
153                        self.add_items(treeitem, item[1])
154                  self.Expand(treeitem)                  self.Expand(treeitem)
155    
156      def session_changed(self, *args):      def session_changed(self, *args):
# Line 123  class SessionTreeCtrl(wxTreeCtrl): Line 158  class SessionTreeCtrl(wxTreeCtrl):
158          # if the session has changed subscribe/unsubscribe          # if the session has changed subscribe/unsubscribe
159          if self.session is not new_session:          if self.session is not new_session:
160              if self.session is not None:              if self.session is not None:
161                  for channel in self.session_channels:                  self.session.Unsubscribe(CHANGED, self.update_tree)
                     self.session.Unsubscribe(channel, self.update_tree)  
162              if new_session is not None:              if new_session is not None:
163                  for channel in self.session_channels:                  new_session.Subscribe(CHANGED, self.update_tree)
                     new_session.Subscribe(channel, self.update_tree)  
164              self.session = new_session              self.session = new_session
165          self.update_tree()          self.update_tree()
166    
# Line 139  class SessionTreeCtrl(wxTreeCtrl): Line 172  class SessionTreeCtrl(wxTreeCtrl):
172              if isinstance(object, Layer) or isinstance(object, Map):              if isinstance(object, Layer) or isinstance(object, Map):
173                  break                  break
174              item = self.GetItemParent(item)              item = self.GetItemParent(item)
175            else:
176                # No layer or map was found in the chain of parents, so
177                # there's nothing we can do.
178                return
179    
180          self.changing_selection = 1          self.changing_selection = 1
181          try:          try:
# Line 176  class SessionTreeView(NonModalDialog): Line 213  class SessionTreeView(NonModalDialog):
213      """Non modal dialog showing the session as a tree"""      """Non modal dialog showing the session as a tree"""
214    
215      def __init__(self, parent, app, name):      def __init__(self, parent, app, name):
216          NonModalDialog.__init__(self, parent, app.interactor, name, "Session")          NonModalDialog.__init__(self, parent, app.interactor, name,
217                                    _("Session"))
218          self.tree = SessionTreeCtrl(self, -1, app)          self.tree = SessionTreeCtrl(self, -1, app)
219    
220      def OnClose(self, event):      def OnClose(self, event):
         #self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape)  
221          NonModalDialog.OnClose(self, event)          NonModalDialog.OnClose(self, event)
222    
223          # if there were a way to get notified when the tree control          # if there were a way to get notified when the tree control
# Line 188  class SessionTreeView(NonModalDialog): Line 225  class SessionTreeView(NonModalDialog):
225          # of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at          # of doing it here. (EVT_WINDOW_DESTROY doesn't seem to sent at
226          # all)          # all)
227          self.tree.unsubscribe_all()          self.tree.unsubscribe_all()
228    

Legend:
Removed from v.217  
changed lines
  Added in v.520

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26