1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Provides a default implementation of an IItemSorter."""
17
18 from muntjac.data.util.item_sorter import IItemSorter
19
20
22 """Provides a default implementation of an IItemSorter. The
23 C{DefaultItemSorter} adheres to the L{ISortable.sort} rules and sorts
24 the container according to the properties given using L{setSortProperties}.
25
26 A Comparator is used for comparing the individual C{Property}
27 values. The comparator can be set using the constructor. If no comparator
28 is provided a default comparator is used.
29 """
30
31 - def __init__(self, propertyValueComparator=None):
32 """Constructs a DefaultItemSorter which uses the C{Comparator}
33 indicated by the C{propertyValueComparator} parameter for
34 comparing C{Property} values. Uses the default C{Comparator}
35 for comparing C{Property} values if propertyValueComparator is None.
36
37 @param propertyValueComparator:
38 The comparator to use when comparing individual
39 C{Property} values
40 """
41 self._sortPropertyIds = None
42 self._sortDirections = None
43 self._container = None
44 self._propertyValueComparator = None
45
46 if propertyValueComparator is None:
47 DefaultItemSorter.__init__(self, DefaultPropertyValueComparator())
48 else:
49 self._propertyValueComparator = propertyValueComparator
50
51
54
55
57 item1 = self._container.getItem(o1)
58 item2 = self._container.getItem(o2)
59
60
61
62 if item1 is None:
63 if item2 is None:
64 return 0
65 else:
66 return 1
67 elif item2 is None:
68 return -1
69
70 for i in range(len(self._sortPropertyIds)):
71 result = self.compareProperty(self._sortPropertyIds[i],
72 self._sortDirections[i], item1, item2)
73
74
75 if result != 0:
76 return result
77
78 return 0
79
80
82 """Compares the property indicated by C{propertyId} in the items
83 indicated by C{item1} and C{item2} for order. Returns a negative
84 integer, zero, or a positive integer as the property value in
85 the first item is less than, equal to, or greater than the property
86 value in the second item. If the C{sortDirection} is false the
87 returned value is negated.
88
89 The comparator set for this C{DefaultItemSorter} is used for
90 comparing the two property values.
91
92 @param propertyId:
93 The property id for the property that is used for comparison.
94 @param sortDirection:
95 The direction of the sort. A false value negates the result.
96 @param item1:
97 The first item to compare.
98 @param item2:
99 The second item to compare.
100 @return: a negative, zero, or positive integer if the property value in
101 the first item is less than, equal to, or greater than the
102 property value in the second item. Negated if
103 C{sortDirection} is false.
104 """
105 property1 = item1.getItemProperty(propertyId)
106 property2 = item2.getItemProperty(propertyId)
107
108
109 value1 = None if property1 is None else property1.getValue()
110 value2 = None if property2 is None else property2.getValue()
111
112
113 r = 0
114 if sortDirection:
115 r = self._propertyValueComparator.compare(value1, value2)
116 else:
117 r = self._propertyValueComparator.compare(value2, value1)
118
119 return r
120
121
123 self._container = container
124
125
126 ids = list()
127 orders = list()
128 sortable = container.getSortableContainerPropertyIds()
129
130 for i in range(len(propertyId)):
131 if propertyId[i] in sortable:
132 ids.append(propertyId[i])
133 order = bool(ascending[i]) if i < len(ascending) else True
134 orders.append(order)
135
136 self._sortPropertyIds = list(ids)
137 self._sortDirections = [None] * len(orders)
138
139 for i in range(len(self._sortDirections)):
140 self._sortDirections[i] = bool( orders[i] )
141
142
144 """Provides a default comparator used for comparing L{Property} values.
145 The C{DefaultPropertyValueComparator} assumes all objects it compares
146 can be cast to Comparable.
147 """
148
151
152
154 r = 0
155
156 if o1 is not None and o2 is not None:
157
158 r = cmp(o1, o2)
159 elif o1 == o2:
160
161 r = 0
162 elif o1 is None:
163
164 r = -1
165 else:
166
167 r = 1
168 return r
169