1 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003, 2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jonathan Coles <[email protected]> |
# Jonathan Coles <[email protected]> |
4 |
# Frank Koormann <[email protected]> |
# Frank Koormann <[email protected]> |
8 |
|
|
9 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
10 |
|
|
11 |
|
from math import fabs, cos, pi |
12 |
|
|
13 |
from Thuban import _ |
from Thuban import _ |
14 |
|
|
15 |
import resource |
import resource |
16 |
|
|
17 |
from wxPython.wx import * |
import wx |
18 |
|
|
19 |
from Thuban.Model.layer import BaseLayer |
from Thuban.Model.layer import BaseLayer |
20 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
21 |
from Thuban.Model.classification import ClassGroup |
from Thuban.Model.classification import ClassGroup |
22 |
|
from Thuban.Model.proj import PROJ_UNITS_DEGREES |
23 |
|
|
24 |
from Thuban.Model.messages import \ |
from Thuban.Model.messages import \ |
25 |
MAP_STACKING_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED, LAYER_CHANGED,\ |
MAP_STACKING_CHANGED, MAP_LAYERS_ADDED, MAP_LAYERS_REMOVED, LAYER_CHANGED,\ |
31 |
from Thuban.UI.dock import DockPanel |
from Thuban.UI.dock import DockPanel |
32 |
from Thuban.UI.scalebar import ScaleBar |
from Thuban.UI.scalebar import ScaleBar |
33 |
|
|
34 |
|
from Thuban.UI.menu import Menu |
35 |
|
|
36 |
from Thuban.Lib.connector import ConnectorError |
from Thuban.Lib.connector import ConnectorError |
37 |
|
|
38 |
ID_LEGEND_TOP = 4001 |
ID_LEGEND_TOP = 4001 |
44 |
ID_LEGEND_SHOWLAYER = 4007 |
ID_LEGEND_SHOWLAYER = 4007 |
45 |
ID_LEGEND_HIDELAYER = 4008 |
ID_LEGEND_HIDELAYER = 4008 |
46 |
|
|
47 |
BMP_SIZE_W = 15 |
BMP_SIZE_W = 16 |
48 |
BMP_SIZE_H = 15 |
BMP_SIZE_H = 16 |
49 |
|
|
50 |
TOP_BMP = "top_layer" |
TOP_BMP = "top_layer" |
51 |
RAISE_BMP = "raise_layer" |
RAISE_BMP = "raise_layer" |
55 |
HIDE_BMP = "hide_layer" |
HIDE_BMP = "hide_layer" |
56 |
PROPS_BMP = "layer_properties" |
PROPS_BMP = "layer_properties" |
57 |
|
|
|
|
|
58 |
class LegendPanel(DockPanel): |
class LegendPanel(DockPanel): |
59 |
|
|
60 |
def __init__(self, parent, map, mainWindow): |
def __init__(self, parent, map, mainWindow): |
61 |
DockPanel.__init__(self, parent, -1) |
DockPanel.__init__(self, parent, -1) |
62 |
|
|
63 |
self.mainWindow = mainWindow |
self.mainWindow = mainWindow |
65 |
|
|
66 |
self.buttons = [] |
self.buttons = [] |
67 |
|
|
68 |
panelBox = wxBoxSizer(wxVERTICAL) |
panelBox = wx.BoxSizer(wx.VERTICAL) |
69 |
|
|
70 |
self.toolBar = wxToolBar(self, -1) |
self.toolBar = wx.ToolBar(self, -1) |
71 |
self.toolBar.SetToolBitmapSize(wxSize(24, 24)) |
self.toolBar.SetToolBitmapSize(wx.Size(24, 24)) |
72 |
|
|
73 |
bmp = resource.GetBitmapResource(TOP_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(TOP_BMP, wx.BITMAP_TYPE_XPM) |
74 |
self.toolBar.AddTool(ID_LEGEND_TOP, bmp, |
self.toolBar.AddTool(ID_LEGEND_TOP, bmp, |
75 |
shortHelpString=_("Top Layer")) |
shortHelpString=_("Top Layer")) |
76 |
|
|
77 |
bmp = resource.GetBitmapResource(RAISE_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(RAISE_BMP, wx.BITMAP_TYPE_XPM) |
78 |
self.toolBar.AddTool(ID_LEGEND_RAISE, bmp, |
self.toolBar.AddTool(ID_LEGEND_RAISE, bmp, |
79 |
shortHelpString=_("Raise Layer")) |
shortHelpString=_("Raise Layer")) |
80 |
|
|
81 |
bmp = resource.GetBitmapResource(LOWER_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(LOWER_BMP, wx.BITMAP_TYPE_XPM) |
82 |
self.toolBar.AddTool(ID_LEGEND_LOWER, bmp, |
self.toolBar.AddTool(ID_LEGEND_LOWER, bmp, |
83 |
shortHelpString=_("Lower Layer")) |
shortHelpString=_("Lower Layer")) |
84 |
|
|
85 |
bmp = resource.GetBitmapResource(BOTTOM_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(BOTTOM_BMP, wx.BITMAP_TYPE_XPM) |
86 |
self.toolBar.AddTool(ID_LEGEND_BOTTOM, bmp, |
self.toolBar.AddTool(ID_LEGEND_BOTTOM, bmp, |
87 |
shortHelpString=_("Bottom Layer")) |
shortHelpString=_("Bottom Layer")) |
88 |
|
|
89 |
bmp = resource.GetBitmapResource(SHOW_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(SHOW_BMP, wx.BITMAP_TYPE_XPM) |
90 |
self.toolBar.AddTool(ID_LEGEND_SHOWLAYER, bmp, |
self.toolBar.AddTool(ID_LEGEND_SHOWLAYER, bmp, |
91 |
shortHelpString=_("Show Layer")) |
shortHelpString=_("Show Layer")) |
92 |
|
|
93 |
bmp = resource.GetBitmapResource(HIDE_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(HIDE_BMP, wx.BITMAP_TYPE_XPM) |
94 |
self.toolBar.AddTool(ID_LEGEND_HIDELAYER, bmp, |
self.toolBar.AddTool(ID_LEGEND_HIDELAYER, bmp, |
95 |
shortHelpString=_("Hide Layer")) |
shortHelpString=_("Hide Layer")) |
96 |
|
|
97 |
bmp = resource.GetBitmapResource(PROPS_BMP, wxBITMAP_TYPE_XPM) |
bmp = resource.GetBitmapResource(PROPS_BMP, wx.BITMAP_TYPE_XPM) |
98 |
self.toolBar.AddTool(ID_LEGEND_PROPS, bmp, |
self.toolBar.AddTool(ID_LEGEND_PROPS, bmp, |
99 |
shortHelpString=_("Edit Layer Properties")) |
shortHelpString=_("Edit Layer Properties")) |
100 |
|
|
101 |
self.toolBar.Realize() |
self.toolBar.Realize() |
102 |
panelBox.Add(self.toolBar, 0, wxGROW, 0) |
panelBox.Add(self.toolBar, 0, wx.GROW, 0) |
103 |
|
|
104 |
EVT_TOOL(self, ID_LEGEND_TOP, self._OnMoveTop) |
self.Bind(wx.EVT_TOOL, self._OnMoveTop, id=ID_LEGEND_TOP) |
105 |
EVT_TOOL(self, ID_LEGEND_RAISE, self._OnMoveUp) |
self.Bind(wx.EVT_TOOL, self._OnMoveUp, id=ID_LEGEND_RAISE) |
106 |
EVT_TOOL(self, ID_LEGEND_LOWER, self._OnMoveDown) |
self.Bind(wx.EVT_TOOL, self._OnMoveDown, id=ID_LEGEND_LOWER) |
107 |
EVT_TOOL(self, ID_LEGEND_BOTTOM, self._OnMoveBottom) |
self.Bind(wx.EVT_TOOL, self._OnMoveBottom, id=ID_LEGEND_BOTTOM) |
108 |
EVT_TOOL(self, ID_LEGEND_PROPS, self._OnProperties) |
self.Bind(wx.EVT_TOOL, self._OnProperties, id=ID_LEGEND_PROPS) |
109 |
EVT_TOOL(self, ID_LEGEND_SHOWLAYER, self._OnShowLayer) |
self.Bind(wx.EVT_TOOL, self._OnShowLayer, id=ID_LEGEND_SHOWLAYER) |
110 |
EVT_TOOL(self, ID_LEGEND_HIDELAYER, self._OnHideLayer) |
self.Bind(wx.EVT_TOOL, self._OnHideLayer, id=ID_LEGEND_HIDELAYER) |
111 |
|
|
112 |
self.tree = LegendTree(self, ID_LEGEND_TREE, map, mainWindow) |
self.tree = LegendTree(self, ID_LEGEND_TREE, map, mainWindow) |
113 |
|
|
114 |
panelBox.Add(self.tree, 1, wxGROW, 0) |
panelBox.Add(self.tree, 1, wx.GROW, 0) |
115 |
|
|
116 |
self.scalebarbitmap = ScaleBarBitmap(self, map, mainWindow) |
self.scalebarbitmap = ScaleBarBitmap(self, map, mainWindow) |
117 |
panelBox.Add(self.scalebarbitmap, 0, wxGROW, 0) |
panelBox.Add(self.scalebarbitmap, 0, wx.GROW, 0) |
118 |
|
|
119 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
120 |
self.SetSizer(panelBox) |
self.SetSizer(panelBox) |
127 |
|
|
128 |
self.Create() |
self.Create() |
129 |
|
|
130 |
EVT_CLOSE(self, self._OnClose) |
self.Bind(wx.EVT_CLOSE, self._OnClose) |
131 |
|
|
132 |
|
|
133 |
def GetMap(self): |
def GetMap(self): |
159 |
|
|
160 |
def _OnMoveTop(self, event): |
def _OnMoveTop(self, event): |
161 |
self.tree.MoveCurrentItemTop() |
self.tree.MoveCurrentItemTop() |
162 |
|
|
163 |
def _OnMoveUp(self, event): |
def _OnMoveUp(self, event): |
164 |
self.tree.MoveCurrentItemUp() |
self.tree.MoveCurrentItemUp() |
165 |
|
|
166 |
def _OnMoveDown(self, event): |
def _OnMoveDown(self, event): |
175 |
|
|
176 |
#def Close(self, force = False): |
#def Close(self, force = False): |
177 |
#DockPanel.Close(self, force) |
#DockPanel.Close(self, force) |
178 |
|
|
179 |
def _OnClose(self, event): |
def _OnClose(self, event): |
180 |
self.__Close() |
self.__Close() |
181 |
|
|
183 |
self.tree.DoOnHideLayer() |
self.tree.DoOnHideLayer() |
184 |
pass |
pass |
185 |
|
|
186 |
|
def _OnToggleVisibility(self, event): |
187 |
|
self.tree.ToggleVisibility() |
188 |
|
|
189 |
|
def _OnProjection(self, event): |
190 |
|
self.tree.LayerProjection() |
191 |
|
|
192 |
|
def _OnRemoveLayer(self, event): |
193 |
|
self.mainWindow.RemoveLayer() |
194 |
|
|
195 |
|
def _OnShowTable(self, event): |
196 |
|
self.mainWindow.LayerShowTable() |
197 |
|
|
198 |
def __EnableButtons(self, on): |
def __EnableButtons(self, on): |
199 |
self.toolBar.EnableTool(ID_LEGEND_TOP, on) |
self.toolBar.EnableTool(ID_LEGEND_TOP, on) |
200 |
self.toolBar.EnableTool(ID_LEGEND_RAISE, on) |
self.toolBar.EnableTool(ID_LEGEND_RAISE, on) |
207 |
def __Close(self): |
def __Close(self): |
208 |
self.tree.Close() |
self.tree.Close() |
209 |
|
|
210 |
class LegendTree(wxTreeCtrl): |
class LegendTree(wx.TreeCtrl): |
211 |
|
|
212 |
def __init__(self, parent, id, map, mainWindow): |
def __init__(self, parent, id, map, mainWindow): |
213 |
wxTreeCtrl.__init__(self, parent, id, |
wx.TreeCtrl.__init__(self, parent, id, |
214 |
style = wxTR_DEFAULT_STYLE | wxTR_HIDE_ROOT, |
style = wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT, |
215 |
size = (200, 200)) |
size = (200, 200)) |
216 |
|
|
217 |
self.mainWindow = mainWindow |
self.mainWindow = mainWindow |
218 |
self.map = None |
self.map = None |
219 |
self.parent = parent |
self.parent = parent |
|
self.layer2id = {} |
|
220 |
self.changing_selection = 0 |
self.changing_selection = 0 |
221 |
|
|
222 |
# |
# |
236 |
|
|
237 |
self.previewer = ClassDataPreviewer() |
self.previewer = ClassDataPreviewer() |
238 |
|
|
239 |
EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated) |
self.preventExpandCollapse = False |
240 |
EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged) |
self.raiseProperties = False |
241 |
|
|
242 |
|
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self._OnItemActivated, id=ID_LEGEND_TREE) |
243 |
|
self.Bind(wx.EVT_TREE_SEL_CHANGED, self._OnSelChanged, id=ID_LEGEND_TREE) |
244 |
|
self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnItemExpandCollapse, id=ID_LEGEND_TREE) |
245 |
|
self.Bind(wx.EVT_TREE_ITEM_COLLAPSING, self.OnItemExpandCollapse, id=ID_LEGEND_TREE) |
246 |
|
self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self._OnRightClick, id=ID_LEGEND_TREE) |
247 |
|
|
248 |
EVT_CLOSE(self, self._OnClose) |
self.Bind(wx.EVT_CLOSE, self._OnClose) |
249 |
|
|
250 |
self.SetMap(map) |
self.SetMap(map) |
251 |
|
|
252 |
|
def _OnRightClick(self, event): |
253 |
|
"""Select item and pop up a context menu""" |
254 |
|
|
255 |
|
# The pop up menu is related to the legend tree, so we have direct |
256 |
|
# access on the tree items. The events issued by the menu are handled |
257 |
|
# by the legend panel, since most of the handlers are already |
258 |
|
# implemented there. |
259 |
|
|
260 |
|
# Update item selection to the right click |
261 |
|
item = event.GetItem() |
262 |
|
self.SelectItem(item) |
263 |
|
|
264 |
|
# Define the menu |
265 |
|
popup_menu = Menu("PopUp", "", |
266 |
|
[ "layer_visibility", |
267 |
|
None, |
268 |
|
"layer_properties", |
269 |
|
"layer_projection", |
270 |
|
"layer_remove", |
271 |
|
"layer_show_table", |
272 |
|
None, |
273 |
|
"layer_to_top", |
274 |
|
"layer_raise", |
275 |
|
"layer_lower", |
276 |
|
"layer_to_bottom" |
277 |
|
]) |
278 |
|
|
279 |
|
# Display the menu |
280 |
|
pos = event.GetPoint() |
281 |
|
shift = self.ClientToScreen((0,0)) |
282 |
|
self.PopupMenu(self.mainWindow.build_menu(popup_menu), pos) |
283 |
|
|
284 |
|
def find_layer(self, layer): |
285 |
|
"""Return the tree item for the layer""" |
286 |
|
root = self.GetRootItem() |
287 |
|
id, cookie = self.GetFirstChild(root) |
288 |
|
while id.IsOk(): |
289 |
|
if self.GetPyData(id) is layer: |
290 |
|
return id |
291 |
|
id, cookie = self.GetNextChild(root, cookie) |
292 |
|
return None |
293 |
|
|
294 |
def _OnClose(self, event): |
def _OnClose(self, event): |
295 |
self.SetMap(None) |
self.SetMap(None) |
296 |
|
|
313 |
#except ConnectorError: |
#except ConnectorError: |
314 |
#pass |
#pass |
315 |
self.DeleteAllItems() |
self.DeleteAllItems() |
316 |
|
|
317 |
self.map = map |
self.map = map |
318 |
|
|
319 |
if self.map is not None: |
if self.map is not None: |
369 |
layers = self.map.Layers() |
layers = self.map.Layers() |
370 |
return layers.index(data2) - layers.index(data1) |
return layers.index(data2) - layers.index(data1) |
371 |
else: |
else: |
372 |
return wxTreeCtrl.OnCompareItems(self, item1, item2) |
return wx.TreeCtrl.OnCompareItems(self, item1, item2) |
373 |
|
|
374 |
def DoOnShowLayer(self): |
def DoOnShowLayer(self): |
375 |
layer, group = self.GetSelectedHierarchy() |
layer, group = self.GetSelectedHierarchy() |
379 |
layer, group = self.GetSelectedHierarchy() |
layer, group = self.GetSelectedHierarchy() |
380 |
layer.SetVisible(False) |
layer.SetVisible(False) |
381 |
|
|
382 |
|
def ToggleVisibility(self): |
383 |
|
layer, group = self.GetSelectedHierarchy() |
384 |
|
|
385 |
|
layer.SetVisible(not layer.Visible()) |
386 |
|
|
387 |
|
def LayerProjection(self): |
388 |
|
self.parent.mainWindow.LayerProjection() |
389 |
|
|
390 |
def Sort(self): |
def Sort(self): |
391 |
self.SortChildren(self.GetRootItem()) |
self.SortChildren(self.GetRootItem()) |
392 |
|
|
410 |
def _OnMsgMapsChanged(self): |
def _OnMsgMapsChanged(self): |
411 |
#print self.map is self.mainWindow.Map() |
#print self.map is self.mainWindow.Map() |
412 |
self.SetMap(self.mainWindow.Map()) |
self.SetMap(self.mainWindow.Map()) |
413 |
|
|
414 |
def _OnSelChanged(self, event): |
def _OnSelChanged(self, event): |
415 |
# If we change the selection from normalize_selection do nothing. |
# If we change the selection from normalize_selection do nothing. |
416 |
if self.changing_selection: |
if self.changing_selection: |
443 |
self.changing_selection = 0 |
self.changing_selection = 0 |
444 |
|
|
445 |
|
|
446 |
|
def OnItemExpandCollapse(self, event): |
447 |
|
if self.preventExpandCollapse: |
448 |
|
event.Veto() |
449 |
|
self.preventExpandCollapse = False |
450 |
|
|
451 |
def _OnItemActivated(self, event): |
def _OnItemActivated(self, event): |
452 |
self.parent.DoOnProperties() |
# The following looks strange but is need under Windows to |
453 |
|
# raise the Properties on double-click: The tree control |
454 |
|
# always gets an Expanded / Collapsed event after the ItemActivated |
455 |
|
# on double click, which raises the main window again. We add a second |
456 |
|
# ItemActivated event to the queue, which simply raises the already |
457 |
|
# displayed window. |
458 |
|
if self.raiseProperties: |
459 |
|
self.parent.DoOnProperties() |
460 |
|
self.raiseProperties = False |
461 |
|
else: |
462 |
|
self.raiseProperties = True |
463 |
|
self.preventExpandCollapse = True |
464 |
|
self.parent.DoOnProperties() |
465 |
|
self.AddPendingEvent(event) |
466 |
|
|
467 |
def _OnMsgLayerChanged(self, layer): |
def _OnMsgLayerChanged(self, layer): |
468 |
assert isinstance(layer, BaseLayer) |
assert isinstance(layer, BaseLayer) |
469 |
|
|
470 |
id = self.layer2id[layer] |
id = self.find_layer(layer) |
471 |
assert id.IsOk() |
assert id is not None |
472 |
|
|
473 |
self.__FillTreeLayer(id) |
self.__FillTreeLayer(id) |
474 |
self.__UpdateSelection() |
self.__UpdateSelection() |
484 |
def _OnMsgMapLayersAdded(self, map): |
def _OnMsgMapLayersAdded(self, map): |
485 |
assert map is self.map |
assert map is self.map |
486 |
|
|
487 |
|
# Build a dict with all layers known by the the tree as keys |
488 |
|
layers = {} |
489 |
root = self.GetRootItem() |
root = self.GetRootItem() |
490 |
|
id, cookie = self.GetFirstChild(root) |
491 |
|
while id.IsOk(): |
492 |
|
layers[self.GetPyData(id)] = 1 |
493 |
|
id, cookie = self.GetNextChild(root, cookie) |
494 |
|
|
495 |
|
# Add layers in the map but not in the dict |
496 |
i = 0 |
i = 0 |
497 |
for l in map.Layers(): |
for l in map.Layers(): |
498 |
if not self.layer2id.has_key(l): |
if not l in layers: |
499 |
self.__AddLayer(i, l) |
self.__AddLayer(i, l) |
500 |
|
|
501 |
self.__UpdateSelection() |
self.__UpdateSelection() |
503 |
def _OnMsgMapLayersRemoved(self, map): |
def _OnMsgMapLayersRemoved(self, map): |
504 |
assert map is self.map |
assert map is self.map |
505 |
|
|
|
layer, group = self.GetSelectedHierarchy() |
|
|
|
|
|
if layer is None: |
|
|
assert False, "Shouldn't be allowed." |
|
|
return |
|
|
|
|
506 |
layers = map.Layers() |
layers = map.Layers() |
507 |
|
|
508 |
for layer, id in self.layer2id.items(): |
root = self.GetRootItem() |
509 |
if layer not in layers: |
id, cookie = self.GetFirstChild(root) |
510 |
if id.IsOk(): |
while id.IsOk(): |
511 |
self.__RemoveLayer(id) |
if self.GetPyData(id) not in layers: |
512 |
|
self.__RemoveLayer(id) |
513 |
|
id, cookie = self.GetNextChild(root, cookie) |
514 |
|
|
515 |
|
|
516 |
self.__UpdateSelection() |
self.__UpdateSelection() |
517 |
|
|
523 |
|
|
524 |
def _OnMsgLayerTitleChanged(self, layer): |
def _OnMsgLayerTitleChanged(self, layer): |
525 |
|
|
526 |
id = self.layer2id[layer] |
id = self.find_layer(layer) |
527 |
if id.IsOk(): |
if id.IsOk(): |
528 |
self.SetItemText(id, layer.Title()) |
self.SetItemText(id, layer.Title()) |
529 |
self.__UpdateSelection() |
self.__UpdateSelection() |
531 |
def __UpdateSelection(self): |
def __UpdateSelection(self): |
532 |
layer, group = self.GetSelectedHierarchy() |
layer, group = self.GetSelectedHierarchy() |
533 |
self.parent.DoOnSelChanged(layer, group) |
self.parent.DoOnSelChanged(layer, group) |
534 |
|
|
535 |
def __FillTree(self, map): |
def __FillTree(self, map): |
536 |
|
|
537 |
self.Freeze() |
self.Freeze() |
546 |
self.Thaw() |
self.Thaw() |
547 |
|
|
548 |
def __FillTreeLayer(self, pid): |
def __FillTreeLayer(self, pid): |
549 |
|
|
550 |
layer = self.GetPyData(pid) |
layer = self.GetPyData(pid) |
551 |
|
|
552 |
self.Freeze() |
self.Freeze() |
569 |
bmp = self.__BuildGroupImage(g, shapeType) |
bmp = self.__BuildGroupImage(g, shapeType) |
570 |
|
|
571 |
if bmp is None: |
if bmp is None: |
572 |
self.SetItemImage(id, -1) |
self.SetItemImage(id, -1, wx.TreeItemIcon_Normal) |
573 |
self.SetItemSelectedImage(id, -1) |
self.SetItemImage(id, -1, wx.TreeItemIcon_Selected) |
574 |
|
#self.SetItemSelectedImage(id, -1) |
575 |
else: |
else: |
576 |
if self.availImgListIndices: |
if self.availImgListIndices: |
577 |
i = self.availImgListIndices.pop(0) |
i = self.availImgListIndices.pop(0) |
579 |
else: |
else: |
580 |
i = self.image_list.Add(bmp) |
i = self.image_list.Add(bmp) |
581 |
|
|
582 |
self.SetItemImage(id, i) |
self.SetItemImage(id, i, wx.TreeItemIcon_Normal) |
583 |
self.SetItemSelectedImage(id, i) |
self.SetItemImage(id, i, wx.TreeItemIcon_Selected) |
584 |
|
#self.SetItemlectedImage(id, i) |
585 |
|
|
586 |
self.Thaw() |
self.Thaw() |
587 |
|
|
588 |
def __BuildGroupImage(self, group, shapeType): |
def __BuildGroupImage(self, group, shapeType): |
589 |
|
|
590 |
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
bmp = wx.EmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
591 |
#brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
#brush = wxBrush(Color2wxColour(item[1]), wxSOLID) |
592 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
593 |
dc.SelectObject(bmp) |
dc.SelectObject(bmp) |
594 |
dc.Clear() |
dc.Clear() |
595 |
|
|
601 |
|
|
602 |
pid = self.GetRootItem() |
pid = self.GetRootItem() |
603 |
|
|
604 |
id, cookie = self.GetFirstChild(pid, 123) |
id, cookie = self.GetFirstChild(pid) |
605 |
while id.IsOk(): |
while id.IsOk(): |
606 |
self.__RemoveLayer(id) |
self.__RemoveLayer(id) |
607 |
id, cookie = self.GetNextChild(pid, cookie) |
id, cookie = self.GetNextChild(pid, cookie) |
608 |
|
|
609 |
wxTreeCtrl.DeleteAllItems(self) |
wx.TreeCtrl.DeleteAllItems(self) |
610 |
|
|
611 |
def __AddLayer(self, before, l): |
def __AddLayer(self, before, l): |
612 |
root = self.GetRootItem() |
root = self.GetRootItem() |
613 |
id = self.InsertItemBefore(root, before, |
id = self.InsertItemBefore(root, before, |
614 |
l.Title(), |
l.Title(), |
615 |
self.mapImageIndex, |
self.mapImageIndex, |
616 |
self.mapImageIndex) |
self.mapImageIndex) |
617 |
|
|
618 |
self.SetPyData(id, l) |
self.SetPyData(id, l) |
619 |
self.__SetVisibilityStyle(l.Visible(), id) |
self.__SetVisibilityStyle(l.Visible(), id) |
620 |
|
|
|
self.layer2id[l] = id |
|
|
|
|
621 |
self.__FillTreeLayer(id) |
self.__FillTreeLayer(id) |
622 |
self.Expand(id) |
self.Expand(id) |
623 |
|
|
624 |
l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged) |
l.Subscribe(LAYER_CHANGED, self._OnMsgLayerChanged) |
625 |
l.Subscribe(LAYER_VISIBILITY_CHANGED, |
l.Subscribe(LAYER_VISIBILITY_CHANGED, |
626 |
self._OnMsgLayerVisibilityChanged) |
self._OnMsgLayerVisibilityChanged) |
627 |
l.Subscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
l.Subscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
628 |
|
|
630 |
self.DeleteChildren(id) |
self.DeleteChildren(id) |
631 |
|
|
632 |
layer = self.GetPyData(id) |
layer = self.GetPyData(id) |
633 |
layer.Unsubscribe(LAYER_CHANGED, |
layer.Unsubscribe(LAYER_CHANGED, |
634 |
self._OnMsgLayerChanged) |
self._OnMsgLayerChanged) |
635 |
layer.Unsubscribe(LAYER_VISIBILITY_CHANGED, |
layer.Unsubscribe(LAYER_VISIBILITY_CHANGED, |
636 |
self._OnMsgLayerVisibilityChanged) |
self._OnMsgLayerVisibilityChanged) |
637 |
layer.Unsubscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
layer.Unsubscribe(TITLE_CHANGED, self._OnMsgLayerTitleChanged) |
638 |
|
|
639 |
self.Delete(id) |
self.Delete(id) |
|
del self.layer2id[layer] |
|
640 |
|
|
641 |
def DeleteChildren(self, pid): |
def DeleteChildren(self, pid): |
642 |
id, cookie = self.GetFirstChild(pid, 123) |
id, cookie = self.GetFirstChild(pid) |
643 |
while id.IsOk(): |
while id.IsOk(): |
644 |
self.availImgListIndices.append(self.GetItemImage(id)) |
self.availImgListIndices.append(self.GetItemImage(id)) |
645 |
id, cookie = self.GetNextChild(pid, cookie) |
id, cookie = self.GetNextChild(pid, cookie) |
646 |
wxTreeCtrl.DeleteChildren(self, pid) |
wx.TreeCtrl.DeleteChildren(self, pid) |
647 |
|
|
648 |
def GetRootItem(self): |
def GetRootItem(self): |
649 |
root = wxTreeCtrl.GetRootItem(self) |
root = wx.TreeCtrl.GetRootItem(self) |
650 |
|
|
651 |
if not root.IsOk(): |
if not root.IsOk(): |
652 |
self.image_list = wxImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0) |
self.image_list = wx.ImageList(BMP_SIZE_W, BMP_SIZE_H, False, 0) |
653 |
|
|
654 |
bmp = wxEmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
bmp = wx.EmptyBitmap(BMP_SIZE_W, BMP_SIZE_H) |
655 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
656 |
dc.SelectObject(bmp) |
dc.SelectObject(bmp) |
657 |
dc.SetBrush(wxBLACK_BRUSH) |
dc.SetBrush(wx.BLACK_BRUSH) |
658 |
dc.Clear() |
dc.Clear() |
659 |
dc.SelectObject(wxNullBitmap) |
dc.SelectObject(wx.NullBitmap) |
660 |
|
|
661 |
self.emptyImageIndex = \ |
self.emptyImageIndex = \ |
662 |
self.image_list.AddWithColourMask(bmp, wxColour(0, 0, 0)) |
self.image_list.AddWithColourMask(bmp, wx.Colour(0, 0, 0)) |
663 |
|
|
664 |
bmp = resource.GetBitmapResource("legend_icon_layer", |
bmp = resource.GetBitmapResource("legend_icon_layer", |
665 |
wxBITMAP_TYPE_XPM) |
wx.BITMAP_TYPE_XPM) |
666 |
self.mapImageIndex = \ |
self.mapImageIndex = \ |
667 |
self.image_list.Add(bmp) |
self.image_list.Add(bmp) |
668 |
|
|
669 |
self.AssignImageList(self.image_list) |
self.AssignImageList(self.image_list) |
670 |
|
self.availImgListIndices = [] |
671 |
|
|
672 |
root = self.AddRoot("") |
root = self.AddRoot("") |
673 |
|
|
677 |
font = self.GetItemFont(id) |
font = self.GetItemFont(id) |
678 |
|
|
679 |
if visible: |
if visible: |
680 |
font.SetStyle(wxNORMAL) |
font.SetStyle(wx.NORMAL) |
681 |
color = wxBLACK |
color = wx.BLACK |
682 |
else: |
else: |
683 |
#font.SetStyle(wxITALIC) |
#font.SetStyle(wxITALIC) |
684 |
font.SetStyle(wxNORMAL) |
font.SetStyle(wx.NORMAL) |
685 |
color = wxLIGHT_GREY |
color = wx.LIGHT_GREY |
686 |
|
|
687 |
self.SetItemTextColour(id, color) |
self.SetItemTextColour(id, color) |
688 |
self.SetItemFont(id, font) |
self.SetItemFont(id, font) |
689 |
|
|
690 |
def __ShowHideLayer(self, layer): |
def __ShowHideLayer(self, layer): |
691 |
parent = self.layer2id[layer] |
parent = self.find_layer(layer) |
692 |
assert parent.IsOk() |
assert parent.IsOk() |
693 |
|
|
694 |
visible = layer.Visible() |
visible = layer.Visible() |
695 |
|
|
696 |
self.__SetVisibilityStyle(visible, parent) |
self.__SetVisibilityStyle(visible, parent) |
697 |
|
|
698 |
id, cookie = self.GetFirstChild(parent, 123) |
id, cookie = self.GetFirstChild(parent) |
699 |
|
|
700 |
while id.IsOk(): |
while id.IsOk(): |
701 |
self.__SetVisibilityStyle(visible, id) |
self.__SetVisibilityStyle(visible, id) |
702 |
id, cookie = self.GetNextChild(parent, cookie) |
id, cookie = self.GetNextChild(parent, cookie) |
703 |
|
|
704 |
class ScaleBarBitmap(wxBoxSizer): |
# In wxPython 2.4 the GetFirstChild method has to be called with a |
705 |
|
# second argument and in 2.5 it must not. Reading the code of |
706 |
|
# wxPython 2.4 it seems that the second parameter was intended to be |
707 |
|
# optional there but due to a bug in the C++ code it doesn't work |
708 |
|
# and omitting the second argument leads to a segfault. To cope |
709 |
|
# with this and to make the code usable with both 2.5 and 2.4 we |
710 |
|
# overwrite the inherited method when running with 2.4 to provide a |
711 |
|
# default value for the second argument. |
712 |
|
if map(int, wx.__version__.split(".")[:2]) < [2, 5]: |
713 |
|
def GetFirstChild(self, item): |
714 |
|
return wx.TreeCtrl.GetFirstChild(self, item, 0) |
715 |
|
|
716 |
|
|
717 |
|
class ScaleBarBitmap(wx.BoxSizer): |
718 |
|
|
719 |
def __init__(self, parent, map, mainWindow): |
def __init__(self, parent, map, mainWindow): |
720 |
# While the width is fixed, get the height _now_. |
# While the width is fixed, get the height _now_. |
721 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
722 |
textwidth, textheight = dc.GetTextExtent("%d"%0) |
textwidth, textheight = dc.GetTextExtent("%d"%0) |
723 |
self.width = 210 |
self.width = 210 |
724 |
self.height = textheight + 3*2 + 8 |
self.height = textheight + 3*2 + 8 |
725 |
|
|
726 |
wxBoxSizer.__init__(self, wxVERTICAL) |
wx.BoxSizer.__init__(self, wx.VERTICAL) |
727 |
bmp=wxEmptyBitmap(self.width, self.height) |
bmp=wx.EmptyBitmap(self.width, self.height) |
728 |
self.scalebarBitmap = wxStaticBitmap(parent, -1, bmp) |
self.scalebarBitmap = wx.StaticBitmap(parent, -1, bmp) |
729 |
self.Add(self.scalebarBitmap, 0, wxALIGN_CENTER|wxLEFT|wxTOP|wxRIGHT, 1) |
self.Add(self.scalebarBitmap, 0, wx.ALIGN_CENTER|wx.LEFT|wx.TOP|wx.RIGHT, 1) |
730 |
|
|
731 |
self.mainWindow = mainWindow |
self.mainWindow = mainWindow |
732 |
self.parent = parent |
self.parent = parent |
738 |
|
|
739 |
if self.canvas is not None: |
if self.canvas is not None: |
740 |
for msg, func in sub_list: self.canvas.Unsubscribe(msg, func) |
for msg, func in sub_list: self.canvas.Unsubscribe(msg, func) |
741 |
|
|
742 |
self.canvas = canvas |
self.canvas = canvas |
743 |
self.scalebar = ScaleBar(canvas.map) |
self.scalebar = ScaleBar(canvas.map) |
744 |
|
|
750 |
self.__SetScale(scale) |
self.__SetScale(scale) |
751 |
|
|
752 |
def __SetScale(self, scale): |
def __SetScale(self, scale): |
753 |
bmp = wxEmptyBitmap(self.width, self.height) |
bmp = wx.EmptyBitmap(self.width, self.height) |
754 |
dc = wxMemoryDC() |
dc = wx.MemoryDC() |
755 |
dc.SelectObject(bmp) |
dc.SelectObject(bmp) |
756 |
dc.Clear() |
dc.Clear() |
757 |
|
|
758 |
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
if self.canvas.map is not None \ |
759 |
|
and self.canvas.map.projection is not None: |
760 |
|
|
761 |
|
# if we are using a projection with geographics coordinates |
762 |
|
# we need to change the scale value based on where we are |
763 |
|
# on the globe. |
764 |
|
if self.canvas.map.projection.GetProjectedUnits() \ |
765 |
|
== PROJ_UNITS_DEGREES: |
766 |
|
|
767 |
|
width, height = self.canvas.GetSizeTuple() |
768 |
|
long, lat = self.canvas.win_to_proj(width/2, height/2) |
769 |
|
|
770 |
|
# slightly inaccurate, but if we are looking at |
771 |
|
# the north/south pole we could end up dividing by zero |
772 |
|
# |
773 |
|
# it shouldn't matter for our purposes that we ignore |
774 |
|
# the original sign of lat. |
775 |
|
if fabs(lat) > 89.9: lat = 89.9 |
776 |
|
|
777 |
|
# |
778 |
|
# one degree is about 111,133m at the equator |
779 |
|
# we need to adjust that for latitude as |
780 |
|
# we move north/south. use the center of the map |
781 |
|
# as the point to scale the length to. |
782 |
|
# |
783 |
|
scale = scale / (111133.0 * fabs(cos(lat * pi/180))) |
784 |
|
|
785 |
|
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
786 |
|
|
787 |
self.scalebarBitmap.SetBitmap(bmp) |
self.scalebarBitmap.SetBitmap(bmp) |
788 |
|
|