Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / albummembers.py
1 from kofoto.gkofoto.objectcollection import ObjectCollection
2 from kofoto.gkofoto.environment import env
3
4 class AlbumMembers(ObjectCollection):
5
6 ######################################################################
7 ### Public functions and constants
8
9     def __init__(self):
10         env.debug("Init AlbumMembers")
11         ObjectCollection.__init__(self)
12         self.__album = None
13
14     def loadAlbum(self, album):
15         env.debug("Loading album: " + album.getTag())
16         self.__album = album
17         self._loadObjectList(album.getChildren())
18
19     def isReorderable(self):
20         return self.__album and self.__album.isMutable()
21
22     def isMutable(self):
23         return (self.__album and
24                 self.__album.isMutable() and
25                 not self.isLoading())
26
27     def getContainer(self):
28         return self.__album
29
30     def cut(self, *unused):
31         self.copy()
32         self.delete()
33
34     def paste(self, *unused):
35         # This method assumes that self.getModel() returns an unsorted
36         # and mutable model.
37         self._freezeViews()
38         locations = list(self.getObjectSelection())
39         newObjects = list(env.clipboard)
40         albumCopied = False
41         for obj in newObjects:
42             if obj.isAlbum():
43                 albumCopied = True
44                 break
45         currentChildren = list(self.__album.getChildren())
46         if len(locations) > 0:
47             locations.sort()
48             insertLocation = locations[0]
49         else:
50             # Insert last.
51             insertLocation = len(currentChildren)
52         self.__album.setChildren(currentChildren[:insertLocation] +
53                                  newObjects +
54                                  currentChildren[insertLocation:])
55         self._insertObjectList(newObjects, insertLocation)
56         if albumCopied:
57             # TODO: Don't reload the whole tree.
58             env.mainwindow.reloadAlbumTree()
59         self.getObjectSelection().unselectAll()
60         self._thawViews()
61
62     def delete(self, *unused):
63         # This method assumes that self.getModel() returns an unsorted
64         # and mutable model
65         model = self.getModel()
66         self._freezeViews()
67         albumMembers = list(self.__album.getChildren())
68         albumDeleted = False
69         for loc in sorted(self.getObjectSelection(), reverse=True):
70             if albumMembers[loc].isAlbum():
71                 albumDeleted = True
72             albumMembers.pop(loc)
73             del model[loc]
74         self.__album.setChildren(albumMembers)
75         self.getObjectSelection().unselectAll()
76         if albumDeleted:
77             # TODO: Don't reload the whole tree.
78             env.mainwindow.reloadAlbumTree()
79         self._thawViews()
80
81 ######################################################################
82 ### Private functions