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