/[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 145 by bh, Tue May 7 14:58:05 2002 UTC
# Line 1  Line 1 
1  # Copyright (c) 2001 by Intevation GmbH  # Copyright (c) 2001, 2002 by Intevation GmbH
2  # Authors:  # Authors:
3  # Bernhard Herzog <[email protected]>  # Bernhard Herzog <[email protected]>
4  #  #
# Line 15  from math import hypot Line 15  from math import hypot
15    
16  from wxPython.wx import wxWindow,\  from wxPython.wx import wxWindow,\
17       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
18       EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION       EVT_PAINT, EVT_LEFT_DOWN, EVT_LEFT_UP, EVT_MOTION, EVT_LEAVE_WINDOW
19    
20    
21  from wxPython import wx  from wxPython import wx
# Line 29  from Thuban.Model.layer import SHAPETYPE Line 29  from Thuban.Model.layer import SHAPETYPE
29       SHAPETYPE_POINT       SHAPETYPE_POINT
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
33    
34  from renderer import ScreenRenderer, PrinterRender  from renderer import ScreenRenderer, PrinterRender
35    
36  import labeldialog  import labeldialog
37    
38  from messages import SELECTED_SHAPE  from messages import SELECTED_SHAPE, VIEW_POSITION
39    
40    
41  #  #
# 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 228  class MapPrintout(wx.wxPrintout): Line 240  class MapPrintout(wx.wxPrintout):
240          return wx.true          return wx.true
241                    
242    
243  class MapCanvas(wxWindow):  class MapCanvas(wxWindow, Publisher):
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    
251            # the map displayed in this canvas. Set with SetMap()
252          self.map = None          self.map = None
253    
254            # scale and offset describe the transformation from projected
255            # coordinates to window coordinates.
256          self.scale = 1.0          self.scale = 1.0
257          self.offset = (0, 0)          self.offset = (0, 0)
258    
259            # whether the user is currently dragging the mouse, i.e. moving
260            # the mouse while pressing a mouse button
261          self.dragging = 0          self.dragging = 0
262    
263            # the currently active tool
264          self.tool = None          self.tool = None
265    
266            # The current mouse position of the last OnMotion event or None
267            # if the mouse is outside the window.
268            self.current_position = None
269    
270            # If true, OnIdle will call do_redraw to do the actual
271            # redrawing. Set by OnPaint to avoid some unnecessary redraws.
272            # To force a redraw call full_redraw().
273          self.redraw_on_idle = 0          self.redraw_on_idle = 0
274    
275            # The region to update when idle
276            self.update_region = wx.wxRegion()
277    
278            # the bitmap serving as backing store
279            self.bitmap = None
280    
281            # the interactor
282            self.interactor = interactor
283            self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)
284    
285            # subscribe the WX events we're interested in
286          EVT_PAINT(self, self.OnPaint)          EVT_PAINT(self, self.OnPaint)
287          EVT_LEFT_DOWN(self, self.OnLeftDown)          EVT_LEFT_DOWN(self, self.OnLeftDown)
288          EVT_LEFT_UP(self, self.OnLeftUp)          EVT_LEFT_UP(self, self.OnLeftUp)
289          EVT_MOTION(self, self.OnMotion)          EVT_MOTION(self, self.OnMotion)
290            EVT_LEAVE_WINDOW(self, self.OnLeaveWindow)
291            wx.EVT_SIZE(self, self.OnSize)
292          wx.EVT_IDLE(self, self.OnIdle)          wx.EVT_IDLE(self, self.OnIdle)
293          import main  
294          self.interactor = main.app.interactor      def __del__(self):
295          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)          wxWindow.__del__(self)
296            Publisher.__del__(self)
297    
298      def OnPaint(self, event):      def OnPaint(self, event):
299          dc = wxPaintDC(self)          dc = wxPaintDC(self)
300          if self.map is None or not self.map.HasLayers():          if self.map is not None and self.map.HasLayers():
301              return              # We have a non-empty map. Redraw it in idle time
302          self.redraw_on_idle = 1              self.redraw_on_idle = 1
303                # update the region that has to be redrawn
304                self.update_region.UnionRegion(self.GetUpdateRegion())
305            else:
306                # If we've got no map or if the map is empty, simply clear
307                # the screen.
308                
309                # XXX it's probably possible to get rid of this. The
310                # background color of the window is already white and the
311                # only thing we may have to do is to call self.Refresh()
312                # with a true argument in the right places.
313                dc.BeginDrawing()
314                dc.Clear()            
315                dc.EndDrawing()
316    
317                # clear the region
318                self.update_region = wx.wxRegion()
319    
320      def do_redraw(self):      def do_redraw(self):
321          width, height = self.GetSizeTuple()          # This should only be called if we have a non-empty map.
         bitmap = wx.wxEmptyBitmap(width, height)  
