11 |
# $Source$ |
# $Source$ |
12 |
# $Id$ |
# $Id$ |
13 |
|
|
14 |
from wxPython.wx import * |
import wx |
15 |
|
|
16 |
from Thuban import _ |
from Thuban import _ |
17 |
|
|
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 |
|
Publisher.__del__(self) |
67 |
|
|
68 |
|
def _subscribe_proj_files(self): |
69 |
|
"""Subscribe to the messages of self.proj_files""" |
70 |
|
for pf in self.proj_files: |
71 |
pf.Subscribe(PROJECTION_ADDED, self.pf_projection_added) |
pf.Subscribe(PROJECTION_ADDED, self.pf_projection_added) |
72 |
pf.Subscribe(PROJECTION_REMOVED, self.pf_projection_removed) |
pf.Subscribe(PROJECTION_REMOVED, self.pf_projection_removed) |
73 |
pf.Subscribe(PROJECTION_REPLACED, self.pf_projection_replaced) |
pf.Subscribe(PROJECTION_REPLACED, self.pf_projection_replaced) |
74 |
|
|
75 |
def __del__(self): |
def _unsubscribe_proj_files(self): |
76 |
wxListCtrl.__del__() |
"""Unsubscribe from the messages subscribed to in _subscribe_proj_files |
77 |
Publisher.__del__() |
""" |
78 |
|
for pf in self.proj_files: |
79 |
|
pf.Unsubscribe(PROJECTION_ADDED, self.pf_projection_added) |
80 |
|
pf.Unsubscribe(PROJECTION_REMOVED, self.pf_projection_removed) |
81 |
|
pf.Unsubscribe(PROJECTION_REPLACED, self.pf_projection_replaced) |
82 |
|
|
83 |
def Destroy(self): |
def Destroy(self): |
84 |
wxListCtrl.Destroy() |
self._unsubscribe_proj_files() |
85 |
Publisher.Destroy() |
# Call wxListCtrl's method last because afterwards self is not |
86 |
|
# an instance of ProjectionList anymore as wxPython replaces |
87 |
|
# self.__class__ with its dead object class |
88 |
|
Publisher.Destroy(self) |
89 |
|
wx.ListCtrl.Destroy(self) |
90 |
|
|
91 |
def update_on_idle(self): |
def update_on_idle(self): |
92 |
self.needs_update = True |
self.needs_update = True |
109 |
|
|
110 |
# Build the new projection list |
# Build the new projection list |
111 |
projections = [(_("<None>"), None, None)] |
projections = [(_("<None>"), None, None)] |
112 |
for pf in (self.user_projs, self.system_projs): |
for pf in self.proj_files: |
113 |
for p in pf.GetProjections(): |
for p in pf.GetProjections(): |
114 |
projections.append((p.Label(), p, pf)) |
projections.append((p.Label(), p, pf)) |
115 |
if self.orig_proj is not None: |
if self.orig_proj is not None: |
125 |
# selected with indices higher than the new count. |
# selected with indices higher than the new count. |
126 |
if len(self.projections) < old_length: |
if len(self.projections) < old_length: |
127 |
for i in xrange(len(self.projections), old_length): |
for i in xrange(len(self.projections), old_length): |
128 |
self.SetItemState(i, 0, wxLIST_STATE_SELECTED) |
self.SetItemState(i, 0, wx.LIST_STATE_SELECTED) |
129 |
|
|
130 |
self.SetItemCount(len(self.projections)) |
self.SetItemCount(len(self.projections)) |
131 |
|
|
135 |
for i in xrange(len(self.projections)): |
for i in xrange(len(self.projections)): |
136 |
p = self.projections[i][1] |
p = self.projections[i][1] |
137 |
if get(id(p)): |
if get(id(p)): |
138 |
state = wxLIST_STATE_SELECTED |
state = wx.LIST_STATE_SELECTED |
139 |
count += 1 |
count += 1 |
140 |
else: |
else: |
141 |
state = 0 |
state = 0 |
142 |
self.SetItemState(i, state, wxLIST_STATE_SELECTED) |
self.SetItemState(i, state, wx.LIST_STATE_SELECTED) |
143 |
|
|
144 |
# If the selection changed send a PROJ_SELECTION_CHANGED message |
# If the selection changed send a PROJ_SELECTION_CHANGED message |
145 |
if count != len(selection): |
if count != len(selection): |
172 |
|
|
173 |
def SelectProjection(self, proj): |
def SelectProjection(self, proj): |
174 |
"""Select the projection and deselect all others.""" |
"""Select the projection and deselect all others.""" |
175 |
|
# Set both the wxLIST_STATE_SELECTED and wxLIST_STATE_FOCUSED |
176 |
|
# flags on the newly selected item. If only |
177 |
|
# wxLIST_STATE_SELECTED is set, some other item is focused and |
178 |
|
# the first time the focus is moved with the keyboard the |
179 |
|
# selection moves in unexpected ways. |
180 |
|
state = wx.LIST_STATE_SELECTED|wx.LIST_STATE_FOCUSED |
181 |
for i in range(len(self.projections)): |
for i in range(len(self.projections)): |
182 |
p = self.projections[i][1] |
p = self.projections[i][1] |
183 |
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) |
|
184 |
|
|
185 |
def ClearSelection(self): |
def ClearSelection(self): |
186 |
"""Deselect all projections.""" |
"""Deselect all projections.""" |
187 |
for i in range(len(self.projections)): |
for i in range(len(self.projections)): |
188 |
self.SetItemState(i, 0, wxLIST_STATE_SELECTED) |
self.SetItemState(i, 0, wx.LIST_STATE_SELECTED) |
189 |
|
|
190 |
|
def SetProjFiles(self, proj_files): |
191 |
|
"""Set the projfile objects whose projections are shown in the list""" |
192 |
|
self._unsubscribe_proj_files() |
193 |
|
self.proj_files = proj_files |
194 |
|
self._subscribe_proj_files() |
195 |
|
self.update_projections() |
196 |
|
|
197 |
def OnGetItemText(self, item, col): |
def OnGetItemText(self, item, col): |
198 |
"""Callback for the virtual ListCtrl mode""" |
"""Callback for the virtual ListCtrl mode""" |
216 |
""" |
""" |
217 |
return [self.projections[i][1:] |
return [self.projections[i][1:] |
218 |
for i in range(len(self.projections)) |
for i in range(len(self.projections)) |
219 |
if self.GetItemState(i, wxLIST_STATE_SELECTED)] |
if self.GetItemState(i, wx.LIST_STATE_SELECTED)] |
220 |
|
|
221 |
def _issue_proj_selection_changed(self): |
def _issue_proj_selection_changed(self): |
222 |
"""Internal: Issue a PROJ_SELECTION_CHANGED message""" |
"""Internal: Issue a PROJ_SELECTION_CHANGED message""" |