/[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 544 by jonathan, Thu Mar 20 09:43:48 2003 UTC revision 960 by jonathan, Wed May 21 17:23:11 2003 UTC
# Line 16  an input value falls with a range that d Line 16  an input value falls with a range that d
16  If no mapping can be found then default data will  If no mapping can be found then default data will
17  be returned. Input values must be hashable objects  be returned. Input values must be hashable objects
18    
19  See the description of GetGroup() for more information  See the description of FindGroup() for more information
20  on the mapping algorithm.  on the mapping algorithm.
21  """  """
22        
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  import copy, operator
27    
28  from types import *  from Thuban import _
29    
30  from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \  import types
31       LAYER_VISIBILITY_CHANGED  
32    from messages import \
33        LAYER_PROJECTION_CHANGED, \
34        LAYER_LEGEND_CHANGED, \
35        LAYER_VISIBILITY_CHANGED
36    
 from Thuban import _  
37  from Thuban.Model.color import Color  from Thuban.Model.color import Color
38    from Thuban.Model.range import Range
39    
40  import Thuban.Model.layer  import Thuban.Model.layer
41    
 # constants  
 RANGE_MIN  = 0  
 RANGE_MAX  = 1  
 RANGE_DATA = 2  
   
42  class Classification:  class Classification:
43      """Encapsulates the classification of layer. The Classification      """Encapsulates the classification of layer.
44      divides some kind of data into Groups which are associated with      
45      properties. Later the properties can be retrieved by matching      The Classification divides some kind of data into Groups which
46      data values to the appropriate group."""      are associated with properties. Later the properties can be
47        retrieved by matching data values to the appropriate group.
48        """
49    
50      def __init__(self, layer = None, field = None):      def __init__(self, layer = None, field = None):
51          """Initialize a classification.          """Initialize a classification.
52    
53             layer -- the Layer object who owns this classification          layer -- the Layer object who owns this classification
54    
55             field -- the name of the data table field that          field -- the name of the data table field that
56                      is to be used to classify layer properties                   is to be used to classify layer properties
57          """          """
58    
59          self.layer = None          self.layer = None
60          self.field = None          self.field = None
61          self.fieldType = None          self.fieldType = None
62          self.groups = []          self.__groups = []
63    
64          self.__setLayerLock = False          self.__setLayerLock = False
65    
# Line 68  class Classification: Line 69  class Classification:
69          self.SetField(field)          self.SetField(field)
70    
71      def __iter__(self):      def __iter__(self):
72          return ClassIterator(self.groups)          return ClassIterator(self.__groups)
73    
74        def __deepcopy__(self, memo):
75            clazz = Classification()
76    
77            # note: the only thing that isn't copied is the layer reference
78            clazz.field = self.field
79            clazz.fieldType = self.fieldType
80            clazz.__groups[0] = copy.deepcopy(self.__groups[0])
81    
82            for i in range(1, len(self.__groups)):
83                clazz.__groups.append(copy.deepcopy(self.__groups[i]))
84    
85            return clazz
86    
87      def __SendNotification(self):      def __SendNotification(self):
88          """Notify the layer that this class has changed."""          """Notify the layer that this class has changed."""
# Line 78  class Classification: Line 92  class Classification:
92      def SetField(self, field):      def SetField(self, field):
93          """Set the name of the data table field to use.          """Set the name of the data table field to use.
94                    
95             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,
96             otherwise the layer is queried to find the type of the          otherwise the layer is queried to find the type of the
97             field data          field data
98    
99             field -- if None then all values map to the default data          field -- if None then all values map to the default data
100          """          """
101    
102          if field == "":          if field == "":
# Line 135  class Classification: Line 149  class Classification:
149      def SetLayer(self, layer):      def SetLayer(self, layer):
150          """Set the owning Layer of this classification.          """Set the owning Layer of this classification.
151    
152             A ValueError exception will be thrown either the field or          A ValueError exception will be thrown either the field or
153             field type mismatch the information in the layer's table.          field type mismatch the information in the layer's table.
154          """          """
155    
156          # prevent infinite recursion when calling SetClassification()          # prevent infinite recursion when calling SetClassification()
# Line 150  class Classification: Line 164  class Classification:
164                  self.layer = None                  self.layer = None
165                  l.SetClassification(None)                  l.SetClassification(None)
166          else:          else:
167              assert(isinstance(layer, Thuban.Model.layer.Layer))              assert isinstance(layer, Thuban.Model.layer.Layer)
168    
169              old_layer = self.layer              old_layer = self.layer
170    
# Line 171  class Classification: Line 185  class Classification:
185          """Return the parent layer."""          """Return the parent layer."""
186          return self.layer          return self.layer
187    
     def SetDefaultGroup(self, group):  
         """Set the group to be used when a value can't be classified.  
   
            group -- group that the value maps to.  
         """  
   
         assert(isinstance(group, ClassGroupDefault))  
         self.AddGroup(group)  
   
     def GetDefaultGroup(self):  
         """Return the default group."""  
         return self.groups[0]  
188    
189      #      #
190      # these SetDefault* methods are really only provided for      # these SetDefault* methods are really only provided for
# Line 195  class Classification: Line 197  class Classification:
197    
198          fill -- a Color object.          fill -- a Color object.
199          """          """
200          assert(isinstance(fill, Color))          assert isinstance(fill, Color)
201          self.GetDefaultGroup().GetProperties().SetFill(fill)          self.GetDefaultGroup().GetProperties().SetFill(fill)
202          self.__SendNotification()          self.__SendNotification()
203                    
# Line 208  class Classification: Line 210  class Classification:
210    
211          color -- a Color object.          color -- a Color object.
212          """          """
213          assert(isinstance(color, Color))          assert isinstance(color, Color)
214          self.GetDefaultGroup().GetProperties().SetLineColor(color)          self.GetDefaultGroup().GetProperties().SetLineColor(color)
215          self.__SendNotification()          self.__SendNotification()
216                    
# Line 221  class Classification: Line 223  class Classification:
223    
224          lineWidth -- an integer > 0.          lineWidth -- an integer > 0.
225          """          """
226          assert(isinstance(lineWidth, IntType))          assert isinstance(lineWidth, types.IntType)
227          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)          self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)
228          self.__SendNotification()          self.__SendNotification()
229                    
# Line 229  class Classification: Line 231  class Classification:
231          """Return the default line width."""          """Return the default line width."""
232          return self.GetDefaultGroup().GetProperties().GetLineWidth()          return self.GetDefaultGroup().GetProperties().GetLineWidth()
233                    
234      def AddGroup(self, item):  
235          """Add a new ClassGroup item to the classification.      #
236        # The methods that manipulate self.__groups have to be kept in
237        # sync. We store the default group in index 0 to make it
238        # convienent to iterate over the classification's groups, but
239        # from the user's perspective the first (non-default) group is
240        # at index 0 and the DefaultGroup is a special entity.
241        #
242    
243        def SetDefaultGroup(self, group):
244            """Set the group to be used when a value can't be classified.
245    
246            group -- group that the value maps to.
247            """
248    
249            assert isinstance(group, ClassGroupDefault)
250            if len(self.__groups) > 0:
251                self.__groups[0] = group
252            else:
253                self.__groups.append(group)
254    
255        def GetDefaultGroup(self):
256            """Return the default group."""
257            return self.__groups[0]
258    
259        def AppendGroup(self, item):
260            """Append a new ClassGroup item to the classification.
261    
262          item -- this must be a valid ClassGroup object          item -- this must be a valid ClassGroup object
263          """          """
264    
265          assert(isinstance(item, ClassGroup))          self.InsertGroup(self.GetNumGroups(), item)
266    
267          if len(self.groups) > 0 and isinstance(item, ClassGroupDefault):      def InsertGroup(self, index, group):
268              self.groups[0] = item          
269          else:          assert isinstance(group, ClassGroup)
270              self.groups.append(item)  
271            self.__groups.insert(index + 1, group)
272    
273            self.__SendNotification()
274    
275        def RemoveGroup(self, index):
276            return self.__groups.pop(index + 1)
277    
278        def ReplaceGroup(self, index, group):
279            assert isinstance(group, ClassGroup)
280    
281            self.__groups[index + 1] = group
282    
283          self.__SendNotification()          self.__SendNotification()
284    
285      def GetGroup(self, value):      def GetGroup(self, index):
286            return self.__groups[index + 1]
287    
288        def GetNumGroups(self):
289            """Return the number of non-default groups in the classification."""
290            return len(self.__groups) - 1
291    
292    
293        def FindGroup(self, value):
294          """Return the associated group, or the default group.          """Return the associated group, or the default group.
295    
296             Groups are checked in the order the were added to the          Groups are checked in the order the were added to the
297             Classification.          Classification.
298    
299             value -- the value to classify. If there is no mapping,          value -- the value to classify. If there is no mapping,
300                      the field is None or value is None,                   the field is None or value is None,
301                      return the default properties                   return the default properties
302          """          """
303    
304          if self.GetField() is not None and value is not None:          if self.GetField() is not None and value is not None:
305    
306              for i in range(1, len(self.groups)):              for i in range(1, len(self.__groups)):
307                  group = self.groups[i]                  group = self.__groups[i]
308                  if group.Matches(value):                  if group.Matches(value):
309                      return group                      return group
310    
311          return self.GetDefaultGroup()          return self.GetDefaultGroup()
312    
313      def GetProperties(self, value):      def GetProperties(self, value):
314          """Return the properties associated with the given value."""          """Return the properties associated with the given value.
315          
316            Use this function rather than Classification.FindGroup().GetProperties()
317            since the returned group may be a ClassGroupMap which doesn't support
318            a call to GetProperties().
319            """
320    
321          group = self.GetGroup(value)          group = self.FindGroup(value)
322          if isinstance(group, ClassGroupMap):          if isinstance(group, ClassGroupMap):
323              return group.GetPropertiesFromValue(value)              return group.GetPropertiesFromValue(value)
324          else:          else:
# Line 277  class Classification: Line 328  class Classification:
328          items = []          items = []
329    
330          def build_color_item(text, color):          def build_color_item(text, color):
331              if color is Color.None:              if color is Color.Transparent:
332                  return ("%s: %s" % (text, _("None")), None)                  return ("%s: %s" % (text, _("None")), None)
333    
334              return ("%s: (%.3f, %.3f, %.3f)" %              return ("%s: (%.3f, %.3f, %.3f)" %
# Line 302  class Classification: Line 353  class Classification:
353              return (label, i)              return (label, i)
354    
355          for p in self:          for p in self:
356              if isinstance(p, ClassGroupDefault):              items.append(build_item(p, p.GetDisplayText()))
357                  items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'")))  
358              elif isinstance(p, ClassGroupSingleton):  #           if isinstance(p, ClassGroupDefault):
359                  items.append(build_item(p, str(p.GetValue())))  #               items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'")))
360              elif isinstance(p, ClassGroupRange):  #           elif isinstance(p, ClassGroupSingleton):
361                  items.append(build_item(p, "%s - %s" %  #               items.append(build_item(p, str(p.GetValue())))
362                                             (p.GetMin(), p.GetMax())))  #           elif isinstance(p, ClassGroupRange):
363    #               items.append(build_item(p, "%s - %s" %
364    #                                          (p.GetMin(), p.GetMax())))
365    
366          return (_("Classification"), items)          return (_("Classification"), items)
367    
# Line 373  class ClassGroupProperties: Line 426  class ClassGroupProperties:
426          props -- a ClassGroupProperties object. The class is copied if          props -- a ClassGroupProperties object. The class is copied if
427                   prop is not None. Otherwise, a default set of properties                   prop is not None. Otherwise, a default set of properties
428                   is created such that: line color = Color.Black, line width = 1,                   is created such that: line color = Color.Black, line width = 1,
429                   and fill color = Color.None                   and fill color = Color.Transparent
430          """          """
431    
432          self.stroke = None          #self.stroke = None
433          self.strokeWidth = 0          #self.strokeWidth = 0
434          self.fill = None          #self.fill = None
435    
436          if props is not None:          if props is not None:
437              self.SetProperties(props)              self.SetProperties(props)
438          else:          else:
439              self.SetLineColor(Color.Black)              self.SetLineColor(Color.Black)
440              self.SetLineWidth(1)              self.SetLineWidth(1)
441              self.SetFill(Color.None)              self.SetFill(Color.Transparent)
442    
443      def SetProperties(self, props):      def SetProperties(self, props):
444          """Set this class's properties to those in class props."""          """Set this class's properties to those in class props."""
445    
446          assert(isinstance(props, ClassGroupProperties))          assert isinstance(props, ClassGroupProperties)
447          self.SetLineColor(props.GetLineColor())          self.SetLineColor(props.GetLineColor())
448          self.SetLineWidth(props.GetLineWidth())          self.SetLineWidth(props.GetLineWidth())
449          self.SetFill(props.GetFill())          self.SetFill(props.GetFill())
450                    
451      def GetLineColor(self):      def GetLineColor(self):
452          """Return the line color as a Color object."""          """Return the line color as a Color object."""
453          return self.stroke          return self.__stroke
454    
455      def SetLineColor(self, color):      def SetLineColor(self, color):
456          """Set the line color.          """Set the line color.
# Line 405  class ClassGroupProperties: Line 458  class ClassGroupProperties:
458          color -- the color of the line. This must be a Color object.          color -- the color of the line. This must be a Color object.
459          """          """
460    
461          assert(isinstance(color, Color))          assert isinstance(color, Color)
462          self.stroke = color          self.__stroke = color
463    
464      def GetLineWidth(self):      def GetLineWidth(self):
465          """Return the line width."""          """Return the line width."""
466          return self.strokeWidth          return self.__strokeWidth
467    
468      def SetLineWidth(self, lineWidth):      def SetLineWidth(self, lineWidth):
469          """Set the line width.          """Set the line width.
470    
471          lineWidth -- the new line width. This must be > 0.          lineWidth -- the new line width. This must be > 0.
472          """          """
473          assert(isinstance(lineWidth, IntType))          assert isinstance(lineWidth, types.IntType)
474          if (lineWidth < 1):          if (lineWidth < 1):
475              raise ValueError(_("lineWidth < 1"))              raise ValueError(_("lineWidth < 1"))
476    
477          self.strokeWidth = lineWidth          self.__strokeWidth = lineWidth
478    
479      def GetFill(self):      def GetFill(self):
480          """Return the fill color as a Color object."""          """Return the fill color as a Color object."""
481          return self.fill          return self.__fill
482    
483      def SetFill(self, fill):      def SetFill(self, fill):
484          """Set the fill color.          """Set the fill color.
# Line 433  class ClassGroupProperties: Line 486  class ClassGroupProperties:
486          fill -- the color of the fill. This must be a Color object.          fill -- the color of the fill. This must be a Color object.
487          """          """
488    
489          assert(isinstance(fill, Color))          assert isinstance(fill, Color)
490          self.fill = fill          self.__fill = fill
491    
492      def __eq__(self, other):      def __eq__(self, other):
493          """Return true if 'props' has the same attributes as this class"""          """Return true if 'props' has the same attributes as this class"""
494    
495            #
496            # using 'is' over '==' results in a huge performance gain
497            # in the renderer
498            #
499          return isinstance(other, ClassGroupProperties)   \          return isinstance(other, ClassGroupProperties)   \
500              and self.stroke      == other.GetLineColor() \              and (self.__stroke is other.__stroke or      \
501              and self.strokeWidth == other.GetLineWidth() \                   self.__stroke == other.__stroke)        \
502              and self.fill        == other.GetFill()              and (self.__fill is other.__fill or          \
503                     self.__fill == other.__fill)            \
504                and self.__strokeWidth == other.__strokeWidth
505    
506      def __ne__(self, other):      def __ne__(self, other):
507          return not self.__eq__(other)          return not self.__eq__(other)
# Line 450  class ClassGroupProperties: Line 509  class ClassGroupProperties:
509      def __copy__(self):      def __copy__(self):
510          return ClassGroupProperties(self)          return ClassGroupProperties(self)
511    
512        def __deepcopy__(self):
513            return ClassGroupProperties(self)
514    
515        def __repr__(self):
516            return repr((self.__stroke, self.__strokeWidth, self.__fill))
517    
518  class ClassGroup:  class ClassGroup:
519      """A base class for all Groups within a Classification"""      """A base class for all Groups within a Classification"""
520    
521      def __init__(self, label = ""):      def __init__(self, label = "", props = None, group = None):
522          """Constructor.          """Constructor.
523    
524          label -- A string representing the Group's label          label -- A string representing the Group's label
525          """          """
526    
527          self.label = None          if group is not None:
528                self.SetLabel(copy.copy(group.GetLabel()))
529          self.SetLabel(label)              self.SetProperties(copy.copy(group.GetProperties()))
530                self.SetVisible(group.IsVisible())
531            else:
532                self.SetLabel(label)
533                self.SetProperties(props)
534                self.SetVisible(True)
535    
536      def GetLabel(self):      def GetLabel(self):
537          """Return the Group's label."""          """Return the Group's label."""
# Line 473  class ClassGroup: Line 543  class ClassGroup:
543          label -- a string representing the Group's label. This must          label -- a string representing the Group's label. This must
544                   not be None.                   not be None.
545          """          """
546          assert(isinstance(label, StringType))          assert isinstance(label, types.StringTypes)
547          self.label = label          self.label = label
548    
549      def GetDisplayText(self):      def GetDisplayText(self):
550          assert(False, "GetDisplay must be overridden by subclass!")          assert False, "GetDisplay must be overridden by subclass!"
551          return ""          return ""
552    
553      def Matches(self, value):      def Matches(self, value):
# Line 485  class ClassGroup: Line 555  class ClassGroup:
555    
556          Returns False. This needs to be overridden by all subclasses.          Returns False. This needs to be overridden by all subclasses.
557          """          """
558          assert(False, "GetMatches must be overridden by subclass!")          assert False, "GetMatches must be overridden by subclass!"
559          return False          return False
560    
561      def GetProperties(self):      def GetProperties(self):
562          """Return the properties associated with the given value.          """Return the properties associated with the given value."""
563    
564          Returns None. This needs to be overridden by all subclasses.          return self.prop
         """  
         assert(False, "GetProperties must be overridden by subclass!")  
         return None  
565    
566        def SetProperties(self, prop):
567            """Set the properties associated with this Group.
568    
569            prop -- a ClassGroupProperties object. if prop is None,
570                    a default set of properties is created.
571            """
572    
573            if prop is None: prop = ClassGroupProperties()
574            assert isinstance(prop, ClassGroupProperties)
575            self.prop = prop
576    
577        def IsVisible(self):
578            return self.visible
579    
580        def SetVisible(self, visible):
581            self.visible = visible
582    
583        def __eq__(self, other):
584            return isinstance(other, ClassGroup) \
585                and self.label == other.label \
586                and self.GetProperties() == other.GetProperties()
587    
588        def __ne__(self, other):
589            return not self.__eq__(other)
590    
591        def __repr__(self):
592            return repr(self.label) + ", " + repr(self.GetProperties())
593            
594  class ClassGroupSingleton(ClassGroup):  class ClassGroupSingleton(ClassGroup):
595      """A Group that is associated with a single value."""      """A Group that is associated with a single value."""
596    
597      def __init__(self, value = 0, prop = None, label = ""):      def __init__(self, value = 0, props = None, label = "", group = None):
598          """Constructor.          """Constructor.
599    
600          value -- the associated value.          value -- the associated value.
# Line 510  class ClassGroupSingleton(ClassGroup): Line 604  class ClassGroupSingleton(ClassGroup):
604    
605          label -- a label for this group.          label -- a label for this group.
606          """          """
607          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.prop = None  
         self.value = None  
608    
609          self.SetValue(value)          self.SetValue(value)
         self.SetProperties(prop)  
610    
611      def __copy__(self):      def __copy__(self):
612          return ClassGroupSingleton(self.GetValue(),          return ClassGroupSingleton(self.GetValue(),
# Line 524  class ClassGroupSingleton(ClassGroup): Line 614  class ClassGroupSingleton(ClassGroup):
614                                     self.GetLabel())                                     self.GetLabel())
615    
616      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
617          return ClassGroupSingleton(copy.copy(self.GetValue()),          return ClassGroupSingleton(self.GetValue(), group = self)
                                    copy.copy(self.GetProperties()),  
                                    copy.copy(self.GetLabel()))  
618    
619      def GetValue(self):      def GetValue(self):
620          """Return the associated value."""          """Return the associated value."""
621          return self.value          return self.__value
622    
623      def SetValue(self, value):      def SetValue(self, value):
624          """Associate this Group with the given value."""          """Associate this Group with the given value."""
625          self.value = value          self.__value = value
626    
627      def Matches(self, value):      def Matches(self, value):
628          """Determine if the given value matches the associated Group value."""          """Determine if the given value matches the associated Group value."""
629    
630          """Returns True if the value matches, False otherwise."""          """Returns True if the value matches, False otherwise."""
631    
632          return self.value == value          return self.__value == value
   
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
   
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
   
         if prop is None: prop = ClassGroupProperties()  
         assert(isinstance(prop, ClassGroupProperties))  
         self.prop = prop  
633    
634      def GetDisplayText(self):      def GetDisplayText(self):
635          label = self.GetLabel()          label = self.GetLabel()
# Line 567  class ClassGroupSingleton(ClassGroup): Line 639  class ClassGroupSingleton(ClassGroup):
639          return str(self.GetValue())          return str(self.GetValue())
640    
641      def __eq__(self, other):      def __eq__(self, other):
642          return isinstance(other, ClassGroupSingleton) \          return ClassGroup.__eq__(self, other) \
643              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupSingleton) \
644              and self.GetValue() == other.GetValue()              and self.__value == other.__value
645    
646      def __ne__(self, other):      def __repr__(self):
647          return not self.__eq__(other)          return "(" + repr(self.__value) + ", " + ClassGroup.__repr__(self) + ")"
648    
649  class ClassGroupDefault(ClassGroup):  class ClassGroupDefault(ClassGroup):
650      """The default Group. When values do not match any other      """The default Group. When values do not match any other
651         Group within a Classification, the properties from this         Group within a Classification, the properties from this
652         class are used."""         class are used."""
653    
654      def __init__(self, prop = None, label = ""):      def __init__(self, props = None, label = "", group = None):
655          """Constructor.          """Constructor.
656    
657          prop -- a ClassGroupProperites object. If prop is None a default          prop -- a ClassGroupProperites object. If prop is None a default
# Line 588  class ClassGroupDefault(ClassGroup): Line 660  class ClassGroupDefault(ClassGroup):
660          label -- a label for this group.          label -- a label for this group.
661          """          """
662    
663          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
         self.SetProperties(prop)  
664    
665      def __copy__(self):      def __copy__(self):
666          return ClassGroupDefault(self.GetProperties(), self.GetLabel())          return ClassGroupDefault(self.GetProperties(), self.GetLabel())
667    
668      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
669          return ClassGroupDefault(copy.copy(self.GetProperties()),          return ClassGroupDefault(label = self.GetLabel(), group = self)
                                  copy.copy(self.GetLabel()))  
670    
671      def Matches(self, value):      def Matches(self, value):
672          return True          return True
673    
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
   
         if prop is None: prop = ClassGroupProperties()  
         assert(isinstance(prop, ClassGroupProperties))  
         self.prop = prop  
   
674      def GetDisplayText(self):      def GetDisplayText(self):
675          label = self.GetLabel()          label = self.GetLabel()
676    
677          if label != "": return label          if label != "": return label
678    
679          return "DEFAULT"          return _("DEFAULT")
680    
681      def __eq__(self, other):      def __eq__(self, other):
682          return isinstance(other, ClassGroupDefault) \          return ClassGroup.__eq__(self, other) \
683                and isinstance(other, ClassGroupDefault) \
684              and self.GetProperties() == other.GetProperties()              and self.GetProperties() == other.GetProperties()
685    
686      def __ne__(self, other):      def __repr__(self):
687          return not self.__eq__(other)          return "(" + ClassGroup.__repr__(self) + ")"
688    
689  class ClassGroupRange(ClassGroup):  class ClassGroupRange(ClassGroup):
690      """A Group that represents a range of values that map to the same      """A Group that represents a range of values that map to the same
691         set of properties."""         set of properties."""
692    
693      def __init__(self, min = 0, max = 1, prop = None, label = ""):      def __init__(self, min = 0, max = 1, props = None, label = "", group=None):
694          """Constructor.          """Constructor.
695    
696          The minumum value must be strictly less than the maximum.          The minumum value must be strictly less than the maximum.
# Line 649  class ClassGroupRange(ClassGroup): Line 705  class ClassGroupRange(ClassGroup):
705          label -- a label for this group.          label -- a label for this group.
706          """          """
707    
708          ClassGroup.__init__(self, label)          ClassGroup.__init__(self, label, props, group)
   
         self.min = self.max = 0  
         self.prop = None  
709    
710            #self.__min = self.__max = 0
711            #self.__range = Range("[" + repr(float(min)) + ";" +
712                                       #repr(float(max)) + "[")
713          self.SetRange(min, max)          self.SetRange(min, max)
         self.SetProperties(prop)  
714    
715      def __copy__(self):      def __copy__(self):
716          return ClassGroupRange(self.GetMin(),          return ClassGroupRange(min = self.__range,
717                                 self.GetMax(),                                 max = None,
718                                 self.GetProperties(),                                 props = self.GetProperties(),
719                                 self.GetLabel())                                 label = self.GetLabel())
720    
721      def __deepcopy__(self, memo):      def __deepcopy__(self, memo):
722          return ClassGroupRange(copy.copy(self.GetMin()),          return ClassGroupRange(min = copy.copy(self.__range),
723                                 copy.copy(self.GetMax()),                                 max = copy.copy(self.GetMax()),
724                                 copy.copy(self.GetProperties()),                                 group = self)
                                copy.copy(self.GetLabel()))  
725    
726      def GetMin(self):      def GetMin(self):
727          """Return the range's minimum value."""          """Return the range's minimum value."""
728          return self.min          return self.__range.GetRange()[1]
729    
730      def SetMin(self, min):      def SetMin(self, min):
731          """Set the range's minimum value.          """Set the range's minimum value.
# Line 680  class ClassGroupRange(ClassGroup): Line 734  class ClassGroupRange(ClassGroup):
734                 maximum value. Use SetRange() to change both min and max values.                 maximum value. Use SetRange() to change both min and max values.
735          """          """
736            
737          self.SetRange(min, self.max)          self.SetRange(min, self.__range.GetRange()[2])
738    
739      def GetMax(self):      def GetMax(self):
740          """Return the range's maximum value."""          """Return the range's maximum value."""
741          return self.max          return self.__range.GetRange()[2]
742    
743      def SetMax(self, max):      def SetMax(self, max):
744          """Set the range's maximum value.          """Set the range's maximum value.
# Line 692  class ClassGroupRange(ClassGroup): Line 746  class ClassGroupRange(ClassGroup):
746          max -- the new maximum. Note that this must be greater than the current          max -- the new maximum. Note that this must be greater than the current
747                 minimum value. Use SetRange() to change both min and max values.                 minimum value. Use SetRange() to change both min and max values.
748          """          """
749          self.SetRange(self.min, max)          self.SetRange(self.__range.GetRange()[1], max)
750    
751      def SetRange(self, min, max):      def SetRange(self, min, max = None):
752          """Set a new range.          """Set a new range.
753    
754          Note that min must be strictly less than max.          Note that min must be strictly less than max.
# Line 703  class ClassGroupRange(ClassGroup): Line 757  class ClassGroupRange(ClassGroup):
757          min -- the new maximum value          min -- the new maximum value
758          """          """
759    
760          if min >= max:          if isinstance(min, Range):
761              raise ValueError(_("ClassGroupRange: %i(min) >= %i(max)!") %              self.__range = min
762                               (min, max))          else:
763          self.min = min              if max is None:
764          self.max = max                  raise ValueError()
765    
766                self.__range = Range(("[", min, max, "["))
767    
768      def GetRange(self):      def GetRange(self):
769          """Return the range as a tuple (min, max)"""          """Return the range as a string"""
770          return (self.min, self.max)          #return (self.__min, self.__max)
771            return self.__range.string(self.__range.GetRange())
772    
773      def Matches(self, value):      def Matches(self, value):
774          """Determine if the given value lies with the current range.          """Determine if the given value lies with the current range.
# Line 719  class ClassGroupRange(ClassGroup): Line 776  class ClassGroupRange(ClassGroup):
776          The following check is used: min <= value < max.          The following check is used: min <= value < max.
777          """          """
778    
779          return self.min <= value < self.max          return operator.contains(self.__range, value)
780            #return self.__min <= value < self.__max
     def GetProperties(self):  
         """Return the Properties associated with this Group."""  
         return self.prop  
   
     def SetProperties(self, prop):  
         """Set the properties associated with this Group.  
   
         prop -- a ClassGroupProperties object. if prop is None,  
                 a default set of properties is created.  
         """  
         if prop is None: prop = ClassGroupProperties()  
         assert(isinstance(prop, ClassGroupProperties))  
         self.prop = prop  
781    
782      def GetDisplayText(self):      def GetDisplayText(self):
783          label = self.GetLabel()          label = self.GetLabel()
784    
785          if label != "": return label          if label != "": return label
786    
787          return _("%s - %s") % (self.GetMin(), self.GetMax())          #return _("%s - %s") % (self.GetMin(), self.GetMax())
788            #return repr(self.__range)
789            return self.__range.string(self.__range.GetRange())
790    
791      def __eq__(self, other):      def __eq__(self, other):
792          return isinstance(other, ClassGroupRange) \          return ClassGroup.__eq__(self, other) \
793              and self.GetProperties() == other.GetProperties() \              and isinstance(other, ClassGroupRange) \
794              and self.GetRange() == other.GetRange()              and self.__range == other.__range
795                #and self.__min == other.__min \
796      def __ne__(self, other):              #and self.__max == other.__max
797          return not self.__eq__(other)  
798        def __repr__(self):
799            return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")"
800            #return "(" + repr(self.__min) + ", " + repr(self.__max) + ", " + \
801                   #ClassGroup.__repr__(self) + ")"
802    
803  class ClassGroupMap(ClassGroup):  class ClassGroupMap(ClassGroup):
804      """Currently, this class is not used."""      """Currently, this class is not used."""

Legend:
Removed from v.544  
changed lines
  Added in v.960

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26