32 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
33 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
34 |
|
|
35 |
|
from selection import Selection |
36 |
from renderer import ScreenRenderer, PrinterRender |
from renderer import ScreenRenderer, PrinterRender |
37 |
|
|
38 |
import labeldialog |
import labeldialog |
39 |
|
|
40 |
from messages import SELECTED_SHAPE, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION |
41 |
|
|
42 |
|
|
43 |
# |
# |
258 |
|
|
259 |
"""A widget that displays a map and offers some interaction""" |
"""A widget that displays a map and offers some interaction""" |
260 |
|
|
261 |
def __init__(self, parent, winid, interactor): |
# Some messages that can be subscribed/unsubscribed directly through |
262 |
|
# the MapCanvas come in fact from other objects. This is a map to |
263 |
|
# map those messages to the names of the instance variables they |
264 |
|
# actually come from. This delegation is implemented in the |
265 |
|
# Subscribe and unsubscribed methods |
266 |
|
delegated_messages = {LAYER_SELECTED: "selection", |
267 |
|
SHAPES_SELECTED: "selection"} |
268 |
|
|
269 |
|
# Methods delegated to some instance variables. The delegation is |
270 |
|
# implemented in the __getattr__ method. |
271 |
|
delegated_methods = {"SelectLayer": "selection", |
272 |
|
"SelectShapes": "selection", |
273 |
|
"SelectedLayer": "selection", |
274 |
|
"HasSelectedLayer": "selection"} |
275 |
|
|
276 |
|
def __init__(self, parent, winid): |
277 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
278 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
279 |
|
|
299 |
# the bitmap serving as backing store |
# the bitmap serving as backing store |
300 |
self.bitmap = None |
self.bitmap = None |
301 |
|
|
302 |
# the interactor |
# the selection |
303 |
self.interactor = interactor |
self.selection = Selection() |
304 |
self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected) |
self.selection.Subscribe(SHAPES_SELECTED , self.shape_selected) |
305 |
|
|
306 |
# keep track of which layers/shapes are selected to make sure we |
# keep track of which layers/shapes are selected to make sure we |
307 |
# only redraw when necessary |
# only redraw when necessary |
320 |
wxWindow.__del__(self) |
wxWindow.__del__(self) |
321 |
Publisher.__del__(self) |
Publisher.__del__(self) |
322 |
|
|
323 |
|
def Subscribe(self, channel, *args): |
324 |
|
"""Extend the inherited method to handle delegated messages. |
325 |
|
|
326 |
|
If channel is one of the delegated messages call the appropriate |
327 |
|
object's Subscribe method. Otherwise just call the inherited |
328 |
|
method. |
329 |
|
""" |
330 |
|
if channel in self.delegated_messages: |
331 |
|
object = getattr(self, self.delegated_messages[channel]) |
332 |
|
object.Subscribe(channel, *args) |
333 |
|
else: |
334 |
|
Publisher.Subscribe(self, channel, *args) |
335 |
|
|
336 |
|
def Unsubscribe(self, channel, *args): |
337 |
|
"""Extend the inherited method to handle delegated messages. |
338 |
|
|
339 |
|
If channel is one of the delegated messages call the appropriate |
340 |
|
object's Unsubscribe method. Otherwise just call the inherited |
341 |
|
method. |
342 |
|
""" |
343 |
|
if channel in self.delegated_messages: |
344 |
|
object = getattr(self, self.delegated_messages[channel]) |
345 |
|
object.Unsubscribe(channel, *args) |
346 |
|
else: |
347 |
|
Publisher.Unsubscribe(self, channel, *args) |
348 |
|
|
349 |
|
def __getattr__(self, attr): |
350 |
|
if attr in self.delegated_methods: |
351 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
352 |
|
raise AttributeError(attr) |
353 |
|
|
354 |
def OnPaint(self, event): |
def OnPaint(self, event): |
355 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
356 |
if self.map is not None and self.map.HasLayers(): |
if self.map is not None and self.map.HasLayers(): |
387 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
388 |
dc.DrawRectangle(0, 0, width, height) |
dc.DrawRectangle(0, 0, width, height) |
389 |
|
|
390 |
if 1: #self.interactor.selected_map is self.map: |
selected_layer = self.selection.SelectedLayer() |
391 |
selected_layer = self.interactor.selected_layer |
selected_shapes = self.selection.SelectedShapes() |
|
selected_shape = self.interactor.selected_shape |
|
|
else: |
|
|
selected_layer = None |
|
|
selected_shape = None |
|
392 |
|
|
393 |
# draw the map into the bitmap |
# draw the map into the bitmap |
394 |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
396 |
# Pass the entire bitmap as update region to the renderer. |
# Pass the entire bitmap as update region to the renderer. |
397 |
# We're redrawing the whole bitmap, after all. |
# We're redrawing the whole bitmap, after all. |
398 |
renderer.RenderMap(self.map, (0, 0, width, height), |
renderer.RenderMap(self.map, (0, 0, width, height), |
399 |
selected_layer, selected_shape) |
selected_layer, selected_shapes) |
400 |
|
|
401 |
dc.EndDrawing() |
dc.EndDrawing() |
402 |
dc.SelectObject(wx.wxNullBitmap) |
dc.SelectObject(wx.wxNullBitmap) |
425 |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
426 |
self.projection_changed) |
self.projection_changed) |
427 |
self.map = map |
self.map = map |
428 |
|
self.selection.ClearSelection() |
429 |
if self.map is not None: |
if self.map is not None: |
430 |
for channel in redraw_channels: |
for channel in redraw_channels: |
431 |
self.map.Subscribe(channel, self.full_redraw) |
self.map.Subscribe(channel, self.full_redraw) |
472 |
|
|
473 |
def FitRectToWindow(self, rect): |
def FitRectToWindow(self, rect): |
474 |
"""Fit the rectangular region given by rect into the window. |
"""Fit the rectangular region given by rect into the window. |
475 |
|
|
476 |
Set scale so that rect (in projected coordinates) just fits into |
Set scale so that rect (in projected coordinates) just fits into |
477 |
the window and center it. |
the window and center it. |
478 |
""" |
""" |
490 |
|
|
491 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
492 |
"""Fit the map to the window |
"""Fit the map to the window |
493 |
|
|
494 |
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 |
495 |
center it in the window. |
center it in the window. |
496 |
""" |
""" |
641 |
self.full_redraw() |
self.full_redraw() |
642 |
|
|
643 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
644 |
"""Redraw the map. |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
645 |
|
# The selection object takes care that it only issues |
646 |
Receiver for the SELECTED_SHAPE messages. Try to redraw only |
# SHAPES_SELECTED messages when the set of selected shapes has |
647 |
when necessary. |
# actually changed, so we can do a full redraw unconditionally. |
648 |
""" |
# FIXME: We should perhaps try to limit the redraw to the are |
649 |
# A redraw is necessary when the display has to change, which |
# actually covered by the shapes before and after the selection |
650 |
# means that either the status changes from having no selection |
# change. |
651 |
# 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 |
|
652 |
|
|
653 |
def unprojected_rect_around_point(self, x, y, dist): |
def unprojected_rect_around_point(self, x, y, dist): |
654 |
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
822 |
# to deselect the currently selected layer, so we simply select |
# to deselect the currently selected layer, so we simply select |
823 |
# the already selected layer again. |
# the already selected layer again. |
824 |
if layer is None: |
if layer is None: |
825 |
layer = self.interactor.SelectedLayer() |
layer = self.selection.SelectedLayer() |
826 |
self.interactor.SelectLayerAndShape(layer, shape) |
shapes = [] |
827 |
|
else: |
828 |
|
shapes = [shape] |
829 |
|
self.selection.SelectShapes(layer, shapes) |
830 |
return result |
return result |
831 |
|
|
832 |
def LabelShapeAt(self, x, y): |
def LabelShapeAt(self, x, y): |