/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/view.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/view.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (hide annotations)
Fri Sep 7 11:55:51 2001 UTC (23 years, 6 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/view.py
File MIME type: text/x-python
File size: 19431 byte(s)
	* Thuban/UI/view.py (MapCanvas.find_shape_at):Add a new parameter
	that can limit the search to the currently selected layer.
	(MapCanvas.SelectShapeAt): Make sure that the currently selected
	layer stays selected even when no shape is found

1 bh 6 # Copyright (c) 2001 by Intevation GmbH
2     # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Classes for display of a map and interaction with it
10     """
11    
12     __version__ = "$Revision$"
13    
14     from math import hypot
15    
16     from wxPython.wx import wxWindow,\
17     wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
18     EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION
19    
20    
21     from wxPython import wx
22    
23     from wxproj import point_in_polygon_shape, shape_centroid
24    
25    
26     from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \
27     LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED
28     from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
29     SHAPETYPE_POINT
30     from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
31     ALIGN_LEFT, ALIGN_RIGHT
32    
33    
34     from renderer import ScreenRenderer, PrinterRender
35    
36     import labeldialog
37    
38     from messages import SELECTED_SHAPE
39    
40    
41     #
42     # The tools
43     #
44    
45     class Tool:
46    
47     """
48     Base class for the interactive tools
49     """
50    
51     def __init__(self, view):
52     """Intitialize the tool. The view is the canvas displaying the map"""
53     self.view = view
54     self.start = self.current = None
55     self.dragging = 0
56     self.drawn = 0
57    
58     def Name(self):
59     """Return the tool's name"""
60     return ''
61    
62     def drag_start(self, x, y):
63     self.start = self.current = x, y
64     self.dragging = 1
65    
66     def drag_move(self, x, y):
67     self.current = x, y
68    
69     def drag_stop(self, x, y):
70     self.current = x, y
71     self.dragging = 0
72    
73     def Show(self, dc):
74     if not self.drawn:
75     self.draw(dc)
76     self.drawn = 1
77    
78     def Hide(self, dc):
79     if self.drawn:
80     self.draw(dc)
81     self.drawn = 0
82    
83     def draw(self, dc):
84     pass
85    
86     def MouseDown(self, event):
87     self.drag_start(event.m_x, event.m_y)
88    
89     def MouseMove(self, event):
90     if self.dragging:
91     self.drag_move(event.m_x, event.m_y)
92    
93     def MouseUp(self, event):
94     if self.dragging:
95     self.drag_move(event.m_x, event.m_y)
96    
97     def Cancel(self):
98     self.dragging = 0
99    
100    
101     class RectTool(Tool):
102    
103     """Base class for tools that draw rectangles while dragging"""
104    
105     def draw(self, dc):
106     sx, sy = self.start
107     cx, cy = self.current
108     dc.DrawRectangle(sx, sy, cx - sx, cy - sy)
109    
110     class ZoomInTool(RectTool):
111    
112     """The Zoom-In Tool"""
113    
114     def Name(self):
115     return "ZoomInTool"
116    
117     def proj_rect(self):
118     """return the rectangle given by start and current in projected
119     coordinates"""
120     sx, sy = self.start
121     cx, cy = self.current
122     left, top = self.view.win_to_proj(sx, sy)
123     right, bottom = self.view.win_to_proj(cx, cy)
124     return (min(left, right), min(top, bottom),
125     max(left, right), max(top, bottom))
126    
127     def MouseUp(self, event):
128     if self.dragging:
129     Tool.MouseUp(self, event)
130     self.view.FitRectToWindow(self.proj_rect())
131    
132    
133     class ZoomOutTool(RectTool):
134    
135     """The Zoom-Out Tool"""
136    
137     def Name(self):
138     return "ZoomOutTool"
139    
140     def MouseUp(self, event):
141     if self.dragging:
142     Tool.MouseUp(self, event)
143     sx, sy = self.start
144     cx, cy = self.current
145     self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),
146     max(sx, cx), max(sy, cy)))
147    
148    
149     class PanTool(Tool):
150    
151     """The Pan Tool"""
152    
153     def Name(self):
154     return "PanTool"
155    
156     def MouseMove(self, event):
157     if self.dragging:
158     x0, y0 = self.current
159     Tool.MouseMove(self, event)
160     x, y = self.current
161     width, height = self.view.GetSizeTuple()
162     dc = self.view.drag_dc
163     dc.Blit(0, 0, width, height, dc, x0 - x, y0 - y)
164    
165     def MouseUp(self, event):
166     if self.dragging:
167     Tool.MouseUp(self, event)
168     sx, sy = self.start
169     cx, cy = self.current
170     self.view.Translate(cx - sx, cy - sy)
171    
172     class IdentifyTool(Tool):
173    
174     """The "Identify" Tool"""
175    
176     def Name(self):
177     return "IdentifyTool"
178    
179     def MouseUp(self, event):
180     self.view.SelectShapeAt(event.m_x, event.m_y)
181    
182    
183     class LabelTool(Tool):
184    
185     """The "Label" Tool"""
186    
187     def Name(self):
188     return "LabelTool"
189    
190     def MouseUp(self, event):
191     self.view.LabelShapeAt(event.m_x, event.m_y)
192    
193    
194    
195    
196     class MapPrintout(wx.wxPrintout):
197    
198     """
199     wxPrintout class for printing Thuban maps
200     """
201    
202     def __init__(self, map):
203     wx.wxPrintout.__init__(self)
204     self.map = map
205    
206     def GetPageInfo(self):
207     return (1, 1, 1, 1)
208    
209     def HasPage(self, pagenum):
210     return pagenum == 1
211    
212     def OnPrintPage(self, pagenum):
213     if pagenum == 1:
214     self.draw_on_dc(self.GetDC())
215    
216     def draw_on_dc(self, dc):
217     width, height = self.GetPageSizePixels()
218     llx, lly, urx, ury = self.map.ProjectedBoundingBox()
219     scalex = width / (urx - llx)
220     scaley = height / (ury - lly)
221     scale = min(scalex, scaley)
222     offx = 0.5 * (width - (urx + llx) * scale)
223     offy = 0.5 * (height + (ury + lly) * scale)
224    
225     resx, resy = self.GetPPIPrinter()
226     renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)
227     renderer.RenderMap(self.map)
228     return wx.true
229    
230    
231     class MapCanvas(wxWindow):
232    
233     """A widget that displays a map and offers some interaction"""
234    
235 bh 23 def __init__(self, parent, winid, interactor):
236 bh 6 wxWindow.__init__(self, parent, winid)
237     self.SetBackgroundColour(wxColour(255, 255, 255))
238     self.map = None
239     self.scale = 1.0
240     self.offset = (0, 0)
241     self.dragging = 0
242     self.tool = None
243     self.redraw_on_idle = 0
244     EVT_PAINT(self, self.OnPaint)
245     EVT_LEFT_DOWN(self, self.OnLeftDown)
246     EVT_LEFT_UP(self, self.OnLeftUp)
247     EVT_MOTION(self, self.OnMotion)
248     wx.EVT_IDLE(self, self.OnIdle)
249 bh 23 self.interactor = interactor
250 bh 6 self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)
251    
252     def OnPaint(self, event):
253     dc = wxPaintDC(self)
254     if self.map is None or not self.map.HasLayers():
255     return
256     self.redraw_on_idle = 1
257    
258     def do_redraw(self):
259     width, height = self.GetSizeTuple()
260     bitmap = wx.wxEmptyBitmap(width, height)
261    
262     dc = wx.wxMemoryDC()
263     dc.SelectObject(bitmap)
264    
265     dc.BeginDrawing()
266    
267     dc.SetBrush(wx.wxWHITE_BRUSH)
268     dc.SetPen(wx.wxTRANSPARENT_PEN)
269     dc.DrawRectangle(0, 0, width, height)
270    
271     if 1: #self.interactor.selected_map is self.map:
272     selected_layer = self.interactor.selected_layer
273     selected_shape = self.interactor.selected_shape
274     else:
275     selected_layer = None
276     selected_shape = None
277    
278     renderer = ScreenRenderer(dc, self.scale, self.offset)
279     renderer.RenderMap(self.map, selected_layer, selected_shape)
280    
281     clientdc = wxClientDC(self)
282     clientdc.BeginDrawing()
283     clientdc.Blit(0, 0, width, height, dc, 0, 0)
284     clientdc.EndDrawing()
285    
286    
287     def Print(self):
288     printer = wx.wxPrinter()
289     printout = MapPrintout(self.map)
290     printer.Print(self, printout, wx.true)
291     printout.Destroy()
292    
293     def SetMap(self, map):
294     redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED,
295     LAYER_VISIBILITY_CHANGED)
296     if self.map is not None:
297     for channel in redraw_channels:
298     self.map.Unsubscribe(channel, self.redraw)
299     self.map.Unsubscribe(MAP_PROJECTION_CHANGED,
300     self.projection_changed)
301     self.map = map
302     if self.map is not None:
303     for channel in redraw_channels:
304     self.map.Subscribe(channel, self.redraw)
305     self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)
306     self.FitMapToWindow()
307    
308     def Map(self):
309     return self.map
310    
311     def redraw(self, *args):
312     self.Refresh(0)
313    
314     def projection_changed(self, *args):
315     self.FitMapToWindow()
316     self.redraw()
317    
318     def set_view_transform(self, scale, offset):
319     self.scale = scale
320     self.offset = offset
321     self.redraw()
322    
323     def proj_to_win(self, x, y):
324     """\
325     Return the point in window coords given by projected coordinates x y
326     """
327     offx, offy = self.offset
328     return (self.scale * x + offx, -self.scale * y + offy)
329    
330     def win_to_proj(self, x, y):
331     """\
332     Return the point in projected coordinates given by window coords x y
333     """
334     offx, offy = self.offset
335     return ((x - offx) / self.scale, (offy - y) / self.scale)
336    
337     def FitRectToWindow(self, rect):
338     width, height = self.GetSizeTuple()
339     llx, lly, urx, ury = rect
340     scalex = width / (urx - llx)
341     scaley = height / (ury - lly)
342     scale = min(scalex, scaley)
343     offx = 0.5 * (width - (urx + llx) * scale)
344     offy = 0.5 * (height + (ury + lly) * scale)
345     self.set_view_transform(scale, (offx, offy))
346    
347     def FitMapToWindow(self):
348     """\
349     Set the scale and offset so that the map is centered in the
350     window
351     """
352     bbox = self.map.ProjectedBoundingBox()
353     if bbox is not None:
354     self.FitRectToWindow(bbox)
355    
356     def ZoomFactor(self, factor):
357     width, height = self.GetSizeTuple()
358     scale = self.scale * factor
359     offx, offy = self.offset
360     offset = (factor * (offx - width / 2) + width / 2,
361     factor * (offy - height / 2) + height / 2)
362     self.set_view_transform(scale, offset)
363    
364     def ZoomOutToRect(self, rect):
365     # rect is given in window coordinates
366    
367     # determine the bbox of the displayed region in projected
368     # coordinates
369     width, height = self.GetSizeTuple()
370     llx, lly = self.win_to_proj(0, height - 1)
371     urx, ury = self.win_to_proj(width - 1, 0)
372    
373     sx, sy, ex, ey = rect
374     scalex = (ex - sx) / (urx - llx)
375     scaley = (ey - sy) / (ury - lly)
376     scale = min(scalex, scaley)
377    
378     offx = 0.5 * ((ex + sx) - (urx + llx) * scale)
379     offy = 0.5 * ((ey + sy) + (ury + lly) * scale)
380     self.set_view_transform(scale, (offx, offy))
381    
382     def Translate(self, dx, dy):
383     offx, offy = self.offset
384     self.set_view_transform(self.scale, (offx + dx, offy + dy))
385    
386     def ZoomInTool(self):
387     self.tool = ZoomInTool(self)
388    
389     def ZoomOutTool(self):
390     self.tool = ZoomOutTool(self)
391    
392     def PanTool(self):
393     self.tool = PanTool(self)
394    
395     def IdentifyTool(self):
396     self.tool = IdentifyTool(self)
397    
398     def LabelTool(self):
399     self.tool = LabelTool(self)
400    
401     def CurrentTool(self):
402     return self.tool and self.tool.Name() or None
403    
404     def OnLeftDown(self, event):
405     if self.tool is not None:
406     self.drag_dc = wxClientDC(self)
407     self.drag_dc.SetLogicalFunction(wxINVERT)
408     self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)
409     self.CaptureMouse()
410     self.tool.MouseDown(event)
411     self.tool.Show(self.drag_dc)
412     self.dragging = 1
413    
414     def OnLeftUp(self, event):
415     self.ReleaseMouse()
416     if self.dragging:
417     self.tool.Hide(self.drag_dc)
418     self.tool.MouseUp(event)
419     self.drag_dc = None
420     self.dragging = 0
421    
422     def OnMotion(self, event):
423     if self.dragging:
424     self.tool.Hide(self.drag_dc)
425     self.tool.MouseMove(event)
426     self.tool.Show(self.drag_dc)
427    
428     def OnIdle(self, event):
429     if self.redraw_on_idle:
430     self.do_redraw()
431     self.redraw_on_idle = 0
432    
433     def shape_selected(self, layer, shape):
434     self.redraw()
435    
436 bh 43 def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):
437     """Determine the shape at point px, py in window coords
438    
439     Return the shape and the corresponding layer as a tuple (layer,
440     shape).
441    
442     If the optional parameter select_labels is true (default false)
443     search through the labels. If a label is found return it's index
444     as the shape and None as the layer.
445    
446     If the optional parameter selected_layer is true (default), only
447     search in the currently selected layer.
448     """
449 bh 6 map_proj = self.map.projection
450     if map_proj is not None:
451     forward = map_proj.Forward
452     else:
453     forward = None
454    
455     scale = self.scale
456     offx, offy = self.offset
457    
458     if select_labels:
459     labels = self.map.LabelLayer().Labels()
460    
461     if labels:
462     dc = wxClientDC(self)
463     font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
464     dc.SetFont(font)
465     for i in range(len(labels)):
466     label = labels[i]
467     x = label.x
468     y = label.y
469     text = label.text
470     if forward:
471     x, y = forward(x, y)
472     x = x * scale + offx
473     y = -y * scale + offy
474     width, height = dc.GetTextExtent(text)
475     if label.halign == ALIGN_LEFT:
476     # nothing to be done
477     pass
478     elif label.halign == ALIGN_RIGHT:
479     x = x - width
480     elif label.halign == ALIGN_CENTER:
481     x = x - width/2
482     if label.valign == ALIGN_TOP:
483     # nothing to be done
484     pass
485     elif label.valign == ALIGN_BOTTOM:
486     y = y - height
487     elif label.valign == ALIGN_CENTER:
488     y = y - height/2
489     if x <= px < x + width and y <= py <= y + height:
490     return None, i
491 bh 43
492     if selected_layer:
493     layer = self.interactor.SelectedLayer()
494     if layer is not None:
495     layers = [layer]
496     else:
497     # no layer selected. Use an empty list to effectively
498     # ignore all layers.
499     layers = []
500     else:
501     layers = self.map.Layers()
502    
503 bh 6 for layer_index in range(len(layers) - 1, -1, -1):
504     layer = layers[layer_index]
505    
506     # search only in visible layers
507     if not layer.Visible():
508     continue
509    
510     filled = layer.fill is not None
511     stroked = layer.stroke is not None
512    
513     layer_proj = layer.projection
514     if layer_proj is not None:
515     inverse = layer_proj.Inverse
516     else:
517     inverse = None
518    
519     shapetype = layer.ShapeType()
520    
521     select_shape = -1
522     if shapetype == SHAPETYPE_POLYGON:
523     for i in range(layer.NumShapes()):
524     result = point_in_polygon_shape(layer.shapefile.cobject(),
525     i,
526     filled, stroked,
527     map_proj, layer_proj,
528     scale, -scale, offx, offy,
529     px, py)
530     if result:
531     select_shape = i
532     break
533     elif shapetype == SHAPETYPE_ARC:
534     for i in range(layer.NumShapes()):
535     result = point_in_polygon_shape(layer.shapefile.cobject(),
536     i, 0, 1,
537     map_proj, layer_proj,
538     scale, -scale, offx, offy,
539     px, py)
540     if result < 0:
541     select_shape = i
542     break
543     elif shapetype == SHAPETYPE_POINT:
544     for i in range(layer.NumShapes()):
545     shape = layer.Shape(i)
546     x, y = shape.Points()[0]
547     if inverse:
548     x, y = inverse(x, y)
549     if forward:
550     x, y = forward(x, y)
551     x = x * scale + offx
552     y = -y * scale + offy
553     if hypot(px - x, py - y) < 5:
554     select_shape = i
555     break
556    
557     if select_shape >= 0:
558     return layer, select_shape
559     return None, None
560    
561     def SelectShapeAt(self, x, y):
562     layer, shape = self.find_shape_at(x, y)
563 bh 43 # If layer is None, then shape will also be None. We don't want
564     # to deselect the currently selected layer, so we simply select
565     # the already selected layer again.
566     if layer is None:
567     layer = self.interactor.SelectedLayer()
568 bh 6 self.interactor.SelectLayerAndShape(layer, shape)
569    
570     def LabelShapeAt(self, x, y):
571     ox = x; oy = y
572     label_layer = self.map.LabelLayer()
573     layer, shape_index = self.find_shape_at(x, y, select_labels = 1)
574     if layer is None and shape_index is not None:
575     # a label was selected
576     label_layer.RemoveLabel(shape_index)
577     elif layer is not None:
578     text = labeldialog.run_label_dialog(self, layer.table, shape_index)
579     if text:
580     proj = self.map.projection
581     if proj is not None:
582     map_proj = proj
583     else:
584     map_proj = None
585     proj = layer.projection
586     if proj is not None:
587     layer_proj = proj
588     else:
589     layer_proj = None
590    
591     shapetype = layer.ShapeType()
592     if shapetype == SHAPETYPE_POLYGON:
593     x, y = shape_centroid(layer.shapefile.cobject(),
594     shape_index,
595     map_proj, layer_proj, 1, 1, 0, 0)
596     if map_proj is not None:
597     x, y = map_proj.Inverse(x, y)
598     else:
599     shape = layer.Shape(shape_index)
600     if shapetype == SHAPETYPE_POINT:
601     x, y = shape.Points()[0]
602     else:
603     # assume SHAPETYPE_ARC
604     points = shape.Points()
605     x, y = points[len(points) / 2]
606     if layer_proj is not None:
607     x, y = layer_proj.Inverse(x, y)
608     if shapetype == SHAPETYPE_POINT:
609     halign = ALIGN_LEFT
610     valign = ALIGN_CENTER
611     elif shapetype == SHAPETYPE_POLYGON:
612     halign = ALIGN_CENTER
613     valign = ALIGN_CENTER
614     elif shapetype == SHAPETYPE_ARC:
615     halign = ALIGN_LEFT
616     valign = ALIGN_CENTER
617     label_layer.AddLabel(x, y, text,
618     halign = halign, valign = valign)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26