1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
|
# Frank Koormann <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
12 |
|
|
13 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
14 |
|
|
15 |
|
from Thuban import _ |
16 |
|
|
17 |
|
import sys |
18 |
|
import os.path |
19 |
|
|
20 |
from math import hypot |
from math import hypot |
21 |
|
|
22 |
from wxPython.wx import wxWindow,\ |
from wxPython.wx import wxWindow,\ |
23 |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
24 |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \ |
25 |
|
wxBITMAP_TYPE_XPM, wxBeginBusyCursor, wxEndBusyCursor, wxCursor, \ |
26 |
|
wxImageFromBitmap, wxPlatform |
27 |
|
|
28 |
|
# Export related stuff |
29 |
|
if wxPlatform == '__WXMSW__': |
30 |
|
from wxPython.wx import wxMetaFileDC |
31 |
|
from wxPython.wx import wxFileDialog, wxSAVE, wxOVERWRITE_PROMPT, wxID_OK |
32 |
|
|
33 |
from wxPython import wx |
from wxPython import wx |
34 |
|
|
35 |
from wxproj import point_in_polygon_shape, shape_centroid |
from wxproj import point_in_polygon_shape, shape_centroid |
36 |
|
|
|
|
|
37 |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \ |
38 |
LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED |
MAP_LAYERS_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED |
39 |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \ |
40 |
SHAPETYPE_POINT |
SHAPETYPE_POINT |
41 |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \ |
42 |
ALIGN_LEFT, ALIGN_RIGHT |
ALIGN_LEFT, ALIGN_RIGHT |
43 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
44 |
|
from Thuban.Model.color import Color |
45 |
|
|
46 |
|
import resource |
47 |
|
|
48 |
from renderer import ScreenRenderer, PrinterRender |
from selection import Selection |
49 |
|
from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer |
50 |
|
|
51 |
import labeldialog |
import labeldialog |
52 |
|
|
53 |
from messages import SELECTED_SHAPE, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
54 |
|
SCALE_CHANGED |
55 |
|
|
56 |
|
|
57 |
# |
# |
231 |
self.view.LabelShapeAt(event.m_x, event.m_y) |
self.view.LabelShapeAt(event.m_x, event.m_y) |
232 |
|
|
233 |
|
|
|
|
|
|
|
|
234 |
class MapPrintout(wx.wxPrintout): |
class MapPrintout(wx.wxPrintout): |
235 |
|
|
236 |
""" |
""" |
237 |
wxPrintout class for printing Thuban maps |
wxPrintout class for printing Thuban maps |
238 |
""" |
""" |
239 |
|
|
240 |
def __init__(self, map): |
def __init__(self, canvas, map, region, selected_layer, selected_shapes): |
241 |
wx.wxPrintout.__init__(self) |
wx.wxPrintout.__init__(self) |
242 |
|
self.canvas = canvas |
243 |
self.map = map |
self.map = map |
244 |
|
self.region = region |
245 |
|
self.selected_layer = selected_layer |
246 |
|
self.selected_shapes = selected_shapes |
247 |
|
|
248 |
def GetPageInfo(self): |
def GetPageInfo(self): |
249 |
return (1, 1, 1, 1) |
return (1, 1, 1, 1) |
257 |
|
|
258 |
def draw_on_dc(self, dc): |
def draw_on_dc(self, dc): |
259 |
width, height = self.GetPageSizePixels() |
width, height = self.GetPageSizePixels() |
260 |
llx, lly, urx, ury = self.map.ProjectedBoundingBox() |
scale, offset, mapregion = OutputTransform(self.canvas.scale, |
261 |
scalex = width / (urx - llx) |
self.canvas.offset, |
262 |
scaley = height / (ury - lly) |
self.canvas.GetSizeTuple(), |
263 |
scale = min(scalex, scaley) |
self.GetPageSizePixels()) |
|
offx = 0.5 * (width - (urx + llx) * scale) |
|
|
offy = 0.5 * (height + (ury + lly) * scale) |
|
|
|
|
264 |
resx, resy = self.GetPPIPrinter() |
resx, resy = self.GetPPIPrinter() |
265 |
renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx) |
renderer = PrinterRenderer(dc, scale, offset, resolution = resy) |
266 |
renderer.RenderMap(self.map) |
x, y, width, height = self.region |
267 |
return wx.true |
canvas_scale = self.canvas.scale |
268 |
|
renderer.RenderMap(self.map, |
269 |
|
(0,0, |
270 |
|
(width/canvas_scale)*scale, |
271 |
|
(height/canvas_scale)*scale), |
272 |
|
mapregion, |
273 |
|
self.selected_layer, self.selected_shapes) |
274 |
|
return True |
275 |
|
|
276 |
|
|
277 |
class MapCanvas(wxWindow, Publisher): |
class MapCanvas(wxWindow, Publisher): |
278 |
|
|
279 |
"""A widget that displays a map and offers some interaction""" |
"""A widget that displays a map and offers some interaction""" |
280 |
|
|
281 |
def __init__(self, parent, winid, interactor): |
# Some messages that can be subscribed/unsubscribed directly through |
282 |
|
# the MapCanvas come in fact from other objects. This is a dict |
283 |
|
# mapping those messages to the names of the instance variables they |
284 |
|
# actually come from. The delegation is implemented in the Subscribe |
285 |
|
# and Unsubscribe methods |
286 |
|
delegated_messages = {LAYER_SELECTED: "selection", |
287 |
|
SHAPES_SELECTED: "selection"} |
288 |
|
|
289 |
|
# Methods delegated to some instance variables. The delegation is |
290 |
|
# implemented in the __getattr__ method. |
291 |
|
delegated_methods = {"SelectLayer": "selection", |
292 |
|
"SelectShapes": "selection", |
293 |
|
"SelectedLayer": "selection", |
294 |
|
"HasSelectedLayer": "selection", |
295 |
|
"HasSelectedShapes": "selection", |
296 |
|
"SelectedShapes": "selection"} |
297 |
|
|
298 |
|
def __init__(self, parent, winid): |
299 |
wxWindow.__init__(self, parent, winid) |
wxWindow.__init__(self, parent, winid) |
300 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
301 |
|
|
318 |
# if the mouse is outside the window. |
# if the mouse is outside the window. |
319 |
self.current_position = None |
self.current_position = None |
320 |
|
|
|
# If true, OnIdle will call do_redraw to do the actual |
|
|
# redrawing. Set by OnPaint to avoid some unnecessary redraws. |
|
|
# To force a redraw call full_redraw(). |
|
|
self.redraw_on_idle = 0 |
|
|
|
|
|
# The region to update when idle |
|
|
self.update_region = wx.wxRegion() |
|
|
|
|
321 |
# the bitmap serving as backing store |
# the bitmap serving as backing store |
322 |
self.bitmap = None |
self.bitmap = None |
323 |
|
|
324 |
# the interactor |
# the selection |
325 |
self.interactor = interactor |
self.selection = Selection() |
326 |
self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected) |
self.selection.Subscribe(SHAPES_SELECTED , self.shape_selected) |
327 |
|
|
328 |
# keep track of which layers/shapes are selected to make sure we |
# keep track of which layers/shapes are selected to make sure we |
329 |
# only redraw when necessary |
# only redraw when necessary |
337 |
EVT_MOTION(self, self.OnMotion) |
EVT_MOTION(self, self.OnMotion) |
338 |
EVT_LEAVE_WINDOW(self, self.OnLeaveWindow) |
EVT_LEAVE_WINDOW(self, self.OnLeaveWindow) |
339 |
wx.EVT_SIZE(self, self.OnSize) |
wx.EVT_SIZE(self, self.OnSize) |
|
wx.EVT_IDLE(self, self.OnIdle) |
|
340 |
|
|
341 |
def __del__(self): |
def __del__(self): |
342 |
wxWindow.__del__(self) |
wxWindow.__del__(self) |
343 |
Publisher.__del__(self) |
Publisher.__del__(self) |
344 |
|
|
345 |
def OnPaint(self, event): |
def Subscribe(self, channel, *args): |
346 |
dc = wxPaintDC(self) |
"""Extend the inherited method to handle delegated messages. |
347 |
if self.map is not None and self.map.HasLayers(): |
|
348 |
# We have a non-empty map. Redraw it in idle time |
If channel is one of the delegated messages call the appropriate |
349 |
self.redraw_on_idle = 1 |
object's Subscribe method. Otherwise just call the inherited |
350 |
# update the region that has to be redrawn |
method. |
351 |
self.update_region.UnionRegion(self.GetUpdateRegion()) |
""" |
352 |
|
if channel in self.delegated_messages: |
353 |
|
object = getattr(self, self.delegated_messages[channel]) |
354 |
|
object.Subscribe(channel, *args) |
355 |
else: |
else: |
356 |
# If we've got no map or if the map is empty, simply clear |
Publisher.Subscribe(self, channel, *args) |
|
# the screen. |
|
357 |
|
|
358 |
# XXX it's probably possible to get rid of this. The |
def Unsubscribe(self, channel, *args): |
359 |
# background color of the window is already white and the |
"""Extend the inherited method to handle delegated messages. |
360 |
# only thing we may have to do is to call self.Refresh() |
|
361 |
# with a true argument in the right places. |
If channel is one of the delegated messages call the appropriate |
362 |
dc.BeginDrawing() |
object's Unsubscribe method. Otherwise just call the inherited |
363 |
dc.Clear() |
method. |
364 |
dc.EndDrawing() |
""" |
365 |
|
if channel in self.delegated_messages: |
366 |
|
object = getattr(self, self.delegated_messages[channel]) |
367 |
|
object.Unsubscribe(channel, *args) |
368 |
|
else: |
369 |
|
Publisher.Unsubscribe(self, channel, *args) |
370 |
|
|
371 |
# clear the region |
def __getattr__(self, attr): |
372 |
self.update_region = wx.wxRegion() |
if attr in self.delegated_methods: |
373 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
374 |
|
raise AttributeError(attr) |
375 |
|
|
376 |
|
def OnPaint(self, event): |
377 |
|
dc = wxPaintDC(self) |
378 |
|
clear = self.map is None or not self.map.HasLayers() |
379 |
|
|
380 |
|
wxBeginBusyCursor() |
381 |
|
try: |
382 |
|
if not clear: |
383 |
|
self.do_redraw() |
384 |
|
try: |
385 |
|
pass |
386 |
|
except: |
387 |
|
print "Error during drawing:", sys.exc_info()[0] |
388 |
|
clear = True |
389 |
|
|
390 |
|
if clear: |
391 |
|
# If we've got no map or if the map is empty, simply clear |
392 |
|
# the screen. |
393 |
|
|
394 |
|
# XXX it's probably possible to get rid of this. The |
395 |
|
# background color of the window is already white and the |
396 |
|
# only thing we may have to do is to call self.Refresh() |
397 |
|
# with a true argument in the right places. |
398 |
|
dc.BeginDrawing() |
399 |
|
dc.Clear() |
400 |
|
dc.EndDrawing() |
401 |
|
finally: |
402 |
|
wxEndBusyCursor() |
403 |
|
|
404 |
def do_redraw(self): |
def do_redraw(self): |
405 |
# This should only be called if we have a non-empty map. |
# This should only be called if we have a non-empty map. |
406 |
|
|
|
# get the update region and reset it. We're not actually using |
|
|
# it anymore, though. |
|
|
update_box = self.update_region.GetBox() |
|
|
self.update_region = wx.wxRegion() |
|
|
|
|
407 |
# Get the window size. |
# Get the window size. |
408 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
409 |
|
|
417 |
dc.BeginDrawing() |
dc.BeginDrawing() |
418 |
|
|
419 |
# clear the background |
# clear the background |
420 |
dc.SetBrush(wx.wxWHITE_BRUSH) |
#dc.SetBrush(wx.wxWHITE_BRUSH) |
421 |
dc.SetPen(wx.wxTRANSPARENT_PEN) |
#dc.SetPen(wx.wxTRANSPARENT_PEN) |
422 |
dc.DrawRectangle(0, 0, width, height) |
#dc.DrawRectangle(0, 0, width, height) |
423 |
|
dc.SetBackground(wx.wxWHITE_BRUSH) |
424 |
if 1: #self.interactor.selected_map is self.map: |
dc.Clear() |
425 |
selected_layer = self.interactor.selected_layer |
|
426 |
selected_shape = self.interactor.selected_shape |
selected_layer = self.selection.SelectedLayer() |
427 |
else: |
selected_shapes = self.selection.SelectedShapes() |
|
selected_layer = None |
|
|
selected_shape = None |
|
428 |
|
|
429 |
# draw the map into the bitmap |
# draw the map into the bitmap |
430 |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
renderer = ScreenRenderer(dc, self.scale, self.offset) |
431 |
|
|
432 |
# Pass the entire bitmap as update_region to the renderer. |
# Pass the entire bitmap as update region to the renderer. |
433 |
# We're redrawing the whole bitmap, after all. |
# We're redrawing the whole bitmap, after all. |
434 |
renderer.RenderMap(self.map, (0, 0, width, height), |
renderer.RenderMap(self.map, (0, 0, width, height), |
435 |
selected_layer, selected_shape) |
selected_layer, selected_shapes) |
436 |
|
|
437 |
dc.EndDrawing() |
dc.EndDrawing() |
438 |
dc.SelectObject(wx.wxNullBitmap) |
dc.SelectObject(wx.wxNullBitmap) |
446 |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
clientdc.Blit(0, 0, width, height, dc, 0, 0) |
447 |
clientdc.EndDrawing() |
clientdc.EndDrawing() |
448 |
|
|
449 |
|
def Export(self): |
450 |
|
if self.scale == 0: |
451 |
|
return |
452 |
|
|
453 |
|
if hasattr(self, "export_path"): |
454 |
|
export_path = self.export_path |
455 |
|
else: |
456 |
|
export_path="." |
457 |
|
dlg = wxFileDialog(self, _("Export Map"), export_path, "", |
458 |
|
"Enhanced Metafile (*.wmf)|*.wmf", |
459 |
|
wxSAVE|wxOVERWRITE_PROMPT) |
460 |
|
if dlg.ShowModal() == wxID_OK: |
461 |
|
self.export_path = os.path.dirname(dlg.GetPath()) |
462 |
|
dc = wxMetaFileDC(dlg.GetPath()) |
463 |
|
|
464 |
|
scale, offset, mapregion = OutputTransform(self.scale, |
465 |
|
self.offset, |
466 |
|
self.GetSizeTuple(), |
467 |
|
dc.GetSizeTuple()) |
468 |
|
|
469 |
|
selected_layer = self.selection.SelectedLayer() |
470 |
|
selected_shapes = self.selection.SelectedShapes() |
471 |
|
|
472 |
|
renderer = ExportRenderer(dc, scale, offset) |
473 |
|
|
474 |
|
# Pass the entire bitmap as update region to the renderer. |
475 |
|
# We're redrawing the whole bitmap, after all. |
476 |
|
width, height = self.GetSizeTuple() |
477 |
|
renderer.RenderMap(self.map, |
478 |
|
(0,0, |
479 |
|
(width/self.scale)*scale, |
480 |
|
(height/self.scale)*scale), |
481 |
|
mapregion, |
482 |
|
selected_layer, selected_shapes) |
483 |
|
dc.EndDrawing() |
484 |
|
dc.Close() |
485 |
|
dlg.Destroy() |
486 |
|
|
487 |
def Print(self): |
def Print(self): |
488 |
printer = wx.wxPrinter() |
printer = wx.wxPrinter() |
489 |
printout = MapPrintout(self.map) |
width, height = self.GetSizeTuple() |
490 |
printer.Print(self, printout, wx.true) |
selected_layer = self.selection.SelectedLayer() |
491 |
|
selected_shapes = self.selection.SelectedShapes() |
492 |
|
|
493 |
|
printout = MapPrintout(self, self.map, (0, 0, width, height), |
494 |
|
selected_layer, selected_shapes) |
495 |
|
printer.Print(self, printout, True) |
496 |
printout.Destroy() |
printout.Destroy() |
497 |
|
|
498 |
def SetMap(self, map): |
def SetMap(self, map): |
499 |
redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED, |
redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED, |
500 |
LAYER_VISIBILITY_CHANGED) |
LAYER_VISIBILITY_CHANGED) |
501 |
if self.map is not None: |
if self.map is not None: |
502 |
for channel in redraw_channels: |
for channel in redraw_channels: |
504 |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
self.map.Unsubscribe(MAP_PROJECTION_CHANGED, |
505 |
self.projection_changed) |
self.projection_changed) |
506 |
self.map = map |
self.map = map |
507 |
|
self.selection.ClearSelection() |
508 |
if self.map is not None: |
if self.map is not None: |
509 |
for channel in redraw_channels: |
for channel in redraw_channels: |
510 |
self.map.Subscribe(channel, self.full_redraw) |
self.map.Subscribe(channel, self.full_redraw) |
516 |
self.full_redraw() |
self.full_redraw() |
517 |
|
|
518 |
def Map(self): |
def Map(self): |
519 |
|
"""Return the map displayed by this canvas""" |
520 |
return self.map |
return self.map |
521 |
|
|
522 |
def redraw(self, *args): |
def redraw(self, *args): |
532 |
|
|
533 |
def set_view_transform(self, scale, offset): |
def set_view_transform(self, scale, offset): |
534 |
self.scale = scale |
self.scale = scale |
535 |
|
|
536 |
self.offset = offset |
self.offset = offset |
537 |
self.full_redraw() |
self.full_redraw() |
538 |
|
self.issue(SCALE_CHANGED, scale) |
539 |
|
|
540 |
def proj_to_win(self, x, y): |
def proj_to_win(self, x, y): |
541 |
"""\ |
"""\ |
542 |
Return the point in window coords given by projected coordinates x y |
Return the point in window coords given by projected coordinates x y |
543 |
""" |
""" |
544 |
|
if self.scale == 0: |
545 |
|
return (0, 0) |
546 |
|
|
547 |
offx, offy = self.offset |
offx, offy = self.offset |
548 |
return (self.scale * x + offx, -self.scale * y + offy) |
return (self.scale * x + offx, -self.scale * y + offy) |
549 |
|
|
551 |
"""\ |
"""\ |
552 |
Return the point in projected coordinates given by window coords x y |
Return the point in projected coordinates given by window coords x y |
553 |
""" |
""" |
554 |
|
if self.scale == 0: |
555 |
|
return (0, 0) |
556 |
|
|
557 |
offx, offy = self.offset |
offx, offy = self.offset |
558 |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
return ((x - offx) / self.scale, (offy - y) / self.scale) |
559 |
|
|
560 |
def FitRectToWindow(self, rect): |
def FitRectToWindow(self, rect): |
561 |
"""Fit the rectangular region given by rect into the window. |
"""Fit the rectangular region given by rect into the window. |
562 |
|
|
563 |
Set scale so that rect (in projected coordinates) just fits into |
Set scale so that rect (in projected coordinates) just fits into |
564 |
the window and center it. |
the window and center it. |
565 |
""" |
""" |
566 |
width, height = self.GetSizeTuple() |
width, height = self.GetSizeTuple() |
567 |
llx, lly, urx, ury = rect |
llx, lly, urx, ury = rect |
568 |
if llx == urx or lly == ury: |
if llx == urx or lly == ury: |
569 |
# zero with or zero height. Do Nothing |
# zero width or zero height. Do Nothing |
570 |
return |
return |
571 |
scalex = width / (urx - llx) |
scalex = width / (urx - llx) |
572 |
scaley = height / (ury - lly) |
scaley = height / (ury - lly) |
577 |
|
|
578 |
def FitMapToWindow(self): |
def FitMapToWindow(self): |
579 |
"""Fit the map to the window |
"""Fit the map to the window |
580 |
|
|
581 |
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 |
582 |
center it in the window. |
center it in the window. |
583 |
""" |
""" |
585 |
if bbox is not None: |
if bbox is not None: |
586 |
self.FitRectToWindow(bbox) |
self.FitRectToWindow(bbox) |
587 |
|
|
588 |
|
def FitLayerToWindow(self, layer): |
589 |
|
"""Fit the given layer to the window. |
590 |
|
|
591 |
|
Set the scale so that the layer fits exactly into the window and |
592 |
|
center it in the window. |
593 |
|
""" |
594 |
|
|
595 |
|
bbox = layer.LatLongBoundingBox() |
596 |
|
if bbox is not None: |
597 |
|
proj = self.map.GetProjection() |
598 |
|
if proj is not None: |
599 |
|
bbox = proj.ForwardBBox(bbox) |
600 |
|
|
601 |
|
if bbox is not None: |
602 |
|
self.FitRectToWindow(bbox) |
603 |
|
|
604 |
|
def FitSelectedToWindow(self): |
605 |
|
layer = self.selection.SelectedLayer() |
606 |
|
shapes = self.selection.SelectedShapes() |
607 |
|
|
608 |
|
bbox = layer.ShapesBoundingBox(shapes) |
609 |
|
if bbox is not None: |
610 |
|
proj = self.map.GetProjection() |
611 |
|
if proj is not None: |
612 |
|
bbox = proj.ForwardBBox(bbox) |
613 |
|
|
614 |
|
if bbox is not None: |
615 |
|
if len(shapes) == 1 and layer.ShapeType() == SHAPETYPE_POINT: |
616 |
|
self.ZoomFactor(1, self.proj_to_win(bbox[0], bbox[1])) |
617 |
|
else: |
618 |
|
self.FitRectToWindow(bbox) |
619 |
|
|
620 |
def ZoomFactor(self, factor, center = None): |
def ZoomFactor(self, factor, center = None): |
621 |
"""Multiply the zoom by factor and center on center. |
"""Multiply the zoom by factor and center on center. |
622 |
|
|
624 |
that should be centered. If it is omitted, it defaults to the |
that should be centered. If it is omitted, it defaults to the |
625 |
center of the window |
center of the window |
626 |
""" |
""" |
627 |
width, height = self.GetSizeTuple() |
if self.scale > 0: |
628 |
scale = self.scale * factor |
width, height = self.GetSizeTuple() |
629 |
offx, offy = self.offset |
scale = self.scale * factor |
630 |
if center is not None: |
offx, offy = self.offset |
631 |
cx, cy = center |
if center is not None: |
632 |
else: |
cx, cy = center |
633 |
cx = width / 2 |
else: |
634 |
cy = height / 2 |
cx = width / 2 |
635 |
offset = (factor * (offx - cx) + width / 2, |
cy = height / 2 |
636 |
factor * (offy - cy) + height / 2) |
offset = (factor * (offx - cx) + width / 2, |
637 |
self.set_view_transform(scale, offset) |
factor * (offy - cy) + height / 2) |
638 |
|
self.set_view_transform(scale, offset) |
639 |
|
|
640 |
def ZoomOutToRect(self, rect): |
def ZoomOutToRect(self, rect): |
641 |
"""Zoom out to fit the currently visible region into rect. |
"""Zoom out to fit the currently visible region into rect. |
658 |
self.set_view_transform(scale, (offx, offy)) |
self.set_view_transform(scale, (offx, offy)) |
659 |
|
|
660 |
def Translate(self, dx, dy): |
def Translate(self, dx, dy): |
661 |
|
"""Move the map by dx, dy pixels""" |
662 |
offx, offy = self.offset |
offx, offy = self.offset |
663 |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
self.set_view_transform(self.scale, (offx + dx, offy + dy)) |
664 |
|
|
665 |
|
def SelectTool(self, tool): |
666 |
|
"""Make tool the active tool. |
667 |
|
|
668 |
|
The parameter should be an instance of Tool or None to indicate |
669 |
|
that no tool is active. |
670 |
|
""" |
671 |
|
self.tool = tool |
672 |
|
|
673 |
def ZoomInTool(self): |
def ZoomInTool(self): |
674 |
self.tool = ZoomInTool(self) |
"""Start the zoom in tool""" |
675 |
|
self.SelectTool(ZoomInTool(self)) |
676 |
|
|
677 |
def ZoomOutTool(self): |
def ZoomOutTool(self): |
678 |
self.tool = ZoomOutTool(self) |
"""Start the zoom out tool""" |
679 |
|
self.SelectTool(ZoomOutTool(self)) |
680 |
|
|
681 |
def PanTool(self): |
def PanTool(self): |
682 |
self.tool = PanTool(self) |
"""Start the pan tool""" |
683 |
|
self.SelectTool(PanTool(self)) |
684 |
|
#img = resource.GetImageResource("pan", wxBITMAP_TYPE_XPM) |
685 |
|
#bmp = resource.GetBitmapResource("pan", wxBITMAP_TYPE_XPM) |
686 |
|
#print bmp |
687 |
|
#img = wxImageFromBitmap(bmp) |
688 |
|
#print img |
689 |
|
#cur = wxCursor(img) |
690 |
|
#print cur |
691 |
|
#self.SetCursor(cur) |
692 |
|
|
693 |
def IdentifyTool(self): |
def IdentifyTool(self): |
694 |
self.tool = IdentifyTool(self) |
"""Start the identify tool""" |
695 |
|
self.SelectTool(IdentifyTool(self)) |
696 |
|
|
697 |
def LabelTool(self): |
def LabelTool(self): |
698 |
self.tool = LabelTool(self) |
"""Start the label tool""" |
699 |
|
self.SelectTool(LabelTool(self)) |
700 |
|
|
701 |
def CurrentTool(self): |
def CurrentTool(self): |
702 |
|
"""Return the name of the current tool or None if no tool is active""" |
703 |
return self.tool and self.tool.Name() or None |
return self.tool and self.tool.Name() or None |
704 |
|
|
705 |
def CurrentPosition(self): |
def CurrentPosition(self): |
742 |
self.set_current_position(event) |
self.set_current_position(event) |
743 |
if self.dragging: |
if self.dragging: |
744 |
self.ReleaseMouse() |
self.ReleaseMouse() |
745 |
self.tool.Hide(self.drag_dc) |
try: |
746 |
self.tool.MouseUp(event) |
self.tool.Hide(self.drag_dc) |
747 |
self.drag_dc = None |
self.tool.MouseUp(event) |
748 |
self.dragging = 0 |
finally: |
749 |
|
self.drag_dc = None |
750 |
|
self.dragging = 0 |
751 |
|
|
752 |
def OnMotion(self, event): |
def OnMotion(self, event): |
753 |
self.set_current_position(event) |
self.set_current_position(event) |
759 |
def OnLeaveWindow(self, event): |
def OnLeaveWindow(self, event): |
760 |
self.set_current_position(None) |
self.set_current_position(None) |
761 |
|
|
|
def OnIdle(self, event): |
|
|
if self.redraw_on_idle: |
|
|
self.do_redraw() |
|
|
self.redraw_on_idle = 0 |
|
|
|
|
762 |
def OnSize(self, event): |
def OnSize(self, event): |
763 |
# the window's size has changed. We have to get a new bitmap. If |
# the window's size has changed. We have to get a new bitmap. If |
764 |
# we want to be clever we could try to get by without throwing |
# we want to be clever we could try to get by without throwing |
767 |
# Even when the window becomes larger some parts of the bitmap |
# Even when the window becomes larger some parts of the bitmap |
768 |
# could be reused. |
# could be reused. |
769 |
self.full_redraw() |
self.full_redraw() |
770 |
|
pass |
771 |
|
|
772 |
def shape_selected(self, layer, shape): |
def shape_selected(self, layer, shape): |
773 |
"""Redraw the map. |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
774 |
|
# The selection object takes care that it only issues |
775 |
Receiver for the SELECTED_SHAPE messages. Try to redraw only |
# SHAPES_SELECTED messages when the set of selected shapes has |
776 |
when necessary. |
# actually changed, so we can do a full redraw unconditionally. |
777 |
""" |
# FIXME: We should perhaps try to limit the redraw to the are |
778 |
# A redraw is necessary when the display has to change, which |
# actually covered by the shapes before and after the selection |
779 |
# means that either the status changes from having no selection |
# change. |
780 |
# 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 |
|
781 |
|
|
782 |
def unprojected_rect_around_point(self, x, y): |
def unprojected_rect_around_point(self, x, y, dist): |
783 |
"""return a rect a few pixels around (x, y) in unprojected corrdinates |
"""return a rect dist pixels around (x, y) in unprojected corrdinates |
784 |
|
|
785 |
The return value is a tuple (minx, miny, maxx, maxy) suitable a |
The return value is a tuple (minx, miny, maxx, maxy) suitable a |
786 |
parameter to a layer's ShapesInRegion method. |
parameter to a layer's ShapesInRegion method. |
794 |
xs = [] |
xs = [] |
795 |
ys = [] |
ys = [] |
796 |
for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)): |
for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)): |
797 |
px, py = self.win_to_proj(x + dx, y + dy) |
px, py = self.win_to_proj(x + dist * dx, y + dist * dy) |
798 |
if inverse: |
if inverse: |
799 |
px, py = inverse(px, py) |
px, py = inverse(px, py) |
800 |
xs.append(px) |
xs.append(px) |
821 |
forward = None |
forward = None |
822 |
|
|
823 |
scale = self.scale |
scale = self.scale |
|
offx, offy = self.offset |
|
824 |
|
|
825 |
box = self.unprojected_rect_around_point(px, py) |
if scale == 0: |
826 |
|
return None, None |
827 |
|
|
828 |
|
offx, offy = self.offset |
829 |
|
|
830 |
if select_labels: |
if select_labels: |
831 |
labels = self.map.LabelLayer().Labels() |
labels = self.map.LabelLayer().Labels() |
873 |
if not layer.Visible(): |
if not layer.Visible(): |
874 |
continue |
continue |
875 |
|
|
876 |
filled = layer.fill is not None |
filled = layer.GetClassification().GetDefaultFill() \ |
877 |
stroked = layer.stroke is not None |
is not Color.Transparent |
878 |
|
stroked = layer.GetClassification().GetDefaultLineColor() \ |
879 |
|
is not Color.Transparent |
880 |
|
|
881 |
layer_proj = layer.projection |
layer_proj = layer.projection |
882 |
if layer_proj is not None: |
if layer_proj is not None: |
888 |
|
|
889 |
select_shape = -1 |
select_shape = -1 |
890 |
|
|
891 |
|
# Determine the ids of the shapes that overlap a tiny area |
892 |
|
# around the point. For layers containing points we have to |
893 |
|
# choose a larger size of the box we're testing agains so |
894 |
|
# that we take the size of the markers into account |
895 |
|
# FIXME: Once the markers are more flexible this part has to |
896 |
|
# become more flexible too, of course |
897 |
|
if shapetype == SHAPETYPE_POINT: |
898 |
|
box = self.unprojected_rect_around_point(px, py, 5) |
899 |
|
else: |
900 |
|
box = self.unprojected_rect_around_point(px, py, 1) |
901 |
shape_ids = layer.ShapesInRegion(box) |
shape_ids = layer.ShapesInRegion(box) |
902 |
shape_ids.reverse() |
shape_ids.reverse() |
903 |
|
|
955 |
# to deselect the currently selected layer, so we simply select |
# to deselect the currently selected layer, so we simply select |
956 |
# the already selected layer again. |
# the already selected layer again. |
957 |
if layer is None: |
if layer is None: |
958 |
layer = self.interactor.SelectedLayer() |
layer = self.selection.SelectedLayer() |
959 |
self.interactor.SelectLayerAndShape(layer, shape) |
shapes = [] |
960 |
|
else: |
961 |
|
shapes = [shape] |
962 |
|
self.selection.SelectShapes(layer, shapes) |
963 |
return result |
return result |
964 |
|
|
965 |
def LabelShapeAt(self, x, y): |
def LabelShapeAt(self, x, y): |
966 |
|
"""Add or remove a label at window position x, y. |
967 |
|
|
968 |
|
If there's a label at the given position, remove it. Otherwise |
969 |
|
determine the shape at the position, run the label dialog and |
970 |
|
unless the user cancels the dialog, add a laber. |
971 |
|
""" |
972 |
ox = x; oy = y |
ox = x; oy = y |
973 |
label_layer = self.map.LabelLayer() |
label_layer = self.map.LabelLayer() |
974 |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
1017 |
valign = ALIGN_CENTER |
valign = ALIGN_CENTER |
1018 |
label_layer.AddLabel(x, y, text, |
label_layer.AddLabel(x, y, text, |
1019 |
halign = halign, valign = valign) |
halign = halign, valign = valign) |
1020 |
|
|
1021 |
|
def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend): |
1022 |
|
"""Calculate dimensions to transform canvas content to output device.""" |
1023 |
|
width, height = device_extend |
1024 |
|
|
1025 |
|
# Only 80 % of the with are available for the map |
1026 |
|
width = width * 0.8 |
1027 |
|
|
1028 |
|
# Define the distance of the map from DC border |
1029 |
|
distance = 20 |
1030 |
|
|
1031 |
|
if height < width: |
1032 |
|
# landscape |
1033 |
|
map_height = height - 2*distance |
1034 |
|
map_width = map_height |
1035 |
|
else: |
1036 |
|
# portrait, recalibrate width (usually the legend width is too |
1037 |
|
# small |
1038 |
|
width = width * 0.9 |
1039 |
|
map_height = width - 2*distance |
1040 |
|
map_width = map_height |
1041 |
|
|
1042 |
|
mapregion = (distance, distance, |
1043 |
|
distance+map_width, distance+map_height) |
1044 |
|
|
1045 |
|
canvas_width, canvas_height = canvas_size |
1046 |
|
|
1047 |
|
scalex = map_width / (canvas_width/canvas_scale) |
1048 |
|
scaley = map_height / (canvas_height/canvas_scale) |
1049 |
|
scale = min(scalex, scaley) |
1050 |
|
canvas_offx, canvas_offy = canvas_offset |
1051 |
|
offx = scale*canvas_offx/canvas_scale |
1052 |
|
offy = scale*canvas_offy/canvas_scale |
1053 |
|
|
1054 |
|
return scale, (offx, offy), mapregion |