322    
323          dc = wx.wxMemoryDC()          # get the update region and reset it.
324          dc.SelectObject(bitmap)          update_box = self.update_region.GetBox()
325            self.update_region = wx.wxRegion()
326    
327          dc.BeginDrawing()          # Get the window size.
328            width, height = self.GetSizeTuple()
329    
330          dc.SetBrush(wx.wxWHITE_BRUSH)          # If self.bitmap's still there, reuse it. Otherwise redraw it
331          dc.SetPen(wx.wxTRANSPARENT_PEN)          if self.bitmap is not None:
332          dc.DrawRectangle(0, 0, width, height)              bitmap = self.bitmap
   
         if 1: #self.interactor.selected_map is self.map:  
             selected_layer = self.interactor.selected_layer  
             selected_shape = self.interactor.selected_shape  
333          else:          else:
334              selected_layer = None              bitmap = wx.wxEmptyBitmap(width, height)
335              selected_shape = None              dc = wx.wxMemoryDC()
336                            dc.SelectObject(bitmap)
337          renderer = ScreenRenderer(dc, self.scale, self.offset)              dc.BeginDrawing()
338          renderer.RenderMap(self.map, selected_layer, selected_shape)  
339                # clear the background
340                dc.SetBrush(wx.wxWHITE_BRUSH)
341                dc.SetPen(wx.wxTRANSPARENT_PEN)
342                dc.DrawRectangle(0, 0, width, height)
343    
344                if 1: #self.interactor.selected_map is self.map:
345                    selected_layer = self.interactor.selected_layer
346                    selected_shape = self.interactor.selected_shape
347                else:
348                    selected_layer = None
349                    selected_shape = None
350    
351                # draw the map into the bitmap
352                renderer = ScreenRenderer(dc, self.scale, self.offset)
353                renderer.RenderMap(self.map, update_box,
354                                   selected_layer, selected_shape)
355    
356                dc.EndDrawing()
357                dc.SelectObject(wx.wxNullBitmap)
358                self.bitmap = bitmap
359    
360            # blit the bitmap to the screen
361            dc = wx.wxMemoryDC()
362            dc.SelectObject(bitmap)
363          clientdc = wxClientDC(self)          clientdc = wxClientDC(self)
364          clientdc.BeginDrawing()          clientdc.BeginDrawing()
365          clientdc.Blit(0, 0, width, height, dc, 0, 0)          clientdc.Blit(0, 0, width, height, dc, 0, 0)
366          clientdc.EndDrawing()          clientdc.EndDrawing()
367    
   
