1 |
# Copyright (c) 2001 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
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,\ |
19 |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
20 |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \ |
21 |
|
wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \ |
22 |
|
wxImageFromBitmap |
23 |
|
|
24 |
|
|
25 |
from wxPython import wx |
from wxPython import wx |
28 |
|
|
29 |
|
|
30 |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
31 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED |
MAP_LAYERS_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED |
32 |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
33 |
SHAPETYPE_POINT |
SHAPETYPE_POINT |
34 |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
35 |
ALIGN_LEFT, ALIGN_RIGHT |
ALIGN_LEFT, ALIGN_RIGHT |
36 |
|
from Thuban.Lib.connector import Publisher |
37 |
|
from Thuban.Model.color import Color |
38 |
|
|
39 |
|
import resource |
40 |
|
|
41 |
|
from selection import Selection |
42 |
from renderer import ScreenRenderer, PrinterRender |
from renderer import ScreenRenderer, PrinterRender |
43 |
|
|
44 |
import labeldialog |
import labeldialog |
45 |
|
|
46 |
from messages import SELECTED_SHAPE |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
47 |
|
SCALE_CHANGED |
48 |
|
|
49 |
|
|
50 |
# |
# |
136 |
def MouseUp(self, event): |
def MouseUp(self, event): |
137 |
if self.dragging: |
if self.dragging: |
138 |
Tool.MouseUp(self, event) |
Tool.MouseUp(self, event) |
139 |
self.view.FitRectToWindow(self.proj_rect()) |
sx, sy = self.start |
140 |
|
cx, cy = self.current |
141 |
|
if sx == cx or sy == cy: |
142 |
|
# Just a mouse click or a degenerate rectangle. Simply |
143 |
|
# zoom in by a factor of two |
144 |
|
# FIXME: For a click this is the desired behavior but should we |
145 |
|
# really do this for degenrate rectagles as well or |
146 |
|
# should we ignore them? |
147 |
|
self.view.ZoomFactor(2, center = (cx, cy)) |
148 |
|
else: |
149 |
|
# A drag. Zoom in to the rectangle |
150 |
|
self.view.FitRectToWindow(self.proj_rect()) |
151 |
|
|
152 |
|
|
153 |
class ZoomOutTool(RectTool): |
class ZoomOutTool(RectTool): |
154 |
|
|
155 |
"""The Zoom-Out Tool""" |
"""The Zoom-Out Tool""" |
156 |
|
|
157 |
def Name(self): |
def Name(self): |
158 |
return "ZoomOutTool" |
return "ZoomOutTool" |
159 |
|
|
162 |
Tool.MouseUp(self, event) |
Tool.MouseUp(self, event) |
163 |
sx, sy = self.start |
sx, sy = self.start |
164 |
cx, cy = self.current |
cx, cy = self.current |
165 |
self.view.ZoomOutToRect((min(sx, cx), min(sy, cy), |
if sx == cx or sy == cy: |
166 |
max(sx, cx), max(sy, cy))) |
# Just a mouse click or a degenerate rectangle. Simply |
167 |
|
# zoom out by a factor of two. |
168 |
|
# FIXME: For a click this is the desired behavior but should we |
169 |
|
# really do this for degenrate rectagles as well or |
170 |
|
# should we ignore them? |
171 |
|
self.view.ZoomFactor(0.5, center = (cx, cy)) |
172 |
|
else: |
173 |
|
# A drag. Zoom out to the rectangle |
174 |
|
self.view.ZoomOutToRect((min(sx, cx), min(sy, cy), |
175 |
|
max(sx, cx), max(sy, cy))) |
176 |
|
|
177 |
|
|
178 |
class PanTool(Tool): |
class PanTool(Tool): |
184 |
|
|
185 |
def MouseMove(self, event): |
def MouseMove(self, event): |
186 |
if self.dragging: |
if self.dragging: |
|
x0, y0 = self.current |
|
187 |
Tool.MouseMove(self, event) |
Tool.MouseMove(self, event) |
188 |
|
sx, sy = self.start |
189 |
x, y = self.current |
x, y = self.current |
190 |
width, height = self.view.GetSizeTuple() |
width, height = self.view.GetSizeTuple() |
191 |
|
|
192 |
|
bitmapdc = wx.wxMemoryDC() |
193 |
|
bitmapdc.SelectObject(self.view.bitmap) |
194 |
|
|
195 |
dc = self.view.drag_dc |
dc = self.view.drag_dc |
196 |
dc.Blit(0, 0, width, height, dc, x0 - x, y0 - y) |
dc.Blit(0, 0, width, height, bitmapdc, sx - x, sy - y) |
197 |
|
|
198 |
def MouseUp(self, event): |
def MouseUp(self, event): |
199 |
if self.dragging: |
if self.dragging: |
201 |
sx, sy = self.start |
sx, sy = self.start |
202 |
cx, cy = self.current |
cx, cy = self.current |
203 |
self.view.Translate(cx - sx, cy - sy) |
self.view.Translate(cx - sx, cy - sy) |
204 |
|
|
205 |
class IdentifyTool(Tool): |
class IdentifyTool(Tool): |
206 |
|
|
207 |
"""The "Identify" Tool""" |
"""The "Identify" Tool""" |
208 |
|
|
209 |
def Name(self): |
def Name(self): |
210 |
return "IdentifyTool" |
return "IdentifyTool" |
211 |
|
|
259 |
renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx) |
renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx) |
260 |
renderer.RenderMap(self.map) |
renderer.RenderMap(self.map) |
261 |
return wx.true |
return wx.true |
|
|
|
262 |
|
|
263 |
class MapCanvas(wxWindow): |
|
264 |
|
class MapCanvas(wxWindow, Publisher): |
265 |
|
|
266 |
"""A widget that displays a map and offers some interaction""" |
"""A widget that displays a map and offers some interaction""" |
267 |
|
|
268 |
|
# Some messages that can be subscribed/unsubscribed directly through |
269 |
|
# the MapCanvas come in fact from other objects. This is a dict |
270 |
|
# mapping those messages to the names of the instance variables they |
271 |
|
# actually come from. The delegation is implemented in the Subscribe |
272 |
|
# and Unsubscribe methods |
273 |
|
delegated_messages = {LAYER_SELECTED: "selection", |
274 |
|
SHAPES_SELECTED: "selection"} |
275 |
|
|
276 |
|
# Methods delegated to some instance variables. The delegation is |
277 |
|
# implemented in the __getattr__ method. |
278 |
|
delegated_methods = {"SelectLayer": "selection", |
279 |
|
"SelectShapes": "selection", |
280 |
|
"SelectedLayer": "selection", |
281 |
|
"HasSelectedLayer": "selection", |
282 |
|
"HasSelectedShapes": "selection"} |
283 |
|
|
284 |
def __init__(self, parent, winid): |
def __init__(self, parent, winid): |
285 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
286 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
287 |
|
|
288 |
|
# the map displayed in this canvas. Set with SetMap() |
289 |
self.map = None |
self.map = None |
290 |
|
|
291 |
|
# scale and offset describe the transformation from projected |
292 |
|
# coordinates to window coordinates. |
293 |
self.scale = 1.0 |
self.scale = 1.0 |
294 |
self.offset = (0, 0) |
self.offset = (0, 0) |
295 |
|
|
296 |
|
# whether the user is currently dragging the mouse, i.e. moving |
297 |
|
# the mouse while pressing a mouse button |
298 |
self.dragging = 0 |
self.dragging = 0 |
299 |
|
|
300 |
|
# the currently active tool |
301 |
self.tool = None |
self.tool = None |
302 |
self.redraw_on_idle = 0 |
|
303 |
|
# The current mouse position of the last OnMotion event or None |
304 |
|
# if the mouse is outside the window. |
305 |
|
self.current_position = None |
306 |
|
|
307 |
|
# the bitmap serving as backing store |
308 |
|
self.bitmap = None |
309 |
|
|
310 |
|
# the selection |
311 |
|
self.selection = Selection() |
312 |
|
self.selection.Subscribe(SHAPES_SELECTED , self.shape_selected) |
313 |
|
|
314 |
|
# keep track of which layers/shapes are selected to make sure we |
315 |
|
# only redraw when necessary |
316 |
|
self.last_selected_layer = None |
317 |
|
self.last_selected_shape = None |
318 |
|
|
319 |
|
# subscribe the WX events we're interested in |
320 |
EVT_PAINT(self, self.OnPaint) |
EVT_PAINT(self, self.OnPaint) |
321 |
EVT_LEFT_DOWN(self, self.OnLeftDown) |
EVT_LEFT_DOWN(self, self.OnLeftDown) |
322 |
EVT_LEFT_UP(self, self.OnLeftUp) |
EVT_LEFT_UP(self, self.OnLeftUp) |
323 |
EVT_MOTION(self, self.OnMotion) |
EVT_MOTION(self, self.OnMotion) |
324 |
wx.EVT_IDLE(self, self.OnIdle) |
EVT_LEAVE_WINDOW(self, self.OnLeaveWindow) |
325 |
import main |
wx.EVT_SIZE(self, self.OnSize) |
326 |
self.interactor = main.app.interactor |
|
327 |
self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected) |
def __del__(self): |
328 |
|
wxWindow.__del__(self) |
329 |
|
Publisher.__del__(self) |
330 |
|
|
331 |
|
def Subscribe(self, channel, *args): |
332 |
|
"""Extend the inherited method to handle delegated messages. |
333 |
|
|
334 |
|
If channel is one of the delegated messages call the appropriate |
335 |
|
object's Subscribe method. Otherwise just call the inherited |
336 |
|
method. |
337 |
|
""" |
338 |
|
if channel in self.delegated_messages: |
339 |
|
object = getattr(self, self.delegated_messages[channel]) |
340 |
|
object.Subscribe(channel, *args) |
341 |
|
else: |
342 |
|
Publisher.Subscribe(self, channel, *args) |
343 |
|
|
344 |
|
def Unsubscribe(self, channel, *args): |
345 |
|
"""Extend the inherited method to handle delegated messages. |
346 |
|
|
347 |
|
If channel is one of the delegated messages call the appropriate |
348 |
|
object's Unsubscribe method. Otherwise just call the inherited |
349 |
|
method. |
350 |
|
""" |
351 |
|
if channel in self.delegated_messages: |
352 |
|
object = getattr(self, self.delegated_messages[channel]) |
353 |
|
object.Unsubscribe(channel, *args) |
354 |
|
else: |
355 |
|
Publisher.Unsubscribe(self, channel, *args) |
356 |
|
|
357 |
|
def __getattr__(self, attr): |
358 |
|
if attr in self.delegated_methods: |
359 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
360 |
|
raise AttributeError(attr) |
361 |
|
|
362 |
def OnPaint(self, event): |
def OnPaint(self, event): |
363 |
dc = wxPaintDC(self) |
dc = wxPaintDC(self) |
364 |
if self.map is None or not self.map.HasLayers(): |
clear = self.map is None or not self.map.HasLayers() |
|
return |
|
|
self.redraw_on_idle = 1 |
|
365 |
|
|
366 |
def do_redraw(self): |
#wxBeginBusyCursor() |
|
width, height = self.GetSizeTuple() |
|
|
bitmap = wx.wxEmptyBitmap(width, height) |
|
367 |
|
|
368 |
dc = wx.wxMemoryDC() |
if not clear: |
369 |
dc.SelectObject(bitmap) |
try: |
370 |
|
self.do_redraw() |
371 |
|
except: |
372 |
|
print "Error during drawing:", sys.exc_info()[0] |
373 |
|
clear = True |
374 |
|
|
375 |
|
if clear: |
376 |
|
# If we've got no map or if the map is empty, simply clear |
377 |
|
# the screen. |
378 |
|
|
379 |
|
# XXX it's probably possible to get rid of this. The |
380 |
|
# background color of the window is already white and the |
381 |
|
# only thing we may have to do is to call self.Refresh() |
382 |
|
# with a true argument in the right places. |
383 |
|
dc.BeginDrawing() |
384 |
|
dc.Clear() |
385 |
|
dc.EndDrawing() |
386 |
|
|
387 |
|
#wxEndBusyCursor() |
388 |
|
|
389 |
|
def do_redraw(self): |
390 |
|
# This should only be called if we have a non-empty map. |
391 |
|
|
392 |
dc.BeginDrawing() |
# Get the window size. |
393 |
|
width, height = self.GetSizeTuple() |
394 |
|
|
395 |
dc.SetBrush(wx.wxWHITE_BRUSH) |
# If self.bitmap's still there, reuse it. Otherwise redraw it |
396 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
if self.bitmap is not None: |
397 |
dc.DrawRectangle(0, 0, width, height) |
bitmap = self.bitmap |
|
|
|
|
if 1: #self.interactor.selected_map is self.map: |
|
|
selected_layer = self.interactor.selected_layer |
|
|
selected_shape = self.interactor.selected_shape |
|
398 |
else: |
else: |
399 |
selected_layer = None |
bitmap = wx.wxEmptyBitmap(width, height) |
400 |
selected_shape = None |
dc = wx.wxMemoryDC() |
401 |
|
dc.SelectObject(bitmap) |
402 |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
dc.BeginDrawing() |
403 |
renderer.RenderMap(self.map, selected_layer, selected_shape) |
|
404 |
|
# clear the background |
405 |
|
#dc.SetBrush(wx.wxWHITE_BRUSH) |
406 |
|
#dc.SetPen(wx.wxTRANSPARENT_PEN) |
407 |
|
#dc.DrawRectangle(0, 0, width, height) |
408 |
|
dc.SetBackground(wx.wxWHITE_BRUSH) |
409 |
|
dc.Clear() |
410 |
|
|
411 |
|
selected_layer = self.selection.SelectedLayer() |
412 |
|
selected_shapes = self.selection.SelectedShapes() |
413 |
|
|
414 |
|
# draw the map into the bitmap |
415 |
|
renderer = ScreenRenderer(dc, self.scale, self.offset) |
416 |
|
|
417 |
|
# Pass the entire bitmap as update region to the renderer. |
418 |
|
# We're redrawing the whole bitmap, after all. |
419 |
|
renderer.RenderMap(self.map, (0, 0, width, height), |
420 |
|
selected_layer, selected_shapes) |
421 |
|
|
422 |
|
dc.EndDrawing() |
423 |
|
dc.SelectObject(wx.wxNullBitmap) |
424 |
|
self.bitmap = bitmap |
425 |
|
|
426 |
|
# blit the bitmap to the screen |
427 |
|
dc = wx.wxMemoryDC() |
428 |
|
dc.SelectObject(bitmap) |
429 |
clientdc = wxClientDC(self) |
clientdc = wxClientDC(self) |
430 |
clientdc.BeginDrawing() |
clientdc.BeginDrawing() |
431 |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
432 |
clientdc.EndDrawing() |
clientdc.EndDrawing() |
433 |
|
|
|
|
|
434 |
def Print(self): |
def Print(self): |
435 |
printer = wx.wxPrinter() |
printer = wx.wxPrinter() |
436 |
printout = MapPrintout(self.map) |
printout = MapPrintout(self.map) |
437 |
printer.Print(self, printout, wx.true) |
printer.Print(self, printout, wx.true) |
438 |
printout.Destroy() |
printout.Destroy() |
439 |
|
|
440 |
def SetMap(self, map): |
def SetMap(self, map): |
441 |
redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED, |
442 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
443 |
if self.map is not None: |
if self.map is not None: |
444 |
for channel in redraw_channels: |
for channel in redraw_channels: |
445 |
self.map.Unsubscribe(channel, self.redraw) |
self.map.Unsubscribe(channel, self.full_redraw) |
446 |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
447 |
self.projection_changed) |
self.projection_changed) |
448 |
self.map = map |
self.map = map |
449 |
|
self.selection.ClearSelection() |
450 |
if self.map is not None: |
if self.map is not None: |
451 |
for channel in redraw_channels: |
for channel in redraw_channels: |
452 |
self.map.Subscribe(channel, self.redraw) |
self.map.Subscribe(channel, self.full_redraw) |
453 |
self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed) |
self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed) |
454 |
self.FitMapToWindow() |
self.FitMapToWindow() |
455 |
|
# force a redraw. If map is not empty, it's already been called |
456 |
|
# by FitMapToWindow but if map is empty it hasn't been called |
457 |
|
# yet so we have to explicitly call it. |
458 |
|
self.full_redraw() |
459 |
|
|
460 |
def Map(self): |
def Map(self): |
461 |
|
"""Return the map displayed by this canvas""" |
462 |
return self.map |
return self.map |
463 |
|
|
464 |
def redraw(self, *args): |
def redraw(self, *args): |
465 |
self.Refresh(0) |
self.Refresh(0) |
466 |
|
|
467 |
|
def full_redraw(self, *args): |
468 |
|
self.bitmap = None |
469 |
|
self.redraw() |
470 |
|
|
471 |
def projection_changed(self, *args): |
def projection_changed(self, *args): |
472 |
self.FitMapToWindow() |
self.FitMapToWindow() |
473 |
self.redraw() |
self.full_redraw() |
474 |
|
|
475 |
def set_view_transform(self, scale, offset): |
def set_view_transform(self, scale, offset): |
476 |
self.scale = scale |
self.scale = scale |
477 |
self.offset = offset |
self.offset = offset |
478 |
self.redraw() |
self.full_redraw() |
479 |
|
self.issue(SCALE_CHANGED, scale) |
480 |
|
|
481 |
def proj_to_win(self, x, y): |
def proj_to_win(self, x, y): |
482 |
"""\ |
"""\ |
493 |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
494 |
|
|
495 |
def FitRectToWindow(self, rect): |
def FitRectToWindow(self, rect): |
496 |
|
"""Fit the rectangular region given by rect into the window. |
497 |
|
|
498 |
|
Set scale so that rect (in projected coordinates) just fits into |
499 |
|
the window and center it. |
500 |
|
""" |
501 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
502 |
llx, lly, urx, ury = rect |
llx, lly, urx, ury = rect |
503 |
|
if llx == urx or lly == ury: |
504 |
|
# zero width or zero height. Do Nothing |
505 |
|
return |
506 |
scalex = width / (urx - llx) |
scalex = width / (urx - llx) |
507 |
scaley = height / (ury - lly) |
scaley = height / (ury - lly) |
508 |
scale = min(scalex, scaley) |
scale = min(scalex, scaley) |
511 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
512 |
|
|
513 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
514 |
"""\ |
"""Fit the map to the window |
515 |
Set the scale and offset so that the map is centered in the |
|
516 |
window |
Set the scale so that the map fits exactly into the window and |
517 |
|
center it in the window. |
518 |
""" |
""" |
519 |
bbox = self.map.ProjectedBoundingBox() |
bbox = self.map.ProjectedBoundingBox() |
520 |
if bbox is not None: |
if bbox is not None: |
521 |
self.FitRectToWindow(bbox) |
self.FitRectToWindow(bbox) |
522 |
|
|
523 |
def ZoomFactor(self, factor): |
def FitLayerToWindow(self, layer): |
524 |
|
"""Fit the given layer to the window. |
525 |
|
|
526 |
|
Set the scale so that the layer fits exactly into the window and |
527 |
|
center it in the window. |
528 |
|
""" |
529 |
|
|
530 |
|
bbox = layer.LatLongBoundingBox() |
531 |
|
if bbox is not None: |
532 |
|
proj = self.map.GetProjection() |
533 |
|
if proj is not None: |
534 |
|
bbox = proj.ForwardBBox(bbox) |
535 |
|
|
536 |
|
if bbox is not None: |
537 |
|
self.FitRectToWindow(bbox) |
538 |
|
|
539 |
|
def FitSelectedToWindow(self): |
540 |
|
layer = self.selection.SelectedLayer() |
541 |
|
shapes = self.selection.SelectedShapes() |
542 |
|
|
543 |
|
bbox = layer.ShapesBoundingBox(shapes) |
544 |
|
if bbox is not None: |
545 |
|
proj = self.map.GetProjection() |
546 |
|
if proj is not None: |
547 |
|
bbox = proj.ForwardBBox(bbox) |
548 |
|
|
549 |
|
if bbox is not None: |
550 |
|
self.FitRectToWindow(bbox) |
551 |
|
|
552 |
|
def ZoomFactor(self, factor, center = None): |
553 |
|
"""Multiply the zoom by factor and center on center. |
554 |
|
|
555 |
|
The optional parameter center is a point in window coordinates |
556 |
|
that should be centered. If it is omitted, it defaults to the |
557 |
|
center of the window |
558 |
|
""" |
559 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
560 |
scale = self.scale * factor |
scale = self.scale * factor |
561 |
offx, offy = self.offset |
offx, offy = self.offset |
562 |
offset = (factor * (offx - width / 2) + width / 2, |
if center is not None: |
563 |
factor * (offy - height / 2) + height / 2) |
cx, cy = center |
564 |
|
else: |
565 |
|
cx = width / 2 |
566 |
|
cy = height / 2 |
567 |
|
offset = (factor * (offx - cx) + width / 2, |
568 |
|
factor * (offy - cy) + height / 2) |
569 |
self.set_view_transform(scale, offset) |
self.set_view_transform(scale, offset) |
570 |
|
|
571 |
def ZoomOutToRect(self, rect): |
def ZoomOutToRect(self, rect): |
572 |
# rect is given in window coordinates |
"""Zoom out to fit the currently visible region into rect. |
573 |
|
|
574 |
|
The rect parameter is given in window coordinates |
575 |
|
""" |
576 |
# determine the bbox of the displayed region in projected |
# determine the bbox of the displayed region in projected |
577 |
# coordinates |
# coordinates |
578 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
589 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
590 |
|
|
591 |
def Translate(self, dx, dy): |
def Translate(self, dx, dy): |
592 |
|
"""Move the map by dx, dy pixels""" |
593 |
offx, offy = self.offset |
offx, offy = self.offset |
594 |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
595 |
|
|
596 |
|
def SelectTool(self, tool): |
597 |
|
"""Make tool the active tool. |
598 |
|
|
599 |
|
The parameter should be an instance of Tool or None to indicate |
600 |
|
that no tool is active. |
601 |
|
""" |
602 |
|
self.tool = tool |
603 |
|
|
604 |
def ZoomInTool(self): |
def ZoomInTool(self): |
605 |
self.tool = ZoomInTool(self) |
"""Start the zoom in tool""" |
606 |
|
self.SelectTool(ZoomInTool(self)) |
607 |
|
|
608 |
def ZoomOutTool(self): |
def ZoomOutTool(self): |
609 |
self.tool = ZoomOutTool(self) |
"""Start the zoom out tool""" |
610 |
|
self.SelectTool(ZoomOutTool(self)) |
611 |
|
|
612 |
def PanTool(self): |
def PanTool(self): |
613 |
self.tool = PanTool(self) |
"""Start the pan tool""" |
614 |
|
self.SelectTool(PanTool(self)) |
615 |
|
#img = resource.GetImageResource("pan", wxBITMAP_TYPE_XPM) |
616 |
|
#bmp = resource.GetBitmapResource("pan", wxBITMAP_TYPE_XPM) |
617 |
|
#print bmp |
618 |
|
#img = wxImageFromBitmap(bmp) |
619 |
|
#print img |
620 |
|
#cur = wxCursor(img) |
621 |
|
#print cur |
622 |
|
#self.SetCursor(cur) |
623 |
|
|
624 |
def IdentifyTool(self): |
def IdentifyTool(self): |
625 |
self.tool = IdentifyTool(self) |
"""Start the identify tool""" |
626 |
|
self.SelectTool(IdentifyTool(self)) |
627 |
|
|
628 |
def LabelTool(self): |
def LabelTool(self): |
629 |
self.tool = LabelTool(self) |
"""Start the label tool""" |
630 |
|
self.SelectTool(LabelTool(self)) |
631 |
|
|
632 |
def CurrentTool(self): |
def CurrentTool(self): |
633 |
|
"""Return the name of the current tool or None if no tool is active""" |
634 |
return self.tool and self.tool.Name() or None |
return self.tool and self.tool.Name() or None |
635 |
|
|
636 |
|
def CurrentPosition(self): |
637 |
|
"""Return current position of the mouse in projected coordinates. |
638 |
|
|
639 |
|
The result is a 2-tuple of floats with the coordinates. If the |
640 |
|
mouse is not in the window, the result is None. |
641 |
|
""" |
642 |
|
if self.current_position is not None: |
643 |
|
x, y = self.current_position |
644 |
|
return self.win_to_proj(x, y) |
645 |
|
else: |
646 |
|
return None |
647 |
|
|
648 |
|
def set_current_position(self, event): |
649 |
|
"""Set the current position from event |
650 |
|
|
651 |
|
Should be called by all events that contain mouse positions |
652 |
|
especially EVT_MOTION. The event paramete may be None to |
653 |
|
indicate the the pointer left the window. |
654 |
|
""" |
655 |
|
if event is not None: |
656 |
|
self.current_position = (event.m_x, event.m_y) |
657 |
|
else: |
658 |
|
self.current_position = None |
659 |
|
self.issue(VIEW_POSITION) |
660 |
|
|
661 |
def OnLeftDown(self, event): |
def OnLeftDown(self, event): |
662 |
|
self.set_current_position(event) |
663 |
if self.tool is not None: |
if self.tool is not None: |
664 |
self.drag_dc = wxClientDC(self) |
self.drag_dc = wxClientDC(self) |
665 |
self.drag_dc.SetLogicalFunction(wxINVERT) |
self.drag_dc.SetLogicalFunction(wxINVERT) |
668 |
self.tool.MouseDown(event) |
self.tool.MouseDown(event) |
669 |
self.tool.Show(self.drag_dc) |
self.tool.Show(self.drag_dc) |
670 |
self.dragging = 1 |
self.dragging = 1 |
671 |
|
|
672 |
def OnLeftUp(self, event): |
def OnLeftUp(self, event): |
673 |
self.ReleaseMouse() |
self.set_current_position(event) |
674 |
if self.dragging: |
if self.dragging: |
675 |
self.tool.Hide(self.drag_dc) |
self.ReleaseMouse() |
676 |
self.tool.MouseUp(event) |
try: |
677 |
self.drag_dc = None |
self.tool.Hide(self.drag_dc) |
678 |
self.dragging = 0 |
self.tool.MouseUp(event) |
679 |
|
finally: |
680 |
|
self.drag_dc = None |
681 |
|
self.dragging = 0 |
682 |
|
|
683 |
def OnMotion(self, event): |
def OnMotion(self, event): |
684 |
|
self.set_current_position(event) |
685 |
if self.dragging: |
if self.dragging: |
686 |
self.tool.Hide(self.drag_dc) |
self.tool.Hide(self.drag_dc) |
687 |
self.tool.MouseMove(event) |
self.tool.MouseMove(event) |
688 |
self.tool.Show(self.drag_dc) |
self.tool.Show(self.drag_dc) |
689 |
|
|
690 |
def OnIdle(self, event): |
def OnLeaveWindow(self, event): |
691 |
if self.redraw_on_idle: |
self.set_current_position(None) |
692 |
self.do_redraw() |
|
693 |
self.redraw_on_idle = 0 |
def OnSize(self, event): |
694 |
|
# the window's size has changed. We have to get a new bitmap. If |
695 |
|
# we want to be clever we could try to get by without throwing |
696 |
|
# everything away. E.g. when the window gets smaller, we could |
697 |
|
# either keep the bitmap or create the new one from the old one. |
698 |
|
# Even when the window becomes larger some parts of the bitmap |
699 |
|
# could be reused. |
700 |
|
self.full_redraw() |
701 |
|
pass |
702 |
|
|
703 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
704 |
self.redraw() |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
705 |
|
# The selection object takes care that it only issues |
706 |
|
# SHAPES_SELECTED messages when the set of selected shapes has |
707 |
|
# actually changed, so we can do a full redraw unconditionally. |
708 |
|
# FIXME: We should perhaps try to limit the redraw to the are |
709 |
|
# actually covered by the shapes before and after the selection |
710 |
|
# change. |
711 |
|
self.full_redraw() |
712 |
|
|
713 |
|
def unprojected_rect_around_point(self, x, y, dist): |
714 |
|
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
715 |
|
|
716 |
def find_shape_at(self, px, py, select_labels = 0): |
The return value is a tuple (minx, miny, maxx, maxy) suitable a |
717 |
"""Return a tuple shape at point px, py in window coords.""" |
parameter to a layer's ShapesInRegion method. |
718 |
|
""" |
719 |
|
map_proj = self.map.projection |
720 |
|
if map_proj is not None: |
721 |
|
inverse = map_proj.Inverse |
722 |
|
else: |
723 |
|
inverse = None |
724 |
|
|
725 |
|
xs = [] |
726 |
|
ys = [] |
727 |
|
for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)): |
728 |
|
px, py = self.win_to_proj(x + dist * dx, y + dist * dy) |
729 |
|
if inverse: |
730 |
|
px, py = inverse(px, py) |
731 |
|
xs.append(px) |
732 |
|
ys.append(py) |
733 |
|
return (min(xs), min(ys), max(xs), max(ys)) |
734 |
|
|
735 |
|
def find_shape_at(self, px, py, select_labels = 0, searched_layer = None): |
736 |
|
"""Determine the shape at point px, py in window coords |
737 |
|
|
738 |
|
Return the shape and the corresponding layer as a tuple (layer, |
739 |
|
shape). |
740 |
|
|
741 |
|
If the optional parameter select_labels is true (default false) |
742 |
|
search through the labels. If a label is found return it's index |
743 |
|
as the shape and None as the layer. |
744 |
|
|
745 |
|
If the optional parameter searched_layer is given (or not None |
746 |
|
which it defaults to), only search in that layer. |
747 |
|
""" |
748 |
map_proj = self.map.projection |
map_proj = self.map.projection |
749 |
if map_proj is not None: |
if map_proj is not None: |
750 |
forward = map_proj.Forward |
forward = map_proj.Forward |
756 |
|
|
757 |
if select_labels: |
if select_labels: |
758 |
labels = self.map.LabelLayer().Labels() |
labels = self.map.LabelLayer().Labels() |
759 |
|
|
760 |
if labels: |
if labels: |
761 |
dc = wxClientDC(self) |
dc = wxClientDC(self) |
762 |
font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL) |
font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL) |
763 |
dc.SetFont(font) |
dc.SetFont(font) |
764 |
for i in range(len(labels)): |
for i in range(len(labels) - 1, -1, -1): |
765 |
label = labels[i] |
label = labels[i] |
766 |
x = label.x |
x = label.x |
767 |
y = label.y |
y = label.y |
787 |
y = y - height/2 |
y = y - height/2 |
788 |
if x <= px < x + width and y <= py <= y + height: |
if x <= px < x + width and y <= py <= y + height: |
789 |
return None, i |
return None, i |
790 |
|
|
791 |
layers = self.map.Layers() |
if searched_layer: |
792 |
|
layers = [searched_layer] |
793 |
|
else: |
794 |
|
layers = self.map.Layers() |
795 |
|
|
796 |
for layer_index in range(len(layers) - 1, -1, -1): |
for layer_index in range(len(layers) - 1, -1, -1): |
797 |
layer = layers[layer_index] |
layer = layers[layer_index] |
798 |
|
|
800 |
if not layer.Visible(): |
if not layer.Visible(): |
801 |
continue |
continue |
802 |
|
|
803 |
filled = layer.fill is not None |
filled = layer.GetClassification().GetDefaultFill() \ |
804 |
stroked = layer.stroke is not None |
is not Color.Transparent |
805 |
|
stroked = layer.GetClassification().GetDefaultLineColor() \ |
806 |
|
is not Color.Transparent |
807 |
|
|
808 |
layer_proj = layer.projection |
layer_proj = layer.projection |
809 |
if layer_proj is not None: |
if layer_proj is not None: |
810 |
inverse = layer_proj.Inverse |
inverse = layer_proj.Inverse |
811 |
else: |
else: |
812 |
inverse = None |
inverse = None |
813 |
|
|
814 |
shapetype = layer.ShapeType() |
shapetype = layer.ShapeType() |
815 |
|
|
816 |
select_shape = -1 |
select_shape = -1 |
817 |
|
|
818 |
|
# Determine the ids of the shapes that overlap a tiny area |
819 |
|
# around the point. For layers containing points we have to |
820 |
|
# choose a larger size of the box we're testing agains so |
821 |
|
# that we take the size of the markers into account |
822 |
|
# FIXME: Once the markers are more flexible this part has to |
823 |
|
# become more flexible too, of course |
824 |
|
if shapetype == SHAPETYPE_POINT: |
825 |
|
box = self.unprojected_rect_around_point(px, py, 5) |
826 |
|
else: |
827 |
|
box = self.unprojected_rect_around_point(px, py, 1) |
828 |
|
shape_ids = layer.ShapesInRegion(box) |
829 |
|
shape_ids.reverse() |
830 |
|
|
831 |
if shapetype == SHAPETYPE_POLYGON: |
if shapetype == SHAPETYPE_POLYGON: |
832 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
833 |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
834 |
i, |
i, |
835 |
filled, stroked, |
filled, stroked, |
840 |
select_shape = i |
select_shape = i |
841 |
break |
break |
842 |
elif shapetype == SHAPETYPE_ARC: |
elif shapetype == SHAPETYPE_ARC: |
843 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
844 |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
result = point_in_polygon_shape(layer.shapefile.cobject(), |
845 |
i, 0, 1, |
i, 0, 1, |
846 |
map_proj, layer_proj, |
map_proj, layer_proj, |
850 |
select_shape = i |
select_shape = i |
851 |
break |
break |
852 |
elif shapetype == SHAPETYPE_POINT: |
elif shapetype == SHAPETYPE_POINT: |
853 |
for i in range(layer.NumShapes()): |
for i in shape_ids: |
854 |
shape = layer.Shape(i) |
shape = layer.Shape(i) |
855 |
x, y = shape.Points()[0] |
x, y = shape.Points()[0] |
856 |
if inverse: |
if inverse: |
867 |
return layer, select_shape |
return layer, select_shape |
868 |
return None, None |
return None, None |
869 |
|
|
870 |
def SelectShapeAt(self, x, y): |
def SelectShapeAt(self, x, y, layer = None): |
871 |
layer, shape = self.find_shape_at(x, y) |
"""\ |
872 |
self.interactor.SelectLayerAndShape(layer, shape) |
Select and return the shape and its layer at window position (x, y) |
873 |
|
|
874 |
|
If layer is given, only search in that layer. If no layer is |
875 |
|
given, search through all layers. |
876 |
|
|
877 |
|
Return a tuple (layer, shapeid). If no shape is found, return |
878 |
|
(None, None). |
879 |
|
""" |
880 |
|
layer, shape = result = self.find_shape_at(x, y, searched_layer=layer) |
881 |
|
# If layer is None, then shape will also be None. We don't want |
882 |
|
# to deselect the currently selected layer, so we simply select |
883 |
|
# the already selected layer again. |
884 |
|
if layer is None: |
885 |
|
layer = self.selection.SelectedLayer() |
886 |
|
shapes = [] |
887 |
|
else: |
888 |
|
shapes = [shape] |
889 |
|
self.selection.SelectShapes(layer, shapes) |
890 |
|
return result |
891 |
|
|
892 |
def LabelShapeAt(self, x, y): |
def LabelShapeAt(self, x, y): |
893 |
|
"""Add or remove a label at window position x, y. |
894 |
|
|
895 |
|
If there's a label at the given position, remove it. Otherwise |
896 |
|
determine the shape at the position, run the label dialog and |
897 |
|
unless the user cancels the dialog, add a laber. |
898 |
|
""" |
899 |
ox = x; oy = y |
ox = x; oy = y |
900 |
label_layer = self.map.LabelLayer() |
label_layer = self.map.LabelLayer() |
901 |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |