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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 122 by bh, Mon Apr 29 18:04:54 2002 UTC revision 2454 by bh, Mon Dec 13 18:26:11 2004 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001, 2002 by Intevation GmbH  # Copyright (c) 2001, 2002, 2003, 2004 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.
# Line 9  Line 10 
10  Classes for display of a map and interaction with it  Classes for display of a map and interaction with it
11  """  """
12    
13    from __future__ import generators
14    
15  __version__ = "$Revision$"  __version__ = "$Revision$"
16    # $Source$
17    # $Id$
18    
19  from math import hypot  import os.path
20    import time
21    import traceback
22    
23  from wxPython.wx import wxWindow,\  from wxPython.wx import wxWindow, \
24       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
25       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, \
26         wxPlatform, wxBeginBusyCursor, wxEndBusyCursor, wxFileDialog, wxSAVE, \
27         wxOVERWRITE_PROMPT, wxID_OK
28    
29    # Export related stuff
30    if wxPlatform == '__WXMSW__':
31        from wxPython.wx import wxMetaFileDC
32    
33  from wxPython import wx  from wxPython import wx
34    
35  from wxproj import point_in_polygon_shape, shape_centroid  from Thuban import _
   
36    
37  from Thuban.Model.messages import MAP_PROJECTION_CHANGED, \  from Thuban.Model.messages import MAP_LAYERS_CHANGED, LAYER_CHANGED, \
38       LAYERS_CHANGED, LAYER_LEGEND_CHANGED, LAYER_VISIBILITY_CHANGED       LAYER_VISIBILITY_CHANGED
 from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \  
      SHAPETYPE_POINT  
 from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \  
      ALIGN_LEFT, ALIGN_RIGHT  
 from Thuban.Lib.connector import Publisher  
39    
40  from renderer import ScreenRenderer, PrinterRender  from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer
41    
42  import labeldialog  import labeldialog
43    
44  from messages import SELECTED_SHAPE, VIEW_POSITION  from viewport import ViewPort, PanTool, output_transform
   
   
 #  
 #   The tools  
 #  
   
 class Tool:  
   
     """  
     Base class for the interactive tools  
     """  
   
     def __init__(self, view):  
         """Intitialize the tool. The view is the canvas displaying the map"""  
         self.view = view  
         self.start = self.current = None  
         self.dragging = 0  
         self.drawn = 0  
   
     def Name(self):  
         """Return the tool's name"""  
         return ''  
   
     def drag_start(self, x, y):  
         self.start = self.current = x, y  
         self.dragging = 1  
   
     def drag_move(self, x, y):  
         self.current = x, y  
   
     def drag_stop(self, x, y):  
         self.current = x, y  
         self.dragging = 0  
   
     def Show(self, dc):  
         if not self.drawn:  
             self.draw(dc)  
         self.drawn = 1  
   
     def Hide(self, dc):  
         if self.drawn:  
             self.draw(dc)  
         self.drawn = 0  
45    
46      def draw(self, dc):  class CanvasPanTool(PanTool):
         pass  
47    
48      def MouseDown(self, event):      """The Canvas Pan Tool"""
         self.drag_start(event.m_x, event.m_y)  
49    
50      def MouseMove(self, event):      def MouseMove(self, event):
51          if self.dragging:          if self.dragging:
52              self.drag_move(event.m_x, event.m_y)              PanTool.MouseMove(self, event)
   
     def MouseUp(self, event):  
         if self.dragging:  
             self.drag_move(event.m_x, event.m_y)  
   
     def Cancel(self):  
         self.dragging = 0  
   
   
 class RectTool(Tool):  
   
     """Base class for tools that draw rectangles while dragging"""  
   
     def draw(self, dc):  
         sx, sy = self.start  
         cx, cy = self.current  
         dc.DrawRectangle(sx, sy, cx - sx, cy - sy)  
   
 class ZoomInTool(RectTool):  
   
     """The Zoom-In Tool"""  
   
     def Name(self):  
         return "ZoomInTool"  
   
     def proj_rect(self):  
         """return the rectangle given by start and current in projected  
         coordinates"""  
         sx, sy = self.start  
         cx, cy = self.current  
         left, top = self.view.win_to_proj(sx, sy)  
         right, bottom = self.view.win_to_proj(cx, cy)  
         return (min(left, right), min(top, bottom),  
                 max(left, right), max(top, bottom))  
   
     def MouseUp(self, event):  
         if self.dragging:  
             Tool.MouseUp(self, event)  
             sx, sy = self.start  
             cx, cy = self.current  
             if sx == cx and sy == cy:  
                 # Just a mouse click. Simply zoom in by a factor of two  
                 self.view.ZoomFactor(2, center = (cx, cy))  
             else:  
                 # A drag. Zoom in to the rectangle  
                 self.view.FitRectToWindow(self.proj_rect())  
   
   
 class ZoomOutTool(RectTool):  
   
     """The Zoom-Out Tool"""  
       
     def Name(self):  
         return "ZoomOutTool"  
   
     def MouseUp(self, event):  
         if self.dragging:  
             Tool.MouseUp(self, event)  
