Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / objectselection.py
1 from kofoto.gkofoto.environment import env
2
3 class ObjectSelection:
4     def __init__(self, objectCollection):
5         # Don't forget to update this class when the model is reordered or
6         # when rows are removed or added.
7         self.__selectedObjects = {}
8         # When objects are stored in self.__selectedObjects, the key MUST be
9         # the location in the UNSORTED model since this class is not
10         # notified when/if the model is re-sorted.
11         #
12         # This class must know about each object's row to be able to distinguish
13         # individual objects in an album that contains multiple instances
14         # of the same image or album.
15         self.__changedCallbacks = set()
16         self.__objectCollection = objectCollection
17         self.addChangedCallback(self._nrOfSelectedObjectsChanged)
18
19     def addChangedCallback(self, callback):
20         self.__changedCallbacks.add(callback)
21
22     def removeChangedCallback(self, callback):
23         self.__changedCallbacks.remove(callback)
24
25     def unselectAll(self, notify=True):
26         self.__selectedObjects.clear()
27         if notify:
28             self.__invokeChangedCallbacks()
29
30     def setSelection(self, rowNrs, notify=True):
31         self.__selectedObjects.clear()
32         for rowNr in rowNrs:
33             self.addSelection(rowNr, False)
34         if notify:
35             self.__invokeChangedCallbacks()
36
37     def addSelection(self, rowNr, notify=True):
38         unsortedRowNr = self.__objectCollection.convertToUnsortedRowNr(rowNr)
39         self.__selectedObjects[unsortedRowNr] = self.__getObject(unsortedRowNr)
40         if notify:
41             self.__invokeChangedCallbacks()
42
43     def removeSelection(self, rowNr, notify=True):
44         unsortedRowNr = self.__objectCollection.convertToUnsortedRowNr(rowNr)
45         del self.__selectedObjects[unsortedRowNr]
46         if notify:
47             self.__invokeChangedCallbacks()
48
49     def getSelectedObjects(self):
50         conv = self.__objectCollection.convertFromUnsortedRowNr
51         items = self.__selectedObjects.items()
52         items.sort(key=lambda x: conv(x[0]))
53         return [x[1] for x in items]
54
55     def getLowestSelectedRowNr(self):
56         rowNrs = sorted(self)
57         if (len(rowNrs) > 0):
58             return rowNrs[0]
59         else:
60             return None
61
62     def getMap(self):
63         return self.__selectedObjects
64
65     def getImageFilenamesToPreload(self):
66         oc = self.__objectCollection
67         model = oc.getModel()
68         if len(self) == 0:
69             rowNr = 0
70         else:
71             rowNumbers = sorted(self)
72             rowNr = rowNumbers[0]
73         filenames = []
74         for x in [rowNr - 2, rowNr + 2, rowNr - 1, rowNr + 1]: # TODO: Make configurable.
75             if 0 <= x < len(model):
76                 ux = oc.convertToUnsortedRowNr(x)
77                 obj = self.__getObject(ux)
78                 if not obj.isAlbum():
79                     imageversion = obj.getPrimaryVersion()
80                     if imageversion:
81                         filenames.append(imageversion.getLocation())
82         env.debug("filenames to preload: %s" % str(filenames))
83         return filenames
84
85     def _nrOfSelectedObjectsChanged(self, objectSelection):
86         env.widgets["statusbarSelectedObjects"].pop(1)
87         env.widgets["statusbarSelectedObjects"].push(
88             1, "%d selected" % len(objectSelection))
89
90     def __contains__(self, rowNr):
91         unsortedRowNr = self.__objectCollection.convertToUnsortedRowNr(rowNr)
92         return unsortedRowNr in self.__selectedObjects.keys()
93
94     def __len__(self):
95         return len(self.__selectedObjects)
96
97     def __iter__(self):
98         for unsortedRowNr in self.__selectedObjects.keys():
99             rowNr = self.__objectCollection.convertFromUnsortedRowNr(unsortedRowNr)
100             yield rowNr
101
102     def __getitem__(self, rowNr):
103         unsortedRowNr = self.__objectCollection.convertToUnsortedRowNr(rowNr)
104         return self.__selectedObjects[unsortedRowNr]
105
106     def __invokeChangedCallbacks(self):
107         env.debug("Invoking selection changed callbacks: " + str(self.__selectedObjects.keys()))
108         for callback in self.__changedCallbacks:
109             callback(self)
110
111     def __getObject(self, unsortedRowNr):
112         objectId = self.__objectCollection.getUnsortedModel()[unsortedRowNr][self.__objectCollection.COLUMN_OBJECT_ID]
113         return env.shelf.getObject(objectId)