/[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 1385 by jonathan, Thu Jul 10 14:52:39 2003 UTC revision 1652 by bh, Mon Aug 25 15:59:58 2003 UTC
# Line 14  __version__ = "$Revision$" Line 14  __version__ = "$Revision$"
14    
15  from Thuban import _  from Thuban import _
16    
 import sys  
17  import os.path  import os.path
18    
 from math import hypot  
   
19  from wxPython.wx import wxWindow, \  from wxPython.wx import wxWindow, \
20       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\       wxPaintDC, wxColour, wxClientDC, wxINVERT, wxTRANSPARENT_BRUSH, wxFont,\
21       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, \
22       wxBITMAP_TYPE_XPM, wxCursor, wxPlatform, \       wxPlatform, wxBeginBusyCursor, wxEndBusyCursor, wxFileDialog, wxSAVE, \
      wxBeginBusyCursor, wxEndBusyCursor, wxFileDialog, wxSAVE, \  
23       wxOVERWRITE_PROMPT, wxID_OK       wxOVERWRITE_PROMPT, wxID_OK
24    
25  # Export related stuff  # Export related stuff
# Line 32  if wxPlatform == '__WXMSW__': Line 28  if wxPlatform == '__WXMSW__':
28    
29  from wxPython import wx  from wxPython import wx
30    
31  from wxproj import point_in_polygon_shape, shape_centroid  from Thuban.Model.messages import MAP_LAYERS_CHANGED, LAYER_CHANGED, \
32         LAYER_VISIBILITY_CHANGED
 from Thuban.Model.messages import \  
      MAP_PROJECTION_CHANGED, MAP_LAYERS_CHANGED, \  
      LAYER_PROJECTION_CHANGED, LAYER_CHANGED, LAYER_VISIBILITY_CHANGED  
33    
34  from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer  from renderer import ScreenRenderer, ExportRenderer, PrinterRenderer
35    
36  import labeldialog  import labeldialog
37    
38  from viewport import ViewPort, PanTool  from viewport import ViewPort, PanTool, output_transform
39    
40  class CanvasPanTool(PanTool):  class CanvasPanTool(PanTool):
41    
# Line 87  class MapPrintout(wx.wxPrintout): Line 80  class MapPrintout(wx.wxPrintout):
80    
81      def draw_on_dc(self, dc):      def draw_on_dc(self, dc):
82          width, height = self.GetPageSizePixels()          width, height = self.GetPageSizePixels()
83          scale, offset, mapregion = OutputTransform(self.canvas.scale,          scale, offset, mapregion = output_transform(self.canvas.scale,
84                                                     self.canvas.offset,                                                      self.canvas.offset,
85                                                     self.canvas.GetSizeTuple(),                                                      self.canvas.GetSizeTuple(),
86                                                     self.GetPageSizePixels())                                                      self.GetPageSizePixels())
87          resx, resy = self.GetPPIPrinter()          resx, resy = self.GetPPIPrinter()
88          renderer = PrinterRenderer(dc, scale, offset, resolution = resy)          renderer = PrinterRenderer(dc, scale, offset, resolution = resy)
89          x, y, width, height = self.region          x, y, width, height = self.region
# Line 118  class MapCanvas(wxWindow, ViewPort): Line 111  class MapCanvas(wxWindow, ViewPort):
111    
112          self.backgroundColor = wx.wxWHITE_BRUSH          self.backgroundColor = wx.wxWHITE_BRUSH
113    
114            # Set to true if there ever is an error during redraw. There
115            # should never be errors, but unfortunately bugs happen.
116            self.error_on_redraw = 0
117    
118          # subscribe the WX events we're interested in          # subscribe the WX events we're interested in
119          EVT_PAINT(self, self.OnPaint)          EVT_PAINT(self, self.OnPaint)
120          EVT_LEFT_DOWN(self, self.OnLeftDown)          EVT_LEFT_DOWN(self, self.OnLeftDown)
# Line 180  class MapCanvas(wxWindow, ViewPort): Line 177  class MapCanvas(wxWindow, ViewPort):
177              dc.EndDrawing()              dc.EndDrawing()
178    
179      def OnIdle(self, event):      def OnIdle(self, event):
180          # render the screen if necessary          """Idle handler. Redraw the bitmap if necessary"""
181    
182          if self.bitmap != -1:          if self.bitmap != -1:
183              return              return
184            if self.error_on_redraw:
185                return
186    
187          wxBeginBusyCursor()          wxBeginBusyCursor()
188          try:          try:
189              width, height = self.GetSizeTuple()              try:
190                    self._do_redraw()
191                except:
192                    self.error_on_redraw = True
193                    raise
194            finally:
195                wxEndBusyCursor()
196    
197              bitmap = wx.wxEmptyBitmap(width, height)      def _do_redraw(self):
198              dc = wx.wxMemoryDC()          """Called by OnIdle to do the actual redraw.
199              dc.SelectObject(bitmap)          """
200              dc.BeginDrawing()          width, height = self.GetSizeTuple()
201    
202              dc.SetBackground(self.backgroundColor)          bitmap = wx.wxEmptyBitmap(width, height)
203              dc.Clear()          dc = wx.wxMemoryDC()
204            dc.SelectObject(bitmap)
205            dc.BeginDrawing()
206    
207              selected_layer = self.selection.SelectedLayer()          dc.SetBackground(self.backgroundColor)
208              selected_shapes = self.selection.SelectedShapes()          dc.Clear()
209    
210              # draw the map into the bitmap          selected_layer = self.selection.SelectedLayer()
211              renderer = ScreenRenderer(dc, self.scale, self.offset)          selected_shapes = self.selection.SelectedShapes()
212    
213              # Pass the entire bitmap as update region to the renderer.          # draw the map into the bitmap
214              # We're redrawing the whole bitmap, after all.          renderer = ScreenRenderer(dc, self.scale, self.offset)
             renderer.RenderMap(self.Map(), (0, 0, width, height),  
                                selected_layer, selected_shapes)  
215    
216              dc.EndDrawing()          # Pass the entire bitmap as update region to the renderer.
217              dc.SelectObject(wx.wxNullBitmap)          # We're redrawing the whole bitmap, after all.
218            renderer.RenderMap(self.Map(), (0, 0, width, height),
219                               selected_layer, selected_shapes)
220    
221              self.bitmap = bitmap          dc.EndDrawing()
222          finally:          dc.SelectObject(wx.wxNullBitmap)
             wxEndBusyCursor()  
             pass  
223    
224            self.bitmap = bitmap
225          # This causes a paint event that then draws the bitmap          # This causes a paint event that then draws the bitmap
226          self.redraw()          self.redraw()
227    
# Line 232  class MapCanvas(wxWindow, ViewPort): Line 238  class MapCanvas(wxWindow, ViewPort):
238              self.export_path = os.path.dirname(dlg.GetPath())              self.export_path = os.path.dirname(dlg.GetPath())
239              dc = wxMetaFileDC(dlg.GetPath())              dc = wxMetaFileDC(dlg.GetPath())
240            
241              scale, offset, mapregion = OutputTransform(self.scale,              scale, offset, mapregion = output_transform(self.scale,
242                                                         self.offset,                                                          self.offset,
243                                                         self.GetSizeTuple(),                                                          self.GetSizeTuple(),
244                                                         dc.GetSizeTuple())                                                          dc.GetSizeTuple())
245    
246              selected_layer = self.selection.SelectedLayer()              selected_layer = self.selection.SelectedLayer()
247              selected_shapes = self.selection.SelectedShapes()              selected_shapes = self.selection.SelectedShapes()
# Line 295  class MapCanvas(wxWindow, ViewPort): Line 301  class MapCanvas(wxWindow, ViewPort):
301              self.drag_dc.SetLogicalFunction(wxINVERT)              self.drag_dc.SetLogicalFunction(wxINVERT)
302              self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)              self.drag_dc.SetBrush(wxTRANSPARENT_BRUSH)
303              self.tool.Show(self.drag_dc)              self.tool.Show(self.drag_dc)
304                self.CaptureMouse()
305              self.dragging = 1              self.dragging = 1
306    
307      def OnLeftUp(self, event):      def OnLeftUp(self, event):
308          self.MouseLeftUp(event)          """Handle EVT_LEFT_UP
309    
310            Release the mouse if it was captured, if a tool is active call
311            its Hide method and call self.MouseLeftUp.
312            """
313            # It's important that ReleaseMouse is called before MouseLeftUp.
314            # MouseLeftUp may pop up modal dialogs which leads to an
315            # effectively frozen X session because the user can only
316            # interact with the dialog but the mouse is still grabbed by the
317            # canvas.
318          if self.dragging:          if self.dragging:
319              self.ReleaseMouse()              if self.HasCapture():
320                    self.ReleaseMouse()
321              try:              try:
322                  self.tool.Hide(self.drag_dc)                  self.tool.Hide(self.drag_dc)
323              finally:              finally:
324                  self.drag_dc = None                  self.drag_dc = None
325                  self.dragging = 0                  self.dragging = 0
326            self.MouseLeftUp(event)
327    
328      def OnMotion(self, event):      def OnMotion(self, event):
329          if self.dragging:          if self.dragging:
# Line 347  class MapCanvas(wxWindow, ViewPort): Line 365  class MapCanvas(wxWindow, ViewPort):
365    
366      def LabelShapeAt(self, x, y, text=None):      def LabelShapeAt(self, x, y, text=None):
367          """Add or remove a label at window position x, y.          """Add or remove a label at window position x, y.
368                                                                                    
369          If there's a label at the given position, remove it. Otherwise          If there's a label at the given position, remove it. Otherwise
370          determine the shape at the position, run the label dialog and          determine the shape at the position, run the label dialog and
371          unless the user cancels the dialog, add a label.          unless the user cancels the dialog, add a label.
# Line 361  class MapCanvas(wxWindow, ViewPort): Line 379  class MapCanvas(wxWindow, ViewPort):
379                                                  layer.ShapeStore().Table(),                                                  layer.ShapeStore().Table(),
380                                                  shape_index)                                                  shape_index)
381              ViewPort.LabelShapeAt(self, x, y, text)              ViewPort.LabelShapeAt(self, x, y, text)
           
 def OutputTransform(canvas_scale, canvas_offset, canvas_size, device_extend):  
     """Calculate dimensions to transform canvas content to output device."""  
     width, height = device_extend  
   
     # Only 80 % of the with are available for the map  
     width = width * 0.8  
   
     # Define the distance of the map from DC border  
     distance = 20  
   
     if height < width:  
         # landscape  
         map_height = height - 2*distance  
         map_width = map_height  
     else:  
         # portrait, recalibrate width (usually the legend width is too  
         # small  
         width = width * 0.9  
         map_height = width - 2*distance  
         map_width = map_height  
       
     mapregion = (distance, distance,  
                  distance+map_width, distance+map_height)  
   
     canvas_width, canvas_height = canvas_size  
       
     scalex = map_width / (canvas_width/canvas_scale)  
     scaley = map_height / (canvas_height/canvas_scale)  
     scale = min(scalex, scaley)  
     canvas_offx, canvas_offy = canvas_offset  
     offx = scale*canvas_offx/canvas_scale  
     offy = scale*canvas_offy/canvas_scale  
   
     return scale, (offx, offy), mapregion  

Legend:
Removed from v.1385  
changed lines
  Added in v.1652

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26