11 |
|
|
12 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
13 |
|
|
14 |
|
import sys |
15 |
|
|
16 |
from math import hypot |
from math import hypot |
17 |
|
|
18 |
from wxPython.wx import wxWindow,\ |
from wxPython.wx import wxWindow,\ |
26 |
|
|
27 |
|
|
28 |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
29 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED |
MAP_LAYERS_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED |
30 |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
31 |
SHAPETYPE_POINT |
SHAPETYPE_POINT |
32 |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
34 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
35 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
36 |
|
|
37 |
|
from selection import Selection |
38 |
from renderer import ScreenRenderer, PrinterRender |
from renderer import ScreenRenderer, PrinterRender |
39 |
|
|
40 |
import labeldialog |
import labeldialog |
41 |
|
|
42 |
from messages import SELECTED_SHAPE, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION |
43 |
|
|
44 |
|
|
45 |
# |
# |
260 |
|
|
261 |
"""A widget that displays a map and offers some interaction""" |
"""A widget that displays a map and offers some interaction""" |
262 |
|
|
263 |
def __init__(self, parent, winid, interactor): |
# Some messages that can be subscribed/unsubscribed directly through |
264 |
|
# the MapCanvas come in fact from other objects. This is a dict |
265 |
|
# mapping those messages to the names of the instance variables they |
266 |
|
# actually come from. The delegation is implemented in the Subscribe |
267 |
|
# and Unsubscribe methods |
268 |
|
delegated_messages = {LAYER_SELECTED: "selection", |
269 |
|
SHAPES_SELECTED: "selection"} |
270 |
|
|
271 |
|
# Methods delegated to some instance variables. The delegation is |
272 |
|
# implemented in the __getattr__ method. |
273 |
|
delegated_methods = {"SelectLayer": "selection", |
274 |
|
"SelectShapes": "selection", |
275 |
|
"SelectedLayer": "selection", |
276 |
|
"HasSelectedLayer": "selection"} |
277 |
|
|
278 |
|
def __init__(self, parent, winid): |
279 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
280 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
281 |
|
|
301 |
# the bitmap serving as backing store |
# the bitmap serving as backing store |
302 |
self.bitmap = None |
self.bitmap = None |
303 |
|
|
304 |
# the interactor |
# the selection |
305 |
self.interactor = interactor |
self.selection = Selection() |
306 |
self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected) |
self.selection.Subscribe(SHAPES_SELECTED , self.shape_selected) |
307 |
|
|
308 |
# keep track of which layers/shapes are selected to make sure we |
# keep track of which layers/shapes are selected to make sure we |
309 |
# only redraw when necessary |
# only redraw when necessary |
322 |
wxWindow.__del__(self) |
wxWindow.__del__(self) |
323 |
Publisher.__del__(self) |
Publisher.__del__(self) |
324 |
|
|
325 |
|
def Subscribe(self, channel, *args): |
326 |
|
"""Extend the inherited method to handle delegated messages. |
327 |
|
|
328 |
|
If channel is one of the delegated messages call the appropriate |
329 |
|
object's Subscribe method. Otherwise just call the inherited |
330 |
|
method. |
331 |
|
""" |
332 |
|
if channel in self.delegated_messages: |
333 |
|
object = getattr(self, self.delegated_messages[channel]) |
334 |
|
object.Subscribe(channel, *args) |
335 |
|
else: |
336 |
|
Publisher.Subscribe(self, channel, *args) |
337 |
|
|
338 |
|
def Unsubscribe(self, channel, *args): |
339 |
|
"""Extend the inherited method to handle delegated messages. |
340 |
|
|
341 |
|
If channel is one of the delegated messages call the appropriate |
342 |
|
object's Unsubscribe method. Otherwise just call the inherited |
343 |
|
method. |
344 |
|
""" |
345 |
|
if channel in self.delegated_messages: |
346 |
|
object = getattr(self, self.delegated_messages[channel]) |
347 |
|
object.Unsubscribe(channel, *args) |
348 |
|
else: |
349 |
|
Publisher.Unsubscribe(self, channel, *args) |
350 |
|
|
351 |
|
def __getattr__(self, attr): |
352 |
|
if attr in self.delegated_methods: |
353 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
354 |
|
raise AttributeError(attr) |
355 |
|
|
356 |
def OnPaint(self, event): |
def OnPaint(self, event): |
357 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
358 |
if self.map is not None and self.map.HasLayers(): |
clear = self.map is None or not self.map.HasLayers() |
359 |
self.do_redraw() |
|
360 |
else: |
if not clear: |
361 |
|
try: |
362 |
|
self.do_redraw() |
363 |
|
except: |
364 |
|
print "Error during drawing:", sys.exc_info()[0] |
365 |
|
clear = True |
366 |
|
|
367 |
|
if clear: |
368 |
# If we've got no map or if the map is empty, simply clear |
# If we've got no map or if the map is empty, simply clear |
369 |
# the screen. |
# the screen. |
370 |
|
|
392 |
dc.BeginDrawing() |
dc.BeginDrawing() |
393 |
|
|
394 |
# clear the background |
# clear the background |
395 |
dc.SetBrush(wx.wxWHITE_BRUSH) |
#dc.SetBrush(wx.wxWHITE_BRUSH) |
396 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
#dc.SetPen(wx.wxTRANSPARENT_PEN) |
397 |
dc.DrawRectangle(0, 0, width, height) |
#dc.DrawRectangle(0, 0, width, height) |
398 |
|
dc.SetBackground(wx.wxWHITE_BRUSH) |
399 |
if 1: #self.interactor.selected_map is self.map: |
dc.Clear() |
400 |
selected_layer = self.interactor.selected_layer |
|
401 |
selected_shape = self.interactor.selected_shape |
selected_layer = self.selection.SelectedLayer() |
402 |
else: |
selected_shapes = self.selection.SelectedShapes() |
|
selected_layer = None |
|
|
selected_shape = None |
|
403 |
|
|
404 |
# draw the map into the bitmap |
# draw the map into the bitmap |
405 |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
407 |
# Pass the entire bitmap as update region to the renderer. |
# Pass the entire bitmap as update region to the renderer. |
408 |
# We're redrawing the whole bitmap, after all. |
# We're redrawing the whole bitmap, after all. |
409 |
renderer.RenderMap(self.map, (0, 0, width, height), |
renderer.RenderMap(self.map, (0, 0, width, height), |
410 |
selected_layer, selected_shape) |
selected_layer, selected_shapes) |
411 |
|
|
412 |
dc.EndDrawing() |
dc.EndDrawing() |
413 |
dc.SelectObject(wx.wxNullBitmap) |
dc.SelectObject(wx.wxNullBitmap) |
428 |
printout.Destroy() |
printout.Destroy() |
429 |
|
|
430 |
def SetMap(self, map): |
def SetMap(self, map): |
431 |
redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED, |
432 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
433 |
if self.map is not None: |
if self.map is not None: |
434 |
for channel in redraw_channels: |
for channel in redraw_channels: |
436 |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
437 |
self.projection_changed) |
self.projection_changed) |
438 |
self.map = map |
self.map = map |
439 |
|
self.selection.ClearSelection() |
440 |
if self.map is not None: |
if self.map is not None: |
441 |
for channel in redraw_channels: |
for channel in redraw_channels: |
442 |
self.map.Subscribe(channel, self.full_redraw) |
self.map.Subscribe(channel, self.full_redraw) |
483 |
|
|
484 |
def FitRectToWindow(self, rect): |
def FitRectToWindow(self, rect): |
485 |
"""Fit the rectangular region given by rect into the window. |
"""Fit the rectangular region given by rect into the window. |
486 |
|
|
487 |
Set scale so that rect (in projected coordinates) just fits into |
Set scale so that rect (in projected coordinates) just fits into |
488 |
the window and center it. |
the window and center it. |
489 |
""" |
""" |
490 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
491 |
llx, lly, urx, ury = rect |
llx, lly, urx, ury = rect |
492 |
if llx == urx or lly == ury: |
if llx == urx or lly == ury: |
493 |
# zero with or zero height. Do Nothing |
# zero width or zero height. Do Nothing |
494 |
return |
return |
495 |
scalex = width / (urx - llx) |
scalex = width / (urx - llx) |
496 |
scaley = height / (ury - lly) |
scaley = height / (ury - lly) |
501 |
|
|
502 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
503 |
"""Fit the map to the window |
"""Fit the map to the window |
504 |
|
|
505 |
Set the scale so that the map fits exactly into the window and |
Set the scale so that the map fits exactly into the window and |
506 |
center it in the window. |
center it in the window. |
507 |
""" |
""" |
650 |
# Even when the window becomes larger some parts of the bitmap |
# Even when the window becomes larger some parts of the bitmap |
651 |
# could be reused. |
# could be reused. |
652 |
self.full_redraw() |
self.full_redraw() |
653 |
|
pass |
654 |
|
|
655 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
656 |
"""Redraw the map. |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
657 |
|
# The selection object takes care that it only issues |
658 |
Receiver for the SELECTED_SHAPE messages. Try to redraw only |
# SHAPES_SELECTED messages when the set of selected shapes has |
659 |
when necessary. |
# actually changed, so we can do a full redraw unconditionally. |
660 |
""" |
# FIXME: We should perhaps try to limit the redraw to the are |
661 |
# A redraw is necessary when the display has to change, which |
# actually covered by the shapes before and after the selection |
662 |
# means that either the status changes from having no selection |
# change. |
663 |
# to having a selection shape or vice versa, or when the fact |
self.full_redraw() |
|
# whether there is a selection at all doesn't change, when the |
|
|
# shape which is selected has changed (which means that layer or |
|
|
# shapeid changes). |
|
|
if ((shape is not None or self.last_selected_shape is not None) |
|
|
and (shape != self.last_selected_shape |
|
|
or layer != self.last_selected_layer)): |
|
|
self.full_redraw() |
|
|
|
|
|
# remember the selection so we can compare when it changes again. |
|
|
self.last_selected_layer = layer |
|
|
self.last_selected_shape = shape |
|
664 |
|
|
665 |
def unprojected_rect_around_point(self, x, y, dist): |
def unprojected_rect_around_point(self, x, y, dist): |
666 |
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
753 |
continue |
continue |
754 |
|
|
755 |
filled = layer.GetClassification().GetDefaultFill() \ |
filled = layer.GetClassification().GetDefaultFill() \ |
756 |
is not Color.None |
is not Color.Transparent |
757 |
stroked = layer.GetClassification().GetDefaultLineColor() \ |
stroked = layer.GetClassification().GetDefaultLineColor() \ |
758 |
is not Color.None |
is not Color.Transparent |
759 |
|
|
760 |
layer_proj = layer.projection |
layer_proj = layer.projection |
761 |
if layer_proj is not None: |
if layer_proj is not None: |
834 |
# to deselect the currently selected layer, so we simply select |
# to deselect the currently selected layer, so we simply select |
835 |
# the already selected layer again. |
# the already selected layer again. |
836 |
if layer is None: |
if layer is None: |
837 |
layer = self.interactor.SelectedLayer() |
layer = self.selection.SelectedLayer() |
838 |
self.interactor.SelectLayerAndShape(layer, shape) |
shapes = [] |
839 |
|
else: |
840 |
|
shapes = [shape] |
841 |
|
self.selection.SelectShapes(layer, shapes) |
842 |
return result |
return result |
843 |
|
|
844 |
def LabelShapeAt(self, x, y): |
def LabelShapeAt(self, x, y): |