/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/renderer.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/UI/renderer.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 442 by jonathan, Thu Feb 27 15:55:25 2003 UTC revision 882 by jonathan, Fri May 9 16:34:28 2003 UTC
# Line 8  Line 8 
8    
9  __version__ = "$Revision$"  __version__ = "$Revision$"
10    
11    from Thuban import _
12    
13  from wxPython.wx import wxPoint, wxPen, wxBrush, wxFont, \  from wxPython.wx import wxPoint, wxPen, wxBrush, wxFont, \
14       wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \       wxTRANSPARENT_PEN, wxTRANSPARENT_BRUSH, \
15       wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL       wxBLACK, wxSOLID, wxCROSS_HATCH, wxSWISS, wxNORMAL
16    
17  from wxproj import draw_polygon_shape  from wxproj import draw_polygon_shape, draw_polygon_init
18    
19  from Thuban import _  from Thuban.UI.common import Color2wxColour
 from Thuban.UI.common import *  
20    
21  from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \  from Thuban.Model.layer import SHAPETYPE_POLYGON, SHAPETYPE_ARC, \
22       SHAPETYPE_POINT       SHAPETYPE_POINT
# Line 25  from Thuban.Model.label import ALIGN_CEN Line 26  from Thuban.Model.label import ALIGN_CEN
26  from Thuban.Model.classification import Classification  from Thuban.Model.classification import Classification
27  from Thuban.Model.color import Color  from Thuban.Model.color import Color
28    
   
29  class MapRenderer:  class MapRenderer:
30    
31      """Class to render a map onto a wxDC"""      """Class to render a map onto a wxDC"""
# Line 48  class MapRenderer: Line 48  class MapRenderer:
48                  the renderer's default.                  the renderer's default.
49          """          """
50          # resolution in pixel/inch          # resolution in pixel/inch
51    
52          self.dc = dc          self.dc = dc
53          self.scale = scale          self.scale = scale
54          self.offset = offset          self.offset = offset
# Line 79  class MapRenderer: Line 80  class MapRenderer:
80          pen   = wxTRANSPARENT_PEN          pen   = wxTRANSPARENT_PEN
81    
82          old_prop = None          old_prop = None
83            old_group = None
84            lc = layer.GetClassification()
85            field = lc.GetField()
86            defaultGroup = lc.GetDefaultGroup()
87    
88    
89            if shapetype != SHAPETYPE_POINT:
90                polygon_render_param = self.polygon_render_param(layer)
91    
92            if shapetype == SHAPETYPE_POINT:
93                draw_func = lambda i: \
94                       self.draw_point_shape(layer, i)
95            else:
96                draw_func = lambda i: \
97                       self.draw_polygon_shape(polygon_render_param, i, pen, brush)
98                
99          for i in self.layer_ids(layer):          for i in self.layer_ids(layer):
100              value = None  
101              lc = layer.GetClassification()              if field is None:
102              field = lc.field                  group = defaultGroup
103                else:
104              if field is not None:                  record = layer.table.ReadRowAsDict(i)
105                  record = layer.table.read_record(i)                  assert record is not None
106                  if record is not None:                  group = lc.FindGroup(record[field])
107                      value = record[field]  
108    
109              #              if not group.IsVisible():
110              # if the above statements fail 'value' should                  continue
111              # be null, at which point this call will  
             # at least retreive the NullData  
             #  
             prop = lc.GetProperties(value)  
