PEP8-ified raise clauses.
[joel/kofoto.git] / src / packages / kofoto / gkofoto / thumbnailview.py
1 import gtk
2 from kofoto.gkofoto.objectcollectionview import ObjectCollectionView
3 from kofoto.gkofoto.objectcollection import ObjectCollection
4 from kofoto.gkofoto.environment import env
5
6 class ThumbnailView(ObjectCollectionView):
7
8 ###############################################################################
9 ### Public
10
11     def __init__(self):
12         env.debug("Init ThumbnailView")
13 ##        ObjectCollectionView.__init__(self,
14 ##                                      env.widgets["thumbnailList"])
15         ObjectCollectionView.__init__(self,
16                                       env.widgets["thumbnailView"])
17         self.__currentMaxWidth = env.thumbnailSize[0]
18         self.__selectionLocked = False
19 ##        self._viewWidget.connect("select_icon", self._widgetIconSelected)
20 ##        self._viewWidget.connect("unselect_icon", self._widgetIconUnselected)
21
22     def importSelection(self, objectSelection):
23         if not self.__selectionLocked:
24             env.debug("ThumbnailView is importing selection.")
25             self.__selectionLocked = True
26             self._viewWidget.unselect_all()
27             for rowNr in objectSelection:
28                 self._viewWidget.select_icon(rowNr)
29             self.__selectionLocked = False
30         self._updateContextMenu()
31
32     def _showHelper(self):
33         env.enter("ThumbnailView.showHelper()")
34         env.widgets["thumbnailView"].show()
35         self._viewWidget.grab_focus()
36         self.__scrollToFirstSelectedObject()
37         env.exit("ThumbnailView.showHelper()")
38
39     def _hideHelper(self):
40         env.enter("ThumbnailView.hideHelper()")
41         env.widgets["thumbnailView"].hide()
42         env.exit("ThumbnailView.hideHelper()")
43
44     def _connectObjectCollectionHelper(self):
45         env.enter("Connecting ThumbnailView to object collection")
46         # The model is loaded in thawHelper instead.
47         env.exit("Connecting ThumbnailView to object collection")
48
49     def _disconnectObjectCollectionHelper(self):
50         env.enter("Disconnecting ThumbnailView from object collection")
51         # The model is unloaded in freezeHelper instead.
52         env.exit("Disconnecting ThumbnailView from object collection")
53
54     def _freezeHelper(self):
55         env.enter("ThumbnailView.freezeHelper()")
56         self._clearAllConnections()
57         self._viewWidget.clear()
58         env.exit("ThumbnailView.freezeHelper()")
59
60     def _thawHelper(self):
61         env.enter("ThumbnailView.thawHelper()")
62         model = self._objectCollection.getModel()
63         for row in model:
64             self.__loadRow(row)
65         self._connect(model, "row_inserted",   self._rowInserted_cb)
66         self._connect(model, "row_deleted",    self._rowDeleted_cb)
67         self._connect(model, "rows_reordered", self._rowsReordered_cb)
68         self._connect(model, "row_changed",    self._rowChanged_cb)
69         env.exit("ThumbnailView.thawHelper()")
70
71 ###############################################################################
72 ### Callback functions registered by this class but invoked from other classes.
73
74     def _rowChanged_cb(self, model, path, iterator):
75         env.debug("ThumbnailView row changed.")
76         self.__selectionLocked = True
77         self._viewWidget.remove(path[0])
78         # For some reason that I don't understand model[path].path != path
79         # Hence we pass by path as the location where the icon shall be
80         # inserted.
81         self.__loadRow(model[path[0]], path[0])
82         if path[0] in self._objectCollection.getObjectSelection():
83             self._viewWidget.select_icon(path[0])
84         self.__selectionLocked = False
85
86     def _rowInserted_cb(self, model, path, iterator):
87         env.debug("ThumbnailView row inserted.")
88         self.__loadRow(model[path])
89
90     def _rowsReordered_cb(self, model, b, c, d):
91         env.debug("ThumbnailView rows reordered.")
92         # TODO I Don't know how to parse which rows that has
93         #      been reordered. Hence I must reload all rows.
94         self._viewWidget.clear()
95         for row in self._objectCollection.getModel():
96             self.__loadRow(row)
97         self.importSelection(self._objectCollection.getObjectSelection())
98
99     def _rowDeleted_cb(self, model, path):
100         env.debug("ThumbnailView row deleted.")
101         self._viewWidget.remove(path[0])
102
103     def _widgetIconSelected_cb(self, widget, index, event):
104         if not self.__selectionLocked:
105             env.enter("ThumbnailView selection changed")
106             self.__selectionLocked = True
107             self._objectCollection.getObjectSelection().addSelection(index)
108             self.__selectionLocked = False
109
110     def _widgetIconUnselected_cb(self, widget, index, event):
111         if not self.__selectionLocked:
112             env.enter("ThumbnailView selection changed")
113             self.__selectionLocked = True
114             self._objectCollection.getObjectSelection().removeSelection(index)
115             self.__selectionLocked = False
116
117 ###############################################################################
118 ### Private
119
120     def __loadRow(self, row, location=None):
121         if location is None:
122             location = row.path[0]
123         if row[ObjectCollection.COLUMN_IS_ALBUM]:
124             text = row[ObjectCollection.COLUMN_ALBUM_TAG]
125         else:
126             # TODO Let configuration decide what to show...
127             text = row[ObjectCollection.COLUMN_OBJECT_ID]
128         pixbuf = row[ObjectCollection.COLUMN_THUMBNAIL]
129         if pixbuf == None:
130             # It is possible that we get the row inserted event before
131             # the thumbnail is loaded. The temporary icon will be removed
132             # when we receive the row changed event.
133             pixbuf = env.loadingPixbuf
134         self._viewWidget.insert_pixbuf(location, pixbuf, "", str(text))
135         self.__currentMaxWidth = max(self.__currentMaxWidth, pixbuf.get_width())
136         self._viewWidget.set_icon_width(self.__currentMaxWidth)
137
138     def __scrollToFirstSelectedObject(self):
139         numberOfIcons = self._viewWidget.get_num_icons()
140         if numberOfIcons > 1:
141             # First check that the widget contains icons because I don't know
142             # how icon_is_visible() is handled if the view is empty.
143             if (self._viewWidget.icon_is_visible(0) == gtk.VISIBILITY_FULL
144                 and self._viewWidget.icon_is_visible(numberOfIcons - 1) == gtk.VISIBILITY_FULL):
145                 # All icons already visible. No need to scroll widget.
146                 pass
147             else:
148                 # Scroll widget to first selected icon
149                 rowNr = self._objectCollection.getObjectSelection().getLowestSelectedRowNr()
150                 if rowNr is not None:
151                     self._viewWidget.moveto(rowNr, 0.4)