/[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 159 by bh, Wed May 8 13:46:15 2002 UTC revision 433 by jonathan, Mon Feb 24 18:47:49 2003 UTC
# Line 1  Line 1 
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  #  #
# Line 30  from Thuban.Model.layer import SHAPETYPE Line 30  from Thuban.Model.layer import SHAPETYPE
30  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \  from Thuban.Model.label import ALIGN_CENTER, ALIGN_TOP, ALIGN_BOTTOM, \
31       ALIGN_LEFT, ALIGN_RIGHT       ALIGN_LEFT, ALIGN_RIGHT
32  from Thuban.Lib.connector import Publisher  from Thuban.Lib.connector import Publisher
33    from Thuban.Model.color import Color
34    
35  from renderer import ScreenRenderer, PrinterRender  from renderer import ScreenRenderer, PrinterRender
36    
# Line 129  class ZoomInTool(RectTool): Line 130  class ZoomInTool(RectTool):
130              Tool.MouseUp(self, event)              Tool.MouseUp(self, event)
131              sx, sy = self.start              sx, sy = self.start
132              cx, cy = self.current              cx, cy = self.current
133              if sx == cx and sy == cy:              if sx == cx or sy == cy:
134                  # Just a mouse click. Simply zoom in by a factor of two                  # Just a mouse click or a degenerate rectangle. Simply
135                    # zoom in by a factor of two
136                    # FIXME: For a click this is the desired behavior but should we
137                    # really do this for degenrate rectagles as well or
138                    # should we ignore them?
139                  self.view.ZoomFactor(2, center = (cx, cy))                  self.view.ZoomFactor(2, center = (cx, cy))
140              else:              else:
141                  # A drag. Zoom in to the rectangle                  # A drag. Zoom in to the rectangle
# Line 140  class ZoomInTool(RectTool): Line 145  class ZoomInTool(RectTool):
145  class ZoomOutTool(RectTool):  class ZoomOutTool(RectTool):
146    
147      """The Zoom-Out Tool"""      """The Zoom-Out Tool"""
148        
149      def Name(self):      def Name(self):
150          return "ZoomOutTool"          return "ZoomOutTool"
151    
# Line 149  class ZoomOutTool(RectTool): Line 154  class ZoomOutTool(RectTool):
154              Tool.MouseUp(self, event)              Tool.MouseUp(self, event)
155              sx, sy = self.start              sx, sy = self.start
156              cx, cy = self.current              cx, cy = self.current
157              if sx == cx and sy == cy:              if sx == cx or sy == cy:
158                  # Just a mouse click. Simply zoom out by a factor of two                  # Just a mouse click or a degenerate rectangle. Simply
159                  self.view.ZoomFactor(0.5, center = (cy, cy))                  # zoom out by a factor of two.
160                    # FIXME: For a click this is the desired behavior but should we
161                    # really do this for degenrate rectagles as well or
162                    # should we ignore them?
163                    self.view.ZoomFactor(0.5, center = (cx, cy))
164              else:              else:
165                  # A drag. Zoom out to the rectangle                  # A drag. Zoom out to the rectangle
166                  self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),                  self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),
# Line 184  class PanTool(Tool): Line 193  class PanTool(Tool):
193              sx, sy = self.start              sx, sy = self.start
194              cx, cy = self.current              cx, cy = self.current
195              self.view.Translate(cx - sx, cy - sy)              self.view.Translate(cx - sx, cy - sy)
196            
197  class IdentifyTool(Tool):  class IdentifyTool(Tool):
198    
199      """The "Identify" Tool"""      """The "Identify" Tool"""
200        
201      def Name(self):      def Name(self):
202          return "IdentifyTool"          return "IdentifyTool"
203    
# Line 242  class MapPrintout(wx.wxPrintout): Line 251  class MapPrintout(wx.wxPrintout):
251          renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)          renderer = PrinterRender(dc, scale, (offx, offy), resolution = resx)
252          renderer.RenderMap(self.map)          renderer.RenderMap(self.map)
253          return wx.true          return wx.true
254            
255    
256  class MapCanvas(wxWindow, Publisher):  class MapCanvas(wxWindow, Publisher):
257    
# Line 271  class MapCanvas(wxWindow, Publisher): Line 280  class MapCanvas(wxWindow, Publisher):
280          # if the mouse is outside the window.          # if the mouse is outside the window.
281          self.current_position = None          self.current_position = None
282    
         # 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()  
   
