Corrected image reloading in SingleObjectView._rowChanged. Fixes ticket #69.
[joel/kofoto.git] / src / gkofoto / gkofoto / singleobjectview.py
1 import gtk
2 import sys
3 from environment import env
4 from gkofoto.imageview import *
5 from gkofoto.objectcollectionview import *
6
7 class SingleObjectView(ObjectCollectionView, ImageView):
8
9 ###############################################################################
10 ### Public
11
12     def __init__(self):
13         env.debug("Init SingleObjectView")
14         ImageView.__init__(self)
15         ObjectCollectionView.__init__(self, env.widgets["objectView"])
16         self._viewWidget.add(self)
17         self.show_all()
18         env.widgets["nextButton"].connect("clicked", self._goto, 1)
19         env.widgets["menubarNextImage"].connect("activate", self._goto, 1)
20         env.widgets["previousButton"].connect("clicked", self._goto, -1)
21         env.widgets["menubarPreviousImage"].connect("activate", self._goto, -1)
22         env.widgets["zoomToFit"].connect("clicked", self.fitToWindow)
23         env.widgets["menubarZoomToFit"].connect("activate", self.fitToWindow)
24         env.widgets["zoom100"].connect("clicked", self.zoom100)
25         env.widgets["menubarActualSize"].connect("activate", self.zoom100)
26         env.widgets["zoomIn"].connect("clicked", self.zoomIn)
27         env.widgets["menubarZoomIn"].connect("activate", self.zoomIn)
28         env.widgets["zoomOut"].connect("clicked", self.zoomOut)
29         env.widgets["menubarZoomOut"].connect("activate", self.zoomOut)
30         self.connect("button_press_event", self._mouse_button_pressed)
31         self.__selectionLocked = False
32
33     def importSelection(self, objectSelection):
34         if not self.__selectionLocked:
35             env.debug("SingleImageView is importing selection")
36             self.__selectionLocked = True
37             model = self._objectCollection.getModel()
38             if len(model) == 0:
39                 # Model is empty. No rows can be selected.
40                 self.__selectedRowNr = -1
41                 self.clear()
42             else:
43                 if len(objectSelection) == 0:
44                     # No objects is selected -> select first object
45                     self.__selectedRowNr = 0
46                     objectSelection.setSelection([self.__selectedRowNr])
47                 elif len(objectSelection) > 1:
48                     # More than one object selected -> select first object
49                     self.__selectedRowNr = objectSelection.getLowestSelectedRowNr()
50                     objectSelection.setSelection([self.__selectedRowNr])
51                 else:
52                     # Exactly one object selected
53                     self.__selectedRowNr = objectSelection.getLowestSelectedRowNr()
54                 selectedObject = objectSelection[self.__selectedRowNr]
55                 if selectedObject.isAlbum():
56                     self.loadFile(env.albumIconFileName, False)
57                 else:
58                     self.loadFile(selectedObject.getLocation(), False)
59             enablePreviousButton = (self.__selectedRowNr > 0)
60             env.widgets["previousButton"].set_sensitive(enablePreviousButton)
61             env.widgets["menubarPreviousImage"].set_sensitive(enablePreviousButton)
62             enableNextButton = (self.__selectedRowNr != -1 and
63                                 self.__selectedRowNr < len(model) - 1)
64             env.widgets["nextButton"].set_sensitive(enableNextButton)
65             env.widgets["menubarNextImage"].set_sensitive(enableNextButton)
66             self._preloadImages()
67             self.__selectionLocked = False
68         self._updateContextMenu()
69
70         # Override sensitiveness set in _updateContextMenu.
71         for widgetName in [
72                 "menubarCut",
73                 "menubarCopy",
74                 "menubarDelete",
75                 "menubarDestroy",
76                 "menubarProperties",
77                 "menubarCreateAlbumChild",
78                 "menubarRegisterAndAddImages",
79                 "menubarGenerateHtml",
80                 ]:
81             env.widgets[widgetName].set_sensitive(False)
82
83     def _showHelper(self):
84         env.enter("SingleObjectView.showHelper()")
85         env.widgets["objectView"].show()
86         env.widgets["objectView"].grab_focus()
87         for widgetName in [
88                 "zoom100",
89                 "zoomToFit",
90                 "zoomIn",
91                 "zoomOut",
92                 "menubarZoom",
93                 ]:
94             env.widgets[widgetName].set_sensitive(True)
95         env.exit("SingleObjectView.showHelper()")
96
97     def _hideHelper(self):
98         env.enter("SingleObjectView.hideHelper()")
99         env.widgets["objectView"].hide()
100         for widgetName in [
101                 "previousButton",
102                 "nextButton",
103                 "menubarPreviousImage",
104                 "menubarNextImage",
105                 "zoom100",
106                 "zoomToFit",
107                 "zoomIn",
108                 "zoomOut",
109                 "menubarZoom",
110                 ]:
111             env.widgets[widgetName].set_sensitive(False)
112         env.exit("SingleObjectView.hideHelper()")
113
114     def _connectObjectCollectionHelper(self):
115         env.enter("Connecting SingleObjectView to object collection")
116         env.exit("Connecting SingleObjectView to object collection")
117
118     def _disconnectObjectCollectionHelper(self):
119         env.enter("Disconnecting SingleObjectView from object collection")
120         env.exit("Disconnecting SingleObjectView from object collection")
121
122     def _freezeHelper(self):
123         env.enter("SingleObjectView.freezeHelper()")
124         self._clearAllConnections()
125         self.clear()
126         self._objectCollection.removeInsertedRowCallback(self._modelUpdated)
127         env.exit("SingleObjectView.freezeHelper()")
128
129     def _thawHelper(self):
130         env.enter("SingleObjectView.thawHelper()")
131         model = self._objectCollection.getModel()
132         # The following events are needed to update the previous and
133         # next navigation buttons.
134         self._connect(model, "row_changed", self._rowChanged)
135         self._connect(model, "rows_reordered", self._modelUpdated)
136         self._connect(model, "row_deleted", self._modelUpdated)
137         self._objectCollection.addInsertedRowCallback(self._modelUpdated)
138         self.importSelection(self._objectCollection.getObjectSelection())
139         env.exit("SingleObjectView.thawHelper()")
140
141     def _modelUpdated(self, *foo):
142         env.debug("SingleObjectView is handling model update")
143         self.importSelection(self._objectCollection.getObjectSelection())
144
145     def _rowChanged(self, model, path, iter, arg, *unused):
146         if path[0] == self.__selectedRowNr:
147             env.debug("selected object in SingleObjectView changed")
148             oc = self._objectCollection
149             model = oc.getUnsortedModel()
150             objid = model.get_value(model.get_iter(path), oc.COLUMN_OBJECT_ID)
151             obj = env.shelf.getObject(objid)
152             if not obj.isAlbum():
153                 self.loadFile(obj.getLocation(), True)
154
155     def _goto(self, button, direction):
156         objectSelection = self._objectCollection.getObjectSelection()
157         objectSelection.setSelection([self.__selectedRowNr + direction])
158
159     def _viewWidgetFocusInEvent(self, widget, event):
160         ObjectCollectionView._viewWidgetFocusInEvent(self, widget, event)
161         for widgetName in [
162                 "menubarClear",
163                 "menubarSelectAll",
164                 ]:
165             env.widgets[widgetName].set_sensitive(False)
166
167     def _preloadImages(self):
168         objectSelection = self._objectCollection.getObjectSelection()
169         filenames = objectSelection.getImageFilenamesToPreload()
170         maxWidth, maxHeight = self.getAvailableSpace()
171
172         # Work-around for bug in GTK. (pixbuf.scale_iter(1, 1) crashes.)
173         if maxWidth < 10 and maxHeight < 10:
174             return
175
176         env.mainwindow.getImagePreloader().preloadImages(
177             filenames, maxWidth, maxHeight)