53              sx, sy = self.start              sx, sy = self.start
             cx, cy = self.current  
             if sx == cx and sy == cy:  
                 # Just a mouse click. Simply zoom out by a factor of two  
                 self.view.ZoomFactor(0.5, center = (cy, cy))  
             else:  
                 # A drag. Zoom out to the rectangle  
                 self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),  
                                          max(sx, cx), max(sy, cy)))  
   
   
 class PanTool(Tool):  
   
     """The Pan Tool"""  
   
     def Name(self):  
         return "PanTool"  
   
     def MouseMove(self, event):  
         if self.dragging:  
             x0, y0 = self.current  
             Tool.MouseMove(self, event)  
54              x, y = self.current              x, y = self.current
55              width, height = self.view.GetSizeTuple()              width, height = self.view.GetSizeTuple()
             dc = self.view.drag_dc  
             dc.Blit(0, 0, width, height, dc, x0 - x, y0 - y)  
   
     def MouseUp(self, event):  
         if self.dragging:  
             Tool.MouseUp(self, event)  
             sx, sy = self.start  
             cx, cy = self.current  
             self.view.Translate(cx - sx, cy - sy)  
           
 class IdentifyTool(Tool):  
   
     """The "Identify" Tool"""  
       
     def Name(self):  
         return "IdentifyTool"  
   
     def MouseUp(self, event):  
         self.view.SelectShapeAt(event.m_x, event.m_y)  
   
   
 class LabelTool(Tool):  
   
     """The "Label" Tool"""  
   
     def Name(self):  
         return "LabelTool"  
   
     def MouseUp(self, event):  
         self.view.LabelShapeAt(event.m_x, event.m_y)  
   
56    
57                bitmapdc = wx.wxMemoryDC()
58                bitmapdc.SelectObject(self.view.PreviewBitmap())
59    
60                dc = self.view.drag_dc
61                dc.Blit(0, 0, width, height, bitmapdc, sx - x, sy - y)
62    
63  class MapPrintout(wx.wxPrintout):  class MapPrintout(wx.wxPrintout):
64    
# Line 211  class MapPrintout(wx.wxPrintout): Line 66  class MapPrintout(wx.wxPrintout):
66      wxPrintout class for printing Thuban maps      wxPrintout class for printing Thuban maps
67      """      """
68    
69      def __init__(self, map):      def __init__(self, canvas, map, region, selected_layer, selected_shapes):
70          wx.wxPrintout.__init__(self)          wx.wxPrintout.__init__(self)
71            self.canvas = canvas
72          self.map = map          self.map = map
73            self.region = region
74            self.selected_layer = selected_layer
75            self.selected_shapes = selected_shapes
76    
77      def GetPageInfo(self):      def GetPageInfo(self):
78          return (1, 1, 1, 1)          return (1, 1, 1, 1)
# Line 227  class MapPrintout(wx.wxPrintout): Line 86  class MapPrintout(wx.wxPrintout):
86    
87      def draw_on_dc(self, dc):      def draw_on_dc(self, dc):
88          width, height = self.GetPageSizePixels()          width, height = self.GetPageSizePixels()
89          llx, lly, urx, ury = self.map.ProjectedBoundingBox()          scale, offset, mapregion = output_transform(self.canvas.scale,
90          scalex = width / (urx - llx)                                                      self.canvas.offset,
91          scaley = height / (ury - lly)                                                      self.canvas.GetSizeTuple(),
92          scale = min(scalex, scaley)                                                      self.GetPageSizePixels())
         offx = 0.5 * (width - (urx + llx) * scale)  
         offy = 0.5 * (height + (ury + lly) * scale)  
   