283          # the bitmap serving as backing store          # the bitmap serving as backing store
284          self.bitmap = None          self.bitmap = None
285    
# Line 286  class MapCanvas(wxWindow, Publisher): Line 287  class MapCanvas(wxWindow, Publisher):
287          self.interactor = interactor          self.interactor = interactor
288          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)
289    
290            # keep track of which layers/shapes are selected to make sure we
291            # only redraw when necessary
292            self.last_selected_layer = None
293            self.last_selected_shape = None
294    
295          # subscribe the WX events we're interested in          # subscribe the WX events we're interested in
296          EVT_PAINT(self, self.OnPaint)          EVT_PAINT(self, self.OnPaint)
297          EVT_LEFT_DOWN(self, self.OnLeftDown)          EVT_LEFT_DOWN(self, self.OnLeftDown)
# Line 293  class MapCanvas(wxWindow, Publisher): Line 299  class MapCanvas(wxWindow, Publisher):
299          EVT_MOTION(self, self.OnMotion)          EVT_MOTION(self, self.OnMotion)
300          EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)          EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)
301          wx.EVT_SIZE(self, self.OnSize)          wx.EVT_SIZE(self, self.OnSize)
         wx.EVT_IDLE(self, self.OnIdle)  
302    
303      def __del__(self):      def __del__(self):
304          wxWindow.__del__(self)          wxWindow.__del__(self)
# Line 302  class MapCanvas(wxWindow, Publisher): Line 307  class MapCanvas(wxWindow, Publisher):
307      def OnPaint(self, event):      def OnPaint(self, event):
308          dc = wxPaintDC(self)          dc = wxPaintDC(self)
309          if self.map is not None and self.map.HasLayers():          if self.map is not None and self.map.HasLayers():
310              # We have a non-empty map. Redraw it in idle time              self.do_redraw()
             self.redraw_on_idle = 1  
             # update the region that has to be redrawn  
             self.update_region.UnionRegion(self.GetUpdateRegion())  
311          else:          else:
312              # 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
313              # the screen.              # the screen.
314                
315              # XXX it's probably possible to get rid of this. The              # XXX it's probably possible to get rid of this. The
316              # background color of the window is already white and the              # background color of the window is already white and the
317              # only thing we may have to do is to call self.Refresh()              # only thing we may have to do is to call self.Refresh()
318              # with a true argument in the right places.              # with a true argument in the right places.
319              dc.BeginDrawing()              dc.BeginDrawing()
320              dc.Clear()                          dc.Clear()
321              dc.EndDrawing()              dc.EndDrawing()
322    
             # clear the region  
             self.update_region = wx.wxRegion()  
   
323      def do_redraw(self):      def do_redraw(self):
324          # This should only be called if we have a non-empty map.          # This should only be called if we have a non-empty map.
325    
         # 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()  
   
