2 from gi.repository import Gtk as gtk, GdkPixbuf as gdkpix, Gdk as gdk, GObject as gobject
3 from . import lib, profile
5 class notdone(Exception): pass
7 class future(threading.Thread):
11 super(future, self).__init__()
15 self._tstarted = False
19 if not self._tstarted:
20 super(future, self).start()
26 except Exception as e:
28 gobject.idle_add(self._callcbs, True)
31 gobject.idle_add(self._callcbs, True)
33 def _callcbs(self, final):
35 for cb in self._notlist:
38 self._notlist = [] if final else nls
40 # Caller must hold GDK lock
44 self._notlist.append(cb)
49 gobject.idle_add(self._callcbs, False)
54 if self._exc is not None:
63 return self._exc != None or self._val != None
67 while self.is_alive():
71 class imgload(future):
72 def __init__(self, page):
73 super(imgload, self).__init__()
79 buf = gdkpix.PixbufLoader()
82 with self.page.open() as st:
100 return buf.get_pixbuf()
104 if self.st is None or self.st.clen is None:
106 return float(self.p) / float(self.st.clen)
108 class pagecache(object):
109 def __init__(self, sz=50):
113 def __getitem__(self, page):
115 for ol, f in self.bk:
119 self.bk.append((idl, f))
120 if len(self.bk) > self.sz:
121 self.bk = self.bk[-self.sz:]
124 def __delitem__(self, page):
126 for i, (ol, f) in enumerate(self.bk):
132 class relpageget(future):
133 def __init__(self, cur, prev, cache=None):
134 super(relpageget, self).__init__()
135 self.cur = lib.cursor(cur)
143 page = self.cur.prev()
145 page = self.cur.next()
146 except StopIteration:
153 class idpageget(future):
154 def __init__(self, base, idlist):
155 super(idpageget, self).__init__()
160 return lib.cursor(self.bnode.byidlist(self.idlist)).cur
162 class pageget(future):
163 def __init__(self, fnode):
164 super(pageget, self).__init__()
169 return lib.cursor(self.fnode).cur
171 class ccursor(object):
172 def __init__(self, ob, cache=None):
173 self.cur = lib.cursor(ob)
174 self.prev = relpageget(self.cur, True, cache)
175 self.next = relpageget(self.cur, False, cache)
177 class pageview(gtk.Widget):
178 def __init__(self, pixbuf):
179 super(pageview, self).__init__()
181 self.zoomed = None, None
184 self.interp = gdkpix.InterpType.HYPER
188 return self.pixbuf.get_width(), self.pixbuf.get_height()
191 alloc = self.get_allocation()
192 return alloc.width, alloc.height
194 def do_realize(self):
195 alloc = self.get_allocation()
196 attr = gdk.WindowAttr()
197 attr.window_type = gdk.WindowType.CHILD
200 attr.width = alloc.width
201 attr.height = alloc.height
202 attr.visual = self.get_visual()
203 attr.event_mask = self.get_events() | gdk.EventMask.EXPOSURE_MASK
204 a = gdk.WindowAttributesType
205 wnd = gdk.Window(self.get_parent_window(), attr, a.X | a.Y | a.VISUAL)
206 wnd.set_background_pattern(None)
208 self.register_window(wnd)
209 self.set_realized(True)
211 # XXX: Why don't these actually get called?
212 def get_preferred_width(self):
213 return 0, max(min(self.get_osize()[0], 4096), 0)
214 def get_preferred_height(self):
215 return 0, max(min(self.get_osize()[1], 4096), 0)
218 w, h = self.get_osize()
219 alloc = self.get_allocation()
220 return min(float(alloc.width) / float(w), float(alloc.height) / float(h))
222 def do_size_allocate(self, alloc):
223 self.set_allocation(alloc)
225 self.zoom = self.fitzoom()
227 aw, ah = self.get_asize()
228 zw, zh = self.get_zsize()
230 if zw >= aw and ox + aw > zw:
232 if zh >= ah and oy + ah > zh:
235 if self.get_realized():
236 self.get_window().move_resize(alloc.x, alloc.y, alloc.width, alloc.height)
238 def get_zoomed(self):
240 pz, zbuf = self.zoomed
242 w, h = self.get_osize()
243 zbuf = self.pixbuf.scale_simple(int(w * zoom), int(h * zoom), self.interp)
244 self.zoomed = zoom, zbuf
248 zbuf = self.get_zoomed()
249 return zbuf.get_width(), zbuf.get_height()
251 def do_draw(self, cr):
252 aw, ah = self.get_asize()
254 zbuf = self.get_zoomed()
255 zw, zh = self.get_zsize()
264 gdk.cairo_set_source_pixbuf(cr, zbuf, dx - ox, dy - oy)
267 def set_off(self, off):
268 aw, ah = self.get_asize()
269 zw, zh = self.get_zsize()
271 ox, oy = int(ox), int(oy)
272 if ox > zw - aw: ox = zw - aw
273 if oy > zh - ah: oy = zh - ah
279 def set_zoom(self, zoom):
280 if zoom is not None: zoom = float(zoom)
281 aw, ah = self.get_asize()
282 zw, zh = self.get_zsize()
283 dw, dh = zw - aw, zh - ah
285 xa = float(ox) / float(dw) if dw > 0 else 0.5
286 ya = float(oy) / float(dh) if dh > 0 else 0.5
290 self.zoom = self.fitzoom()
295 zw, zh = self.get_zsize()
296 dw, dh = zw - aw, zh - ah
297 ox = int(xa * dw) if dw > 0 else 0
298 oy = int(ya * dh) if dh > 0 else 0
299 self.set_off((ox, oy))
301 class msgproc(object):
302 def attach(self, reader):
304 self.msg = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
305 self.hlay = gtk.HBox()
306 self.lbl = gtk.Label("")
307 self.hlay.pack_start(self.lbl, True, True, 0)
309 self.msg.add(self.hlay)
311 self.rd.sbar.pack_start(self.msg, True, True, 0)
317 if self._prog is None:
318 self._prog = gtk.ProgressBar()
319 self.hlay.pack_start(self._prog, True, True, 5)
321 self._prog.set_fraction(p)
322 elif p is None and self._prog is not None:
323 self.hlay.remove(self._prog)
327 self.rd.sbar.remove(self.msg)
329 class pagefetch(msgproc):
330 def __init__(self, fpage, setcb=None):
334 def attach(self, reader):
335 super(pagefetch, self).attach(reader)
336 self.lbl.set_text("Fetching page...")
337 self.pg.notify(self.haspage)
340 if self.rd.pagefetch.cur != self: return False
343 if self.pg.val is not None:
344 self.rd.setpage(self.pg.val)
345 if self.setcb is not None:
346 self.setcb(self.pg.val)
347 self.rd.pagefetch.set(None)
349 class imgfetch(msgproc):
350 def __init__(self, fimage):
355 def attach(self, reader):
356 super(imgfetch, self).attach(reader)
357 self.lbl.set_text("Fetching image...")
358 self.img.notify(self.imgprog)
361 if self.rd.imgfetch.cur != self: return False
365 except Exception as e:
370 self.rd.imgfetch.set(None)
372 self.prog(self.img.prog)
376 self.rd.sbar.remove(self.msg)
379 if self.error is not None:
380 self.rd.pagelbl.set_text("Error fetching image: " + self.error)
382 class preload(msgproc):
383 def __init__(self, fpage):
386 def attach(self, reader):
387 super(preload, self).attach(reader)
388 self.lbl.set_text("Fetching next page...")
389 self.pg.notify(self.haspage)
392 if self.rd.preload.cur != self: return False
393 if not self.pg.done: return True
394 if self.pg.val is not None:
395 self.img = self.rd.cache[self.pg.val]
396 self.lbl.set_text("Loading next page...")
397 self.img.notify(self.imgprog)
399 self.rd.preload.set(None)
402 if self.rd.preload.cur != self: return False
404 self.rd.preload.set(None)
406 self.prog(self.img.prog)
410 self.rd.sbar.remove(self.msg)
412 class procslot(object):
413 __slots__ = ["cur", "p"]
414 def __init__(self, p):
419 if self.cur is not None:
430 class plistget(future):
431 def __init__(self, node):
432 super(plistget, self).__init__()
436 return list(self.node)
438 class loadplist(object):
439 def __init__(self, pnode):
441 self.flist = plistget(self.pnode)
443 def attach(self, sbox):
445 self.flist.notify(self.haslist)
448 if self.sbox.loadlist.cur != self: return False
449 if not self.flist.done: return True
450 self.sbox.setlist(self.flist.val)
452 class sbox(gtk.ComboBox):
453 def __init__(self, reader, ptnode):
454 super(sbox, self).__init__()
457 self.pnode, self.pidx = self.node.stack[-1]
459 self.bk = gtk.ListStore(str)
460 self.set_model(self.bk)
461 cell = gtk.CellRendererText()
462 self.pack_start(cell, True)
463 self.add_attribute(cell, "text", 0)
466 self.set_sensitive(False)
467 self.set_focus_on_click(False)
468 self.bk.append([ptnode.name])
469 self.loadlist = procslot(self)
470 self.loadlist.set(loadplist(self.pnode))
472 def setlist(self, ls):
474 for i, ch in enumerate(ls):
475 self.bk.append(["%i/%i: %s" % (i + 1, len(ls), ch.name)])
476 self.set_active(self.pidx)
477 self.set_sensitive(True)
478 self.connect("changed", self.changed_cb)
480 def changed_cb(self, wdg, data=None):
481 self.rd.fetchpage(pageget(self.pnode[self.get_active()]))
483 class profprop(object):
484 def __init__(self, key, default=None):
486 self.default = default
488 def __get__(self, ins, cls):
489 return ins.profile.props.get(self.key, self.default)
491 def __set__(self, ins, val):
492 ins.profile.props[self.key] = val
495 def __delete__(self, ins):
496 del ins.profile.props[self.key]
499 class reader(gtk.Window):
500 def __init__(self, manga, prof=None):
501 super(reader, self).__init__()
502 self.connect("delete_event", lambda wdg, ev, data=None: False)
503 self.connect("destroy", lambda wdg, data=None: self.quit())
504 self.connect("key_press_event", self.key)
505 self.cache = pagecache()
506 self.pagefetch = procslot(self)
507 self.imgfetch = procslot(self)
508 self.preload = procslot(self)
509 self.profile = prof if prof else profile.memmanga(None, None, manga.id)
517 self.pfr = gtk.Frame()
518 self.pfr.set_shadow_type(gtk.ShadowType.NONE)
519 vlay.pack_start(self.pfr, True, True, 0)
521 self.sboxbar = gtk.HBox()
522 algn = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
523 sboxlbl = gtk.Label(self.manga.name + ": ")
526 self.sboxbar.pack_start(algn, False, True, 0)
528 vlay.pack_start(self.sboxbar, False, True, 0)
530 self.sbar = gtk.HBox()
531 self.pagelbl = gtk.Label("")
532 algn = gtk.Alignment(xalign=0.0, yalign=0.5, xscale=0.0, yscale=0.0)
533 algn.add(self.pagelbl)
535 self.sbar.pack_start(algn, True, True, 0)
537 vlay.pack_end(self.sbar, False, True, 0)
542 if self.curpage is not None:
543 self.fetchpage(idpageget(self.manga, self.curpage))
545 self.fetchpage(pageget(self.manga))
548 zmode = profprop("zmode", "fit")
549 curpage = profprop("curpage")
551 def updpagelbl(self):
552 if self.page is None:
553 self.pagelbl.set_text("")
555 w, h = self.page.get_osize()
556 self.pagelbl.set_text("%s\u00d7%s (%d%%)" % (w, h, int(self.page.zoom * 100)))
558 def updsboxes(self, page):
559 nodes = [node for node, idx in page.stack[1:]] + [page]
560 l = min(len(self.sboxes), len(nodes))
561 for i, (pbox, node) in enumerate(zip(self.sboxes, nodes)):
562 if pbox.node != node:
565 for i in range(l, len(self.sboxes)):
566 self.sboxbar.remove(self.sboxes[i])
567 self.sboxes = self.sboxes[:l]
568 for i in range(l, len(nodes)):
569 new = sbox(self, nodes[i])
570 self.sboxbar.pack_start(new, False, True, 5)
571 self.sboxes.append(new)
574 def setimg(self, img):
575 if self.page is not None:
576 self.pfr.remove(self.page)
579 self.page = pageview(img)
580 if self.zmode == "1":
581 self.page.set_zoom(1)
582 self.page.set_off((0, 0))
583 self.pfr.add(self.page)
587 def setpage(self, page):
588 if self.point is not None:
591 self.curpage = page.idlist()
592 self.point = ccursor(page, self.cache)
593 self.imgfetch.set(imgfetch(self.cache[page]))
598 def fetchpage(self, fpage, setcb=None):
599 self.imgfetch.set(None)
600 proc = pagefetch(fpage, setcb)
601 self.pagefetch.set(proc)
605 self.set_title("Automanga \u2013 " + self.manga.name)
609 return self.page.zoom
611 def zoom(self, zoom):
612 self.page.set_zoom(zoom)
616 ox, oy = self.page.off
618 self.page.set_off((ox + px, oy + py))
620 def key(self, wdg, ev, data=None):
621 if ev.keyval in [ord('Q'), ord('q')]:
623 elif ev.keyval in [65307]:
624 if self.page is not None:
625 self.pagefetch.set(None)
626 self.imgfetch.set(None)
627 if self.page is not None:
628 if ev.keyval in [ord('O'), ord('o')]:
631 elif ev.keyval in [ord('P'), ord('p')]:
634 elif ev.keyval in [ord('[')]:
635 self.zoom = min(self.zoom * 1.25, 3)
636 elif ev.keyval in [ord(']')]:
638 elif ev.keyval in [ord('h')]:
640 elif ev.keyval in [ord('j')]:
642 elif ev.keyval in [ord('k')]:
644 elif ev.keyval in [ord('l')]:
646 elif ev.keyval in [ord('H')]:
647 self.page.set_off((0, self.page.off[1]))
648 elif ev.keyval in [ord('J')]:
649 self.page.set_off((self.page.off[0], self.page.get_asize()[1]))
650 elif ev.keyval in [ord('K')]:
651 self.page.set_off((self.page.off[1], 0))
652 elif ev.keyval in [ord('L')]:
653 self.page.set_off((self.page.get_asize()[0], self.page.off[1]))
654 if self.point is not None:
655 if ev.keyval in [ord(' ')]:
656 if self.page and self.page.off[1] + self.page.get_asize()[1] < self.page.get_zsize()[1]:
657 self.pan((0, self.page.get_asize()[1] - 50))
659 self.fetchpage(self.point.next, lambda page: self.preload.set(preload(relpageget(page, False, self.cache))))
660 elif ev.keyval in [65288]:
661 if self.page and self.page.off[1] > 0:
662 self.pan((0, -(self.page.get_asize()[1] - 50)))
664 self.fetchpage(self.point.prev, lambda page: self.preload.set(preload(relpageget(page, True, self.cache))))
665 elif ev.keyval in [ord('R'), ord('r')]:
666 page = self.point.cur.cur
668 self.imgfetch.set(imgfetch(self.cache[page]))
673 gobject.type_register(reader)