368      def Print(self):      def Print(self):
369          printer = wx.wxPrinter()          printer = wx.wxPrinter()
370          printout = MapPrintout(self.map)          printout = MapPrintout(self.map)
# Line 296  class MapCanvas(wxWindow): Line 376  class MapCanvas(wxWindow):
376                             LAYER_VISIBILITY_CHANGED)                             LAYER_VISIBILITY_CHANGED)
377          if self.map is not None:          if self.map is not None:
378              for channel in redraw_channels:              for channel in redraw_channels:
379                  self.map.Unsubscribe(channel, self.redraw)                  self.map.Unsubscribe(channel, self.full_redraw)
380              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,
381                                   self.projection_changed)                                   self.projection_changed)
382          self.map = map          self.map = map
383          if self.map is not None:          if self.map is not None:
384              for channel in redraw_channels:              for channel in redraw_channels:
385                  self.map.Subscribe(channel, self.redraw)                  self.map.Subscribe(channel, self.full_redraw)
386              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)
387          self.FitMapToWindow()          self.FitMapToWindow()
388            # force a redraw. If map is not empty, it's already been called
389            # by FitMapToWindow but if map is empty it hasn't been called
390            # yet so we have to explicitly call it.
391            self.full_redraw()
392    
393      def Map(self):      def Map(self):
394          return self.map          return self.map
# Line 312  class MapCanvas(wxWindow): Line 396  class MapCanvas(wxWindow):
396      def redraw(self, *args):      def redraw(self, *args):
397          self.Refresh(0)          self.Refresh(0)
398    
399        def full_redraw(self, *args):
400            self.bitmap = None
401            self.redraw()
402    
403      def projection_changed(self, *args):      def projection_changed(self, *args):
404          self.FitMapToWindow()          self.FitMapToWindow()
405          self.redraw()          self.full_redraw()
406    
407      def set_view_transform(self, scale, offset):      def set_view_transform(self, scale, offset):
408          self.scale = scale          self.scale = scale
409          self.offset = offset          self.offset = offset
410          self.redraw()          self.full_redraw()
411    
412      def proj_to_win(self, x, y):      def proj_to_win(self, x, y):
413          """\          """\
# Line 338  class MapCanvas(wxWindow): Line 426  class MapCanvas(wxWindow):
426      def FitRectToWindow(self, rect):      def FitRectToWindow(self, rect):
427          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
428          llx, lly, urx, ury = rect          llx, lly, urx, ury = rect
429            if llx == urx or lly == ury:
430                # zero with or zero height. Do Nothing
431                return
432          scalex = width / (urx - llx)          scalex = width / (urx - llx)
433          scaley = height / (ury - lly)          scaley = height / (ury - lly)
434          scale = min(scalex, scaley)          scale = min(scalex, scaley)
# Line 354  class MapCanvas(wxWindow): Line 445  class MapCanvas(wxWindow):
445          if bbox is not None:          if bbox is not None:
446              self.FitRectToWindow(bbox)              self.FitRectToWindow(bbox)
447    
448      def ZoomFactor(self, factor):      def ZoomFactor(self, factor, center = None):
449            """Multiply the zoom by factor and center on center.
450    
451            The optional parameter center is a point in window coordinates
452            that should be centered. If it is omitted, it defaults to the
453            center of the window
454            """
455          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
456          scale = self.scale * factor          scale = self.scale * factor
457          offx, offy = self.offset          offx, offy = self.offset
458          offset = (factor * (offx - width / 2) + width / 2,          if center is not None:
459                    factor * (offy - height / 2) + height / 2)              cx, cy = center
460            else:
461                cx = width / 2
462                cy = height / 2
463            offset = (factor * (offx - cx) + width / 2,
464                      factor * (offy - cy) + height / 2)
465          self.set_view_transform(scale, offset)          self.set_view_transform(scale, offset)
466    
467      def ZoomOutToRect(self, rect):      def ZoomOutToRect(self, rect):
# Line 402  class MapCanvas(wxWindow): Line 504  class MapCanvas(wxWindow):
504      def CurrentTool(self):      def CurrentTool(self):
505          return self.tool and self.tool.Name() or None          return self.tool and self.tool.Name() or None
506    
507        def CurrentPosition(self):
508            """Return current position of the mouse in projected coordinates.
509    
510            The result is a 2-tuple of floats with the coordinates. If the
511            mouse is not in the window, the result is None.
512            """
513            if self.current_position is not None:
514                x, y = self.current_position
515                return self.win_to_proj(x, y)
516            else:
517                return None
518    
519        def set_current_position(self, event):
520            """Set the current position from event
521    
522            Should be called by all events that contain mouse positions
523            especially EVT_MOTION. The event paramete may be None to
524            indicate the the pointer left the window.
525            """
526            if event is not None:
527                self.current_position = (event.m_x, event.m_y)
528            else:
529                self.current_position = None
530            self.issue(VIEW_POSITION)
531    
532      def OnLeftDown(self, event):      def OnLeftDown(self, event):
533            self.set_current_position(event)
534          if self.tool is not None:          if self.tool is not None:
535              self.drag_dc = wxClientDC(self)              self.drag_dc = wxClientDC(self)
536              self.drag_dc.SetLogicalFunction(wxINVERT)              self.drag_dc.SetLogicalFunction(wxINVERT)
# Line 414  class MapCanvas(wxWindow): Line 542  class MapCanvas(wxWindow):
542                    
543      def OnLeftUp(self, event):      def OnLeftUp(self, event):
544          self.ReleaseMouse()          self.ReleaseMouse()
545            self.set_current_position(event)
546          if self.dragging:          if self.dragging:
547              self.tool.Hide(self.drag_dc)              self.tool.Hide(self.drag_dc)
548              self.tool.MouseUp(event)              self.tool.MouseUp(event)
# Line 421  class MapCanvas(wxWindow): Line 550  class MapCanvas(wxWindow):
550          self.dragging = 0          self.dragging = 0
551    
552      def OnMotion(self, event):      def OnMotion(self, event):
553            self.set_current_position(event)
554          if self.dragging:          if self.dragging:
555              self.tool.Hide(self.drag_dc)              self.tool.Hide(self.drag_dc)
556              self.tool.MouseMove(event)              self.tool.MouseMove(event)
557              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
558    
559        def OnLeaveWindow(self, event):
560            self.set_current_position(None)
561    
562      def OnIdle(self, event):      def OnIdle(self, event):
563          if self.redraw_on_idle:          if self.redraw_on_idle:
564              self.do_redraw()              self.do_redraw()
565          self.redraw_on_idle = 0          self.redraw_on_idle = 0
566    
567        def OnSize(self, event):
568            # the window's size has changed. We have to get a new bitmap. If
569            # we want to be clever we could try to get by without throwing
570            # everything away. E.g. when the window gets smaller, we could
571            # either keep the bitmap or create the new one from the old one.
572            # Even when the window becomes larger some parts of the bitmap
573            # could be reused.
574            self.full_redraw()
575    
576      def shape_selected(self, layer, shape):      def shape_selected(self, layer, shape):
577          self.redraw()          self.full_redraw()
578    
579        def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):
580            """Determine the shape at point px, py in window coords
581    
582            Return the shape and the corresponding layer as a tuple (layer,
583            shape).
584    
585      def find_shape_at(self, px, py, select_labels = 0):          If the optional parameter select_labels is true (default false)
586          """Return a tuple shape at point px, py in window coords."""          search through the labels. If a label is found return it's index
587            as the shape and None as the layer.
588    
589            If the optional parameter selected_layer is true (default), only
590            search in the currently selected layer.
591            """
592          map_proj = self.map.projection          map_proj = self.map.projection
593          if map_proj is not None:          if map_proj is not None:
594              forward = map_proj.Forward              forward = map_proj.Forward
# Line 452  class MapCanvas(wxWindow): Line 605  class MapCanvas(wxWindow):
605                  dc = wxClientDC(self)                  dc = wxClientDC(self)
606                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
607                  dc.SetFont(font)                  dc.SetFont(font)
608                  for i in range(len(labels)):                  for i in range(len(labels) - 1, -1, -1):
609                      label = labels[i]                      label = labels[i]
610                      x = label.x                      x = label.x
611                      y = label.y                      y = label.y
# Line 478  class MapCanvas(wxWindow): Line 631  class MapCanvas(wxWindow):
631                          y = y - height/2                          y = y - height/2
632                      if x <= px < x + width and y <= py <= y + height:                      if x <= px < x + width and y <= py <= y + height:
633                          return None, i                          return None, i
634                    
635          layers = self.map.Layers()          if selected_layer:
636                layer = self.interactor.SelectedLayer()
637                if layer is not None:
638                    layers = [layer]
639                else:
640                    # no layer selected. Use an empty list to effectively
641                    # ignore all layers.
642                    layers = []
643            else:
644                layers = self.map.Layers()
645    
646          for layer_index in range(len(layers) - 1, -1, -1):          for layer_index in range(len(layers) - 1, -1, -1):
647              layer = layers[layer_index]              layer = layers[layer_index]
648    
# Line 500  class MapCanvas(wxWindow): Line 663  class MapCanvas(wxWindow):
663    
664              select_shape = -1              select_shape = -1
665              if shapetype == SHAPETYPE_POLYGON:              if shapetype == SHAPETYPE_POLYGON:
666                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
667                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      result = point_in_polygon_shape(layer.shapefile.cobject(),
668                                                      i,                                                      i,
669                                                      filled, stroked,                                                      filled, stroked,
# Line 511  class MapCanvas(wxWindow): Line 674  class MapCanvas(wxWindow):
674                          select_shape = i                          select_shape = i
675                          break                          break
676              elif shapetype == SHAPETYPE_ARC:              elif shapetype == SHAPETYPE_ARC:
677                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
678                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      result = point_in_polygon_shape(layer.shapefile.cobject(),
679                                                      i, 0, 1,                                                      i, 0, 1,
680                                                      map_proj, layer_proj,                                                      map_proj, layer_proj,
# Line 521  class MapCanvas(wxWindow): Line 684  class MapCanvas(wxWindow):
684                          select_shape = i                          select_shape = i
685                          break                          break
686              elif shapetype == SHAPETYPE_POINT:              elif shapetype == SHAPETYPE_POINT:
687                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
688                      shape = layer.Shape(i)                      shape = layer.Shape(i)
689                      x, y = shape.Points()[0]                      x, y = shape.Points()[0]
690                      if inverse:                      if inverse:
# Line 539  class MapCanvas(wxWindow): Line 702  class MapCanvas(wxWindow):
702          return None, None          return None, None
703    
704      def SelectShapeAt(self, x, y):      def SelectShapeAt(self, x, y):
705          layer, shape = self.find_shape_at(x, y)          layer, shape = self.find_shape_at(x, y, selected_layer = 0)
706            # If layer is None, then shape will also be None. We don't want
707            # to deselect the currently selected layer, so we simply select
708            # the already selected layer again.
709            if layer is None:
710                layer = self.interactor.SelectedLayer()
711          self.interactor.SelectLayerAndShape(layer, shape)          self.interactor.SelectLayerAndShape(layer, shape)
712    
713      def LabelShapeAt(self, x, y):      def LabelShapeAt(self, x, y):

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26