93          resx, resy = self.GetPPIPrinter()          resx, resy = self.GetPPIPrinter()
94          renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)          canvas_scale = self.canvas.scale
95          renderer.RenderMap(self.map)          x, y, width, height = self.region
96          return wx.true          renderer = PrinterRenderer(dc, self.map, scale, offset,
97                                               region = (mapregion[0], mapregion[1],
98                                                 (width/canvas_scale)*scale,
99                                                 (height/canvas_scale)*scale),
100                                       resolution = resy,
101                                       destination_region = mapregion)
102            renderer.RenderMap(self.selected_layer, self.selected_shapes)
103            return True
104    
105    
106  class MapCanvas(wxWindow, Publisher):  class MapCanvas(wxWindow, ViewPort):
107    
108      """A widget that displays a map and offers some interaction"""      """A widget that displays a map and offers some interaction"""
109    
110      def __init__(self, parent, winid, interactor):      def __init__(self, parent, winid):
111          wxWindow.__init__(self, parent, winid)          wxWindow.__init__(self, parent, winid)
112            ViewPort.__init__(self)
113    
114          self.SetBackgroundColour(wxColour(255, 255, 255))          self.SetBackgroundColour(wxColour(255, 255, 255))
115          self.map = None  
116          self.scale = 1.0          # the bitmap serving as backing store
117          self.offset = (0, 0)          self.bitmap = None
118          self.dragging = 0          # the monochrome bitmap with the selection if any
119          self.tool = None          self.selection_bitmap = None
120          self.redraw_on_idle = 0  
121          self.current_position = None          self.backgroundColor = wx.wxWHITE_BRUSH
122    
123            # The rendering iterator object. Used when rendering
124            # incrementally
125            self.render_iter = None
126    
127            # subscribe the WX events we're interested in
128          EVT_PAINT(self, self.OnPaint)          EVT_PAINT(self, self.OnPaint)
129          EVT_LEFT_DOWN(self, self.OnLeftDown)          EVT_LEFT_DOWN(self, self.OnLeftDown)
130          EVT_LEFT_UP(self, self.OnLeftUp)          EVT_LEFT_UP(self, self.OnLeftUp)
131          EVT_MOTION(self, self.OnMotion)          EVT_MOTION(self, self.OnMotion)
132          EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)          EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)
133            wx.EVT_SIZE(self, self.OnSize)
134          wx.EVT_IDLE(self, self.OnIdle)          wx.EVT_IDLE(self, self.OnIdle)
         self.interactor = interactor  
         self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)  
