29 |
LAYER_LEGEND_CHANGED, \ |
LAYER_LEGEND_CHANGED, \ |
30 |
LAYER_VISIBILITY_CHANGED |
LAYER_VISIBILITY_CHANGED |
31 |
|
|
32 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color, Transparent, Black |
33 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
34 |
|
|
35 |
import Thuban.Model.layer |
import Thuban.Model.layer |
60 |
|
|
61 |
self.SetDefaultGroup(ClassGroupDefault()) |
self.SetDefaultGroup(ClassGroupDefault()) |
62 |
|
|
63 |
self.SetLayer(layer) |
self.SetFieldInfo(field, None) |
64 |
self.SetField(field) |
|
65 |
|
self._set_layer(layer) |
66 |
|
|
67 |
def __iter__(self): |
def __iter__(self): |
68 |
return ClassIterator(self.__groups) |
return ClassIterator(self.__groups) |
85 |
if self.layer is not None: |
if self.layer is not None: |
86 |
self.layer.ClassChanged() |
self.layer.ClassChanged() |
87 |
|
|
|
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() |
|
|
|
|
88 |
def GetField(self): |
def GetField(self): |
89 |
"""Return the name of the field.""" |
"""Return the name of the field.""" |
90 |
return self.field |
return self.field |
93 |
"""Return the field type.""" |
"""Return the field type.""" |
94 |
return self.fieldType |
return self.fieldType |
95 |
|
|
96 |
def SetFieldType(self, type): |
def SetFieldInfo(self, name, type): |
97 |
"""Set the type of the field used by this classification. |
"""Set the classified field name to 'name' and it's field |
98 |
|
type to 'type' |
99 |
|
|
100 |
|
If this classification has an owning layer a ValueError |
101 |
|
exception will be thrown either the field or field type |
102 |
|
mismatch the information in the layer's table. |
103 |
|
|
104 |
|
If the field info is successful set and the class has |
105 |
|
an owning layer, the layer will be informed that the |
106 |
|
classification has changed. |
107 |
|
""" |
108 |
|
|
109 |
|
if name == "": |
110 |
|
name = None |
111 |
|
|
112 |
|
if self.layer is None: |
113 |
|
self.field = name |
114 |
|
self.fieldType = type |
115 |
|
elif name is None: |
116 |
|
self.field = None |
117 |
|
self.fieldType = None |
118 |
|
else: |
119 |
|
# |
120 |
|
# verify that the field exists in the layer and that |
121 |
|
# the type is correct. |
122 |
|
# |
123 |
|
fieldType = self.layer.GetFieldType(name) |
124 |
|
if fieldType is None: |
125 |
|
raise ValueError("'%s' was not found in the layer's table." |
126 |
|
% self.field) |
127 |
|
elif type is not None and fieldType != type: |
128 |
|
raise ValueError("type doesn't match layer's field type for %s" |
129 |
|
% self.field) |
130 |
|
|
131 |
A ValueError is raised if the owning layer is not None and |
self.field = name |
132 |
'type' is different from the current field type. |
self.fieldType = fieldType |
|
""" |
|
133 |
|
|
134 |
if type != self.fieldType: |
self.__SendNotification() |
|
if self.layer is not None: |
|
|
raise ValueError() |
|
|
else: |
|
|
self.fieldType = type |
|
|
self.__SendNotification() |
|
135 |
|
|
136 |
def SetLayer(self, layer): |
def _set_layer(self, layer): |
137 |
"""Set the owning Layer of this classification. |
"""Internal: Set the owning Layer of this classification. |
138 |
|
|
139 |
A ValueError exception will be thrown either the field or |
A ValueError exception will be thrown either the field or |
140 |
field type mismatch the information in the layer's table. |
field type mismatch the information in the layer's table. |
|
""" |
|
|
|
|
|
# prevent infinite recursion when calling SetClassification() |
|
|
if self.__setLayerLock: return |
|
141 |
|
|
142 |
self.__setLayerLock = True |
If the layer is successful set, the layer will be informed |
143 |
|
that the classification has changed. |
144 |
|
""" |
145 |
|
|
146 |
if layer is None: |
if layer is None: |
147 |
if self.layer is not None: |
self.layer = None |
|
l = self.layer |
|
|
self.layer = None |
|
|
l.SetClassification(None) |
|
148 |
else: |
else: |
|
assert isinstance(layer, Thuban.Model.layer.Layer) |
|
|
|
|
149 |
old_layer = self.layer |
old_layer = self.layer |
|
|
|
150 |
self.layer = layer |
self.layer = layer |
151 |
|
|
152 |
try: |
try: |
153 |
self.SetField(self.GetField()) # this sync's the fieldType |
# this sync's the fieldType |
154 |
|
# and sends a notification to the layer |
155 |
|
self.SetFieldInfo(self.GetField(), None) |
156 |
except ValueError: |
except ValueError: |
157 |
self.layer = old_layer |
self.layer = old_layer |
|
self.__setLayerLock = False |
|
158 |
raise ValueError |
raise ValueError |
|
else: |
|
|
self.layer.SetClassification(self) |
|
|
|
|
|
self.__setLayerLock = False |
|
159 |
|
|
160 |
def GetLayer(self): |
def GetLayer(self): |
161 |
"""Return the parent layer.""" |
"""Return the parent layer.""" |
162 |
return self.layer |
return self.layer |
163 |
|
|
|
|
|
164 |
# |
# |
165 |
# these SetDefault* methods are really only provided for |
# these SetDefault* methods are really only provided for |
166 |
# some backward compatibility. they should be considered |
# some backward compatibility. they should be considered |
301 |
items = [] |
items = [] |
302 |
|
|
303 |
def build_color_item(text, color): |
def build_color_item(text, color): |
304 |
if color is Color.Transparent: |
if color is Transparent: |
305 |
return ("%s: %s" % (text, _("None")), None) |
return ("%s: %s" % (text, _("None")), None) |
306 |
|
|
307 |
return ("%s: (%.3f, %.3f, %.3f)" % |
return ("%s: (%.3f, %.3f, %.3f)" % |
398 |
|
|
399 |
props -- a ClassGroupProperties object. The class is copied if |
props -- a ClassGroupProperties object. The class is copied if |
400 |
prop is not None. Otherwise, a default set of properties |
prop is not None. Otherwise, a default set of properties |
401 |
is created such that: line color = Color.Black, line width = 1, |
is created such that: line color = Black, line width = 1, |
402 |
and fill color = Color.Transparent |
and fill color = Transparent |
403 |
""" |
""" |
404 |
|
|
405 |
#self.stroke = None |
#self.stroke = None |
409 |
if props is not None: |
if props is not None: |
410 |
self.SetProperties(props) |
self.SetProperties(props) |
411 |
else: |
else: |
412 |
self.SetLineColor(Color.Black) |
self.SetLineColor(Black) |
413 |
self.SetLineWidth(1) |
self.SetLineWidth(1) |
414 |
self.SetFill(Color.Transparent) |
self.SetFill(Transparent) |
415 |
|
|
416 |
def SetProperties(self, props): |
def SetProperties(self, props): |
417 |
"""Set this class's properties to those in class props.""" |
"""Set this class's properties to those in class props.""" |