/[thuban]/trunk/thuban/Thuban/UI/projlist.py
ViewVC logotype

Diff of /trunk/thuban/Thuban/UI/projlist.py

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

revision 1850 by bh, Tue Oct 21 14:13:41 2003 UTC revision 2700 by dpinte, Mon Sep 18 14:27:02 2006 UTC
# Line 11  __version__ = "$Revision$" Line 11  __version__ = "$Revision$"
11  # $Source$  # $Source$
12  # $Id$  # $Id$
13    
14  from wxPython.wx import *  import wx
15    
16  from Thuban import _  from Thuban import _
17    
# Line 24  from Thuban.Model.messages import PROJEC Line 24  from Thuban.Model.messages import PROJEC
24  PROJ_SELECTION_CHANGED = "PROJ_SELECTION_CHANGED"  PROJ_SELECTION_CHANGED = "PROJ_SELECTION_CHANGED"
25    
26    
27  class ProjectionList(wxListCtrl, Publisher):  class ProjectionList(wx.ListCtrl, Publisher):
28    
29      """A ListCtrl that shows a list of projections      """A ListCtrl that shows a list of projections
30    
31      The list control is effectively a view on two ProjFile instances and      The list control is effectively a view on several ProjFile instances
32      a specific 'original' projection (e.g. the projection of the current      and a specific 'original' projection (e.g. the projection of the
33      map). The list control subscribes to the change messages of the      current map). The list control subscribes to the change messages of
34      ProjFile instances to update the list whenever they change.      the ProjFile instances to update the list whenever they change.
35    
36      When the selection changes the instance sends a      When the selection changes the instance sends a
37      PROJ_SELECTION_CHANGED message.      PROJ_SELECTION_CHANGED message.
38      """      """
39    
40      def __init__(self, parent, system_projs, user_projs, orig_proj, ID = -1):      def __init__(self, parent, proj_files, orig_proj, ID = -1):
41          """Initialize the ProjectionList          """Initialize the ProjectionList
42    
43          Parameters:          Parameters:
44    
45          parent -- The parent widget          parent -- The parent widget
46          system_projs, user_projs -- the system and user ProjFiles          proj_files -- a sequence of ProjFile objects
47          orig_proj -- The projection originally selected in the map/layer          orig_proj -- The projection originally selected in the map/layer
48          """          """
49          wxListCtrl.__init__(self, parent, ID, style=wxLC_REPORT|wxLC_VIRTUAL)          wx.ListCtrl.__init__(self, parent, ID, style=wx.LC_REPORT|wx.LC_VIRTUAL)
50    
51          self.InsertColumn(0, _("Available Projections"))          self.InsertColumn(0, _("Available Projections"))
52          self.system_projs = system_projs          self.proj_files = proj_files
53          self.user_projs = user_projs          self._subscribe_proj_files()
54          self.orig_proj = orig_proj          self.orig_proj = orig_proj
55          self.projections = []          self.projections = []
56          self.proj_map = {}          self.proj_map = {}
57          self.needs_update = False          self.needs_update = False
58          self.update_projections()          self.update_projections()
59          EVT_SIZE(self, self.OnSize)          self.Bind(wx.EVT_SIZE, self.OnSize)
60          EVT_LEFT_UP(self, self.mouse_left_up)          self.Bind(wx.EVT_LEFT_UP, self.mouse_left_up)
61          EVT_LIST_ITEM_SELECTED(self, self.GetId(), self.item_selected)          self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.item_selected, id=self.GetId())
62          EVT_LIST_ITEM_DESELECTED(self, self.GetId(), self.item_deselected)          self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.item_deselected, id=self.GetId())
63          EVT_IDLE(self, self.OnIdle)          self.Bind(wx.EVT_IDLE, self.OnIdle)
64          for pf in (self.system_projs, self.user_projs):  
65        def __del__(self):
66            wx.ListCtrl.__del__()
67            Publisher.__del__()
68    
69        def _subscribe_proj_files(self):
70            """Subscribe to the messages of self.proj_files"""
71            for pf in self.proj_files:
72              pf.Subscribe(PROJECTION_ADDED, self.pf_projection_added)              pf.Subscribe(PROJECTION_ADDED, self.pf_projection_added)
73              pf.Subscribe(PROJECTION_REMOVED, self.pf_projection_removed)              pf.Subscribe(PROJECTION_REMOVED, self.pf_projection_removed)
74              pf.Subscribe(PROJECTION_REPLACED, self.pf_projection_replaced)              pf.Subscribe(PROJECTION_REPLACED, self.pf_projection_replaced)
75    
76      def __del__(self):      def _unsubscribe_proj_files(self):
77          wxListCtrl.__del__()          """Unsubscribe from the messages subscribed to in _subscribe_proj_files
78          Publisher.__del__()          """
79            for pf in self.proj_files:
80                pf.Unsubscribe(PROJECTION_ADDED, self.pf_projection_added)
81                pf.Unsubscribe(PROJECTION_REMOVED, self.pf_projection_removed)
82                pf.Unsubscribe(PROJECTION_REPLACED, self.pf_projection_replaced)
83    
84      def Destroy(self):      def Destroy(self):
85          wxListCtrl.Destroy()          self._unsubscribe_proj_files()
86          Publisher.Destroy()          # Call wxListCtrl's method last because afterwards self is not
87            # an instance of ProjectionList anymore as wxPython replaces
88            # self.__class__ with its dead object class
89            Publisher.Destroy(self)
90            wx.ListCtrl.Destroy(self)
91    
92      def update_on_idle(self):      def update_on_idle(self):
93          self.needs_update = True          self.needs_update = True
# Line 95  class ProjectionList(wxListCtrl, Publish Line 110  class ProjectionList(wxListCtrl, Publish
110    
111          # Build the new projection list          # Build the new projection list
112          projections = [(_("<None>"), None, None)]          projections = [(_("<None>"), None, None)]
113          for pf in (self.user_projs, self.system_projs):          for pf in self.proj_files:
114              for p in pf.GetProjections():              for p in pf.GetProjections():
115                  projections.append((p.Label(), p, pf))                  projections.append((p.Label(), p, pf))
116          if self.orig_proj is not None:          if self.orig_proj is not None:
# Line 111  class ProjectionList(wxListCtrl, Publish Line 126  class ProjectionList(wxListCtrl, Publish
126          # selected with indices higher than the new count.          # selected with indices higher than the new count.
127          if len(self.projections) < old_length:          if len(self.projections) < old_length:
128              for i in xrange(len(self.projections), old_length):              for i in xrange(len(self.projections), old_length):
129                  self.SetItemState(i, 0, wxLIST_STATE_SELECTED)                  self.SetItemState(i, 0, wx.LIST_STATE_SELECTED)
130    
131          self.SetItemCount(len(self.projections))          self.SetItemCount(len(self.projections))
132    
# Line 121  class ProjectionList(wxListCtrl, Publish Line 136  class ProjectionList(wxListCtrl, Publish
136          for i in xrange(len(self.projections)):          for i in xrange(len(self.projections)):
137              p = self.projections[i][1]              p = self.projections[i][1]
138              if get(id(p)):              if get(id(p)):
139                  state = wxLIST_STATE_SELECTED                  state = wx.LIST_STATE_SELECTED
140                  count += 1                  count += 1
141              else:              else:
142                  state = 0                  state = 0
143              self.SetItemState(i, state, wxLIST_STATE_SELECTED)              self.SetItemState(i, state, wx.LIST_STATE_SELECTED)
144    
145          # If the selection changed send a PROJ_SELECTION_CHANGED message          # If the selection changed send a PROJ_SELECTION_CHANGED message
146          if count != len(selection):          if count != len(selection):
# Line 158  class ProjectionList(wxListCtrl, Publish Line 173  class ProjectionList(wxListCtrl, Publish
173    
174      def SelectProjection(self, proj):      def SelectProjection(self, proj):
175          """Select the projection and deselect all others."""          """Select the projection and deselect all others."""
176            # Set both the wxLIST_STATE_SELECTED and wxLIST_STATE_FOCUSED
177            # flags on the newly selected item. If only
178            # wxLIST_STATE_SELECTED is set, some other item is focused and
179            # the first time the focus is moved with the keyboard the
180            # selection moves in unexpected ways.
181            state = wx.LIST_STATE_SELECTED|wx.LIST_STATE_FOCUSED
182          for i in range(len(self.projections)):          for i in range(len(self.projections)):
183              p = self.projections[i][1]              p = self.projections[i][1]
184              self.SetItemState(i, p is proj and wxLIST_STATE_SELECTED or 0,              self.SetItemState(i, p is proj and state or 0, state)
                               wxLIST_STATE_SELECTED)  
