changeset 950ea9913fee in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset;node=950ea9913fee
description:
        Add symbol widget

        issue9059
        review276871002
diffstat:

 CHANGELOG                                            |   1 +
 COPYRIGHT                                            |   3 +
 tryton/gui/window/about.py                           |   1 +
 tryton/gui/window/view_form/model/field.py           |  22 +++++++++
 tryton/gui/window/view_form/screen/screen.py         |   3 +
 tryton/gui/window/view_form/view/form_gtk/integer.py |  25 +++++++++++
 tryton/gui/window/view_form/view/list.py             |   2 +
 tryton/gui/window/view_form/view/list_gtk/widget.py  |  45 ++++++++++++++++++++
 8 files changed, 102 insertions(+), 0 deletions(-)

diffs (197 lines):

diff -r 60b4dc6f3da9 -r 950ea9913fee CHANGELOG
--- a/CHANGELOG Sun Oct 04 00:35:23 2020 +0200
+++ b/CHANGELOG Fri Oct 09 19:09:06 2020 +0200
@@ -1,3 +1,4 @@
+* Manage symbol widget
 * Fallback to model name as title
 * Manage deletable and writable state
 * Support e-mail template
diff -r 60b4dc6f3da9 -r 950ea9913fee COPYRIGHT
--- a/COPYRIGHT Sun Oct 04 00:35:23 2020 +0200
+++ b/COPYRIGHT Fri Oct 09 19:09:06 2020 +0200
@@ -8,6 +8,9 @@
 Copyright (C) 2010-2019 Nicolas Évrard.
 Copyright (C) 2011-2012 Rodrigo Hübner.
 Copyright (C) 2012-2013 Antoine Smolders.
+Copyright (C) 2020 Maxime Richez
+Copyright (C) 2020 SALUC SA
+
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
diff -r 60b4dc6f3da9 -r 950ea9913fee tryton/gui/window/about.py
--- a/tryton/gui/window/about.py        Sun Oct 04 00:35:23 2020 +0200
+++ b/tryton/gui/window/about.py        Fri Oct 09 19:09:06 2020 +0200
@@ -19,6 +19,7 @@
         'Hartmut Goebel',
         'Korbinian Preisler <[email protected]>',
         'Mathias Behrle <[email protected]>',
+        'Maxime Richez <[email protected]>',
         'Nicolas Évrard <[email protected]>',
         'Sednacom <[email protected]>',
         'Udo Spallek <[email protected]>',
diff -r 60b4dc6f3da9 -r 950ea9913fee tryton/gui/window/view_form/model/field.py
--- a/tryton/gui/window/view_form/model/field.py        Sun Oct 04 00:35:23 
2020 +0200
+++ b/tryton/gui/window/view_form/model/field.py        Fri Oct 09 19:09:06 
2020 +0200
@@ -355,6 +355,28 @@
         shift = int(round(math.log(abs(factor), 10)))
         return (digits[0] + shift, digits[1] - shift)
 
+    def get_symbol(self, record, symbol):
+        if record and symbol in record.group.fields:
+            value = self.get(record) or 0
+            sign = 1
+            if value < 0:
+                sign = -1
+            elif value == 0:
+                sign = 0
+            symbol_field = record.group.fields[symbol]
+            symbol_name = symbol_field.attrs.get('relation')
+            symbol_id = symbol_field.get(record)
+            if symbol_name and symbol_id is not None and symbol_id >= 0:
+                try:
+                    return RPCExecute(
+                        'model', symbol_name, 'get_symbol', symbol_id, sign,
+                        context=record.get_context())
+                except RPCException:
+                    logger.warn(
+                        "Fail to fetch symbol for %s,%s",
+                        symbol_name, symbol_id)
+        return '', 1
+
     def convert(self, value):
         try:
             return locale.atof(value)
diff -r 60b4dc6f3da9 -r 950ea9913fee 
tryton/gui/window/view_form/screen/screen.py
--- a/tryton/gui/window/view_form/screen/screen.py      Sun Oct 04 00:35:23 
2020 +0200
+++ b/tryton/gui/window/view_form/screen/screen.py      Fri Oct 09 19:09:06 
2020 +0200
@@ -209,6 +209,9 @@
                 for attr in ['string', 'factor']:
                     if attributes.get(attr):
                         ofields[name][attr] = attributes[attr]
+                symbol = attributes.get('symbol')
+                if symbol and symbol not in ofields:
+                    ofields[symbol] = fields[symbol]
             fields = ofields
 
         if 'active' in view_tree['fields']:
