af9eee4430345925e4935b85bd67613bf675a907
[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 category entry.
55         self._category_hbox = gtk.HBox()
56         self._category_hbox.set_spacing(5)
57         vbox.pack_start(self._category_hbox, False)
58         label = gtk.Label("Category:")
59         label.modify_fg(gtk.STATE_NORMAL, fg_color)
60         self._category_hbox.pack_start(label, False)
61         self._category_entry = gtk.Entry()
62         self._category_entry.connect(
63             "activate", self._category_entry_activate_cb)
64         self._category_entry.connect(
65             "changed", self._category_entry_changed_cb)
66         self._category_hbox.pack_start(self._category_entry, False)
67         self._category_indicator_image = gtk.Image()
68         self._category_indicator_image.set_from_stock(
69             gtk.STOCK_CANCEL, gtk.ICON_SIZE_MENU)
70         self._category_hbox.pack_start(self._category_indicator_image, False)
71         self._category_info_label = gtk.Label()
72         self._category_info_label.set_line_wrap(True)
73         self._category_info_label.modify_fg(gtk.STATE_NORMAL, fg_color)
74         self._category_hbox.pack_start(self._category_info_label, True, True)
75
76         self.modify_bg(gtk.STATE_NORMAL, bg_color)
77         eventbox.modify_bg(gtk.STATE_NORMAL, bg_color)
78         self.add(eventbox)
79         self.set_modal(True)
80         self.set_default_size(400, 400)
81         self.fullscreen()
82         self.connect_after("map-event", self._after_map_event_cb)
83         self.connect_after("size-allocate", self._after_size_allocate_cb)
84         eventbox.connect("button-press-event", self._button_press_event_cb)
85         self.connect("key-press-event", self._key_press_event_cb)
86
87     def destroy(self):
88         """Destroy the widget."""
89
90         self._maybe_cancel_load()
91         gtk.Window.destroy(self)
92
93     # ----------------------------------------
94
95     def _after_map_event_cb(self, *unused):
96         self._hide_cursor()
97
98     def _after_size_allocate_cb(self, widget, rect):
99         allocated_size = (rect.width, rect.height)
100         if allocated_size == self._last_allocated_size:
101             return
102         self._last_allocated_size = allocated_size
103         self._goto(self._current_index)
104
105     def _button_press_event_cb(self, widget, event):
106         iv = self._image_view
107         if event.button == 1 and iv.get_zoom_mode() == iv.ZoomMode.BestFit:
108             self._goto(self._current_index + 1)
109
110     def _category_entry_activate_cb(self, widget):
111         if self._selected_category is not None:
112             image = self._image_versions[self._current_index].getImage()
113             if self._selected_category in image.getCategories():
114                 image.removeCategory(self._selected_category)
115             else:
116                 image.addCategory(self._selected_category)
117             self._category_hbox.hide_all()
118             self._category_entry.set_text("")
119             # self._selected_category is set to None implicitly by set_text.
120
121     def _category_entry_changed_cb(self, widget):
122         text = self._category_entry.get_text().decode("utf-8")
123         regexp = re.compile(".*%s.*" % re.escape(text.lower()))
124         if text != "":
125             categories = list(env.shelf.getMatchingCategories(regexp))
126         else:
127             categories = []
128         exact_match = None
129         for category in categories:
130             if category.getTag().lower() == text.lower() \
131                    or category.getDescription().lower() == text.lower():
132                 exact_match = category
133                 break
134         if len(categories) == 1 or exact_match is not None:
135             image_stock_id = gtk.STOCK_OK
136             if len(categories) == 1:
137                 self._selected_category = categories[0]
138             else:
139                 self._selected_category = exact_match
140             current_image = \
141                 self._image_versions[self._current_index].getImage()
142             image_categories = current_image.getCategories()
143             category_set = self._selected_category in image_categories
144             self._category_info_label.set_markup(
145                 u"Press enter to <b>%s</b> category <b>%s</b> [<b>%s</b>]" % (
146                     ["set", "unset"][category_set],
147                     self._selected_category.getDescription(),
148                     self._selected_category.getTag()))
149         else:
150             image_stock_id = gtk.STOCK_CANCEL
151             self._selected_category = None
152             self._category_info_label.set_text("No matching category")
153         self._category_indicator_image.set_from_stock(
154             image_stock_id, gtk.ICON_SIZE_MENU)
155
156     def _display_end_screen(self):
157         self._maybe_cancel_load()
158         self._image_view.hide()
159         self._info_label.show()
160         self._category_hbox.hide_all()
161
162     def _display_image(self):
163         self._image_view.set_image(self._get_image_async_cb)
164         self._image_view.show()
165         self._info_label.hide()
166         self._category_hbox.hide_all()
167
168     def _get_image_async_cb(self, size):
169         path = self._image_versions[self._current_index].getLocation()
170         self._maybe_cancel_load()
171         self._latest_handle = env.pixbufLoader.load(
172             path,
173             size,
174             self._image_view.set_from_pixbuf,
175             self._image_view.set_error)
176         if size != self._latest_size:
177             self._unload(self._latest_size)
178         self._preload(size)
179         self._latest_size = size
180
181     def _goto(self, new_index):
182         if new_index < 0:
183             self._current_index = -1
184             self._display_end_screen()
185         elif new_index >= len(self._image_versions):
186             self._current_index = len(self._image_versions)
187             self._display_end_screen()
188         else:
189             self._current_index = new_index
190             self._display_image()
191
192     def _hide_cursor(self):
193         pixmap = gtk.gdk.Pixmap(None, 1, 1, 1)
194         color = gtk.gdk.Color()
195         invisible_cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0)
196         self.window.set_cursor(invisible_cursor)
197
198     def _is_valid_index(self, index):
199         return 0 <= index < len(self._image_versions)
200
201     def _key_press_event_cb(self, unused, event):
202         # GIMP: 1 --> 100%, C-S-e --> fit
203         # EOG: [1,C-0,C-1] --> 100%
204         # f-spot: [0,1,C-0,C-1] --> fit
205
206         k = gtk.keysyms
207         CTRL = gtk.gdk.CONTROL_MASK
208         e = (event.keyval, event.state & CTRL)
209         if self._category_hbox.props.visible:
210             # Showing category entry -- disable bindings except escape.
211             if e in [(k.Escape, 0), (k.t, CTRL)]:
212                 self._toggle_category_field()
213                 return True
214             else:
215                 return False
216         if e in [(k.space, 0), (k.Right, 0), (k.Down, 0), (k.Page_Down, 0)]:
217             self._goto(self._current_index + 1)
218             return True
219         if e in [(k.BackSpace, 0), (k.Left, 0), (k.Up, 0), (k.Page_Up, 0)]:
220             self._goto(self._current_index - 1)
221             return True
222         if e == (k.Home, 0):
223             self._goto(0)
224             return True
225         if e == (k.End, 0):
226             self._goto(len(self._image_versions) - 1)
227             return True
228         if e == (k.Escape, 0):
229             self.destroy()
230             return True
231         if e == (k.plus, 0):
232             self._image_view.zoom_in()
233             return True
234         if e == (k.minus, 0):
235             self._image_view.zoom_out()
236             return True
237         if e == (k._1, 0):
238             self._image_view.zoom_to_actual()
239             return True
240         if e in [(k.equal, 0), (k._0, 0)]:
241             self._image_view.zoom_to_fit()
242             return True
243         if e == (k.t, CTRL):
244             self._toggle_category_field()
245             return True
246         return False
247
248     def _maybe_cancel_load(self):
249         if self._latest_handle is None:
250             # Nothing to cancel.
251             return
252         env.pixbufLoader.cancel_load(self._latest_handle)
253         self._latest_handle = None
254
255     def _preload(self, size):
256         self._preload_or_unload(size, True)
257
258     def _preload_or_unload(self, size, preload):
259         index = self._current_index
260         for x in [index + 2, index - 1, index + 1]:
261             if self._is_valid_index(x):
262                 location = self._image_versions[x].getLocation()
263                 if preload:
264                     env.pixbufLoader.preload(location, size)
265                 else:
266                     env.pixbufLoader.unload(location, size)
267
268     def _toggle_category_field(self):
269         if not self._image_view.props.visible:
270             # Display end screen.
271             return
272         if self._category_hbox.props.visible:
273             self._category_hbox.hide_all()
274         else:
275             self._category_hbox.show_all()
276             self._category_entry.grab_focus()
277
278     def _unload(self, size):
279         self._preload_or_unload(size, False)
280
281 gobject.type_register(FullScreenWindow) # TODO: Not needed in PyGTK 2.8.