From a90bcacfe90b9a3114be66e6a7b1fbd832bdc2aa Mon Sep 17 00:00:00 2001 From: souliane Date: Fri, 21 Aug 2015 14:17:41 +0200 Subject: [PATCH] tools, frontends (xmlui): allow to select a tab when adding it --- frontends/src/primitivus/xmlui.py | 4 ++-- frontends/src/tools/xmlui.py | 3 ++- src/tools/xml_tools.py | 33 ++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/frontends/src/primitivus/xmlui.py b/frontends/src/primitivus/xmlui.py index e4cf44b7..ccc54310 100644 --- a/frontends/src/primitivus/xmlui.py +++ b/frontends/src/primitivus/xmlui.py @@ -287,9 +287,9 @@ class PrimitivusTabsContainer(xmlui.TabsContainer, sat_widgets.TabsContainer): def _xmluiAppend(self, widget): self.body.append(widget) - def _xmluiAddTab(self, label): + def _xmluiAddTab(self, label, selected): tab = PrimitivusVerticalContainer(None) - self.addTab(label, tab) + self.addTab(label, tab, selected) return tab diff --git a/frontends/src/tools/xmlui.py b/frontends/src/tools/xmlui.py index 944ea3c8..9c6420c5 100644 --- a/frontends/src/tools/xmlui.py +++ b/frontends/src/tools/xmlui.py @@ -374,12 +374,13 @@ class XMLUIPanel(XMLUIBase): elif node.nodeName == 'tab': name = node.getAttribute('name') label = node.getAttribute('label') + selected = C.bool(node.getAttribute('selected') or C.BOOL_FALSE) if not name or not isinstance(data, TabsContainer): raise InvalidXMLUI if self.type == 'param': self._current_category = name #XXX: awful hack because params need category and we don't keep parent tab_cont = data - new_tab = tab_cont._xmluiAddTab(label or name) + new_tab = tab_cont._xmluiAddTab(label or name, selected) self._parseChilds(new_tab, node, ('widget', 'container')) elif node.nodeName == 'row': diff --git a/src/tools/xml_tools.py b/src/tools/xml_tools.py index d02aacf8..b9e3744a 100644 --- a/src/tools/xml_tools.py +++ b/src/tools/xml_tools.py @@ -381,12 +381,28 @@ class TabElement(Element): """ Used by TabsContainer to give name and label to tabs.""" type = 'tab' - def __init__(self, parent, name, label): + def __init__(self, parent, name, label, selected=False): + """ + + @param parent (TabsContainer): parent container + @param name (unicode): tab name + @param label (unicode): tab label + @param selected (bool): set to True to select this tab + """ if not isinstance(parent, TabsContainer): raise exceptions.DataError(_("TabElement must be a child of TabsContainer")) super(TabElement, self).__init__(parent.xmlui, parent) self.elem.setAttribute('name', name) self.elem.setAttribute('label', label) + if selected: + self.setSelected(selected) + + def setSelected(self, selected=False): + """Set the tab selected. + + @param selected (bool): set to True to select this tab + """ + self.elem.setAttribute('selected', 'true' if selected else 'false') class FieldBackElement(Element): @@ -543,13 +559,20 @@ class PairsContainer(Container): class TabsContainer(Container): type = "tabs" - def addTab(self, name, label=None, container=VerticalContainer): - """Add a tab""" + def addTab(self, name, label=None, selected=None, container=VerticalContainer): + """Add a tab. + + @param name (unicode): tab name + @param label (unicode): tab label + @param selected (bool): set to True to select this tab + @param container (class): container class, inheriting from Container + @return: the container for the new tab + """ if not label: label = name - tab_elt = TabElement(self, name, label) + tab_elt = TabElement(self, name, label, selected) new_container = container(self.xmlui, tab_elt) - self.xmlui.changeContainer(new_container) + return self.xmlui.changeContainer(new_container) def end(self): """ Called when we have finished tabs