/[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 479 by jonathan, Thu Mar 6 16:46:07 2003 UTC revision 491 by jonathan, Mon Mar 10 10:44:42 2003 UTC
# Line 23  on the mapping algorithm. Line 23  on the mapping algorithm.
23  # fix for people using python2.1  # fix for people using python2.1
24  from __future__ import nested_scopes  from __future__ import nested_scopes
25    
26    import copy
27    
28  from types import *  from types import *
29    
30  from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \  from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
# Line 57  class Classification: Line 59  class Classification:
59          self.field = None          self.field = None
60          self.fieldType = None          self.fieldType = None
61          self.groups = []          self.groups = []
         self.__sendMessages = False  
62    
         self.__ToggleMessages(False)  
63          self.SetDefaultGroup(ClassGroupDefault())          self.SetDefaultGroup(ClassGroupDefault())
64    
65          self.SetLayer(layer)          self.SetLayer(layer)
66          self.SetField(field)          self.SetField(field)
67    
         self.__ToggleMessages(True)  
   
68      def __iter__(self):      def __iter__(self):
69          return ClassIterator(self.groups)          return ClassIterator(self.groups)
70    
71      def __ToggleMessages(self, on):      def __SendNotification(self):
72          self.__sendMessages = on          """Notify the layer that this class has changed."""
73            if self.layer is not None:
74      def __SendMessage(self, message):              self.layer.ClassChanged()
         """Send the message 'message' to the parent layer."""  
         if self.__sendMessages and self.layer is not None:  
             self.layer.changed(message, self.layer)  
75            
76      def SetField(self, field = None):      def SetField(self, field):
77          """Set the name of the data table field to use.          """Set the name of the data table field to use.
78                    
79             If there is no layer then the field type is set to None,             If there is no layer then the field type is set to None,
# Line 90  class Classification: Line 86  class Classification:
86          if field == "":          if field == "":
87              field = None              field = None
88    
         self.field = field  
89    
90          if self.layer is not None:          if field is None:
91              fieldType = self.layer.GetFieldType(field)              if self.layer is not None:
92                    self.fieldType = None
93          else:          else:
94              fieldType = None              if self.layer is not None:
95                    fieldType = self.layer.GetFieldType(field)
96          self.SetFieldType(fieldType)                  if fieldType is None:
97                        raise ValueError("'%s' was not found in the layer's table."
98                                         % self.field)
99    
100                    #
101                    # unfortunately we cannot call SetFieldType() because it
102                    # requires the layer to be None
103                    #
104                    self.fieldType = fieldType
105                    #self.SetFieldType(fieldType)
106    
107          # XXX: if fieldType comes back None then field isn't in the table!          self.field = field
108    
109          self.__SendMessage(LAYER_LEGEND_CHANGED)          self.__SendNotification()
110    
111      def GetField(self):      def GetField(self):
112          """Return the name of the field."""          """Return the name of the field."""
# Line 112  class Classification: Line 117  class Classification:
117          return self.fieldType          return self.fieldType
118    
119      def SetFieldType(self, type):      def SetFieldType(self, type):
120          self.fieldType = type          """Set the type of the field used by this classification.
121    
122      def SetLayer(self, layer):          A ValueError is raised if the owning layer is not None and
123          """Set the owning Layer of this classification."""          'type' is different from the current field type.
124            """
125    
126          if __debug__:          if type != self.fieldType:
127              if layer is not None:              if self.layer is not None:
128                  assert(isinstance(layer, Thuban.Model.layer.Layer))                  raise ValueError()
129                else:
130          # prevent infinite recursion when calling SetClassification()                  self.fieldType = type
131          if self.layer is not None and layer == self.layer:                  self.__SendNotification()
             return  
132    
133          self.layer = layer      def SetLayer(self, layer):
134          self.SetField(self.GetField()) # XXX: this sync's the fieldType          """Set the owning Layer of this classification.
135    
136               A ValueError exception will be thrown either the field or
137               field type mismatch the information in the layer's table.
138            """
139    
140          if self.layer is not None:          if layer is None:
141              self.layer.SetClassification(self)              if self.layer is not None:
142                    l = self.layer
143                    self.layer = None
144                    l.SetClassification(None)
145            else:
146                assert(isinstance(layer, Thuban.Model.layer.Layer))
147    
148          #self.__SendMessage(LAYER_LEGEND_CHANGED)              # prevent infinite recursion when calling SetClassification()
149                if layer == self.layer:
150                    return
151                
152                old_layer = self.layer
153    
154                self.layer = layer
155    
156                try:
157                    self.SetField(self.GetField()) # this sync's the fieldType
158                except ValueError:
159                    self.layer = old_layer
160                    raise ValueError
161                else:
162                    self.layer.SetClassification(self)
163    
164      def GetLayer(self):      def GetLayer(self):
165          """Return the parent layer."""          """Return the parent layer."""
# Line 163  class Classification: Line 191  class Classification:
191          """          """
192          assert(isinstance(fill, Color))          assert(isinstance(fill, Color))
193          self.GetDefaultGroup().GetProperties().SetFill(fill)          self.GetDefaultGroup().GetProperties().SetFill(fill)
194          self.__SendMessage(LAYER_LEGEND_CHANGED)          self.__SendNotification()
195                    
196      def GetDefaultFill(self):      def GetDefaultFill(self):
197          """Return the default fill color."""          """Return the default fill color."""
# Line 176  class Classification: Line 204  class Classification:
204          """          """
205          assert(isinstance(color, Color))          assert(isinstance(color, Color))
206          self.GetDefaultGroup().GetProperties().SetLineColor(color)          self.GetDefaultGroup().GetProperties().SetLineColor(color)
207          self.__SendMessage(LAYER_LEGEND_CHANGED)          self.__SendNotification()
208                    
209      def GetDefaultLineColor(self):      def GetDefaultLineColor(self):
210          """Return the default line color."""          """Return the default line color."""
# Line 189  class Classification: Line 217  class Classification:
217          """          """
218          assert(isinstance(lineWidth, IntType))          assert(isinstance(lineWidth, IntType))
219          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)
220          self.__SendMessage(LAYER_LEGEND_CHANGED)          self.__SendNotification()
221                    
222      def GetDefaultLineWidth(self):      def GetDefaultLineWidth(self):
223          """Return the default line width."""          """Return the default line width."""
# Line 205  class Classification: Line 233  class Classification:
233    
234          if len(self.groups) > 0 and isinstance(item, ClassGroupDefault):          if len(self.groups) > 0 and isinstance(item, ClassGroupDefault):
235              self.groups[0] = item              self.groups[0] = item
             #self.SetDefaultGroup(item)  
236          else:          else:
237              self.groups.append(item)              self.groups.append(item)
238    
239          self.__SendMessage(LAYER_LEGEND_CHANGED)          self.__SendNotification()
240    
241      def GetGroup(self, value):      def GetGroup(self, value):
242          """Return the associated group, or the default group.          """Return the associated group, or the default group.
# Line 350  class ClassGroupProperties: Line 377  class ClassGroupProperties:
377          if props is not None:          if props is not None:
378              self.SetProperties(props)              self.SetProperties(props)
379          else:          else:
380              self.SetLineColor(Color.None)              self.SetLineColor(Color.Black)
381              self.SetLineWidth(1)              self.SetLineWidth(1)
382              self.SetFill(Color.None)              self.SetFill(Color.None)
383    
# Line 414  class ClassGroupProperties: Line 441  class ClassGroupProperties:
441      def __ne__(self, other):      def __ne__(self, other):
442          return not self.__eq__(other)          return not self.__eq__(other)
443    
444        def __copy__(self):
445            return ClassGroupProperties(self)
446    
447  class ClassGroup:  class ClassGroup:
448      """A base class for all Groups within a Classification"""      """A base class for all Groups within a Classification"""
449    
# Line 481  class ClassGroupSingleton(ClassGroup): Line 511  class ClassGroupSingleton(ClassGroup):
511                                     self.GetProperties(),                                     self.GetProperties(),
512                                     self.GetLabel())                                     self.GetLabel())
513    
514        def __deepcopy__(self, memo):
515            return ClassGroupSingleton(copy.copy(self.GetValue()),
516                                       copy.copy(self.GetProperties()),
517                                       copy.copy(self.GetLabel()))
518    
519      def GetValue(self):      def GetValue(self):
520          """Return the associated value."""          """Return the associated value."""
521          return self.value          return self.value
# Line 540  class ClassGroupDefault(ClassGroup): Line 575  class ClassGroupDefault(ClassGroup):
575      def __copy__(self):      def __copy__(self):
576          return ClassGroupDefault(self.GetProperties(), self.GetLabel())          return ClassGroupDefault(self.GetProperties(), self.GetLabel())
577    
578        def __deepcopy__(self, memo):
579            return ClassGroupDefault(copy.copy(self.GetProperties()),
580                                     copy.copy(self.GetLabel()))
581    
582      def Matches(self, value):      def Matches(self, value):
583          return True          return True
584    
# Line 598  class ClassGroupRange(ClassGroup): Line 637  class ClassGroupRange(ClassGroup):
637                                 self.GetProperties(),                                 self.GetProperties(),
638                                 self.GetLabel())                                 self.GetLabel())
639    
640        def __deepcopy__(self, memo):
641            return ClassGroupRange(copy.copy(self.GetMin()),
642                                   copy.copy(self.GetMax()),
643                                   copy.copy(self.GetProperties()),
644                                   copy.copy(self.GetLabel()))
645    
646      def GetMin(self):      def GetMin(self):
647          """Return the range's minimum value."""          """Return the range's minimum value."""
648          return self.min          return self.min

Legend:
Removed from v.479  
changed lines
  Added in v.491

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26