Make it possible to display categories in full screen window
[joel/kofoto.git] / src / packages / kofoto / gkofoto / fullscreenwindow.py
1 """This module contains the FullScreenWindow class."""
2
3 __all__ = ["FullScreenWindow"]
4
5 import gtk
6 import gobject
7 import re
8 from kofoto.gkofoto.imageview import ImageView
9 from kofoto.gkofoto.environment import env
10
11 class FullScreenWindow(gtk.Window):
12     """A fullscreen window widget."""
13
14     def __init__(self, image_versions, current_index=0):
15         """Constructor.
16
17         Arguments:
18
19         image_versions -- A list of ImageVersion instance to display.
20         current_index  -- Where to start in image_versions.
21         """
22
23         self.__gobject_init__() # TODO: Use gtk.Window.__init__ in PyGTK 2.8.
24
25         self._last_allocated_size = None
26         self._image_versions = image_versions
27         self._current_index = current_index
28         self._latest_handle = None
29         self._latest_size = (0, 0)
30         self._selected_category = None
31
32         bg_color = gtk.gdk.color_parse("#000000")
33         fg_color = gtk.gdk.color_parse("#999999")
34
35         eventbox = gtk.EventBox()
36         vbox = gtk.VBox()
37         eventbox.add(vbox)
38
39         # Add the image widget.
40         self._image_view = ImageView()
41         self._image_view.set_error_pixbuf(
42             gtk.gdk.pixbuf_new_from_file(env.unknownImageIconFileName))
43         self._image_view.modify_bg(gtk.STATE_NORMAL, bg_color)
44         vbox.pack_start(self._image_view)
45
46         # Add informative label.
47         self._info_label = gtk.Label()
48         self._info_label.set_text(
49             "No more images.\nPress escape to get back to GKofoto.")
50         self._info_label.set_justify(gtk.JUSTIFY_CENTER)
51         self._info_label.modify_fg(gtk.STATE_NORMAL, fg_color)
52         vbox.pack_start(self._info_label)
53
54         # Add categories display field.
55         self._categories_label = gtk.Label()
56         label = self._categories_label
57         label.modify_fg(gtk.STATE_NORMAL, fg_color)
58         vbox.pack_start(label, False)
59
60         # Add categorization field.
61         self._categorization_hbox = gtk.HBox()
62         self._categorization_hbox.set_spacing(5)
63         vbox.pack_start(self._categorization_hbox, False)
64         label = gtk.Label("Category:")
65         label.modify_fg(gtk.STATE_NORMAL, fg_color)
66         self._categorization_hbox.pack_start(label, False)
67         self._category_entry = gtk.Entry()
68         self._category_entry.connect(
69             "activate", self._category_entry_activate_cb)
70         self._category_entry.connect(
71             "changed", self._category_entry_changed_cb)
72         self._categorization_hbox.pack_start(self._category_entry, False)
73         self._category_indicator_image = gtk.Image()
74         self._category_indicator_image.set_from_stock(
75             gtk.STOCK_CANCEL, gtk.ICON_SIZE_MENU)
76         self._categorization_hbox.pack_start(self._category_indicator_image, False)
77         self._category_info_label = gtk.Label()
78         self._category_info_label.set_line_wrap(True)
79         self._category_info_label.modify_fg(gtk.STATE_NORMAL, fg_color)
80         self._categorization_hbox.pack_start(self._category_info_label, True, True)
81
82         self.modify_bg(gtk.STATE_NORMAL, bg_color)
83         eventbox.modify_bg(gtk.STATE_NORMAL, bg_color)
84         self.add(eventbox)
85         self.set_modal(True)
86         self.set_default_size(400, 400)
87         self.fullscreen()
88         self.connect_after("map-event", self._after_map_event_cb)
89         self.connect_after("size-allocate", self._after_size_allocate_cb)
90         eventbox.connect("button-press-event", self._button_press_event_cb)
91         self.connect("key-press-event", self._key_press_event_cb)
92
93     def destroy(self):
94         """Destroy the widget."""
95
96         self._maybe_cancel_load()
97         gtk.Window.destroy(self)
98
99     # ----------------------------------------
100
101     def _after_map_event_cb(self, *unused):
102         self._hide_cursor()
103
104     def _after_size_allocate_cb(self, widget, rect):
105         allocated_size = (rect.width, rect.height)
106         if allocated_size == self._last_allocated_size:
107             return
108         self._last_allocated_size = allocated_size
109         self._goto(self._current_index)
110
111     def _button_press_event_cb(self, widget, event):
112         iv = self._image_view
113         if event.button == 1 and iv.get_zoom_mode() == iv.ZoomMode.BestFit:
114             self._goto(self._current_index + 1)
115
116     def _category_entry_activate_cb(self, widget):
117         if self._selected_category is not None:
118             image = self._image_versions[self._current_index].getImage()
119             if self._selected_category in image.getCategories():
120                 image.removeCategory(self._selected_category)
121             else:
122                 image.addCategory(self._selected_category)
123             self._categorization_hbox.hide_all()
124             self._category_entry.set_text("")
125             # self._selected_category is set to None implicitly by set_text.
126
127     def _category_entry_changed_cb(self, widget):
128         text = self._category_entry.get_text().decode("utf-8")
129         regexp = re.compile(".*%s.*" % re.escape(text.lower()))
130         if text != "":
131             categories = list(env.shelf.getMatchingCategories(regexp))
132         else:
133             categories = []
134         exact_match = None
135         for category in categories:
136             if category.getTag().lower() == text.lower() \
137                    or category.getDescription().lower() == text.lower():
138                 exact_match = category
139                 break
140         if len(categories) == 1 or exact_match is not None:
141             image_stock_id = gtk.STOCK_OK
142             if len(categories) == 1:
143                 self._selected_category = categories[0]
144             else:
145                 self._selected_category = exact_match
146             current_image = \
147                 self._image_versions[self._current_index].getImage()
148             image_categories = current_image.getCategories()
149             category_set = self._selected_category in image_categories
150             self._category_info_label.set_markup(
151                 u"Press enter to <b>%s</b> category <b>%s</b> [<b>%s</b>]" % (
152                     ["set", "unset"][category_set],
153                     self._selected_category.getDescription(),
154                     self._selected_category.getTag()))
155         else:
156             image_stock_id = gtk.STOCK_CANCEL
157             self._selected_category = None
158             self._category_info_label.set_text("No matching category")
159         self._category_indicator_image.set_from_stock(
160             image_stock_id, gtk.ICON_SIZE_MENU)
161
162     def _display_end_screen(self):
163         self._maybe_cancel_load()
164         self._image_view.hide()
165         self._info_label.show()
166         self._categorization_hbox.hide_all()
167         self._categories_label.hide()
168
169     def _display_image(self):
170         self._image_view.set_image(self._get_image_async_cb)
171         self._image_view.show()
172         self._info_label.hide()
173         self._categorization_hbox.hide_all()
174         self._categories_label.show()
175         self._update_categories_label()
176
177     def _get_image_async_cb(self, size):
178         path = self._image_versions[self._current_index].getLocation()
179         self._maybe_cancel_load()
180         self._latest_handle = env.pixbufLoader.load(
181             path,
182             size,
183             self._image_view.set_from_pixbuf,
184             self._image_view.set_error)
185         if size != self._latest_size:
186             self._unload(self._latest_size)
187         self._preload(size)
188         self._latest_size = size
189
190     def _goto(self, new_index):
191         if new_index < 0:
192             self._current_index = -1
193             self._display_end_screen()
194         elif new_index >= len(self._image_versions):
195             self._current_index = len(self._image_versions)
196             self._display_end_screen()
197         else:
198             self._current_index = new_index
199             self._display_image()
200
201     def _hide_cursor(self):
202         pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
203         color = gtk.gdk.Color()
204         invisible_cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
205         self.window.set_cursor(invisible_cursor)
206
207     def _is_valid_index(self, index):
208         return 0 <= index < len(self._image_versions)
209
210     def _key_press_event_cb(self, unused, event):
211         # GIMP: 1 --> 100%, C-S-e --> fit
212         # EOG: [1,C-0,C-1] --> 100%
213         # f-spot: [0,1,C-0,C-1] --> fit
214
215         k = gtk.keysyms
216         CTRL = gtk.gdk.CONTROL_MASK
217         e = (event.keyval, event.state & CTRL)
218
219         if self._categorization_hbox.props.visible:
220             #
221             # Showing category entry -- disable bindings except escape.
222             #
223             if e in [(k.Escape, 0), (k.t, CTRL)]:
224                 self._toggle_categorization_field()
225                 return True
226             else:
227                 return False
228
229         #
230         # Normal case.
231         #
232         if e in [(k.space, 0), (k.Right, 0), (k.Down, 0), (k.Page_Down, 0)]:
233             self._goto(self._current_index + 1)
234             return True
235         if e in [(k.BackSpace, 0), (k.Left, 0), (k.Up, 0), (k.Page_Up, 0)]:
236             self._goto(self._current_index - 1)
237             return True
238         if e == (k.Home, 0):
239             self._goto(0)
240             return True
241         if e == (k.End, 0):
242             self._goto(len(self._image_versions) - 1)
243             return True
244         if e == (k.Escape, 0):
245             self.destroy()
246             return True
247         if e == (k.plus, 0):
248             self._image_view.zoom_in()
249             return True
250         if e == (k.minus, 0):
251             self._image_view.zoom_out()
252             return True
253         if e == (k._1, 0):
254             self._image_view.zoom_to_actual()
255             return True
256         if e in [(k.equal, 0), (k._0, 0)]:
257             self._image_view.zoom_to_fit()
258             return True
259         if e == (k.c, CTRL):
260             self._toggle_categories_field()
261             return True
262         if e == (k.t, CTRL):
263             self._toggle_categorization_field()
264             return True
265         return False
266
267     def _maybe_cancel_load(self):
268         if self._latest_handle is None:
269             # Nothing to cancel.
270             return
271         env.pixbufLoader.cancel_load(self._latest_handle)
272         self._latest_handle = None
273
274     def _preload(self, size):
275         self._preload_or_unload(size, True)
276
277     def _preload_or_unload(self, size, preload):
278         index = self._current_index
279         for x in [index + 2, index - 1, index + 1]:
280             if self._is_valid_index(x):
281                 location = self._image_versions[x].getLocation()
282                 if preload:
283                     env.pixbufLoader.preload(location, size)
284                 else:
285                     env.pixbufLoader.unload(location, size)
286
287     def _toggle_categories_field(self):
288         cl = self._categories_label
289         if cl.props.visible:
290             cl.hide()
291         else:
292             cl.show()
293
294     def _toggle_categorization_field(self):
295         if not self._image_view.props.visible:
296             # Display end screen.
297             return
298         if self._categorization_hbox.props.visible:
299             self._categorization_hbox.hide_all()
300         else:
301             self._categorization_hbox.show_all()
302             self._category_entry.grab_focus()
303
304     def _unload(self, size):
305         self._preload_or_unload(size, False)
306
307     def _update_categories_label(self):
308         image = self._image_versions[self._current_index].getImage()
309         categories = image.getCategories()
310         texts = sorted(x.getTag() for x in categories)
311         markup = u" | ".join(u"<b>%s</b>" % x for x in texts)
312         self._categories_label.set_markup(markup)
313
314 gobject.type_register(FullScreenWindow) # TODO: Not needed in PyGTK 2.8.