/[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 57 by bh, Thu Sep 13 13:55:33 2001 UTC revision 149 by bh, Tue May 7 16:41:07 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 240  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, interactor):      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          self.interactor = interactor  
294          self.interactor.Subscribe(SELECTED_SHAPE, self.shape_selected)      def __del__(self):
295            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 not None and self.map.HasLayers():          if self.map is not None and self.map.HasLayers():
301              # We have a non-empty map. Redraw it in idle time              # 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:          else:
306              # 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
307              # the screen.              # the screen.
# Line 278  class MapCanvas(wxWindow): Line 314  class MapCanvas(wxWindow):
314              dc.Clear()                          dc.Clear()            
315              dc.EndDrawing()              dc.EndDrawing()
316    
317                # clear the region
318                self.update_region = wx.wxRegion()
319    
320      def do_redraw(self):      def do_redraw(self):
321          # This should only be called if we have a non-empty map. We draw          # This should only be called if we have a non-empty map.
322          # it into a memory DC and then blit it to the screen.  
323            # get the update region and reset it. We're not actually using
324            # it anymore, though.
325            update_box = self.update_region.GetBox()
326            self.update_region = wx.wxRegion()
327    
328            # Get the window size.
329          width, height = self.GetSizeTuple()          width, height = self.GetSizeTuple()
         bitmap = wx.wxEmptyBitmap(width, height)  
         dc = wx.wxMemoryDC()  
         dc.SelectObject(bitmap)  
         dc.BeginDrawing()  
330    
331          # clear the background          # If self.bitmap's still there, reuse it. Otherwise redraw it
332          dc.SetBrush(wx.wxWHITE_BRUSH)          if self.bitmap is not None:
333          dc.SetPen(wx.wxTRANSPARENT_PEN)              bitmap = self.bitmap
         dc.DrawRectangle(0, 0, width, height)  
   
         if 1: #self.interactor.selected_map is self.map:  
             selected_layer = self.interactor.selected_layer  
             selected_shape = self.interactor.selected_shape  
