1 |
jonathan |
542 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jonathan Coles <[email protected]> |
4 |
frank |
914 |
# Frank Koormann <[email protected]> |
5 |
jonathan |
542 |
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
|
9 |
|
|
__version__ = "$Revision$" |
10 |
|
|
|
11 |
|
|
from Thuban import _ |
12 |
|
|
|
13 |
jonathan |
652 |
import resource |
14 |
|
|
|
15 |
jonathan |
542 |
from wxPython.wx import * |
16 |
|
|
|
17 |
jonathan |
936 |
from Thuban.Model.layer import BaseLayer |
18 |
jonathan |
542 |
from Thuban.Model.map import Map |
19 |
|
|
from Thuban.Model.classification import ClassGroup |
20 |
|
|
|
21 |
jonathan |
882 |
from Thuban.Model.messages import \ |
22 |
|
|
MAP_STACKING_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED, LAYER_CHANGED,\ |
23 |
|
|
LAYER_VISIBILITY_CHANGED, TITLE_CHANGED |
24 |
|
|
|
25 |
|
|
from Thuban.UI.messages import SCALE_CHANGED |
26 |
|
|
|
27 |
jonathan |
542 |
from Thuban.UI.classifier import ClassDataPreviewer |
28 |
jonathan |
562 |
from Thuban.UI.dock import DockPanel |
29 |
frank |
864 |
from Thuban.UI.scalebar import ScaleBar |
30 |
jonathan |
542 |
|
31 |
jonathan |
572 |
from Thuban.Lib.connector import ConnectorError |
32 |
|
|
|
33 |
frank |
990 |
ID_LEGEND_TOP = 4001 |
34 |
|
|
ID_LEGEND_RAISE = 4002 |
35 |
|
|
ID_LEGEND_LOWER = 4003 |
36 |
|
|
ID_LEGEND_BOTTOM = 4004 |
37 |
|
|
ID_LEGEND_TREE = 4005 |
38 |
|
|
ID_LEGEND_PROPS = 4006 |
39 |
|
|
ID_LEGEND_SHOWLAYER = 4007 |
40 |
|
|
ID_LEGEND_HIDELAYER = 4008 |
41 |
jonathan |
542 |
|
42 |
frank |
1050 |
BMP_SIZE_W = 15 |
43 |
jonathan |
542 |
BMP_SIZE_H = 15 |
44 |
|
|
|
45 |
frank |
990 |
TOP_BMP = "top_layer" |
46 |
jonathan |
652 |
RAISE_BMP = "raise_layer" |
47 |
|
|
LOWER_BMP = "lower_layer" |
48 |
frank |
990 |
BOTTOM_BMP = "bottom_layer" |
49 |
jonathan |
652 |
SHOW_BMP = "show_layer" |
50 |
|
|
HIDE_BMP = "hide_layer" |
51 |
|
|
PROPS_BMP = "layer_properties" |
52 |
|
|
|
53 |
|
|
|
54 |
jonathan |
562 |
class LegendPanel(DockPanel): |
55 |
|
|
|
56 |
|
|
def __init__(self, parent, map, mainWindow): |
57 |
|
|
DockPanel.__init__(self, parent, -1) |
58 |
|
|
|
59 |
|
|
self.mainWindow = mainWindow |
60 |
|
|
self.parent = parent |
61 |
|
|
|
62 |
jonathan |
652 |
self.buttons = [] |
63 |
|
|
|
64 |
jonathan |
542 |
panelBox = wxBoxSizer(wxVERTICAL) |
65 |
|
|
|
66 |
jonathan |
652 |
self.toolBar = wxToolBar(self, -1) |
67 |
jonathan |
658 |
self.toolBar.SetToolBitmapSize(wxSize(24, 24)) |
68 |
jonathan |
542 |
|
69 |
frank |
990 |
bmp = resource.GetBitmapResource(TOP_BMP, wxBITMAP_TYPE_XPM) |
70 |
|
|
self.toolBar.AddTool(ID_LEGEND_TOP, bmp, |
71 |
|
|
shortHelpString=_("Top Layer")) |
72 |
|
|
|
73 |
jonathan |
652 |
bmp = resource.GetBitmapResource(RAISE_BMP, wxBITMAP_TYPE_XPM) |
74 |
|
|
self.toolBar.AddTool(ID_LEGEND_RAISE, bmp, |
75 |
|
|
shortHelpString=_("Raise Layer")) |
76 |
jonathan |
542 |
|
77 |
jonathan |
652 |
bmp = resource.GetBitmapResource(LOWER_BMP, wxBITMAP_TYPE_XPM) |
78 |
|
|
self.toolBar.AddTool(ID_LEGEND_LOWER, bmp, |
79 |
|
|
shortHelpString=_("Lower Layer")) |
80 |
jonathan |
542 |
|
81 |
frank |
990 |
bmp = resource.GetBitmapResource(BOTTOM_BMP, wxBITMAP_TYPE_XPM) |
82 |
|
|
self.toolBar.AddTool(ID_LEGEND_BOTTOM, bmp, |
83 |
|
|
shortHelpString=_("Bottom Layer")) |
84 |
|
|
|
85 |
jonathan |
652 |
bmp = resource.GetBitmapResource(SHOW_BMP, wxBITMAP_TYPE_XPM) |
86 |
|
|
self.toolBar.AddTool(ID_LEGEND_SHOWLAYER, bmp, |
87 |
|
|
shortHelpString=_("Show Layer")) |
88 |
jonathan |
542 |
|
89 |
jonathan |
652 |
bmp = resource.GetBitmapResource(HIDE_BMP, wxBITMAP_TYPE_XPM) |
90 |
|
|
self.toolBar.AddTool(ID_LEGEND_HIDELAYER, bmp, |
91 |
|
|
shortHelpString=_("Hide Layer")) |
92 |
jonathan |
542 |
|
93 |
jonathan |
652 |
bmp = resource.GetBitmapResource(PROPS_BMP, wxBITMAP_TYPE_XPM) |
94 |
|
|
self.toolBar.AddTool(ID_LEGEND_PROPS, bmp, |
95 |
|
|
shortHelpString=_("Edit Layer Properties")) |
96 |
jonathan |
542 |
|
97 |
jonathan |
652 |
self.toolBar.Realize() |
98 |
jonathan |
668 |
panelBox.Add(self.toolBar, 0, wxGROW, 0) |
99 |
jonathan |
542 |
|
100 |
frank |
990 |
EVT_TOOL(self, ID_LEGEND_TOP, self._OnMoveTop) |
101 |
jonathan |
652 |
EVT_TOOL(self, ID_LEGEND_RAISE, self._OnMoveUp) |
102 |
|
|
EVT_TOOL(self, ID_LEGEND_LOWER, self._OnMoveDown) |
103 |
frank |
990 |
EVT_TOOL(self, ID_LEGEND_BOTTOM, self._OnMoveBottom) |
104 |
jonathan |
652 |
EVT_TOOL(self, ID_LEGEND_PROPS, self._OnProperties) |
105 |
|
|
EVT_TOOL(self, ID_LEGEND_SHOWLAYER, self._OnShowLayer) |
106 |
|
|
EVT_TOOL(self, ID_LEGEND_HIDELAYER, self._OnHideLayer) |
107 |
jonathan |
542 |
|
108 |
jonathan |
562 |
self.tree = LegendTree(self, ID_LEGEND_TREE, map, mainWindow) |
109 |
jonathan |
542 |
|
110 |
jonathan |
658 |
panelBox.Add(self.tree, 1, wxGROW, 0) |
111 |
jonathan |
542 |
|
112 |
frank |
854 |
self.scalebarbitmap = ScaleBarBitmap(self, map, mainWindow) |
113 |
|
|
panelBox.Add(self.scalebarbitmap, 0, wxGROW, 0) |
114 |
|
|
|
115 |
jonathan |
562 |
self.SetAutoLayout(True) |
116 |
|
|
self.SetSizer(panelBox) |
117 |
jonathan |
542 |
panelBox.SetSizeHints(self) |
118 |
|
|
|
119 |
jonathan |
658 |
|
120 |
jonathan |
562 |
self.panelBox = panelBox |
121 |
jonathan |
542 |
|
122 |
jonathan |
652 |
self.__EnableButtons(False) |
123 |
|
|
|
124 |
jonathan |
658 |
self.Create() |
125 |
|
|
|
126 |
jonathan |
572 |
EVT_CLOSE(self, self._OnClose) |
127 |
|
|
|
128 |
|
|
|
129 |
jonathan |
562 |
def GetMap(self): |
130 |
|
|
return self.tree.GetMap() |
131 |
|
|
|
132 |
|
|
def SetMap(self, map): |
133 |
|
|
self.tree.SetMap(map) |
134 |
frank |
854 |
self.scalebarbitmap.SetCanvas(self.mainWindow.canvas) |
135 |
jonathan |
562 |
|
136 |
jonathan |
572 |
def DoOnSelChanged(self, layer, group): |
137 |
jonathan |
542 |
|
138 |
jonathan |
936 |
ok = isinstance(layer, BaseLayer) |
139 |
jonathan |
572 |
self.__EnableButtons(ok) |
140 |
|
|
|
141 |
jonathan |
655 |
self.mainWindow.SelectLayer(layer) |
142 |
jonathan |
568 |
|
143 |
jonathan |
639 |
def DoOnProperties(self): |
144 |
jonathan |
572 |
list = self.tree.GetSelectedHierarchy() |
145 |
|
|
|
146 |
jonathan |
936 |
ok = isinstance(list[0], BaseLayer) |
147 |
jonathan |
572 |
if ok: |
148 |
jonathan |
639 |
self.mainWindow.OpenLayerProperties(list[0], list[1]) |
149 |
jonathan |
572 |
|
150 |
jonathan |
578 |
def Destroy(self): |
151 |
|
|
self.__Close() |
152 |
|
|
|
153 |
jonathan |
639 |
def _OnProperties(self, event): |
154 |
|
|
self.DoOnProperties() |
155 |
jonathan |
542 |
|
156 |
frank |
990 |
def _OnMoveTop(self, event): |
157 |
|
|
self.tree.MoveCurrentItemTop() |
158 |
|
|
|
159 |
jonathan |
542 |
def _OnMoveUp(self, event): |
160 |
|
|
self.tree.MoveCurrentItemUp() |
161 |
|
|
|
162 |
|
|
def _OnMoveDown(self, event): |
163 |
|
|
self.tree.MoveCurrentItemDown() |
164 |
|
|
|
165 |
frank |
990 |
def _OnMoveBottom(self, event): |
166 |
|
|
self.tree.MoveCurrentItemBottom() |
167 |
|
|
|
168 |
jonathan |
542 |
def _OnShowLayer(self, event): |
169 |
|
|
self.tree.DoOnShowLayer() |
170 |
|
|
pass |
171 |
|
|
|
172 |
jonathan |
578 |
#def Close(self, force = False): |
173 |
|
|
#DockPanel.Close(self, force) |
174 |
|
|
|
175 |
jonathan |
572 |
def _OnClose(self, event): |
176 |
jonathan |
578 |
self.__Close() |
177 |
jonathan |
572 |
|
178 |
jonathan |
542 |
def _OnHideLayer(self, event): |
179 |
|
|
self.tree.DoOnHideLayer() |
180 |
|
|
pass |
181 |
|
|
|
182 |
|
|
def __EnableButtons(self, on): |
183 |
frank |
990 |
self.toolBar.EnableTool(ID_LEGEND_TOP, on) |
184 |
jonathan |
652 |
self.toolBar.EnableTool(ID_LEGEND_RAISE, on) |
185 |
|
|
self.toolBar.EnableTool(ID_LEGEND_LOWER, on) |
186 |
frank |
990 |
self.toolBar.EnableTool(ID_LEGEND_BOTTOM, on) |
187 |
jonathan |
652 |
self.toolBar.EnableTool(ID_LEGEND_SHOWLAYER, on) |
188 |
|
|
self.toolBar.EnableTool(ID_LEGEND_HIDELAYER, on) |
189 |
|
|
self.toolBar.EnableTool(ID_LEGEND_PROPS, on) |
190 |
jonathan |
542 |
|
191 |
jonathan |
578 |
def __Close(self): |
192 |
|
|
self.tree.Close() |
193 |
|
|
|
194 |
jonathan |
542 |
class LegendTree(wxTreeCtrl): |
195 |
|
|
|
196 |
jonathan |
562 |
def __init__(self, parent, id, map, mainWindow): |
197 |
jonathan |
542 |
wxTreeCtrl.__init__(self, parent, id, |
198 |
|
|
style = wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT, |
199 |
|
|
size = (200, 200)) |
200 |
|
|
|
201 |
jonathan |
572 |
self.mainWindow = mainWindow |
202 |
jonathan |
562 |
self.map = None |
203 |
jonathan |
542 |
self.parent = parent |
204 |
bh |
1113 |
self.changing_selection = 0 |
205 |
jonathan |
542 |
|
206 |
jonathan |
1102 |
# |
207 |
|
|
# The image list used by the wxTreeCtrl causes problems when |
208 |
|
|
# we remove layers and/or change a classification because it |
209 |
|
|
# changes the image indices if you remove images from the list. |
210 |
|
|
# Rather than removing unused images we use this list to keep |
211 |
|
|
# track of which indices are available in the image list |
212 |
|
|
# (because of a previous removal) and then replace those indices |
213 |
|
|
# with new images rather than appending to the end of the image |
214 |
|
|
# list (assuming there are any that are available). |
215 |
|
|
# |
216 |
|
|
self.availImgListIndices = [] |
217 |
|
|
|
218 |
jonathan |
542 |
self.image_list = None |
219 |
|
|
self.emptyImageIndex = 0 |
220 |
|
|
|
221 |
|
|
self.previewer = ClassDataPreviewer() |
222 |
|
|
|
223 |
jonathan |
1211 |
self.preventExpandCollapse = False |
224 |
|
|
|
225 |
jonathan |
542 |
EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated) |
226 |
|
|
EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged) |
227 |
jonathan |
1211 |
EVT_TREE_ITEM_EXPANDING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
228 |
|
|
EVT_TREE_ITEM_COLLAPSING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
229 |
jonathan |
542 |
|
230 |
jonathan |
572 |
EVT_CLOSE(self, self._OnClose) |
231 |
|
|
|
232 |
jonathan |
562 |
self.SetMap(map) |
233 |
jonathan |
542 |
|
234 |
bh |
1129 |
def find_layer(self, layer): |
235 |
|
|
"""Return the tree item for the layer""" |
236 |
|
|
root = self.GetRootItem() |
237 |
|
|
id, cookie = self.GetFirstChild(root, 0) |
238 |
|
|
while id.IsOk(): |
239 |
|
|
if self.GetPyData(id) is layer: |
240 |
|
|
return id |
241 |
|
|
id, cookie = self.GetNextChild(root, cookie) |
242 |
|
|
return None |
243 |
|
|
|
244 |
jonathan |
572 |
def _OnClose(self, event): |
245 |
|
|
self.SetMap(None) |
246 |
|
|
|
247 |
jonathan |
562 |
def GetMap(self): |
248 |
|
|
return self.map |
249 |
jonathan |
542 |
|
250 |
jonathan |
562 |
def SetMap(self, map): |
251 |
|
|
|
252 |
|
|
sub_list = [(MAP_STACKING_CHANGED, self._OnMsgMapStackingChanged), |
253 |
jonathan |
1102 |
(MAP_LAYERS_ADDED, self._OnMsgMapLayersAdded), |
254 |
|
|
(MAP_LAYERS_REMOVED, self._OnMsgMapLayersRemoved)] |
255 |
jonathan |
562 |
|
256 |
|
|
if self.map is not None: |
257 |
|
|
for msg, func in sub_list: self.map.Unsubscribe(msg, func) |
258 |
jonathan |
572 |
#self.mainWindow.application.Unsubscribe(SESSION_REPLACED, |
259 |
|
|
#self._OnMsgMapsChanged) |
260 |
|
|
#try: |
261 |
|
|
#self.mainWindow.application.session.Unsubscribe(MAPS_CHANGED, |
262 |
|
|
#self._OnMsgMapsChanged) |
263 |
|
|
#except ConnectorError: |
264 |
|
|
#pass |
265 |
jonathan |
1102 |
self.DeleteAllItems() |
266 |
jonathan |
562 |
|
267 |
|
|
self.map = map |
268 |
|
|
|
269 |
|
|
if self.map is not None: |
270 |
|
|
for msg, func in sub_list: self.map.Subscribe(msg, func) |
271 |
jonathan |
572 |
#self.mainWindow.application.session.Subscribe(MAPS_CHANGED, |
272 |
|
|
#self._OnMsgMapsChanged) |
273 |
|
|
#self.mainWindow.application.Subscribe(SESSION_REPLACED, |
274 |
|
|
#self._OnMsgMapsChanged) |
275 |
jonathan |
562 |
self.__FillTree(self.map) |
276 |
|
|
|
277 |
frank |
990 |
def MoveCurrentItemTop(self): |
278 |
|
|
layer, group = self.GetSelectedHierarchy() |
279 |
|
|
|
280 |
|
|
if layer is not None: |
281 |
jonathan |
1102 |
self.map.MoveLayerToTop(layer) |
282 |
frank |
990 |
else: |
283 |
|
|
assert False, "Shouldn't be allowed." |
284 |
|
|
pass |
285 |
|
|
|
286 |
jonathan |
542 |
def MoveCurrentItemUp(self): |
287 |
jonathan |
795 |
layer, group = self.GetSelectedHierarchy() |
288 |
jonathan |
542 |
|
289 |
jonathan |
795 |
if layer is not None: |
290 |
|
|
self.map.RaiseLayer(layer) |
291 |
jonathan |
542 |
else: |
292 |
jonathan |
795 |
assert False, "Shouldn't be allowed." |
293 |
jonathan |
542 |
pass |
294 |
|
|
|
295 |
|
|
def MoveCurrentItemDown(self): |
296 |
jonathan |
795 |
layer, group = self.GetSelectedHierarchy() |
297 |
jonathan |
542 |
|
298 |
jonathan |
795 |
if layer is not None: |
299 |
|
|
self.map.LowerLayer(layer) |
300 |
jonathan |
542 |
else: |
301 |
jonathan |
795 |
assert False, "Shouldn't be allowed." |
302 |
jonathan |
542 |
pass |
303 |
|
|
|
304 |
frank |
990 |
def MoveCurrentItemBottom(self): |
305 |
|
|
layer, group = self.GetSelectedHierarchy() |
306 |
|
|
|
307 |
|
|
if layer is not None: |
308 |
jonathan |
1102 |
self.map.MoveLayerToBottom(layer) |
309 |
frank |
990 |
else: |
310 |
|
|
assert False, "Shouldn't be allowed." |
311 |
|
|
pass |
312 |
|
|
|
313 |
jonathan |
542 |
def OnCompareItems(self, item1, item2): |
314 |
|
|
|
315 |
|
|
data1 = self.GetPyData(item1) |
316 |
|
|
data2 = self.GetPyData(item2) |
317 |
|
|
|
318 |
jonathan |
936 |
if isinstance(data1, BaseLayer): |
319 |
jonathan |
542 |
layers = self.map.Layers() |
320 |
|
|
return layers.index(data2) - layers.index(data1) |
321 |
|
|
else: |
322 |
|
|
return wxTreeCtrl.OnCompareItems(self, item1, item2) |
323 |
|
|
|
324 |
|
|
def DoOnShowLayer(self): |
325 |
jonathan |
572 |
layer, group = self.GetSelectedHierarchy() |
326 |
|
|
layer.SetVisible(True) |
327 |
jonathan |
542 |
|
328 |
|
|
def DoOnHideLayer(self): |
329 |
jonathan |
572 |
layer, group = self.GetSelectedHierarchy() |
330 |
|
|
layer.SetVisible(False) |
331 |
jonathan |
542 |
|
332 |
|
|
def Sort(self): |
333 |
|
|
self.SortChildren(self.GetRootItem()) |
334 |
|
|
|
335 |
jonathan |
655 |
def GetSelectedHierarchy(self): |
336 |
|
|
id = self.GetSelection() |
337 |
|
|
|
338 |
|
|
if not id.IsOk(): |
339 |
|
|
return (None, None) |
340 |
|
|
|
341 |
|
|
layer = self.GetPyData(id) |
342 |
|
|
group = None |
343 |
|
|
|
344 |
|
|
if isinstance(layer, ClassGroup): |
345 |
|
|
id = self.GetItemParent(id) |
346 |
|
|
assert id.IsOk() |
347 |
|
|
group = layer |
348 |
|
|
layer = self.GetPyData(id) |
349 |
|
|
|
350 |
|
|
return (layer, group) |
351 |
|
|
|
352 |
jonathan |
572 |
def _OnMsgMapsChanged(self): |
353 |
jonathan |
639 |
#print self.map is self.mainWindow.Map() |
354 |
jonathan |
572 |
self.SetMap(self.mainWindow.Map()) |
355 |
|
|
|
356 |
jonathan |
542 |
def _OnSelChanged(self, event): |
357 |
bh |
1113 |
# If we change the selection from normalize_selection do nothing. |
358 |
|
|
if self.changing_selection: |
359 |
|
|
return |
360 |
|
|
|
361 |
|
|
self.normalize_selection() |
362 |
jonathan |
655 |
self.__UpdateSelection() |
363 |
jonathan |
542 |
|
364 |
bh |
1113 |
def normalize_selection(self): |
365 |
|
|
"""Select the layer containing currently selected item""" |
366 |
|
|
# This is really a workaround for a bug in wx where deleting a |
367 |
|
|
# subtree with DeleteChildren does not update the selection |
368 |
|
|
# properly and can lead to segfaults later because the return |
369 |
|
|
# value of GetSelection points to invalid data. |
370 |
|
|
item = self.GetSelection() |
371 |
|
|
while item.IsOk(): |
372 |
|
|
object = self.GetPyData(item) |
373 |
|
|
if isinstance(object, BaseLayer): |
374 |
|
|
break |
375 |
|
|
item = self.GetItemParent(item) |
376 |
|
|
else: |
377 |
|
|
# No layer was found in the chain of parents, so there's |
378 |
|
|
# nothing we can do. |
379 |
|
|
return |
380 |
|
|
|
381 |
|
|
self.changing_selection = 1 |
382 |
|
|
try: |
383 |
|
|
self.SelectItem(item) |
384 |
|
|
finally: |
385 |
|
|
self.changing_selection = 0 |
386 |
|
|
|
387 |
|
|
|
388 |
jonathan |
1211 |
def OnItemExpandCollapse(self, event): |
389 |
|
|
if self.preventExpandCollapse: |
390 |
|
|
event.Veto() |
391 |
|
|
self.preventExpandCollapse = False |
392 |
|
|
|
393 |
jonathan |
542 |
def _OnItemActivated(self, event): |
394 |
jonathan |
1211 |
self.preventExpandCollapse = True |
395 |
jonathan |
639 |
self.parent.DoOnProperties() |
396 |
jonathan |
542 |
|
397 |
|
|
def _OnMsgLayerChanged(self, layer): |
398 |
jonathan |
936 |
assert isinstance(layer, BaseLayer) |
399 |
jonathan |
542 |
|
400 |
bh |
1129 |
id = self.find_layer(layer) |
401 |
|
|
assert id is not None |
402 |
jonathan |
542 |
|
403 |
jonathan |
1102 |
self.__FillTreeLayer(id) |
404 |
jonathan |
655 |
self.__UpdateSelection() |
405 |
jonathan |
542 |
|
406 |
|
|
def _OnMsgMapStackingChanged(self, *args): |
407 |
|
|
self.Sort() |
408 |
jonathan |
562 |
id = self.GetSelection() |
409 |
jonathan |
542 |
|
410 |
jonathan |
562 |
if id.IsOk(): |
411 |
|
|
self.EnsureVisible(id) |
412 |
jonathan |
655 |
self.__UpdateSelection() |
413 |
jonathan |
562 |
|
414 |
jonathan |
1102 |
def _OnMsgMapLayersAdded(self, map): |
415 |
jonathan |
605 |
assert map is self.map |
416 |
jonathan |
542 |
|
417 |
bh |
1129 |
# Build a dict with all layers known by the the tree as keys |
418 |
|
|
layers = {} |
419 |
jonathan |
1102 |
root = self.GetRootItem() |
420 |
bh |
1129 |
id, cookie = self.GetFirstChild(root, 0) |
421 |
|
|
while id.IsOk(): |
422 |
|
|
layers[self.GetPyData(id)] = 1 |
423 |
|
|
id, cookie = self.GetNextChild(root, cookie) |
424 |
jonathan |
1102 |
|
425 |
bh |
1129 |
# Add layers in the map but not in the dict |
426 |
jonathan |
1102 |
i = 0 |
427 |
|
|
for l in map.Layers(): |
428 |
bh |
1129 |
if not l in layers: |
429 |
jonathan |
1102 |
self.__AddLayer(i, l) |
430 |
|
|
|
431 |
jonathan |
655 |
self.__UpdateSelection() |
432 |
jonathan |
542 |
|
433 |
jonathan |
1102 |
def _OnMsgMapLayersRemoved(self, map): |
434 |
|
|
assert map is self.map |
435 |
|
|
|
436 |
|
|
layers = map.Layers() |
437 |
|
|
|
438 |
bh |
1129 |
root = self.GetRootItem() |
439 |
|
|
id, cookie = self.GetFirstChild(root, 0) |
440 |
|
|
while id.IsOk(): |
441 |
|
|
if self.GetPyData(id) not in layers: |
442 |
|
|
self.__RemoveLayer(id) |
443 |
|
|
id, cookie = self.GetNextChild(root, cookie) |
444 |
jonathan |
1102 |
|
445 |
bh |
1129 |
|
446 |
jonathan |
1102 |
self.__UpdateSelection() |
447 |
|
|
|
448 |
jonathan |
572 |
def _OnMsgLayerVisibilityChanged(self, layer): |
449 |
jonathan |
936 |
assert isinstance(layer, BaseLayer) |
450 |
jonathan |
572 |
|
451 |
|
|
self.__ShowHideLayer(layer) |
452 |
jonathan |
655 |
self.__UpdateSelection() |
453 |
jonathan |
572 |
|
454 |
jonathan |
652 |
def _OnMsgLayerTitleChanged(self, layer): |
455 |
|
|
|
456 |
bh |
1129 |
id = self.find_layer(layer) |
457 |
jonathan |
652 |
if id.IsOk(): |
458 |
|
|
self.SetItemText(id, layer.Title()) |
459 |
jonathan |
655 |
self.__UpdateSelection() |
460 |
jonathan |
652 |
|
461 |
jonathan |
655 |
def __UpdateSelection(self): |
462 |
|
|
layer, group = self.GetSelectedHierarchy() |
463 |
|
|
self.parent.DoOnSelChanged(layer, group) |
464 |
|
|
|
465 |
jonathan |
542 |
def __FillTree(self, map): |
466 |
|
|
|
467 |
|
|
self.Freeze() |
468 |
|
|
|
469 |
jonathan |
1102 |
self.DeleteAllItems() |
470 |
jonathan |
542 |
|
471 |
|
|
if map.HasLayers(): |
472 |
jonathan |
1102 |
root = self.GetRootItem() |
473 |
jonathan |
542 |
for l in map.Layers(): |
474 |
jonathan |
1102 |
self.__AddLayer(0, l) |
475 |
frank |
1050 |
|
476 |
jonathan |
542 |
self.Thaw() |
477 |
|
|
|
478 |
|
|
def __FillTreeLayer(self, pid): |
479 |
|
|
layer = self.GetPyData(pid) |
480 |
|
|
|
481 |
|
|
self.Freeze() |
482 |
|
|
|
483 |
|
|
self.DeleteChildren(pid) |
484 |
|
|
|
485 |
jonathan |
936 |
if layer.HasClassification(): |
486 |
jonathan |
542 |
|
487 |
jonathan |
936 |
clazz = layer.GetClassification() |
488 |
jonathan |
542 |
|
489 |
jonathan |
936 |
shapeType = layer.ShapeType() |
490 |
jonathan |
542 |
|
491 |
jonathan |
936 |
show = layer.Visible() |
492 |
|
|
for g in clazz: |
493 |
|
|
if g.IsVisible(): |
494 |
|
|
id = self.AppendItem(pid, g.GetDisplayText()) |
495 |
|
|
self.SetPyData(id, g) |
496 |
|
|
self.__SetVisibilityStyle(show, id) |
497 |
jonathan |
542 |
|
498 |
jonathan |
936 |
bmp = self.__BuildGroupImage(g, shapeType) |
499 |
jonathan |
542 |
|
500 |
jonathan |
936 |
if bmp is None: |
501 |
frank |
1050 |
self.SetItemImage(id, -1) |
502 |
|
|
self.SetItemSelectedImage(id, -1) |
503 |
jonathan |
936 |
else: |
504 |
jonathan |
1102 |
if self.availImgListIndices: |
505 |
|
|
i = self.availImgListIndices.pop(0) |
506 |
|
|
self.image_list.Replace(i, bmp) |
507 |
|
|
else: |
508 |
|
|
i = self.image_list.Add(bmp) |
509 |
|
|
|
510 |
jonathan |
936 |
self.SetItemImage(id, i) |
511 |
frank |
1050 |
self.SetItemSelectedImage(id, i) |
512 |
jonathan |
936 |
|
513 |
jonathan |
542 |
self.Thaw() |
514 |
|
|
|
515 |
|
|
def __BuildGroupImage(self, group, shapeType): |
516 |
|
|
|
517 |
|
|
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
518 |
|
|
#brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
519 |
|
|
dc = wxMemoryDC() |
520 |
|
|
dc.SelectObject(bmp) |
521 |
|
|
dc.Clear() |
522 |
|
|
|
523 |
|
|
self.previewer.Draw(dc, None, group.GetProperties(), shapeType) |
524 |
|
|
|
525 |
|
|
return bmp |
526 |
|
|
|
527 |
jonathan |
1102 |
def DeleteAllItems(self): |
528 |
jonathan |
542 |
|
529 |
jonathan |
1102 |
pid = self.GetRootItem() |
530 |
jonathan |
542 |
|
531 |
jonathan |
1102 |
id, cookie = self.GetFirstChild(pid, 123) |
532 |
|
|
while id.IsOk(): |
533 |
|
|
self.__RemoveLayer(id) |
534 |
|
|
id, cookie = self.GetNextChild(pid, cookie) |
535 |
jonathan |
578 |
|
536 |
jonathan |
1102 |
wxTreeCtrl.DeleteAllItems(self) |
537 |
|
|
|
538 |
|
|
def __AddLayer(self, before, l): |
539 |
|
|
root = self.GetRootItem() |
540 |
|
|
id = self.InsertItemBefore(root, before, |
541 |
|
|
l.Title(), |
542 |
|
|
self.mapImageIndex, |
543 |
|
|
self.mapImageIndex) |
544 |
|
|
|
545 |
|
|
self.SetPyData(id, l) |
546 |
|
|
self.__SetVisibilityStyle(l.Visible(), id) |
547 |
|
|
|
548 |
|
|
self.__FillTreeLayer(id) |
549 |
|
|
self.Expand(id) |
550 |
|
|
|
551 |
|
|
l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged) |
552 |
|
|
l.Subscribe(LAYER_VISIBILITY_CHANGED, |
553 |
|
|
self._OnMsgLayerVisibilityChanged) |
554 |
|
|
l.Subscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
555 |
|
|
|
556 |
|
|
def __RemoveLayer(self, id): |
557 |
|
|
self.DeleteChildren(id) |
558 |
|
|
|
559 |
|
|
layer = self.GetPyData(id) |
560 |
|
|
layer.Unsubscribe(LAYER_CHANGED, |
561 |
|
|
self._OnMsgLayerChanged) |
562 |
|
|
layer.Unsubscribe(LAYER_VISIBILITY_CHANGED, |
563 |
|
|
self._OnMsgLayerVisibilityChanged) |
564 |
|
|
layer.Unsubscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
565 |
|
|
|
566 |
|
|
self.Delete(id) |
567 |
|
|
|
568 |
|
|
def DeleteChildren(self, pid): |
569 |
|
|
id, cookie = self.GetFirstChild(pid, 123) |
570 |
|
|
while id.IsOk(): |
571 |
|
|
self.availImgListIndices.append(self.GetItemImage(id)) |
572 |
|
|
id, cookie = self.GetNextChild(pid, cookie) |
573 |
|
|
wxTreeCtrl.DeleteChildren(self, pid) |
574 |
|
|
|
575 |
|
|
def GetRootItem(self): |
576 |
|
|
root = wxTreeCtrl.GetRootItem(self) |
577 |
|
|
|
578 |
|
|
if not root.IsOk(): |
579 |
|
|
self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0) |
580 |
bh |
1115 |
|
581 |
jonathan |
1102 |
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
582 |
|
|
dc = wxMemoryDC() |
583 |
|
|
dc.SelectObject(bmp) |
584 |
|
|
dc.SetBrush(wxBLACK_BRUSH) |
585 |
|
|
dc.Clear() |
586 |
|
|
dc.SelectObject(wxNullBitmap) |
587 |
bh |
1115 |
|
588 |
jonathan |
1102 |
self.emptyImageIndex = \ |
589 |
|
|
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
590 |
bh |
1115 |
|
591 |
jonathan |
1102 |
bmp = resource.GetBitmapResource("legend_icon_layer", |
592 |
|
|
wxBITMAP_TYPE_XPM) |
593 |
|
|
self.mapImageIndex = \ |
594 |
|
|
self.image_list.Add(bmp) |
595 |
|
|
|
596 |
|
|
self.AssignImageList(self.image_list) |
597 |
bh |
1115 |
self.availImgListIndices = [] |
598 |
jonathan |
1102 |
|
599 |
|
|
root = self.AddRoot("") |
600 |
|
|
|
601 |
|
|
return root |
602 |
|
|
|
603 |
jonathan |
632 |
def __SetVisibilityStyle(self, visible, id): |
604 |
jonathan |
572 |
font = self.GetItemFont(id) |
605 |
jonathan |
542 |
|
606 |
jonathan |
632 |
if visible: |
607 |
jonathan |
572 |
font.SetStyle(wxNORMAL) |
608 |
|
|
color = wxBLACK |
609 |
|
|
else: |
610 |
frank |
980 |
#font.SetStyle(wxITALIC) |
611 |
|
|
font.SetStyle(wxNORMAL) |
612 |
jonathan |
572 |
color = wxLIGHT_GREY |
613 |
jonathan |
542 |
|
614 |
jonathan |
572 |
self.SetItemTextColour(id, color) |
615 |
|
|
self.SetItemFont(id, font) |
616 |
|
|
|
617 |
|
|
def __ShowHideLayer(self, layer): |
618 |
bh |
1129 |
parent = self.find_layer(layer) |
619 |
jonathan |
605 |
assert parent.IsOk() |
620 |
jonathan |
542 |
|
621 |
jonathan |
632 |
visible = layer.Visible() |
622 |
jonathan |
542 |
|
623 |
jonathan |
632 |
self.__SetVisibilityStyle(visible, parent) |
624 |
jonathan |
542 |
|
625 |
jonathan |
572 |
id, cookie = self.GetFirstChild(parent, 123) |
626 |
jonathan |
542 |
|
627 |
jonathan |
572 |
while id.IsOk(): |
628 |
jonathan |
632 |
self.__SetVisibilityStyle(visible, id) |
629 |
jonathan |
572 |
id, cookie = self.GetNextChild(parent, cookie) |
630 |
|
|
|
631 |
frank |
854 |
class ScaleBarBitmap(wxBoxSizer): |
632 |
|
|
|
633 |
|
|
def __init__(self, parent, map, mainWindow): |
634 |
|
|
# While the width is fixed, get the height _now_. |
635 |
|
|
dc = wxMemoryDC() |
636 |
|
|
textwidth, textheight = dc.GetTextExtent("%d"%0) |
637 |
frank |
990 |
self.width = 210 |
638 |
frank |
854 |
self.height = textheight + 3*2 + 8 |
639 |
|
|
|
640 |
|
|
wxBoxSizer.__init__(self, wxVERTICAL) |
641 |
|
|
bmp=wxEmptyBitmap(self.width, self.height) |
642 |
|
|
self.scalebarBitmap = wxStaticBitmap(parent, -1, bmp) |
643 |
|
|
self.Add(self.scalebarBitmap, 0, wxALIGN_CENTER|wxLEFT|wxTOP|wxRIGHT, 1) |
644 |
|
|
|
645 |
|
|
self.mainWindow = mainWindow |
646 |
|
|
self.parent = parent |
647 |
|
|
self.canvas = None |
648 |
|
|
self.SetCanvas(self.mainWindow.canvas) |
649 |
|
|
|
650 |
|
|
def SetCanvas(self, canvas): |
651 |
|
|
sub_list = [(SCALE_CHANGED, self._OnMsgScaleChanged)] |
652 |
|
|
|
653 |
|
|
if self.canvas is not None: |
654 |
|
|
for msg, func in sub_list: self.canvas.Unsubscribe(msg, func) |
655 |
|
|
|
656 |
|
|
self.canvas = canvas |
657 |
frank |
858 |
self.scalebar = ScaleBar(canvas.map) |
658 |
frank |
854 |
|
659 |
|
|
if self.canvas is not None: |
660 |
|
|
for msg, func in sub_list: self.canvas.Subscribe(msg, func) |
661 |
|
|
self.__SetScale(self.canvas.scale) |
662 |
|
|
|
663 |
|
|
def _OnMsgScaleChanged(self, scale): |
664 |
|
|
self.__SetScale(scale) |
665 |
|
|
|
666 |
|
|
def __SetScale(self, scale): |
667 |
|
|
bmp = wxEmptyBitmap(self.width, self.height) |
668 |
|
|
dc = wxMemoryDC() |
669 |
|
|
dc.SelectObject(bmp) |
670 |
|
|
dc.Clear() |
671 |
|
|
|
672 |
jonathan |
1231 |
if self.canvas.map is not None \ |
673 |
|
|
and self.canvas.map.projection is not None: |
674 |
jonathan |
1180 |
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
675 |
frank |
854 |
|
676 |
|
|
self.scalebarBitmap.SetBitmap(bmp) |
677 |
|
|
|