112    
113              # don't recreate new objects if they are the same as before              # don't recreate new objects if they are the same as before
114              if prop != old_prop:              if group is not old_group:
115                  old_prop = prop                  old_group = group
116    
117                  if shapetype == SHAPETYPE_ARC:                  prop = group.GetProperties()
118                      fill = Color.None  
119                  else:                  if prop != old_prop:
120                      fill = prop.GetFill()                      old_prop = prop
121        
122                  if fill is Color.None:                      if shapetype == SHAPETYPE_ARC:
123                      brush = wxTRANSPARENT_BRUSH                          fill = Color.Transparent
124                  else:                      else:
125                      color = Color2wxColour(fill)                          fill = prop.GetFill()
126                      brush = wxBrush(color, wxSOLID)  
127        
128                  stroke = prop.GetStroke()                      if fill is Color.Transparent:
129                  stroke_width = prop.GetStrokeWidth()                          brush = wxTRANSPARENT_BRUSH
130                  if stroke is Color.None:                      else:
131                      pen = wxTRANSPARENT_PEN                          color = Color2wxColour(fill)
132                  else:                          brush = wxBrush(color, wxSOLID)
133                      color = Color2wxColour(stroke)  
134                      pen = wxPen(color, stroke_width, wxSOLID)                      stroke = prop.GetLineColor()
135                            stroke_width = prop.GetLineWidth()
136              if shapetype == SHAPETYPE_POINT:                      if stroke is Color.Transparent:
137                  self.dc.SetBrush(brush)                          pen = wxTRANSPARENT_PEN
138                  self.dc.SetPen(pen)                      else:
139                  self.draw_point_shape(layer, i)                          color = Color2wxColour(stroke)
140              else:                          pen = wxPen(color, stroke_width, wxSOLID)
141                  self.draw_polygon_shape(layer, i, pen, brush)  
142                        if shapetype == SHAPETYPE_POINT:
143                            self.dc.SetBrush(brush)
144                            self.dc.SetPen(pen)
145    
146                draw_func(i)
147    
148      def layer_ids(self, layer):      def layer_ids(self, layer):
149          """Return the shape ids of the given layer that have to be drawn.          """Return the shape ids of the given layer that have to be drawn.
150            
151          The default implementation simply returns all ids in the layer.          The default implementation simply returns all ids in the layer.
152          Override in derived classes to be more precise.          Override in derived classes to be more precise.
153          """          """
154          return range(layer.NumShapes())          return range(layer.NumShapes())
155    
156      def draw_polygon_shape(self, layer, index, pen, brush):      def polygon_render_param(self, layer):
157          offx, offy = self.offset                  """Return the low-lever render parameter for the layer"""
158          draw_polygon_shape(layer.shapefile.cobject(), index,          offx, offy = self.offset
159                             self.dc, pen, brush,          return draw_polygon_init(layer.shapefile, self.dc,
160                             self.map.projection, layer.projection,                                   self.map.projection,
161                             self.scale, -self.scale, offx, offy)                                   layer.projection,
162                                     self.scale, -self.scale,
163                                     offx, offy)
164    
165        def draw_polygon_shape(self, draw_polygon_info, index, pen, brush):
166            draw_polygon_shape(draw_polygon_info, index, pen, brush)
167    
168      def projected_points(self, layer, index):      def projected_points(self, layer, index):
169          proj = self.map.projection          proj = self.map.GetProjection()
170          if proj is not None:          if proj is not None:
171              forward = proj.Forward              forward = proj.Forward
172          else:          else:
173              forward = None              forward = None
174          proj = layer.projection          proj = layer.GetProjection()
175          if proj is not None:          if proj is not None:
176              inverse = proj.Inverse              inverse = proj.Inverse
177          else:          else:
# Line 170  class MapRenderer: Line 194  class MapRenderer:
194          self.dc.DrawLines(points)          self.dc.DrawLines(points)
195    
196      def draw_point_shape(self, layer, index):      def draw_point_shape(self, layer, index):
197          p = self.projected_points(layer, index)[0]          pp = self.projected_points(layer, index)
198    
199            if len(pp) == 0: return # ignore Null Shapes which have no points
200    
201            p = pp[0]
202          radius = self.resolution * 5          radius = self.resolution * 5
203          self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)          self.dc.DrawEllipse(p.x - radius, p.y - radius, 2*radius, 2*radius)
204    
# Line 218  class ScreenRenderer(MapRenderer): Line 246  class ScreenRenderer(MapRenderer):
246      # On the screen we want to see only visible layers by default      # On the screen we want to see only visible layers by default
247      honor_visibility = 1      honor_visibility = 1
248            
249      def RenderMap(self, map, region, selected_layer, selected_shape):      def RenderMap(self, map, region, selected_layer, selected_shapes):
250          """Render the map.          """Render the map.
251    
252          Only the given region (a tuple in window coordinates as returned          Only the given region (a tuple in window coordinates as returned
253          by a wxrect's asTuple method) needs to be redrawn. Highlight the          by a wxrect's asTuple method) needs to be redrawn. Highlight the
254          shape with id selected_shape in the selected_layer.          shapes given by the ids in selected_shapes in the
255            selected_layer.
256          """          """
257          self.update_region = region          self.update_region = region
258          self.selected_layer = selected_layer          self.selected_layer = selected_layer
259          self.selected_shape = selected_shape          self.selected_shapes = selected_shapes
260          self.render_map(map)          self.render_map(map)
261    
262      def draw_shape_layer(self, layer):      def draw_shape_layer(self, layer):
263          MapRenderer.draw_shape_layer(self, layer)          MapRenderer.draw_shape_layer(self, layer)
264          if layer is self.selected_layer and self.selected_shape is not None:          if layer is self.selected_layer and self.selected_shapes:
265              pen = wxPen(wxBLACK, 3, wxSOLID)              pen = wxPen(wxBLACK, 3, wxSOLID)
266              brush = wxBrush(wxBLACK, wxCROSS_HATCH)              brush = wxBrush(wxBLACK, wxCROSS_HATCH)
267                
268              shapetype = layer.ShapeType()              shapetype = layer.ShapeType()
             index = self.selected_shape  
269              if shapetype == SHAPETYPE_POLYGON:              if shapetype == SHAPETYPE_POLYGON:
270                  self.draw_polygon_shape(layer, index, pen, brush)                  offx, offy = self.offset
271                    renderparam = self.polygon_render_param(layer)
272                    func = self.draw_polygon_shape
273                    args = (pen, brush)
274              elif shapetype == SHAPETYPE_ARC:              elif shapetype == SHAPETYPE_ARC:
275                  self.draw_polygon_shape(layer, index, pen, None)                  renderparam = self.polygon_render_param(layer)
276              else:                  func = self.draw_polygon_shape
277                    args = (pen, None)
278                elif shapetype == SHAPETYPE_POINT:
279                    renderparam = layer
280                  self.dc.SetBrush(brush)                  self.dc.SetBrush(brush)
281                  self.dc.SetPen(pen)                  self.dc.SetPen(pen)
282                  if shapetype == SHAPETYPE_POINT:                  func = self.draw_point_shape
283                      self.draw_point_shape(layer, index)                  args = ()
284                  else:              else:
285                      raise TypeError(_("Unhandled shape type %s") % shapetype)                  raise TypeError(_("Unhandled shape type %s") % shapetype)
286    
287                for index in self.selected_shapes:
288                    func(renderparam, index, *args)
289    
290    
291      def layer_ids(self, layer):      def layer_ids(self, layer):
292          """Return the shapeids covered by the region that has to be redrawn          """Return the shapeids covered by the region that has to be redrawn

Legend:
Removed from v.442  
changed lines
  Added in v.882

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26