185    
186      def ClearSelection(self):      def ClearSelection(self):
187          """Deselect all projections."""          """Deselect all projections."""
188          for i in range(len(self.projections)):          for i in range(len(self.projections)):
189              self.SetItemState(i, 0, wxLIST_STATE_SELECTED)              self.SetItemState(i, 0, wx.LIST_STATE_SELECTED)
190    
191        def SetProjFiles(self, proj_files):
192            """Set the projfile objects whose projections are shown in the list"""
193            self._unsubscribe_proj_files()
194            self.proj_files = proj_files
195            self._subscribe_proj_files()
196            self.update_projections()
197    
198      def OnGetItemText(self, item, col):      def OnGetItemText(self, item, col):
199          """Callback for the virtual ListCtrl mode"""          """Callback for the virtual ListCtrl mode"""
# Line 190  class ProjectionList(wxListCtrl, Publish Line 217  class ProjectionList(wxListCtrl, Publish
217          """          """
218          return [self.projections[i][1:]          return [self.projections[i][1:]
219                      for i in range(len(self.projections))                      for i in range(len(self.projections))
220                          if self.GetItemState(i, wxLIST_STATE_SELECTED)]                          if self.GetItemState(i, wx.LIST_STATE_SELECTED)]
221    
222      def _issue_proj_selection_changed(self):      def _issue_proj_selection_changed(self):
223          """Internal: Issue a PROJ_SELECTION_CHANGED message"""          """Internal: Issue a PROJ_SELECTION_CHANGED message"""

Legend:
Removed from v.1850  
changed lines
  Added in v.2700

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26