kuuko pushed a commit to branch master. http://git.enlightenment.org/apps/epour.git/commit/?id=cab124ca29d9ae1070ebbec837b3bfb4926aa40c
commit cab124ca29d9ae1070ebbec837b3bfb4926aa40c Author: Kai Huuhko <kai.huu...@gmail.com> Date: Mon Jul 7 09:54:46 2014 +0300 Add a bit of sanity to UnitSpinner --- epour/gui/Preferences.py | 5 +---- epour/gui/TorrentSelector.py | 3 +-- epour/gui/Widgets.py | 27 +++++++++++++++++---------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/epour/gui/Preferences.py b/epour/gui/Preferences.py index f678024..6635ae1 100644 --- a/epour/gui/Preferences.py +++ b/epour/gui/Preferences.py @@ -688,9 +688,6 @@ class Limits(Frame): self.text = "Limits" self.size_hint_align = FILL_HORIZ - base = 1024 - units = "bytes/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s" - t = Table(parent) for r, values in enumerate(( ("Upload limit", @@ -714,7 +711,7 @@ class Limits(Frame): t.pack(l, 0, r, 1, 1) l.show() - usw = UnitSpinner(parent, base, units) + usw = UnitSpinner(parent, "B/s", 1024, UnitSpinner.binary_prefixes) usw.size_hint_weight = EXPAND_HORIZ usw.size_hint_align = FILL_HORIZ usw.set_value(rfunc()) diff --git a/epour/gui/TorrentSelector.py b/epour/gui/TorrentSelector.py index 195e65b..ffa7b71 100644 --- a/epour/gui/TorrentSelector.py +++ b/epour/gui/TorrentSelector.py @@ -195,8 +195,7 @@ class TorrentSelector(StandardWindow): for n in ( "upload_limit", "download_limit" ): - units = "bytes/s", "KiB/s", "MiB/s", "GiB/s", "TiB/s" - s = UnitSpinner(opt_box, 1024, units) + s = UnitSpinner(opt_box, "B/s", 1024, UnitSpinner.binary_prefixes) s.size_hint_weight = EXPAND_HORIZ s.size_hint_align = FILL_HORIZ s.set_value(0) diff --git a/epour/gui/Widgets.py b/epour/gui/Widgets.py index e12cf89..2ca5efe 100644 --- a/epour/gui/Widgets.py +++ b/epour/gui/Widgets.py @@ -14,10 +14,15 @@ FILL_HORIZ = -1.0, 0.5 class UnitSpinner(Box): - def __init__(self, parent, base, units): - self.base = base # the divisor/multiplier for units - self.units = units # a list of strings with the base unit description - # at index 0 + + si_prefixes = ("", "k", "M", "G", "T", "P") + binary_prefixes = ("", "Ki", "Mi", "Gi", "Ti", "Pi") + + def __init__(self, parent, unit, base=1000, prefixes=si_prefixes): + + self.unit = unit + self.base = base + self.prefixes = prefixes super(UnitSpinner, self).__init__(parent) self.horizontal = True @@ -32,11 +37,13 @@ class UnitSpinner(Box): self.pack_end(s) hs = self.hoversel = Hoversel(parent) - for u in units: - hs.item_add(u, None, 0, lambda x=hs, y=None, u=u: x.text_set(u)) + for p in prefixes: + hs.item_add(p + unit) hs.show() self.pack_end(hs) + hs.callback_selected_add(lambda x, y: x.text_set(y.text)) + def callback_changed_add(self, func, delay=None): self.spinner.callback_changed_add(self.changed_cb, func, delay) self.hoversel.callback_selected_add(self.changed_cb, func, delay) @@ -59,7 +66,7 @@ class UnitSpinner(Box): def get_value(self): return self.spinner.value * ( - self.base ** self.units.index(self.hoversel.text) + self.base ** self.prefixes.index(self.hoversel.text+self.unit) ) def set_value(self, v): @@ -69,11 +76,11 @@ class UnitSpinner(Box): i += 1 v = float(v) / float(self.base) - if i > len(self.units): - i = len(self.units) - 1 + if i > len(self.prefixes): + i = -1 self.spinner.value = v - self.hoversel.text = self.units[i] + self.hoversel.text = self.prefixes[i] + self.unit class Information(object): --