kuuko pushed a commit to branch master.

http://git.enlightenment.org/apps/epour.git/commit/?id=434d0a2b3bb13d78fe83996734bdccb349ceb197

commit 434d0a2b3bb13d78fe83996734bdccb349ceb197
Author: Kai Huuhko <[email protected]>
Date:   Sun Jul 6 12:36:21 2014 +0300

    Optimize the files selector, add a timer to update every 5 seconds
---
 epour/gui/TorrentProps.py | 140 ++++++++++++++++++----------------------------
 1 file changed, 53 insertions(+), 87 deletions(-)

diff --git a/epour/gui/TorrentProps.py b/epour/gui/TorrentProps.py
index b20703f..0a03ac0 100644
--- a/epour/gui/TorrentProps.py
+++ b/epour/gui/TorrentProps.py
@@ -31,7 +31,7 @@ import libtorrent as lt
 from efl import ecore
 from efl.evas import EVAS_HINT_EXPAND, EVAS_HINT_FILL
 from efl.elementary.genlist import Genlist, GenlistItemClass, \
-    ELM_GENLIST_ITEM_NONE, ELM_GENLIST_ITEM_TREE
+    ELM_GENLIST_ITEM_NONE, ELM_GENLIST_ITEM_TREE, ELM_GENLIST_ITEM_FIELD_TEXT
 from efl.elementary.window import StandardWindow
 from efl.elementary.innerwindow import InnerWindow
 from efl.elementary.button import Button
@@ -58,6 +58,41 @@ from collections import defaultdict
 FILE_MARKER = '<files>'
 
 
+class FileClass(GenlistItemClass):
+
+    def text_get(self, obj, part, data):
+        fn, file_entry, n = data
+        progress = obj.data["progress"][n]
+        return "{} - {}/{} ({:.0%})".format(
+            fn,
+            intrepr(progress),
+            intrepr(file_entry.size),
+            float(progress)/float(file_entry.size)
+        )
+
+    def content_get(self, obj, part, data):
+        fn, file_entry, n = data
+        h = obj.data["handle"]
+        if part == "elm.swallow.icon":
+            check = Check(obj)
+            check.tooltip_text_set("Enable/disable file download")
+            check.state = h.file_priority(n)
+            check.callback_changed_add(lambda x: h.file_priority(n, x.state))
+            return check
+
+
+class DirectoryClass(GenlistItemClass):
+    def text_get(self, obj, part, data):
+        dir_name, d = data
+        return "{}".format(
+            dir_name
+        )
+
+
+ITEM_CLASS_FILES = FileClass()
+ITEM_CLASS_DIRS = DirectoryClass()
+
+
 def attach(branch, file_entry, n, trunk):
     """Insert a branch of directories on its trunk."""
     parts = branch.split('/', 1)
@@ -152,6 +187,15 @@ class TorrentFiles(StandardWindow):
             )
         filelist.data["handle"] = h
         filelist.data["file_dict"] = file_dict
+        filelist.data["progress"] = h.file_progress()
+
+        def update_progress(h):
+            filelist.data["progress"] = h.file_progress()
+            for it in filelist.realized_items:
+                it.fields_update("*", ELM_GENLIST_ITEM_FIELD_TEXT)
+            return True
+
+        ecore.Timer(5.0, update_progress, h)
 
         filelist.callback_expand_request_add(self.expand_request_cb)
         filelist.callback_contract_request_add(self.contract_request_cb)
@@ -203,9 +247,6 @@ class TorrentFiles(StandardWindow):
         i = h.get_torrent_info()
         entries = i.files()
 
-        f_cls = FileClass(h)
-        d_cls = DirectoryClass()
-
         for n, f in enumerate(entries):
             attach(f.path, f, n, file_dict)
 