326          # Get the window size.          # Get the window size.
327          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
328    
# Line 356  class MapCanvas(wxWindow, Publisher): Line 350  class MapCanvas(wxWindow, Publisher):
350              # draw the map into the bitmap              # draw the map into the bitmap
351              renderer = ScreenRenderer(dc, self.scale, self.offset)              renderer = ScreenRenderer(dc, self.scale, self.offset)
352    
353              # Pass the entire bitmap as update_region to the renderer.              # Pass the entire bitmap as update region to the renderer.
354              # We're redrawing the whole bitmap, after all.              # We're redrawing the whole bitmap, after all.
355              renderer.RenderMap(self.map, (0, 0, width, height),              renderer.RenderMap(self.map, (0, 0, width, height),
356                                 selected_layer, selected_shape)                                 selected_layer, selected_shape)
# Line 378  class MapCanvas(wxWindow, Publisher): Line 372  class MapCanvas(wxWindow, Publisher):
372          printout = MapPrintout(self.map)          printout = MapPrintout(self.map)
373          printer.Print(self, printout, wx.true)          printer.Print(self, printout, wx.true)
374          printout.Destroy()          printout.Destroy()
375            
376      def SetMap(self, map):      def SetMap(self, map):
377          redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED,          redraw_channels = (LAYERS_CHANGED, LAYER_LEGEND_CHANGED,
378                             LAYER_VISIBILITY_CHANGED)                             LAYER_VISIBILITY_CHANGED)
# Line 399  class MapCanvas(wxWindow, Publisher): Line 393  class MapCanvas(wxWindow, Publisher):
393          self.full_redraw()          self.full_redraw()
394    
395      def Map(self):      def Map(self):
396            """Return the map displayed by this canvas"""
397          return self.map          return self.map
398    
399      def redraw(self, *args):      def redraw(self, *args):
# Line 432  class MapCanvas(wxWindow, Publisher): Line 427  class MapCanvas(wxWindow, Publisher):
427          return ((x - offx) / self.scale, (offy - y) / self.scale)          return ((x - offx) / self.scale, (offy - y) / self.scale)
428    
429      def FitRectToWindow(self, rect):      def FitRectToWindow(self, rect):
430            """Fit the rectangular region given by rect into the window.
431            
432            Set scale so that rect (in projected coordinates) just fits into
433            the window and center it.
434            """
435          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
436          llx, lly, urx, ury = rect          llx, lly, urx, ury = rect
437          if llx == urx or lly == ury:          if llx == urx or lly == ury:
# Line 445  class MapCanvas(wxWindow, Publisher): Line 445  class MapCanvas(wxWindow, Publisher):
445          self.set_view_transform(scale, (offx, offy))          self.set_view_transform(scale, (offx, offy))
446    
447      def FitMapToWindow(self):      def FitMapToWindow(self):
448          """\          """Fit the map to the window
449          Set the scale and offset so that the map is centered in the          
450          window          Set the scale so that the map fits exactly into the window and
451            center it in the window.
452          """          """
453          bbox = self.map.ProjectedBoundingBox()          bbox = self.map.ProjectedBoundingBox()
454          if bbox is not None:          if bbox is not None:
# Line 473  class MapCanvas(wxWindow, Publisher): Line 474  class MapCanvas(wxWindow, Publisher):
474          self.set_view_transform(scale, offset)          self.set_view_transform(scale, offset)
475    
476      def ZoomOutToRect(self, rect):      def ZoomOutToRect(self, rect):
477          # rect is given in window coordinates          """Zoom out to fit the currently visible region into rect.
478    
479            The rect parameter is given in window coordinates
480            """
481          # determine the bbox of the displayed region in projected          # determine the bbox of the displayed region in projected
482          # coordinates          # coordinates
483          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
# Line 491  class MapCanvas(wxWindow, Publisher): Line 494  class MapCanvas(wxWindow, Publisher):
494          self.set_view_transform(scale, (offx, offy))          self.set_view_transform(scale, (offx, offy))
495    
496      def Translate(self, dx, dy):      def Translate(self, dx, dy):
497            """Move the map by dx, dy pixels"""
498          offx, offy = self.offset          offx, offy = self.offset
499          self.set_view_transform(self.scale, (offx + dx, offy + dy))          self.set_view_transform(self.scale, (offx + dx, offy + dy))
500    
501        def SelectTool(self, tool):
502            """Make tool the active tool.
503    
504            The parameter should be an instance of Tool or None to indicate
505            that no tool is active.
506            """
507            self.tool = tool
508    
509      def ZoomInTool(self):      def ZoomInTool(self):
510          self.tool = ZoomInTool(self)          """Start the zoom in tool"""
511            self.SelectTool(ZoomInTool(self))
512    
513      def ZoomOutTool(self):      def ZoomOutTool(self):
514          self.tool = ZoomOutTool(self)          """Start the zoom out tool"""
515            self.SelectTool(ZoomOutTool(self))
516    
517      def PanTool(self):      def PanTool(self):
518          self.tool = PanTool(self)          """Start the pan tool"""
519            self.SelectTool(PanTool(self))
520    
521      def IdentifyTool(self):      def IdentifyTool(self):
522          self.tool = IdentifyTool(self)          """Start the identify tool"""
523            self.SelectTool(IdentifyTool(self))
524    
525      def LabelTool(self):      def LabelTool(self):
526          self.tool = LabelTool(self)          """Start the label tool"""
527            self.SelectTool(LabelTool(self))
528    
529      def CurrentTool(self):      def CurrentTool(self):
530            """Return the name of the current tool or None if no tool is active"""
531          return self.tool and self.tool.Name() or None          return self.tool and self.tool.Name() or None
532    
533      def CurrentPosition(self):      def CurrentPosition(self):
# Line 547  class MapCanvas(wxWindow, Publisher): Line 565  class MapCanvas(wxWindow, Publisher):
565              self.tool.MouseDown(event)              self.tool.MouseDown(event)
566              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
567              self.dragging = 1              self.dragging = 1
568            
569      def OnLeftUp(self, event):      def OnLeftUp(self, event):
         self.ReleaseMouse()  
570          self.set_current_position(event)          self.set_current_position(event)
571          if self.dragging:          if self.dragging:
572              self.tool.Hide(self.drag_dc)              self.ReleaseMouse()
573              self.tool.MouseUp(event)              try:
574              self.drag_dc = None                  self.tool.Hide(self.drag_dc)
575          self.dragging = 0                  self.tool.MouseUp(event)
576                finally:
577                    self.drag_dc = None
578                    self.dragging = 0
579    
580      def OnMotion(self, event):      def OnMotion(self, event):
581          self.set_current_position(event)          self.set_current_position(event)
# Line 567  class MapCanvas(wxWindow, Publisher): Line 587  class MapCanvas(wxWindow, Publisher):
587      def OnLeaveWindow(self, event):      def OnLeaveWindow(self, event):
588          self.set_current_position(None)          self.set_current_position(None)
589    
     def OnIdle(self, event):  
         if self.redraw_on_idle:  
             self.do_redraw()  
         self.redraw_on_idle = 0  
   
590      def OnSize(self, event):      def OnSize(self, event):
591          # 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
592          # 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
# Line 582  class MapCanvas(wxWindow, Publisher): Line 597  class MapCanvas(wxWindow, Publisher):
597          self.full_redraw()          self.full_redraw()
598    
599      def shape_selected(self, layer, shape):      def shape_selected(self, layer, shape):
600          self.full_redraw()          """Redraw the map.
601    
602            Receiver for the SELECTED_SHAPE messages. Try to redraw only
603            when necessary.
604            """
605            # A redraw is necessary when the display has to change, which
606            # means that either the status changes from having no selection
607            # to having a selection shape or vice versa, or when the fact
608            # whether there is a selection at all doesn't change, when the
609            # shape which is selected has changed (which means that layer or
610            # shapeid changes).
611            if ((shape is not None or self.last_selected_shape is not None)
612                and (shape != self.last_selected_shape
613                     or layer != self.last_selected_layer)):
614                self.full_redraw()
615    
616            # remember the selection so we can compare when it changes again.
617            self.last_selected_layer = layer
618            self.last_selected_shape = shape
619    
620      def unprojected_rect_around_point(self, x, y):      def unprojected_rect_around_point(self, x, y, dist):
621          """return a rect a few pixels around (x, y) in unprojected corrdinates          """return a rect dist pixels around (x, y) in unprojected corrdinates
622    
623          The return value is a tuple (minx, miny, maxx, maxy) suitable a          The return value is a tuple (minx, miny, maxx, maxy) suitable a
624          parameter to a layer's ShapesInRegion method.          parameter to a layer's ShapesInRegion method.
# Line 599  class MapCanvas(wxWindow, Publisher): Line 632  class MapCanvas(wxWindow, Publisher):
632          xs = []          xs = []
633          ys = []          ys = []
634          for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)):          for dx, dy in ((-1, -1), (1, -1), (1, 1), (-1, 1)):
635              px, py = self.win_to_proj(x + dx, y + dy)              px, py = self.win_to_proj(x + dist * dx, y + dist * dy)
636              if inverse:              if inverse:
637                  px, py = inverse(px, py)                  px, py = inverse(px, py)
638              xs.append(px)              xs.append(px)
639              ys.append(py)              ys.append(py)
640          return (min(xs), min(ys), max(xs), max(ys))          return (min(xs), min(ys), max(xs), max(ys))
641    
642      def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):      def find_shape_at(self, px, py, select_labels = 0, searched_layer = None):
643          """Determine the shape at point px, py in window coords          """Determine the shape at point px, py in window coords
644    
645          Return the shape and the corresponding layer as a tuple (layer,          Return the shape and the corresponding layer as a tuple (layer,
# Line 616  class MapCanvas(wxWindow, Publisher): Line 649  class MapCanvas(wxWindow, Publisher):
649          search through the labels. If a label is found return it's index          search through the labels. If a label is found return it's index
650          as the shape and None as the layer.          as the shape and None as the layer.
651    
652          If the optional parameter selected_layer is true (default), only          If the optional parameter searched_layer is given (or not None
653          search in the currently selected layer.          which it defaults to), only search in that layer.
654          """          """
655          map_proj = self.map.projection          map_proj = self.map.projection
656          if map_proj is not None:          if map_proj is not None:
# Line 628  class MapCanvas(wxWindow, Publisher): Line 661  class MapCanvas(wxWindow, Publisher):
661          scale = self.scale          scale = self.scale
662          offx, offy = self.offset          offx, offy = self.offset
663    
         box = self.unprojected_rect_around_point(px, py)  
   
664          if select_labels:          if select_labels:
665              labels = self.map.LabelLayer().Labels()              labels = self.map.LabelLayer().Labels()
666                
667              if labels:              if labels:
668                  dc = wxClientDC(self)                  dc = wxClientDC(self)
669                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
# Line 664  class MapCanvas(wxWindow, Publisher): Line 695  class MapCanvas(wxWindow, Publisher):
695                      if x <= px < x + width and y <= py <= y + height:                      if x <= px < x + width and y <= py <= y + height:
696                          return None, i                          return None, i
697    
698          if selected_layer:          if searched_layer:
699              layer = self.interactor.SelectedLayer()              layers = [searched_layer]
             if layer is not None:  
                 layers = [layer]  
             else:  
                 # no layer selected. Use an empty list to effectively  
                 # ignore all layers.  
                 layers = []  
700          else:          else:
701              layers = self.map.Layers()              layers = self.map.Layers()
702    
# Line 682  class MapCanvas(wxWindow, Publisher): Line 707  class MapCanvas(wxWindow, Publisher):
707              if not layer.Visible():              if not layer.Visible():
708                  continue                  continue
709    
710              filled = layer.fill is not None              filled = layer.GetClassification().GetDefaultFill() \
711              stroked = layer.stroke is not None                       is not Color.None
712                                stroked = layer.GetClassification().GetDefaultStroke() \
713                          is not Color.None
714    
715              layer_proj = layer.projection              layer_proj = layer.projection
716              if layer_proj is not None:              if layer_proj is not None:
717                  inverse = layer_proj.Inverse                  inverse = layer_proj.Inverse
718              else:              else:
719                  inverse = None                  inverse = None
720                    
721              shapetype = layer.ShapeType()              shapetype = layer.ShapeType()
722    
723              select_shape = -1              select_shape = -1
724    
725                # Determine the ids of the shapes that overlap a tiny area
726                # around the point. For layers containing points we have to
727                # choose a larger size of the box we're testing agains so
728                # that we take the size of the markers into account
729                # FIXME: Once the markers are more flexible this part has to
730                # become more flexible too, of course
731                if shapetype == SHAPETYPE_POINT:
732                    box = self.unprojected_rect_around_point(px, py, 5)
733                else:
734                    box = self.unprojected_rect_around_point(px, py, 1)
735              shape_ids = layer.ShapesInRegion(box)              shape_ids = layer.ShapesInRegion(box)
736              shape_ids.reverse()              shape_ids.reverse()
737    
# Line 737  class MapCanvas(wxWindow, Publisher): Line 774  class MapCanvas(wxWindow, Publisher):
774                  return layer, select_shape                  return layer, select_shape
775          return None, None          return None, None
776    
777      def SelectShapeAt(self, x, y):      def SelectShapeAt(self, x, y, layer = None):
778          layer, shape = self.find_shape_at(x, y, selected_layer = 0)          """\
779            Select and return the shape and its layer at window position (x, y)
780    
781            If layer is given, only search in that layer. If no layer is
782            given, search through all layers.
783    
784            Return a tuple (layer, shapeid). If no shape is found, return
785            (None, None).
786            """
787            layer, shape = result = self.find_shape_at(x, y, searched_layer=layer)
788          # If layer is None, then shape will also be None. We don't want          # If layer is None, then shape will also be None. We don't want
789          # to deselect the currently selected layer, so we simply select          # to deselect the currently selected layer, so we simply select
790          # the already selected layer again.          # the already selected layer again.
791          if layer is None:          if layer is None:
792              layer = self.interactor.SelectedLayer()              layer = self.interactor.SelectedLayer()
793          self.interactor.SelectLayerAndShape(layer, shape)          self.interactor.SelectLayerAndShape(layer, shape)
794            return result
795    
796      def LabelShapeAt(self, x, y):      def LabelShapeAt(self, x, y):
797            """Add or remove a label at window position x, y.
798    
799            If there's a label at the given position, remove it. Otherwise
800            determine the shape at the position, run the label dialog and
801            unless the user cancels the dialog, add a laber.
802            """
803          ox = x; oy = y          ox = x; oy = y
804          label_layer = self.map.LabelLayer()          label_layer = self.map.LabelLayer()
805          layer, shape_index = self.find_shape_at(x, y, select_labels = 1)          layer, shape_index = self.find_shape_at(x, y, select_labels = 1)

Legend:
Removed from v.159  
changed lines
  Added in v.433

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26