From abad19a010eb61d0b884300cc8e5a417ea63f774 Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Sat, 13 Jun 2015 11:35:22 +0200
Subject: [PATCH] Add referential type to detect quantitative or relative type

---
 src/cutecoin/core/account.py         | 25 +++++++++++++++++++++----
 src/cutecoin/gui/informations_tab.py | 11 ++++++++---
 src/cutecoin/gui/transactions_tab.py | 21 ++++++---------------
 src/cutecoin/gui/transfer.py         | 14 ++++++++++----
 src/cutecoin/gui/wallets_tab.py      | 19 ++++++++++---------
 src/cutecoin/models/txhistory.py     |  7 +++++--
 src/cutecoin/models/wallets.py       |  7 +++++--
 7 files changed, 65 insertions(+), 39 deletions(-)

diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index e713887c..165adb64 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -90,14 +90,24 @@ class Account(QObject):
     Each account has only one key, and a key can
     be locally referenced by only one account.
     """
+    # referentials are defined here
+    # it is a list of tupple, each tupple contains :
+    # (
+    #   function used to calculate value,
+    #   format string to display value,
+    #   function used to calculate on differential value,
+    #   format string to display differential value,
+    #   translated name of referential,
+    #   type relative "r" or quantitative "q" to help choose precision on display
+    # )
     referentials = (
-        (quantitative, '{0}', quantitative, '{0}', QT_TRANSLATE_NOOP('Account', 'Units')),
+        (quantitative, '{0}', quantitative, '{0}', QT_TRANSLATE_NOOP('Account', 'Units'), 'q'),
         (relative, QT_TRANSLATE_NOOP('Account', 'UD {0}'), relative, QT_TRANSLATE_NOOP('Account', 'UD {0}'),
-         QT_TRANSLATE_NOOP('Account', 'UD')),
+         QT_TRANSLATE_NOOP('Account', 'UD'), 'r'),
         (quantitative_zerosum, QT_TRANSLATE_NOOP('Account', 'Q0 {0}'), quantitative, '{0}',
-         QT_TRANSLATE_NOOP('Account', 'Quant Z-sum')),
+         QT_TRANSLATE_NOOP('Account', 'Quant Z-sum'), 'q'),
         (relative_zerosum, QT_TRANSLATE_NOOP('Account', 'R0 {0}'), relative, QT_TRANSLATE_NOOP('Account', 'UD {0}'),
-         QT_TRANSLATE_NOOP('Account', 'Relat Z-sum'))
+         QT_TRANSLATE_NOOP('Account', 'Relat Z-sum'), 'r')
     )
 
     loading_progressed = pyqtSignal(int, int)
@@ -250,6 +260,13 @@ class Account(QObject):
         text = QCoreApplication.translate('Account', Account.referentials[self.referential][3])
         return text.format(currency)
 
+    def ref_type(self):
+        """
+        Return type of referential ('q' or 'r', for quantitative or relative)
+        :return: str
+        """
+        return Account.referentials[self.referential][5]
+
     def set_walletpool_size(self, size, password):
         '''
         Change the size of the wallet pool
diff --git a/src/cutecoin/gui/informations_tab.py b/src/cutecoin/gui/informations_tab.py
index 14d42f3d..cba4bc49 100644
--- a/src/cutecoin/gui/informations_tab.py
+++ b/src/cutecoin/gui/informations_tab.py
@@ -49,7 +49,9 @@ class InformationsTabWidget(QWidget, Ui_InformationsTabWidget):
 
         if block:
             ud = self.get_referential_diff_value(block['dividend'])
-            if isinstance(ud, int):
+            # if referential type is quantitative...
+            if self.account.ref_type() == 'q':
+                # display int values
                 # use the float type of 64bits, to avoid display a 32bit signed integer...
                 localized_ud = QLocale().toString(float(ud), 'f', 0)
                 localized_mass_per_member = QLocale().toString(
@@ -58,8 +60,8 @@ class InformationsTabWidget(QWidget, Ui_InformationsTabWidget):
                 localized_monetary_mass = QLocale().toString(
                     float(self.get_referential_diff_value(block['monetaryMass'])), 'f', 0
                 )
-
             else:
+                # display float values
                 localized_ud = QLocale().toString(ud, 'f', 6)
                 localized_mass_per_member = QLocale().toString(
                     self.get_referential_diff_value(block['monetaryMass'] / block['membersCount']), 'f', 6
@@ -106,7 +108,9 @@ class InformationsTabWidget(QWidget, Ui_InformationsTabWidget):
             self.label_general.setText(self.tr('No Universal Dividend created yet.'))
 
         if block:
-            if isinstance(ud, int):
+            # if referential type is quantitative...
+            if self.account.ref_type() == 'q':
+                # display int values
                 localized_ud_t1 = QLocale().toString(
                     float(
                         self.get_referential_diff_value(
@@ -119,6 +123,7 @@ class InformationsTabWidget(QWidget, Ui_InformationsTabWidget):
                     0
                 )
             else:
+                # display float values
                 localized_ud_t1 = QLocale().toString(
                     float(
                         self.get_referential_diff_value(
diff --git a/src/cutecoin/gui/transactions_tab.py b/src/cutecoin/gui/transactions_tab.py
index 5b825e37..4d6b382e 100644
--- a/src/cutecoin/gui/transactions_tab.py
+++ b/src/cutecoin/gui/transactions_tab.py
@@ -94,21 +94,12 @@ class TransactionsTabWidget(QWidget, Ui_transactionsTabWidget):
 
         proxy = self.table_history.model()
         balance = proxy.deposits - proxy.payments
-        if isinstance(proxy.deposits, int):
-            localized_deposits = QLocale().toString(
-                self.app.current_account.units_to_ref(proxy.deposits, self.community))
-            localized_payments = QLocale().toString(
-                self.app.current_account.units_to_ref(proxy.payments, self.community))
-            localized_balance = QLocale().toString(
-                self.app.current_account.units_to_diff_ref(balance, self.community))
-
-        else:
-            localized_deposits = QLocale().toString(
-                self.app.current_account.units_to_ref(proxy.deposits, self.community), 'f', 6)
-            localized_payments = QLocale().toString(
-                self.app.current_account.units_to_ref(proxy.payments, self.community), 'f', 6)
-            localized_balance = QLocale().toString(
-                self.app.current_account.units_to_diff_ref(balance, self.community), 'f', 6)
+        localized_deposits = QLocale().toString(
+            self.app.current_account.units_to_diff_ref(proxy.deposits, self.community))
+        localized_payments = QLocale().toString(
+            self.app.current_account.units_to_diff_ref(proxy.payments, self.community))
+        localized_balance = QLocale().toString(
+            self.app.current_account.units_to_diff_ref(balance, self.community))
 
         self.label_deposit.setText(QCoreApplication.translate("TransactionsTabWidget", "<b>Deposits</b> {:} {:}").format(
             localized_deposits,
diff --git a/src/cutecoin/gui/transfer.py b/src/cutecoin/gui/transfer.py
index ad0aa4a2..070b4e26 100644
--- a/src/cutecoin/gui/transfer.py
+++ b/src/cutecoin/gui/transfer.py
@@ -124,9 +124,12 @@ Please try again later"""))
         amount = self.wallet.value(self.community)
         ref_amount = self.account.units_to_ref(amount, self.community)
         ref_name = self.account.ref_name(self.community.currency)
