Make gkofoto run on pygtk 2.13
[joel/kofoto.git] / src / packages / kofoto / gkofoto / mainwindow.py
1 import gtk
2 from gtk import gdk
3 import os
4
5 from kofoto.gkofoto.categories import Categories
6 from kofoto.gkofoto.albums import Albums
7 from kofoto.gkofoto.environment import env
8 from kofoto.gkofoto.tableview import TableView
9 from kofoto.gkofoto.singleobjectview import SingleObjectView
10 from kofoto.gkofoto.objectcollectionfactory import ObjectCollectionFactory
11 from kofoto.gkofoto.registerimagesdialog import RegisterImagesDialog
12 from kofoto.gkofoto.handleimagesdialog import HandleImagesDialog
13 from kofoto.gkofoto.generatehtmldialog import GenerateHTMLDialog
14 from kofoto.gkofoto.persistentstate import PersistentState
15
16 class MainWindow(gtk.Window):
17     def __init__(self):
18         self._hiddenViews = None
19         self.__query = None
20         env.mainwindow = self
21         self._toggleLock = False
22         self.__currentObjectCollection = None
23         self._currentView = None
24         self.__persistentState = PersistentState(env)
25         self.__sourceEntry = env.widgets["sourceEntry"]
26         self.__filterEntry = env.widgets["filterEntry"]
27         self.__filterEntry.set_text(self.__persistentState.filterText)
28         self.__isFilterEnabledCheckbox = env.widgets["isFilterEnabledCheckbox"]
29
30         env.widgets["menubarViewTreePane"].connect("toggled", self._toggleTree)
31         env.widgets["menubarViewDetailsPane"].connect("toggled", self._toggleDetails)
32
33         env.widgets["tableViewToggleButton"].connect("clicked", self._toggleTableView)
34         env.widgets["tableViewToggleButton"].set_icon_widget(self.getIconImage("tableview.png"))
35         env.widgets["menubarTableView"].connect("activate", self._toggleTableView)
36
37         env.widgets["objectViewToggleButton"].connect("clicked", self._toggleObjectView)
38         env.widgets["objectViewToggleButton"].set_icon_widget(self.getIconImage("objectview.png"))
39         env.widgets["menubarObjectView"].connect("activate", self._toggleObjectView)
40
41         env.widgets["fullScreenViewButton"].connect("clicked", self._fullScreen)
42         env.widgets["fullScreenViewButton"].set_icon_widget(self.getIconImage("fullscreen-24.png"))
43         env.widgets["menubarFullScreenView"].connect("activate", self._fullScreen)
44
45         env.widgets["menubarSearch"].connect("activate", self._search)
46
47         env.widgets["previousButton"].set_sensitive(False)
48         env.widgets["nextButton"].set_sensitive(False)
49         env.widgets["zoom100"].set_sensitive(False)
50         env.widgets["zoomToFit"].set_sensitive(False)
51         env.widgets["zoomIn"].set_sensitive(False)
52         env.widgets["zoomOut"].set_sensitive(False)
53
54         env.widgets["menubarSave"].connect("activate", env.controller.save_cb)
55         env.widgets["menubarSave"].set_sensitive(False)
56         env.widgets["menubarRevert"].connect("activate", env.controller.revert_cb)
57         env.widgets["menubarRevert"].set_sensitive(False)
58         env.widgets["menubarQuit"].connect("activate", env.controller.quit_cb)
59
60         env.widgets["menubarNextImage"].set_sensitive(False)
61         env.widgets["menubarPreviousImage"].set_sensitive(False)
62         env.widgets["menubarZoom"].set_sensitive(False)
63
64         env.widgets["menubarRegisterImages"].connect("activate", self.registerImages, None)
65         env.widgets["menubarHandleModifiedOrRenamedImages"].connect(
66             "activate", self.handleModifiedOrRenamedImages, None)
67
68         env.widgets["menubarRotateLeft"].set_image(
69             gtk.image_new_from_file(os.path.join(env.iconDir, "rotateleft.png")))
70         env.widgets["menubarRotateRight"].set_image(
71             gtk.image_new_from_file(os.path.join(env.iconDir, "rotateright.png")))
72         env.widgets["menubarRotateImageVersionLeft"].set_image(
73             gtk.image_new_from_file(os.path.join(env.iconDir, "rotateleft.png")))
74         env.widgets["menubarRotateImageVersionRight"].set_image(
75             gtk.image_new_from_file(os.path.join(env.iconDir, "rotateright.png")))
76         env.widgets["menubarAbout"].set_image(
77             gtk.image_new_from_file(os.path.join(env.iconDir, "about-icon.png")))
78
79         env.widgets["menubarAbout"].connect("activate", self.showAboutBox)
80
81         self.__sourceEntry.connect("activate", self._queryChanged)
82         self.__filterEntry.connect("activate", self._queryChanged)
83         self.__isFilterEnabledCheckbox.connect("toggled", self._queryChanged)
84
85         env.shelf.registerModificationCallback(self._shelfModificationChangedCallback)
86
87         self.__factory = ObjectCollectionFactory()
88         self.__categories = Categories(self)
89         self.__albums = Albums(self)
90         self.__tableView = TableView()
91         self.__singleObjectView = SingleObjectView()
92         self.showTableView()
93
94     def saveState(self):
95         self.__persistentState.save()
96
97     def _queryChanged(self, *unused):
98         query = self.__sourceEntry.get_text().decode("utf-8")
99         self.loadQuery(query)
100         self.__sourceEntry.grab_remove()
101         self.__persistentState.filterText = self.__filterEntry.get_text()
102
103     def loadQuery(self, query):
104         self.__query = query
105         self.__sourceEntry.set_text(query)
106         useFilter = self.__isFilterEnabledCheckbox.get_active()
107         self.__filterEntry.set_sensitive(useFilter)
108         if useFilter:
109             filterText = self.__filterEntry.get_text().decode("utf-8")
110         else:
111             filterText = ""
112         self.__setObjectCollection(
113             self.__factory.getObjectCollection(query, filterText))
114
115     def reload(self):
116         self.__albums.loadAlbumTree()
117         self.__categories.loadCategoryTree()
118         self.loadQuery(self.__query)
119
120     def reloadObjectList(self):
121         self.loadQuery(self.__query)
122
123     def reloadAlbumTree(self):
124         self.__albums.loadAlbumTree()
125
126     def unselectAlbumTree(self):
127         self.__albums.unselect()
128
129     def registerImages(self, *unused):
130         dialog = RegisterImagesDialog()
131         if dialog.run() == gtk.RESPONSE_OK:
132             self.reload() # TODO: don't reload everything.
133         dialog.destroy()
134
135     def handleModifiedOrRenamedImages(self, *unused):
136         dialog = HandleImagesDialog()
137         dialog.run()
138         dialog.destroy()
139
140     def showAboutBox(self, *unused):
141         widgets = gtk.glade.XML(env.gladeFile, "aboutDialog")
142         aboutDialog = widgets.get_widget("aboutDialog")
143         nameAndVersionLabel = widgets.get_widget("nameAndVersionLabel")
144         nameAndVersionLabel.set_text("Kofoto %s" % env.version)
145         aboutDialog.run()
146         aboutDialog.destroy()
147
148     def generateHtml(self, album):
149         dialog = GenerateHTMLDialog(album)
150         dialog.run()
151
152     def getIconImage(self, name):
153         pixbuf = gdk.pixbuf_new_from_file(os.path.join(env.iconDir, name))
154         image = gtk.Image()
155         image.set_from_pixbuf(pixbuf)
156         image.show()
157         return image
158
159     def _viewChanged(self):
160         for hiddenView in self._hiddenViews:
161             hiddenView.hide()
162         self._currentView.show(self.__currentObjectCollection)
163
164     def showTableView(self):
165         self._currentView = self.__tableView
166         self._hiddenViews = [self.__singleObjectView]
167         self._viewChanged()
168
169     def showSingleObjectView(self):
170         self._currentView = self.__singleObjectView
171         self._hiddenViews = [self.__tableView]
172         self._viewChanged()
173
174     def _fullScreen(self, *unused):
175         if self.__currentObjectCollection is not None:
176             self.__currentObjectCollection.fullScreen()
177
178     def _search(self, *unused):
179         self.__sourceEntry.grab_focus()
180
181     def _toggleTree(self, button):
182         if button.get_active():
183             env.widgets["sourceNotebook"].show()
184         else:
185             env.widgets["sourceNotebook"].hide()
186
187     def _toggleDetails(self, button):
188         if button.get_active():
189             self.__singleObjectView.showDetailsPane()
190         else:
191             self.__singleObjectView.hideDetailsPane()
192
193     def _toggleObjectView(self, button):
194         if not self._toggleLock:
195             self._toggleLock = True
196             button.set_active(True)
197             env.widgets["objectViewToggleButton"].set_active(True)
198             env.widgets["tableViewToggleButton"].set_active(False)
199             env.widgets["menubarObjectView"].set_active(True)
200             env.widgets["menubarTableView"].set_active(False)
201             self.showSingleObjectView()
202             self._toggleLock = False
203
204     def _toggleTableView(self, button):
205         if not self._toggleLock:
206             self._toggleLock = True
207             button.set_active(True)
208             env.widgets["objectViewToggleButton"].set_active(False)
209             env.widgets["tableViewToggleButton"].set_active(True)
210             env.widgets["menubarObjectView"].set_active(False)
211             env.widgets["menubarTableView"].set_active(True)
212             self.showTableView()
213             self._toggleLock = False
214
215     def _shelfModificationChangedCallback(self, modified):
216         env.widgets["menubarRevert"].set_sensitive(modified)
217         env.widgets["menubarSave"].set_sensitive(modified)
218         env.widgets["statusbarModified"].pop(1)
219         if modified:
220             env.widgets["statusbarModified"].push(1, "Modified")
221
222     def __setObjectCollection(self, objectCollection):
223         if self.__currentObjectCollection != objectCollection:
224             env.debug("MainWindow is propagating a new ObjectCollection")
225             self.__currentObjectCollection = objectCollection
226             self.__categories.setCollection(objectCollection)
227             if self._currentView is not None:
228                 self._currentView.setObjectCollection(objectCollection)