135    
136      def __del__(self):      def __del__(self):
137          wxWindow.__del__(self)          wxWindow.__del__(self)
138          Publisher.__del__(self)          ViewPort.__del__(self)
139    
140        def PreviewBitmap(self):
141            return self.bitmap
142    
143        def PanTool(self):
144            """Start the canvas pan tool"""
145            self.SelectTool(CanvasPanTool(self))
146            
147        def SetMap(self, map):
148            redraw_channels = (MAP_LAYERS_CHANGED, LAYER_CHANGED,
149                               LAYER_VISIBILITY_CHANGED)
150            if self.Map() is not None:
151                for channel in redraw_channels:
152                    self.Map().Unsubscribe(channel, self.full_redraw)
153    
154            ViewPort.SetMap(self, map)
155    
156            if self.Map() is not None:
157                for channel in redraw_channels:
158                    self.Map().Subscribe(channel, self.full_redraw)
159    
160            # force a redraw. If map is not empty, it's already been called
161            # by FitMapToWindow but if map is empty it hasn't been called
162            # yet so we have to explicitly call it.
163            self.full_redraw()
164    
165      def OnPaint(self, event):      def OnPaint(self, event):
166          dc = wxPaintDC(self)          dc = wxPaintDC(self)
167          if self.map is not None and self.map.HasLayers():          if self.Map() is not None and self.Map().HasLayers():
168              # We have a non-empty map. Redraw it in idle time              if self.bitmap is not None:
169              self.redraw_on_idle = 1                  dc.BeginDrawing()
170                    dc.DrawBitmap(self.bitmap, 0, 0)
171                    if self.selection_bitmap is not None:
172                        dc.DrawBitmap(self.selection_bitmap, 0, 0, True)
173                    dc.EndDrawing()
174          else:          else:
175              # If we've got no map or if the map is empty, simply clear              # If we've got no map or if the map is empty, simply clear
176              # the screen.              # the screen.
177                
178              # XXX it's probably possible to get rid of this. The              # XXX it's probably possible to get rid of this. The
179              # background color of the window is already white and the              # background color of the window is already white and the
180              # only thing we may have to do is to call self.Refresh()              # only thing we may have to do is to call self.Refresh()
181              # with a true argument in the right places.              # with a true argument in the right places.
182              dc.BeginDrawing()              dc.BeginDrawing()
183              dc.Clear()                          dc.SetBackground(self.backgroundColor)
184                dc.Clear()
185              dc.EndDrawing()              dc.EndDrawing()
186    
187      def do_redraw(self):      def OnIdle(self, event):
188          # This should only be called if we have a non-empty map. We draw          """Idle handler. Redraw the bitmap if necessary"""
189          # it into a memory DC and then blit it to the screen.          if (self.Map() is not None
190          width, height = self.GetSizeTuple()              and (self.bitmap is None
191          bitmap = wx.wxEmptyBitmap(width, height)                   or self.render_iter is not None
192          dc = wx.wxMemoryDC()                   or (self.HasSelectedShapes()
193          dc.SelectObject(bitmap)                       and self.selection_bitmap is None))):
194          dc.BeginDrawing()              event.RequestMore(self._do_redraw())
195    
196          # clear the background      def _do_redraw(self):
197          dc.SetBrush(wx.wxWHITE_BRUSH)          """Redraw a bit and return whether this method has to be called again.
198          dc.SetPen(wx.wxTRANSPARENT_PEN)  
199          dc.DrawRectangle(0, 0, width, height)          Called by OnIdle to handle the actual redraw. Redraw is
200            incremental for both the bitmap with the normal layers and the
201          if 1: #self.interactor.selected_map is self.map:          bitmap with the selection.
202              selected_layer = self.interactor.selected_layer          """
203              selected_shape = self.interactor.selected_shape          finished = False
204            if self.render_iter is not None:
205                try:
206                    if self.render_iter.next():
207                        # Redraw if the last preview redraw was some time
208                        # ago and the user is not currently dragging the
209                        # mouse because redrawing would interfere with what
210                        # the current tool is drawing on the window.
211                        if not self.dragging \
212                               and time.time() - self.render_last_preview > 0.5:
213                            client_dc = wxClientDC(self)
214                            client_dc.BeginDrawing()
215                            client_dc.DrawBitmap(self.bitmap, 0, 0)
216                            client_dc.EndDrawing()
217                            self.render_last_preview = time.time()
218                    else:
219                        self.render_iter = None
220                        # Redraw if not dragging because redrawing would
221                        # interfere with what the current tool is drawing on
222                        # the window.
223                        if not self.dragging:
224                            self.redraw()
225                        finished = True
226                except StopIteration:
227                    finished = True
228                    self.render_iter = None
229                except:
230                    finished = True
231                    self.render_iter = None
232                    traceback.print_exc()
233          else:          else:
234              selected_layer = None              self.render_iter = self._render_iterator()
235              selected_shape = None              self.render_last_preview = time.time()
236            return not finished
         # draw the map into the bitmap  
         renderer = ScreenRenderer(dc, self.scale, self.offset)  
         renderer.RenderMap(self.map, selected_layer, selected_shape)  
   
         dc.EndDrawing()  
   
         # blit the bitmap to the screen  
         clientdc = wxClientDC(self)  
         clientdc.BeginDrawing()  
         clientdc.Blit(0, 0, width, height, dc, 0, 0)  
         clientdc.EndDrawing()  
   
     def Print(self):  
         printer = wx.wxPrinter()  
         printout = MapPrintout(self.map)  
         printer.Print(self, printout, wx.true)  
         printout.Destroy()  
           
     def SetMap(self, map):  
         redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED,  
                            LAYER_VISIBILITY_CHANGED)  
         if self.map is not None:  
             for channel in redraw_channels:  
                 self.map.Unsubscribe(channel, self.redraw)  
             self.map.Unsubscribe(MAP_PROJECTION_CHANGED,  
                                  self.projection_changed)  
         self.map = map  
         if self.map is not None:  
             for channel in redraw_channels:  
                 self.map.Subscribe(channel, self.redraw)  
             self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)  
         self.FitMapToWindow()  
         # force a redraw. If map is not empty, it's already been called  
         # by FitMapToWindow but if map is empty it hasn't been called  
         # yet so we have to explicitly call it.  
         self.redraw()  
237    
238      def Map(self):      def _render_iterator(self):
239          return self.map          width, height = self.GetSizeTuple()
240            dc = wx.wxMemoryDC()
241    
242      def redraw(self, *args):          render_start = time.time()
         self.Refresh(0)  
243    
244      def projection_changed(self, *args):          if self.bitmap is None:
245          self.FitMapToWindow()              self.bitmap = wx.wxEmptyBitmap(width, height)
246          self.redraw()              dc.SelectObject(self.bitmap)
247                dc.BeginDrawing()
248    
249      def set_view_transform(self, scale, offset):              dc.SetBackground(self.backgroundColor)
250          self.scale = scale              dc.Clear()
         self.offset = offset  
         self.redraw()  
