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