From fd58087a6f50885a56fbcf54822e6afea8c00e86 Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Fri, 30 Jan 2015 08:31:08 +0100
Subject: [PATCH] Referentials implementation

---
 src/cutecoin/core/account.py   | 43 ++++++++++++++--------------------
 src/cutecoin/gui/mainwindow.py | 16 +++++++------
 2 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index 314428cd..a57ade90 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -29,6 +29,20 @@ class Account(object):
     be locally referenced by only one account.
     '''
 
+    @staticmethod
+    def units(units, community):
+        return units
+
+    @staticmethod
+    def relative(units, community):
+        ud = community.dividend()
+        relative_value = units / float(ud)
+        return relative_value
+
+    referentials = {'Units': (units.__func__, '{0}'),
+                    'UD': (relative.__func__, 'UD {0}')
+                    }
+
     def __init__(self, salt, pubkey, name, communities, wallets, contacts,
                  dead_communities):
         '''
@@ -41,7 +55,7 @@ class Account(object):
         self.dead_communities = dead_communities
         self.wallets = wallets
         self.contacts = contacts
-        self.referential = 0
+        self.referential = 'Units'
 
     @classmethod
     def create(cls, name, communities, wallets, confpath):
@@ -108,33 +122,10 @@ class Account(object):
 
     @property
     def units_to_ref(self):
-        def units(units, community):
-            return units
-
-        def relative(units, community):
-            ud = community.dividend()
-            relative_value = units / float(ud)
-            return relative_value
-
-        def units_to_zero(units, community):
-            monetary_mass = community.monetary_mass()
-            to_zero_value = units - (monetary_mass / 2)
-            return to_zero_value
-
-        def relative_to_zero(units, community):
-            monetary_mass = community.monetary_mass()
-            ud = community.dividend()
-            relative_mass = monetary_mass / float(ud)
-            relative_value = units / float(ud)
-            to_zero_value = relative_value - (relative_mass / 2)
-            return to_zero_value
-
-        referentials = [units, relative, units_to_zero, relative_to_zero]
-        return referentials[self.referential]
+        return Account.referentials[self.referential][0]
 
     def ref_name(self, currency):
-        referentials = ['{0}', 'UD {0}', '{0} -> 0', 'UD {0} -> 0']
-        return referentials[self.referential].format(currency)
+        return Account.referentials[self.referential][1].format(currency)
 
     def set_walletpool_size(self, size, password):
         logging.debug("Defining wallet pool size")
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index 1e2bba81..f21d2d38 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -16,6 +16,7 @@ from .import_account import ImportAccountDialog
 from .certification import CertificationDialog
 from .password_asker import PasswordAskerDialog
 from ..tools.exceptions import NoPeerAvailable
+from ..core.account import Account
 from ..__init__ import __version__
 
 import logging
@@ -71,9 +72,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
 
         self.combo_referential = QComboBox(self)
         self.combo_referential.setEnabled(False)
-        self.combo_referential.currentIndexChanged.connect(self.referential_changed)
-        self.combo_referential.addItems(("Units", "UD",
-                                         "Units to zero", "UD to zero"))
+        self.combo_referential.currentTextChanged.connect(self.referential_changed)
 
         self.status_label = QLabel("", self.statusbar)
         self.statusbar.addPermanentWidget(self.status_label)
@@ -105,11 +104,12 @@ class MainWindow(QMainWindow, Ui_MainWindow):
                     error,
                     QMessageBox.Ok)
 
-    @pyqtSlot(int)
-    def referential_changed(self, index):
+    @pyqtSlot(str)
+    def referential_changed(self, text):
         if self.app.current_account:
-            self.app.current_account.set_display_referential(index)
-            self.currencies_tabwidget.currentWidget().referential_changed()
+            self.app.current_account.set_display_referential(text)
+            if self.currencies_tabwidget.currentWidget():
+                self.currencies_tabwidget.currentWidget().referential_changed()
 
     def action_change_account(self, account_name):
         self.busybar.show()
@@ -190,6 +190,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
             self.action_set_as_default.setEnabled(self.app.current_account.name
                                                   != self.app.default_account)
             self.password_asker = PasswordAskerDialog(self.app.current_account)
+
+            self.combo_referential.addItems(Account.referentials.keys())
             self.combo_referential.setEnabled(True)
             self.menu_contacts.setEnabled(True)
             self.action_configure_parameters.setEnabled(True)
-- 
GitLab