davemds pushed a commit to branch master.

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

commit a473d92f5105c975357fcdf63568d036e06e7867
Author: davemds <d...@gurumeditation.it>
Date:   Fri Nov 8 23:19:44 2013 +0100

    Epour: factorize the preference dialogs
    
    Implemented a base class used by all the 3 preferences dialogs
---
 epour/gui/Preferences.py | 115 +++++++++++++++++++----------------------------
 1 file changed, 46 insertions(+), 69 deletions(-)

diff --git a/epour/gui/Preferences.py b/epour/gui/Preferences.py
index 4014f90..a7c4729 100644
--- a/epour/gui/Preferences.py
+++ b/epour/gui/Preferences.py
@@ -68,15 +68,33 @@ EXPAND_HORIZ = 1.0, 0.0
 FILL_BOTH = -1.0, -1.0
 FILL_HORIZ = -1.0, 0.5
 
-class PreferencesGeneral(InnerWindow):
+class PreferencesDialog(InnerWindow):
+    """ Base class for all preferences dialogs """
+    def __init__(self, parent):
+        InnerWindow.__init__(self, parent)
+
+        self.scroller = Scroller(self)
+        self.scroller.policy = ELM_SCROLLER_POLICY_OFF, 
ELM_SCROLLER_POLICY_AUTO
+        self.content = self.scroller
+        
+        self.box = Box(self)
+        self.box.size_hint_weight = 1.0, 1.0
+        self.scroller.content = self.box
+
+    def activate(self):
+        btn = Button(self, text="Close")
+        btn.callback_clicked_add(lambda x: self.delete())
+        self.box.pack_end(btn)
+        btn.show()
+        
+        InnerWindow.activate(self)
+
+class PreferencesGeneral(PreferencesDialog):
+    """ General preference dialog """
     def __init__(self, parent, session):
         self.session = session
         conf = session.conf
-        InnerWindow.__init__(self, parent.win)
-
-        mbox = Box(self)
-        mbox.size_hint_weight = 1.0, 1.0
-        mbox.size_hint_align = -1.0, -1.0
+        PreferencesDialog.__init__(self, parent.win)
 
         limits = Limits(self, session)
         ports = ListenPorts(self, session)
@@ -107,20 +125,10 @@ class PreferencesGeneral(InnerWindow):
         sep2 = Separator(self)
         sep2.horizontal = True
 
-        xbtn = Button(self)
-        xbtn.text_set("Close")
-        xbtn.callback_clicked_add(lambda x: self.delete())
-
-        for w in ports, limits, dlsel, pe, pad, \
-            sep1, chk1, chk2, sep2, xbtn:
+        for w in ports, limits, dlsel, pe, pad, sep1, chk1, chk2, sep2:
             w.show()
-            mbox.pack_end(w)
+            self.box.pack_end(w)
 
-        scr = Scroller(self)
-        scr.policy = ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO
-        scr.content = mbox
-
-        self.content = scr
         self.activate()
 
 class DataStorageSelector(Frame):
@@ -205,13 +213,10 @@ class ListenPorts(Frame):
         self.session.conf.set("Settings", "listen_low", str(low))
         self.session.conf.set("Settings", "listen_high", str(high))
 
-class PreferencesProxy(InnerWindow):
+class PreferencesProxy(PreferencesDialog):
+    """ Proxy preference dialog """
     def __init__(self, parent, session):
-        InnerWindow.__init__(self, parent.win)
-        self.session = session
-
-        b = Box(parent.win)
-        b.size_hint_weight = 1.0, 1.0
+        PreferencesDialog.__init__(self, parent.win)
 
         proxies = [
             ["Proxy for torrent peer connections",
@@ -224,25 +229,11 @@ class PreferencesProxy(InnerWindow):
                 session.dht_proxy, session.set_dht_proxy],
         ]
 
-        xbtn = Button(self)
-        xbtn.text_set("Close")
-        xbtn.callback_clicked_add(lambda x: self.delete())
-
         for title, rfunc, wfunc in proxies:
-            pg = ProxyGroup(parent.win, title, rfunc, wfunc)
+            pg = ProxyGroup(self, title, rfunc, wfunc)
             pg.show()
-            b.pack_end(pg)
-
-        b.pack_end(xbtn)
-        xbtn.show()
+            self.box.pack_end(pg)
 