251    
252      def proj_to_win(self, x, y):              # draw the map into the bitmap
253          """\              renderer = ScreenRenderer(dc, self.Map(), self.scale, self.offset,
254          Return the point in  window coords given by projected coordinates x y                                        (0, 0, width, height))
255          """              for cont in renderer.RenderMapIncrementally():
256          offx, offy = self.offset                  yield True
         return (self.scale * x + offx, -self.scale * y + offy)  
257    
258      def win_to_proj(self, x, y):              dc.EndDrawing()
259          """\              dc.SelectObject(wx.wxNullBitmap)
         Return the point in projected coordinates given by window coords x y  
         """  
         offx, offy = self.offset  
         return ((x - offx) / self.scale, (offy - y) / self.scale)  
260    
261      def FitRectToWindow(self, rect):          if self.HasSelectedShapes() and self.selection_bitmap is None:
262          width, height = self.GetSizeTuple()              bitmap = wx.wxEmptyBitmap(width, height)
263          llx, lly, urx, ury = rect              dc.SelectObject(bitmap)
264          if llx == urx or lly == ury:              dc.BeginDrawing()
265              # zero with or zero height. Do Nothing              dc.SetBackground(wx.wxWHITE_BRUSH)
266              return              dc.Clear()
         scalex = width / (urx - llx)  
         scaley = height / (ury - lly)  
         scale = min(scalex, scaley)  
         offx = 0.5 * (width - (urx + llx) * scale)  
         offy = 0.5 * (height + (ury + lly) * scale)  
         self.set_view_transform(scale, (offx, offy))  
   
     def FitMapToWindow(self):  
         """\  
         Set the scale and offset so that the map is centered in the  
         window  
         """  
         bbox = self.map.ProjectedBoundingBox()  
         if bbox is not None:  
             self.FitRectToWindow(bbox)  
   
     def ZoomFactor(self, factor, center = None):  
         """Multiply the zoom by factor and center on center.  
   
         The optional parameter center is a point in window coordinates  
         that should be centered. If it is omitted, it defaults to the  
         center of the window  
         """  
         width, height = self.GetSizeTuple()  
         scale = self.scale * factor  
         offx, offy = self.offset  
         if center is not None:  
             cx, cy = center  
         else:  
             cx = width / 2  
             cy = height / 2  
         offset = (factor * (offx - cx) + width / 2,  
                   factor * (offy - cy) + height / 2)  
         self.set_view_transform(scale, offset)  
267    
268      def ZoomOutToRect(self, rect):              renderer = ScreenRenderer(dc, self.Map(), self.scale, self.offset,
269          # rect is given in window coordinates                                        (0, 0, width, height))
270                layer = self.SelectedLayer()
271                shapes = self.selection.SelectedShapes()
272                for cont in renderer.draw_selection_incrementally(layer, shapes):
273                    yield True
274    
275          # determine the bbox of the displayed region in projected              dc.EndDrawing()
276          # coordinates              dc.SelectObject(wx.wxNullBitmap)
         width, height = self.GetSizeTuple()  
         llx, lly = self.win_to_proj(0, height - 1)  
         urx, ury = self.win_to_proj(width - 1, 0)  
277    
278          sx, sy, ex, ey = rect              bitmap.SetMask(wx.wxMaskColour(bitmap, wx.wxWHITE))
279          scalex = (ex - sx) / (urx - llx)              self.selection_bitmap = bitmap
         scaley = (ey - sy) / (ury - lly)  
         scale = min(scalex, scaley)  
   
         offx = 0.5 * ((ex + sx) - (urx + llx) * scale)  
         offy = 0.5 * ((ey + sy) + (ury + lly) * scale)  
         self.set_view_transform(scale, (offx, offy))  
   
     def Translate(self, dx, dy):  
         offx, offy = self.offset  
         self.set_view_transform(self.scale, (offx + dx, offy + dy))  
280    
281      def ZoomInTool(self):          yield False
         self.tool = ZoomInTool(self)  
282    
283      def ZoomOutTool(self):      def Export(self):
         self.tool = ZoomOutTool(self)  
284    
285      def PanTool(self):          if hasattr(self, "export_path"):
286          self.tool = PanTool(self)              export_path = self.export_path
287            else:
288                export_path="."
289            dlg = wxFileDialog(self, _("Export Map"), export_path, "",
290                               "Enhanced Metafile (*.wmf)|*.wmf",
291                               wxSAVE|wxOVERWRITE_PROMPT)
292            if dlg.ShowModal() == wxID_OK:
293                self.export_path = os.path.dirname(dlg.GetPath())
294                dc = wxMetaFileDC(dlg.GetPath())
295        
296                scale, offset, mapregion = output_transform(self.scale,
297                                                            self.offset,
298                                                            self.GetSizeTuple(),
299                                                            dc.GetSizeTuple())
300    
301                selected_layer = self.selection.SelectedLayer()
302                selected_shapes = self.selection.SelectedShapes()
303    
304                width, height = self.GetSizeTuple()
305                renderer = ExportRenderer(dc, self.Map(), scale, offset,
306                                          region = (0, 0,
307                                                    (width/self.scale)*scale,
308                                                    (height/self.scale)*scale),
309                                          destination_region = mapregion)
310                renderer.RenderMap(selected_layer, selected_shapes)
311    
312      def IdentifyTool(self):              dc.EndDrawing()
313          self.tool = IdentifyTool(self)              dc.Close()
314            dlg.Destroy()
315            
316        def Print(self):
317            printer = wx.wxPrinter()
318            width, height = self.GetSizeTuple()
319            selected_layer = self.selection.SelectedLayer()
320            selected_shapes = self.selection.SelectedShapes()
321            
322            printout = MapPrintout(self, self.Map(), (0, 0, width, height),
323                                   selected_layer, selected_shapes)
324            printer.Print(self, printout, True)
325            printout.Destroy()
326    
327      def LabelTool(self):      def redraw(self, *args):
328          self.tool = LabelTool(self)          self.Refresh(False)
329    
330      def CurrentTool(self):      def full_redraw(self, *args):
331          return self.tool and self.tool.Name() or None          self.bitmap = None
332            self.selection_bitmap = None
333            self.render_iter = None
334            self.redraw()
335    
336      def CurrentPosition(self):      def redraw_selection(self, *args):
337          """Return current position of the mouse in projected coordinates.          self.selection_bitmap = None
338            self.render_iter = None
339            self.redraw()
340    
341          The result is a 2-tuple of floats with the coordinates. If the      def map_projection_changed(self, map, old_proj):
342          mouse is not in the window, the result is None.          ViewPort.map_projection_changed(self, map, old_proj)
343          """          self.full_redraw()
344          if self.current_position is not None:  
345              x, y = self.current_position      def layer_projection_changed(self, *args):
346              return self.win_to_proj(x, y)          ViewPort.layer_projection_changed(self, args)
347          else:          self.full_redraw()
             return None  
348    
349      def set_current_position(self, event):      def set_view_transform(self, scale, offset):
350          """Set the current position from event          ViewPort.set_view_transform(self, scale, offset)
351            self.full_redraw()
352    
353          Should be called by all events that contain mouse positions      def GetPortSizeTuple(self):
354          especially EVT_MOTION. The event paramete may be None to          return self.GetSizeTuple()
         indicate the the pointer left the window.  
         """  
         if event is not None:  
             self.current_position = (event.m_x, event.m_y)  
         else:  
             self.current_position = None  
         self.issue(VIEW_POSITION)  
355    
356      def OnLeftDown(self, event):      def OnLeftDown(self, event):
357          self.set_current_position(event)          self.MouseLeftDown(event)
358          if self.tool is not None:          if self.tool is not None:
359              self.drag_dc = wxClientDC(self)              self.drag_dc = wxClientDC(self)
360              self.drag_dc.SetLogicalFunction(wxINVERT)              self.drag_dc.SetLogicalFunction(wxINVERT)
361              self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)              self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)
             self.CaptureMouse()  
             self.tool.MouseDown(event)  
362              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
363                self.CaptureMouse()
364              self.dragging = 1              self.dragging = 1
365            
366      def OnLeftUp(self, event):      def OnLeftUp(self, event):
367          self.ReleaseMouse()          """Handle EVT_LEFT_UP
368          self.set_current_position(event)  
369            Release the mouse if it was captured, if a tool is active call
370            its Hide method and call self.MouseLeftUp.
371            """
372            # It's important that ReleaseMouse is called before MouseLeftUp.
373            # MouseLeftUp may pop up modal dialogs which leads to an
374            # effectively frozen X session because the user can only
375            # interact with the dialog but the mouse is still grabbed by the
376            # canvas.
377          if self.dragging:          if self.dragging:
378              self.tool.Hide(self.drag_dc)              if self.HasCapture():
379              self.tool.MouseUp(event)                  self.ReleaseMouse()
380              self.drag_dc = None              try:
381          self.dragging = 0                  self.tool.Hide(self.drag_dc)
382                finally:
383                    self.drag_dc = None
384                    self.dragging = 0
385            self.MouseLeftUp(event)
386    
387      def OnMotion(self, event):      def OnMotion(self, event):
         self.set_current_position(event)  
