/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/classification.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/Model/classification.py

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

revision 873 by jonathan, Fri May 9 16:30:54 2003 UTC revision 1351 by jonathan, Tue Jul 1 16:17:02 2003 UTC
# Line 20  See the description of FindGroup() for m Line 20  See the description of FindGroup() for m
20  on the mapping algorithm.  on the mapping algorithm.
21  """  """
22        
23  # fix for people using python2.1  import copy, operator, types
 from __future__ import nested_scopes  
   
 import copy, operator  
24    
25  from Thuban import _  from Thuban import _
26    
 import types  
   
27  from messages import \  from messages import \
28      LAYER_PROJECTION_CHANGED, \      LAYER_PROJECTION_CHANGED, \
29      LAYER_LEGEND_CHANGED, \      LAYER_LEGEND_CHANGED, \
30      LAYER_VISIBILITY_CHANGED      LAYER_VISIBILITY_CHANGED
31    
32  from Thuban.Model.color import Color  from Thuban.Model.color import Color, Transparent, Black
33  from Thuban.Model.range import Range  from Thuban.Model.range import Range
34    
35  import Thuban.Model.layer  import Thuban.Model.layer
# Line 61  class Classification: Line 56  class Classification:
56          self.fieldType = None          self.fieldType = None
57          self.__groups = []          self.__groups = []
58    
         self.__setLayerLock = False  
   
59          self.SetDefaultGroup(ClassGroupDefault())          self.SetDefaultGroup(ClassGroupDefault())
60    
61          self.SetLayer(layer)          self.SetFieldInfo(field, None)
62          self.SetField(field)  
63            self._set_layer(layer)
64    
65      def __iter__(self):      def __iter__(self):
66          return ClassIterator(self.__groups)          return ClassIterator(self.__groups)
# Line 89  class Classification: Line 83  class Classification:
83          if self.layer is not None:          if self.layer is not None:
84              self.layer.ClassChanged()              self.layer.ClassChanged()
85            
     def SetField(self, field):  
         """Set the name of the data table field to use.  
           
         If there is no layer then the field type is set to None,  
         otherwise the layer is queried to find the type of the  
         field data  
   
         field -- if None then all values map to the default data  
         """  
   
         if field == "":  
             field = None  
   
   
         if field is None:  
             if self.layer is not None:  
                 self.fieldType = None  
         else:  
             if self.layer is not None:  
                 fieldType = self.layer.GetFieldType(field)  
                 if fieldType is None:  
                     raise ValueError("'%s' was not found in the layer's table."  
                                      % self.field)  
   
                 #  
                 # unfortunately we cannot call SetFieldType() because it  
                 # requires the layer to be None  
                 #  
                 self.fieldType = fieldType  
                 #self.SetFieldType(fieldType)  
   
         self.field = field  
   
         self.__SendNotification()  
   
86      def GetField(self):      def GetField(self):
87          """Return the name of the field."""          """Return the name of the field."""
88          return self.field          return self.field
# Line 132  class Classification: Line 91  class Classification:
91          """Return the field type."""          """Return the field type."""
92          return self.fieldType          return self.fieldType
93    
94      def SetFieldType(self, type):      def SetFieldInfo(self, name, type):
95          """Set the type of the field used by this classification.          """Set the classified field name to 'name' and it's field
96            type to 'type'
97    
98            If this classification has an owning layer a ValueError
99            exception will be thrown either the field or field type
100            mismatch the information in the layer's table.
101    
102            If the field info is successful set and the class has
103            an owning layer, the layer will be informed that the
104            classification has changed.
105            """
106    
107            if name == "":
108                name = None
109    
110            if self.layer is None:
111                self.field = name
112                self.fieldType = type
113            elif name is None:
114                self.field = None
115                self.fieldType = None
116            else:
117                #
118                # verify that the field exists in the layer and that
119                # the type is correct.
120                #
121                fieldType = self.layer.GetFieldType(name)
122                if fieldType is None:
123                    raise ValueError("'%s' was not found in the layer's table."
124                                     % self.field)
125                elif type is not None and fieldType != type:
126                    raise ValueError("type doesn't match layer's field type for %s"
127                                     % self.field)
128    
129          A ValueError is raised if the owning layer is not None and              self.field = name
130          'type' is different from the current field type.              self.fieldType = fieldType
         """  
131    
132          if type != self.fieldType:          self.__SendNotification()
             if self.layer is not None:  
                 raise ValueError()  
             else:  
                 self.fieldType = type  
                 self.__SendNotification()  
133    
134      def SetLayer(self, layer):      def _set_layer(self, layer):
135          """Set the owning Layer of this classification.          """Internal: Set the owning Layer of this classification.
136    
137          A ValueError exception will be thrown either the field or          A ValueError exception will be thrown either the field or
138          field type mismatch the information in the layer's table.          field type mismatch the information in the layer's table.
         """  
139    
140          # prevent infinite recursion when calling SetClassification()          If the layer is successful set, the layer will be informed
141          if self.__setLayerLock: return          that the classification has changed.
142            """
         self.__setLayerLock = True  