diff -r 60b4dc6f3da9 -r 950ea9913fee 
tryton/gui/window/view_form/view/form_gtk/integer.py
--- a/tryton/gui/window/view_form/view/form_gtk/integer.py      Sun Oct 04 
00:35:23 2020 +0200
+++ b/tryton/gui/window/view_form/view/form_gtk/integer.py      Fri Oct 09 
19:09:06 2020 +0200
@@ -18,8 +18,17 @@
         self.entry.connect('activate', self.sig_activate)
         self.entry.connect('focus-out-event', lambda *a: self._focus_out())
         self.entry.connect('key-press-event', self.send_modified)
+        self.symbol = attrs.get('symbol')
+        if self.symbol:
+            self.symbol_start = Gtk.Entry(editable=False)
+            self.widget.pack_start(
+                self.symbol_start, expand=False, fill=False, padding=1)
         self.widget.pack_start(self.entry, expand=False, fill=False, padding=0)
         self.factor = float(attrs.get('factor', 1))
+        if self.symbol:
+            self.symbol_end = Gtk.Entry(editable=False)
+            self.widget.pack_start(
+                self.symbol_end, expand=False, fill=False, padding=1)
 
     @property
     def modified(self):
@@ -47,8 +56,24 @@
         return 8
 
     def display(self):
+        def set_symbol(entry, text):
+            if text:
+                entry.set_text(text)
+                entry.set_width_chars(len(text) + 1)
+                entry.show()
+            else:
+                entry.set_text('')
+                entry.hide()
         super().display()
         self.entry.set_width_chars(self.width)
+        if self.field and self.symbol:
+            symbol, position = self.field.get_symbol(self.record, self.symbol)
+            if position < 0.5:
+                set_symbol(self.symbol_start, symbol)
+                set_symbol(self.symbol_end, '')
+            else:
+                set_symbol(self.symbol_start, '')
+                set_symbol(self.symbol_end, symbol)
         value = self.get_client_value()
         self.entry.set_text(value)
         reset_position(self.entry)
diff -r 60b4dc6f3da9 -r 950ea9913fee tryton/gui/window/view_form/view/list.py
--- a/tryton/gui/window/view_form/view/list.py  Sun Oct 04 00:35:23 2020 +0200
+++ b/tryton/gui/window/view_form/view/list.py  Fri Oct 09 19:09:06 2020 +0200
@@ -414,6 +414,8 @@
                 width = int(attributes['width'])
             elif field_attrs:
                 width = default_width.get(field_attrs['type'], 100)
+                if attributes.get('symbol'):
+                    width += 20
             else:
                 width = 80
         column.width = width
diff -r 60b4dc6f3da9 -r 950ea9913fee 
tryton/gui/window/view_form/view/list_gtk/widget.py
--- a/tryton/gui/window/view_form/view/list_gtk/widget.py       Sun Oct 04 
00:35:23 2020 +0200
+++ b/tryton/gui/window/view_form/view/list_gtk/widget.py       Fri Oct 09 
19:09:06 2020 +0200
@@ -207,6 +207,35 @@
             webbrowser.open(value, new=2)
 
 
+class Symbol(Cell):
+    expand = False
+
+    def __init__(self, view, attrs, position):
+        super().__init__()
+        self.attrs = attrs
+        self.symbol = attrs.get('symbol')
+        self.position = position
+        self.renderer = Gtk.CellRendererText()
+        self.view = view
+
+    @realized
+    @CellCache.cache
+    def setter(self, column, cell, store, iter_, user_data=None):
+        record, field = self._get_record_field_from_iter(iter_, store)
+        invisible = field.get_state_attrs(record).get('invisible', False)
+        if invisible:
+            cell.set_property('text', '')
+            cell.set_property('visible', not invisible)
+            return
+        symbol, position = field.get_symbol(record, self.symbol)
+        if round(position) == self.position:
+            cell.set_property('text', symbol)
+            cell.set_property('visible', True)
+        else:
+            cell.set_property('text', '')
+            cell.set_property('visible', False)
+
+
 class GenericText(Cell):
     align = 0
     editable = None
@@ -328,6 +357,22 @@
             renderer = CellRendererInteger
         super(Int, self).__init__(view, attrs, renderer=renderer)
         self.factor = float(attrs.get('factor', 1))
+        self.symbol = attrs.get('symbol')
+        if self.symbol:
+            self.renderer_prefix = Symbol(view, attrs, 0)
+            self.renderer_suffix = Symbol(view, attrs, 1)
+
+    @property
+    def prefixes(self):
+        if self.symbol:
+            return [self.renderer_prefix]
+        return []
+
+    @property
+    def suffixes(self):
+        if self.symbol:
+            return [self.renderer_suffix]
+        return []
 
     def get_textual_value(self, record):
         if not record:

Reply via email to