changeset 466d79cc48fb in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=466d79cc48fb
description:
        Add optional column on tree view

        issue10989
        review374321002
diffstat:

 CHANGELOG                                    |   1 +
 tryton/common/popup_menu.py                  |  17 ++++++++
 tryton/gui/window/form.py                    |  17 +-------
 tryton/gui/window/view_form/screen/screen.py |   1 +
 tryton/gui/window/view_form/view/list.py     |  56 +++++++++++++++++++++++++++-
 5 files changed, 75 insertions(+), 17 deletions(-)

diffs (184 lines):

diff -r e4a532dba812 -r 466d79cc48fb CHANGELOG
--- a/CHANGELOG Thu Feb 03 23:05:47 2022 +0100
+++ b/CHANGELOG Thu Feb 03 23:13:27 2022 +0100
@@ -1,3 +1,4 @@
+* Manage optional column
 * Default CSV encoding to UTF8 with BOM
 * Display the number of selected records
 * Humanize the count result
diff -r e4a532dba812 -r 466d79cc48fb tryton/common/popup_menu.py
--- a/tryton/common/popup_menu.py       Thu Feb 03 23:05:47 2022 +0100
+++ b/tryton/common/popup_menu.py       Thu Feb 03 23:13:27 2022 +0100
@@ -143,3 +143,20 @@
         action_menu.append(email_item)
         email_item.connect('activate', email, toolbar)
     menu.show_all()
+
+
+def popup(menu, widget):
+    def menu_position(menu, x, y, user_data):
+        widget_allocation = widget.get_allocation()
+        x, y = widget.get_window().get_root_coords(
+            widget_allocation.x, widget_allocation.y)
+        return (x, y + widget_allocation.height, False)
+    menu.show_all()
+    if hasattr(menu, 'popup_at_widget'):
+        menu.popup_at_widget(
+            widget, Gdk.Gravity.SOUTH_WEST, Gdk.Gravity.NORTH_WEST,
+            Gtk.get_current_event())
+    else:
+        menu.popup(
+            None, None, menu_position, None, 0,
+            Gtk.get_current_event_time())
diff -r e4a532dba812 -r 466d79cc48fb tryton/gui/window/form.py
--- a/tryton/gui/window/form.py Thu Feb 03 23:05:47 2022 +0100
+++ b/tryton/gui/window/form.py Thu Feb 03 23:13:27 2022 +0100
@@ -17,6 +17,7 @@
 from tryton.common import (
     RPCException, RPCExecute, message, sur, sur_3b, timezoned_date)
 from tryton.common.common import selection as selection_
+from tryton.common.popup_menu import popup
 from tryton.common.underline import set_underline
 from tryton.gui import Main
 from tryton.gui.window import Window
@@ -587,21 +588,7 @@
         if not widget.props.active:
             menu.popdown()
             return
-
-        def menu_position(menu, x, y, user_data):
-            widget_allocation = widget.get_allocation()
-            x, y = widget.get_window().get_root_coords(
-                widget_allocation.x, widget_allocation.y)
-            return (x, y + widget_allocation.height, False)
-        menu.show_all()
-        if hasattr(menu, 'popup_at_widget'):
-            menu.popup_at_widget(
-                widget, Gdk.Gravity.SOUTH_WEST, Gdk.Gravity.NORTH_WEST,
-                Gtk.get_current_event())
-        else:
-            menu.popup(
-                None, None, menu_position, None, 0,
-                Gtk.get_current_event_time())
+        popup(menu, widget)
 
     def record_message(self, position, size, max_size, record_id):
         name = str(position) if position else '_'
diff -r e4a532dba812 -r 466d79cc48fb 
tryton/gui/window/view_form/screen/screen.py
--- a/tryton/gui/window/view_form/screen/screen.py      Thu Feb 03 23:05:47 
2022 +0100
+++ b/tryton/gui/window/view_form/screen/screen.py      Thu Feb 03 23:13:27 
2022 +0100
@@ -37,6 +37,7 @@
     # Width of tree columns per model
     # It is shared with all connection but it is the price for speed.
     tree_column_width = collections.defaultdict(lambda: {})