388          if self.dragging:          if self.dragging:
389              self.tool.Hide(self.drag_dc)              self.tool.Hide(self.drag_dc)
390              self.tool.MouseMove(event)  
391            self.MouseMove(event)
392    
393            if self.dragging:
394              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
395    
396      def OnLeaveWindow(self, event):      def OnLeaveWindow(self, event):
397          self.set_current_position(None)          self.set_current_position(None)
398    
399      def OnIdle(self, event):      def OnSize(self, event):
400          if self.redraw_on_idle:          # the window's size has changed. We have to get a new bitmap. If
401              self.do_redraw()          # we want to be clever we could try to get by without throwing
402          self.redraw_on_idle = 0          # everything away. E.g. when the window gets smaller, we could
403            # either keep the bitmap or create the new one from the old one.
404            # Even when the window becomes larger some parts of the bitmap
405            # could be reused.
406            self.full_redraw()
407    
408      def shape_selected(self, layer, shape):      def shape_selected(self, layer, shape):
409          self.redraw()          """Receiver for the SHAPES_SELECTED messages. Redraw the map."""
410            # The selection object takes care that it only issues
411      def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):          # SHAPES_SELECTED messages when the set of selected shapes has
412          """Determine the shape at point px, py in window coords          # actually changed, so we can do a full redraw of the
413            # selection_bitmap unconditionally.
414          Return the shape and the corresponding layer as a tuple (layer,          ViewPort.shape_selected(self, layer, shape)
415          shape).          self.redraw_selection()
416    
417          If the optional parameter select_labels is true (default false)      def GetTextExtent(self, text):
418          search through the labels. If a label is found return it's index          dc = wxClientDC(self)
419          as the shape and None as the layer.          font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
420            dc.SetFont(font)
421          If the optional parameter selected_layer is true (default), only          return dc.GetTextExtent(text)
422          search in the currently selected layer.  
423        def LabelShapeAt(self, x, y, text=None):
424            """Add or remove a label at window position x, y.
425    
426            If there's a label at the given position, remove it. Otherwise
427            determine the shape at the position, run the label dialog and
428            unless the user cancels the dialog, add a label.
429          """          """
         map_proj = self.map.projection  
         if map_proj is not None:  
             forward = map_proj.Forward  
         else:  
             forward = None  
   
         scale = self.scale  
         offx, offy = self.offset  
   
         if select_labels:  
             labels = self.map.LabelLayer().Labels()  
               
             if labels:  
                 dc = wxClientDC(self)  
                 font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)  
                 dc.SetFont(font)  
                 for i in range(len(labels) - 1, -1, -1):  
                     label = labels[i]  
                     x = label.x  
                     y = label.y  
                     text = label.text  
                     if forward:  
                         x, y = forward(x, y)  
                     x = x * scale + offx  
                     y = -y * scale + offy  
                     width, height = dc.GetTextExtent(text)  
                     if label.halign == ALIGN_LEFT:  
                         # nothing to be done  
                         pass  
                     elif label.halign == ALIGN_RIGHT:  
                         x = x - width  
                     elif label.halign == ALIGN_CENTER:  
                         x = x - width/2  
                     if label.valign == ALIGN_TOP:  
                         # nothing to be done  
                         pass  
                     elif label.valign == ALIGN_BOTTOM:  
                         y = y - height  
                     elif label.valign == ALIGN_CENTER:  
                         y = y - height/2  
                     if x <= px < x + width and y <= py <= y + height:  
                         return None, i  
   
         if selected_layer:  
             layer = self.interactor.SelectedLayer()  
             if layer is not None:  
                 layers = [layer]  
             else:  
                 # no layer selected. Use an empty list to effectively  
                 # ignore all layers.  
                 layers = []  
         else:  
             layers = self.map.Layers()  
   
         for layer_index in range(len(layers) - 1, -1, -1):  
             layer = layers[layer_index]  
   
             # search only in visible layers  
             if not layer.Visible():  
                 continue  
   
             filled = layer.fill is not None  
             stroked = layer.stroke is not None  
                   
             layer_proj = layer.projection  
             if layer_proj is not None:  
                 inverse = layer_proj.Inverse  
             else:  
                 inverse = None  
                   
             shapetype = layer.ShapeType()  
   
             select_shape = -1  
             if shapetype == SHAPETYPE_POLYGON:  
                 for i in range(layer.NumShapes() - 1, -1, -1):  
                     result = point_in_polygon_shape(layer.shapefile.cobject(),  
                                                     i,  
                                                     filled, stroked,  
                                                     map_proj, layer_proj,  
                                                     scale, -scale, offx, offy,  
                                                     px, py)  
                     if result:  
                         select_shape = i  
                         break  
             elif shapetype == SHAPETYPE_ARC:  
                 for i in range(layer.NumShapes() - 1, -1, -1):  
                     result = point_in_polygon_shape(layer.shapefile.cobject(),  
                                                     i, 0, 1,  
                                                     map_proj, layer_proj,  
                                                     scale, -scale, offx, offy,  
                                                     px, py)  
                     if result < 0:  
                         select_shape = i  
                         break  
             elif shapetype == SHAPETYPE_POINT:  
                 for i in range(layer.NumShapes() - 1, -1, -1):  
                     shape = layer.Shape(i)  
                     x, y = shape.Points()[0]  
                     if inverse:  
                         x, y = inverse(x, y)  
                     if forward:  
                         x, y = forward(x, y)  
                     x = x * scale + offx  
                     y = -y * scale + offy  
                     if hypot(px - x, py - y) < 5:  
                         select_shape = i  
                         break  
   
             if select_shape >= 0:  
                 return layer, select_shape  
         return None, None  
   
     def SelectShapeAt(self, x, y):  
         layer, shape = self.find_shape_at(x, y, selected_layer = 0)  
         # If layer is None, then shape will also be None. We don't want  
         # to deselect the currently selected layer, so we simply select  
         # the already selected layer again.  
         if layer is None:  
             layer = self.interactor.SelectedLayer()  
         self.interactor.SelectLayerAndShape(layer, shape)  
   
     def LabelShapeAt(self, x, y):  
         ox = x; oy = y  
