Some changes to be more compatible with Python 3 in the future
[joel/kofoto.git] / src / packages / kofoto / gkofoto / sortableobjectcollection.py
1 import gtk
2 from kofoto.gkofoto.environment import env
3 from kofoto.gkofoto.mysortedmodel import MySortedModel
4 from kofoto.gkofoto.objectcollection import ObjectCollection
5
6 def attributeSortFunc(model, iterA, iterB, column):
7     valueA = model.get_value(iterA, column)
8     valueB = model.get_value(iterB, column)
9     try:
10         result = cmp(float(valueA), float(valueB))
11     except (ValueError, TypeError):
12         result = cmp(valueA, valueB)
13     if result == 0:
14         result = cmp(model.get_value(iterA, ObjectCollection.COLUMN_OBJECT_ID),
15                      model.get_value(iterB, ObjectCollection.COLUMN_OBJECT_ID))
16     return result
17
18 class SortableObjectCollection(ObjectCollection):
19
20 ######################################################################
21 ### Public
22
23     def __init__(self):
24         ObjectCollection.__init__(self)
25         self.__sortOrder = None
26         self.__sortColumnName = None
27         self.__sortedTreeModel = MySortedModel(self.getUnsortedModel())
28         self.setSortOrder(order=gtk.SORT_ASCENDING)
29         self.setSortColumnName(columnName=env.defaultSortColumn)
30
31     def isSortable(self):
32         return True
33
34     def isReorderable(self):
35         return False
36
37     def getModel(self):
38         return self.__sortedTreeModel
39
40     def getUnsortedModel(self):
41         return ObjectCollection.getModel(self)
42
43     def convertToUnsortedRowNr(self, rowNr):
44         return self.__sortedTreeModel.convert_path_to_child_path(rowNr)[0]
45
46     def convertFromUnsortedRowNr(self, unsortedRowNr):
47         return self.__sortedTreeModel. convert_child_path_to_path(unsortedRowNr)[0]
48
49     def getSortOrder(self):
50         return self.__sortOrder
51
52     def getSortColumnName(self):
53         return self.__sortColumnName
54
55     def setSortOrder(self, widget=None, order=None):
56         if widget != None and not widget.get_active():
57             # ignore the callback when the radio menu item is unselected
58             return
59         if self.__sortOrder != order:
60             env.debug("Setting sort order to: " + str(order))
61             self.__sortOrder = order
62             self.__configureSortedModel(self.__sortColumnName, self.__sortOrder)
63             self.__emitSortOrderChanged()
64
65     def setSortColumnName(self, widget=None, columnName=None):
66         if widget != None and not widget.get_active():
67             # ignore the callback when the radio menu item is unselected
68             return
69         if self.__sortColumnName != columnName:
70             if not columnName in self.getObjectMetadataMap():
71                 columnName = "id"
72             env.debug("Setting sort column to: " + columnName)
73             self.__sortColumnName = columnName
74             self.__configureSortedModel(self.__sortColumnName, self.__sortOrder)
75             self.__emitSortColumnChanged()
76
77     def registerView(self, view):
78         ObjectCollection.registerView(self, view)
79         self.__emitSortOrderChanged()
80         self.__emitSortColumnChanged()
81
82     def __emitSortOrderChanged(self):
83         for view in self._getRegisteredViews():
84             view.sortOrderChanged(self.__sortOrder)
85
86     def __emitSortColumnChanged(self):
87         for view in self._getRegisteredViews():
88             view.sortColumnChanged(self.__sortColumnName)
89
90     def __configureSortedModel(self, sortColumnName, sortOrder):
91         if (sortOrder != None and sortColumnName != None):
92             metaDataMap = self.getObjectMetadataMap()
93             if not sortColumnName in metaDataMap:
94                 sortColumnName = u"id"
95             sortColumnNr = metaDataMap[sortColumnName][self.COLUMN_NR]
96             model = self.getModel()
97             model.set_sort_column_id(sortColumnNr, self.__sortOrder)
98             # It is important that the attributeSortFunc is not an class member method,
99             # otherwise we are leaking memmory.
100             model.set_sort_func(sortColumnNr,
101                                 attributeSortFunc,
102                                 sortColumnNr)