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 |
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 |
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): |
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) |
237 |
self.previewer = ClassDataPreviewer() |
self.previewer = ClassDataPreviewer() |
238 |
|
|
239 |
self.preventExpandCollapse = False |
self.preventExpandCollapse = False |
240 |
|
self.raiseProperties = False |
241 |
|
|
242 |
EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated) |
EVT_TREE_ITEM_ACTIVATED(self, ID_LEGEND_TREE, self._OnItemActivated) |
243 |
EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged) |
EVT_TREE_SEL_CHANGED(self, ID_LEGEND_TREE, self._OnSelChanged) |
244 |
EVT_TREE_ITEM_EXPANDING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
EVT_TREE_ITEM_EXPANDING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
245 |
EVT_TREE_ITEM_COLLAPSING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
EVT_TREE_ITEM_COLLAPSING(self, ID_LEGEND_TREE, self.OnItemExpandCollapse) |
246 |
|
EVT_TREE_ITEM_RIGHT_CLICK(self, ID_LEGEND_TREE, self._OnRightClick) |
247 |
|
|
248 |
EVT_CLOSE(self, self._OnClose) |
EVT_CLOSE(self, 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): |
def find_layer(self, layer): |
285 |
"""Return the tree item for the layer""" |
"""Return the tree item for the layer""" |
286 |
root = self.GetRootItem() |
root = self.GetRootItem() |
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 |
|
|
449 |
self.preventExpandCollapse = False |
self.preventExpandCollapse = False |
450 |
|
|
451 |
def _OnItemActivated(self, event): |
def _OnItemActivated(self, event): |
452 |
self.preventExpandCollapse = True |
# The following looks strange but is need under Windows to |
453 |
self.parent.DoOnProperties() |
# 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) |
741 |
|
|
742 |
if self.canvas.map is not None \ |
if self.canvas.map is not None \ |
743 |
and self.canvas.map.projection is not None: |
and self.canvas.map.projection is not None: |
744 |
|
|
745 |
|
# if we are using a projection with geographics coordinates |
746 |
|
# we need to change the scale value based on where we are |
747 |
|
# on the globe. |
748 |
|
if self.canvas.map.projection.GetProjectedUnits() \ |
749 |
|
== PROJ_UNITS_DEGREES: |
750 |
|
|
751 |
|
width, height = self.canvas.GetSizeTuple() |
752 |
|
long, lat = self.canvas.win_to_proj(width/2, height/2) |
753 |
|
|
754 |
|
# slightly inaccurate, but if we are looking at |
755 |
|
# the north/south pole we could end up dividing by zero |
756 |
|
# |
757 |
|
# it shouldn't matter for our purposes that we ignore |
758 |
|
# the original sign of lat. |
759 |
|
if fabs(lat) > 89.9: lat = 89.9 |
760 |
|
|
761 |
|
# |
762 |
|
# one degree is about 111,133m at the equator |
763 |
|
# we need to adjust that for latitude as |
764 |
|
# we move north/south. use the center of the map |
765 |
|
# as the point to scale the length to. |
766 |
|
# |
767 |
|
scale = scale / (111133.0 * fabs(cos(lat * pi/180))) |
768 |
|
|
769 |
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
self.scalebar.DrawScaleBar(scale, dc, (0,0), dc.GetSizeTuple()) |
770 |
|
|
771 |
self.scalebarBitmap.SetBitmap(bmp) |
self.scalebarBitmap.SetBitmap(bmp) |