430          label_layer = self.map.LabelLayer()          label_layer = self.map.LabelLayer()
431          layer, shape_index = self.find_shape_at(x, y, select_labels = 1)          layer, shape_index = self.find_shape_at(x, y, select_labels = 1)
432          if layer is None and shape_index is not None:          if layer is None and shape_index is not None:
433              # a label was selected              ViewPort.LabelShapeAt(self, x, y)
             label_layer.RemoveLabel(shape_index)  
434          elif layer is not None:          elif layer is not None:
435              text = labeldialog.run_label_dialog(self, layer.table, shape_index)              text = labeldialog.run_label_dialog(self,
436              if text:                                                  layer.ShapeStore().Table(),
437                  proj = self.map.projection                                                  shape_index)
438                  if proj is not None:              ViewPort.LabelShapeAt(self, x, y, text)
                     map_proj = proj  
                 else:  
                     map_proj = None  
                 proj = layer.projection  
                 if proj is not None:  
                     layer_proj = proj  
                 else:  
                     layer_proj = None  
   
                 shapetype = layer.ShapeType()  
                 if shapetype == SHAPETYPE_POLYGON:  
                     x, y = shape_centroid(layer.shapefile.cobject(),  
                                           shape_index,  
                                           map_proj, layer_proj, 1, 1, 0, 0)  
                     if map_proj is not None:  
                         x, y = map_proj.Inverse(x, y)  
                 else:  
                     shape = layer.Shape(shape_index)  
                     if shapetype == SHAPETYPE_POINT:  
                         x, y = shape.Points()[0]  
                     else:  
                         # assume SHAPETYPE_ARC  
                         points = shape.Points()  
                         x, y = points[len(points) / 2]  
                     if layer_proj is not None:  
                         x, y = layer_proj.Inverse(x, y)  
                 if shapetype == SHAPETYPE_POINT:  
                     halign = ALIGN_LEFT  
                     valign = ALIGN_CENTER  
                 elif shapetype == SHAPETYPE_POLYGON:  
                     halign = ALIGN_CENTER  
                     valign = ALIGN_CENTER  
                 elif shapetype == SHAPETYPE_ARC:  
                     halign = ALIGN_LEFT  
                     valign = ALIGN_CENTER  
                 label_layer.AddLabel(x, y, text,  
                                      halign = halign, valign = valign)  

Legend:
Removed from v.122  
changed lines
  Added in v.2454

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26