1 import threading, gtk, gio, gobject
4 class notdone(Exception): pass
6 class future(threading.Thread):
10 super(future, self).__init__()
19 super(future, self).start()
25 except Exception as e:
27 gobject.idle_add(self._callcbs, True)
30 gobject.idle_add(self._callcbs, True)
32 def _callcbs(self, final):
34 for cb in self._notlist:
37 self._notlist = [] if final else nls
39 # Caller must hold GDK lock
43 self._notlist.append(cb)
48 gobject.idle_add(self._callcbs, False)
53 if self._exc is not None:
62 return self._exc != None or self._val != None
66 while self.is_alive():
70 class imgload(future):
71 def __init__(self, page):
72 super(imgload, self).__init__()
79 with self.page.open() as st:
92 return gtk.gdk.pixbuf_new_from_stream(gio.memory_input_stream_new_from_data(str(buf)))
98 if self.st is None or self.st.clen is None:
100 return float(self.p) / float(self.st.clen)
102 class pagecache(object):
103 def __init__(self, sz=50):
107 def __getitem__(self, page):
109 for ol, f in self.bk:
113 self.bk.append((idl, f))
114 if len(self.bk) > self.sz:
115 self.bk = self.bk[-self.sz:]
118 class relpageget(future):
119 def __init__(self, cur, prev, cache=None):
120 super(relpageget, self).__init__()
121 self.cur = lib.cursor(cur)
129 page = self.cur.prev()
131 page = self.cur.next()
132 except StopIteration:
139 class idpageget(future):
140 def __init__(self, base, idlist):
141 super(idpageget, self).__init__()
146 return lib.cursor(self.bnode.byidlist(self.idlist)).cur
148 class pageget(future):
149 def __init__(self, fnode):
150 super(pageget, self).__init__()
155 return lib.cursor(self.fnode).cur
157 class ccursor(object):
158 def __init__(self, ob, cache=None):
159 self.cur = lib.cursor(ob)
160 self.prev = relpageget(self.cur, True, cache)
161 self.next = relpageget(self.cur, False, cache)
163 class pageview(gtk.Widget):
164 def __init__(self, pixbuf):
165 super(pageview, self).__init__()
167 self.zoomed = None, None
170 self.interp = gtk.gdk.INTERP_HYPER
174 return self.pixbuf.get_width(), self.pixbuf.get_height()
177 return self.allocation.width, self.allocation.height
179 def do_realize(self):
180 self.set_flags(self.flags() | gtk.REALIZED)
181 alloc = self.allocation
182 self.window = gtk.gdk.Window(self.get_parent_window(),
183 width=alloc.width, height=alloc.height,
184 window_type = gtk.gdk.WINDOW_CHILD,
185 wclass = gtk.gdk.INPUT_OUTPUT,
186 event_mask = self.get_events() | gtk.gdk.EXPOSURE_MASK
188 self.window.set_user_data(self)
189 self.style.attach(self.window)
190 self.style.set_background(self.window, gtk.STATE_NORMAL)
191 self.window.move_resize(*alloc)
193 def do_unrealize(self):
194 self.window.set_user_data(None)
196 def do_size_request(self, req):
197 w, h = self.get_osize()
198 req.width, req.height = max(min(w, 4096), 0), max(min(h, 4096), 0)
201 w, h = self.get_osize()
202 alloc = self.allocation
203 return min(float(alloc.width) / float(w), float(alloc.height) / float(h))
205 def do_size_allocate(self, alloc):
206 self.allocation = alloc
208 self.zoom = self.fitzoom()
209 if self.flags() & gtk.REALIZED:
210 self.window.move_resize(*alloc)
212 def get_zoomed(self):
214 pz, zbuf = self.zoomed
216 w, h = self.get_osize()
217 zbuf = self.pixbuf.scale_simple(int(w * zoom), int(h * zoom), self.interp)
218 self.zoomed = zoom, zbuf
222 zbuf = self.get_zoomed()
223 return zbuf.get_width(), zbuf.get_height()
225 def do_expose_event(self, event):
226 aw, ah = self.get_asize()
228 zbuf = self.get_zoomed()
229 zw, zh = self.get_zsize()
238 gc = self.style.fg_gc[gtk.STATE_NORMAL]
239 self.window.draw_pixbuf(gc, zbuf, ox, oy, dx, dy, dw, dh)
241 def set_off(self, off):
242 aw, ah = self.get_asize()
243 zw, zh = self.get_zsize()
245 ox, oy = int(ox), int(oy)
246 if ox > zw - aw: ox = zw - aw
247 if oy > zh - ah: oy = zh - ah
253 def set_zoom(self, zoom):
254 if zoom is not None: zoom = float(zoom)
255 aw, ah = self.get_asize()
256 zw, zh = self.get_zsize()
257 dw, dh = zw - aw, zh - ah
259 xa = float(ox) / float(dw) if dw > 0 else 0.5
260 ya = float(oy) / float(dh) if dh > 0 else 0.5
264 self.zoom = self.fitzoom()
269 zw, zh = self.get_zsize()
270 dw, dh = zw - aw, zh - ah
271 ox = int(xa * dw) if dw > 0 else 0
272 oy = int(ya * dh) if dh > 0 else 0
273 self.set_off((ox, oy))
274 gobject.type_register(pageview)
276 class msgproc(object):
277 def attach(self, reader):
279 self.msg = gtk.Alignment(0, 0.5, 0, 0)
280 self.hlay = gtk.HBox()
281 self.lbl = gtk.Label("")
282 self.hlay.pack_start(self.lbl)
284 self.msg.add(self.hlay)
286 self.rd.sbar.pack_start(self.msg)
291 if p is not None and self._prog is None:
292 self._prog = gtk.ProgressBar()
293 self._prog.set_fraction(p)
294 self.hlay.pack_start(self._prog, padding=5)
296 elif p is None and self._prog is not None:
297 self.hlay.remove(self._prog)
301 self.rd.sbar.remove(self.msg)
303 class pagefetch(msgproc):
304 def __init__(self, fpage, setcb=None):
308 def attach(self, reader):
309 super(pagefetch, self).attach(reader)
310 self.lbl.set_text("Fetching page...")
311 self.pg.notify(self.haspage)
314 if self.rd.pagefetch.cur != self: return False
317 if self.pg.val is not None:
318 self.rd.setpage(self.pg.val)
319 if self.setcb is not None:
320 self.setcb(self.pg.val)
321 self.rd.pagefetch.set(None)
323 class imgfetch(msgproc):
324 def __init__(self, fimage):
329 def attach(self, reader):
330 super(imgfetch, self).attach(reader)
331 self.lbl.set_text("Fetching image...")
332 self.img.notify(self.imgprog)
335 if self.rd.imgfetch.cur != self: return False
339 except Exception as e:
344 self.rd.imgfetch.set(None)
346 self.prog(self.img.prog)
350 self.rd.sbar.remove(self.msg)
353 if self.error is not None:
354 self.rd.pagelbl.set_text("Error fetching image: " + self.error)
356 class preload(msgproc):
357 def __init__(self, fpage):
360 def attach(self, reader):
361 super(preload, self).attach(reader)
362 self.lbl.set_text("Fetching next page...")
363 self.pg.notify(self.haspage)
366 if self.rd.preload.cur != self: return False
367 if not self.pg.done: return True
368 if self.pg.val is not None:
369 self.img = self.rd.cache[self.pg.val]
370 self.lbl.set_text("Loading next page...")
371 self.img.notify(self.imgprog)
373 self.rd.preload.set(None)
376 if self.rd.preload.cur != self: return False
378 self.rd.preload.set(None)
380 self.prog(self.img.prog)
384 self.rd.sbar.remove(self.msg)
386 class procslot(object):
387 __slots__ = ["cur", "p"]
388 def __init__(self, p):
393 if self.cur is not None:
404 class plistget(future):
405 def __init__(self, node):
406 super(plistget, self).__init__()
410 return list(self.node)
412 class loadplist(object):
413 def __init__(self, pnode):
415 self.flist = plistget(self.pnode)
417 def attach(self, sbox):
419 self.flist.notify(self.haslist)
422 if self.sbox.loadlist.cur != self: return False
423 if not self.flist.done: return True
424 self.sbox.setlist(self.flist.val)
426 class sbox(gtk.ComboBox):
427 def __init__(self, reader, ptnode):
428 super(sbox, self).__init__()
431 self.pnode, self.pidx = self.node.stack[-1]
433 self.bk = gtk.ListStore(str)
434 self.set_model(self.bk)
435 cell = gtk.CellRendererText()
436 self.pack_start(cell, True)
437 self.add_attribute(cell, "text", 0)
440 self.set_sensitive(False)
441 self.set_focus_on_click(False)
442 self.bk.append([ptnode.name])
443 self.loadlist = procslot(self)
444 self.loadlist.set(loadplist(self.pnode))
446 def setlist(self, ls):
448 for i, ch in enumerate(ls):
449 self.bk.append(["%i/%i: %s" % (i + 1, len(ls), ch.name)])
450 self.set_active(self.pidx)
451 self.set_sensitive(True)
452 self.connect("changed", self.changed_cb)
454 def changed_cb(self, wdg, data=None):
455 self.rd.fetchpage(pageget(self.pnode[self.get_active()]))
457 class reader(gtk.Window):
458 def __init__(self, manga, profile=None):
459 super(reader, self).__init__(gtk.WINDOW_TOPLEVEL)
460 self.connect("delete_event", lambda wdg, ev, data=None: False)
461 self.connect("destroy", lambda wdg, data=None: self.quit())
462 self.connect("key_press_event", self.key)
463 self.cache = pagecache()
464 self.pagefetch = procslot(self)
465 self.imgfetch = procslot(self)
466 self.preload = procslot(self)
467 self.profile = profile
475 self.pfr = gtk.Frame(None)
476 self.pfr.set_shadow_type(gtk.SHADOW_NONE)
477 vlay.pack_start(self.pfr)
479 self.sboxbar = gtk.HBox()
480 algn = gtk.Alignment(0, 0.5, 0, 0)
481 sboxlbl = gtk.Label(self.manga.name + u": ")
484 self.sboxbar.pack_start(algn, False)
486 vlay.pack_start(self.sboxbar, False)
488 self.sbar = gtk.HBox()
489 self.pagelbl = gtk.Label("")
490 algn = gtk.Alignment(0, 0.5, 0, 0)
491 algn.add(self.pagelbl)
493 self.sbar.pack_start(algn)
495 vlay.pack_end(self.sbar, False)
500 if self.profile and "curpage" in self.profile:
501 self.fetchpage(idpageget(self.manga, self.profile["curpage"]))
503 self.fetchpage(pageget(self.manga))
506 def updpagelbl(self):
507 if self.page is None:
508 self.pagelbl.set_text("")
510 w, h = self.page.get_osize()
511 self.pagelbl.set_text(u"%s\u00d7%s (%d%%)" % (w, h, int(self.page.zoom * 100)))
513 def updsboxes(self, page):
514 nodes = [node for node, idx in page.stack[1:]] + [page]
515 l = min(len(self.sboxes), len(nodes))
516 for i, (pbox, node) in enumerate(zip(self.sboxes, nodes)):
517 if pbox.node != node:
520 for i in xrange(l, len(self.sboxes)):
521 self.sboxbar.remove(self.sboxes[i])
522 self.sboxes = self.sboxes[:l]
523 for i in xrange(l, len(nodes)):
524 new = sbox(self, nodes[i])
525 self.sboxbar.pack_start(new, False, padding=5)
526 self.sboxes.append(new)
529 def setimg(self, img):
530 if self.page is not None:
531 self.pfr.remove(self.page)
534 self.page = pageview(img)
535 self.pfr.add(self.page)
539 def setpage(self, page):
540 if self.point is not None:
544 self.profile.setprop("curpage", page.idlist())
545 self.profile.saveprops()
546 self.point = ccursor(page, self.cache)
547 self.imgfetch.set(imgfetch(self.cache[page]))
552 def fetchpage(self, fpage, setcb=None):
553 self.imgfetch.set(None)
554 proc = pagefetch(fpage, setcb)
555 self.pagefetch.set(proc)
559 self.set_title(u"Automanga \u2013 " + self.manga.name)
563 return self.page.zoom
565 def zoom(self, zoom):
566 self.page.set_zoom(zoom)
570 ox, oy = self.page.off
572 self.page.set_off((ox + px, oy + py))
574 def key(self, wdg, ev, data=None):
575 if ev.keyval in [ord('Q'), ord('q')]:
577 elif ev.keyval in [65307]:
578 if self.page is not None:
579 self.pagefetch.set(None)
580 self.imgfetch.set(None)
581 if self.page is not None:
582 if ev.keyval in [ord('O'), ord('o')]:
584 elif ev.keyval in [ord('P'), ord('p')]:
586 elif ev.keyval in [ord('[')]:
587 self.zoom = min(self.zoom * 1.25, 3)
588 elif ev.keyval in [ord(']')]:
590 elif ev.keyval in [ord('h')]:
592 elif ev.keyval in [ord('j')]:
594 elif ev.keyval in [ord('k')]:
596 elif ev.keyval in [ord('l')]:
598 elif ev.keyval in [ord('H')]:
599 self.page.set_off((0, self.page.off[1]))
600 elif ev.keyval in [ord('J')]:
601 self.page.set_off((self.page.off[0], self.page.get_asize()[1]))
602 elif ev.keyval in [ord('K')]:
603 self.page.set_off((self.page.off[1], 0))
604 elif ev.keyval in [ord('L')]:
605 self.page.set_off((self.page.get_asize()[0], self.page.off[1]))
606 if self.point is not None:
607 if ev.keyval in [ord(' ')]:
608 self.fetchpage(self.point.next, lambda page: self.preload.set(preload(relpageget(page, False, self.cache))))
609 elif ev.keyval in [65288]:
610 self.fetchpage(self.point.prev, lambda page: self.preload.set(preload(relpageget(page, True, self.cache))))
615 gobject.type_register(reader)