Added full screen view button. Reordered table and single object view
[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.singleobjectview import SingleObjectView
12 from kofoto.gkofoto.objectcollectionfactory import ObjectCollectionFactory
13 from kofoto.gkofoto.registerimagesdialog import RegisterImagesDialog
14 from kofoto.gkofoto.handleimagesdialog import HandleImagesDialog
15 from kofoto.gkofoto.generatehtmldialog import GenerateHTMLDialog
16 from kofoto.gkofoto.persistentstate import PersistentState
17
18 class MainWindow(gtk.Window):
19     def __init__(self):
20         self._hiddenViews = None
21         self.__query = None
22         env.mainwindow = self
23         self._toggleLock = False
24         self.__currentObjectCollection = None
25         self._currentView = None
26         self.__persistentState = PersistentState(env)
27         self.__sourceEntry = env.widgets["sourceEntry"]
28         self.__filterEntry = env.widgets["filterEntry"]
29         self.__filterEntry.set_text(self.__persistentState.filterText)
30         self.__isFilterEnabledCheckbox = env.widgets["isFilterEnabledCheckbox"]
31
32         env.widgets["menubarViewTreePane"].connect("toggled", self._toggleTree)
33         env.widgets["menubarViewDetailsPane"].connect("toggled", self._toggleDetails)
34
35         env.widgets["tableViewToggleButton"].connect("clicked", self._toggleTableView)
36         env.widgets["tableViewToggleButton"].set_icon_widget(self.getIconImage("tableview.png"))
37         env.widgets["menubarTableView"].connect("activate", self._toggleTableView)
38
39         env.widgets["objectViewToggleButton"].connect("clicked", self._toggleObjectView)
40         env.widgets["objectViewToggleButton"].set_icon_widget(self.getIconImage("objectview.png"))
41         env.widgets["menubarObjectView"].connect("activate", self._toggleObjectView)
42
43         env.widgets["fullScreenViewButton"].connect("clicked", self._fullScreen)
44         env.widgets["fullScreenViewButton"].set_icon_widget(self.getIconImage("fullscreen-24.png"))
45         env.widgets["menubarFullScreenView"].connect("activate", self._fullScreen)
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"].get_children()[1].set_from_pixbuf(
69             gtk.gdk.pixbuf_new_from_file(os.path.join(env.iconDir, "rotateleft.png")))
70         env.widgets["menubarRotateRight"].get_children()[1].set_from_pixbuf(
71             gtk.gdk.pixbuf_new_from_file(os.path.join(env.iconDir, "rotateright.png")))
72         env.widgets["menubarRotateImageVersionLeft"].get_children()[1].set_from_pixbuf(
73             gtk.gdk.pixbuf_new_from_file(os.path.join(env.iconDir, "rotateleft.png")))
74         env.widgets["menubarRotateImageVersionRight"].get_children()[1].set_from_pixbuf(
75             gtk.gdk.pixbuf_new_from_file(os.path.join(env.iconDir, "rotateright.png")))
76         env.widgets["menubarAbout"].get_children()[1].set_from_pixbuf(
77             gtk.gdk.pixbuf_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 = gtk.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 _toggleTree(self, button):
179         if button.get_active():
180             env.widgets["sourceNotebook"].show()
181         else:
182             env.widgets["sourceNotebook"].hide()
183
184     def _toggleDetails(self, button):
185         if button.get_active():
186             self.__singleObjectView.showDetailsPane()
187         else:
188             self.__singleObjectView.hideDetailsPane()
189
190     def _toggleObjectView(self, button):
191         if not self._toggleLock:
192             self._toggleLock = True
193             button.set_active(True)
194             env.widgets["objectViewToggleButton"].set_active(True)
195             env.widgets["tableViewToggleButton"].set_active(False)
196             env.widgets["menubarObjectView"].set_active(True)
197             env.widgets["menubarTableView"].set_active(False)
198             self.showSingleObjectView()
199             self._toggleLock = False
200
201     def _toggleTableView(self, button):
202         if not self._toggleLock:
203             self._toggleLock = True
204             button.set_active(True)
205             env.widgets["objectViewToggleButton"].set_active(False)
206             env.widgets["tableViewToggleButton"].set_active(True)
207             env.widgets["menubarObjectView"].set_active(False)
208             env.widgets["menubarTableView"].set_active(True)
209             self.showTableView()
210             self._toggleLock = False
211
212     def _shelfModificationChangedCallback(self, modified):
213         env.widgets["menubarRevert"].set_sensitive(modified)
214         env.widgets["menubarSave"].set_sensitive(modified)
215         env.widgets["statusbarModified"].pop(1)
216         if modified:
217             env.widgets["statusbarModified"].push(1, "Modified")
218
219     def __setObjectCollection(self, objectCollection):
220         if self.__currentObjectCollection != objectCollection:
221             env.debug("MainWindow is propagating a new ObjectCollection")
222             self.__currentObjectCollection = objectCollection
223             self.__categories.setCollection(objectCollection)
224             if self._currentView is not None:
225                 self._currentView.setObjectCollection(objectCollection)