143    
144          if layer is None:          if layer is None:
145              if self.layer is not None:              self.layer = None
                 l = self.layer  
                 self.layer = None  
                 l.SetClassification(None)  
146          else:          else:
             assert isinstance(layer, Thuban.Model.layer.Layer)  
   
147              old_layer = self.layer              old_layer = self.layer
   
148              self.layer = layer              self.layer = layer
149    
150              try:              try:
151                  self.SetField(self.GetField()) # this sync's the fieldType                  # this sync's the fieldType
152                    # and sends a notification to the layer
153                    self.SetFieldInfo(self.GetField(), None)
154              except ValueError:              except ValueError:
155                  self.layer = old_layer                  self.layer = old_layer
                 self.__setLayerLock = False  
156                  raise ValueError                  raise ValueError
             else:  
                 self.layer.SetClassification(self)  
   
         self.__setLayerLock = False  
157    
158      def GetLayer(self):      def GetLayer(self):
159          """Return the parent layer."""          """Return the parent layer."""
160          return self.layer          return self.layer
161    
   
162      #      #
163      # these SetDefault* methods are really only provided for      # these SetDefault* methods are really only provided for
164      # some backward compatibility. they should be considered      # some backward compatibility. they should be considered
# Line 197  class Classification: Line 170  class Classification:
170    
171          fill -- a Color object.          fill -- a Color object.
172          """          """
         assert isinstance(fill, Color)  
173          self.GetDefaultGroup().GetProperties().SetFill(fill)          self.GetDefaultGroup().GetProperties().SetFill(fill)
174          self.__SendNotification()          self.__SendNotification()
175                    
# Line 210  class Classification: Line 182  class Classification:
182    
183          color -- a Color object.          color -- a Color object.
184          """          """
         assert isinstance(color, Color)  
185          self.GetDefaultGroup().GetProperties().SetLineColor(color)          self.GetDefaultGroup().GetProperties().SetLineColor(color)
186          self.__SendNotification()          self.__SendNotification()
187                    
# Line 328  class Classification: Line 299  class Classification:
299          items = []          items = []
300    
301          def build_color_item(text, color):          def build_color_item(text, color):
302              if color is Color.Transparent:              if color is Transparent:
303                  return ("%s: %s" % (text, _("None")), None)                  return ("%s: %s" % (text, _("None")), None)
304    
305              return ("%s: (%.3f, %.3f, %.3f)" %              return ("%s: (%.3f, %.3f, %.3f)" %
# Line 425  class ClassGroupProperties: Line 396  class ClassGroupProperties:
396    
397          props -- a ClassGroupProperties object. The class is copied if          props -- a ClassGroupProperties object. The class is copied if
398                   prop is not None. Otherwise, a default set of properties                   prop is not None. Otherwise, a default set of properties
399                   is created such that: line color = Color.Black, line width = 1,                   is created such that: line color = Black, line width = 1,
400                   and fill color = Color.Transparent                   and fill color = Transparent
401          """          """
402    
403          #self.stroke = None          #self.stroke = None
# Line 436  class ClassGroupProperties: Line 407  class ClassGroupProperties:
407          if props is not None:          if props is not None:
408              self.SetProperties(props)              self.SetProperties(props)
409          else:          else:
410              self.SetLineColor(Color.Black)              self.SetLineColor(Black)
411              self.SetLineWidth(1)              self.SetLineWidth(1)
412              self.SetFill(Color.Transparent)              self.SetFill(Transparent)
413    
414      def SetProperties(self, props):      def SetProperties(self, props):
415          """Set this class's properties to those in class props."""          """Set this class's properties to those in class props."""
# Line 458  class ClassGroupProperties: Line 429  class ClassGroupProperties:
429          color -- the color of the line. This must be a Color object.          color -- the color of the line. This must be a Color object.
430          """          """
431    
         assert isinstance(color, Color)  
432          self.__stroke = color          self.__stroke = color
433    
434      def GetLineWidth(self):      def GetLineWidth(self):
# Line 486  class ClassGroupProperties: Line 456  class ClassGroupProperties:
456          fill -- the color of the fill. This must be a Color object.          fill -- the color of the fill. This must be a Color object.
457          """          """
458    
         assert isinstance(fill, Color)  
459          self.__fill = fill          self.__fill = fill
460    
461      def __eq__(self, other):      def __eq__(self, other):
# Line 763  class ClassGroupRange(ClassGroup): Line 732  class ClassGroupRange(ClassGroup):
732              if max is None:              if max is None:
733                  raise ValueError()                  raise ValueError()
734    
735              self.__range = Range("[" + repr(float(min)) + ";" +              self.__range = Range(("[", min, max, "["))
                                        repr(float(max)) + "[")  
736    
737      def GetRange(self):      def GetRange(self):
738          """Return the range as a string"""          """Return the range as a string"""

Legend:
Removed from v.873  
changed lines
  Added in v.1351

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26