1 import threading, gtk, 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__()
78 buf = gtk.gdk.PixbufLoader()
80 with self.page.open() as st:
93 return buf.get_pixbuf()
97 if self.st is None or self.st.clen is None:
99 return float(self.p) / float(self.st.clen)
101 class pagecache(object):
102 def __init__(self, sz=50):
106 def __getitem__(self, page):
108 for ol, f in self.bk:
112 self.bk.append((idl, f))
113 if len(self.bk) > self.sz:
114 self.bk = self.bk[-self.sz:]
117 def __delitem__(self, page):
119 for i, (ol, f) in enumerate(self.bk):
125 class relpageget(future):
126 def __init__(self, cur, prev, cache=None):
127 super(relpageget, self).__init__()
128 self.cur = lib.cursor(cur)
136 page = self.cur.prev()
138 page = self.cur.next()
139 except StopIteration:
146 class idpageget(future):
147 def __init__(self, base, idlist):
148 super(idpageget, self).__init__()
153 return lib.cursor(self.bnode.byidlist(self.idlist)).cur
155 class pageget(future):
156 def __init__(self, fnode):
157 super(pageget, self).__init__()
162 return lib.cursor(self.fnode).cur
164 class ccursor(object):
165 def __init__(self, ob, cache=None):
166 self.cur = lib.cursor(ob)
167 self.prev = relpageget(self.cur, True, cache)
168 self.next = relpageget(self.cur, False, cache)
170 class pageview(gtk.Widget):
171 def __init__(self, pixbuf):
172 super(pageview, self).__init__()
174 self.zoomed = None, None
177 self.interp = gtk.gdk.INTERP_HYPER
181 return self.pixbuf.get_width(), self.pixbuf.get_height()
184 return self.allocation.width, self.allocation.height
186 def do_realize(self):
187 self.set_flags(self.flags() | gtk.REALIZED)
188 alloc = self.allocation
189 self.window = gtk.gdk.Window(self.get_parent_window(),
190 width=alloc.width, height=alloc.height,
191 window_type = gtk.gdk.WINDOW_CHILD,
192 wclass = gtk.gdk.INPUT_OUTPUT,
193 event_mask = self.get_events() | gtk.gdk.EXPOSURE_MASK
195 self.window.set_user_data(self)
196 self.style.attach(self.window)
197 self.style.set_background(self.window, gtk.STATE_NORMAL)
198 self.window.move_resize(*alloc)
200 def do_unrealize(self):
201 self.window.set_user_data(None)
203 def do_size_request(self, req):
204 w, h = self.get_osize()
205 req.width, req.height = max(min(w, 4096), 0), max(min(h, 4096), 0)
208 w, h = self.get_osize()
209 alloc = self.allocation
210 return min(float(alloc.width) / float(w), float(alloc.height) / float(h))
212 def do_size_allocate(self, alloc):
213 self.allocation = alloc
215 self.zoom = self.fitzoom()
217 aw, ah = self.get_asize()
218 zw, zh = self.get_zsize()
220 if zw >= aw and ox + aw > zw:
222 if zh >= ah and oy + ah > zh:
225 if self.flags() & gtk.REALIZED:
226 self.window.move_resize(*alloc)
228 def get_zoomed(self):
230 pz, zbuf = self.zoomed
232 w, h = self.get_osize()
233 zbuf = self.pixbuf.scale_simple(int(w * zoom), int(h * zoom), self.interp)
234 self.zoomed = zoom, zbuf
238 zbuf = self.get_zoomed()
239 return zbuf.get_width(), zbuf.get_height()
241 def do_expose_event(self, event):
242 aw, ah = self.get_asize()
244 zbuf = self.get_zoomed()
245 zw, zh = self.get_zsize()
254 gc = self.style.fg_gc[gtk.STATE_NORMAL]
255 self.window.draw_pixbuf(gc, zbuf, ox, oy, dx, dy, dw, dh)
257 def set_off(self, off):
258 aw, ah = self.get_asize()
259 zw, zh = self.get_zsize()
261 ox, oy = int(ox), int(oy)
262 if ox > zw - aw: ox = zw - aw
263 if oy > zh - ah: oy = zh - ah
269 def set_zoom(self, zoom):
270 if zoom is not None: zoom = float(zoom)
271 aw, ah = self.get_asize()
272 zw, zh = self.get_zsize()
273 dw, dh = zw - aw, zh - ah
275 xa = float(ox) / float(dw) if dw > 0 else 0.5
276 ya = float(oy) / float(dh) if dh > 0 else 0.5
280 self.zoom = self.fitzoom()
285 zw, zh = self.get_zsize()
286 dw, dh = zw - aw, zh - ah
287 ox = int(xa * dw) if dw > 0 else 0
288 oy = int(ya * dh) if dh > 0 else 0
289 self.set_off((ox, oy))
290 gobject.type_register(pageview)
292 class msgproc(object):
293 def attach(self, reader):
295 self.msg = gtk.Alignment(0, 0.5, 0, 0)
296 self.hlay = gtk.HBox()
297 self.lbl = gtk.Label("")
298 self.hlay.pack_start(self.lbl)
300 self.msg.add(self.hlay)
302 self.rd.sbar.pack_start(self.msg)
308 if self._prog is None:
309 self._prog = gtk.ProgressBar()
310 self.hlay.pack_start(self._prog, padding=5)
312 self._prog.set_fraction(p)
313 elif p is None and self._prog is not None:
314 self.hlay.remove(self._prog)
318 self.rd.sbar.remove(self.msg)
320 class pagefetch(msgproc):
321 def __init__(self, fpage, setcb=None):
325 def attach(self, reader):
326 super(pagefetch, self).attach(reader)
327 self.lbl.set_text("Fetching page...")
328 self.pg.notify(self.haspage)
331 if self.rd.pagefetch.cur != self: return False
334 if self.pg.val is not None:
335 self.rd.setpage(self.pg.val)
336 if self.setcb is not None:
337 self.setcb(self.pg.val)
338 self.rd.pagefetch.set(None)
340 class imgfetch(msgproc):
341 def __init__(self, fimage):
346 def attach(self, reader):
347 super(imgfetch, self).attach(reader)
348 self.lbl.set_text("Fetching image...")
349 self.img.notify(self.imgprog)
352 if self.rd.imgfetch.cur != self: return False
356 except Exception as e:
361 self.rd.imgfetch.set(None)
363 self.prog(self.img.prog)
367 self.rd.sbar.remove(self.msg)
370 if self.error is not None:
371 self.rd.pagelbl.set_text("Error fetching image: " + self.error)
373 class preload(msgproc):
374 def __init__(self, fpage):
377 def attach(self, reader):
378 super(preload, self).attach(reader)
379 self.lbl.set_text("Fetching next page...")
380 self.pg.notify(self.haspage)
383 if self.rd.preload.cur != self: return False
384 if not self.pg.done: return True
385 if self.pg.val is not None:
386 self.img = self.rd.cache[self.pg.val]
387 self.lbl.set_text("Loading next page...")
388 self.img.notify(self.imgprog)
390 self.rd.preload.set(None)
393 if self.rd.preload.cur != self: return False
395 self.rd.preload.set(None)
397 self.prog(self.img.prog)
401 self.rd.sbar.remove(self.msg)
403 class procslot(object):
404 __slots__ = ["cur", "p"]
405 def __init__(self, p):
410 if self.cur is not None:
421 class plistget(future):
422 def __init__(self, node):
423 super(plistget, self).__init__()
427 return list(self.node)
429 class loadplist(object):
430 def __init__(self, pnode):
432 self.flist = plistget(self.pnode)
434 def attach(self, sbox):
436 self.flist.notify(self.haslist)
439 if self.sbox.loadlist.cur != self: return False
440 if not self.flist.done: return True
441 self.sbox.setlist(self.flist.val)
443 class sbox(gtk.ComboBox):
444 def __init__(self, reader, ptnode):
445 super(sbox, self).__init__()
448 self.pnode, self.pidx = self.node.stack[-1]
450 self.bk = gtk.ListStore(str)
451 self.set_model(self.bk)
452 cell = gtk.CellRendererText()
453 self.pack_start(cell, True)
454 self.add_attribute(cell, "text", 0)
457 self.set_sensitive(False)
458 self.set_focus_on_click(False)
459 self.bk.append([ptnode.name])
460 self.loadlist = procslot(self)
461 self.loadlist.set(loadplist(self.pnode))
463 def setlist(self, ls):
465 for i, ch in enumerate(ls):
466 self.bk.append(["%i/%i: %s" % (i + 1, len(ls), ch.name)])
467 self.set_active(self.pidx)
468 self.set_sensitive(True)
469 self.connect("changed", self.changed_cb)
471 def changed_cb(self, wdg, data=None):
472 self.rd.fetchpage(pageget(self.pnode[self.get_active()]))
474 class profprop(object):
475 def __init__(self, key, default=None):
477 self.default = default
479 def __get__(self, ins, cls):
480 return ins.profile.props.get(self.key, self.default)
482 def __set__(self, ins, val):
483 ins.profile.props[self.key] = val
486 def __delete__(self, ins):
487 del ins.profile.props[self.key]
490 class reader(gtk.Window):
491 def __init__(self, manga, prof=None):
492 super(reader, self).__init__(gtk.WINDOW_TOPLEVEL)
493 self.connect("delete_event", lambda wdg, ev, data=None: False)
494 self.connect("destroy", lambda wdg, data=None: self.quit())
495 self.connect("key_press_event", self.key)
496 self.cache = pagecache()
497 self.pagefetch = procslot(self)
498 self.imgfetch = procslot(self)
499 self.preload = procslot(self)
500 self.profile = prof if prof else profile.memmanga(None, None, manga.id)
508 self.pfr = gtk.Frame(None)
509 self.pfr.set_shadow_type(gtk.SHADOW_NONE)
510 vlay.pack_start(self.pfr)
512 self.sboxbar = gtk.HBox()
513 algn = gtk.Alignment(0, 0.5, 0, 0)
514 sboxlbl = gtk.Label(self.manga.name + u": ")
517 self.sboxbar.pack_start(algn, False)
519 vlay.pack_start(self.sboxbar, False)
521 self.sbar = gtk.HBox()
522 self.pagelbl = gtk.Label("")
523 algn = gtk.Alignment(0, 0.5, 0, 0)
524 algn.add(self.pagelbl)
526 self.sbar.pack_start(algn)
528 vlay.pack_end(self.sbar, False)
533 if self.curpage is not None:
534 self.fetchpage(idpageget(self.manga, self.curpage))
536 self.fetchpage(pageget(self.manga))
539 zmode = profprop("zmode", "fit")
540 curpage = profprop("curpage")
542 def updpagelbl(self):
543 if self.page is None:
544 self.pagelbl.set_text("")
546 w, h = self.page.get_osize()
547 self.pagelbl.set_text(u"%s\u00d7%s (%d%%)" % (w, h, int(self.page.zoom * 100)))
549 def updsboxes(self, page):
550 nodes = [node for node, idx in page.stack[1:]] + [page]
551 l = min(len(self.sboxes), len(nodes))
552 for i, (pbox, node) in enumerate(zip(self.sboxes, nodes)):
553 if pbox.node != node:
556 for i in xrange(l, len(self.sboxes)):
557 self.sboxbar.remove(self.sboxes[i])
558 self.sboxes = self.sboxes[:l]
559 for i in xrange(l, len(nodes)):
560 new = sbox(self, nodes[i])
561 self.sboxbar.pack_start(new, False, padding=5)
562 self.sboxes.append(new)
565 def setimg(self, img):
566 if self.page is not None:
567 self.pfr.remove(self.page)
570 self.page = pageview(img)
571 if self.zmode == "1":
572 self.page.set_zoom(1)
573 self.page.set_off((0, 0))
574 self.pfr.add(self.page)
578 def setpage(self, page):
579 if self.point is not None:
582 self.curpage = page.idlist()
583 self.point = ccursor(page, self.cache)
584 self.imgfetch.set(imgfetch(self.cache[page]))
589 def fetchpage(self, fpage, setcb=None):
590 self.imgfetch.set(None)
591 proc = pagefetch(fpage, setcb)
592 self.pagefetch.set(proc)
596 self.set_title(u"Automanga \u2013 " + self.manga.name)
600 return self.page.zoom
602 def zoom(self, zoom):
603 self.page.set_zoom(zoom)
607 ox, oy = self.page.off
609 self.page.set_off((ox + px, oy + py))
611 def key(self, wdg, ev, data=None):
612 if ev.keyval in [ord('Q'), ord('q')]:
614 elif ev.keyval in [65307]:
615 if self.page is not None:
616 self.pagefetch.set(None)
617 self.imgfetch.set(None)
618 if self.page is not None:
619 if ev.keyval in [ord('O'), ord('o')]:
622 elif ev.keyval in [ord('P'), ord('p')]:
625 elif ev.keyval in [ord('[')]:
626 self.zoom = min(self.zoom * 1.25, 3)
627 elif ev.keyval in [ord(']')]:
629 elif ev.keyval in [ord('h')]:
631 elif ev.keyval in [ord('j')]:
633 elif ev.keyval in [ord('k')]:
635 elif ev.keyval in [ord('l')]:
637 elif ev.keyval in [ord('H')]:
638 self.page.set_off((0, self.page.off[1]))
639 elif ev.keyval in [ord('J')]:
640 self.page.set_off((self.page.off[0], self.page.get_asize()[1]))
641 elif ev.keyval in [ord('K')]:
642 self.page.set_off((self.page.off[1], 0))
643 elif ev.keyval in [ord('L')]:
644 self.page.set_off((self.page.get_asize()[0], self.page.off[1]))
645 if self.point is not None:
646 if ev.keyval in [ord(' ')]:
647 if self.page and self.page.off[1] + self.page.get_asize()[1] < self.page.get_zsize()[1]:
648 self.pan((0, self.page.get_asize()[1] - 50))
650 self.fetchpage(self.point.next, lambda page: self.preload.set(preload(relpageget(page, False, self.cache))))
651 elif ev.keyval in [65288]:
652 if self.page and self.page.off[1] > 0:
653 self.pan((0, -(self.page.get_asize()[1] - 50)))
655 self.fetchpage(self.point.prev, lambda page: self.preload.set(preload(relpageget(page, True, self.cache))))
656 elif ev.keyval in [ord('R'), ord('r')]:
657 page = self.point.cur.cur
659 self.imgfetch.set(imgfetch(self.cache[page]))
664 gobject.type_register(reader)