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