kuuko pushed a commit to branch master.

http://git.enlightenment.org/apps/epour.git/commit/?id=44fbfde15f58929997cee3b35f31a6573ccddd4c

commit 44fbfde15f58929997cee3b35f31a6573ccddd4c
Author: Kai Huuhko <[email protected]>
Date:   Mon Jul 7 06:43:53 2014 +0300

    Make torrent tooltips a class, add auto-update for them.
---
 epour/gui/__init__.py | 178 +++++++++++++++++++++++++++++---------------------
 1 file changed, 105 insertions(+), 73 deletions(-)

diff --git a/epour/gui/__init__.py b/epour/gui/__init__.py
index c795abe..281eb50 100644
--- a/epour/gui/__init__.py
+++ b/epour/gui/__init__.py
@@ -259,81 +259,10 @@ class MainInterface(object):
     def _torrent_item_tooltip_cb(self, gl, it, tooltip, h):
         if not h.is_valid():
             return
-        s = h.status()
-        table = Table(tooltip, size_hint_weight=EXPAND_BOTH)
-
-        for i, (name, info) in enumerate((
-            ("Time when added", datetime.fromtimestamp(s.added_time)),
-            ("Time when completed", datetime.fromtimestamp(s.completed_time)),
-            ("All time downloaded", intrepr(s.all_time_download)),
-            ("All time uploaded", intrepr(s.all_time_upload)),
-            ("Total wanted done", intrepr(s.total_wanted_done)),
-            ("Total wanted", intrepr(s.total_wanted)),
-            ("Total downloaded this session",
-                intrepr(s.total_payload_download)),
-            ("Total uploaded this session", intrepr(s.total_payload_upload)),
-            ("Total failed", intrepr(s.total_failed_bytes)),
-            ("Number of seeds", s.num_seeds),
-            ("Number of peers", s.num_peers),
-                )):
-            l1 = Label(table, text=name)
-            l1.show()
-            table.pack(l1, 0, i, 1, 1)
-            l2 = Label(table, text=str(info))
-            l2.show()
-            table.pack(l2, 1, i, 1, 1)
-
-        i += 1
-
-        l = Label(table, text="Pieces")
-        table.pack(l, 0, i, 1, 1)
-        l.show()
-
-        WIDTH = 30
-        HEIGHT = 4
-        TOTAL = WIDTH * HEIGHT
-
-        pieces = s.pieces
-        len_pieces = len(pieces)
-        block_size = len_pieces//TOTAL + bool(len_pieces % TOTAL)
-
-        g = Grid(table, size=(WIDTH, HEIGHT), size_hint_align=FILL_BOTH)
-
-        blocks = []
-
-        for block in chunker(pieces, block_size):
-            if all(block):
-                blocks.append(2)
-            elif any(block):
-                blocks.append(1)
-            else:
-                blocks.append(0)
-
-        len_blocks = len(blocks)
 
-        p = 0
-        for y in xrange(HEIGHT):
-            for x in xrange(WIDTH):
-                if p >= len_blocks:
-                    continue
-                else:
-                    block = blocks[p]
-                    if block == 0:
-                        color = 255, 0, 0
-                    elif block == 1:
-                        color = 255, 255, 0
-                    else:
-                        color = 0, 255, 0
+        tt = TorrentTooltip(tooltip, h)
 
-                rect = Rectangle(g.evas, color=color)
-                g.pack(rect, x, y, 1, 1)
-                rect.show()
-                p += 1
-
-        table.pack(g, 1, i, 1, 1)
-        g.show()
-
-        return table
+        return tt
 
     def add_torrent_item(self, h):
         ihash = str(h.info_hash())
@@ -641,3 +570,106 @@ class ItemMenu(Menu):
     def torrent_props_cb(self, menu, item, h):
         from TorrentProps import TorrentProps
         TorrentProps(self.top_widget, h)
+
+
+class TorrentTooltip(Table):
+
+    items = (
+        ("Time when added", datetime.fromtimestamp, "added_time"),
+        ("Time when completed", datetime.fromtimestamp, "completed_time"),
+        ("All time downloaded", intrepr, "all_time_download"),
+        ("All time uploaded", intrepr, "all_time_upload"),
+        ("Total wanted done", intrepr, "total_wanted_done"),
+        ("Total wanted", intrepr, "total_wanted"),
+        ("Total downloaded this session",
+            intrepr, "total_payload_download"),
+        ("Total uploaded this session", intrepr, "total_payload_upload"),
+        ("Total failed", intrepr, "total_failed_bytes"),
+        ("Number of seeds", None, "num_seeds"),
+        ("Number of peers", None, "num_peers"),
+        )
+
+    def __init__(self, parent, h):
+
+        Table.__init__(self, parent, size_hint_weight=EXPAND_BOTH)
+
+        s = h.status()
+
+        value_labels = []
+
+        for i, (desc, conv, attr_name) in enumerate(self.items):
+            l1 = Label(self, text=desc)
+            l1.show()
+            self.pack(l1, 0, i, 1, 1)
+            v = getattr(s, attr_name)
+            if conv:
+                v = conv(v)
+            l2 = Label(self, text=str(v))
+            value_labels.append(l2)
+            l2.show()
+            self.pack(l2, 1, i, 1, 1)
+
+        i += 1
+
+        l = Label(self, text="Pieces")
+        self.pack(l, 0, i, 1, 1)
+        l.show()
+
+        WIDTH = 30
+        HEIGHT = 4
+        TOTAL = WIDTH * HEIGHT
+
+        pieces = s.pieces
+        len_pieces = len(pieces)
+        block_size = len_pieces//TOTAL + bool(len_pieces % TOTAL)
+
+        g = Grid(self, size=(WIDTH, HEIGHT), size_hint_align=FILL_BOTH)
+
+        blocks = []
+
+        for block in chunker(pieces, block_size):
+            if all(block):
+                blocks.append(2)
+            elif any(block):
+                blocks.append(1)
+            else:
+                blocks.append(0)
+
+        len_blocks = len(blocks)
+
+        p = 0
+        for y in xrange(HEIGHT):
+            for x in xrange(WIDTH):
+                if p >= len_blocks:
+                    continue
+                else:
+                    block = blocks[p]
+                    if block == 0:
+                        color = 255, 0, 0
+                    elif block == 1:
+                        color = 255, 255, 0
+                    else:
+                        color = 0, 255, 0
+
+                rect = Rectangle(g.evas, color=color)
+                g.pack(rect, x, y, 1, 1)
+                rect.show()
+                p += 1
+
+        self.pack(g, 1, i, 1, 1)
+        g.show()
+
+        def update_labels(h, items, value_labels):
+            s = h.status()
+            for i, l in enumerate(value_labels):
+                conv, attr_name = items[i][1:]
+                v = getattr(s, attr_name)
+                if conv:
+                    v = conv(v)
+                l.text = str(v)
+            return True
+
+        # TODO: Update block graph
+
+        self.timer = Timer(1.0, update_labels, h, self.items, value_labels)
+        self.on_del_add(lambda x: self.timer.delete())

-- 


Reply via email to