1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> |
4 |
|
# Frank Koormann <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
10 |
|
|
11 |
from Thuban import _ |
from Thuban import _ |
12 |
|
|
13 |
|
import resource |
14 |
|
|
15 |
from wxPython.wx import * |
from wxPython.wx import * |
16 |
|
|
17 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import BaseLayer |
18 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
|
from Thuban.Model.messages import * |
|
19 |
from Thuban.Model.classification import ClassGroup |
from Thuban.Model.classification import ClassGroup |
20 |
|
|
21 |
from Thuban.UI.messages import * |
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 |
from Thuban.UI.classifier import ClassDataPreviewer |
28 |
from Thuban.UI.dock import DockPanel |
from Thuban.UI.dock import DockPanel |
29 |
|
from Thuban.UI.scalebar import ScaleBar |
30 |
|
|
31 |
from Thuban.Lib.connector import ConnectorError |
from Thuban.Lib.connector import ConnectorError |
32 |
|
|
33 |
ID_LEGEND_MOVEUP = 4001 |
ID_LEGEND_TOP = 4001 |
34 |
ID_LEGEND_MOVEDOWN = 4002 |
ID_LEGEND_RAISE = 4002 |
35 |
ID_LEGEND_TREE = 4003 |
ID_LEGEND_LOWER = 4003 |
36 |
ID_LEGEND_CLASSIFY = 4004 |
ID_LEGEND_BOTTOM = 4004 |
37 |
ID_LEGEND_SHOWLAYER = 4005 |
ID_LEGEND_TREE = 4005 |
38 |
ID_LEGEND_HIDELAYER = 4006 |
ID_LEGEND_PROPS = 4006 |
39 |
|
ID_LEGEND_SHOWLAYER = 4007 |
40 |
|
ID_LEGEND_HIDELAYER = 4008 |
41 |
|
|
42 |
BMP_SIZE_W = 30 |
BMP_SIZE_W = 15 |
43 |
BMP_SIZE_H = 15 |
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): |
class LegendPanel(DockPanel): |
55 |
|
|
56 |
def __init__(self, parent, map, mainWindow): |
def __init__(self, parent, map, mainWindow): |
59 |
self.mainWindow = mainWindow |
self.mainWindow = mainWindow |
60 |
self.parent = parent |
self.parent = parent |
61 |
|
|
|
panelBox = wxBoxSizer(wxVERTICAL) |
|
|
|
|
|
buttonBox = wxGridSizer(2, 3, 0, 0) |
|
|
|
|
62 |
self.buttons = [] |
self.buttons = [] |
63 |
|
|
64 |
button = wxButton(self, ID_LEGEND_MOVEUP, _("Move Up")) |
panelBox = wxBoxSizer(wxVERTICAL) |
65 |
buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0) |
|
66 |
self.buttons.append(button) |
self.toolBar = wxToolBar(self, -1) |
67 |
|
self.toolBar.SetToolBitmapSize(wxSize(24, 24)) |
|
button = wxButton(self, ID_LEGEND_SHOWLAYER, _("Show Layer")) |
|
|
buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0) |
|
|
self.buttons.append(button) |
|
|
|
|
|
button = wxButton(self, ID_LEGEND_CLASSIFY, _("Classify")) |
|
|
buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0) |
|
|
self.buttons.append(button) |
|
|
|
|
|
button = wxButton(self, ID_LEGEND_MOVEDOWN, _("Move Down")) |
|
|
buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0) |
|
|
self.buttons.append(button) |
|
|
|
|
|
button = wxButton(self, ID_LEGEND_HIDELAYER, _("Hide Layer")) |
|
|
buttonBox.Add(button, 0, wxGROW | wxLEFT | wxRIGHT, 0) |
|
|
self.buttons.append(button) |
|
|
|
|
|
EVT_BUTTON(self, ID_LEGEND_MOVEUP, self._OnMoveUp) |
|
|
EVT_BUTTON(self, ID_LEGEND_MOVEDOWN, self._OnMoveDown) |
|
|
EVT_BUTTON(self, ID_LEGEND_CLASSIFY, self._OnClassify) |
|
|
EVT_BUTTON(self, ID_LEGEND_SHOWLAYER, self._OnShowLayer) |
|
|
EVT_BUTTON(self, ID_LEGEND_HIDELAYER, self._OnHideLayer) |
|
68 |
|
|
69 |
panelBox.Add(buttonBox, 0, 0, 4) |
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) |
self.tree = LegendTree(self, ID_LEGEND_TREE, map, mainWindow) |
109 |
|
|
110 |
panelBox.Add(self.tree, 1, wxGROW, 4) |
panelBox.Add(self.tree, 1, wxGROW, 0) |
111 |
|
|
112 |
panelBox.Fit(self) |
self.scalebarbitmap = ScaleBarBitmap(self, map, mainWindow) |
113 |
|
panelBox.Add(self.scalebarbitmap, 0, wxGROW, 0) |
114 |
|
|
115 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
116 |
self.SetSizer(panelBox) |
self.SetSizer(panelBox) |
117 |
panelBox.SetSizeHints(self) |
panelBox.SetSizeHints(self) |
118 |
|
|
|
#panelBox.SetSizeHints(self.parent) |
|
119 |
|
|
120 |
self.panelBox = panelBox |
self.panelBox = panelBox |
121 |
|
|
122 |
|
self.__EnableButtons(False) |
123 |
|
|
124 |
|
self.Create() |
125 |
|
|
126 |
EVT_CLOSE(self, self._OnClose) |
EVT_CLOSE(self, self._OnClose) |
127 |
|
|
128 |
|
|
131 |
|
|
132 |
def SetMap(self, map): |
def SetMap(self, map): |
133 |
self.tree.SetMap(map) |
self.tree.SetMap(map) |
134 |
|
self.scalebarbitmap.SetCanvas(self.mainWindow.canvas) |
135 |
|
|
136 |
def DoOnSelChanged(self, layer, group): |
def DoOnSelChanged(self, layer, group): |
137 |
|
|
138 |
ok = isinstance(layer, Layer) |
ok = isinstance(layer, BaseLayer) |
139 |
self.__EnableButtons(ok) |
self.__EnableButtons(ok) |
140 |
|
|
141 |
if ok: |
self.mainWindow.SelectLayer(layer) |
|
self.mainWindow.SelectLayer(layer) |
|
142 |
|
|
143 |
def DoOnClassify(self): |
def DoOnProperties(self): |
144 |
list = self.tree.GetSelectedHierarchy() |
list = self.tree.GetSelectedHierarchy() |
145 |
|
|
146 |
ok = isinstance(list[0], Layer) |
ok = isinstance(list[0], BaseLayer) |
147 |
if ok: |
if ok: |
148 |
self.mainWindow.OpenClassifier(list[0], list[1]) |
self.mainWindow.OpenLayerProperties(list[0], list[1]) |
149 |
|
|
150 |
def Destroy(self): |
def Destroy(self): |
151 |
self.__Close() |
self.__Close() |
152 |
|
|
153 |
def _OnClassify(self, event): |
def _OnProperties(self, event): |
154 |
self.DoOnClassify() |
self.DoOnProperties() |
155 |
|
|
156 |
|
def _OnMoveTop(self, event): |
157 |
|
self.tree.MoveCurrentItemTop() |
158 |
|
|
159 |
def _OnMoveUp(self, event): |
def _OnMoveUp(self, event): |
160 |
self.tree.MoveCurrentItemUp() |
self.tree.MoveCurrentItemUp() |
161 |
|
|
162 |
def _OnMoveDown(self, event): |
def _OnMoveDown(self, event): |
163 |
self.tree.MoveCurrentItemDown() |
self.tree.MoveCurrentItemDown() |
164 |
|
|
165 |
|
def _OnMoveBottom(self, event): |
166 |
|
self.tree.MoveCurrentItemBottom() |
167 |
|
|
168 |
def _OnShowLayer(self, event): |
def _OnShowLayer(self, event): |
169 |
self.tree.DoOnShowLayer() |
self.tree.DoOnShowLayer() |
170 |
pass |
pass |
180 |
pass |
pass |
181 |
|
|
182 |
def __EnableButtons(self, on): |
def __EnableButtons(self, on): |
183 |
for b in self.buttons: |
self.toolBar.EnableTool(ID_LEGEND_TOP, on) |
184 |
b.Enable(on) |
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): |
def __Close(self): |
192 |
self.tree.Close() |
self.tree.Close() |
202 |
self.map = None |
self.map = None |
203 |
self.parent = parent |
self.parent = parent |
204 |
self.layer2id = {} |
self.layer2id = {} |
205 |
|
self.changing_selection = 0 |
206 |
|
|
207 |
|
# |
208 |
|
# The image list used by the wxTreeCtrl causes problems when |
209 |
|
# we remove layers and/or change a classification because it |
210 |
|
# changes the image indices if you remove images from the list. |
211 |
|
# Rather than removing unused images we use this list to keep |
212 |
|
# track of which indices are available in the image list |
213 |
|
# (because of a previous removal) and then replace those indices |
214 |
|
# with new images rather than appending to the end of the image |
215 |
|
# list (assuming there are any that are available). |
216 |
|
# |
217 |
|
self.availImgListIndices = [] |
218 |
|
|
219 |
self.image_list = None |
self.image_list = None |
220 |
self.emptyImageIndex = 0 |
self.emptyImageIndex = 0 |
237 |
def SetMap(self, map): |
def SetMap(self, map): |
238 |
|
|
239 |
sub_list = [(MAP_STACKING_CHANGED, self._OnMsgMapStackingChanged), |
sub_list = [(MAP_STACKING_CHANGED, self._OnMsgMapStackingChanged), |
240 |
(MAP_LAYERS_ADDED, self._OnMsgMapLayersAddedRemoved), |
(MAP_LAYERS_ADDED, self._OnMsgMapLayersAdded), |
241 |
(MAP_LAYERS_REMOVED, self._OnMsgMapLayersAddedRemoved)] |
(MAP_LAYERS_REMOVED, self._OnMsgMapLayersRemoved)] |
242 |
|
|
243 |
if self.map is not None: |
if self.map is not None: |
244 |
for msg, func in sub_list: self.map.Unsubscribe(msg, func) |
for msg, func in sub_list: self.map.Unsubscribe(msg, func) |
249 |
#self._OnMsgMapsChanged) |
#self._OnMsgMapsChanged) |
250 |
#except ConnectorError: |
#except ConnectorError: |
251 |
#pass |
#pass |
252 |
self.__DeleteAllItems() |
self.DeleteAllItems() |
253 |
|
|
254 |
self.map = map |
self.map = map |
255 |
|
|
261 |
#self._OnMsgMapsChanged) |
#self._OnMsgMapsChanged) |
262 |
self.__FillTree(self.map) |
self.__FillTree(self.map) |
263 |
|
|
264 |
|
def MoveCurrentItemTop(self): |
265 |
def MoveCurrentItemUp(self): |
layer, group = self.GetSelectedHierarchy() |
|
cur_id = self.GetSelection() |
|
|
assert(cur_id.IsOk()) |
|
|
|
|
|
cur_data = self.GetPyData(cur_id) |
|
266 |
|
|
267 |
#prev_id = self.GetPrevSibling(cur_id) |
if layer is not None: |
268 |
|
self.map.MoveLayerToTop(layer) |
269 |
|
else: |
270 |
|
assert False, "Shouldn't be allowed." |
271 |
|
pass |
272 |
|
|
273 |
# |
def MoveCurrentItemUp(self): |
274 |
# Get out if there's nowhere to go |
layer, group = self.GetSelectedHierarchy() |
|
# |
|
|
#if prev_id == INVALID_TREE_ID: return |
|
275 |
|
|
276 |
if isinstance(cur_data, Layer): |
if layer is not None: |
277 |
self.map.RaiseLayer(cur_data) |
self.map.RaiseLayer(layer) |
|
elif isinstance(cur_data, ClassGroup): |
|
|
pass |
|
278 |
else: |
else: |
279 |
assert(False, "Shouldn't be here.") |
assert False, "Shouldn't be allowed." |
280 |
pass |
pass |
281 |
|
|
282 |
def MoveCurrentItemDown(self): |
def MoveCurrentItemDown(self): |
283 |
cur_id = self.GetSelection() |
layer, group = self.GetSelectedHierarchy() |
|
assert(cur_id.IsOk()) |
|
|
|
|
|
cur_data = self.GetPyData(cur_id) |
|
284 |
|
|
285 |
if isinstance(cur_data, Layer): |
if layer is not None: |
286 |
self.map.LowerLayer(cur_data) |
self.map.LowerLayer(layer) |
|
elif isinstance(cur_data, ClassGroup): |
|
|
pass |
|
287 |
else: |
else: |
288 |
assert(False, "Shouldn't be here.") |
assert False, "Shouldn't be allowed." |
289 |
pass |
pass |
290 |
|
|
291 |
|
def MoveCurrentItemBottom(self): |
292 |
|
layer, group = self.GetSelectedHierarchy() |
293 |
|
|
294 |
|
if layer is not None: |
295 |
|
self.map.MoveLayerToBottom(layer) |
296 |
|
else: |
297 |
|
assert False, "Shouldn't be allowed." |
298 |
|
pass |
299 |
|
|
300 |
def OnCompareItems(self, item1, item2): |
def OnCompareItems(self, item1, item2): |
301 |
|
|
302 |
data1 = self.GetPyData(item1) |
data1 = self.GetPyData(item1) |
303 |
data2 = self.GetPyData(item2) |
data2 = self.GetPyData(item2) |
304 |
|
|
305 |
if isinstance(data1, Layer): |
if isinstance(data1, BaseLayer): |
306 |
layers = self.map.Layers() |
layers = self.map.Layers() |
307 |
return layers.index(data2) - layers.index(data1) |
return layers.index(data2) - layers.index(data1) |
308 |
else: |
else: |
309 |
return wxTreeCtrl.OnCompareItems(self, item1, item2) |
return wxTreeCtrl.OnCompareItems(self, item1, item2) |
310 |
|
|
|
|
|
311 |
def DoOnShowLayer(self): |
def DoOnShowLayer(self): |
|
#self.__ShowHideLayer(True) |
|
312 |
layer, group = self.GetSelectedHierarchy() |
layer, group = self.GetSelectedHierarchy() |
313 |
layer.SetVisible(True) |
layer.SetVisible(True) |
314 |
|
|
315 |
def DoOnHideLayer(self): |
def DoOnHideLayer(self): |
|
#self.__ShowHideLayer(False) |
|
316 |
layer, group = self.GetSelectedHierarchy() |
layer, group = self.GetSelectedHierarchy() |
317 |
layer.SetVisible(False) |
layer.SetVisible(False) |
318 |
|
|
|
# def DoOnClassify(self): |
|
|
# id = self.GetSelection() |
|
|
# assert(id.IsOk()) |
|
|
|
|
|
# item = self.GetPyData(id) |
|
|
# if isinstance(item, ClassGroup): |
|
|
# id = self.GetItemParent(id) |
|
|
# assert(id.IsOk()) |
|
|
# item = self.GetPyData(id) |
|
|
|
|
|
# # XXX: THIS IS SUCH AWFUL STYLE! YUCK! |
|
|
# self.parent.mainWindow.OpenClassifier(item) |
|
|
# #assert(False, "XXX: FIXME HERE") |
|
|
|
|
319 |
def Sort(self): |
def Sort(self): |
320 |
self.SortChildren(self.GetRootItem()) |
self.SortChildren(self.GetRootItem()) |
321 |
|
|
322 |
|
def GetSelectedHierarchy(self): |
323 |
|
id = self.GetSelection() |
324 |
|
|
325 |
|
if not id.IsOk(): |
326 |
|
return (None, None) |
327 |
|
|
328 |
|
layer = self.GetPyData(id) |
329 |
|
group = None |
330 |
|
|
331 |
|
if isinstance(layer, ClassGroup): |
332 |
|
id = self.GetItemParent(id) |
333 |
|
assert id.IsOk() |
334 |
|
group = layer |
335 |
|
layer = self.GetPyData(id) |
336 |
|
|
337 |
|
return (layer, group) |
338 |
|
|
339 |
def _OnMsgMapsChanged(self): |
def _OnMsgMapsChanged(self): |
340 |
print "ADfsadfsadfsadfsa" |
#print self.map is self.mainWindow.Map() |
|
print self.map is self.mainWindow.Map() |
|
341 |
self.SetMap(self.mainWindow.Map()) |
self.SetMap(self.mainWindow.Map()) |
342 |
|
|
343 |
def _OnSelChanged(self, event): |
def _OnSelChanged(self, event): |
344 |
|
# If we change the selection from normalize_selection do nothing. |
345 |
|
if self.changing_selection: |
346 |
|
return |
347 |
|
|
348 |
|
self.normalize_selection() |
349 |
|
self.__UpdateSelection() |
350 |
|
|
351 |
|
def normalize_selection(self): |
352 |
|
"""Select the layer containing currently selected item""" |
353 |
|
# This is really a workaround for a bug in wx where deleting a |
354 |
|
# subtree with DeleteChildren does not update the selection |
355 |
|
# properly and can lead to segfaults later because the return |
356 |
|
# value of GetSelection points to invalid data. |
357 |
|
item = self.GetSelection() |
358 |
|
while item.IsOk(): |
359 |
|
object = self.GetPyData(item) |
360 |
|
if isinstance(object, BaseLayer): |
361 |
|
break |
362 |
|
item = self.GetItemParent(item) |
363 |
|
else: |
364 |
|
# No layer was found in the chain of parents, so there's |
365 |
|
# nothing we can do. |
366 |
|
return |
367 |
|
|
368 |
|
self.changing_selection = 1 |
369 |
|
try: |
370 |
|
self.SelectItem(item) |
371 |
|
finally: |
372 |
|
self.changing_selection = 0 |
373 |
|
|
|
layer, group = self.GetSelectedHierarchy() |
|
|
self.parent.DoOnSelChanged(layer, group) |
|
374 |
|
|
375 |
def _OnItemActivated(self, event): |
def _OnItemActivated(self, event): |
376 |
self.parent.DoOnClassify() |
self.parent.DoOnProperties() |
377 |
|
|
378 |
def _OnMsgLayerChanged(self, layer): |
def _OnMsgLayerChanged(self, layer): |
379 |
assert(isinstance(layer, Layer)) |
assert isinstance(layer, BaseLayer) |
380 |
|
|
381 |
id = self.layer2id[layer] |
id = self.layer2id[layer] |
382 |
assert(id.IsOk()) |
assert id.IsOk() |
383 |
|
|
384 |
# XXX: yikes! this is so bad, we should be doing what is |
self.__FillTreeLayer(id) |
385 |
# commented out, but there is a problem with keeping |
self.__UpdateSelection() |
|
# track of the images in the image list when we replace |
|
|
# a layer. it ends up causing a seg fault. |
|
|
self.__FillTree(self.map) |
|
|
#self.__FillTreeLayer(id) |
|
386 |
|
|
387 |
def _OnMsgMapStackingChanged(self, *args): |
def _OnMsgMapStackingChanged(self, *args): |
388 |
self.Sort() |
self.Sort() |
390 |
|
|
391 |
if id.IsOk(): |
if id.IsOk(): |
392 |
self.EnsureVisible(id) |
self.EnsureVisible(id) |
393 |
|
self.__UpdateSelection() |
394 |
|
|
395 |
def _OnMsgMapLayersAddedRemoved(self, map): |
def _OnMsgMapLayersAdded(self, map): |
396 |
assert(map is self.map) |
assert map is self.map |
397 |
|
|
398 |
self.__FillTree(self.map) |
root = self.GetRootItem() |
399 |
|
|
400 |
def _OnMsgLayerVisibilityChanged(self, layer): |
i = 0 |
401 |
assert(isinstance(layer, Layer)) |
for l in map.Layers(): |
402 |
|
if not self.layer2id.has_key(l): |
403 |
|
self.__AddLayer(i, l) |
404 |
|
|
405 |
self.__ShowHideLayer(layer) |
self.__UpdateSelection() |
406 |
|
|
407 |
def GetSelectedHierarchy(self): |
def _OnMsgMapLayersRemoved(self, map): |
408 |
id = self.GetSelection() |
assert map is self.map |
|
assert(id.IsOk()) |
|
409 |
|
|
410 |
layer = self.GetPyData(id) |
layer, group = self.GetSelectedHierarchy() |
|
group = None |
|
411 |
|
|
412 |
if isinstance(layer, ClassGroup): |
if layer is None: |
413 |
id = self.GetItemParent(id) |
assert False, "Shouldn't be allowed." |
414 |
assert(id.IsOk()) |
return |
415 |
group = layer |
|
416 |
layer = self.GetPyData(id) |
layers = map.Layers() |
417 |
|
|
418 |
|
for layer, id in self.layer2id.items(): |
419 |
|
if layer not in layers: |
420 |
|
if id.IsOk(): |
421 |
|
self.__RemoveLayer(id) |
422 |
|
|
423 |
return (layer, group) |
self.__UpdateSelection() |
424 |
|
|
425 |
def __FillTree(self, map): |
def _OnMsgLayerVisibilityChanged(self, layer): |
426 |
|
assert isinstance(layer, BaseLayer) |
427 |
|
|
428 |
assert(isinstance(map, Map)) |
self.__ShowHideLayer(layer) |
429 |
|
self.__UpdateSelection() |
430 |
|
|
431 |
self.Freeze() |
def _OnMsgLayerTitleChanged(self, layer): |
432 |
|
|
433 |
self.__DeleteAllItems() |
id = self.layer2id[layer] |
434 |
|
if id.IsOk(): |
435 |
|
self.SetItemText(id, layer.Title()) |
436 |
|
self.__UpdateSelection() |
437 |
|
|
438 |
if map.HasLayers(): |
def __UpdateSelection(self): |
439 |
|
layer, group = self.GetSelectedHierarchy() |
440 |
|
self.parent.DoOnSelChanged(layer, group) |
441 |
|
|
442 |
|
def __FillTree(self, map): |
443 |
|
|
444 |
self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0) |
self.Freeze() |
|
|
|
|
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
|
|
dc = wxMemoryDC() |
|
|
dc.SelectObject(bmp) |
|
|
dc.SetBrush(wxBLACK_BRUSH) |
|
|
dc.Clear() |
|
|
dc.SelectObject(wxNullBitmap) |
|
|
|
|
|
self.emptyImageIndex = \ |
|
|
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
|
|
|
|
|
self.AssignImageList(self.image_list) |
|
445 |
|
|
446 |
root = self.AddRoot("") |
self.DeleteAllItems() |
447 |
|
|
448 |
|
if map.HasLayers(): |
449 |
|
root = self.GetRootItem() |
450 |
for l in map.Layers(): |
for l in map.Layers(): |
451 |
id = self.PrependItem(root, l.Title()) |
self.__AddLayer(0, l) |
|
l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged) |
|
|
l.Subscribe(LAYER_VISIBILITY_CHANGED, |
|
|
self._OnMsgLayerVisibilityChanged) |
|
|
self.SetPyData(id, l) |
|
|
font = self.GetItemFont(id) |
|
|
if not l.Visible(): |
|
|
font.SetStyle(wxITALIC) |
|
|
self.SetItemFont(id, font) |
|
|
|
|
|
self.layer2id[l] = id |
|
|
|
|
|
self.__FillTreeLayer(id) |
|
|
self.Expand(id) |
|
452 |
|
|
453 |
self.Thaw() |
self.Thaw() |
454 |
|
|
459 |
|
|
460 |
self.DeleteChildren(pid) |
self.DeleteChildren(pid) |
461 |
|
|
462 |
clazz = layer.GetClassification() |
if layer.HasClassification(): |
463 |
|
|
464 |
shapeType = layer.ShapeType() |
clazz = layer.GetClassification() |
465 |
|
|
466 |
for g in clazz: |
shapeType = layer.ShapeType() |
|
id = self.AppendItem(pid, g.GetDisplayText()) |
|
|
self.SetPyData(id, g) |
|
467 |
|
|
468 |
bmp = self.__BuildGroupImage(g, shapeType) |
show = layer.Visible() |
469 |
|
for g in clazz: |
470 |
|
if g.IsVisible(): |
471 |
|
id = self.AppendItem(pid, g.GetDisplayText()) |
472 |
|
self.SetPyData(id, g) |
473 |
|
self.__SetVisibilityStyle(show, id) |
474 |
|
|
475 |
|
bmp = self.__BuildGroupImage(g, shapeType) |
476 |
|
|
477 |
|
if bmp is None: |
478 |
|
self.SetItemImage(id, -1) |
479 |
|
self.SetItemSelectedImage(id, -1) |
480 |
|
else: |
481 |
|
if self.availImgListIndices: |
482 |
|
i = self.availImgListIndices.pop(0) |
483 |
|
self.image_list.Replace(i, bmp) |
484 |
|
else: |
485 |
|
i = self.image_list.Add(bmp) |
486 |
|
|
487 |
if bmp is None: |
self.SetItemImage(id, i) |
488 |
self.SetItemImage(id, self.emptyImageIndex) |
self.SetItemSelectedImage(id, i) |
|
else: |
|
|
i = self.image_list.Add(bmp) |
|
|
self.SetItemImage(id, i) |
|
489 |
|
|
490 |
self.Thaw() |
self.Thaw() |
491 |
|
|
492 |
def __BuildGroupImage(self, group, shapeType): |
def __BuildGroupImage(self, group, shapeType): |
|
assert(isinstance(group, ClassGroup)) |
|
493 |
|
|
494 |
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
495 |
#brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
#brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
501 |
|
|
502 |
return bmp |
return bmp |
503 |
|
|
504 |
def __DeleteAllItems(self): |
def DeleteAllItems(self): |
505 |
|
|
506 |
while len(self.layer2id) > 0: |
pid = self.GetRootItem() |
|
layer, id = self.layer2id.popitem() |
|
|
layer.Unsubscribe(LAYER_CHANGED, |
|
|
self._OnMsgLayerChanged) |
|
|
layer.Unsubscribe(LAYER_VISIBILITY_CHANGED, |
|
|
self._OnMsgLayerVisibilityChanged) |
|
507 |
|
|
508 |
self.DeleteAllItems() |
id, cookie = self.GetFirstChild(pid, 123) |
509 |
|
while id.IsOk(): |
510 |
|
self.__RemoveLayer(id) |
511 |
|
id, cookie = self.GetNextChild(pid, cookie) |
512 |
|
|
513 |
|
wxTreeCtrl.DeleteAllItems(self) |
514 |
|
|
515 |
|
def __AddLayer(self, before, l): |
516 |
|
root = self.GetRootItem() |
517 |
|
id = self.InsertItemBefore(root, before, |
518 |
|
l.Title(), |
519 |
|
self.mapImageIndex, |
520 |
|
self.mapImageIndex) |
521 |
|
|
522 |
|
self.SetPyData(id, l) |
523 |
|
self.__SetVisibilityStyle(l.Visible(), id) |
524 |
|
|
525 |
|
self.layer2id[l] = id |
526 |
|
|
527 |
|
self.__FillTreeLayer(id) |
528 |
|
self.Expand(id) |
529 |
|
|
530 |
|
l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged) |
531 |
|
l.Subscribe(LAYER_VISIBILITY_CHANGED, |
532 |
|
self._OnMsgLayerVisibilityChanged) |
533 |
|
l.Subscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
534 |
|
|
535 |
|
def __RemoveLayer(self, id): |
536 |
|
self.DeleteChildren(id) |
537 |
|
|
538 |
|
layer = self.GetPyData(id) |
539 |
|
layer.Unsubscribe(LAYER_CHANGED, |
540 |
|
self._OnMsgLayerChanged) |
541 |
|
layer.Unsubscribe(LAYER_VISIBILITY_CHANGED, |
542 |
|
self._OnMsgLayerVisibilityChanged) |
543 |
|
layer.Unsubscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
544 |
|
|
545 |
|
self.Delete(id) |
546 |
|
del self.layer2id[layer] |
547 |
|
|
548 |
|
def DeleteChildren(self, pid): |
549 |
|
id, cookie = self.GetFirstChild(pid, 123) |
550 |
|
while id.IsOk(): |
551 |
|
self.availImgListIndices.append(self.GetItemImage(id)) |
552 |
|
id, cookie = self.GetNextChild(pid, cookie) |
553 |
|
wxTreeCtrl.DeleteChildren(self, pid) |
554 |
|
|
555 |
|
def GetRootItem(self): |
556 |
|
root = wxTreeCtrl.GetRootItem(self) |
557 |
|
|
558 |
def __ShowHideHelper(self, show, id): |
if not root.IsOk(): |
559 |
|
self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0) |
560 |
|
|
561 |
|
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
562 |
|
dc = wxMemoryDC() |
563 |
|
dc.SelectObject(bmp) |
564 |
|
dc.SetBrush(wxBLACK_BRUSH) |
565 |
|
dc.Clear() |
566 |
|
dc.SelectObject(wxNullBitmap) |
567 |
|
|
568 |
|
self.emptyImageIndex = \ |
569 |
|
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
570 |
|
|
571 |
|
bmp = resource.GetBitmapResource("legend_icon_layer", |
572 |
|
wxBITMAP_TYPE_XPM) |
573 |
|
self.mapImageIndex = \ |
574 |
|
self.image_list.Add(bmp) |
575 |
|
|
576 |
|
self.AssignImageList(self.image_list) |
577 |
|
|
578 |
|
root = self.AddRoot("") |
579 |
|
|
580 |
|
return root |
581 |
|
|
582 |
|
def __SetVisibilityStyle(self, visible, id): |
583 |
font = self.GetItemFont(id) |
font = self.GetItemFont(id) |
584 |
|
|
585 |
if show: |
if visible: |
586 |
font.SetStyle(wxNORMAL) |
font.SetStyle(wxNORMAL) |
587 |
color = wxBLACK |
color = wxBLACK |
588 |
else: |
else: |
589 |
font.SetStyle(wxITALIC) |
#font.SetStyle(wxITALIC) |
590 |
|
font.SetStyle(wxNORMAL) |
591 |
color = wxLIGHT_GREY |
color = wxLIGHT_GREY |
592 |
|
|
593 |
self.SetItemTextColour(id, color) |
self.SetItemTextColour(id, color) |
595 |
|
|
596 |
def __ShowHideLayer(self, layer): |
def __ShowHideLayer(self, layer): |
597 |
parent = self.layer2id[layer] |
parent = self.layer2id[layer] |
598 |
assert(parent.IsOk()) |
assert parent.IsOk() |
599 |
|
|
600 |
show = layer.Visible() |
visible = layer.Visible() |
601 |
|
|
602 |
|
self.__SetVisibilityStyle(visible, parent) |
|
self.__ShowHideHelper(show, parent) |
|
603 |
|
|
604 |
id, cookie = self.GetFirstChild(parent, 123) |
id, cookie = self.GetFirstChild(parent, 123) |
605 |
|
|
606 |
while id.IsOk(): |
while id.IsOk(): |
607 |
self.__ShowHideHelper(show, id) |
self.__SetVisibilityStyle(visible, id) |
608 |
id, cookie = self.GetNextChild(parent, cookie) |
id, cookie = self.GetNextChild(parent, cookie) |
609 |
|
|
610 |
|
class ScaleBarBitmap(wxBoxSizer): |
611 |
|
|
612 |
|
def __init__(self, parent, map, mainWindow): |
613 |
|
# While the width is fixed, get the height _now_. |
614 |
|
dc = wxMemoryDC() |
615 |
|
textwidth, textheight = dc.GetTextExtent("%d"%0) |
616 |
|
self.width = 210 |
617 |
|
self.height = textheight + 3*2 + 8 |
618 |
|
|
619 |
|
wxBoxSizer.__init__(self, wxVERTICAL) |
620 |
|
bmp=wxEmptyBitmap(self.width, self.height) |
621 |
|
self.scalebarBitmap = wxStaticBitmap(parent, -1, bmp) |
622 |
|
self.Add(self.scalebarBitmap, 0, wxALIGN_CENTER|wxLEFT|wxTOP|wxRIGHT, 1) |
623 |
|
|
624 |
|
self.mainWindow = mainWindow |
625 |
|
self.parent = parent |
626 |
|
self.canvas = None |
627 |
|
self.SetCanvas(self.mainWindow.canvas) |
628 |
|
|
629 |
|
def SetCanvas(self, canvas): |
630 |
|
sub_list = [(SCALE_CHANGED, self._OnMsgScaleChanged)] |
631 |
|
|
632 |
|
if self.canvas is not None: |
633 |
|
for msg, func in sub_list: self.canvas.Unsubscribe(msg, func) |
634 |
|
|
635 |
|
self.canvas = canvas |
636 |
|
self.scalebar = ScaleBar(canvas.map) |
637 |
|
|
638 |
|
if self.canvas is not None: |
639 |
|
for msg, func in sub_list: self.canvas.Subscribe(msg, func) |
640 |
|
self.__SetScale(self.canvas.scale) |
641 |
|
|
642 |
|
def _OnMsgScaleChanged(self, scale): |
643 |
|
self.__SetScale(scale) |
644 |
|
|
645 |
|
def __SetScale(self, scale): |
646 |
|
bmp = wxEmptyBitmap(self.width, self.height) |
647 |
|
dc = wxMemoryDC() |
648 |
|
dc.SelectObject(bmp) |
649 |
|
dc.Clear() |
650 |
|
|
651 |
|
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
652 |
|
|
653 |
|
self.scalebarBitmap.SetBitmap(bmp) |
654 |
|
|