/[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 6 by bh, Tue Aug 28 15:41:52 2001 UTC revision 57 by bh, Thu Sep 13 13:55:33 2001 UTC
# Line 127  class ZoomInTool(RectTool): Line 127  class ZoomInTool(RectTool):
127      def MouseUp(self, event):      def MouseUp(self, event):
128          if self.dragging:          if self.dragging:
129              Tool.MouseUp(self, event)              Tool.MouseUp(self, event)
130              self.view.FitRectToWindow(self.proj_rect())              sx, sy = self.start
131                cx, cy = self.current
132                if sx == cx and sy == cy:
133                    # Just a mouse click. Simply zoom in by a factor of two
134                    self.view.ZoomFactor(2, center = (cx, cy))
135                else:
136                    # A drag. Zoom in to the rectangle
137                    self.view.FitRectToWindow(self.proj_rect())
138    
139    
140  class ZoomOutTool(RectTool):  class ZoomOutTool(RectTool):
# Line 142  class ZoomOutTool(RectTool): Line 149  class ZoomOutTool(RectTool):
149              Tool.MouseUp(self, event)              Tool.MouseUp(self, event)
150              sx, sy = self.start              sx, sy = self.start
151              cx, cy = self.current              cx, cy = self.current
152              self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),              if sx == cx and sy == cy:
153                                       max(sx, cx), max(sy, cy)))                  # Just a mouse click. Simply zoom out by a factor of two
154                    self.view.ZoomFactor(0.5, center = (cy, cy))
155                else:
156                    # A drag. Zoom out to the rectangle
157                    self.view.ZoomOutToRect((min(sx, cx), min(sy, cy),
158                                             max(sx, cx), max(sy, cy)))
159    
160    
161  class PanTool(Tool):  class PanTool(Tool):
# Line 232  class MapCanvas(wxWindow): Line 244  class MapCanvas(wxWindow):
244    
245      """A widget that displays a map and offers some interaction"""      """A widget that displays a map and offers some interaction"""
246    
247      def __init__(self, parent, winid):      def __init__(self, parent, winid, interactor):
248          wxWindow.__init__(self, parent, winid)          wxWindow.__init__(self, parent, winid)
249          self.SetBackgroundColour(wxColour(255, 255, 255))          self.SetBackgroundColour(wxColour(255, 255, 255))
250          self.map = None          self.map = None
# Line 246  class MapCanvas(wxWindow): Line 258  class MapCanvas(wxWindow):
258          EVT_LEFT_UP(self, self.OnLeftUp)          EVT_LEFT_UP(self, self.OnLeftUp)
259          EVT_MOTION(self, self.OnMotion)          EVT_MOTION(self, self.OnMotion)
260          wx.EVT_IDLE(self, self.OnIdle)          wx.EVT_IDLE(self, self.OnIdle)
261          import main          self.interactor = interactor
         self.interactor = main.app.interactor  
262          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)
263    
264      def OnPaint(self, event):      def OnPaint(self, event):
265          dc = wxPaintDC(self)          dc = wxPaintDC(self)
266          if self.map is None or not self.map.HasLayers():          if self.map is not None and self.map.HasLayers():
267              return              # We have a non-empty map. Redraw it in idle time
268          self.redraw_on_idle = 1              self.redraw_on_idle = 1
269            else:
270                # If we've got no map or if the map is empty, simply clear
271                # the screen.
272                
273                # XXX it's probably possible to get rid of this. The
274                # background color of the window is already white and the
275                # only thing we may have to do is to call self.Refresh()
276                # with a true argument in the right places.
277                dc.BeginDrawing()
278                dc.Clear()            
279                dc.EndDrawing()
280    
281      def do_redraw(self):      def do_redraw(self):
282            # This should only be called if we have a non-empty map. We draw
283            # it into a memory DC and then blit it to the screen.
284          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
285          bitmap = wx.wxEmptyBitmap(width, height)          bitmap = wx.wxEmptyBitmap(width, height)
   
286          dc = wx.wxMemoryDC()          dc = wx.wxMemoryDC()
287          dc.SelectObject(bitmap)          dc.SelectObject(bitmap)
   
288          dc.BeginDrawing()          dc.BeginDrawing()
289    
290            # clear the background
291          dc.SetBrush(wx.wxWHITE_BRUSH)          dc.SetBrush(wx.wxWHITE_BRUSH)
292          dc.SetPen(wx.wxTRANSPARENT_PEN)          dc.SetPen(wx.wxTRANSPARENT_PEN)
293          dc.DrawRectangle(0, 0, width, height)          dc.DrawRectangle(0, 0, width, height)
# Line 275  class MapCanvas(wxWindow): Line 298  class MapCanvas(wxWindow):
298          else:          else:
299              selected_layer = None              selected_layer = None
300              selected_shape = None              selected_shape = None
301                
302            # draw the map into the bitmap
303          renderer = ScreenRenderer(dc, self.scale, self.offset)          renderer = ScreenRenderer(dc, self.scale, self.offset)
304          renderer.RenderMap(self.map, selected_layer, selected_shape)          renderer.RenderMap(self.map, selected_layer, selected_shape)
305    
306            dc.EndDrawing()
307    
308            # blit the bitmap to the screen
309          clientdc = wxClientDC(self)          clientdc = wxClientDC(self)
310          clientdc.BeginDrawing()          clientdc.BeginDrawing()
311          clientdc.Blit(0, 0, width, height, dc, 0, 0)          clientdc.Blit(0, 0, width, height, dc, 0, 0)
312          clientdc.EndDrawing()          clientdc.EndDrawing()
313    
   