-        if isinstance(ref_amount, int):
-            ref_amount = QLocale().toString(ref_amount)
+        # if referential type is quantitative...
+        if self.account.ref_type() == 'q':
+            # display int values
+            ref_amount = QLocale().toString(float(ref_amount), 'f', 0)
         else:
+            # display float values
             ref_amount = QLocale().toString(ref_amount, 'f', 6)
         self.label_total.setText("{0} {1}".format(ref_amount, ref_name))
         self.spinbox_amount.setSuffix(" " + self.community.currency)
@@ -141,9 +144,12 @@ Please try again later"""))
         amount = self.wallet.value(self.community)
         ref_amount = self.account.units_to_ref(amount, self.community)
         ref_name = self.account.ref_name(self.community.currency)
-        if isinstance(ref_amount, int):
-            ref_amount = QLocale().toString(ref_amount)
+        # if referential type is quantitative...
+        if self.account.ref_type() == 'q':
+            # display int values
+            ref_amount = QLocale().toString(float(ref_amount), 'f', 0)
         else:
+            # display float values
             ref_amount = QLocale().toString(ref_amount, 'f', 6)
         self.label_total.setText("{0} {1}".format(ref_amount, ref_name))
         self.spinbox_amount.setValue(0)
diff --git a/src/cutecoin/gui/wallets_tab.py b/src/cutecoin/gui/wallets_tab.py
index c652b942..22cdc574 100644
--- a/src/cutecoin/gui/wallets_tab.py
+++ b/src/cutecoin/gui/wallets_tab.py
@@ -101,16 +101,17 @@ class WalletsTabWidget(QWidget, Ui_WalletsTab):
 
         amount = self.account.amount(self.community)
         maximum = self.community.monetary_mass
-
-        if isinstance(self.get_referential_value(amount), int):
+        # if referential type is quantitative...
+        if self.account.ref_type() == 'q':
+            # display int values
             localized_amount = QLocale().toString(float(self.get_referential_value(amount)), 'f', 0)
-        else:
-            localized_amount = QLocale().toString(self.get_referential_value(amount), 'f', 6)
-
-        if isinstance(self.get_referential_value(maximum), int):
+            localized_minimum = QLocale().toString(float(self.get_referential_value(0)), 'f', 0)
             localized_maximum = QLocale().toString(float(self.get_referential_value(maximum)), 'f', 0)
         else:
-            localized_maximum = QLocale().toString(self.get_referential_value(maximum), 'f', 6)
+            # display float values
+            localized_amount = QLocale().toString(float(self.get_referential_value(amount)), 'f', 6)
+            localized_minimum = QLocale().toString(float(self.get_referential_value(0)), 'f', 6)
+            localized_maximum = QLocale().toString(float(self.get_referential_value(maximum)), 'f', 6)
 
         # set infos in label
         self.label_balance.setText(
@@ -123,11 +124,11 @@ class WalletsTabWidget(QWidget, Ui_WalletsTab):
                 self.tr("Your money share "),
                 self.tr("{:.2f}%").format(amount / maximum * 100) if maximum != 0 else "0%",
                 self.tr("Your part "),
-                self.tr("{:} {:} in [{:.2f} ; {:}] {:}")
+                self.tr("{:} {:} in [{:} ; {:}] {:}")
                 .format(
                     localized_amount,
                     self.get_referential_name(),
-                    self.get_referential_value(0),
+                    localized_minimum,
                     localized_maximum,
                     self.get_referential_name()
                 )
diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py
index ea09932a..bebd9e13 100644
--- a/src/cutecoin/models/txhistory.py
+++ b/src/cutecoin/models/txhistory.py
@@ -109,9 +109,12 @@ class TxFilterProxyModel(QSortFilterProxyModel):
                 if source_data is not "":
                     amount_ref = self.account.units_to_diff_ref(source_data,
                                                                 self.community)
-                    if isinstance(amount_ref, int):
-                        return QLocale().toString(amount_ref)
+                    # if referential type is quantitative...
+                    if self.account.ref_type() == 'q':
+                        # display int values
+                        return QLocale().toString(float(amount_ref), 'f', 0)
                     else:
+                        # display float values
                         return QLocale().toString(amount_ref, 'f', 6)
 
         if role == Qt.FontRole:
diff --git a/src/cutecoin/models/wallets.py b/src/cutecoin/models/wallets.py
index efb0a247..56a59423 100644
--- a/src/cutecoin/models/wallets.py
+++ b/src/cutecoin/models/wallets.py
@@ -39,9 +39,12 @@ class WalletsFilterProxyModel(QSortFilterProxyModel):
                 return source_data
             if source_index.column() == self.sourceModel().columns_types.index('amount'):
                 amount_ref = self.account.units_to_ref(source_data, self.community)
-                if isinstance(amount_ref, int):
-                    return QLocale().toString(amount_ref)
+                # if referential type is quantitative...
+                if self.account.ref_type() == 'q':
+                    # display int values
+                    return QLocale().toString(float(amount_ref), 'f', 0)
                 else:
+                    # display float values
                     return QLocale().toString(amount_ref, 'f', 6)
 
         if role == Qt.TextAlignmentRole:
-- 
GitLab