From 25050ae683510e5bec744de2e98760d6476ab081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 18 Mar 2015 11:09:31 +0100 Subject: [PATCH] Hide columns containing always the same value The domain inversion is used on the screen domain combined with the tab domain to find columns that has always the same value. issue4552 review10181002 --- CHANGELOG | 1 + tryton/common/__init__.py | 2 +- tryton/common/domain_inversion.py | 19 +++++++++++++++ tryton/gui/window/view_form/model/field.py | 8 +++---- tryton/gui/window/view_form/view/list.py | 28 +++++++++++++++++++--- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 288bc69b0a..bb9c5ee2a6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Hide columns containing always the same value * Remove Tabs Position option * Manage product attribute * Remove float_time widget diff --git a/tryton/common/__init__.py b/tryton/common/__init__.py index 028075a8d5..5dad4afcf3 100644 --- a/tryton/common/__init__.py +++ b/tryton/common/__init__.py @@ -3,6 +3,6 @@ from common import * from datetime_strftime import * from domain_inversion import domain_inversion, eval_domain, localize_domain, \ - merge, inverse_leaf, concat, simplify + merge, inverse_leaf, concat, simplify, unique_value from environment import EvalEnvironment import timedelta diff --git a/tryton/common/domain_inversion.py b/tryton/common/domain_inversion.py index f03ae3d06c..306a4e0c8a 100644 --- a/tryton/common/domain_inversion.py +++ b/tryton/common/domain_inversion.py @@ -193,6 +193,16 @@ def concat(*domains, **kwargs): return simplify(merge(result)) +def unique_value(domain): + "Return if unique, the field and the value" + if (isinstance(domain, list) + and len(domain) == 1 + and domain[0][1] == '='): + return True, domain[0][1], domain[0][2] + else: + return False, None, None + + def parse(domain): if is_leaf(domain): return domain @@ -481,6 +491,15 @@ def test_concat(): 'OR', [['a', '=', 1]], [['b', '=', 2]]] +def test_unique_value(): + domain = [['a', '=', 1]] + assert unique_value(domain) == (True, '=', 1) + domain = [['a', '!=', 1]] + assert unique_value(domain)[0] == False + domain = [['a', '=', 1], ['a', '=', 2]] + assert unique_value(domain)[0] == False + + def test_evaldomain(): domain = [['x', '>', 5]] assert eval_domain(domain, {'x': 6}) diff --git a/tryton/gui/window/view_form/model/field.py b/tryton/gui/window/view_form/model/field.py index 53d6992804..0244d8be1c 100644 --- a/tryton/gui/window/view_form/model/field.py +++ b/tryton/gui/window/view_form/model/field.py @@ -6,7 +6,7 @@ import tempfile import locale from tryton.common import \ domain_inversion, eval_domain, localize_domain, \ - merge, inverse_leaf, concat, simplify, EvalEnvironment + merge, inverse_leaf, concat, simplify, unique_value, EvalEnvironment import tryton.common as common import datetime import decimal @@ -87,14 +87,12 @@ class Field(object): elif domain == [('id', '=', None)]: res = False else: - if (isinstance(domain, list) - and len(domain) == 1 - and domain[0][1] == '='): + unique, leftpart, value = unique_value(domain) + if unique: # If the inverted domain is so constraint that only one value # is possible we should use it. But we must also pay attention # to the fact that the original domain might be a 'OR' domain # and thus not preventing the modification of fields. - leftpart, _, value = domain[0][:3] if value is False: # XXX to remove once server domains are fixed value = None diff --git a/tryton/gui/window/view_form/view/list.py b/tryton/gui/window/view_form/view/list.py index b1738684e7..cd650eafc8 100644 --- a/tryton/gui/window/view_form/view/list.py +++ b/tryton/gui/window/view_form/view/list.py @@ -18,6 +18,7 @@ from tryton.common.cellrenderertoggle import CellRendererToggle from tryton.gui.window import Window from tryton.common.popup_menu import populate from tryton.common import RPCExecute, RPCException, node_attributes, Tooltips +from tryton.common import domain_inversion, simplify, unique_value import tryton.common as common from . import View from .list_gtk.editabletree import EditableTreeView, TreeView @@ -389,9 +390,6 @@ class ViewTree(View): and not self.children_field and field.attrs.get('sortable', True)): column.connect('clicked', self.sort_model) - column.set_visible(not node_attrs.get('tree_invisible', False)) - if name == self.screen.exclude_field: - column.set_visible(False) self.treeview.append_column(column) @@ -1086,6 +1084,30 @@ class ViewTree(View): self.set_state() self.update_sum() + # Set column visibility depending on attributes and domain + domain = [] + if self.screen.domain: + domain.append(self.screen.domain) + tab_domain = self.screen.screen_container.get_tab_domain() + if tab_domain: + domain.append(tab_domain) + domain = simplify(domain) + for column in self.treeview.get_columns(): + name = column.name + if not name: + continue + widget = self.get_column_widget(column) + if widget.attrs.get('tree_invisible', False): + column.set_visible(False) + elif name == self.screen.exclude_field: + column.set_visible(False) + else: + inv_domain = domain_inversion(domain, name) + if not isinstance(inv_domain, bool): + inv_domain = simplify(inv_domain) + unique, _, _ = unique_value(inv_domain) + column.set_visible(not unique) + def set_state(self): record = self.screen.current_record if record: