1 |
bh |
404 |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
bh |
6 |
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
frank |
910 |
# Frank Koormann <[email protected]> |
5 |
bh |
6 |
# |
6 |
|
|
# This program is free software under the GPL (>=v2) |
7 |
|
|
# Read the file COPYING coming with Thuban for details. |
8 |
|
|
|
9 |
|
|
""" |
10 |
|
|
Classes for display of a map and interaction with it |
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
__version__ = "$Revision$" |
14 |
|
|
|
15 |
frank |
910 |
from Thuban import _ |
16 |
|
|
|
17 |
|
|
import os.path |
18 |
jonathan |
799 |
|
19 |
jonathan |
1285 |
from wxPython.wx import wxWindow, \ |
20 |
bh |
6 |
wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\ |
21 |
jonathan |
822 |
EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW, \ |
22 |
bh |
1456 |
wxPlatform, wxBeginBusyCursor, wxEndBusyCursor, wxFileDialog, wxSAVE, \ |
23 |
jonathan |
1385 |
wxOVERWRITE_PROMPT, wxID_OK |
24 |
bh |
6 |
|
25 |
frank |
910 |
# Export related stuff |
26 |
|
|
if wxPlatform == '__WXMSW__': |
27 |
|
|
from wxPython.wx import wxMetaFileDC |
28 |
bh |
6 |
|
29 |
|
|
from wxPython import wx |
30 |
|
|
|
31 |
bh |
1456 |
from Thuban.Model.messages import MAP_LAYERS_CHANGED, LAYER_CHANGED, \ |
32 |
|
|
LAYER_VISIBILITY_CHANGED |
33 |
bh |
6 |
|
34 |
frank |
910 |
from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer |
35 |
bh |
6 |
|
36 |
|
|
import labeldialog |
37 |
|
|
|
38 |
bh |
1454 |
from viewport import ViewPort, PanTool, output_transform |
39 |
bh |
6 |
|
40 |
jonathan |
1385 |
class CanvasPanTool(PanTool): |
41 |
bh |
6 |
|
42 |
jonathan |
1385 |
"""The Canvas Pan Tool""" |
43 |
bh |
6 |
|
44 |
|
|
def MouseMove(self, event): |
45 |
|
|
if self.dragging: |
46 |
jonathan |
1385 |
PanTool.MouseMove(self, event) |
47 |
bh |
57 |
sx, sy = self.start |
48 |
bh |
6 |
x, y = self.current |
49 |
|
|
width, height = self.view.GetSizeTuple() |
50 |
bh |
159 |
|
51 |
|
|
bitmapdc = wx.wxMemoryDC() |
52 |
|
|
bitmapdc.SelectObject(self.view.bitmap) |
53 |
|
|
|
54 |
bh |
6 |
dc = self.view.drag_dc |
55 |
bh |
159 |
dc.Blit(0, 0, width, height, bitmapdc, sx - x, sy - y) |
56 |
bh |
6 |
|
57 |
|
|
class MapPrintout(wx.wxPrintout): |
58 |
|
|
|
59 |
|
|
""" |
60 |
|
|
wxPrintout class for printing Thuban maps |
61 |
|
|
""" |
62 |
|
|
|
63 |
frank |
910 |
def __init__(self, canvas, map, region, selected_layer, selected_shapes): |
64 |
bh |
6 |
wx.wxPrintout.__init__(self) |
65 |
frank |
910 |
self.canvas = canvas |
66 |
bh |
6 |
self.map = map |
67 |
frank |
910 |
self.region = region |
68 |
|
|
self.selected_layer = selected_layer |
69 |
|
|
self.selected_shapes = selected_shapes |
70 |
bh |
6 |
|
71 |
|
|
def GetPageInfo(self): |
72 |
|
|
return (1, 1, 1, 1) |
73 |
|
|
|
74 |
|
|
def HasPage(self, pagenum): |
75 |
|
|
return pagenum == 1 |
76 |
|
|
|
77 |
|
|
def OnPrintPage(self, pagenum): |
78 |
|
|
if pagenum == 1: |
79 |
|
|
self.draw_on_dc(self.GetDC()) |
80 |
|
|
|
81 |
|
|
def draw_on_dc(self, dc): |
82 |
|
|
width, height = self.GetPageSizePixels() |
83 |
bh |
1454 |
scale, offset, mapregion = output_transform(self.canvas.scale, |
84 |
|
|
self.canvas.offset, |
85 |
|
|
self.canvas.GetSizeTuple(), |
86 |
|
|
self.GetPageSizePixels()) |
87 |
bh |
6 |
resx, resy = self.GetPPIPrinter() |
88 |
frank |
910 |
renderer = PrinterRenderer(dc, scale, offset, resolution = resy) |
89 |
|
|
x, y, width, height = self.region |
90 |
|
|
canvas_scale = self.canvas.scale |
91 |
|
|
renderer.RenderMap(self.map, |
92 |
|
|
(0,0, |
93 |
|
|
(width/canvas_scale)*scale, |
94 |
|
|
(height/canvas_scale)*scale), |
95 |
|
|
mapregion, |
96 |
|
|
self.selected_layer, self.selected_shapes) |
97 |
jan |
1035 |
return True |
98 |
bh |
6 |
|
99 |
jonathan |
1385 |
class MapCanvas(wxWindow, ViewPort): |
100 |
bh |
6 |
|
101 |
|
|
"""A widget that displays a map and offers some interaction""" |
102 |
|
|
|
103 |
bh |
535 |
def __init__(self, parent, winid): |
104 |
bh |
6 |
wxWindow.__init__(self, parent, winid) |
105 |
jonathan |
1385 |
ViewPort.__init__(self) |
106 |
|
|
|
107 |
bh |
6 |
self.SetBackgroundColour(wxColour(255, 255, 255)) |
108 |
bh |
125 |
|
109 |
|
|
# the bitmap serving as backing store |
110 |
|
|
self.bitmap = None |
111 |
|
|
|
112 |
jonathan |
1344 |
self.backgroundColor = wx.wxWHITE_BRUSH |
113 |
|
|
|
114 |
bh |
125 |
# subscribe the WX events we're interested in |
115 |
bh |
6 |
EVT_PAINT(self, self.OnPaint) |
116 |
|
|
EVT_LEFT_DOWN(self, self.OnLeftDown) |
117 |
|
|
EVT_LEFT_UP(self, self.OnLeftUp) |
118 |
|
|
EVT_MOTION(self, self.OnMotion) |
119 |
bh |
122 |
EVT_LEAVE_WINDOW(self, self.OnLeaveWindow) |
120 |
bh |
125 |
wx.EVT_SIZE(self, self.OnSize) |
121 |
jonathan |
1344 |
wx.EVT_IDLE(self, self.OnIdle) |
122 |
bh |
6 |
|
123 |
bh |
122 |
def __del__(self): |
124 |
|
|
wxWindow.__del__(self) |
125 |
jonathan |
1385 |
ViewPort.__del__(self) |
126 |
bh |
122 |
|
127 |
jonathan |
1385 |
def PanTool(self): |
128 |
|
|
"""Start the canvas pan tool""" |
129 |
|
|
self.SelectTool(CanvasPanTool(self)) |
130 |
|
|
|
131 |
|
|
def SetMap(self, map): |
132 |
|
|
redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED, |
133 |
|
|
LAYER_VISIBILITY_CHANGED) |
134 |
|
|
if self.Map() is not None: |
135 |
|
|
for channel in redraw_channels: |
136 |
|
|
self.Map().Unsubscribe(channel, self.full_redraw) |
137 |
bh |
535 |
|
138 |
jonathan |
1385 |
ViewPort.SetMap(self, map) |
139 |
bh |
535 |
|
140 |
jonathan |
1385 |
if self.Map() is not None: |
141 |
|
|
for channel in redraw_channels: |
142 |
|
|
self.Map().Subscribe(channel, self.full_redraw) |
143 |
bh |
535 |
|
144 |
jonathan |
1385 |
# force a redraw. If map is not empty, it's already been called |
145 |
|
|
# by FitMapToWindow but if map is empty it hasn't been called |
146 |
|
|
# yet so we have to explicitly call it. |
147 |
|
|
self.full_redraw() |
148 |
bh |
535 |
|
149 |
bh |
6 |
def OnPaint(self, event): |
150 |
|
|
dc = wxPaintDC(self) |
151 |
jonathan |
1285 |
|
152 |
jonathan |
1385 |
if self.Map() is not None and self.Map().HasLayers(): |
153 |
jonathan |
1344 |
if self.bitmap in (None, -1): |
154 |
|
|
# set the flag that we should redraw the |
155 |
|
|
# bitmap in idle time |
156 |
|
|
self.bitmap = -1 |
157 |
jonathan |
1385 |
else: |
158 |
|
|
# blit the bitmap to the screen |
159 |
|
|
dc.BeginDrawing() |
160 |
|
|
dc.DrawBitmap(self.bitmap, 0, 0) |
161 |
|
|
dc.EndDrawing() |
162 |
jonathan |
1344 |
else: |
163 |
|
|
# If we've got no map or if the map is empty, simply clear |
164 |
|
|
# the screen. |
165 |
jonathan |
799 |
|
166 |
jonathan |
1344 |
# XXX it's probably possible to get rid of this. The |
167 |
|
|
# background color of the window is already white and the |
168 |
|
|
# only thing we may have to do is to call self.Refresh() |
169 |
|
|
# with a true argument in the right places. |
170 |
|
|
dc.BeginDrawing() |
171 |
|
|
dc.SetBackground(self.backgroundColor) |
172 |
|
|
dc.Clear() |
173 |
|
|
dc.EndDrawing() |
174 |
bh |
246 |
|
175 |
jonathan |
1344 |
def OnIdle(self, event): |
176 |
|
|
# render the screen if necessary |
177 |
bh |
125 |
|
178 |
jonathan |
1344 |
if self.bitmap != -1: |
179 |
|
|
return |
180 |
bh |
6 |
|
181 |
jonathan |
1344 |
wxBeginBusyCursor() |
182 |
|
|
try: |
183 |
|
|
width, height = self.GetSizeTuple() |
184 |
|
|
|
185 |
bh |
125 |
bitmap = wx.wxEmptyBitmap(width, height) |
186 |
|
|
dc = wx.wxMemoryDC() |
187 |
|
|
dc.SelectObject(bitmap) |
188 |
|
|
dc.BeginDrawing() |
189 |
bh |
57 |
|
190 |
jonathan |
1344 |
dc.SetBackground(self.backgroundColor) |
191 |
jonathan |
799 |
dc.Clear() |
192 |
bh |
6 |
|
193 |
bh |
535 |
selected_layer = self.selection.SelectedLayer() |
194 |
|
|
selected_shapes = self.selection.SelectedShapes() |
195 |
bh |
57 |
|
196 |
bh |
125 |
# draw the map into the bitmap |
197 |
|
|
renderer = ScreenRenderer(dc, self.scale, self.offset) |
198 |
bh |
149 |
|
199 |
bh |
296 |
# Pass the entire bitmap as update region to the renderer. |
200 |
bh |
149 |
# We're redrawing the whole bitmap, after all. |
201 |
jonathan |
1385 |
renderer.RenderMap(self.Map(), (0, 0, width, height), |
202 |
bh |
535 |
selected_layer, selected_shapes) |
203 |
bh |
125 |
|
204 |
|
|
dc.EndDrawing() |
205 |
|
|
dc.SelectObject(wx.wxNullBitmap) |
206 |
jonathan |
1344 |
|
207 |
bh |
125 |
self.bitmap = bitmap |
208 |
jonathan |
1344 |
finally: |
209 |
|
|
wxEndBusyCursor() |
210 |
|
|
pass |
211 |
bh |
125 |
|
212 |
jonathan |
1344 |
# This causes a paint event that then draws the bitmap |
213 |
|
|
self.redraw() |
214 |
bh |
6 |
|
215 |
frank |
910 |
def Export(self): |
216 |
jonathan |
967 |
|
217 |
frank |
910 |
if hasattr(self, "export_path"): |
218 |
|
|
export_path = self.export_path |
219 |
|
|
else: |
220 |
|
|
export_path="." |
221 |
|
|
dlg = wxFileDialog(self, _("Export Map"), export_path, "", |
222 |
|
|
"Enhanced Metafile (*.wmf)|*.wmf", |
223 |
|
|
wxSAVE|wxOVERWRITE_PROMPT) |
224 |
|
|
if dlg.ShowModal() == wxID_OK: |
225 |
|
|
self.export_path = os.path.dirname(dlg.GetPath()) |
226 |
|
|
dc = wxMetaFileDC(dlg.GetPath()) |
227 |
|
|
|
228 |
bh |
1454 |
scale, offset, mapregion = output_transform(self.scale, |
229 |
|
|
self.offset, |
230 |
|
|
self.GetSizeTuple(), |
231 |
|
|
dc.GetSizeTuple()) |
232 |
frank |
910 |
|
233 |
|
|
selected_layer = self.selection.SelectedLayer() |
234 |
|
|
selected_shapes = self.selection.SelectedShapes() |
235 |
|
|
|
236 |
|
|
renderer = ExportRenderer(dc, scale, offset) |
237 |
|
|
|
238 |
|
|
# Pass the entire bitmap as update region to the renderer. |
239 |
|
|
# We're redrawing the whole bitmap, after all. |
240 |
|
|
width, height = self.GetSizeTuple() |
241 |
jonathan |
1385 |
renderer.RenderMap(self.Map(), |
242 |
frank |
910 |
(0,0, |
243 |
|
|
(width/self.scale)*scale, |
244 |
|
|
(height/self.scale)*scale), |
245 |
|
|
mapregion, |
246 |
|
|
selected_layer, selected_shapes) |
247 |
|
|
dc.EndDrawing() |
248 |
|
|
dc.Close() |
249 |
|
|
dlg.Destroy() |
250 |
|
|
|
251 |
bh |
6 |
def Print(self): |
252 |
|
|
printer = wx.wxPrinter() |
253 |
frank |
910 |
width, height = self.GetSizeTuple() |
254 |
|
|
selected_layer = self.selection.SelectedLayer() |
255 |
|
|
selected_shapes = self.selection.SelectedShapes() |
256 |
|
|
|
257 |
jonathan |
1385 |
printout = MapPrintout(self, self.Map(), (0, 0, width, height), |
258 |
frank |
910 |
selected_layer, selected_shapes) |
259 |
jan |
1035 |
printer.Print(self, printout, True) |
260 |
bh |
6 |
printout.Destroy() |
261 |
bh |
246 |
|
262 |
bh |
6 |
def redraw(self, *args): |
263 |
jonathan |
1344 |
self.Refresh(False) |
264 |
bh |
6 |
|
265 |
bh |
125 |
def full_redraw(self, *args): |
266 |
|
|
self.bitmap = None |
267 |
|
|
self.redraw() |
268 |
|
|
|
269 |
jonathan |
1385 |
def map_projection_changed(self, map, old_proj): |
270 |
|
|
ViewPort.map_projection_changed(self, map, old_proj) |
271 |
bh |
125 |
self.full_redraw() |
272 |
bh |
6 |
|
273 |
jonathan |
1221 |
def layer_projection_changed(self, *args): |
274 |
jonathan |
1385 |
ViewPort.layer_projection_changed(self, args) |
275 |
jonathan |
1221 |
self.full_redraw() |
276 |
|
|
|
277 |
bh |
6 |
def set_view_transform(self, scale, offset): |
278 |
jonathan |
1385 |
ViewPort.set_view_transform(self, scale, offset) |
279 |
bh |
125 |
self.full_redraw() |
280 |
bh |
6 |
|
281 |
jonathan |
1385 |
def GetPortSizeTuple(self): |
282 |
|
|
return self.GetSizeTuple() |
283 |
jonathan |
967 |
|
284 |
bh |
6 |
def OnLeftDown(self, event): |
285 |
jonathan |
1385 |
self.MouseLeftDown(event) |
286 |
bh |
6 |
if self.tool is not None: |
287 |
|
|
self.drag_dc = wxClientDC(self) |
288 |
|
|
self.drag_dc.SetLogicalFunction(wxINVERT) |
289 |
|
|
self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH) |
290 |
|
|
self.tool.Show(self.drag_dc) |
291 |
bh |
1460 |
self.CaptureMouse() |
292 |
bh |
6 |
self.dragging = 1 |
293 |
bh |
246 |
|
294 |
bh |
6 |
def OnLeftUp(self, event): |
295 |
jonathan |
1385 |
self.MouseLeftUp(event) |
296 |
bh |
6 |
if self.dragging: |
297 |
bh |
261 |
self.ReleaseMouse() |
298 |
bh |
404 |
try: |
299 |
|
|
self.tool.Hide(self.drag_dc) |
300 |
|
|
finally: |
301 |
|
|
self.drag_dc = None |
302 |
|
|
self.dragging = 0 |
303 |
bh |
6 |
|
304 |
|
|
def OnMotion(self, event): |
305 |
|
|
if self.dragging: |
306 |
|
|
self.tool.Hide(self.drag_dc) |
307 |
jonathan |
1385 |
|
308 |
|
|
self.MouseMove(event) |
309 |
|
|
|
310 |
|
|
if self.dragging: |
311 |
bh |
6 |
self.tool.Show(self.drag_dc) |
312 |
|
|
|
313 |
bh |
122 |
def OnLeaveWindow(self, event): |
314 |
|
|
self.set_current_position(None) |
315 |
|
|
|
316 |
bh |
125 |
def OnSize(self, event): |
317 |
|
|
# the window's size has changed. We have to get a new bitmap. If |
318 |
|
|
# we want to be clever we could try to get by without throwing |
319 |
|
|
# everything away. E.g. when the window gets smaller, we could |
320 |
|
|
# either keep the bitmap or create the new one from the old one. |
321 |
|
|
# Even when the window becomes larger some parts of the bitmap |
322 |
|
|
# could be reused. |
323 |
|
|
self.full_redraw() |
324 |
|
|
|
325 |
bh |
6 |
def shape_selected(self, layer, shape): |
326 |
bh |
535 |
"""Receiver for the SHAPES_SELECTED messages. Redraw the map.""" |
327 |
|
|
# The selection object takes care that it only issues |
328 |
|
|
# SHAPES_SELECTED messages when the set of selected shapes has |
329 |
|
|
# actually changed, so we can do a full redraw unconditionally. |
330 |
|
|
# FIXME: We should perhaps try to limit the redraw to the are |
331 |
|
|
# actually covered by the shapes before and after the selection |
332 |
|
|
# change. |
333 |
jonathan |
1385 |
ViewPort.shape_selected(self, layer, shape) |
334 |
bh |
535 |
self.full_redraw() |
335 |
bh |
6 |
|
336 |
jonathan |
1385 |
def GetTextExtent(self, text): |
337 |
|
|
dc = wxClientDC(self) |
338 |
|
|
font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL) |
339 |
|
|
dc.SetFont(font) |
340 |
|
|
return dc.GetTextExtent(text) |
341 |
bh |
159 |
|
342 |
jonathan |
1385 |
def LabelShapeAt(self, x, y, text=None): |
343 |
bh |
295 |
"""Add or remove a label at window position x, y. |
344 |
bh |
1454 |
|
345 |
bh |
295 |
If there's a label at the given position, remove it. Otherwise |
346 |
|
|
determine the shape at the position, run the label dialog and |
347 |
jonathan |
1385 |
unless the user cancels the dialog, add a label. |
348 |
bh |
295 |
""" |
349 |
bh |
6 |
label_layer = self.map.LabelLayer() |
350 |
|
|
layer, shape_index = self.find_shape_at(x, y, select_labels = 1) |
351 |
|
|
if layer is None and shape_index is not None: |
352 |
jonathan |
1385 |
ViewPort.LabelShapeAt(self, x, y) |
353 |
bh |
6 |
elif layer is not None: |
354 |
bh |
1219 |
text = labeldialog.run_label_dialog(self, |
355 |
|
|
layer.ShapeStore().Table(), |
356 |
|
|
shape_index) |
357 |
jonathan |
1385 |
ViewPort.LabelShapeAt(self, x, y, text) |