334          else:          else:
335              selected_layer = None              bitmap = wx.wxEmptyBitmap(width, height)
336              selected_shape = None              dc = wx.wxMemoryDC()
337                dc.SelectObject(bitmap)
338                dc.BeginDrawing()
339    
340          # draw the map into the bitmap              # clear the background
341          renderer = ScreenRenderer(dc, self.scale, self.offset)              dc.SetBrush(wx.wxWHITE_BRUSH)
342          renderer.RenderMap(self.map, selected_layer, selected_shape)              dc.SetPen(wx.wxTRANSPARENT_PEN)
343                dc.DrawRectangle(0, 0, width, height)
344    
345                if 1: #self.interactor.selected_map is self.map:
346                    selected_layer = self.interactor.selected_layer
347                    selected_shape = self.interactor.selected_shape
348                else:
349                    selected_layer = None
350                    selected_shape = None
351    
352          dc.EndDrawing()              # draw the map into the bitmap
353                renderer = ScreenRenderer(dc, self.scale, self.offset)
354    
355                # Pass the entire bitmap as update_region to the renderer.
356                # We're redrawing the whole bitmap, after all.
357                renderer.RenderMap(self.map, (0, 0, width, height),
358                                   selected_layer, selected_shape)
359    
360                dc.EndDrawing()
361                dc.SelectObject(wx.wxNullBitmap)
362                self.bitmap = bitmap
363    
364          # blit the bitmap to the screen          # blit the bitmap to the screen
365            dc = wx.wxMemoryDC()
366            dc.SelectObject(bitmap)
367          clientdc = wxClientDC(self)          clientdc = wxClientDC(self)
368          clientdc.BeginDrawing()          clientdc.BeginDrawing()
369          clientdc.Blit(0, 0, width, height, dc, 0, 0)          clientdc.Blit(0, 0, width, height, dc, 0, 0)
# Line 322  class MapCanvas(wxWindow): Line 380  class MapCanvas(wxWindow):
380                             LAYER_VISIBILITY_CHANGED)                             LAYER_VISIBILITY_CHANGED)
381          if self.map is not None:          if self.map is not None:
382              for channel in redraw_channels:              for channel in redraw_channels:
383                  self.map.Unsubscribe(channel, self.redraw)                  self.map.Unsubscribe(channel, self.full_redraw)
384              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,              self.map.Unsubscribe(MAP_PROJECTION_CHANGED,
385                                   self.projection_changed)                                   self.projection_changed)
386          self.map = map          self.map = map
387          if self.map is not None:          if self.map is not None:
388              for channel in redraw_channels:              for channel in redraw_channels:
389                  self.map.Subscribe(channel, self.redraw)                  self.map.Subscribe(channel, self.full_redraw)
390              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)              self.map.Subscribe(MAP_PROJECTION_CHANGED, self.projection_changed)
391          self.FitMapToWindow()          self.FitMapToWindow()
392          # force a redraw. If map is not empty, it's already been called          # force a redraw. If map is not empty, it's already been called
393          # by FitMapToWindow but if map is empty it hasn't been called          # by FitMapToWindow but if map is empty it hasn't been called
394          # yet so we have to explicitly call it.          # yet so we have to explicitly call it.
395          self.redraw()          self.full_redraw()
396    
397      def Map(self):      def Map(self):
398          return self.map          return self.map
# Line 342  class MapCanvas(wxWindow): Line 400  class MapCanvas(wxWindow):
400      def redraw(self, *args):      def redraw(self, *args):
401          self.Refresh(0)          self.Refresh(0)
402    
403        def full_redraw(self, *args):
404            self.bitmap = None
405            self.redraw()
406    
407      def projection_changed(self, *args):      def projection_changed(self, *args):
408          self.FitMapToWindow()          self.FitMapToWindow()
409          self.redraw()          self.full_redraw()
410    
411      def set_view_transform(self, scale, offset):      def set_view_transform(self, scale, offset):
412          self.scale = scale          self.scale = scale
413          self.offset = offset          self.offset = offset
414          self.redraw()          self.full_redraw()
415    
416      def proj_to_win(self, x, y):      def proj_to_win(self, x, y):
417          """\          """\
# Line 446  class MapCanvas(wxWindow): Line 508  class MapCanvas(wxWindow):
508      def CurrentTool(self):      def CurrentTool(self):
509          return self.tool and self.tool.Name() or None          return self.tool and self.tool.Name() or None
510    
511        def CurrentPosition(self):
512            """Return current position of the mouse in projected coordinates.
513    
514            The result is a 2-tuple of floats with the coordinates. If the
515            mouse is not in the window, the result is None.
516            """
517            if self.current_position is not None:
518                x, y = self.current_position
519                return self.win_to_proj(x, y)
520            else:
521                return None
522    
523        def set_current_position(self, event):
524            """Set the current position from event
525    
526            Should be called by all events that contain mouse positions
527            especially EVT_MOTION. The event paramete may be None to
528            indicate the the pointer left the window.
529            """
530            if event is not None:
531                self.current_position = (event.m_x, event.m_y)
532            else:
533                self.current_position = None
534            self.issue(VIEW_POSITION)
535    
536      def OnLeftDown(self, event):      def OnLeftDown(self, event):
537            self.set_current_position(event)
538          if self.tool is not None:          if self.tool is not None:
539              self.drag_dc = wxClientDC(self)              self.drag_dc = wxClientDC(self)
540              self.drag_dc.SetLogicalFunction(wxINVERT)              self.drag_dc.SetLogicalFunction(wxINVERT)
# Line 458  class MapCanvas(wxWindow): Line 546  class MapCanvas(wxWindow):
546                    
547      def OnLeftUp(self, event):      def OnLeftUp(self, event):
548          self.ReleaseMouse()          self.ReleaseMouse()
549            self.set_current_position(event)
550          if self.dragging:          if self.dragging:
551              self.tool.Hide(self.drag_dc)              self.tool.Hide(self.drag_dc)
552              self.tool.MouseUp(event)              self.tool.MouseUp(event)
# Line 465  class MapCanvas(wxWindow): Line 554  class MapCanvas(wxWindow):
554          self.dragging = 0          self.dragging = 0
555    
556      def OnMotion(self, event):      def OnMotion(self, event):
557            self.set_current_position(event)
558          if self.dragging:          if self.dragging:
559              self.tool.Hide(self.drag_dc)              self.tool.Hide(self.drag_dc)
560              self.tool.MouseMove(event)              self.tool.MouseMove(event)
561              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
562    
563        def OnLeaveWindow(self, event):
564            self.set_current_position(None)
565    
566      def OnIdle(self, event):      def OnIdle(self, event):
567          if self.redraw_on_idle:          if self.redraw_on_idle:
568              self.do_redraw()              self.do_redraw()
569          self.redraw_on_idle = 0          self.redraw_on_idle = 0
570    
571        def OnSize(self, event):
572            # the window's size has changed. We have to get a new bitmap. If
573            # we want to be clever we could try to get by without throwing
574            # everything away. E.g. when the window gets smaller, we could
575            # either keep the bitmap or create the new one from the old one.
576            # Even when the window becomes larger some parts of the bitmap
577            # could be reused.
578            self.full_redraw()
579    
580      def shape_selected(self, layer, shape):      def shape_selected(self, layer, shape):
581          self.redraw()          self.full_redraw()
582    
583      def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):      def find_shape_at(self, px, py, select_labels = 0, selected_layer = 1):
584          """Determine the shape at point px, py in window coords          """Determine the shape at point px, py in window coords
# Line 507  class MapCanvas(wxWindow): Line 609  class MapCanvas(wxWindow):
609                  dc = wxClientDC(self)                  dc = wxClientDC(self)
610                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)                  font = wxFont(10, wx.wxSWISS, wx.wxNORMAL, wx.wxNORMAL)
611                  dc.SetFont(font)                  dc.SetFont(font)
612                  for i in range(len(labels)):                  for i in range(len(labels) - 1, -1, -1):
613                      label = labels[i]                      label = labels[i]
614                      x = label.x                      x = label.x
615                      y = label.y                      y = label.y
# Line 565  class MapCanvas(wxWindow): Line 667  class MapCanvas(wxWindow):
667    
668              select_shape = -1              select_shape = -1
669              if shapetype == SHAPETYPE_POLYGON:              if shapetype == SHAPETYPE_POLYGON:
670                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
671                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      result = point_in_polygon_shape(layer.shapefile.cobject(),
672                                                      i,                                                      i,
673                                                      filled, stroked,                                                      filled, stroked,
# Line 576  class MapCanvas(wxWindow): Line 678  class MapCanvas(wxWindow):
678                          select_shape = i                          select_shape = i
679                          break                          break
680              elif shapetype == SHAPETYPE_ARC:              elif shapetype == SHAPETYPE_ARC:
681                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
682                      result = point_in_polygon_shape(layer.shapefile.cobject(),                      result = point_in_polygon_shape(layer.shapefile.cobject(),
683                                                      i, 0, 1,                                                      i, 0, 1,
684                                                      map_proj, layer_proj,                                                      map_proj, layer_proj,
# Line 586  class MapCanvas(wxWindow): Line 688  class MapCanvas(wxWindow):
688                          select_shape = i                          select_shape = i
689                          break                          break
690              elif shapetype == SHAPETYPE_POINT:              elif shapetype == SHAPETYPE_POINT:
691                  for i in range(layer.NumShapes()):                  for i in range(layer.NumShapes() - 1, -1, -1):
692                      shape = layer.Shape(i)                      shape = layer.Shape(i)
693                      x, y = shape.Points()[0]                      x, y = shape.Points()[0]
694                      if inverse:                      if inverse:
# Line 604  class MapCanvas(wxWindow): Line 706  class MapCanvas(wxWindow):
706          return None, None          return None, None
707    
708      def SelectShapeAt(self, x, y):      def SelectShapeAt(self, x, y):
709          layer, shape = self.find_shape_at(x, y)          layer, shape = self.find_shape_at(x, y, selected_layer = 0)
710          # 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
711          # to deselect the currently selected layer, so we simply select          # to deselect the currently selected layer, so we simply select
712          # the already selected layer again.          # the already selected layer again.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26