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 |
|
|
25 |
|
|
26 |
import copy |
import copy |
27 |
|
|
28 |
|
from Thuban import _ |
29 |
|
|
30 |
from types import * |
from types import * |
31 |
|
|
32 |
from messages import LAYER_PROJECTION_CHANGED, LAYER_LEGEND_CHANGED, \ |
from messages import \ |
33 |
LAYER_VISIBILITY_CHANGED |
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 |
|
|
39 |
import Thuban.Model.layer |
import Thuban.Model.layer |
40 |
|
|
|
# constants |
|
|
RANGE_MIN = 0 |
|
|
RANGE_MAX = 1 |
|
|
RANGE_DATA = 2 |
|
|
|
|
41 |
class Classification: |
class Classification: |
42 |
"""Encapsulates the classification of layer. The Classification |
"""Encapsulates the classification of layer. |
43 |
divides some kind of data into Groups which are associated with |
|
44 |
properties. Later the properties can be retrieved by matching |
The Classification divides some kind of data into Groups which |
45 |
data values to the appropriate group.""" |
are associated with properties. Later the properties can be |
46 |
|
retrieved by matching data values to the appropriate group. |
47 |
|
""" |
48 |
|
|
49 |
def __init__(self, layer = None, field = None): |
def __init__(self, layer = None, field = None): |
50 |
"""Initialize a classification. |
"""Initialize a classification. |
51 |
|
|
52 |
layer -- the Layer object who owns this classification |
layer -- the Layer object who owns this classification |
53 |
|
|
54 |
field -- the name of the data table field that |
field -- the name of the data table field that |
55 |
is to be used to classify layer properties |
is to be used to classify layer properties |
56 |
""" |
""" |
57 |
|
|
58 |
self.layer = None |
self.layer = None |
59 |
self.field = None |
self.field = None |
60 |
self.fieldType = None |
self.fieldType = None |
61 |
self.groups = [] |
self.__groups = [] |
62 |
|
|
63 |
self.__setLayerLock = False |
self.__setLayerLock = False |
64 |
|
|
68 |
self.SetField(field) |
self.SetField(field) |
69 |
|
|
70 |
def __iter__(self): |
def __iter__(self): |
71 |
return ClassIterator(self.groups) |
return ClassIterator(self.__groups) |
72 |
|
|
73 |
|
def __deepcopy__(self, memo): |
74 |
|
clazz = Classification() |
75 |
|
|
76 |
|
clazz.__groups[0] = copy.deepcopy(self.__groups[0]) |
77 |
|
|
78 |
|
for i in range(1, len(self.__groups)): |
79 |
|
clazz.__groups.append(copy.deepcopy(self.__groups[i])) |
80 |
|
|
81 |
|
print "Classification.__deepcopy__" |
82 |
|
return clazz |
83 |
|
|
84 |
def __SendNotification(self): |
def __SendNotification(self): |
85 |
"""Notify the layer that this class has changed.""" |
"""Notify the layer that this class has changed.""" |
89 |
def SetField(self, field): |
def SetField(self, field): |
90 |
"""Set the name of the data table field to use. |
"""Set the name of the data table field to use. |
91 |
|
|
92 |
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, |
93 |
otherwise the layer is queried to find the type of the |
otherwise the layer is queried to find the type of the |
94 |
field data |
field data |
95 |
|
|
96 |
field -- if None then all values map to the default data |
field -- if None then all values map to the default data |
97 |
""" |
""" |
98 |
|
|
99 |
if field == "": |
if field == "": |
146 |
def SetLayer(self, layer): |
def SetLayer(self, layer): |
147 |
"""Set the owning Layer of this classification. |
"""Set the owning Layer of this classification. |
148 |
|
|
149 |
A ValueError exception will be thrown either the field or |
A ValueError exception will be thrown either the field or |
150 |
field type mismatch the information in the layer's table. |
field type mismatch the information in the layer's table. |
151 |
""" |
""" |
152 |
|
|
153 |
# prevent infinite recursion when calling SetClassification() |
# prevent infinite recursion when calling SetClassification() |
182 |
"""Return the parent layer.""" |
"""Return the parent layer.""" |
183 |
return self.layer |
return self.layer |
184 |
|
|
|
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] |
|
185 |
|
|
186 |
# |
# |
187 |
# these SetDefault* methods are really only provided for |
# these SetDefault* methods are really only provided for |
228 |
"""Return the default line width.""" |
"""Return the default line width.""" |
229 |
return self.GetDefaultGroup().GetProperties().GetLineWidth() |
return self.GetDefaultGroup().GetProperties().GetLineWidth() |
230 |
|
|
231 |
def AddGroup(self, item): |
|
232 |
"""Add a new ClassGroup item to the classification. |
# |
233 |
|
# The methods that manipulate self.__groups have to be kept in |
234 |
|
# sync. We store the default group in index 0 to make it |
235 |
|
# convienent to iterate over the classification's groups, but |
236 |
|
# from the user's perspective the first (non-default) group is |
237 |
|
# at index 0 and the DefaultGroup is a special entity. |
238 |
|
# |
239 |
|
|
240 |
|
def SetDefaultGroup(self, group): |
241 |
|
"""Set the group to be used when a value can't be classified. |
242 |
|
|
243 |
|
group -- group that the value maps to. |
244 |
|
""" |
245 |
|
|
246 |
|
assert isinstance(group, ClassGroupDefault) |
247 |
|
if len(self.__groups) > 0: |
248 |
|
self.__groups[0] = group |
249 |
|
else: |
250 |
|
self.__groups.append(group) |
251 |
|
|
252 |
|
def GetDefaultGroup(self): |
253 |
|
"""Return the default group.""" |
254 |
|
return self.__groups[0] |
255 |
|
|
256 |
|
def AppendGroup(self, item): |
257 |
|
"""Append a new ClassGroup item to the classification. |
258 |
|
|
259 |
item -- this must be a valid ClassGroup object |
item -- this must be a valid ClassGroup object |
260 |
""" |
""" |
261 |
|
|
262 |
assert isinstance(item, ClassGroup) |
self.InsertGroup(self.GetNumGroups(), item) |
263 |
|
|
264 |
if len(self.groups) > 0 and isinstance(item, ClassGroupDefault): |
def InsertGroup(self, index, group): |
265 |
self.groups[0] = item |
|
266 |
else: |
assert isinstance(group, ClassGroup) |
267 |
self.groups.append(item) |
|
268 |
|
self.__groups.insert(index + 1, group) |
269 |
|
|
270 |
|
self.__SendNotification() |
271 |
|
|
272 |
|
def RemoveGroup(self, index): |
273 |
|
return self.__groups.pop(index + 1) |
274 |
|
|
275 |
|
def ReplaceGroup(self, index, group): |
276 |
|
assert isinstance(group, ClassGroup) |
277 |
|
|
278 |
|
self.__groups[index + 1] = group |
279 |
|
|
280 |
self.__SendNotification() |
self.__SendNotification() |
281 |
|
|
282 |
def GetGroup(self, value): |
def GetGroup(self, index): |
283 |
|
return self.__groups[index + 1] |
284 |
|
|
285 |
|
def GetNumGroups(self): |
286 |
|
"""Return the number of non-default groups in the classification.""" |
287 |
|
return len(self.__groups) - 1 |
288 |
|
|
289 |
|
|
290 |
|
def FindGroup(self, value): |
291 |
"""Return the associated group, or the default group. |
"""Return the associated group, or the default group. |
292 |
|
|
293 |
Groups are checked in the order the were added to the |
Groups are checked in the order the were added to the |
294 |
Classification. |
Classification. |
295 |
|
|
296 |
value -- the value to classify. If there is no mapping, |
value -- the value to classify. If there is no mapping, |
297 |
the field is None or value is None, |
the field is None or value is None, |
298 |
return the default properties |
return the default properties |
299 |
""" |
""" |
300 |
|
|
301 |
if self.GetField() is not None and value is not None: |
if self.GetField() is not None and value is not None: |
302 |
|
|
303 |
for i in range(1, len(self.groups)): |
for i in range(1, len(self.__groups)): |
304 |
group = self.groups[i] |
group = self.__groups[i] |
305 |
if group.Matches(value): |
if group.Matches(value): |
306 |
return group |
return group |
307 |
|
|
308 |
return self.GetDefaultGroup() |
return self.GetDefaultGroup() |
309 |
|
|
310 |
def GetProperties(self, value): |
def GetProperties(self, value): |
311 |
"""Return the properties associated with the given value.""" |
"""Return the properties associated with the given value. |
312 |
|
|
313 |
|
Use this function rather than Classification.FindGroup().GetProperties() |
314 |
|
since the returned group may be a ClassGroupMap which doesn't support |
315 |
|
a call to GetProperties(). |
316 |
|
""" |
317 |
|
|
318 |
group = self.GetGroup(value) |
group = self.FindGroup(value) |
319 |
if isinstance(group, ClassGroupMap): |
if isinstance(group, ClassGroupMap): |
320 |
return group.GetPropertiesFromValue(value) |
return group.GetPropertiesFromValue(value) |
321 |
else: |
else: |
350 |
return (label, i) |
return (label, i) |
351 |
|
|
352 |
for p in self: |
for p in self: |
353 |
if isinstance(p, ClassGroupDefault): |
items.append(build_item(p, p.GetDisplayText())) |
354 |
items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'"))) |
|
355 |
elif isinstance(p, ClassGroupSingleton): |
# if isinstance(p, ClassGroupDefault): |
356 |
items.append(build_item(p, str(p.GetValue()))) |
# items.append(build_item(self.GetDefaultGroup(), _("'DEFAULT'"))) |
357 |
elif isinstance(p, ClassGroupRange): |
# elif isinstance(p, ClassGroupSingleton): |
358 |
items.append(build_item(p, "%s - %s" % |
# items.append(build_item(p, str(p.GetValue()))) |
359 |
(p.GetMin(), p.GetMax()))) |
# elif isinstance(p, ClassGroupRange): |
360 |
|
# items.append(build_item(p, "%s - %s" % |
361 |
|
# (p.GetMin(), p.GetMax()))) |
362 |
|
|
363 |
return (_("Classification"), items) |
return (_("Classification"), items) |
364 |
|
|
674 |
|
|
675 |
if label != "": return label |
if label != "": return label |
676 |
|
|
677 |
return "DEFAULT" |
return _("DEFAULT") |
678 |
|
|
679 |
def __eq__(self, other): |
def __eq__(self, other): |
680 |
return isinstance(other, ClassGroupDefault) \ |
return isinstance(other, ClassGroupDefault) \ |