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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 609 - (hide annotations)
Fri Apr 4 13:55:59 2003 UTC (21 years, 11 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/classification.py
File MIME type: text/x-python
File size: 22976 byte(s)
Rename Color.None to
        Color.Transparent.
(ClassGroupProperties.SetLineColori, ClassGroupProperties.SetFill):
        Don't bother copying the color, since Colors are immutable.

1 bh 453 # Copyright (c) 2001, 2003 by Intevation GmbH
2 jonathan 371 # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     __version__ = "$Revision$"
9    
10     """
11     A Classification provides a mapping from an input value
12     to data. This mapping can be specified in two ways.
13     First, specific values can be associated with data.
14     Second, ranges can be associated with data such that if
15     an input value falls with a range that data is returned.
16 jonathan 388 If no mapping can be found then default data will
17 jonathan 371 be returned. Input values must be hashable objects
18    
19 jonathan 449 See the description of GetGroup() for more information
20 jonathan 371 on the mapping algorithm.
21     """
22    
23 jonathan 397 # fix for people using python2.1
24     from __future__ import nested_scopes
25    
26 jonathan 484 import copy
27    
28 jonathan 436 from types import *
29    
30 jonathan 388 from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \
31     LAYER_VISIBILITY_CHANGED
32    
33 jan 374 from Thuban import _
34 jonathan 381 from Thuban.Model.color import Color
35 jan 374
36 jonathan 436 import Thuban.Model.layer
37    
38 jonathan 371 # constants
39     RANGE_MIN = 0
40     RANGE_MAX = 1
41     RANGE_DATA = 2
42    
43     class Classification:
44 jonathan 462 """Encapsulates the classification of layer. The Classification
45     divides some kind of data into Groups which are associated with
46     properties. Later the properties can be retrieved by matching
47     data values to the appropriate group."""
48 jonathan 371
49 jonathan 410 def __init__(self, layer = None, field = None):
50 jonathan 371 """Initialize a classification.
51    
52 jonathan 462 layer -- the Layer object who owns this classification
53 jonathan 388
54 jonathan 371 field -- the name of the data table field that
55     is to be used to classify layer properties
56     """
57    
58 jonathan 462 self.layer = None
59     self.field = None
60     self.fieldType = None
61     self.groups = []
62 jonathan 436
63 jonathan 528 self.__setLayerLock = False
64    
65 jonathan 436 self.SetDefaultGroup(ClassGroupDefault())
66 jonathan 491
67 jonathan 462 self.SetLayer(layer)
68 jonathan 436 self.SetField(field)
69    
70 jonathan 428 def __iter__(self):
71 jonathan 462 return ClassIterator(self.groups)
72 jonathan 428
73 jonathan 491 def __SendNotification(self):
74     """Notify the layer that this class has changed."""
75     if self.layer is not None:
76     self.layer.ClassChanged()
77 jonathan 410
78 jonathan 491 def SetField(self, field):
79 jonathan 371 """Set the name of the data table field to use.
80    
81 jonathan 479 If there is no layer then the field type is set to None,
82     otherwise the layer is queried to find the type of the
83     field data
84    
85 jonathan 388 field -- if None then all values map to the default data
86 jonathan 371 """
87    
88 jonathan 436 if field == "":
89     field = None
90    
91 jonathan 462
92 jonathan 491 if field is None:
93     if self.layer is not None:
94     self.fieldType = None
95 jonathan 462 else:
96 jonathan 491 if self.layer is not None:
97     fieldType = self.layer.GetFieldType(field)
98     if fieldType is None:
99     raise ValueError("'%s' was not found in the layer's table."
100     % self.field)
101 jonathan 462
102 jonathan 491 #
103     # unfortunately we cannot call SetFieldType() because it
104     # requires the layer to be None
105     #
106     self.fieldType = fieldType
107     #self.SetFieldType(fieldType)
108 jonathan 462
109 jonathan 491 self.field = field
110 jonathan 462
111 jonathan 491 self.__SendNotification()
112 jonathan 371
113 jonathan 388 def GetField(self):
114 jonathan 462 """Return the name of the field."""
115 jonathan 388 return self.field
116    
117 jonathan 462 def GetFieldType(self):
118     """Return the field type."""
119     return self.fieldType
120    
121     def SetFieldType(self, type):
122 jonathan 491 """Set the type of the field used by this classification.
123 jonathan 462
124 jonathan 491 A ValueError is raised if the owning layer is not None and
125     'type' is different from the current field type.
126     """
127    
128     if type != self.fieldType:
129     if self.layer is not None:
130     raise ValueError()
131     else:
132     self.fieldType = type
133     self.__SendNotification()
134    
135 jonathan 410 def SetLayer(self, layer):
136 jonathan 491 """Set the owning Layer of this classification.
137    
138     A ValueError exception will be thrown either the field or
139     field type mismatch the information in the layer's table.
140     """
141 jonathan 462
142 jonathan 528 # prevent infinite recursion when calling SetClassification()
143     if self.__setLayerLock: return
144    
145     self.__setLayerLock = True
146    
147 jonathan 491 if layer is None:
148     if self.layer is not None:
149     l = self.layer
150     self.layer = None
151     l.SetClassification(None)
152     else:
153 jonathan 602 assert isinstance(layer, Thuban.Model.layer.Layer)
154 jonathan 462
155 jonathan 491 old_layer = self.layer
156 jonathan 479
157 jonathan 491 self.layer = layer
158 jonathan 462
159 jonathan 491 try:
160     self.SetField(self.GetField()) # this sync's the fieldType
161     except ValueError:
162     self.layer = old_layer
163 jonathan 528 self.__setLayerLock = False
164 jonathan 491 raise ValueError
165     else:
166     self.layer.SetClassification(self)
167 jonathan 410
168 jonathan 528 self.__setLayerLock = False
169    
170 jonathan 410 def GetLayer(self):
171 jonathan 462 """Return the parent layer."""
172     return self.layer
173 jonathan 410
174 jonathan 436 def SetDefaultGroup(self, group):
175     """Set the group to be used when a value can't be classified.
176 jonathan 371
177 jonathan 462 group -- group that the value maps to.
178 jonathan 371 """
179    
180 jonathan 602 assert isinstance(group, ClassGroupDefault)
181 jonathan 462 self.AddGroup(group)
182 jonathan 371
183 jonathan 436 def GetDefaultGroup(self):
184 jonathan 462 """Return the default group."""
185     return self.groups[0]
186 jonathan 385
187 jonathan 428 #
188     # these SetDefault* methods are really only provided for
189     # some backward compatibility. they should be considered
190     # for removal once all the classification code is finished.
191     #
192    
193 jonathan 388 def SetDefaultFill(self, fill):
194 jonathan 462 """Set the default fill color.
195    
196     fill -- a Color object.
197     """
198 jonathan 602 assert isinstance(fill, Color)
199 jonathan 462 self.GetDefaultGroup().GetProperties().SetFill(fill)
200 jonathan 491 self.__SendNotification()
201 jonathan 388
202     def GetDefaultFill(self):
203 jonathan 462 """Return the default fill color."""
204     return self.GetDefaultGroup().GetProperties().GetFill()
205 jonathan 388
206 jonathan 462 def SetDefaultLineColor(self, color):
207     """Set the default line color.
208    
209     color -- a Color object.
210     """
211 jonathan 602 assert isinstance(color, Color)
212 jonathan 462 self.GetDefaultGroup().GetProperties().SetLineColor(color)
213 jonathan 491 self.__SendNotification()
214 jonathan 388
215 jonathan 462 def GetDefaultLineColor(self):
216     """Return the default line color."""
217     return self.GetDefaultGroup().GetProperties().GetLineColor()
218 jonathan 388
219 jonathan 462 def SetDefaultLineWidth(self, lineWidth):
220     """Set the default line width.
221    
222     lineWidth -- an integer > 0.
223     """
224 jonathan 602 assert isinstance(lineWidth, IntType)
225 jonathan 462 self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth)
226 jonathan 491 self.__SendNotification()
227 jonathan 388
228 jonathan 462 def GetDefaultLineWidth(self):
229     """Return the default line width."""
230     return self.GetDefaultGroup().GetProperties().GetLineWidth()
231 jonathan 388
232 jonathan 436 def AddGroup(self, item):
233 jonathan 462 """Add a new ClassGroup item to the classification.
234    
235     item -- this must be a valid ClassGroup object
236     """
237    
238 jonathan 602 assert isinstance(item, ClassGroup)
239 jonathan 371
240 jonathan 462 if len(self.groups) > 0 and isinstance(item, ClassGroupDefault):
241     self.groups[0] = item
242 jonathan 410 else:
243 jonathan 462 self.groups.append(item)
244 jonathan 371
245 jonathan 491 self.__SendNotification()
246 jonathan 371
247 jonathan 436 def GetGroup(self, value):
248 jonathan 462 """Return the associated group, or the default group.
249 jonathan 371
250 jonathan 462 Groups are checked in the order the were added to the
251     Classification.
252 jonathan 371
253 jonathan 388 value -- the value to classify. If there is no mapping,
254 jonathan 479 the field is None or value is None,
255     return the default properties
256 jonathan 371 """
257    
258 jonathan 479 if self.GetField() is not None and value is not None:
259 jonathan 371
260 jonathan 462 for i in range(1, len(self.groups)):
261     group = self.groups[i]
262     if group.Matches(value):
263     return group
264 jonathan 371
265 jonathan 462 return self.GetDefaultGroup()
266 jonathan 371
267 jonathan 462 def GetProperties(self, value):
268     """Return the properties associated with the given value."""
269 jonathan 371
270 jonathan 462 group = self.GetGroup(value)
271     if isinstance(group, ClassGroupMap):
272     return group.GetPropertiesFromValue(value)
273     else:
274     return group.GetProperties()
275 jonathan 436
276 jonathan 381 def TreeInfo(self):
277     items = []
278 jonathan 378
279 jonathan 410 def build_color_item(text, color):
280 jonathan 609 if color is Color.Transparent:
281 jonathan 410 return ("%s: %s" % (text, _("None")), None)
282 jonathan 381
283 jonathan 410 return ("%s: (%.3f, %.3f, %.3f)" %
284     (text, color.red, color.green, color.blue),
285     color)
286 jonathan 381
287 jonathan 436 def build_item(group, string):
288     label = group.GetLabel()
289 jonathan 410 if label == "":
290     label = string
291     else:
292     label += " (%s)" % string
293    
294 jonathan 436 props = group.GetProperties()
295 jonathan 381 i = []
296 jonathan 462 v = props.GetLineColor()
297     i.append(build_color_item(_("Line Color"), v))
298     v = props.GetLineWidth()
299     i.append(_("Line Width: %s") % v)
300 jonathan 436 v = props.GetFill()
301 jonathan 410 i.append(build_color_item(_("Fill"), v))
302     return (label, i)
303 jonathan 388
304 jonathan 428 for p in self:
305 jonathan 436 if isinstance(p, ClassGroupDefault):
306 jonathan 462 items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'")))
307 jonathan 436 elif isinstance(p, ClassGroupSingleton):
308 jonathan 428 items.append(build_item(p, str(p.GetValue())))
309 jonathan 436 elif isinstance(p, ClassGroupRange):
310 jonathan 428 items.append(build_item(p, "%s - %s" %
311     (p.GetMin(), p.GetMax())))
312 jonathan 388
313 jonathan 436 return (_("Classification"), items)
314 jonathan 381
315 jonathan 428 class ClassIterator:
316 jonathan 462 """Allows the Groups in a Classifcation to be interated over.
317 jonathan 388
318 jonathan 462 The items are returned in the following order:
319     default data, singletons, ranges, maps
320     """
321 jonathan 428
322 jonathan 462 def __init__(self, data): #default, points, ranges, maps):
323     """Constructor.
324    
325     default -- the default group
326    
327     points -- a list of singleton groups
328    
329     ranges -- a list of range groups
330    
331     maps -- a list of map groups
332     """
333    
334     self.data = data #[default, points, ranges, maps]
335     self.data_index = 0
336     #self.data_iter = iter(self.data)
337     #self.iter = None
338    
339 jonathan 428 def __iter__(self):
340     return self
341    
342     def next(self):
343 jonathan 462 """Return the next item."""
344 jonathan 428
345 jonathan 462 if self.data_index >= len(self.data):
346     raise StopIteration
347     else:
348     d = self.data[self.data_index]
349     self.data_index += 1
350     return d
351    
352     # if self.iter is None:
353     # try:
354     # self.data_item = self.data_iter.next()
355     # self.iter = iter(self.data_item)
356     # except TypeError:
357     # return self.data_item
358    
359     # try:
360     # return self.iter.next()
361     # except StopIteration:
362     # self.iter = None
363     # return self.next()
364 jonathan 428
365 jonathan 436 class ClassGroupProperties:
366 jonathan 462 """Represents the properties of a single Classification Group.
367    
368     These are used when rendering a layer."""
369 jonathan 388
370 jonathan 462 def __init__(self, props = None):
371     """Constructor.
372 jonathan 410
373 jonathan 462 props -- a ClassGroupProperties object. The class is copied if
374     prop is not None. Otherwise, a default set of properties
375 jonathan 479 is created such that: line color = Color.Black, line width = 1,
376 jonathan 609 and fill color = Color.Transparent
377 jonathan 462 """
378    
379     self.stroke = None
380     self.strokeWidth = 0
381     self.fill = None
382    
383     if props is not None:
384     self.SetProperties(props)
385 jonathan 410 else:
386 jonathan 484 self.SetLineColor(Color.Black)
387 jonathan 462 self.SetLineWidth(1)
388 jonathan 609 self.SetFill(Color.Transparent)
389 jonathan 410
390 jonathan 462 def SetProperties(self, props):
391     """Set this class's properties to those in class props."""
392    
393 jonathan 602 assert isinstance(props, ClassGroupProperties)
394 jonathan 462 self.SetLineColor(props.GetLineColor())
395     self.SetLineWidth(props.GetLineWidth())
396     self.SetFill(props.GetFill())
397    
398     def GetLineColor(self):
399     """Return the line color as a Color object."""
400 jonathan 388 return self.stroke
401    
402 jonathan 462 def SetLineColor(self, color):
403     """Set the line color.
404 jonathan 388
405 jonathan 462 color -- the color of the line. This must be a Color object.
406     """
407 jonathan 388
408 jonathan 602 assert isinstance(color, Color)
409 jonathan 609 self.stroke = color
410 jonathan 410
411 jonathan 462 def GetLineWidth(self):
412     """Return the line width."""
413     return self.strokeWidth
414 jonathan 388
415 jonathan 462 def SetLineWidth(self, lineWidth):
416     """Set the line width.
417    
418     lineWidth -- the new line width. This must be > 0.
419     """
420 jonathan 602 assert isinstance(lineWidth, IntType)
421 jonathan 462 if (lineWidth < 1):
422     raise ValueError(_("lineWidth < 1"))
423    
424     self.strokeWidth = lineWidth
425    
426 jonathan 388 def GetFill(self):
427 jonathan 462 """Return the fill color as a Color object."""
428 jonathan 388 return self.fill
429    
430     def SetFill(self, fill):
431 jonathan 462 """Set the fill color.
432    
433     fill -- the color of the fill. This must be a Color object.
434     """
435    
436 jonathan 602 assert isinstance(fill, Color)
437 jonathan 609 self.fill = fill
438 jonathan 388
439 jonathan 479 def __eq__(self, other):
440     """Return true if 'props' has the same attributes as this class"""
441 jonathan 436
442 jonathan 479 return isinstance(other, ClassGroupProperties) \
443     and self.stroke == other.GetLineColor() \
444     and self.strokeWidth == other.GetLineWidth() \
445     and self.fill == other.GetFill()
446    
447     def __ne__(self, other):
448     return not self.__eq__(other)
449    
450 jonathan 484 def __copy__(self):
451     return ClassGroupProperties(self)
452    
453 jonathan 602 def __deepcopy__(self):
454     return ClassGroupProperties(self)
455    
456 jonathan 436 class ClassGroup:
457 jonathan 462 """A base class for all Groups within a Classification"""
458 jonathan 436
459     def __init__(self, label = ""):
460 jonathan 462 """Constructor.
461 jonathan 436
462 jonathan 462 label -- A string representing the Group's label
463     """
464    
465     self.label = None
466    
467     self.SetLabel(label)
468    
469 jonathan 388 def GetLabel(self):
470 jonathan 462 """Return the Group's label."""
471 jonathan 388 return self.label
472    
473     def SetLabel(self, label):
474 jonathan 462 """Set the Group's label.
475    
476     label -- a string representing the Group's label. This must
477     not be None.
478     """
479 jonathan 602 assert isinstance(label, StringType)
480 jonathan 388 self.label = label
481    
482 jonathan 544 def GetDisplayText(self):
483 jonathan 602 assert False, "GetDisplay must be overridden by subclass!"
484 jonathan 544 return ""
485    
486 jonathan 436 def Matches(self, value):
487 jonathan 462 """Determines if this Group is associated with the given value.
488    
489 jonathan 479 Returns False. This needs to be overridden by all subclasses.
490 jonathan 462 """
491 jonathan 602 assert False, "GetMatches must be overridden by subclass!"
492 jonathan 479 return False
493 jonathan 436
494 jonathan 462 def GetProperties(self):
495     """Return the properties associated with the given value.
496    
497 jonathan 479 Returns None. This needs to be overridden by all subclasses.
498 jonathan 462 """
499 jonathan 602 assert False, "GetProperties must be overridden by subclass!"
500 jonathan 479 return None
501 jonathan 436
502 jonathan 410
503 jonathan 436 class ClassGroupSingleton(ClassGroup):
504 jonathan 462 """A Group that is associated with a single value."""
505 jonathan 410
506 jonathan 436 def __init__(self, value = 0, prop = None, label = ""):
507 jonathan 462 """Constructor.
508    
509     value -- the associated value.
510    
511     prop -- a ClassGroupProperites object. If prop is None a default
512     set of properties is created.
513    
514     label -- a label for this group.
515     """
516 jonathan 436 ClassGroup.__init__(self, label)
517 jonathan 410
518 jonathan 462 self.prop = None
519     self.value = None
520    
521 jonathan 436 self.SetValue(value)
522     self.SetProperties(prop)
523 jonathan 410
524 jonathan 436 def __copy__(self):
525 jonathan 479 return ClassGroupSingleton(self.GetValue(),
526     self.GetProperties(),
527     self.GetLabel())
528 jonathan 436
529 jonathan 484 def __deepcopy__(self, memo):
530     return ClassGroupSingleton(copy.copy(self.GetValue()),
531     copy.copy(self.GetProperties()),
532     copy.copy(self.GetLabel()))
533    
534 jonathan 410 def GetValue(self):
535 jonathan 462 """Return the associated value."""
536 jonathan 410 return self.value
537    
538     def SetValue(self, value):
539 jonathan 462 """Associate this Group with the given value."""
540 jonathan 410 self.value = value
541    
542 jonathan 436 def Matches(self, value):
543 jonathan 462 """Determine if the given value matches the associated Group value."""
544    
545     """Returns True if the value matches, False otherwise."""
546    
547 jonathan 436 return self.value == value
548 jonathan 410
549 jonathan 462 def GetProperties(self):
550     """Return the Properties associated with this Group."""
551 jonathan 410
552 jonathan 462 return self.prop
553 jonathan 410
554 jonathan 436 def SetProperties(self, prop):
555 jonathan 462 """Set the properties associated with this Group.
556    
557     prop -- a ClassGroupProperties object. if prop is None,
558     a default set of properties is created.
559     """
560    
561 jonathan 436 if prop is None: prop = ClassGroupProperties()
562 jonathan 602 assert isinstance(prop, ClassGroupProperties)
563 jonathan 436 self.prop = prop
564    
565 jonathan 544 def GetDisplayText(self):
566     label = self.GetLabel()
567    
568     if label != "": return label
569    
570     return str(self.GetValue())
571    
572 jonathan 479 def __eq__(self, other):
573     return isinstance(other, ClassGroupSingleton) \
574     and self.GetProperties() == other.GetProperties() \
575     and self.GetValue() == other.GetValue()
576 jonathan 436
577 jonathan 479 def __ne__(self, other):
578     return not self.__eq__(other)
579    
580     class ClassGroupDefault(ClassGroup):
581 jonathan 462 """The default Group. When values do not match any other
582     Group within a Classification, the properties from this
583     class are used."""
584    
585 jonathan 436 def __init__(self, prop = None, label = ""):
586 jonathan 462 """Constructor.
587    
588     prop -- a ClassGroupProperites object. If prop is None a default
589     set of properties is created.
590    
591     label -- a label for this group.
592     """
593    
594 jonathan 479 ClassGroup.__init__(self, label)
595     self.SetProperties(prop)
596 jonathan 436
597     def __copy__(self):
598 jonathan 479 return ClassGroupDefault(self.GetProperties(), self.GetLabel())
599 jonathan 436
600 jonathan 484 def __deepcopy__(self, memo):
601     return ClassGroupDefault(copy.copy(self.GetProperties()),
602     copy.copy(self.GetLabel()))
603    
604 jonathan 479 def Matches(self, value):
605     return True
606    
607 jonathan 462 def GetProperties(self):
608     """Return the Properties associated with this Group."""
609 jonathan 436 return self.prop
610    
611 jonathan 479 def SetProperties(self, prop):
612     """Set the properties associated with this Group.
613    
614     prop -- a ClassGroupProperties object. if prop is None,
615     a default set of properties is created.
616     """
617    
618     if prop is None: prop = ClassGroupProperties()
619 jonathan 602 assert isinstance(prop, ClassGroupProperties)
620 jonathan 479 self.prop = prop
621    
622 jonathan 544 def GetDisplayText(self):
623     label = self.GetLabel()
624    
625     if label != "": return label
626    
627     return "DEFAULT"
628    
629 jonathan 479 def __eq__(self, other):
630     return isinstance(other, ClassGroupDefault) \
631     and self.GetProperties() == other.GetProperties()
632    
633     def __ne__(self, other):
634     return not self.__eq__(other)
635    
636 jonathan 436 class ClassGroupRange(ClassGroup):
637 jonathan 462 """A Group that represents a range of values that map to the same
638     set of properties."""
639 jonathan 436
640     def __init__(self, min = 0, max = 1, prop = None, label = ""):
641 jonathan 462 """Constructor.
642    
643     The minumum value must be strictly less than the maximum.
644    
645     min -- the minimum range value
646    
647     max -- the maximum range value
648    
649     prop -- a ClassGroupProperites object. If prop is None a default
650     set of properties is created.
651    
652     label -- a label for this group.
653     """
654    
655 jonathan 436 ClassGroup.__init__(self, label)
656    
657 jonathan 462 self.min = self.max = 0
658     self.prop = None
659    
660 jonathan 410 self.SetRange(min, max)
661 jonathan 436 self.SetProperties(prop)
662 jonathan 410
663 jonathan 436 def __copy__(self):
664 jonathan 479 return ClassGroupRange(self.GetMin(),
665     self.GetMax(),
666     self.GetProperties(),
667     self.GetLabel())
668 jonathan 436
669 jonathan 484 def __deepcopy__(self, memo):
670     return ClassGroupRange(copy.copy(self.GetMin()),
671     copy.copy(self.GetMax()),
672     copy.copy(self.GetProperties()),
673     copy.copy(self.GetLabel()))
674    
675 jonathan 410 def GetMin(self):
676 jonathan 462 """Return the range's minimum value."""
677 jonathan 410 return self.min
678    
679     def SetMin(self, min):
680 jonathan 462 """Set the range's minimum value.
681    
682     min -- the new minimum. Note that this must be less than the current
683     maximum value. Use SetRange() to change both min and max values.
684     """
685    
686 jonathan 410 self.SetRange(min, self.max)
687    
688     def GetMax(self):
689 jonathan 462 """Return the range's maximum value."""
690 jonathan 410 return self.max
691    
692     def SetMax(self, max):
693 jonathan 462 """Set the range's maximum value.
694    
695     max -- the new maximum. Note that this must be greater than the current
696     minimum value. Use SetRange() to change both min and max values.
697     """
698 jonathan 410 self.SetRange(self.min, max)
699    
700     def SetRange(self, min, max):
701 jonathan 462 """Set a new range.
702    
703     Note that min must be strictly less than max.
704    
705     min -- the new minimum value
706     min -- the new maximum value
707     """
708    
709 jonathan 436 if min >= max:
710     raise ValueError(_("ClassGroupRange: %i(min) >= %i(max)!") %
711     (min, max))
712 jonathan 410 self.min = min
713     self.max = max
714    
715     def GetRange(self):
716 jonathan 462 """Return the range as a tuple (min, max)"""
717 jonathan 410 return (self.min, self.max)
718    
719 jonathan 436 def Matches(self, value):
720 jonathan 462 """Determine if the given value lies with the current range.
721    
722     The following check is used: min <= value < max.
723     """
724    
725 jonathan 410 return self.min <= value < self.max
726    
727 jonathan 462 def GetProperties(self):
728     """Return the Properties associated with this Group."""
729     return self.prop
730 jonathan 410
731 jonathan 462 def SetProperties(self, prop):
732     """Set the properties associated with this Group.
733 jonathan 436
734 jonathan 462 prop -- a ClassGroupProperties object. if prop is None,
735     a default set of properties is created.
736     """
737 jonathan 436 if prop is None: prop = ClassGroupProperties()
738 jonathan 602 assert isinstance(prop, ClassGroupProperties)
739 jonathan 436 self.prop = prop
740    
741 jonathan 544 def GetDisplayText(self):
742     label = self.GetLabel()
743    
744     if label != "": return label
745    
746     return _("%s - %s") % (self.GetMin(), self.GetMax())
747    
748 jonathan 479 def __eq__(self, other):
749     return isinstance(other, ClassGroupRange) \
750     and self.GetProperties() == other.GetProperties() \
751     and self.GetRange() == other.GetRange()
752    
753     def __ne__(self, other):
754     return not self.__eq__(other)
755    
756 jonathan 436 class ClassGroupMap(ClassGroup):
757 jonathan 462 """Currently, this class is not used."""
758 jonathan 436
759 jonathan 410 FUNC_ID = "id"
760    
761 jonathan 436 def __init__(self, map_type = FUNC_ID, func = None, prop = None, label=""):
762 jonathan 462 ClassGroup.__init__(self, label)
763 jonathan 410
764     self.map_type = map_type
765     self.func = func
766    
767     if self.func is None:
768     self.func = func_id
769    
770     def Map(self, value):
771     return self.func(value)
772    
773 jonathan 462 def GetProperties(self):
774     return None
775    
776     def GetPropertiesFromValue(self, value):
777     pass
778    
779 jonathan 544 def GetDisplayText(self):
780     return "Map: " + self.map_type
781    
782 jonathan 410 #
783     # built-in mappings
784     #
785     def func_id(value):
786     return value
787    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26