@@ -217,14 +258,14 @@ class TorrentFiles(StandardWindow):
                     if value:
                         for fn, fe, n in value:
                             filelist.item_append(
-                                f_cls,
+                                ITEM_CLASS_FILES,
                                 (fn, fe, n),
                                 parent,
                                 ELM_GENLIST_ITEM_NONE)
                 else:
                     it = filelist.item_append(
-                        d_cls,
-                        key,
+                        ITEM_CLASS_DIRS,
+                        (key, value),
                         parent,
                         ELM_GENLIST_ITEM_TREE)
                     it.expanded = True
@@ -238,22 +279,7 @@ class TorrentFiles(StandardWindow):
         item.expanded = False
 
     def item_expanded_cb(self, gl, item):
-        dir_path = [item.data]
-        it = item.parent
-        while it:
-            dir_path.append(it.data)
-            it = it.parent
-        dir_path.reverse()
-
-        h = gl.data["handle"]
-        file_dict = gl.data["file_dict"]
-
-        f_cls = FileClass(h)
-        d_cls = DirectoryClass()
-
-        d = file_dict
-        for p in dir_path:
-            d = d[p]
+        dir_node, d = item.data
 
         for key in sorted(d.keys()):
             value = d[key]
@@ -261,14 +287,14 @@ class TorrentFiles(StandardWindow):
                 if value:
                     for fn, fe, n in value:
                         gl.item_append(
-                            f_cls,
+                            ITEM_CLASS_FILES,
                             (fn, fe, n),
                             item,
                             ELM_GENLIST_ITEM_NONE)
             else:
-                it = gl.item_append(
-                    d_cls,
-                    key,
+                gl.item_append(
+                    ITEM_CLASS_DIRS,
+                    (key, value),
                     item,
                     ELM_GENLIST_ITEM_TREE)
 
@@ -297,66 +323,6 @@ class TorrentFiles(StandardWindow):
             os.startfile(path)
 
 
-class FileClass(GenlistItemClass):
-
-    def __init__(self, h):
-        GenlistItemClass.__init__(self)
-        self.h = h
-        self.progress = h.file_progress()
-
-        def update_progress():
-            self.progress = h.file_progress()
-
-        ecore.Timer(5.0, update_progress)
-
-    def text_get(self, obj, part, data):
-        fn, file_entry, n = data
-        progress = self.progress[n]
-        return "{} - {}/{} ({:.0%})".format(
-            fn,
-            intrepr(progress),
-            intrepr(file_entry.size),
-            float(progress)/float(file_entry.size)
-        )
-
-    def content_get(self, obj, part, data):
-        fn, file_entry, n = data
-        h = obj.data["handle"]
-        if part == "elm.swallow.icon":
-            check = Check(obj)
-            check.tooltip_text_set("Enable/disable file download")
-            check.state = h.file_priorities()[n]
-            check.callback_changed_add(self.toggle_file_enabled, n, h)
-            return check
-
-    def toggle_file_enabled(self, ck, n, h):
-        priorities = h.file_priorities()
-        priorities[n] = int(ck.state)
-        h.prioritize_files(priorities)
-
-
-class DirectoryClass(GenlistItemClass):
-    def text_get(self, obj, part, data):
-        dir_name = data
-        return "{}".format(
-            dir_name
-        )
-
-    # def content_get(self, obj, part, data):
-    #     file_entry, progress, n, h = data
-    #     if part == "elm.swallow.icon":
-    #         check = Check(obj)
-    #         check.tooltip_text_set("Enable/disable file download")
-    #         check.state = h.file_priorities()[n]
-    #         check.callback_changed_add(self.toggle_file_enabled, n, h)
-    #         return check
-
-    # def toggle_file_enabled(self, ck, n, h):
-    #     priorities = h.file_priorities()
-    #     priorities[n] = int(ck.state)
-    #     h.prioritize_files(priorities)
-
-
 class TorrentInfo(Frame):
     def __init__(self, parent, h, *args, **kwargs):
         Frame.__init__(self, parent, *args, **kwargs)

-- 


Reply via email to