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 |
|
24 |
|
|
25 |
from Thuban import _ |
from Thuban import _ |
26 |
|
|
|
from types import * |
|
|
|
|
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 |
34 |
|
|
35 |
import Thuban.Model.layer |
import Thuban.Model.layer |
36 |
|
|
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) |
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 |
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 |
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 |
|
|
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 |
|
|
194 |
|
|
195 |
lineWidth -- an integer > 0. |
lineWidth -- an integer > 0. |
196 |
""" |
""" |
197 |
assert isinstance(lineWidth, IntType) |
assert isinstance(lineWidth, types.IntType) |
198 |
self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth) |
self.GetDefaultGroup().GetProperties().SetLineWidth(lineWidth) |
199 |
self.__SendNotification() |
self.__SendNotification() |
200 |
|
|
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)" % |
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 |
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.""" |
421 |
|
|
422 |
def GetLineColor(self): |
def GetLineColor(self): |
423 |
"""Return the line color as a Color object.""" |
"""Return the line color as a Color object.""" |
424 |
return self.stroke |
return self.__stroke |
425 |
|
|
426 |
def SetLineColor(self, color): |
def SetLineColor(self, color): |
427 |
"""Set the line color. |
"""Set the line color. |
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 |
|
|
432 |
assert isinstance(color, Color) |
self.__stroke = color |
|
self.stroke = color |
|
433 |
|
|
434 |
def GetLineWidth(self): |
def GetLineWidth(self): |
435 |
"""Return the line width.""" |
"""Return the line width.""" |
436 |
return self.strokeWidth |
return self.__strokeWidth |
437 |
|
|
438 |
def SetLineWidth(self, lineWidth): |
def SetLineWidth(self, lineWidth): |
439 |
"""Set the line width. |
"""Set the line width. |
440 |
|
|
441 |
lineWidth -- the new line width. This must be > 0. |
lineWidth -- the new line width. This must be > 0. |
442 |
""" |
""" |
443 |
assert isinstance(lineWidth, IntType) |
assert isinstance(lineWidth, types.IntType) |
444 |
if (lineWidth < 1): |
if (lineWidth < 1): |
445 |
raise ValueError(_("lineWidth < 1")) |
raise ValueError(_("lineWidth < 1")) |
446 |
|
|
447 |
self.strokeWidth = lineWidth |
self.__strokeWidth = lineWidth |
448 |
|
|
449 |
def GetFill(self): |
def GetFill(self): |
450 |
"""Return the fill color as a Color object.""" |
"""Return the fill color as a Color object.""" |
451 |
return self.fill |
return self.__fill |
452 |
|
|
453 |
def SetFill(self, fill): |
def SetFill(self, fill): |
454 |
"""Set the fill color. |
"""Set the fill color. |
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 |
|
|
459 |
assert isinstance(fill, Color) |
self.__fill = fill |
|
self.fill = fill |
|
460 |
|
|
461 |
def __eq__(self, other): |
def __eq__(self, other): |
462 |
"""Return true if 'props' has the same attributes as this class""" |
"""Return true if 'props' has the same attributes as this class""" |
463 |
|
|
464 |
|
# |
465 |
|
# using 'is' over '==' results in a huge performance gain |
466 |
|
# in the renderer |
467 |
|
# |
468 |
return isinstance(other, ClassGroupProperties) \ |
return isinstance(other, ClassGroupProperties) \ |
469 |
and self.stroke == other.GetLineColor() \ |
and (self.__stroke is other.__stroke or \ |
470 |
and self.strokeWidth == other.GetLineWidth() \ |
self.__stroke == other.__stroke) \ |
471 |
and self.fill == other.GetFill() |
and (self.__fill is other.__fill or \ |
472 |
|
self.__fill == other.__fill) \ |
473 |
|
and self.__strokeWidth == other.__strokeWidth |
474 |
|
|
475 |
def __ne__(self, other): |
def __ne__(self, other): |
476 |
return not self.__eq__(other) |
return not self.__eq__(other) |
481 |
def __deepcopy__(self): |
def __deepcopy__(self): |
482 |
return ClassGroupProperties(self) |
return ClassGroupProperties(self) |
483 |
|
|
484 |
|
def __repr__(self): |
485 |
|
return repr((self.__stroke, self.__strokeWidth, self.__fill)) |
486 |
|
|
487 |
class ClassGroup: |
class ClassGroup: |
488 |
"""A base class for all Groups within a Classification""" |
"""A base class for all Groups within a Classification""" |
489 |
|
|
512 |
label -- a string representing the Group's label. This must |
label -- a string representing the Group's label. This must |
513 |
not be None. |
not be None. |
514 |
""" |
""" |
515 |
assert isinstance(label, StringType) |
assert isinstance(label, types.StringTypes) |
516 |
self.label = label |
self.label = label |
517 |
|
|
518 |
def GetDisplayText(self): |
def GetDisplayText(self): |
551 |
|
|
552 |
def __eq__(self, other): |
def __eq__(self, other): |
553 |
return isinstance(other, ClassGroup) \ |
return isinstance(other, ClassGroup) \ |
554 |
|
and self.label == other.label \ |
555 |
and self.GetProperties() == other.GetProperties() |
and self.GetProperties() == other.GetProperties() |
556 |
|
|
557 |
def __ne__(self, other): |
def __ne__(self, other): |
558 |
return not self.__eq__(other) |
return not self.__eq__(other) |
559 |
|
|
560 |
|
def __repr__(self): |
561 |
|
return repr(self.label) + ", " + repr(self.GetProperties()) |
562 |
|
|
563 |
class ClassGroupSingleton(ClassGroup): |
class ClassGroupSingleton(ClassGroup): |
564 |
"""A Group that is associated with a single value.""" |
"""A Group that is associated with a single value.""" |
587 |
|
|
588 |
def GetValue(self): |
def GetValue(self): |
589 |
"""Return the associated value.""" |
"""Return the associated value.""" |
590 |
return self.value |
return self.__value |
591 |
|
|
592 |
def SetValue(self, value): |
def SetValue(self, value): |
593 |
"""Associate this Group with the given value.""" |
"""Associate this Group with the given value.""" |
594 |
self.value = value |
self.__value = value |
595 |
|
|
596 |
def Matches(self, value): |
def Matches(self, value): |
597 |
"""Determine if the given value matches the associated Group value.""" |
"""Determine if the given value matches the associated Group value.""" |
598 |
|
|
599 |
"""Returns True if the value matches, False otherwise.""" |
"""Returns True if the value matches, False otherwise.""" |
600 |
|
|
601 |
return self.value == value |
return self.__value == value |
602 |
|
|
603 |
def GetDisplayText(self): |
def GetDisplayText(self): |
604 |
label = self.GetLabel() |
label = self.GetLabel() |
610 |
def __eq__(self, other): |
def __eq__(self, other): |
611 |
return ClassGroup.__eq__(self, other) \ |
return ClassGroup.__eq__(self, other) \ |
612 |
and isinstance(other, ClassGroupSingleton) \ |
and isinstance(other, ClassGroupSingleton) \ |
613 |
and self.GetValue() == other.GetValue() |
and self.__value == other.__value |
614 |
|
|
615 |
|
def __repr__(self): |
616 |
|
return "(" + repr(self.__value) + ", " + ClassGroup.__repr__(self) + ")" |
617 |
|
|
618 |
class ClassGroupDefault(ClassGroup): |
class ClassGroupDefault(ClassGroup): |
619 |
"""The default Group. When values do not match any other |
"""The default Group. When values do not match any other |
652 |
and isinstance(other, ClassGroupDefault) \ |
and isinstance(other, ClassGroupDefault) \ |
653 |
and self.GetProperties() == other.GetProperties() |
and self.GetProperties() == other.GetProperties() |
654 |
|
|
655 |
|
def __repr__(self): |
656 |
|
return "(" + ClassGroup.__repr__(self) + ")" |
657 |
|
|
658 |
class ClassGroupRange(ClassGroup): |
class ClassGroupRange(ClassGroup): |
659 |
"""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 |
660 |
set of properties.""" |
set of properties.""" |
661 |
|
|
662 |
def __init__(self, min = 0, max = 1, props = None, label = "", group=None): |
def __init__(self, _range = (0,1), props = None, label = "", group=None): |
663 |
"""Constructor. |
"""Constructor. |
664 |
|
|
665 |
The minumum value must be strictly less than the maximum. |
The minumum value must be strictly less than the maximum. |
666 |
|
|
667 |
min -- the minimum range value |
_range -- either a tuple (min, max) where min < max or |
668 |
|
a Range object |
|
max -- the maximum range value |
|
669 |
|
|
670 |
prop -- a ClassGroupProperites object. If prop is None a default |
prop -- a ClassGroupProperites object. If prop is None a default |
671 |
set of properties is created. |
set of properties is created. |
673 |
label -- a label for this group. |
label -- a label for this group. |
674 |
""" |
""" |
675 |
|
|
676 |
ClassGroup.__init__(self, label, props, None) |
ClassGroup.__init__(self, label, props, group) |
|
|
|
|
self.min = self.max = 0 |
|
677 |
|
|
678 |
self.SetRange(min, max) |
#self.__min = self.__max = 0 |
679 |
|
#self.__range = Range("[" + repr(float(min)) + ";" + |
680 |
|
#repr(float(max)) + "[") |
681 |
|
self.SetRange(_range) |
682 |
|
|
683 |
def __copy__(self): |
def __copy__(self): |
684 |
return ClassGroupRange(self.GetMin(), |
return ClassGroupRange(self.__range, |
685 |
self.GetMax(), |
props = self.GetProperties(), |
686 |
self.GetProperties(), |
label = self.GetLabel()) |
|
self.GetLabel()) |
|
687 |
|
|
688 |
def __deepcopy__(self, memo): |
def __deepcopy__(self, memo): |
689 |
return ClassGroupRange(copy.copy(self.GetMin()), |
return ClassGroupRange(copy.copy(self.__range), |
|
copy.copy(self.GetMax()), |
|
690 |
group = self) |
group = self) |
691 |
|
|
692 |
def GetMin(self): |
def GetMin(self): |
693 |
"""Return the range's minimum value.""" |
"""Return the range's minimum value.""" |
694 |
return self.min |
return self.__range.GetRange()[1] |
695 |
|
|
696 |
def SetMin(self, min): |
def SetMin(self, min): |
697 |
"""Set the range's minimum value. |
"""Set the range's minimum value. |
700 |
maximum value. Use SetRange() to change both min and max values. |
maximum value. Use SetRange() to change both min and max values. |
701 |
""" |
""" |
702 |
|
|
703 |
self.SetRange(min, self.max) |
self.SetRange((min, self.__range.GetRange()[2])) |
704 |
|
|
705 |
def GetMax(self): |
def GetMax(self): |
706 |
"""Return the range's maximum value.""" |
"""Return the range's maximum value.""" |
707 |
return self.max |
return self.__range.GetRange()[2] |
708 |
|
|
709 |
def SetMax(self, max): |
def SetMax(self, max): |
710 |
"""Set the range's maximum value. |
"""Set the range's maximum value. |
712 |
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 |
713 |
minimum value. Use SetRange() to change both min and max values. |
minimum value. Use SetRange() to change both min and max values. |
714 |
""" |
""" |
715 |
self.SetRange(self.min, max) |
self.SetRange((self.__range.GetRange()[1], max)) |
716 |
|
|
717 |
def SetRange(self, min, max): |
def SetRange(self, _range): |
718 |
"""Set a new range. |
"""Set a new range. |
719 |
|
|
720 |
Note that min must be strictly less than max. |
_range -- Either a tuple (min, max) where min < max or |
721 |
|
a Range object. |
722 |
|
|
723 |
min -- the new minimum value |
Raises ValueError on error. |
|
min -- the new maximum value |
|
724 |
""" |
""" |
725 |
|
|
726 |
if min >= max: |
if isinstance(_range, Range): |
727 |
raise ValueError(_("ClassGroupRange: %i(min) >= %i(max)!") % |
self.__range = _range |
728 |
(min, max)) |
elif isinstance(_range, types.TupleType) and len(_range) == 2: |
729 |
self.min = min |
self.__range = Range(("[", _range[0], _range[1], "[")) |
730 |
self.max = max |
else: |
731 |
|
raise ValueError() |
732 |
|
|
733 |
def GetRange(self): |
def GetRange(self): |
734 |
"""Return the range as a tuple (min, max)""" |
"""Return the range as a string""" |
735 |
return (self.min, self.max) |
#return (self.__min, self.__max) |
736 |
|
return self.__range.string(self.__range.GetRange()) |
737 |
|
|
738 |
def Matches(self, value): |
def Matches(self, value): |
739 |
"""Determine if the given value lies with the current range. |
"""Determine if the given value lies with the current range. |
741 |
The following check is used: min <= value < max. |
The following check is used: min <= value < max. |
742 |
""" |
""" |
743 |
|
|
744 |
return self.min <= value < self.max |
return operator.contains(self.__range, value) |
745 |
|
#return self.__min <= value < self.__max |
746 |
|
|
747 |
def GetDisplayText(self): |
def GetDisplayText(self): |
748 |
label = self.GetLabel() |
label = self.GetLabel() |
749 |
|
|
750 |
if label != "": return label |
if label != "": return label |
751 |
|
|
752 |
return _("%s - %s") % (self.GetMin(), self.GetMax()) |
#return _("%s - %s") % (self.GetMin(), self.GetMax()) |
753 |
|
#return repr(self.__range) |
754 |
|
return self.__range.string(self.__range.GetRange()) |
755 |
|
|
756 |
def __eq__(self, other): |
def __eq__(self, other): |
757 |
return ClassGroup.__eq__(self, other) \ |
return ClassGroup.__eq__(self, other) \ |
758 |
and isinstance(other, ClassGroupRange) \ |
and isinstance(other, ClassGroupRange) \ |
759 |
and self.GetRange() == other.GetRange() |
and self.__range == other.__range |
760 |
|
#and self.__min == other.__min \ |
761 |
|
#and self.__max == other.__max |
762 |
|
|
763 |
|
def __repr__(self): |
764 |
|
return "(" + str(self.__range) + ClassGroup.__repr__(self) + ")" |
765 |
|
#return "(" + repr(self.__min) + ", " + repr(self.__max) + ", " + \ |
766 |
|
#ClassGroup.__repr__(self) + ")" |
767 |
|
|
768 |
class ClassGroupMap(ClassGroup): |
class ClassGroupMap(ClassGroup): |
769 |
"""Currently, this class is not used.""" |
"""Currently, this class is not used.""" |