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