+    tree_column_optional = {}
 
     def __init__(self, model_name, **attributes):
         context = attributes.get('context', {})
diff -r e4a532dba812 -r 466d79cc48fb tryton/gui/window/view_form/view/list.py
--- a/tryton/gui/window/view_form/view/list.py  Thu Feb 03 23:05:47 2022 +0100
+++ b/tryton/gui/window/view_form/view/list.py  Thu Feb 03 23:13:27 2022 +0100
@@ -14,7 +14,7 @@
     RPCException, RPCExecute, Tooltips, domain_inversion, node_attributes,
     simplify, unique_value)
 from tryton.common.cellrendererbutton import CellRendererButton
-from tryton.common.popup_menu import populate
+from tryton.common.popup_menu import populate, popup
 from tryton.config import CONFIG
 from tryton.gui.window import Window
 from tryton.pyson import PYSONDecoder
@@ -334,6 +334,9 @@
 
         self.view.treeview.append_column(column)
 
+        if 'optional' in attributes:
+            self.view.optionals.append(column)
+
         if 'sum' in attributes:
             text = attributes['sum'] + _(':')
             label, sum_ = Gtk.Label(label=text), Gtk.Label()
@@ -435,6 +438,7 @@
 
     def __init__(self, view_id, screen, xml, children_field):
         self.children_field = children_field
+        self.optionals = []
         self.sum_widgets = []
         self.sum_box = Gtk.HBox()
         self.treeview = None
@@ -485,6 +489,23 @@
 
         self.display()
 
+        if self.optionals:
+            if self.draggable:
+                column = self.treeview.get_columns()[0]
+            else:
+                column = Gtk.TreeViewColumn()
+                column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
+                column.name = None
+                column._type = 'optional'
+                self.treeview.insert_column(column, 0)
+            image = Gtk.Image()
+            image.set_from_pixbuf(common.IconFactory.get_pixbuf('tryton-menu'))
+            image.show()
+            column.set_widget(image)
+            column.set_fixed_width(25)
+            column.set_clickable(True)
+            column.connect('clicked', self.optional_menu)
+
         # Add last column if necessary after display for updated visible
         for column in self.treeview.get_columns():
             if column.get_expand() and column.get_visible():
@@ -496,6 +517,30 @@
             column.set_sizing(Gtk.TreeViewColumnSizing.FIXED)
             self.treeview.append_column(column)
 
+    def optional_menu(self, column):
+        def toggle(menuitem, column):
+            column.set_visible(menuitem.get_active())
+            self.save_optional()
+
+        widget = column.get_widget()
+        menu = Gtk.Menu()
+        for optional in self.optionals:
+            menuitem = Gtk.CheckMenuItem(label=optional.get_title())
+            menuitem.set_active(optional.get_visible())
+            menuitem.connect('toggled', toggle, optional)
+            menu.add(menuitem)
+        popup(menu, widget)
+
+    def save_optional(self):
+        fields = {c.name: not c.get_visible() for c in self.optionals}
+        try:
+            RPCExecute(
+                'model', 'ir.ui.view_tree_optional', 'set_optional',
+                self.view_id, fields)
+        except RPCException:
+            pass
+        self.screen.tree_column_optional[self.view_id] = fields
+
     def get_column_widget(self, column):
         'Return the widget of the column'
         idx = [c for c in self.treeview.get_columns()
@@ -1066,13 +1111,20 @@
             domain.append(tab_domain)
         domain = simplify(domain)
         decoder = PYSONDecoder(self.screen.context)
+        tree_column_optional = self.screen.tree_column_optional.get(
+            self.view_id, {})
         for column in self.treeview.get_columns():
             name = column.name
             if not name:
                 continue
             widget = self.get_column_widget(column)
             widget.set_editable()
-            if decoder.decode(widget.attrs.get('tree_invisible', '0')):
+            if column.name in tree_column_optional:
+                optional = tree_column_optional[column.name]
+            else:
+                optional = bool(int(widget.attrs.get('optional', '0')))
+            invisible = decoder.decode(widget.attrs.get('tree_invisible', '0'))
+            if invisible or optional:
                 column.set_visible(False)
             elif name == self.screen.exclude_field:
                 column.set_visible(False)

Reply via email to