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