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