-        b.show()
-
-        scr = Scroller(self)
-        scr.policy = ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO
-        scr.content = b
-
-        self.content = scr
         self.activate()
 
 class ProxyGroup(Frame):
@@ -435,9 +426,10 @@ class ActSWithLabel(Box):
     def get_value(self):
         return self.vd[self.w.indicator_pos]
 
-class PreferencesSession(InnerWindow):
+class PreferencesSession(PreferencesDialog):
+    """ Session preference dialog """
     def __init__(self, parent, session):
-        InnerWindow.__init__(self, parent.win)
+        PreferencesDialog.__init__(self, parent.win)
 
         # TODO: Construct and populate this with an Idler
 
@@ -450,10 +442,9 @@ class PreferencesSession(InnerWindow):
 
         s = session.settings()
 
-        t = Table(parent.win)
-        t.padding = 5, 5
-        t.size_hint_weight = 1.0, 1.0
-        t.size_hint_align = -1.0, -1.0
+        t = Table(self, padding=(5,5))
+        self.box.pack_end(t)
+        t.show()
 
         i = 0
 
@@ -465,7 +456,7 @@ class PreferencesSession(InnerWindow):
             try:
                 a = getattr(s, k)
                 if isinstance(a, lt.disk_cache_algo_t):
-                    w = Spinner(parent.win)
+                    w = Spinner(self)
                     w.size_hint_align = 1.0, 0.0
                     # XXX: lt-rb python bindings don't have all values.
                     w.min_max = 0, 2 #len(lt.disk_cache_algo_t.values.keys())
@@ -473,16 +464,16 @@ class PreferencesSession(InnerWindow):
                         w.special_value_add(val, name)
                     w.value = a
                 elif isinstance(a, bool):
-                    w = Check(parent.win)
+                    w = Check(self)
                     w.size_hint_align = 1.0, 0.0
                     w.style = "toggle"
                     w.state = a
                 elif isinstance(a, int):
-                    w = Spinner(parent.win)
+                    w = Spinner(self)
                     w.min_max = INT_MIN, INT_MAX
                     w.value = a
                 elif isinstance(a, float):
-                    w = Slider(parent.win)
+                    w = Slider(self)
                     w.size_hint_align = -1.0, 0.0
                     w.size_hint_weight = 1.0, 0.0
                     w.unit_format = "%1.2f"
@@ -496,19 +487,19 @@ class PreferencesSession(InnerWindow):
                     #      which doesn't have a python equivalent.
                     continue
                 elif k == "user_agent":
-                    w = Entry(parent.win)
+                    w = Entry(self)
                     w.size_hint_align = 1.0, 0.0
                     w.size_hint_weight = 1.0, 0.0
                     w.single_line = True
                     w.editable = False
                     w.entry = cgi.escape(a)
                 else:
-                    w = Entry(parent.win)
+                    w = Entry(self)
                     w.size_hint_align = -1.0, 0.0
                     w.size_hint_weight = 1.0, 0.0
                     w.single_line = True
                     w.entry = cgi.escape(a)
-                l = Label(parent.win)
+                l = Label(self)
                 l.text = k.replace("_", " ").capitalize()
                 l.size_hint_align = 0.0, 0.0
                 l.size_hint_weight = 1.0, 0.0
@@ -524,28 +515,14 @@ class PreferencesSession(InnerWindow):
             except TypeError:
                 pass #print("Error {}".format(k))
 
-
-        save_btn = Button(parent.win)
+        save_btn = Button(self)
         save_btn.text = "Apply session settings"
         save_btn.callback_clicked_add(self.apply_settings, widgets, session)
         save_btn.show()
-        t.pack(save_btn, 0, i, 2, 1)
-        i += 1
+        self.box.pack_end(save_btn)
 
-        xbtn = Button(self)
-        xbtn.text_set("Close")
-        xbtn.callback_clicked_add(lambda x: self.delete())
-        xbtn.show()
-        t.pack(xbtn, 0, i, 2, 1)
-
-        scr = Scroller(self)
-        scr.policy = ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO
-        scr.content = t
-
-        self.content = scr
         self.activate()
 
-
     def apply_settings(self, btn, widgets, session):
         s = lt.session_settings()
 

-- 


Reply via email to