314      def Print(self):      def Print(self):
315          printer = wx.wxPrinter()          printer = wx.wxPrinter()
316          printout = MapPrintout(self.map)          printout = MapPrintout(self.map)
# Line 305  class MapCanvas(wxWindow): Line 331  class MapCanvas(wxWindow):
331                  self.map.Subscribe(channel, self.redraw)                  self.map.Subscribe(channel, self.redraw)
332              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)
333          self.FitMapToWindow()          self.FitMapToWindow()
334            # force a redraw. If map is not empty, it's already been called
335            # by FitMapToWindow but if map is empty it hasn't been called
336            # yet so we have to explicitly call it.
337            self.redraw()
338    
339      def Map(self):      def Map(self):
340          return self.map          return self.map
# Line 338  class MapCanvas(wxWindow): Line 368  class MapCanvas(wxWindow):
368      def FitRectToWindow(self, rect):      def FitRectToWindow(self, rect):
369          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
370          llx, lly, urx, ury = rect          llx, lly, urx, ury = rect
371            if llx == urx or lly == ury:
372                # zero with or zero height. Do Nothing
373                return
374          scalex = width / (urx - llx)          scalex = width / (urx - llx)
375          scaley = height / (ury - lly)          scaley = height / (ury - lly)
376          scale = min(scalex, scaley)          scale = min(scalex, scaley)
# Line 354  class MapCanvas(wxWindow): Line 387  class MapCanvas(wxWindow):
387          if bbox is not None:          if bbox is not None:
388              self.FitRectToWindow(bbox)              self.FitRectToWindow(bbox)
389    
390      def ZoomFactor(self, factor):      def ZoomFactor(self, factor, center = None):
391            """Multiply the zoom by factor and center on center.
392    
393            The optional parameter center is a point in window coordinates
394            that should be centered. If it is omitted, it defaults to the
395            center of the window
396            """
397          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
398          scale = self.scale * factor          scale = self.scale * factor
399          offx, offy = self.offset          offx, offy = self.offset
400          offset = (factor * (offx - width / 2) + width / 2,          if center is not None:
401                    factor * (offy - height / 2) + height / 2)              cx, cy = center
402            else:
403                cx = width / 2
404                cy = height / 2
405            offset = (factor * (offx - cx) + width / 2,
406                      factor * (offy - cy) + height / 2)
407          self.set_view_transform(scale, offset)          self.set_view_transform(scale, offset)
408    
409      def ZoomOutToRect(self, rect):      def ZoomOutToRect(self, rect):
# Line 434  class MapCanvas(wxWindow): Line 478  class MapCanvas(wxWindow):
478      def shape_selected(self, layer, shape):      def shape_selected(self, layer, shape):
479          self.redraw()          self.redraw()
480    
481      def find_shape_at(self, px, py, select_labels = 0):      def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):
482          """Return a tuple shape at point px, py in window coords."""          """Determine the shape at point px, py in window coords
483    
484            Return the shape and the corresponding layer as a tuple (layer,
485            shape).
486    
487            If the optional parameter select_labels is true (default false)
488            search through the labels. If a label is found return it's index
489            as the shape and None as the layer.
490    
491            If the optional parameter selected_layer is true (default), only
492            search in the currently selected layer.
493            """
494          map_proj = self.map.projection          map_proj = self.map.projection
495          if map_proj is not None:          if map_proj is not None:
496              forward = map_proj.Forward              forward = map_proj.Forward
# Line 478  class MapCanvas(wxWindow): Line 533  class MapCanvas(wxWindow):
533                          y = y - height/2                          y = y - height/2
534                      if x <= px < x + width and y <= py <= y + height:                      if x <= px < x + width and y <= py <= y + height:
535                          return None, i                          return None, i
536                    
537          layers = self.map.Layers()          if selected_layer:
538                layer = self.interactor.SelectedLayer()
539                if layer is not None:
540                    layers = [layer]
541                else:
542                    # no layer selected. Use an empty list to effectively
543                    # ignore all layers.
544                    layers = []
545            else:
546                layers = self.map.Layers()
547    
548          for layer_index in range(len(layers) - 1, -1, -1):          for layer_index in range(len(layers) - 1, -1, -1):
549              layer = layers[layer_index]              layer = layers[layer_index]
550    
# Line 540  class MapCanvas(wxWindow): Line 605  class MapCanvas(wxWindow):
605    
606      def SelectShapeAt(self, x, y):      def SelectShapeAt(self, x, y):
607          layer, shape = self.find_shape_at(x, y)          layer, shape = self.find_shape_at(x, y)
608            # If layer is None, then shape will also be None. We don't want
609            # to deselect the currently selected layer, so we simply select
610            # the already selected layer again.
611            if layer is None:
612                layer = self.interactor.SelectedLayer()
613          self.interactor.SelectLayerAndShape(layer, shape)          self.interactor.SelectLayerAndShape(layer, shape)
614    
615      def LabelShapeAt(self, x, y):      def LabelShapeAt(self, x, y):

Legend:
Removed from v.6  
changed lines
  Added in v.57

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26